12 #include "moveresize.h"
14 #include "extensions.h"
21 #include "parser/parse.h"
22 #include "render/render.h"
23 #include "render/theme.h"
41 #ifdef HAVE_SYS_STAT_H
42 # include <sys/stat.h>
43 # include <sys/types.h>
47 #include <X11/SM/SMlib.h>
50 #include <X11/cursorfont.h>
53 gboolean ob_sm_use
= TRUE
;
58 RrInstance
*ob_rr_inst
;
62 Cursor ob_cursors
[OB_NUM_CURSORS
];
63 KeyCode ob_keys
[OB_NUM_KEYS
];
66 static gboolean xsync
;
67 static gboolean shutdown
;
68 static gboolean restart
;
69 static char *restart_path
;
71 static void signal_handler(const ObEvent
*e
, void *data
);
72 static void parse_args(int argc
, char **argv
);
74 static void sm_startup(int argc
, char **argv
);
75 static void sm_shutdown();
78 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
79 Bool shutdown
, int interact_style
, Bool fast
);
80 static void sm_die(SmcConn conn
, SmPointer data
);
81 static void sm_save_complete(SmcConn conn
, SmPointer data
);
82 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
);
84 static void exit_with_error(gchar
*msg
);
86 int main(int argc
, char **argv
)
88 struct sigaction action
;
94 state
= OB_STATE_STARTING
;
96 /* initialize the locale */
97 if (!setlocale(LC_ALL
, ""))
98 g_warning("Couldn't set locale from environment.\n");
99 bindtextdomain(PACKAGE_NAME
, LOCALEDIR
);
100 bind_textdomain_codeset(PACKAGE_NAME
, "UTF-8");
101 textdomain(PACKAGE_NAME
);
103 /* start our event dispatcher and register for signals */
105 dispatch_register(Event_Signal
, signal_handler
, NULL
);
107 /* set up signal handler */
108 sigemptyset(&sigset
);
109 action
.sa_handler
= dispatch_signal
;
110 action
.sa_mask
= sigset
;
111 action
.sa_flags
= SA_NOCLDSTOP
| SA_NODEFER
;
112 sigaction(SIGUSR1
, &action
, (struct sigaction
*) NULL
);
113 sigaction(SIGPIPE
, &action
, (struct sigaction
*) NULL
);
114 /* sigaction(SIGSEGV, &action, (struct sigaction *) NULL);*/
115 sigaction(SIGFPE
, &action
, (struct sigaction
*) NULL
);
116 sigaction(SIGTERM
, &action
, (struct sigaction
*) NULL
);
117 sigaction(SIGINT
, &action
, (struct sigaction
*) NULL
);
118 sigaction(SIGHUP
, &action
, (struct sigaction
*) NULL
);
120 /* create the ~/.openbox dir */
121 path
= g_build_filename(g_get_home_dir(), ".openbox", NULL
);
122 mkdir(path
, (S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IWGRP
| S_IXGRP
|
123 S_IROTH
| S_IWOTH
| S_IXOTH
));
125 /* create the ~/.openbox/themes dir */
126 path
= g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL
);
127 mkdir(path
, (S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IWGRP
| S_IXGRP
|
128 S_IROTH
| S_IWOTH
| S_IXOTH
));
131 g_set_prgname(argv
[0]);
133 /* parse out command line args */
134 parse_args(argc
, argv
);
136 ob_display
= XOpenDisplay(NULL
);
137 if (ob_display
== NULL
)
138 exit_with_error("Failed to open the display.");
139 if (fcntl(ConnectionNumber(ob_display
), F_SETFD
, 1) == -1)
140 exit_with_error("Failed to set display as close-on-exec.");
142 sm_startup(argc
, argv
);
145 ob_sn_display
= sn_display_new(ob_display
, NULL
, NULL
);
148 ob_screen
= DefaultScreen(ob_display
);
150 ob_rr_inst
= RrInstanceNew(ob_display
, ob_screen
);
151 if (ob_rr_inst
== NULL
)
152 exit_with_error("Failed to initialize the render library.");
154 /* XXX fork self onto other screens */
156 XSynchronize(ob_display
, xsync
);
158 /* check for locale support */
159 if (!XSupportsLocale())
160 g_warning("X server does not support locale.");
161 if (!XSetLocaleModifiers(""))
162 g_warning("Cannot set locale modifiers for the X server.");
164 /* set our error handler */
165 XSetErrorHandler(xerror_handler
);
167 /* set the DISPLAY environment variable for any lauched children, to the
168 display we're using, so they open in the right place. */
169 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display
)));
171 /* create available cursors */
172 ob_cursors
[OB_CURSOR_POINTER
] =
173 XCreateFontCursor(ob_display
, XC_left_ptr
);
174 ob_cursors
[OB_CURSOR_BUSY
] =
175 XCreateFontCursor(ob_display
, XC_watch
);
176 ob_cursors
[OB_CURSOR_MOVE
] =
177 XCreateFontCursor(ob_display
, XC_fleur
);
178 ob_cursors
[OB_CURSOR_NORTH
] =
179 XCreateFontCursor(ob_display
, XC_top_side
);
180 ob_cursors
[OB_CURSOR_NORTHEAST
] =
181 XCreateFontCursor(ob_display
, XC_top_right_corner
);
182 ob_cursors
[OB_CURSOR_EAST
] =
183 XCreateFontCursor(ob_display
, XC_right_side
);
184 ob_cursors
[OB_CURSOR_SOUTHEAST
] =
185 XCreateFontCursor(ob_display
, XC_bottom_right_corner
);
186 ob_cursors
[OB_CURSOR_SOUTH
] =
187 XCreateFontCursor(ob_display
, XC_bottom_side
);
188 ob_cursors
[OB_CURSOR_SOUTHWEST
] =
189 XCreateFontCursor(ob_display
, XC_bottom_left_corner
);
190 ob_cursors
[OB_CURSOR_WEST
] =
191 XCreateFontCursor(ob_display
, XC_left_side
);
192 ob_cursors
[OB_CURSOR_NORTHWEST
] =
193 XCreateFontCursor(ob_display
, XC_top_left_corner
);
195 /* create available keycodes */
196 ob_keys
[OB_KEY_RETURN
] =
197 XKeysymToKeycode(ob_display
, XStringToKeysym("Return"));
198 ob_keys
[OB_KEY_ESCAPE
] =
199 XKeysymToKeycode(ob_display
, XStringToKeysym("Escape"));
200 ob_keys
[OB_KEY_LEFT
] =
201 XKeysymToKeycode(ob_display
, XStringToKeysym("Left"));
202 ob_keys
[OB_KEY_RIGHT
] =
203 XKeysymToKeycode(ob_display
, XStringToKeysym("Right"));
205 XKeysymToKeycode(ob_display
, XStringToKeysym("Up"));
206 ob_keys
[OB_KEY_DOWN
] =
207 XKeysymToKeycode(ob_display
, XStringToKeysym("Down"));
209 prop_startup(); /* get atoms values for the display */
210 extensions_query_all(); /* find which extensions are present */
212 /* save stuff that we can use to restore state */
215 if (screen_annex()) { /* it will be ours! */
216 /* startup the parsing so everything can register sections of the rc */
219 /* anything that is going to read data from the rc file needs to be
224 /* focus_backup is used for stacking, so this needs to come before
225 anything that calls stacking_add */
229 /* load the plugins specified in the pluginrc */
232 /* set up the kernel config shit */
235 /* parse/load user options */
236 if (parse_load_rc(&doc
, &node
))
237 parse_tree(doc
, node
->xmlChildrenNode
, NULL
);
238 /* we're done with parsing now, kill it */
241 /* load the theme specified in the rc file */
242 ob_rr_theme
= RrThemeNew(ob_rr_inst
, config_theme
);
243 if (ob_rr_theme
== NULL
)
244 exit_with_error("Unable to load a theme.");
246 moveresize_startup();
252 /* call startup for all the plugins */
255 /* get all the existing windows */
258 state
= OB_STATE_RUNNING
;
261 state
= OB_STATE_EXITING
;
264 client_unmanage_all();
266 plugin_shutdown(); /* calls all the plugins' shutdown functions */
272 moveresize_shutdown();
283 RrThemeFree(ob_rr_theme
);
284 RrInstanceFree(ob_rr_inst
);
288 XCloseDisplay(ob_display
);
291 if (restart_path
!= NULL
) {
297 if (g_shell_parse_argv(restart_path
, &argcp
, &argvp
, &err
)) {
298 execvp(argvp
[0], argvp
);
301 g_warning("failed to execute '%s': %s", restart_path
,
307 execvp(argv
[0], argv
); /* try how we were run */
313 static void sm_startup(int argc
, char **argv
)
317 #define SM_ERR_LEN 1024
320 char sm_err
[SM_ERR_LEN
];
322 cb
.save_yourself
.callback
= sm_save_yourself
;
323 cb
.save_yourself
.client_data
= NULL
;
325 cb
.die
.callback
= sm_die
;
326 cb
.die
.client_data
= NULL
;
328 cb
.save_complete
.callback
= sm_save_complete
;
329 cb
.save_complete
.client_data
= NULL
;
331 cb
.shutdown_cancelled
.callback
= sm_shutdown_cancelled
;
332 cb
.shutdown_cancelled
.client_data
= NULL
;
334 ob_sm_conn
= SmcOpenConnection(NULL
, NULL
, 1, 0,
335 SmcSaveYourselfProcMask
|
337 SmcSaveCompleteProcMask
|
338 SmcShutdownCancelledProcMask
,
339 &cb
, ob_sm_id
, &ob_sm_id
,
341 if (ob_sm_conn
== NULL
)
342 g_warning("Failed to connect to session manager: %s", sm_err
);
344 SmPropValue val_prog
;
346 SmPropValue val_hint
;
349 SmProp prop_cmd
= { SmCloneCommand
, SmLISTofARRAY8
, 1, };
350 SmProp prop_res
= { SmRestartCommand
, SmLISTofARRAY8
, };
351 SmProp prop_prog
= { SmProgram
, SmARRAY8
, 1, };
352 SmProp prop_uid
= { SmUserID
, SmARRAY8
, 1, };
353 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
354 SmProp prop_pid
= { SmProcessID
, SmARRAY8
, 1, };
355 SmProp prop_pri
= { "_GSM_Priority", SmCARD8
, 1, };
362 for (i
= 1; i
< argc
- 1; ++i
)
363 if (strcmp(argv
[i
], "--sm-client-id") == 0)
365 has_id
= (i
< argc
- 1);
367 prop_cmd
.vals
= g_new(SmPropValue
, (has_id
? argc
-2 : argc
));
368 prop_cmd
.num_vals
= (has_id
? argc
-2 : argc
);
369 for (i
= 0, j
= 0; i
< argc
; ++i
, ++j
) {
370 if (strcmp (argv
[i
], "--sm-client-id") == 0) {
371 ++i
, --j
; /* skip the next as well, keep j where it is */
373 prop_cmd
.vals
[j
].value
= argv
[i
];
374 prop_cmd
.vals
[j
].length
= strlen(argv
[i
]);
378 prop_res
.vals
= g_new(SmPropValue
, (has_id
? argc
: argc
+2));
379 prop_res
.num_vals
= (has_id
? argc
: argc
+2);
380 for (i
= 0, j
= 0; i
< argc
; ++i
, ++j
) {
381 if (strcmp (argv
[i
], "--sm-client-id") == 0) {
382 ++i
, --j
; /* skip the next as well, keep j where it is */
384 prop_res
.vals
[j
].value
= argv
[i
];
385 prop_res
.vals
[j
].length
= strlen(argv
[i
]);
388 prop_res
.vals
[j
].value
= "--sm-client-id";
389 prop_res
.vals
[j
++].length
= strlen("--sm-client-id");
390 prop_res
.vals
[j
].value
= ob_sm_id
;
391 prop_res
.vals
[j
++].length
= strlen(ob_sm_id
);
393 val_prog
.value
= argv
[0];
394 val_prog
.length
= strlen(argv
[0]);
396 val_uid
.value
= g_strdup(g_get_user_name());
397 val_uid
.length
= strlen(val_uid
.value
);
399 hint
= SmRestartImmediately
;
400 val_hint
.value
= &hint
;
403 sprintf(pid
, "%ld", (long)getpid());
405 val_pid
.length
= strlen(pid
);
407 /* priority with gnome-session-manager, low to run before other apps */
409 val_pri
.value
= &pri
;
412 prop_prog
.vals
= &val_prog
;
413 prop_uid
.vals
= &val_uid
;
414 prop_hint
.vals
= &val_hint
;
415 prop_pid
.vals
= &val_pid
;
416 prop_pri
.vals
= &val_pri
;
418 props
[0] = &prop_prog
;
419 props
[1] = &prop_cmd
;
420 props
[2] = &prop_res
;
421 props
[3] = &prop_uid
;
422 props
[4] = &prop_hint
;
423 props
[5] = &prop_pid
;
424 props
[6] = &prop_pri
;
426 SmcSetProperties(ob_sm_conn
, 7, props
);
428 g_free(val_uid
.value
);
429 g_free(prop_cmd
.vals
);
430 g_free(prop_res
.vals
);
432 g_message("Connected to session manager with id %s", ob_sm_id
);
438 static void sm_shutdown()
442 SmPropValue val_hint
;
443 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
447 /* when we exit, we want to reset this to a more friendly state */
448 hint
= SmRestartIfRunning
;
449 val_hint
.value
= &hint
;
452 prop_hint
.vals
= &val_hint
;
454 props
[0] = &prop_hint
;
456 SmcSetProperties(ob_sm_conn
, 1, props
);
458 SmcCloseConnection(ob_sm_conn
, 0, NULL
);
463 static void signal_handler(const ObEvent
*e
, void *data
)
467 s
= e
->data
.s
.signal
;
470 fprintf(stderr
, "Caught SIGUSR1 signal. Restarting.");
478 fprintf(stderr
, "Caught signal %d. Exiting.", s
);
484 fprintf(stderr
, "Caught signal %d. Aborting and dumping core.", s
);
489 static void print_version()
491 g_print("Openbox %s\n\n", PACKAGE_VERSION
);
492 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
493 g_print("This is free software, and you are welcome to redistribute it\n");
494 g_print("under certain conditions. See the file COPYING for details.\n\n");
497 static void print_help()
500 g_print("Syntax: openbox [options]\n\n");
501 g_print("Options:\n\n");
503 g_print(" --sm-client-id ID Specify session management ID\n");
504 g_print(" --sm-disable Disable connection to session manager\n");
506 g_print(" --help Display this help and exit\n");
507 g_print(" --version Display the version and exit\n");
508 g_print(" --sync Run in synchronous mode (this is slow and\n"
509 " meant for debugging X routines)\n");
510 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT
);
513 static void parse_args(int argc
, char **argv
)
517 for (i
= 1; i
< argc
; ++i
) {
518 if (!strcmp(argv
[i
], "--version")) {
521 } else if (!strcmp(argv
[i
], "--help")) {
524 } else if (!strcmp(argv
[i
], "--g-fatal-warnings")) {
525 g_log_set_always_fatal(G_LOG_LEVEL_CRITICAL
);
526 } else if (!strcmp(argv
[i
], "--sync")) {
529 } else if (!strcmp(argv
[i
], "--sm-client-id")) {
530 if (i
== argc
- 1) /* no args left */
531 g_printerr(_("--sm-client-id requires an argument\n"));
533 ob_sm_id
= argv
[++i
];
534 } else if (!strcmp(argv
[i
], "--sm-disable")) {
538 g_printerr("Invalid option: '%s'\n\n", argv
[i
]);
546 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
547 Bool shutdown
, int interact_style
, Bool fast
)
549 g_message("got SAVE YOURSELF from session manager");
550 SmcSaveYourselfDone(conn
, TRUE
);
553 static void sm_die(SmcConn conn
, SmPointer data
)
556 g_message("got DIE from session manager");
559 static void sm_save_complete(SmcConn conn
, SmPointer data
)
561 g_message("got SAVE COMPLETE from session manager");
564 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
)
566 g_message("got SHUTDOWN CANCELLED from session manager");
570 static void exit_with_error(gchar
*msg
)
577 void ob_restart_other(const gchar
*path
)
579 restart_path
= g_strdup(path
);
594 Cursor
ob_cursor(ObCursor cursor
)
596 g_assert(cursor
< OB_NUM_CURSORS
);
597 return ob_cursors
[cursor
];
600 KeyCode
ob_keycode(ObKey key
)
602 g_assert(key
< OB_NUM_KEYS
);