gtdc/control_tab.c

112 lines
2.6 KiB
C
Raw Normal View History

2008-03-16 00:16:41 +01:00
#include <unistd.h>
#include <netinet/in.h>
2008-03-15 23:18:09 +01:00
#include <gtk/gtk.h>
2008-03-16 00:16:41 +01:00
#include <errno.h>
2008-03-15 23:18:09 +01:00
2008-03-16 00:16:41 +01:00
#include "tcpsocket.h"
#include "sockaddr.h"
2008-03-15 23:18:09 +01:00
2008-03-16 00:39:37 +01:00
int tdc_parse_data(int fd);
void tdc_flush_vars(void);
void tdc_send_hello(int fd);
2008-03-16 00:16:41 +01:00
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;
2008-03-15 23:18:09 +01:00
2008-03-16 00:16:41 +01:00
static void update_connect_button(void)
{
if (connect_status == STAT_DISCONNECTED)
gtk_button_set_label(GTK_BUTTON(button_connect), "Connect");
2008-03-15 23:18:09 +01:00
else
2008-03-16 00:16:41 +01:00
gtk_button_set_label(GTK_BUTTON(button_connect), "Disconnect");
}
static void read_cb(gpointer data, gint source, GdkInputCondition condition)
{
2008-03-16 00:39:37 +01:00
if (tdc_parse_data(source) <= 0) {
2008-03-16 00:16:41 +01:00
g_warning("gtdc:read_cb:read:%s", g_strerror(errno));
connect_status = STAT_DISCONNECTED;
update_connect_button();
gdk_input_remove(socket_tag);
2008-03-16 00:39:37 +01:00
close(source);
2008-03-16 00:16:41 +01:00
}
}
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);
2008-03-16 00:39:37 +01:00
tdc_flush_vars();
tdc_send_hello(source);
2008-03-16 00:16:41 +01:00
}
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();
2008-03-15 23:18:09 +01:00
}
gint control_tab_init(GtkNotebook *notebook)
{
GtkWidget *table = gtk_table_new(10, 10, FALSE);
gtk_container_set_border_width(GTK_CONTAINER(table), 10);
2008-03-16 00:16:41 +01:00
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);
2008-03-15 23:18:09 +01:00
2008-03-16 00:39:37 +01:00
/*
* TODO:
* - address field
* - all_variable_refresh_timer
* - graph_variable_refresh_timer
* later:
* - board select
* - connection/protocol details/errors
*/
2008-03-15 23:18:09 +01:00
GtkWidget *label = gtk_label_new(" Control ");
return gtk_notebook_append_page(GTK_NOTEBOOK(notebook), table, label);
}