Commit c82a582a authored by rhp's avatar rhp
Browse files

..

parent ce7c53bf
SUBDIRS=uislave
INCLUDES=@METACITY_CFLAGS@ -DMETACITY_LIBEXECDIR=\"$(libexecdir)\" -DHOST_ALIAS=\"@HOST_ALIAS@\"
INCLUDES=@METACITY_CFLAGS@ -DMETACITY_LIBEXECDIR=\"$(libexecdir)\" -DHOST_ALIAS=\"@HOST_ALIAS@\" -DMETACITY_COMPILE=1
## convenience libraries lead to annoying slow compiles, plus we want
## to be extra-hacky by using #ifdef METACITY_COMPILE, ergo cheesy
## hack that follows.
copied_sources= \
messagequeue.h \
messagequeue.c
copied_sources_deps= \
$(srcdir)/uislave/messagequeue.h \
$(srcdir)/uislave/messagequeue.c
BUILT_SOURCES=$(copied_sources)
$(copied_sources): $(copied_sources_deps)
for I in $(copied_sources); do \
rm -f $$I ; \
cp $(srcdir)/uislave/$$I . ; \
done
metacity_SOURCES= \
api.c \
......@@ -27,7 +45,8 @@ metacity_SOURCES= \
util.c \
util.h \
window.c \
window.h
window.h \
$(copied_sources)
bin_PROGRAMS=metacity
......
#! /bin/bash
Xnest :1 -scrns 2 -geometry 270x270 &
if test -z "$SCREENS"; then
SCREENS=2
fi
Xnest :1 -scrns $SCREENS -geometry 270x270 &
METACITY_UISLAVE_DIR=./uislave DISPLAY=:1 unst libtool --mode=execute gdb ./metacity
killall Xnest
......@@ -25,6 +25,7 @@
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
typedef enum
{
......@@ -34,9 +35,6 @@ typedef enum
} ReadResult;
static void respawn_child (MetaUISlave *uislave);
static gboolean output_callback (GIOChannel *source,
GIOCondition condition,
gpointer data);
static gboolean error_callback (GIOChannel *source,
GIOCondition condition,
gpointer data);
......@@ -149,8 +147,6 @@ respawn_child (MetaUISlave *uislave)
uislave->child_pid = child_pid;
uislave->in_pipe = inpipe;
uislave->err_pipe = errpipe;
uislave->out_poll.fd = outpipe;
uislave->out_poll.events = G_IO_IN;
uislave->err_channel = g_io_channel_unix_new (errpipe);
......@@ -159,6 +155,11 @@ respawn_child (MetaUISlave *uislave)
error_callback,
uislave);
uislave->out_poll.fd = outpipe;
uislave->out_poll.events = G_IO_IN;
g_source_add_poll ((GSource*)uislave, &uislave->out_poll);
meta_verbose ("Spawned UI slave with PID %d\n", uislave->child_pid);
}
else
......@@ -182,42 +183,59 @@ append_pending (MetaUISlave *uislave)
needed = MIN (needed, uislave->buf->len);
/* Move data from buf to current_message */
g_string_append_len (uislave->current_message,
uislave->buf->str,
needed);
g_string_erase (uislave->buf,
0, needed);
}
if (needed > 0)
{
meta_verbose ("Moving %d bytes from buffer to current incomplete message\n",
needed);
g_string_append_len (uislave->current_message,
uislave->buf->str,
needed);
g_string_erase (uislave->buf,
0, needed);
}
g_assert (uislave->current_message->len <= uislave->current_required_len);
static gboolean
output_callback (GIOChannel *source,
GIOCondition condition,
gpointer data)
{
/* Read messages from slave */
MetaUISlave *uislave;
ReadResult res;
if (uislave->current_required_len > 0 &&
uislave->current_message->len == uislave->current_required_len)
{
MetaMessage *message;
MetaMessageFooter *footer;
uislave = data;
message = g_new (MetaMessage, 1);
memcpy (message,
uislave->current_message->str, uislave->current_message->len);
res = read_data (uislave->buf, uislave->out_pipe);
if (message->header.length != uislave->current_required_len)
meta_bug ("Message length changed?\n");
switch (res)
if (message->header.serial != uislave->last_serial)
meta_bug ("Message serial changed?\n");
footer = META_MESSAGE_FOOTER (message);
if (footer->checksum == META_MESSAGE_CHECKSUM (message))
{
g_queue_push_tail (uislave->queue, message);
meta_verbose ("Added %d-byte message serial %d to queue\n",
uislave->current_message->len, message->header.serial);
}
else
{
meta_bug ("Bad checksum %d on %d-byte message from UI slave\n",
footer->checksum, uislave->current_message->len);
}
uislave->current_required_len = 0;
g_string_truncate (uislave->current_message, 0);
}
else if (uislave->current_required_len > 0)
{
case READ_OK:
meta_verbose ("Read data from slave, %d bytes in buffer\n",
uislave->buf->len);
break;
case READ_EOF:
meta_verbose ("EOF reading stdout from slave process\n");
break;
case READ_FAILED:
/* read_data printed the error */
break;
meta_verbose ("Storing %d bytes of incomplete message\n",
uislave->current_message->len);
}
return TRUE;
}
static gboolean
......@@ -232,7 +250,7 @@ error_callback (GIOChannel *source,
int n;
uislave = data;
/* Classic loop from Stevens */
n = read (uislave->err_pipe, buf, BUFSIZE);
if (n > 0)
......@@ -250,110 +268,147 @@ error_callback (GIOChannel *source,
static void
mq_queue_messages (MetaUISlave *uislave)
{
if (uislave->buf->len == 0)
return;
if (uislave->current_message->len > 0)
{
/* We had a pending message. */
append_pending (uislave);
}
else if (uislave->buf->len > META_MESSAGE_ESCAPE_LEN)
{
if (uislave->out_poll.revents & G_IO_IN)
{
/* See if we can start a current message */
const char *p;
int esc_pos;
const char *esc;
MetaMessageHeader header;
/* note that the string from the UI slave includes the nul byte */
esc = META_MESSAGE_ESCAPE;
ReadResult res;
res = read_data (uislave->buf, uislave->out_poll.fd);
esc_pos = 0;
p = uislave->buf->str;
while (p != (uislave->buf->str + uislave->buf->len) &&
esc_pos < META_MESSAGE_ESCAPE_LEN)
switch (res)
{
if (*p != esc[esc_pos])
esc_pos = 0;
else
++esc_pos;
++p;
case READ_OK:
meta_verbose ("Read data from slave, %d bytes in buffer\n",
uislave->buf->len);
break;
case READ_EOF:
meta_verbose ("EOF reading stdout from slave process\n");
break;
case READ_FAILED:
/* read_data printed the error */
break;
}
if (esc_pos == META_MESSAGE_ESCAPE_LEN)
}
while (uislave->buf->len > 0)
{
if (uislave->current_required_len > 0)
{
/* We found an entire escape sequence; can safely toss
* out the entire buffer before it
*/
int ignored;
ignored = p - uislave->buf->str;
ignored -= META_MESSAGE_ESCAPE_LEN;
g_assert (ignored >= 0);
if (ignored > 0)
{
meta_verbose ("Ignoring %d bytes from UI slave\n",
ignored);
g_string_erase (uislave->buf, 0, ignored);
}
/* We had a pending message. */
append_pending (uislave);
}
else if (esc_pos == 0)
else if (uislave->buf->len > META_MESSAGE_ESCAPE_LEN)
{
/* End of buffer doesn't begin an escape sequence;
* toss out entire buffer.
*/
meta_verbose ("Ignoring %d bytes from UI slave\n",
/* See if we can start a current message */
const char *p;
int esc_pos;
const char *esc;
MetaMessageHeader header;
g_assert (uislave->current_required_len == 0);
g_assert (uislave->current_message->len == 0);
meta_verbose ("Scanning for escape sequence in %d bytes\n",
uislave->buf->len);
g_string_truncate (uislave->buf, 0);
}
if (uislave->buf->len < (META_MESSAGE_ESCAPE_LEN + sizeof (MetaMessageHeader)))
return; /* Not enough data yet. */
memcpy (&header, uislave->buf->str + META_MESSAGE_ESCAPE_LEN, sizeof (MetaMessageHeader));
/* note that the data from the UI slave includes the nul byte */
esc = META_MESSAGE_ESCAPE;
esc_pos = -1;
p = uislave->buf->str;
while (p != (uislave->buf->str + uislave->buf->len) &&
esc_pos < META_MESSAGE_ESCAPE_LEN)
{
++esc_pos;
if (*p != esc[esc_pos])
esc_pos = -1;
/* Length includes the header even though it's in the header. */
meta_verbose ("Read header code: %d length: %d from UI slave\n",
header.message_code, header.length);
++p;
}
uislave->current_required_len = header.length;
g_string_erase (uislave->buf, 0, META_MESSAGE_ESCAPE_LEN);
if (esc_pos == META_MESSAGE_ESCAPE_LEN)
{
/* We found an entire escape sequence; can safely toss
* out the entire buffer before it
*/
int ignored;
ignored = p - uislave->buf->str;
ignored -= META_MESSAGE_ESCAPE_LEN;
g_assert (ignored >= 0);
if (ignored > 0)
{
g_string_erase (uislave->buf, 0, ignored);
meta_verbose ("Ignoring %d bytes before escape, new buffer len %d\n",
ignored, uislave->buf->len);
}
else
{
g_assert (p == (uislave->buf->str + META_MESSAGE_ESCAPE_LEN));
}
}
else if (esc_pos < 0)
{
/* End of buffer doesn't begin an escape sequence;
* toss out entire buffer.
*/
meta_verbose ("Emptying %d-byte buffer not containing escape sequence\n",
uislave->buf->len);
g_string_truncate (uislave->buf, 0);
goto need_more_data;
}
else
{
meta_verbose ("Buffer ends with partial escape sequence: '%s'\n",
uislave->buf->str + (uislave->buf->len - esc_pos));
goto need_more_data;
}
append_pending (uislave);
}
g_assert (strcmp (uislave->buf->str, META_MESSAGE_ESCAPE) == 0);
if (uislave->buf->len < (META_MESSAGE_ESCAPE_LEN + sizeof (MetaMessageHeader)))
{
meta_verbose ("Buffer has full escape sequence but lacks header\n");
goto need_more_data;
}
g_assert (uislave->current_message->len <= uislave->current_required_len);
g_string_erase (uislave->buf, 0, META_MESSAGE_ESCAPE_LEN);
meta_verbose ("Stripped escape off front of buffer, new buffer len %d\n",
uislave->buf->len);
if (uislave->current_required_len > 0 &&
uislave->current_message->len == uislave->current_required_len)
{
MetaMessage *message;
g_assert (uislave->buf->len >= sizeof (MetaMessageHeader));
memcpy (&header, uislave->buf->str, sizeof (MetaMessageHeader));
message = g_new (MetaMessage, 1);
memcpy (message,
uislave->current_message->str, uislave->current_message->len);
/* Length includes the header even though it's in the header. */
meta_verbose ("Read header, code: %d length: %d serial: %d\n",
header.message_code, header.length, header.serial);
g_queue_push_tail (uislave->queue, message);
if (header.serial != uislave->last_serial + 1)
meta_bug ("Wrong message serial number %d from UI slave!\n", header.serial);
meta_verbose ("Added %d-byte message to queue\n",
uislave->current_message->len);
uislave->current_required_len = 0;
g_string_truncate (uislave->current_message, 0);
uislave->last_serial = header.serial;
uislave->current_required_len = header.length;
append_pending (uislave);
}
else
goto need_more_data;
}
need_more_data:
return;
}
static gboolean
mq_messages_pending (MetaUISlave *uislave)
{
return uislave->queue->length > 0 || uislave->buf->len > 0;
return uislave->queue->length > 0 ||
uislave->buf->len > 0 ||
uislave->current_message->len > 0;
}
static gboolean
......@@ -364,6 +419,8 @@ mq_prepare (GSource *source, gint *timeout)
uislave = (MetaUISlave*) source;
*timeout = -1;
mq_queue_messages (uislave);
return mq_messages_pending (uislave);
}
......@@ -372,8 +429,11 @@ static gboolean
mq_check (GSource *source)
{
MetaUISlave *uislave;
uislave = (MetaUISlave*) source;
mq_queue_messages (uislave);
uislave->out_poll.revents = 0;
return mq_messages_pending (uislave);
}
......@@ -384,8 +444,6 @@ mq_dispatch (GSource *source, GSourceFunc callback, gpointer user_data)
MetaUISlave *uislave;
uislave = (MetaUISlave*) source;
mq_queue_messages (uislave);
if (uislave->queue->length > 0)
{
......@@ -411,20 +469,17 @@ mq_dispatch (GSource *source, GSourceFunc callback, gpointer user_data)
static void
kill_child (MetaUISlave *uislave)
{
if (uislave->outwatch != 0)
g_source_remove (uislave->outwatch);
if (uislave->errwatch != 0)
g_source_remove (uislave->errwatch);
if (uislave->out_channel)
g_io_channel_unref (uislave->out_channel);
g_source_remove (uislave->errwatch);
if (uislave->err_channel)
g_io_channel_unref (uislave->err_channel);
if (uislave->out_pipe >= 0)
close (uislave->out_pipe);
if (uislave->out_poll.fd >= 0)
{
g_source_remove_poll ((GSource*)uislave, &uislave->out_poll);
close (uislave->out_poll.fd);
}
if (uislave->in_pipe >= 0)
close (uislave->in_pipe);
......@@ -467,14 +522,13 @@ reset_vals (MetaUISlave *uislave)
{
uislave->child_pid = 0;
uislave->in_pipe = -1;
uislave->out_pipe = -1;
uislave->err_pipe = -1;
uislave->out_poll.fd = -1;
uislave->no_respawn = FALSE;
uislave->out_channel = NULL;
uislave->err_channel = NULL;
uislave->outwatch = 0;
uislave->errwatch = 0;
uislave->current_required_len = 0;
uislave->last_serial = -1;
}
static void
......@@ -503,7 +557,7 @@ static ReadResult
read_data (GString *str,
gint fd)
{
#define BUFSIZE 16
#define BUFSIZE 4000
gint bytes;
gchar buf[BUFSIZE];
......
......@@ -45,6 +45,7 @@ struct _MetaUISlave
GString *buf;
GString *current_message;
int current_required_len;
int last_serial;
/* if we determine that our available slave is hosed,
* set this bit.
*/
......
......@@ -4,7 +4,9 @@ INCLUDES=@UISLAVE_CFLAGS@ -DHOST_ALIAS=\"@HOST_ALIAS@\"
metacity_uislave_SOURCES = \
main.c \
messages.c \
messages.h
messages.h \
messagequeue.c \
messagequeue.h
libexec_PROGRAMS=metacity-uislave
......
......@@ -23,6 +23,7 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <gtk/gtk.h>
......@@ -50,13 +51,15 @@ main (int argc, char **argv)
/* report our nature to the window manager */
meta_message_send_check ();
write (1, "abcdefghijklmnopqrstuvwxyz", 27);
#if 1
/* Try breaking message queue system. */
i = 0;
while (i < 100)
while (i < 1500)
{
meta_message_send_check ();
write (1, "abcdefghijklmnopqrstuvwxyz", 27);
if (g_random_boolean ())
{
int j;
......
/* Metacity UI slave main() */
/*
* Copyright (C) 2001 Havoc Pennington
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#ifndef META_UI_MAIN_H
#define META_UI_MAIN_H
void meta_ui_warning (const char *format, ...);
/* FIXME */
#define _(x) x
#endif
/* Metacity IPC message source for main loop */
/*
* Copyright (C) 2001 Havoc Pennington
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#include "messagequeue.h"
MetaMessageQueue*
meta_message_queue_new (int fd,
MetaMessageQueueFunc func,
gpointer data)
{
}
void
meta_message_queue_free (MetaMessageQueue *mq)
{
}
/* Metacity IPC message source for main loop */
/*
* Copyright (C) 2001 Havoc Pennington
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
#ifndef META_MESSAGE_QUEUE_H
#define META_MESSAGE_QUEUE_H
#include <glib.h>