📄 callbacks.c
字号:
#ifdef HAVE_CONFIG_H# include <config.h>#endif#include <time.h>#include <sys/types.h>#include <gtk/gtk.h>#include "callbacks.h"#include "windows.h"#include "windows.h"#include "support.h"#include "winutils.h"#include "gcomm.h"#include "confirmer.h"#include "buffers.h"#include "serial.h"#include "transfers.h"#include "dialog.h"#define GA(t) gtk_toggle_button_get_active(t)voidon_file_activate(GtkMenuItem *menuitem, gpointer user_data){}gbooleanon_mainwindow_delete_event (GtkWidget *widget, GdkEvent *event, gpointer user_data){ return connectionActive && !get_confirmation(quitConfirm, FALSE) ;}voidon_quit(GtkMenuItem *item, gpointer data){ if( !connectionActive || get_confirmation(quitConfirm, FALSE) ) gtk_widget_destroy(mainwindow) ;}voidsensitiveCB (GtkToggleButton *togglebutton, gpointer user_data){ gtk_widget_set_sensitive(GTK_WIDGET(user_data), gtk_toggle_button_get_active(togglebutton)) ;}voidconnToggle (GtkToggleButton *togglebutton, gpointer user_data){ if( gtk_toggle_button_get_active(togglebutton) ) gtk_widget_show( GTK_WIDGET( user_data ) ) ; else gtk_widget_hide( GTK_WIDGET( user_data ) ) ;}voidconnTypeCB (GtkToggleButton *togglebutton, gpointer user_data){ if( !gtk_toggle_button_get_active(togglebutton) ) return ; connectionType = (int) user_data ;}voidconnectCB(GtkButton *button, gpointer user_data){ if( !connectionActive ) MakeConnection() ;}voiddisconnectCB(GtkButton *button, gpointer user_data){ BreakConnection() ;}voidclearScreenCB(GtkButton *button, gpointer user_data){ /* TODO: use termbuf (translation)? */ if( clearScreen != NULL && *clearScreen != '\0' ) { append_buffer( terminalBuffer, clearScreen, strlen(clearScreen), 1 ) ; (void) terminalOutput() ; }}voidsendBrkCB (GtkButton *button, gpointer user_data){ printf("TODO: break\n") ;}voidlocalEchoCB (GtkToggleButton *togglebutton, gpointer user_data){ localEcho = (bool) gtk_toggle_button_get_active(togglebutton) ; TermEcho() ;} /* User selects one of 8n1, 7e1, 7o1. Change other widgets * accordingly. Calling this function will result in three * calls to SerialPortControl() when one would have sufficed, * but it won't kill us. */voidprotocolCB (GtkToggleButton *togglebutton, gpointer user_data){ if( !gtk_toggle_button_get_active(togglebutton) ) return ; switch( (int) user_data ) { case Proto8n1: gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(noneBut), True ) ; gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(bit8But), True ) ; gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(stripBut), False ) ; break ; case Proto7e1: gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(evenBut), True ) ; gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(bit7But), True ) ; gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(stripBut), True ) ; break ; case Proto7o1: gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(oddBut), True ) ; gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(bit7But), True ) ; gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(stripBut), True ) ; break ; } gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(stop1But), True ) ;} /* One of the individual attribute widgets have been changed; * see if this affects the 8n1/7e1/7o1 widgets. Changing one * of those widgets will invoke the callback above, but it * shouldn't propagate any further. */static voidreEvaluateProtocol(){ if( byteSize == 8 && parityType == ParityNone && stopBits == 1 ) gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(p8n1But), True ) ; else if( byteSize == 7 && parityType == ParityEven && stopBits == 1 ) gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(p7e1But), True ) ; else if( byteSize == 7 && parityType == ParityOdd && stopBits == 1 ) gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(p7o1But), True ) ; else { gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(p8n1But), False ) ; gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(p7e1But), False ) ; gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON(p7o1But), False ) ; }}voidbyteSizeCB (GtkToggleButton *togglebutton, gpointer user_data){ if( !gtk_toggle_button_get_active(togglebutton) ) return ; byteSize = (int) user_data ; reEvaluateProtocol() ; SerialPortControl(baudRate, byteSize, parityType, stopBits, inputStrip);}voidparityCB (GtkToggleButton *togglebutton, gpointer user_data){ if( !gtk_toggle_button_get_active(togglebutton) ) return ; parityType = (int) user_data ; reEvaluateProtocol() ; SerialPortControl(baudRate, byteSize, parityType, stopBits, inputStrip);}extern voidbaudCB (gpointer user_data){ baudRate = (int) user_data ; SerialPortControl(baudRate, byteSize, parityType, stopBits, inputStrip);}extern voidxferTypeCB (gpointer user_data){ xferType = (int) user_data ;}voidstopBitsCB (GtkToggleButton *togglebutton, gpointer user_data){ if( !gtk_toggle_button_get_active(togglebutton) ) return ; stopBits = (int) user_data ; reEvaluateProtocol() ; SerialPortControl(baudRate, byteSize, parityType, stopBits, inputStrip);}voidstripCB (GtkToggleButton *togglebutton, gpointer user_data){ inputStrip = (bool) gtk_toggle_button_get_active(togglebutton) ; SerialPortControl(baudRate, byteSize, parityType, stopBits, inputStrip);}voidflowCB (GtkToggleButton *togglebutton, gpointer user_data){ if( !gtk_toggle_button_get_active(togglebutton) ) return ; flowControl = (int) user_data ;}voidattnCB (GtkToggleButton *togglebutton, gpointer user_data){ if( !gtk_toggle_button_get_active(togglebutton) ) return ; modemAttn = (int) user_data ;}voidxferProtocolCB (GtkToggleButton *togglebutton, gpointer user_data){ if( !gtk_toggle_button_get_active(togglebutton) ) return ; xferProtocol = (int) user_data ;}voidreceiveCB(GtkButton *button, gpointer user_data){ WindowTransferActive(True) ; Download( gtk_entry_get_text(GTK_ENTRY(fileEntry))) ;}voidsendCB(GtkButton *button, gpointer user_data){ WindowTransferActive(True) ; Upload( gtk_entry_get_text(GTK_ENTRY(fileEntry))) ;}voidcancelCB(GtkButton *button, gpointer user_data){ XferCancel() ;}voidserverCB (GtkButton *button, gpointer user_data){ printf("TODO: server\n") ;}voidchdirCB (GtkButton *button, gpointer user_data){ printf("TODO: chdir\n") ;}voidtermiCB( gpointer data, gint fd, GdkInputCondition cond){ int len ; /* Read as much as we can, echo it back to terminal if local * echo is enabled, transmit to output fd if there is a * connection. If can't empty the buffer, turn off input * handler and turn on output handler. */ len = handle_input( fd, &outputBuf, "Terminal read") ; if( ofd < 0 ) { /* drop it on the floor */ clear_buffers() ; return ; } if( len > 0 ) { len = handle_output(ofd, &outputBuf, "Remote output") ; /* if we couldn't write it all, install a callback so we can * write it later. */ if( len > 0 && outputHandler == -1 ) outputHandler = gdk_input_add(ofd, GDK_INPUT_WRITE, outputCB, NULL); } /* If input is full, shut it down */ if( len >= BufLen ) { printf("shut down terminal input\n") ; gdk_input_remove( termiHandler ) ; termiHandler = -1 ; /* TODO: safety device: set a timeout to restore terminal input in * one second. */ }}voidtermoCB( gpointer data, gint fd, GdkInputCondition cond){ int len ; terminalOutput() ; /* Write as much as we can. If buffer is emptied, turn off * output handler. If buffer is non-full, turn on input handler. */ len = terminalOutput() ; /* TODO: if len < 0, that's an error */ /* Buffer empty? Turn output off. */ if( len == 0 ) { gdk_input_remove( termoHandler ) ; termoHandler = -1 ; }} /* Input from remote host. */voidinputCB( gpointer data, gint fd, GdkInputCondition cond){ int len ; /* Read as much as we can. Turn on terminal output handler. * Turn off remote input handler if buffer is full. */ len = handle_input( fd, &inputBuf, "Remote read") ; if( len == 0 && connectionType != ConnectionSerial ) { BreakConnection() ; WindowStatus("Connection closed by remote") ; } else if( len > 0 ) { if( dialogActive ) DialogChars() ; else if( transferActive ) TransferData() ; if( outputType != OutputVerbatim && inputBuf.cnt > 0 ) TranslateOutput() ; len = handle_output(1, terminalBuffer, "Terminal output") ; /* if we couldn't write it all, install a callback so we can * write it later. */ if( len > 0 && termoHandler == -1 ) termoHandler = gdk_input_add(1, GDK_INPUT_WRITE, termoCB, NULL); } /* If input is full, shut it down */ if( len >= BufLen ) { gdk_input_remove( inputHandler ) ; inputHandler = -1 ; /* TODO: safety device: set a timeout to restore remote input in * one second. */ }}voidoutputCB( gpointer data, gint fd, GdkInputCondition cond){ int len ; /* Write as much as we can. If buffer is emptied, turn off * output handler. If buffer is non-full, turn on input handler. */ len = handle_output(fd, &outputBuf, "Transmit") ; /* TODO: if len < 0, that's an error */ if( len < BufLen && termiHandler == -1 ) termiHandler = gdk_input_add( 0, GDK_INPUT_READ, termiCB, NULL ) ; if( len == 0 ) { gdk_input_remove( outputHandler ) ; outputHandler = -1 ; }} /* update the clock */gintWindowClock(gpointer data){static time_t lastUpdate = 0 ; time_t now = time(NULL) ; int ss ; int mm ; int hh ; char timestr[20] ; if( connectionActive && now > lastUpdate ) { ss = now - connectTime0 ; lastUpdate = now ; mm = ss/60 ; hh = mm/60 ; ss %= 60 ; mm %= 60 ; sprintf(timestr, "%d:%2.2d:%2.2d", hh,mm,ss) ; gtk_label_set_text( GTK_LABEL(timeLabel), timestr ) ; } return TRUE ;} /* update the serial port lights */gintSerialWatch(gpointer data){ SerialModemLines() ; return TRUE ;} /* Generic boolean toggle callback */extern voidtoggleCB(GtkToggleButton *togglebutton, gpointer user_data){ int *ptr = (int *)user_data ; *ptr = gtk_toggle_button_get_active(togglebutton) ;}voidfocusCB(GtkWidget *w, gpointer user_data){ gtk_widget_grab_focus(GTK_WIDGET(user_data)) ;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -