Make Tilda work without DBus
[tilda-gobject.git] / tilda.c
1 #include <stdlib.h>
2 #include <signal.h>
3
4 #include "tilda.h"
5 #include "tilda-config.h"
6 #include "tilda-controller.h"
7 #include "tomboykeybinder.h"
8
9 /* Project-global variables */
10 DBusGConnection *dbus_connection = NULL;
11
12 static void
13 tilda_dbus_init ()
14 {
15         debug_enter  ();
16
17         static const gchar service_name[] = "net.sourceforge.Tilda";
18         GError *error = NULL;
19         DBusGProxy *driver_proxy;
20         guint request_ret;
21         gboolean ret;
22
23         // Initialize the DBus connection
24         dbus_connection = dbus_g_bus_get (DBUS_BUS_SESSION, &error);
25         if (dbus_connection == NULL)
26         {
27                 g_warning (_("Unable to connect to DBus: %s\n"), error->message);
28                 g_error_free (error);
29                 return;
30         }
31
32         // Register the service name
33         driver_proxy = dbus_g_proxy_new_for_name (dbus_connection,
34                                                                                           DBUS_SERVICE_DBUS,
35                                                                                           DBUS_PATH_DBUS,
36                                                                                           DBUS_INTERFACE_DBUS);
37
38         ret = org_freedesktop_DBus_request_name (driver_proxy,
39                                                                                          service_name,
40                                                                                          DBUS_NAME_FLAG_DO_NOT_QUEUE,
41                                                                                          &request_ret,
42                                                                                          &error);
43
44         if (!ret)
45         {
46                 g_warning (_("Unable to communicate with DBus: %s\n"), error->message);
47                 g_error_free (error);
48         }
49
50         if (request_ret == DBUS_REQUEST_NAME_REPLY_EXISTS)
51         {
52                 g_critical (_("There is already an instance of Tilda running\n"));
53                 exit (EXIT_FAILURE);
54         }
55
56         g_object_unref (driver_proxy);
57 }
58
59 static void
60 tilda_parse_command_line (gint argc, gchar *argv[])
61 {
62         debug_enter ();
63
64         gboolean version = FALSE;
65
66         /* All of the various command-line options */
67         const GOptionEntry cl_opts[] = {
68                 { "version",                    'V', 0, G_OPTION_ARG_NONE,              &version,                       N_("Show version information"), NULL },
69                 { NULL },
70         };
71
72         /* Set up the command-line parser */
73         GError *error = NULL;
74         GOptionContext *context = g_option_context_new (NULL);
75         g_option_context_add_main_entries (context, cl_opts, NULL);
76         g_option_context_add_group (context, gtk_get_option_group (TRUE));
77         g_option_context_parse (context, &argc, &argv, &error);
78         g_option_context_free (context);
79
80         /* Check for unknown options, and give a nice message if there are some */
81         if (error)
82         {
83                 g_printerr (_("Error parsing command-line options: %s\n"), error->message);
84                 g_printerr (_("The command \"tilda --help\" will show all possible options\n"));
85                 g_error_free (error);
86                 exit (EXIT_FAILURE);
87         }
88
89         /* If we need to show the version, show it then exit normally */
90         if (version)
91         {
92                 g_print ("%s\n\n", TILDA_VERSION);
93
94                 g_print ("Copyright (c) 2005-2008 Tristan Sloughter (sloutri@iit.edu)\n");
95                 g_print ("Copyright (c) 2005-2008 Ira W. Snyder (tilda@irasnyder.com)\n\n");
96
97                 g_print ("This program comes with ABSOLUTELY NO WARRANTY.\n");
98                 g_print ("This is free software, and you are welcome to redistribute it\n");
99                 g_print ("under certain conditions. See the file COPYING for details.\n");
100
101                 exit (EXIT_SUCCESS);
102         }
103 }
104
105 static void
106 tilda_termination_handler (gint signum)
107 {
108         debug_enter  ();
109         debug_printf ("signum: %d\n", signum);
110
111         gtk_main_quit ();
112 }
113
114 /* Hook up all system signal handlers */
115 static void
116 tilda_signal_handlers_init ()
117 {
118         struct sigaction sa;
119
120         /* Hook up signal handlers */
121         sa.sa_handler = tilda_termination_handler;
122         sigemptyset (&sa.sa_mask);
123         sa.sa_flags = 0;
124
125         sigaction (SIGINT,  &sa, NULL);
126         sigaction (SIGQUIT, &sa, NULL);
127         sigaction (SIGABRT, &sa, NULL);
128         sigaction (SIGTERM, &sa, NULL);
129
130         /* SIGKILL cannot be caught according to sigaction(2) and signal(7) */
131         /* sigaction (SIGKILL, &sa, NULL); */
132 }
133
134 int main (int argc, char *argv[])
135 {
136         debug_enter ();
137
138         TildaController *tilda;
139
140 #if ENABLE_NLS
141         /* Gettext Initialization */
142         setlocale (LC_ALL, "");
143         bindtextdomain (PACKAGE, LOCALEDIR);
144         textdomain (PACKAGE);
145 #endif
146
147         /* Parse the command-line options */
148         tilda_parse_command_line (argc, argv);
149
150         /* Initialize GTK+ (and the GObject system) */
151         gtk_init (&argc, &argv);
152
153         /* Hook up the signal handlers */
154         tilda_signal_handlers_init ();
155
156         /* Initialize the keybinder */
157         tomboy_keybinder_init ();
158
159         /* Initialize the configuration system */
160         tilda_config_init ("tilda.conf");
161
162         /* Start our connection to DBus */
163         tilda_dbus_init ();
164
165         /* Create a TildaController, which manages TildaWindows, which in turn manages
166          * TildaTerminals. Exit when it does. */
167         tilda = g_object_new (TILDA_TYPE_CONTROLLER, NULL);
168
169         debug_printf ("Starting gtk_main()!\n");
170         gtk_main ();
171         debug_printf ("Out of gtk_main(), going down\n");
172
173         /* Unref the TildaController that controls this whole operation */
174         g_object_unref (G_OBJECT(tilda));
175
176         /* Clean up after the config system */
177         tilda_config_free ();
178
179         return 0;
180 }
181
182 /* vim: set ts=4 sts=4 sw=4 noet tw=112: */