1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 openbox.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
29 #include "startupnotify.h"
31 #include "focus_cycle.h"
32 #include "focus_cycle_indicator.h"
33 #include "focus_cycle_popup.h"
34 #include "moveresize.h"
36 #include "framerender.h"
39 #include "menuframe.h"
46 #include "obrender/render.h"
47 #include "obrender/theme.h"
48 #include "obt/display.h"
49 #include "obt/xqueue.h"
51 #include "obt/keyboard.h"
66 #ifdef HAVE_SYS_STAT_H
67 # include <sys/stat.h>
68 # include <sys/types.h>
70 #ifdef HAVE_SYS_WAIT_H
71 # include <sys/types.h>
72 # include <sys/wait.h>
79 #include <X11/cursorfont.h>
81 #include <X11/Xcursor/Xcursor.h>
84 RrInstance
*ob_rr_inst
;
85 RrImageCache
*ob_rr_icons
;
87 GMainLoop
*ob_main_loop
;
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
;
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
;
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);
116 gint
main(gint argc
, gchar
**argv
)
120 ob_set_state(OB_STATE_STARTING
);
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
);
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
));
137 /* parse the command line args, which can change the argv[0] */
138 parse_args(&argc
, argv
);
139 /* parse the environment variables */
142 program_name
= g_path_get_basename(argv
[0]);
143 g_set_prgname(program_name
);
146 session_startup(argc
, argv
);
148 if (!obt_display_open(NULL
))
149 ob_exit_with_error(_("Failed to open the display from the DISPLAY environment variable."));
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);
161 ob_main_loop
= g_main_loop_new(NULL
, FALSE
);
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);
174 ob_screen
= DefaultScreen(obt_display
);
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,
183 ob_rr_icons
= RrImageCacheNew(3);
185 XSynchronize(obt_display
, xsync
);
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."));
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
);
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",
216 if (screen_annex()) { /* it will be ours! */
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. */
223 ObPrompt
*xmlprompt
= NULL
;
225 if (reconfigure
) obt_keyboard_reload();
230 /* startup the parsing so everything can register sections
232 i
= obt_xml_instance_new();
234 /* register all the available actions */
235 actions_startup(reconfigure
);
236 /* start up config which sets up with the parser */
239 /* parse/load user options */
241 obt_xml_load_file(i
, config_file
, "openbox_config")) ||
242 obt_xml_load_config_file(i
, "openbox", "rc.xml",
245 obt_xml_tree_from_root(i
);
249 g_message(_("Unable to find a valid config file, using some simple defaults"));
254 gchar
*p
= g_filename_to_utf8(config_file
, -1,
257 OBT_PROP_SETS(obt_root(ob_screen
), OB_CONFIG_FILE
,
262 OBT_PROP_ERASE(obt_root(ob_screen
), OB_CONFIG_FILE
);
264 /* we're done with parsing now, kill it */
265 obt_xml_instance_unref(i
);
268 /* load the theme specified in the rc file */
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
)))
279 RrThemeFree(ob_rr_theme
);
282 if (ob_rr_theme
== NULL
)
283 ob_exit_with_error(_("Unable to load a theme."));
285 OBT_PROP_SETS(obt_root(ob_screen
),
286 OB_THEME
, utf8
, ob_rr_theme
->name
);
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
);
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
);
320 /* do this after everything is started so no events will get
328 /* get all the existing windows */
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
))
336 client_focus(WINDOW_AS_CLIENT(w
));
341 /* redecorate all existing windows */
342 for (it
= client_list
; it
; it
= g_list_next(it
)) {
343 ObClient
*c
= it
->data
;
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
);
357 ob_set_state(OB_STATE_RUNNING
);
359 if (startup_cmd
) run_startup_cmd();
361 /* look for parsing errors */
363 xmlErrorPtr e
= xmlGetLastError();
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
);
369 prompt_show_message(m
, _("Openbox Syntax Error"), _("Close"));
375 g_main_loop_run(ob_main_loop
);
376 ob_set_state(reconfigure
?
377 OB_STATE_RECONFIGURING
: OB_STATE_EXITING
);
380 prompt_unref(xmlprompt
);
385 window_unmanage_all();
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
);
407 actions_shutdown(reconfigure
);
408 } while (reconfigure
);
411 XSync(obt_display
, FALSE
);
413 RrThemeFree(ob_rr_theme
);
414 RrImageCacheUnref(ob_rr_icons
);
415 RrInstanceFree(ob_rr_inst
);
417 session_shutdown(being_replaced
);
423 if (restart_path
!= NULL
) {
428 /* run other window manager */
429 if (g_shell_parse_argv(restart_path
, &argcp
, &argvp
, &err
)) {
430 execvp(argvp
[0], argvp
);
434 _("Restart failed to execute new executable \"%s\": %s"),
435 restart_path
, err
->message
);
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
) {
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
)
453 if (ob_sm_save_file
!= NULL
) {
454 nargv
[i
++] = g_strdup("--sm-save-file");
455 nargv
[i
++] = ob_sm_save_file
;
457 if (ob_sm_id
!= NULL
) {
458 nargv
[i
++] = g_strdup("--sm-client-id");
459 nargv
[i
++] = ob_sm_id
;
461 nargv
[i
++] = g_strdup("--sm-no-load");
467 execvp(argv
[0], argv
); /* try how we were run */
468 execlp(argv
[0], program_name
, (gchar
*)NULL
); /* last resort */
471 /* free stuff passed in from the command line or environment */
472 g_free(ob_sm_save_file
);
474 g_free(program_name
);
482 static void signal_handler(gint signal
, gpointer data
)
486 ob_debug("Caught signal %d. Restarting.", signal
);
490 ob_debug("Caught signal %d. Reconfiguring.", signal
);
495 while (waitpid(-1, NULL
, WNOHANG
) > 0);
499 ob_debug("Caught signal %d. Ignoring.", signal
);
502 ob_debug("Caught signal %d. Exiting.", signal
);
503 /* TERM and INT return a 0 code */
504 ob_exit(!(signal
== SIGTERM
|| signal
== SIGINT
));
508 static void print_version(void)
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");
520 static void print_help(void)
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
);
546 static void remove_args(gint
*argc
, gchar
**argv
, gint index
, gint num
)
550 for (i
= index
; i
< *argc
- num
; ++i
)
551 argv
[i
] = argv
[i
+num
];
552 for (; i
< *argc
; ++i
)
557 static void run_startup_cmd(void)
563 if (!g_shell_parse_argv(startup_cmd
, NULL
, &argv
, &e
)) {
564 g_message("Error parsing startup command: %s",
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",
581 static void parse_env(void)
585 /* unset this so we don't pass it on unknowingly */
586 unsetenv("DESKTOP_STARTUP_ID");
588 /* this is how gnome-session passes in a session client id */
589 id
= g_getenv("DESKTOP_AUTOSTART_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",
600 static void parse_args(gint
*argc
, gchar
**argv
)
604 for (i
= 1; i
< *argc
; ++i
) {
605 if (!strcmp(argv
[i
], "--version")) {
609 else if (!strcmp(argv
[i
], "--help")) {
613 else if (!strcmp(argv
[i
], "--g-fatal-warnings")) {
614 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL
);
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 */
621 else if (!strcmp(argv
[i
], "--sync")) {
624 else if (!strcmp(argv
[i
], "--startup")) {
625 if (i
== *argc
- 1) /* no args left */
626 g_printerr(_("--startup requires an argument\n"));
628 /* this will be in the current locale encoding, which is
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
);
636 else if (!strcmp(argv
[i
], "--debug")) {
637 ob_debug_enable(OB_DEBUG_NORMAL
, TRUE
);
638 ob_debug_enable(OB_DEBUG_APP_BUGS
, TRUE
);
640 else if (!strcmp(argv
[i
], "--debug-focus")) {
641 ob_debug_enable(OB_DEBUG_FOCUS
, TRUE
);
643 else if (!strcmp(argv
[i
], "--debug-session")) {
644 ob_debug_enable(OB_DEBUG_SM
, TRUE
);
646 else if (!strcmp(argv
[i
], "--debug-xinerama")) {
647 ob_debug_xinerama
= TRUE
;
649 else if (!strcmp(argv
[i
], "--reconfigure")) {
652 else if (!strcmp(argv
[i
], "--restart")) {
655 else if (!strcmp(argv
[i
], "--exit")) {
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"));
662 /* this will be in the current locale encoding, which is
664 config_file
= argv
[i
+1];
665 ++i
; /* skip the argument */
666 ob_debug("--config-file %s", config_file
);
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");
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",
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");
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
);
692 else if (!strcmp(argv
[i
], "--sm-disable")) {
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 */
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
);
709 static Cursor
load_cursor(const gchar
*name
, guint fontval
)
714 c
= XcursorLibraryLoadCursor(obt_display
, name
);
717 c
= XCreateFontCursor(obt_display
, fontval
);
721 void ob_exit_with_error(const gchar
*msg
)
723 g_message("%s", msg
);
724 session_shutdown(TRUE
);
728 void ob_restart_other(const gchar
*path
)
730 restart_path
= g_strdup(path
);
734 void ob_restart(void)
740 void ob_reconfigure(void)
746 void ob_exit(gint code
)
749 g_main_loop_quit(ob_main_loop
);
752 void ob_exit_replace(void)
755 being_replaced
= TRUE
;
756 g_main_loop_quit(ob_main_loop
);
759 Cursor
ob_cursor(ObCursor cursor
)
761 g_assert(cursor
< OB_NUM_CURSORS
);
762 return cursors
[cursor
];
765 ObState
ob_state(void)
770 void ob_set_state(ObState s
)