keep stacking_list_tail a pointer to the tail of the stacking list
[dana/openbox.git] / openbox / openbox.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    openbox.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "debug.h"
21 #include "openbox.h"
22 #include "session.h"
23 #include "dock.h"
24 #include "event.h"
25 #include "menu.h"
26 #include "client.h"
27 #include "screen.h"
28 #include "actions.h"
29 #include "startupnotify.h"
30 #include "focus.h"
31 #include "focus_cycle.h"
32 #include "focus_cycle_indicator.h"
33 #include "focus_cycle_popup.h"
34 #include "moveresize.h"
35 #include "frame.h"
36 #include "framerender.h"
37 #include "keyboard.h"
38 #include "mouse.h"
39 #include "menuframe.h"
40 #include "grab.h"
41 #include "group.h"
42 #include "config.h"
43 #include "ping.h"
44 #include "prompt.h"
45 #include "gettext.h"
46 #include "obrender/render.h"
47 #include "obrender/theme.h"
48 #include "obt/display.h"
49 #include "obt/xqueue.h"
50 #include "obt/prop.h"
51 #include "obt/keyboard.h"
52 #include "obt/xml.h"
53
54 #ifdef HAVE_FCNTL_H
55 #  include <fcntl.h>
56 #endif
57 #ifdef HAVE_SIGNAL_H
58 #  include <signal.h>
59 #endif
60 #ifdef HAVE_STDLIB_H
61 #  include <stdlib.h>
62 #endif
63 #ifdef HAVE_LOCALE_H
64 #  include <locale.h>
65 #endif
66 #ifdef HAVE_SYS_STAT_H
67 #  include <sys/stat.h>
68 #  include <sys/types.h>
69 #endif
70 #ifdef HAVE_SYS_WAIT_H
71 #  include <sys/types.h>
72 #  include <sys/wait.h>
73 #endif
74 #ifdef HAVE_UNISTD_H
75 #  include <unistd.h>
76 #endif
77 #include <errno.h>
78
79 #include <X11/cursorfont.h>
80 #if USE_XCURSOR
81 #include <X11/Xcursor/Xcursor.h>
82 #endif
83
84 RrInstance   *ob_rr_inst;
85 RrImageCache *ob_rr_icons;
86 RrTheme      *ob_rr_theme;
87 GMainLoop    *ob_main_loop;
88 gint          ob_screen;
89 gboolean      ob_replace_wm = FALSE;
90 gboolean      ob_sm_use = TRUE;
91 gchar        *ob_sm_id = NULL;
92 gchar        *ob_sm_save_file = NULL;
93 gboolean      ob_sm_restore = TRUE;
94 gboolean      ob_debug_xinerama = FALSE;
95 const gchar  *ob_locale_msg = NULL;
96
97 static ObState   state;
98 static gboolean  xsync = FALSE;
99 static gboolean  reconfigure = FALSE;
100 static gboolean  restart = FALSE;
101 static gchar    *restart_path = NULL;
102 static Cursor    cursors[OB_NUM_CURSORS];
103 static gint      exitcode = 0;
104 static guint     remote_control = 0;
105 static gboolean  being_replaced = FALSE;
106 static gchar    *config_file = NULL;
107 static gchar    *startup_cmd = NULL;
108
109 static void signal_handler(gint signal, gpointer data);
110 static void remove_args(gint *argc, gchar **argv, gint index, gint num);
111 static void parse_env();
112 static void parse_args(gint *argc, gchar **argv);
113 static Cursor load_cursor(const gchar *name, guint fontval);
114 static void run_startup_cmd(void);
115
116 gint main(gint argc, gchar **argv)
117 {
118     gchar *program_name;
119
120     ob_set_state(OB_STATE_STARTING);
121
122     ob_debug_startup();
123
124     /* initialize the locale */
125     if (!(ob_locale_msg = setlocale(LC_MESSAGES, "")))
126         g_message("Couldn't set messages locale category from environment.");
127     if (!setlocale(LC_ALL, ""))
128         g_message("Couldn't set locale from environment.");
129     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
130     bind_textdomain_codeset(PACKAGE_NAME, "UTF-8");
131     textdomain(PACKAGE_NAME);
132
133     if (chdir(g_get_home_dir()) == -1)
134         g_message(_("Unable to change to home directory \"%s\": %s"),
135                   g_get_home_dir(), g_strerror(errno));
136
137     /* parse the command line args, which can change the argv[0] */
138     parse_args(&argc, argv);
139     /* parse the environment variables */
140     parse_env();
141
142     program_name = g_path_get_basename(argv[0]);
143     g_set_prgname(program_name);
144
145     if (!remote_control)
146         session_startup(argc, argv);
147
148     if (!obt_display_open(NULL))
149         ob_exit_with_error(_("Failed to open the display from the DISPLAY environment variable."));
150
151     if (remote_control) {
152         /* Send client message telling the OB process to:
153          * remote_control = 1 -> reconfigure
154          * remote_control = 2 -> restart */
155         OBT_PROP_MSG(ob_screen, obt_root(ob_screen),
156                      OB_CONTROL, remote_control, 0, 0, 0, 0);
157         obt_display_close();
158         exit(EXIT_SUCCESS);
159     }
160
161     ob_main_loop = g_main_loop_new(NULL, FALSE);
162
163     /* set up signal handler */
164 //  obt_main_loop_signal_add(ob_main_loop, SIGUSR1, signal_handler, NULL,NULL);
165 //  obt_main_loop_signal_add(ob_main_loop, SIGUSR2, signal_handler, NULL,NULL);
166 //  obt_main_loop_signal_add(ob_main_loop, SIGTERM, signal_handler, NULL,NULL);
167 //  obt_main_loop_signal_add(ob_main_loop, SIGINT, signal_handler,  NULL,NULL);
168 //  obt_main_loop_signal_add(ob_main_loop, SIGHUP, signal_handler,  NULL,NULL);
169 //  obt_main_loop_signal_add(ob_main_loop, SIGPIPE, signal_handler, NULL,NULL);
170 //  obt_main_loop_signal_add(ob_main_loop, SIGCHLD, signal_handler, NULL,NULL);
171 //  obt_main_loop_signal_add(ob_main_loop, SIGTTIN, signal_handler, NULL,NULL);
172 //  obt_main_loop_signal_add(ob_main_loop, SIGTTOU, signal_handler, NULL,NULL);
173
174     ob_screen = DefaultScreen(obt_display);
175
176     ob_rr_inst = RrInstanceNew(obt_display, ob_screen);
177     if (ob_rr_inst == NULL)
178         ob_exit_with_error(_("Failed to initialize the obrender library."));
179     /* Saving 3 resizes of an RrImage makes a lot of sense for icons, as there
180        are generally 3 icon sizes needed: the titlebar icon, the menu icon,
181        and the alt-tab icon
182     */
183     ob_rr_icons = RrImageCacheNew(3);
184
185     XSynchronize(obt_display, xsync);
186
187     /* check for locale support */
188     if (!XSupportsLocale())
189         g_message(_("X server does not support locale."));
190     if (!XSetLocaleModifiers(""))
191         g_message(_("Cannot set locale modifiers for the X server."));
192
193     /* set the DISPLAY environment variable for any lauched children, to the
194        display we're using, so they open in the right place. */
195     setenv("DISPLAY", DisplayString(obt_display), TRUE);
196
197     /* create available cursors */
198     cursors[OB_CURSOR_NONE] = None;
199     cursors[OB_CURSOR_POINTER] = load_cursor("left_ptr", XC_left_ptr);
200     cursors[OB_CURSOR_BUSYPOINTER] = load_cursor("left_ptr_watch",XC_left_ptr);
201     cursors[OB_CURSOR_BUSY] = load_cursor("watch", XC_watch);
202     cursors[OB_CURSOR_MOVE] = load_cursor("fleur", XC_fleur);
203     cursors[OB_CURSOR_NORTH] = load_cursor("top_side", XC_top_side);
204     cursors[OB_CURSOR_NORTHEAST] = load_cursor("top_right_corner",
205                                                XC_top_right_corner);
206     cursors[OB_CURSOR_EAST] = load_cursor("right_side", XC_right_side);
207     cursors[OB_CURSOR_SOUTHEAST] = load_cursor("bottom_right_corner",
208                                                XC_bottom_right_corner);
209     cursors[OB_CURSOR_SOUTH] = load_cursor("bottom_side", XC_bottom_side);
210     cursors[OB_CURSOR_SOUTHWEST] = load_cursor("bottom_left_corner",
211                                                XC_bottom_left_corner);
212     cursors[OB_CURSOR_WEST] = load_cursor("left_side", XC_left_side);
213     cursors[OB_CURSOR_NORTHWEST] = load_cursor("top_left_corner",
214                                                XC_top_left_corner);
215
216     if (screen_annex()) { /* it will be ours! */
217
218         /* get a timestamp from after taking over as the WM.  if we use the
219            old timestamp to set focus it can fail when replacing another WM. */
220         event_reset_time();
221
222         do {
223             ObPrompt *xmlprompt = NULL;
224
225             if (reconfigure) obt_keyboard_reload();
226
227             {
228                 ObtXmlInst *i;
229
230                 /* startup the parsing so everything can register sections
231                    of the rc */
232                 i = obt_xml_instance_new();
233
234                 /* register all the available actions */
235                 actions_startup(reconfigure);
236                 /* start up config which sets up with the parser */
237                 config_startup(i);
238
239                 /* parse/load user options */
240                 if ((config_file &&
241                      obt_xml_load_file(i, config_file, "openbox_config")) ||
242                     obt_xml_load_config_file(i, "openbox", "rc.xml",
243                                              "openbox_config"))
244                 {
245                     obt_xml_tree_from_root(i);
246                     obt_xml_close(i);
247                 }
248                 else {
249                     g_message(_("Unable to find a valid config file, using some simple defaults"));
250                     config_file = NULL;
251                 }
252
253                 if (config_file) {
254                     gchar *p = g_filename_to_utf8(config_file, -1,
255                                                   NULL, NULL, NULL);
256                     if (p)
257                         OBT_PROP_SETS(obt_root(ob_screen), OB_CONFIG_FILE,
258                                       utf8, p);
259                     g_free(p);
260                 }
261                 else
262                     OBT_PROP_ERASE(obt_root(ob_screen), OB_CONFIG_FILE);
263
264                 /* we're done with parsing now, kill it */
265                 obt_xml_instance_unref(i);
266             }
267
268             /* load the theme specified in the rc file */
269             {
270                 RrTheme *theme;
271                 if ((theme = RrThemeNew(ob_rr_inst, config_theme, TRUE,
272                                         config_font_activewindow,
273                                         config_font_inactivewindow,
274                                         config_font_menutitle,
275                                         config_font_menuitem,
276                                         config_font_activeosd,
277                                         config_font_inactiveosd)))
278                 {
279                     RrThemeFree(ob_rr_theme);
280                     ob_rr_theme = theme;
281                 }
282                 if (ob_rr_theme == NULL)
283                     ob_exit_with_error(_("Unable to load a theme."));
284
285                 OBT_PROP_SETS(obt_root(ob_screen),
286                               OB_THEME, utf8, ob_rr_theme->name);
287             }
288
289             if (reconfigure) {
290                 GList *it;
291
292                 /* update all existing windows for the new theme */
293                 for (it = client_list; it; it = g_list_next(it)) {
294                     ObClient *c = it->data;
295                     frame_adjust_theme(c->frame);
296                 }
297             }
298             event_startup(reconfigure);
299             /* focus_backup is used for stacking, so this needs to come before
300                anything that calls stacking_add */
301             sn_startup(reconfigure);
302             window_startup(reconfigure);
303             focus_startup(reconfigure);
304             focus_cycle_startup(reconfigure);
305             focus_cycle_indicator_startup(reconfigure);
306             focus_cycle_popup_startup(reconfigure);
307             screen_startup(reconfigure);
308             grab_startup(reconfigure);
309             group_startup(reconfigure);
310             ping_startup(reconfigure);
311             client_startup(reconfigure);
312             dock_startup(reconfigure);
313             moveresize_startup(reconfigure);
314             keyboard_startup(reconfigure);
315             mouse_startup(reconfigure);
316             menu_frame_startup(reconfigure);
317             menu_startup(reconfigure);
318             prompt_startup(reconfigure);
319
320             /* do this after everything is started so no events will get
321                missed */
322             xqueue_listen();
323
324             if (!reconfigure) {
325                 guint32 xid;
326                 ObWindow *w;
327
328                 /* get all the existing windows */
329                 window_manage_all();
330
331                 /* focus what was focused if a wm was already running */
332                 if (OBT_PROP_GET32(obt_root(ob_screen),
333                                    NET_ACTIVE_WINDOW, WINDOW, &xid) &&
334                     (w = window_find(xid)) && WINDOW_IS_CLIENT(w))
335                 {
336                     client_focus(WINDOW_AS_CLIENT(w));
337                 }
338             } else {
339                 GList *it;
340
341                 /* redecorate all existing windows */
342                 for (it = client_list; it; it = g_list_next(it)) {
343                     ObClient *c = it->data;
344
345                     /* the new config can change the window's decorations */
346                     client_setup_decor_and_functions(c, FALSE);
347                     /* redraw the frames */
348                     frame_adjust_area(c->frame, TRUE, TRUE, FALSE);
349                     /* the decor sizes may have changed, so the windows may
350                        end up in new positions */
351                     client_reconfigure(c, FALSE);
352                 }
353             }
354
355             reconfigure = FALSE;
356
357             ob_set_state(OB_STATE_RUNNING);
358
359             if (startup_cmd) run_startup_cmd();
360
361             /* look for parsing errors */
362             {
363                 xmlErrorPtr e = xmlGetLastError();
364                 if (e) {
365                     gchar *m;
366
367                     m = g_strdup_printf(_("One or more XML syntax errors were found while parsing the Openbox configuration files.  See stdout for more information.  The last error seen was in file \"%s\" line %d, with message: %s"), e->file, e->line, e->message);
368                     xmlprompt =
369                         prompt_show_message(m, _("Openbox Syntax Error"), _("Close"));
370                     g_free(m);
371                     xmlResetError(e);
372                 }
373             }
374
375             g_main_loop_run(ob_main_loop);
376             ob_set_state(reconfigure ?
377                          OB_STATE_RECONFIGURING : OB_STATE_EXITING);
378
379             if (xmlprompt) {
380                 prompt_unref(xmlprompt);
381                 xmlprompt = NULL;
382             }
383
384             if (!reconfigure)
385                 window_unmanage_all();
386
387             prompt_shutdown(reconfigure);
388             menu_shutdown(reconfigure);
389             menu_frame_shutdown(reconfigure);
390             mouse_shutdown(reconfigure);
391             keyboard_shutdown(reconfigure);
392             moveresize_shutdown(reconfigure);
393             dock_shutdown(reconfigure);
394             client_shutdown(reconfigure);
395             ping_shutdown(reconfigure);
396             group_shutdown(reconfigure);
397             grab_shutdown(reconfigure);
398             screen_shutdown(reconfigure);
399             focus_cycle_popup_shutdown(reconfigure);
400             focus_cycle_indicator_shutdown(reconfigure);
401             focus_cycle_shutdown(reconfigure);
402             focus_shutdown(reconfigure);
403             window_shutdown(reconfigure);
404             sn_shutdown(reconfigure);
405             event_shutdown(reconfigure);
406             config_shutdown();
407             actions_shutdown(reconfigure);
408         } while (reconfigure);
409     }
410
411     XSync(obt_display, FALSE);
412
413     RrThemeFree(ob_rr_theme);
414     RrImageCacheUnref(ob_rr_icons);
415     RrInstanceFree(ob_rr_inst);
416
417     session_shutdown(being_replaced);
418
419     obt_display_close();
420
421     if (restart) {
422         ob_debug_shutdown();
423         if (restart_path != NULL) {
424             gint argcp;
425             gchar **argvp;
426             GError *err = NULL;
427
428             /* run other window manager */
429             if (g_shell_parse_argv(restart_path, &argcp, &argvp, &err)) {
430                 execvp(argvp[0], argvp);
431                 g_strfreev(argvp);
432             } else {
433                 g_message(
434                     _("Restart failed to execute new executable \"%s\": %s"),
435                     restart_path, err->message);
436                 g_error_free(err);
437             }
438         }
439
440         /* we remove the session arguments from argv, so put them back,
441            also don't restore the session on restart */
442         if (ob_sm_save_file != NULL || ob_sm_id != NULL) {
443             gchar **nargv;
444             gint i, l;
445
446             l = argc + 1 +
447                 (ob_sm_save_file != NULL ? 2 : 0) +
448                 (ob_sm_id != NULL ? 2 : 0);
449             nargv = g_new0(gchar*, l+1);
450             for (i = 0; i < argc; ++i)
451                 nargv[i] = argv[i];
452
453             if (ob_sm_save_file != NULL) {
454                 nargv[i++] = g_strdup("--sm-save-file");
455                 nargv[i++] = ob_sm_save_file;
456             }
457             if (ob_sm_id != NULL) {
458                 nargv[i++] = g_strdup("--sm-client-id");
459                 nargv[i++] = ob_sm_id;
460             }
461             nargv[i++] = g_strdup("--sm-no-load");
462             g_assert(i == l);
463             argv = nargv;
464         }
465
466         /* re-run me */
467         execvp(argv[0], argv); /* try how we were run */
468         execlp(argv[0], program_name, (gchar*)NULL); /* last resort */
469     }
470
471     /* free stuff passed in from the command line or environment */
472     g_free(ob_sm_save_file);
473     g_free(ob_sm_id);
474     g_free(program_name);
475
476     if (!restart)
477         ob_debug_shutdown();
478
479     return exitcode;
480 }
481
482 static void signal_handler(gint signal, gpointer data)
483 {
484     switch (signal) {
485     case SIGUSR1:
486         ob_debug("Caught signal %d. Restarting.", signal);
487         ob_restart();
488         break;
489     case SIGUSR2:
490         ob_debug("Caught signal %d. Reconfiguring.", signal);
491         ob_reconfigure();
492         break;
493     case SIGCHLD:
494         /* reap children */
495         while (waitpid(-1, NULL, WNOHANG) > 0);
496         break;
497     case SIGTTIN:
498     case SIGTTOU:
499         ob_debug("Caught signal %d. Ignoring.", signal);
500         break;
501     default:
502         ob_debug("Caught signal %d. Exiting.", signal);
503         /* TERM and INT return a 0 code */
504         ob_exit(!(signal == SIGTERM || signal == SIGINT));
505     }
506 }
507
508 static void print_version(void)
509 {
510     g_print("Openbox %s\n", PACKAGE_VERSION);
511     g_print(_("Copyright (c)"));
512     g_print(" 2008        Mikael Magnusson\n");
513     g_print(_("Copyright (c)"));
514     g_print(" 2003-2006   Dana Jansens\n\n");
515     g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
516     g_print("This is free software, and you are welcome to redistribute it\n");
517     g_print("under certain conditions. See the file COPYING for details.\n\n");
518 }
519
520 static void print_help(void)
521 {
522     g_print(_("Syntax: openbox [options]\n"));
523     g_print(_("\nOptions:\n"));
524     g_print(_("  --help              Display this help and exit\n"));
525     g_print(_("  --version           Display the version and exit\n"));
526     g_print(_("  --replace           Replace the currently running window manager\n"));
527     /* TRANSLATORS: if you translate "FILE" here, make sure to keep the "Specify..."
528        aligned still, if you have to, make a new line with \n and 22 spaces. It's
529        fine to leave it as FILE though. */
530     g_print(_("  --config-file FILE  Specify the path to the config file to use\n"));
531     g_print(_("  --sm-disable        Disable connection to the session manager\n"));
532     g_print(_("\nPassing messages to a running Openbox instance:\n"));
533     g_print(_("  --reconfigure       Reload Openbox's configuration\n"));
534     g_print(_("  --restart           Restart Openbox\n"));
535     g_print(_("  --exit              Exit Openbox\n"));
536     g_print(_("\nDebugging options:\n"));
537     g_print(_("  --sync              Run in synchronous mode\n"));
538     g_print(_("  --startup CMD       Run CMD after starting\n"));
539     g_print(_("  --debug             Display debugging output\n"));
540     g_print(_("  --debug-focus       Display debugging output for focus handling\n"));
541     g_print(_("  --debug-session     Display debugging output for session management\n"));
542     g_print(_("  --debug-xinerama    Split the display into fake xinerama screens\n"));
543     g_print(_("\nPlease report bugs at %s\n"), PACKAGE_BUGREPORT);
544 }
545
546 static void remove_args(gint *argc, gchar **argv, gint index, gint num)
547 {
548     gint i;
549
550     for (i = index; i < *argc - num; ++i)
551         argv[i] = argv[i+num];
552     for (; i < *argc; ++i)
553         argv[i] = NULL;
554     *argc -= num;
555 }
556
557 static void run_startup_cmd(void)
558 {
559     gchar **argv = NULL;
560     GError *e = NULL;
561     gboolean ok;
562
563     if (!g_shell_parse_argv(startup_cmd, NULL, &argv, &e)) {
564         g_message("Error parsing startup command: %s",
565                   e->message);
566         g_error_free(e);
567         e = NULL;
568     }
569     ok = g_spawn_async(NULL, argv, NULL,
570                        G_SPAWN_SEARCH_PATH |
571                        G_SPAWN_DO_NOT_REAP_CHILD,
572                        NULL, NULL, NULL, &e);
573     if (!g_shell_parse_argv(startup_cmd, NULL, &argv, &e)) {
574         g_message("Error launching startup command: %s",
575                   e->message);
576         g_error_free(e);
577         e = NULL;
578     }
579 }
580
581 static void parse_env(void)
582 {
583     const gchar *id;
584
585     /* unset this so we don't pass it on unknowingly */
586     unsetenv("DESKTOP_STARTUP_ID");
587
588     /* this is how gnome-session passes in a session client id */
589     id = g_getenv("DESKTOP_AUTOSTART_ID");
590     if (id) {
591         unsetenv("DESKTOP_AUTOSTART_ID");
592         if (ob_sm_id) g_free(ob_sm_id);
593         ob_sm_id = g_strdup(id);
594         ob_debug_type(OB_DEBUG_SM,
595                       "DESKTOP_AUTOSTART_ID %s supercedes --sm-client-id\n",
596                       ob_sm_id);
597     }
598 }
599
600 static void parse_args(gint *argc, gchar **argv)
601 {
602     gint i;
603
604     for (i = 1; i < *argc; ++i) {
605         if (!strcmp(argv[i], "--version")) {
606             print_version();
607             exit(0);
608         }
609         else if (!strcmp(argv[i], "--help")) {
610             print_help();
611             exit(0);
612         }
613         else if (!strcmp(argv[i], "--g-fatal-warnings")) {
614             g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL);
615         }
616         else if (!strcmp(argv[i], "--replace")) {
617             ob_replace_wm = TRUE;
618             remove_args(argc, argv, i, 1);
619             --i; /* this arg was removed so go back */
620         }
621         else if (!strcmp(argv[i], "--sync")) {
622             xsync = TRUE;
623         }
624         else if (!strcmp(argv[i], "--startup")) {
625             if (i == *argc - 1) /* no args left */
626                 g_printerr(_("--startup requires an argument\n"));
627             else {
628                 /* this will be in the current locale encoding, which is
629                    what we want */
630                 startup_cmd = argv[i+1];
631                 remove_args(argc, argv, i, 2);
632                 --i; /* this arg was removed so go back */
633                 ob_debug("--startup %s", startup_cmd);
634             }
635         }
636         else if (!strcmp(argv[i], "--debug")) {
637             ob_debug_enable(OB_DEBUG_NORMAL, TRUE);
638             ob_debug_enable(OB_DEBUG_APP_BUGS, TRUE);
639         }
640         else if (!strcmp(argv[i], "--debug-focus")) {
641             ob_debug_enable(OB_DEBUG_FOCUS, TRUE);
642         }
643         else if (!strcmp(argv[i], "--debug-session")) {
644             ob_debug_enable(OB_DEBUG_SM, TRUE);
645         }
646         else if (!strcmp(argv[i], "--debug-xinerama")) {
647             ob_debug_xinerama = TRUE;
648         }
649         else if (!strcmp(argv[i], "--reconfigure")) {
650             remote_control = 1;
651         }
652         else if (!strcmp(argv[i], "--restart")) {
653             remote_control = 2;
654         }
655         else if (!strcmp(argv[i], "--exit")) {
656             remote_control = 3;
657         }
658         else if (!strcmp(argv[i], "--config-file")) {
659             if (i == *argc - 1) /* no args left */
660                 g_printerr(_("--config-file requires an argument\n"));
661             else {
662                 /* this will be in the current locale encoding, which is
663                    what we want */
664                 config_file = argv[i+1];
665                 ++i; /* skip the argument */
666                 ob_debug("--config-file %s", config_file);
667             }
668         }
669         else if (!strcmp(argv[i], "--sm-save-file")) {
670             if (i == *argc - 1) /* no args left */
671                 /* not translated cuz it's sekret */
672                 g_printerr("--sm-save-file requires an argument\n");
673             else {
674                 ob_sm_save_file = g_strdup(argv[i+1]);
675                 remove_args(argc, argv, i, 2);
676                 --i; /* this arg was removed so go back */
677                 ob_debug_type(OB_DEBUG_SM, "--sm-save-file %s",
678                               ob_sm_save_file);
679             }
680         }
681         else if (!strcmp(argv[i], "--sm-client-id")) {
682             if (i == *argc - 1) /* no args left */
683                 /* not translated cuz it's sekret */
684                 g_printerr("--sm-client-id requires an argument\n");
685             else {
686                 ob_sm_id = g_strdup(argv[i+1]);
687                 remove_args(argc, argv, i, 2);
688                 --i; /* this arg was removed so go back */
689                 ob_debug_type(OB_DEBUG_SM, "--sm-client-id %s", ob_sm_id);
690             }
691         }
692         else if (!strcmp(argv[i], "--sm-disable")) {
693             ob_sm_use = FALSE;
694         }
695         else if (!strcmp(argv[i], "--sm-no-load")) {
696             ob_sm_restore = FALSE;
697             remove_args(argc, argv, i, 1);
698             --i; /* this arg was removed so go back */
699         }
700         else {
701             /* this is a memleak.. oh well.. heh */
702             gchar *err = g_strdup_printf
703                 (_("Invalid command line argument \"%s\"\n"), argv[i]);
704             ob_exit_with_error(err);
705         }
706     }
707 }
708
709 static Cursor load_cursor(const gchar *name, guint fontval)
710 {
711     Cursor c = None;
712
713 #if USE_XCURSOR
714     c = XcursorLibraryLoadCursor(obt_display, name);
715 #endif
716     if (c == None)
717         c = XCreateFontCursor(obt_display, fontval);
718     return c;
719 }
720
721 void ob_exit_with_error(const gchar *msg)
722 {
723     g_message("%s", msg);
724     session_shutdown(TRUE);
725     exit(EXIT_FAILURE);
726 }
727
728 void ob_restart_other(const gchar *path)
729 {
730     restart_path = g_strdup(path);
731     ob_restart();
732 }
733
734 void ob_restart(void)
735 {
736     restart = TRUE;
737     ob_exit(0);
738 }
739
740 void ob_reconfigure(void)
741 {
742     reconfigure = TRUE;
743     ob_exit(0);
744 }
745
746 void ob_exit(gint code)
747 {
748     exitcode = code;
749     g_main_loop_quit(ob_main_loop);
750 }
751
752 void ob_exit_replace(void)
753 {
754     exitcode = 0;
755     being_replaced = TRUE;
756     g_main_loop_quit(ob_main_loop);
757 }
758
759 Cursor ob_cursor(ObCursor cursor)
760 {
761     g_assert(cursor < OB_NUM_CURSORS);
762     return cursors[cursor];
763 }
764
765 ObState ob_state(void)
766 {
767     return state;
768 }
769
770 void ob_set_state(ObState s)
771 {
772     state = s;
773 }