Made io and thread sources recursive, so that callbacks may trigger new callbacks

This commit is contained in:
Yevgen Muntyan 2007-05-14 20:08:25 -05:00
parent d422c6e3ee
commit 13822bea3c
2 changed files with 40 additions and 18 deletions

View File

@ -169,31 +169,39 @@ _moo_app_input_get_name (MooAppInput *ch)
static void
commit (MooAppInput *self)
{
_moo_app_input_ref (self);
if (0)
g_print ("%s: commit %c\n%s\n-----\n", G_STRLOC,
self->buffer->str[0], self->buffer->str + 1);
char buf[MAX_BUFFER_SIZE];
GString *freeme = NULL;
char *ptr;
gsize len;
if (!self->buffer->len)
g_warning ("%s: got empty command", G_STRLOC);
else
_moo_app_exec_cmd (moo_app_get_instance (),
self->buffer->str[0],
self->buffer->str + 1,
self->buffer->len - 1);
if (self->buffer->len > MAX_BUFFER_SIZE)
{
g_string_free (self->buffer, TRUE);
g_warning ("%s: got empty command", G_STRLOC);
return;
}
if (self->buffer->len + 1 > MAX_BUFFER_SIZE)
{
freeme = self->buffer;
self->buffer = g_string_new_len (NULL, MAX_BUFFER_SIZE);
ptr = freeme->str;
len = freeme->len;
}
else
{
memcpy (buf, self->buffer->str, self->buffer->len + 1);
ptr = buf;
len = self->buffer->len;
g_string_truncate (self->buffer, 0);
}
_moo_app_input_unref (self);
if (0)
g_print ("%s: commit %c\n%s\n-----\n", G_STRLOC, ptr[0], ptr + 1);
_moo_app_exec_cmd (moo_app_get_instance (), ptr[0], ptr + 1, len - 1);
if (freeme)
g_string_free (freeme, TRUE);
}
@ -274,7 +282,7 @@ listener_main (ListenerInfo *info)
}
}
g_message ("%s: client connected", G_STRLOC);
_moo_message_async ("%s: client connected", G_STRLOC);
while (ReadFile (input, &c, 1, &bytes_read, NULL))
{
@ -321,7 +329,11 @@ event_callback (GList *events,
if (c != 0)
g_string_append_c (chan->buffer, c);
else
{
gdk_threads_enter ();
commit (chan);
gdk_threads_leave ();
}
events = events->next;
}
@ -560,6 +572,8 @@ get_pipe_dir (const char *pipe_basename)
gboolean
_moo_app_input_start (MooAppInput *ch)
{
GSource *source;
g_return_val_if_fail (!ch->ready, FALSE);
if (!ch->pipe_dir)
@ -606,6 +620,9 @@ _moo_app_input_start (MooAppInput *ch)
(GIOFunc) read_input,
ch);
source = g_main_context_find_source_by_id (NULL, ch->io_watch);
g_source_set_can_recurse (source, TRUE);
ch->ready = TRUE;
return TRUE;
}

View File

@ -77,7 +77,7 @@ invoke_callback (gpointer id,
GList *l;
QueueClient *client;
g_message ("processing events for id %u", GPOINTER_TO_UINT (id));
_moo_message ("processing events for id %u", GPOINTER_TO_UINT (id));
client = get_event_client (GPOINTER_TO_UINT (id));
if (client)
@ -162,6 +162,7 @@ init_queue (void)
if (!queue)
{
int fds[2];
GSource *source;
if (pipe (fds) != 0)
{
@ -182,7 +183,11 @@ init_queue (void)
queue->io = g_io_channel_unix_new (queue->pipe_out);
#endif
g_io_add_watch (queue->io, G_IO_IN, (GIOFunc) got_data, NULL);
source = g_io_create_watch (queue->io, G_IO_IN);
g_source_set_callback (source, (GSourceFunc) got_data, NULL, NULL);
g_source_set_can_recurse (source, TRUE);
g_source_attach (source, NULL);
queue->data = NULL;
}