diff options
author | Robert Manea <gotmor@gmail.com> | 2009-09-13 02:25:20 +0200 |
---|---|---|
committer | Robert Manea <gotmor@gmail.com> | 2009-09-13 02:25:20 +0200 |
commit | d0330cba2849f716be672da6183724786dfb3b9b (patch) | |
tree | ba1f22951f8aaea6d8f041de054d8572e71f9755 | |
parent | 96f9f888b795bd76e5939011ad0756e32f31e95c (diff) |
added ability to buffer events until a client attached to the socket
-rw-r--r-- | uzbl-core.c | 70 | ||||
-rw-r--r-- | uzbl-core.h | 1 |
2 files changed, 65 insertions, 6 deletions
diff --git a/uzbl-core.c b/uzbl-core.c index 522bf08..9a71c31 100644 --- a/uzbl-core.c +++ b/uzbl-core.c @@ -396,21 +396,63 @@ expand(const char *s, guint recurse) { } void +event_buffer_timeout(guint sec) { + struct itimerval t; + memset(&t, 0, sizeof t); + t.it_value.tv_sec = sec; + t.it_value.tv_usec = 0; + setitimer(ITIMER_REAL, &t, NULL); +} + + +void send_event_socket(GString *msg) { GError *error = NULL; + GString *tmp; GIOStatus ret; gsize len; + guint i=0; if(uzbl.comm.socket_path && uzbl.comm.clientchan && uzbl.comm.clientchan->is_writeable) { - ret = g_io_channel_write_chars (uzbl.comm.clientchan, - msg->str, msg->len, - &len, &error); - if (ret == G_IO_STATUS_ERROR) { - g_warning ("Error sending event to socket: %s", error->message); + + if(uzbl.state.event_buffer) { + event_buffer_timeout(0); + + while(i < uzbl.state.event_buffer->len) { + tmp = g_ptr_array_index(uzbl.state.event_buffer, i++); + ret = g_io_channel_write_chars (uzbl.comm.clientchan, + tmp->str, tmp->len, + &len, &error); + /* is g_ptr_array_free(uzbl.state.event_buffer, TRUE) enough? */ + //g_string_free(tmp, TRUE); + if (ret == G_IO_STATUS_ERROR) { + g_warning ("Error sending event to socket: %s", error->message); + } + g_io_channel_flush(uzbl.comm.clientchan, &error); + } + g_ptr_array_free(uzbl.state.event_buffer, TRUE); + uzbl.state.event_buffer = NULL; } - g_io_channel_flush(uzbl.comm.clientchan, &error); + if(msg) { + ret = g_io_channel_write_chars (uzbl.comm.clientchan, + msg->str, msg->len, + &len, &error); + + if (ret == G_IO_STATUS_ERROR) { + g_warning ("Error sending event to socket: %s", error->message); + } + g_io_channel_flush(uzbl.comm.clientchan, &error); + } + } + /* buffer events until a socket is set and connected + * or a timeout is encountered + */ + else { + if(!uzbl.state.event_buffer) + uzbl.state.event_buffer = g_ptr_array_new(); + g_ptr_array_add(uzbl.state.event_buffer, (gpointer)g_string_new(msg->str)); } } @@ -570,6 +612,13 @@ catch_sigint(int s) { exit(EXIT_SUCCESS); } +void +catch_sigalrm(int s) { + (void) s; + g_ptr_array_free(uzbl.state.event_buffer, TRUE); + uzbl.state.event_buffer = NULL; +} + /* --- CALLBACKS --- */ gboolean @@ -2213,6 +2262,8 @@ control_socket(GIOChannel *chan) { if ((uzbl.comm.clientchan = g_io_channel_unix_new(clientsock))) { g_io_add_watch(uzbl.comm.clientchan, G_IO_IN|G_IO_HUP, (GIOFunc) control_client_socket, uzbl.comm.clientchan); + /* replay buffered events */ + send_event_socket(NULL); } return TRUE; @@ -2971,6 +3022,13 @@ initialize(int argc, char *argv[]) { if(setup_signal(SIGINT, catch_sigint) == SIG_ERR) fprintf(stderr, "uzbl: error hooking SIGINT\n"); + /* Set up the timer for the event buffer */ + if(setup_signal(SIGALRM, catch_sigalrm) == SIG_ERR) { + fprintf(stderr, "uzbl: error hooking SIGALRM\n"); + exit(EXIT_FAILURE); + } + event_buffer_timeout(10); + uzbl.gui.sbar.progress_s = g_strdup("="); //TODO: move these to config.h uzbl.gui.sbar.progress_u = g_strdup("ยท"); uzbl.gui.sbar.progress_w = 10; diff --git a/uzbl-core.h b/uzbl-core.h index 07d307f..5b59c61 100644 --- a/uzbl-core.h +++ b/uzbl-core.h @@ -71,6 +71,7 @@ typedef struct { gchar* keycmd; gchar* searchtx; gboolean verbose; + GPtrArray *event_buffer; } State; |