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
;
55 gchar
*ob_sm_id
= NULL
;
58 RrInstance
*ob_rr_inst
= NULL
;
59 RrTheme
*ob_rr_theme
= NULL
;
60 Display
*ob_display
= NULL
;
64 gboolean ob_shutdown
= FALSE
;
65 gboolean ob_restart
= FALSE
;
66 char *ob_restart_path
= NULL
;
67 gboolean ob_remote
= TRUE
;
68 gboolean ob_sync
= FALSE
;
69 Cursor ob_cursors
[OB_NUM_CURSORS
];
70 KeyCode ob_keys
[OB_NUM_KEYS
];
71 char *ob_rc_path
= NULL
;
73 static void signal_handler(const ObEvent
*e
, void *data
);
74 static void parse_args(int argc
, char **argv
);
76 static void sm_startup(int argc
, char **argv
);
77 static void sm_shutdown();
80 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
81 Bool shutdown
, int interact_style
, Bool fast
);
82 static void sm_die(SmcConn conn
, SmPointer data
);
83 static void sm_save_complete(SmcConn conn
, SmPointer data
);
84 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
);
86 static void exit_with_error(gchar
*msg
);
88 int main(int argc
, char **argv
)
90 struct sigaction action
;
96 ob_state
= OB_STATE_STARTING
;
98 /* initialize the locale */
99 if (!setlocale(LC_ALL
, ""))
100 g_warning("Couldn't set locale from environment.\n");
101 bindtextdomain(PACKAGE_NAME
, LOCALEDIR
);
102 bind_textdomain_codeset(PACKAGE_NAME
, "UTF-8");
103 textdomain(PACKAGE_NAME
);
105 /* start our event dispatcher and register for signals */
107 dispatch_register(Event_Signal
, signal_handler
, NULL
);
109 /* set up signal handler */
110 sigemptyset(&sigset
);
111 action
.sa_handler
= dispatch_signal
;
112 action
.sa_mask
= sigset
;
113 action
.sa_flags
= SA_NOCLDSTOP
| SA_NODEFER
;
114 sigaction(SIGUSR1
, &action
, (struct sigaction
*) NULL
);
115 sigaction(SIGPIPE
, &action
, (struct sigaction
*) NULL
);
116 /* sigaction(SIGSEGV, &action, (struct sigaction *) NULL);*/
117 sigaction(SIGFPE
, &action
, (struct sigaction
*) NULL
);
118 sigaction(SIGTERM
, &action
, (struct sigaction
*) NULL
);
119 sigaction(SIGINT
, &action
, (struct sigaction
*) NULL
);
120 sigaction(SIGHUP
, &action
, (struct sigaction
*) NULL
);
122 /* create the ~/.openbox dir */
123 path
= g_build_filename(g_get_home_dir(), ".openbox", NULL
);
124 mkdir(path
, (S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IWGRP
| S_IXGRP
|
125 S_IROTH
| S_IWOTH
| S_IXOTH
));
127 /* create the ~/.openbox/themes dir */
128 path
= g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL
);
129 mkdir(path
, (S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IWGRP
| S_IXGRP
|
130 S_IROTH
| S_IWOTH
| S_IXOTH
));
133 g_set_prgname(argv
[0]);
135 /* parse out command line args */
136 parse_args(argc
, argv
);
138 ob_display
= XOpenDisplay(NULL
);
139 if (ob_display
== NULL
)
140 exit_with_error("Failed to open the display.");
141 if (fcntl(ConnectionNumber(ob_display
), F_SETFD
, 1) == -1)
142 exit_with_error("Failed to set display as close-on-exec.");
144 sm_startup(argc
, argv
);
147 ob_sn_display
= sn_display_new(ob_display
, NULL
, NULL
);
150 ob_screen
= DefaultScreen(ob_display
);
151 ob_root
= RootWindow(ob_display
, ob_screen
);
153 ob_rr_inst
= RrInstanceNew(ob_display
, ob_screen
);
154 if (ob_rr_inst
== NULL
)
155 exit_with_error("Failed to initialize the render library.");
157 /* XXX fork self onto other screens */
159 XSynchronize(ob_display
, ob_sync
);
161 /* check for locale support */
162 if (!XSupportsLocale())
163 g_warning("X server does not support locale.");
164 if (!XSetLocaleModifiers(""))
165 g_warning("Cannot set locale modifiers for the X server.");
167 /* set our error handler */
168 XSetErrorHandler(xerror_handler
);
170 /* set the DISPLAY environment variable for any lauched children, to the
171 display we're using, so they open in the right place. */
172 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display
)));
174 /* create available cursors */
175 ob_cursors
[OB_CURSOR_POINTER
] =
176 XCreateFontCursor(ob_display
, XC_left_ptr
);
177 ob_cursors
[OB_CURSOR_BUSY
] =
178 XCreateFontCursor(ob_display
, XC_watch
);
179 ob_cursors
[OB_CURSOR_MOVE
] =
180 XCreateFontCursor(ob_display
, XC_fleur
);
181 ob_cursors
[OB_CURSOR_NORTH
] =
182 XCreateFontCursor(ob_display
, XC_top_side
);
183 ob_cursors
[OB_CURSOR_NORTHEAST
] =
184 XCreateFontCursor(ob_display
, XC_top_right_corner
);
185 ob_cursors
[OB_CURSOR_EAST
] =
186 XCreateFontCursor(ob_display
, XC_right_side
);
187 ob_cursors
[OB_CURSOR_SOUTHEAST
] =
188 XCreateFontCursor(ob_display
, XC_bottom_right_corner
);
189 ob_cursors
[OB_CURSOR_SOUTH
] =
190 XCreateFontCursor(ob_display
, XC_bottom_side
);
191 ob_cursors
[OB_CURSOR_SOUTHWEST
] =
192 XCreateFontCursor(ob_display
, XC_bottom_left_corner
);
193 ob_cursors
[OB_CURSOR_WEST
] =
194 XCreateFontCursor(ob_display
, XC_left_side
);
195 ob_cursors
[OB_CURSOR_NORTHWEST
] =
196 XCreateFontCursor(ob_display
, XC_top_left_corner
);
198 /* create available keycodes */
199 ob_keys
[OB_KEY_RETURN
] =
200 XKeysymToKeycode(ob_display
, XStringToKeysym("Return"));
201 ob_keys
[OB_KEY_ESCAPE
] =
202 XKeysymToKeycode(ob_display
, XStringToKeysym("Escape"));
203 ob_keys
[OB_KEY_LEFT
] =
204 XKeysymToKeycode(ob_display
, XStringToKeysym("Left"));
205 ob_keys
[OB_KEY_RIGHT
] =
206 XKeysymToKeycode(ob_display
, XStringToKeysym("Right"));
208 XKeysymToKeycode(ob_display
, XStringToKeysym("Up"));
209 ob_keys
[OB_KEY_DOWN
] =
210 XKeysymToKeycode(ob_display
, XStringToKeysym("Down"));
212 prop_startup(); /* get atoms values for the display */
213 extensions_query_all(); /* find which extensions are present */
215 /* save stuff that we can use to restore state */
218 if (screen_annex()) { /* it will be ours! */
219 /* startup the parsing so everything can register sections of the rc */
222 /* anything that is going to read data from the rc file needs to be
227 /* focus_backup is used for stacking, so this needs to come before
228 anything that calls stacking_add */
232 /* load the plugins specified in the pluginrc */
235 /* set up the kernel config shit */
238 /* parse/load user options */
239 if (parse_load_rc(&doc
, &node
))
240 parse_tree(doc
, node
->xmlChildrenNode
, NULL
);
241 /* we're done with parsing now, kill it */
244 /* load the theme specified in the rc file */
245 ob_rr_theme
= RrThemeNew(ob_rr_inst
, config_theme
);
246 if (ob_rr_theme
== NULL
)
247 exit_with_error("Unable to load a theme.");
250 moveresize_startup();
256 /* call startup for all the plugins */
259 /* get all the existing windows */
262 ob_state
= OB_STATE_RUNNING
;
265 ob_state
= OB_STATE_EXITING
;
268 client_unmanage_all();
270 plugin_shutdown(); /* calls all the plugins' shutdown functions */
276 moveresize_shutdown();
288 RrThemeFree(ob_rr_theme
);
289 RrInstanceFree(ob_rr_inst
);
293 XCloseDisplay(ob_display
);
296 if (ob_restart_path
!= NULL
) {
302 if (g_shell_parse_argv(ob_restart_path
, &argcp
, &argvp
, &err
)) {
303 execvp(argvp
[0], argvp
);
306 g_warning("failed to execute '%s': %s", ob_restart_path
,
312 execvp(argv
[0], argv
); /* try how we were run */
318 static void sm_startup(int argc
, char **argv
)
322 #define SM_ERR_LEN 1024
325 char sm_err
[SM_ERR_LEN
];
327 cb
.save_yourself
.callback
= sm_save_yourself
;
328 cb
.save_yourself
.client_data
= NULL
;
330 cb
.die
.callback
= sm_die
;
331 cb
.die
.client_data
= NULL
;
333 cb
.save_complete
.callback
= sm_save_complete
;
334 cb
.save_complete
.client_data
= NULL
;
336 cb
.shutdown_cancelled
.callback
= sm_shutdown_cancelled
;
337 cb
.shutdown_cancelled
.client_data
= NULL
;
339 ob_sm_conn
= SmcOpenConnection(NULL
, NULL
, 1, 0,
340 SmcSaveYourselfProcMask
|
342 SmcSaveCompleteProcMask
|
343 SmcShutdownCancelledProcMask
,
344 &cb
, ob_sm_id
, &ob_sm_id
,
346 if (ob_sm_conn
== NULL
)
347 g_warning("Failed to connect to session manager: %s", sm_err
);
349 SmPropValue val_prog
;
351 SmPropValue val_hint
;
354 SmProp prop_cmd
= { SmCloneCommand
, SmLISTofARRAY8
, 1, };
355 SmProp prop_res
= { SmRestartCommand
, SmLISTofARRAY8
, };
356 SmProp prop_prog
= { SmProgram
, SmARRAY8
, 1, };
357 SmProp prop_uid
= { SmUserID
, SmARRAY8
, 1, };
358 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
359 SmProp prop_pid
= { SmProcessID
, SmARRAY8
, 1, };
360 SmProp prop_pri
= { "_GSM_Priority", SmCARD8
, 1, };
367 for (i
= 1; i
< argc
- 1; ++i
)
368 if (strcmp(argv
[i
], "--sm-client-id") == 0)
370 has_id
= (i
< argc
- 1);
372 prop_cmd
.vals
= g_new(SmPropValue
, (has_id
? argc
-2 : argc
));
373 prop_cmd
.num_vals
= (has_id
? argc
-2 : argc
);
374 for (i
= 0, j
= 0; i
< argc
; ++i
, ++j
) {
375 if (strcmp (argv
[i
], "--sm-client-id") == 0) {
376 ++i
, --j
; /* skip the next as well, keep j where it is */
378 prop_cmd
.vals
[j
].value
= argv
[i
];
379 prop_cmd
.vals
[j
].length
= strlen(argv
[i
]);
383 prop_res
.vals
= g_new(SmPropValue
, (has_id
? argc
: argc
+2));
384 prop_res
.num_vals
= (has_id
? argc
: argc
+2);
385 for (i
= 0, j
= 0; i
< argc
; ++i
, ++j
) {
386 if (strcmp (argv
[i
], "--sm-client-id") == 0) {
387 ++i
, --j
; /* skip the next as well, keep j where it is */
389 prop_res
.vals
[j
].value
= argv
[i
];
390 prop_res
.vals
[j
].length
= strlen(argv
[i
]);
393 prop_res
.vals
[j
].value
= "--sm-client-id";
394 prop_res
.vals
[j
++].length
= strlen("--sm-client-id");
395 prop_res
.vals
[j
].value
= ob_sm_id
;
396 prop_res
.vals
[j
++].length
= strlen(ob_sm_id
);
398 val_prog
.value
= argv
[0];
399 val_prog
.length
= strlen(argv
[0]);
401 val_uid
.value
= g_strdup(g_get_user_name());
402 val_uid
.length
= strlen(val_uid
.value
);
404 hint
= SmRestartImmediately
;
405 val_hint
.value
= &hint
;
408 sprintf(pid
, "%ld", (long)getpid());
410 val_pid
.length
= strlen(pid
);
412 /* priority with gnome-session-manager, low to run before other apps */
414 val_pri
.value
= &pri
;
417 prop_prog
.vals
= &val_prog
;
418 prop_uid
.vals
= &val_uid
;
419 prop_hint
.vals
= &val_hint
;
420 prop_pid
.vals
= &val_pid
;
421 prop_pri
.vals
= &val_pri
;
423 props
[0] = &prop_prog
;
424 props
[1] = &prop_cmd
;
425 props
[2] = &prop_res
;
426 props
[3] = &prop_uid
;
427 props
[4] = &prop_hint
;
428 props
[5] = &prop_pid
;
429 props
[6] = &prop_pri
;
431 SmcSetProperties(ob_sm_conn
, 7, props
);
433 g_free(val_uid
.value
);
434 g_free(prop_cmd
.vals
);
435 g_free(prop_res
.vals
);
437 g_message("Connected to session manager with id %s", ob_sm_id
);
443 static void sm_shutdown()
447 SmPropValue val_hint
;
448 SmProp prop_hint
= { SmRestartStyleHint
, SmCARD8
, 1, };
452 /* when we exit, we want to reset this to a more friendly state */
453 hint
= SmRestartIfRunning
;
454 val_hint
.value
= &hint
;
457 prop_hint
.vals
= &val_hint
;
459 props
[0] = &prop_hint
;
461 SmcSetProperties(ob_sm_conn
, 1, props
);
463 SmcCloseConnection(ob_sm_conn
, 0, NULL
);
468 static void signal_handler(const ObEvent
*e
, void *data
)
472 s
= e
->data
.s
.signal
;
475 fprintf(stderr
, "Caught SIGUSR1 signal. Restarting.");
476 ob_shutdown
= ob_restart
= TRUE
;
483 fprintf(stderr
, "Caught signal %d. Exiting.", s
);
489 fprintf(stderr
, "Caught signal %d. Aborting and dumping core.", s
);
494 static void print_version()
496 g_print("Openbox %s\n\n", PACKAGE_VERSION
);
497 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
498 g_print("This is free software, and you are welcome to redistribute it\n");
499 g_print("under certain conditions. See the file COPYING for details.\n\n");
502 static void print_help()
505 g_print("Syntax: openbox [options]\n\n");
506 g_print("Options:\n\n");
507 g_print(" --rc PATH Specify the path to the rc file to use\n");
509 g_print(" --sm-client-id ID Specify session management ID\n");
510 g_print(" --sm-disable Disable connection to session manager\n");
512 g_print(" --help Display this help and exit\n");
513 g_print(" --version Display the version and exit\n");
514 g_print(" --sync Run in synchronous mode (this is slow and\n"
515 " meant for debugging X routines)\n");
516 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT
);
519 static void parse_args(int argc
, char **argv
)
523 for (i
= 1; i
< argc
; ++i
) {
524 if (!strcmp(argv
[i
], "--version")) {
527 } else if (!strcmp(argv
[i
], "--help")) {
530 } else if (!strcmp(argv
[i
], "--sync")) {
532 } else if (!strcmp(argv
[i
], "--rc")) {
533 if (i
== argc
- 1) /* no args left */
534 g_printerr(_("--rc requires an argument\n"));
536 ob_rc_path
= argv
[++i
];
538 } else if (!strcmp(argv
[i
], "--sm-client-id")) {
539 if (i
== argc
- 1) /* no args left */
540 g_printerr(_("--sm-client-id requires an argument\n"));
542 ob_sm_id
= argv
[++i
];
543 } else if (!strcmp(argv
[i
], "--sm-disable")) {
547 g_printerr("Invalid option: '%s'\n\n", argv
[i
]);
554 gboolean
ob_pointer_pos(int *x
, int *y
)
560 return !!XQueryPointer(ob_display
, ob_root
, &w
, &w
, x
, y
, &i
, &i
, &u
);
564 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
565 Bool shutdown
, int interact_style
, Bool fast
)
567 g_message("got SAVE YOURSELF from session manager");
568 SmcSaveYourselfDone(conn
, TRUE
);
571 static void sm_die(SmcConn conn
, SmPointer data
)
574 g_message("got DIE from session manager");
577 static void sm_save_complete(SmcConn conn
, SmPointer data
)
579 g_message("got SAVE COMPLETE from session manager");
582 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
)
584 g_message("got SHUTDOWN CANCELLED from session manager");
588 static void exit_with_error(gchar
*msg
)
595 Cursor
ob_cursor(ObCursor cursor
)
597 g_assert(cursor
< OB_NUM_CURSORS
);
598 return ob_cursors
[cursor
];
601 KeyCode
ob_keycode(ObKey key
)
603 g_assert(key
< OB_NUM_KEYS
);