#include #include #include #include #include "tcpsocket.h" #include "sockaddr.h" int tdc_parse_data(int fd); void tdc_flush_vars(void); void tdc_send_hello(int fd); enum { STAT_DISCONNECTED = 0, STAT_CONNECTING, STAT_CONNECTED }; static GtkWidget *button_connect; static int connect_status = STAT_DISCONNECTED; static gint socket_tag; static int socket_fd; static void update_connect_button(void) { if (connect_status == STAT_DISCONNECTED) gtk_button_set_label(GTK_BUTTON(button_connect), "Connect"); else gtk_button_set_label(GTK_BUTTON(button_connect), "Disconnect"); } static void read_cb(gpointer data, gint source, GdkInputCondition condition) { if (tdc_parse_data(source) <= 0) { g_warning("gtdc:read_cb:read:%s", g_strerror(errno)); connect_status = STAT_DISCONNECTED; update_connect_button(); gdk_input_remove(socket_tag); close(source); } } static void connect_cb(gpointer data, gint source, GdkInputCondition condition) { gdk_input_remove(socket_tag); if (tcp_connect_error(source) < 0) { g_warning("gtdc:connect_cb:tcp_connect_error:%s", g_strerror(errno)); connect_status = STAT_DISCONNECTED; close(socket_fd); } else { connect_status = STAT_CONNECTED; socket_tag = gdk_input_add(socket_fd, GDK_INPUT_READ, read_cb, NULL); tdc_flush_vars(); tdc_send_hello(source); } update_connect_button(); } static void connect_button_cb(GtkWidget *widget, gpointer data) { switch (connect_status) { case STAT_DISCONNECTED: connect_status = STAT_CONNECTING; struct sockaddr_in sa; parse_sockaddr("127.0.0.1:5000", &sa); socket_fd = tcp_connect_nonblock(&sa); socket_tag = gdk_input_add(socket_fd, GDK_INPUT_WRITE, connect_cb, NULL); break; case STAT_CONNECTING: case STAT_CONNECTED: connect_status = STAT_DISCONNECTED; gdk_input_remove(socket_tag); close(socket_fd); break; } update_connect_button(); } gint control_tab_init(GtkNotebook *notebook) { GtkWidget *table = gtk_table_new(10, 10, FALSE); gtk_container_set_border_width(GTK_CONTAINER(table), 10); button_connect = gtk_button_new_with_label("Connect"); gtk_table_attach(GTK_TABLE(table), button_connect, 0, 1, 0, 1, 0, 0, 10, 10); g_signal_connect(G_OBJECT(button_connect), "clicked", G_CALLBACK(connect_button_cb), NULL); /* * TODO: * - address field * - all_variable_refresh_timer * - graph_variable_refresh_timer * later: * - board select * - connection/protocol details/errors */ GtkWidget *label = gtk_label_new(" Control "); return gtk_notebook_append_page(GTK_NOTEBOOK(notebook), table, label); }