From e1b2e8f7f88a9359ebb43efd3ff0513287ed7b0c Mon Sep 17 00:00:00 2001 From: "Ira W. Snyder" Date: Thu, 10 Jan 2008 21:49:49 -0800 Subject: [PATCH] Hook into DBus This hooks both TildaWindow and TildaTerminal into DBus so that methods can be called remotely. This makes all of the requested scripting possible, which will be great. This is mainly good for testing. The only functions that are exposed are the Get and Set properties of the GObjects. I wired in the font-size change in the terminal, so it is possible to see it in action. --- Makefile | 29 ++++++++++++++++++++++++++--- tilda-terminal.c | 42 ++++++++++++++++++++++++++++++++++++++++++ tilda-terminal.h | 6 ++++-- tilda-terminal.xml | 11 +++++++++++ tilda-window.c | 24 +++++++++++++++++++++++- tilda-window.xml | 11 +++++++++++ tilda.c | 41 +++++++++++++++++++++++++++++++++++++++++ tilda.h | 12 ++++++++++++ 8 files changed, 170 insertions(+), 6 deletions(-) create mode 100644 tilda-terminal.xml create mode 100644 tilda-window.xml create mode 100644 tilda.h diff --git a/Makefile b/Makefile index 5136211..49c074d 100644 --- a/Makefile +++ b/Makefile @@ -3,8 +3,8 @@ CFLAGS=-ggdb -O1 -pipe GOBJ_CFLAGS=`pkg-config --cflags gobject-2.0` GOBJ_LIBS=`pkg-config --libs gobject-2.0` -ALL_CFLAGS=`pkg-config --cflags gtk+-2.0 vte` -ALL_LIBS=`pkg-config --libs gtk+-2.0 vte` +ALL_CFLAGS=`pkg-config --cflags gtk+-2.0 vte dbus-glib-1` +ALL_LIBS=`pkg-config --libs gtk+-2.0 vte dbus-glib-1` .PHONY: all memcheck clean @@ -17,9 +17,11 @@ tilda.o: tilda.c $(GCC) $(CFLAGS) -c -o $@ $< $(ALL_CFLAGS) tilda-window.o: tilda-window.c tilda-window.h + dbus-binding-tool --mode=glib-server --prefix=tilda_window tilda-window.xml > tilda-window-dbus-glue.h $(GCC) $(CFLAGS) -c -o $@ $< $(ALL_CFLAGS) tilda-terminal.o: tilda-terminal.c tilda-terminal.h + dbus-binding-tool --mode=glib-server --prefix=tilda_terminal tilda-terminal.xml > tilda-terminal-dbus-glue.h $(GCC) $(CFLAGS) -c -o $@ $< $(ALL_CFLAGS) memcheck: tilda @@ -30,4 +32,25 @@ clean: rm -f tilda-window rm -f tilda-terminal rm -f tilda - + rm -f tilda-window-dbus-glue.h + rm -f tilda-terminal-dbus-glue.h + +boost-font: + @echo "Only run this when tilda is running, it attempts to send a message" + @echo "to the running tilda process to boost the font size" + dbus-send --print-reply \ + --dest=net.sourceforge.Tilda /net/sourceforge/Tilda/Window0/Terminal0 \ + org.freedesktop.DBus.Properties.Set \ + string:'' \ + string:font \ + variant:string:'Bitstream Vera Sans Mono 14' + +shrink-font: + @echo "Only run this when tilda is running, it attempts to send a message" + @echo "to the running tilda process to shrink the font size" + dbus-send --print-reply \ + --dest=net.sourceforge.Tilda /net/sourceforge/Tilda/Window0/Terminal0 \ + org.freedesktop.DBus.Properties.Set \ + string:'' \ + string:font \ + variant:string:'Bitstream Vera Sans Mono 10' diff --git a/tilda-terminal.c b/tilda-terminal.c index 1c76325..60c169c 100644 --- a/tilda-terminal.c +++ b/tilda-terminal.c @@ -1,4 +1,18 @@ +#include "tilda.h" #include "tilda-terminal.h" +#include "tilda-terminal-dbus-glue.h" + +static void +tilda_terminal_dbus_register_object (TildaTerminal *tt) +{ + gchar *object_path; + + // Register this object with DBus + object_path = g_strdup_printf ("/net/sourceforge/Tilda/Window%d/Terminal%d", + tt->window_number, tt->number); + dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(tt)); + g_free (object_path); +} static GObjectClass *parent_class = NULL; @@ -10,6 +24,7 @@ static GObjectClass *parent_class = NULL; enum tilda_terminal_properties { TILDA_TERMINAL_NUMBER = 1, + TILDA_TERMINAL_WINDOW_NUMBER, /* All non-constructor-only properties */ TILDA_TERMINAL_BACKGROUND_IMAGE, @@ -58,6 +73,11 @@ tilda_terminal_set_property (GObject *object, g_print ("terminal number: %d\n", self->number); break; + case TILDA_TERMINAL_WINDOW_NUMBER: + self->window_number = g_value_get_int (value); + g_print ("terminal parent window number: %d\n", self->window_number); + break; + case TILDA_TERMINAL_BACKGROUND_IMAGE: g_free (self->background_image); self->background_image = g_value_dup_string (value); @@ -74,6 +94,7 @@ tilda_terminal_set_property (GObject *object, case TILDA_TERMINAL_FONT: g_free (self->font); self->font = g_value_dup_string (value); + vte_terminal_set_font_from_string (VTE_TERMINAL(self->vte_term), self->font); g_print ("terminal font: %s\n", self->font); break; @@ -142,6 +163,10 @@ tilda_terminal_get_property (GObject *object, g_value_set_int (value, self->number); break; + case TILDA_TERMINAL_WINDOW_NUMBER: + g_value_set_int (value, self->window_number); + break; + case TILDA_TERMINAL_BACKGROUND_IMAGE: g_value_set_string (value, self->background_image); break; @@ -209,6 +234,8 @@ tilda_terminal_constructor (GType type, vte_terminal_fork_command (VTE_TERMINAL(self->vte_term), NULL, NULL, NULL, NULL, FALSE, FALSE, FALSE); + tilda_terminal_dbus_register_object (self); + return obj; } @@ -273,6 +300,9 @@ tilda_terminal_class_init (gpointer g_class, parent_class = g_type_class_peek_parent (klass); + /* Hook the TildaTerminal type into DBus */ + dbus_g_object_type_install_info (tilda_terminal_get_type(), &dbus_glib_tilda_terminal_object_info); + /* Install all of the properties */ pspec = g_param_spec_int ("number", "Terminal number", @@ -286,6 +316,18 @@ tilda_terminal_class_init (gpointer g_class, TILDA_TERMINAL_NUMBER, pspec); + pspec = g_param_spec_int ("window-number", + "Number of the window to which this terminal belongs", + "Set the number of the parent window", + 0, + INT_MAX, + 0x0000beef, + G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); + + g_object_class_install_property (gobject_class, + TILDA_TERMINAL_WINDOW_NUMBER, + pspec); + pspec = g_param_spec_string ("background-image", "Terminal's background image", "Get/Set terminal's background image", diff --git a/tilda-terminal.h b/tilda-terminal.h index 80b1bef..3887617 100644 --- a/tilda-terminal.h +++ b/tilda-terminal.h @@ -5,6 +5,8 @@ #include #include +#include "tilda-window.h" + #define TILDA_TYPE_TERMINAL (tilda_terminal_get_type()) #define TILDA_TERMINAL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TILDA_TYPE_TERMINAL, TildaTerminal)) @@ -21,8 +23,8 @@ struct _TildaTerminal { gboolean dispose_has_run; /* Instance Members */ - // FIXME: ADD THESE BACK - // TildaWindow *tw; + gint window_number; + //TildaWindow *tw; GtkWidget *vte_term; GtkWidget *scrollbar; GtkWidget *hbox; diff --git a/tilda-terminal.xml b/tilda-terminal.xml new file mode 100644 index 0000000..f17c40f --- /dev/null +++ b/tilda-terminal.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/tilda-window.c b/tilda-window.c index a9a2a79..0b197f2 100644 --- a/tilda-window.c +++ b/tilda-window.c @@ -1,11 +1,16 @@ +#include "tilda.h" #include "tilda-window.h" +#include "tilda-window-dbus-glue.h" static gboolean tilda_window_add_term (TildaWindow *tw) { // FIXME: this is totally bad, but it's a good hack for feasability static gint mynumber = 0; - TildaTerminal *tt = g_object_new (TILDA_TYPE_TERMINAL, "number", mynumber++, NULL); + TildaTerminal *tt = g_object_new (TILDA_TYPE_TERMINAL, + "number", mynumber++, + "window-number", tw->number, + NULL); g_ptr_array_add (tw->terms, tt); GtkWidget *label = gtk_label_new ("Tilda"); @@ -35,6 +40,17 @@ tilda_window_remove_term (TildaWindow *tw, int number) return TRUE; } +static void +tilda_window_dbus_register_object (TildaWindow *tw) +{ + gchar *object_path; + + // Register this object with DBus + object_path = g_strdup_printf ("/net/sourceforge/Tilda/Window%d", tw->number); + dbus_g_connection_register_g_object (dbus_connection, object_path, G_OBJECT(tw)); + g_free (object_path); +} + /******************************************************************************* * ALL GOBJECT STUFF BELOW PLEASE ******************************************************************************/ @@ -125,6 +141,9 @@ tilda_window_constructor (GType type, * TODO: This is the place to do DBus-init */ self = TILDA_WINDOW(obj); + /* Register this object with DBus */ + tilda_window_dbus_register_object (self); + gtk_container_add (GTK_CONTAINER(self->window), self->notebook); gtk_widget_show (self->notebook); @@ -213,6 +232,9 @@ tilda_window_class_init (gpointer g_class, pspec); /* TODO: more properties */ + + /* Hook the TildaWindow type into DBus */ + dbus_g_object_type_install_info (tilda_window_get_type(), &dbus_glib_tilda_window_object_info); } GType diff --git a/tilda-window.xml b/tilda-window.xml new file mode 100644 index 0000000..2615808 --- /dev/null +++ b/tilda-window.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/tilda.c b/tilda.c index fc0497d..e1459dd 100644 --- a/tilda.c +++ b/tilda.c @@ -1,8 +1,46 @@ #include +#include "tilda.h" #include "tilda-window.h" #include "tilda-terminal.h" +DBusGConnection *dbus_connection; + +static void +tilda_initialize_dbus () +{ + const gchar service_name[] = "net.sourceforge.Tilda"; + GError *error = NULL; + DBusGProxy *driver_proxy; + int request_ret; + + // Initialize the DBus connection + dbus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error); + if (dbus_connection == NULL) + { + g_warning ("Unable to connect to dbus: %s", error->message); + g_error_free (error); + return; + } + + // Register the service name + driver_proxy = dbus_g_proxy_new_for_name (dbus_connection, + DBUS_SERVICE_DBUS, + DBUS_PATH_DBUS, + DBUS_INTERFACE_DBUS); + + if (!org_freedesktop_DBus_request_name (driver_proxy, service_name, 0, &request_ret, &error)) + { + // FIXME: for whatever reason, this is wrong. The error message doesn't appear + // FIXME: when we were unable to register the service. Perhaps there's a more + // FIXME: GLib-y way of doing this? + g_warning ("Unable to register service: %s", error->message); + g_error_free (error); + } + + g_object_unref (driver_proxy); +} + int main (int argc, char *argv[]) { TildaWindow *tw; @@ -10,6 +48,9 @@ int main (int argc, char *argv[]) /* Initialize GTK+ (and the GObject system) */ gtk_init (&argc, &argv); + /* Start our connection to DBus */ + tilda_initialize_dbus (); + /* Create a TildaWindow, run it, and exit when it does, basically. * * This is nothing like what the real main() will be, but it's diff --git a/tilda.h b/tilda.h new file mode 100644 index 0000000..77dbe53 --- /dev/null +++ b/tilda.h @@ -0,0 +1,12 @@ +#ifndef TILDA_H +#define TILDA_H + +#include + +/* Project-global variables */ +extern DBusGConnection *dbus_connection; + +#endif /* TILDA_H */ + +/* vim: set ts=4 sts=4 sw=4 noet tw=112: */ + -- 2.25.1