12 #include "moveresize.h"
14 #include "extensions.h"
21 #include "parser/parse.h"
22 #include "render/render.h"
23 #include "render/theme.h"
40 #ifdef HAVE_SYS_STAT_H
41 # include <sys/stat.h>
42 # include <sys/types.h>
46 #include <X11/SM/SMlib.h>
49 #include <X11/cursorfont.h>
53 gchar
*ob_sm_id
= NULL
;
56 RrInstance
*ob_rr_inst
= NULL
;
57 RrTheme
*ob_rr_theme
= NULL
;
58 Display
*ob_display
= NULL
;
62 gboolean ob_shutdown
= FALSE
;
63 gboolean ob_restart
= FALSE
;
64 char *ob_restart_path
= NULL
;
65 gboolean ob_remote
= TRUE
;
66 gboolean ob_sync
= FALSE
;
68 char *ob_rc_path
= NULL
;
70 static void signal_handler(const ObEvent
*e
, void *data
);
71 static void parse_args(int argc
, char **argv
);
73 static void sm_startup(int argc
, char **argv
);
74 static void sm_shutdown();
77 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
78 Bool shutdown
, int interact_style
, Bool fast
);
79 static void sm_die(SmcConn conn
, SmPointer data
);
80 static void sm_save_complete(SmcConn conn
, SmPointer data
);
81 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
);
83 static void exit_with_error(gchar
*msg
);
85 int main(int argc
, char **argv
)
87 struct sigaction action
;
93 ob_state
= State_Starting
;
95 /* initialize the locale */
96 if (!setlocale(LC_ALL
, ""))
97 g_warning("Couldn't set locale from environment.\n");
98 bindtextdomain(PACKAGE_NAME
, LOCALEDIR
);
99 bind_textdomain_codeset(PACKAGE_NAME
, "UTF-8");
100 textdomain(PACKAGE_NAME
);
102 /* start our event dispatcher and register for signals */
104 dispatch_register(Event_Signal
, signal_handler
, NULL
);
106 /* set up signal handler */
107 sigemptyset(&sigset
);
108 action
.sa_handler
= dispatch_signal
;
109 action
.sa_mask
= sigset
;
110 action
.sa_flags
= SA_NOCLDSTOP
;
111 sigaction(SIGUSR1
, &action
, (struct sigaction
*) NULL
);
112 sigaction(SIGPIPE
, &action
, (struct sigaction
*) NULL
);
113 sigaction(SIGSEGV
, &action
, (struct sigaction
*) NULL
);
114 sigaction(SIGFPE
, &action
, (struct sigaction
*) NULL
);
115 sigaction(SIGTERM
, &action
, (struct sigaction
*) NULL
);
116 sigaction(SIGINT
, &action
, (struct sigaction
*) NULL
);
117 sigaction(SIGHUP
, &action
, (struct sigaction
*) NULL
);
119 /* create the ~/.openbox dir */
120 path
= g_build_filename(g_get_home_dir(), ".openbox", NULL
);
121 mkdir(path
, (S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IWGRP
| S_IXGRP
|
122 S_IROTH
| S_IWOTH
| S_IXOTH
));
124 /* create the ~/.openbox/themes dir */
125 path
= g_build_filename(g_get_home_dir(), ".openbox", "themes", NULL
);
126 mkdir(path
, (S_IRUSR
| S_IWUSR
| S_IXUSR
| S_IRGRP
| S_IWGRP
| S_IXGRP
|
127 S_IROTH
| S_IWOTH
| S_IXOTH
));
130 /* parse out command line args */
131 parse_args(argc
, argv
);
133 ob_display
= XOpenDisplay(NULL
);
134 if (ob_display
== NULL
)
135 exit_with_error("Failed to open the display.");
136 if (fcntl(ConnectionNumber(ob_display
), F_SETFD
, 1) == -1)
137 exit_with_error("Failed to set display as close-on-exec.");
139 sm_startup(argc
, argv
);
142 ob_sn_display
= sn_display_new(ob_display
, NULL
, NULL
);
145 ob_screen
= DefaultScreen(ob_display
);
146 ob_root
= RootWindow(ob_display
, ob_screen
);
148 ob_rr_inst
= RrInstanceNew(ob_display
, ob_screen
);
149 if (ob_rr_inst
== NULL
)
150 exit_with_error("Failed to initialize the render library.");
152 /* XXX fork self onto other screens */
154 XSynchronize(ob_display
, ob_sync
);
156 /* check for locale support */
157 if (!XSupportsLocale())
158 g_warning("X server does not support locale.");
159 if (!XSetLocaleModifiers(""))
160 g_warning("Cannot set locale modifiers for the X server.");
162 /* set our error handler */
163 XSetErrorHandler(xerror_handler
);
165 /* set the DISPLAY environment variable for any lauched children, to the
166 display we're using, so they open in the right place. */
167 putenv(g_strdup_printf("DISPLAY=%s", DisplayString(ob_display
)));
169 ob_cursors
.ptr
= XCreateFontCursor(ob_display
, XC_left_ptr
);
170 ob_cursors
.busy
= XCreateFontCursor(ob_display
, XC_watch
);
171 ob_cursors
.move
= XCreateFontCursor(ob_display
, XC_fleur
);
172 ob_cursors
.tl
= XCreateFontCursor(ob_display
, XC_top_left_corner
);
173 ob_cursors
.tr
= XCreateFontCursor(ob_display
, XC_top_right_corner
);
174 ob_cursors
.bl
= XCreateFontCursor(ob_display
, XC_bottom_left_corner
);
175 ob_cursors
.br
= XCreateFontCursor(ob_display
, XC_bottom_right_corner
);
176 ob_cursors
.t
= XCreateFontCursor(ob_display
, XC_top_side
);
177 ob_cursors
.r
= XCreateFontCursor(ob_display
, XC_right_side
);
178 ob_cursors
.b
= XCreateFontCursor(ob_display
, XC_bottom_side
);
179 ob_cursors
.l
= XCreateFontCursor(ob_display
, XC_left_side
);
181 prop_startup(); /* get atoms values for the display */
182 extensions_query_all(); /* find which extensions are present */
184 /* save stuff that we can use to restore state */
187 if (screen_annex()) { /* it will be ours! */
188 /* startup the parsing so everything can register sections of the rc */
191 /* anything that is going to read data from the rc file needs to be
197 /* load the plugins specified in the pluginrc */
200 /* set up the kernel config shit */
202 /* parse/load user options */
203 if (parse_load_rc(&doc
, &node
))
204 parse_tree(doc
, node
->xmlChildrenNode
, NULL
);
205 /* we're done with parsing now, kill it */
208 /* load the theme specified in the rc file */
209 ob_rr_theme
= RrThemeNew(ob_rr_inst
, config_theme
);
210 if (ob_rr_theme
== NULL
)
211 exit_with_error("Unable to load a theme.");
216 moveresize_startup();
223 /* call startup for all the plugins */
226 /* get all the existing windows */
229 ob_state
= State_Running
;
232 ob_state
= State_Exiting
;
235 client_unmanage_all();
237 plugin_shutdown(); /* calls all the plugins' shutdown functions */
243 moveresize_shutdown();
255 RrThemeFree(ob_rr_theme
);
256 RrInstanceFree(ob_rr_inst
);
260 XCloseDisplay(ob_display
);
263 if (ob_restart_path
!= NULL
) {
269 if (g_shell_parse_argv(ob_restart_path
, &argcp
, &argvp
, &err
)) {
270 execvp(argvp
[0], argvp
);
273 g_warning("failed to execute '%s': %s", ob_restart_path
,
279 execvp(argv
[0], argv
); /* try how we were run */
280 execlp(BINARY
, BINARY
, NULL
); /* try this as a last resort */
286 static void sm_startup(int argc
, char **argv
)
290 #define SM_ERR_LEN 1024
293 char sm_err
[SM_ERR_LEN
];
295 cb
.save_yourself
.callback
= sm_save_yourself
;
296 cb
.save_yourself
.client_data
= NULL
;
298 cb
.die
.callback
= sm_die
;
299 cb
.die
.client_data
= NULL
;
301 cb
.save_complete
.callback
= sm_save_complete
;
302 cb
.save_complete
.client_data
= NULL
;
304 cb
.shutdown_cancelled
.callback
= sm_shutdown_cancelled
;
305 cb
.shutdown_cancelled
.client_data
= NULL
;
307 ob_sm_conn
= SmcOpenConnection(NULL
, NULL
, 1, 0,
308 SmcSaveYourselfProcMask
|
310 SmcSaveCompleteProcMask
|
311 SmcShutdownCancelledProcMask
,
312 &cb
, ob_sm_id
, &ob_sm_id
,
314 if (ob_sm_conn
== NULL
)
315 g_warning("Failed to connect to session manager: %s", sm_err
);
319 SmPropValue val_prog
;
321 SmProp prop_cmd
= { SmCloneCommand
, "SmLISTofARRAY8", 1, };
322 SmProp prop_res
= { SmRestartCommand
, "SmLISTofARRAY8", 1, };
323 SmProp prop_prog
= { SmProgram
, "SmARRAY8", 1, };
324 SmProp prop_uid
= { SmUserID
, "SmARRAY8", 1, };
327 val_cmd
.value
= argv
[0];
328 val_cmd
.length
= strlen(argv
[0]);
329 val_res
.value
= argv
[0];
330 val_res
.length
= strlen(argv
[0]); /* XXX -id foo */
331 val_prog
.value
= argv
[0];
332 val_prog
.length
= strlen(argv
[0]);
334 val_uid
.value
= g_strdup_printf("%ld", (long)getuid());
335 val_uid
.length
= strlen(val_uid
.value
);
337 prop_cmd
.vals
= &val_cmd
;
338 prop_res
.vals
= &val_res
;
339 prop_prog
.vals
= &val_prog
;
340 prop_uid
.vals
= &val_uid
;
342 props
[0] = &prop_cmd
;
343 props
[1] = &prop_res
;
344 props
[2] = &prop_prog
;
345 props
[3] = &prop_uid
;
347 SmcSetProperties(ob_sm_conn
, 3, props
);
349 g_free(val_uid
.value
);
351 g_message("Connected to session manager with id %s", ob_sm_id
);
357 static void sm_shutdown()
361 SmcCloseConnection(ob_sm_conn
, 0, NULL
);
365 static void signal_handler(const ObEvent
*e
, void *data
)
369 s
= e
->data
.s
.signal
;
372 g_message("Caught SIGUSR1 signal. Restarting.");
373 ob_shutdown
= ob_restart
= TRUE
;
380 g_message("Caught signal %d. Exiting.", s
);
386 g_message("Caught signal %d. Aborting and dumping core.", s
);
391 static void print_version()
393 g_print("Openbox %s\n\n", PACKAGE_VERSION
);
394 g_print("This program comes with ABSOLUTELY NO WARRANTY.\n");
395 g_print("This is free software, and you are welcome to redistribute it\n");
396 g_print("under certain conditions. See the file COPYING for details.\n\n");
399 static void print_help()
402 g_print("Syntax: %s [options]\n\n", BINARY
);
403 g_print("Options:\n\n");
404 g_print(" -rc PATH Specify the path to the rc file to use\n");
405 g_print(" -help Display this help and exit\n");
406 g_print(" -version Display the version and exit\n");
407 g_print(" -sync Run in synchronous mode (this is slow and meant\n"
408 " for debugging X routines)\n");
409 g_print("\nPlease report bugs at %s\n", PACKAGE_BUGREPORT
);
412 static void parse_args(int argc
, char **argv
)
416 for (i
= 1; i
< argc
; ++i
) {
417 if (!strcmp(argv
[i
], "-version")) {
420 } else if (!strcmp(argv
[i
], "-help")) {
423 } else if (!strcmp(argv
[i
], "-sync")) {
425 } else if (!strcmp(argv
[i
], "-rc")) {
426 if (i
== argc
- 1) /* no args left */
427 g_printerr("-rc requires an argument\n");
429 ob_rc_path
= argv
[++i
];
431 g_printerr("Invalid option: '%s'\n\n", argv
[i
]);
438 gboolean
ob_pointer_pos(int *x
, int *y
)
444 return !!XQueryPointer(ob_display
, ob_root
, &w
, &w
, x
, y
, &i
, &i
, &u
);
448 static void sm_save_yourself(SmcConn conn
, SmPointer data
, int save_type
,
449 Bool shutdown
, int interact_style
, Bool fast
)
451 g_message("got SAVE YOURSELF from session manager");
452 SmcSaveYourselfDone(conn
, TRUE
);
455 static void sm_die(SmcConn conn
, SmPointer data
)
458 g_message("got DIE from session manager");
461 static void sm_save_complete(SmcConn conn
, SmPointer data
)
463 g_message("got SAVE COMPLETE from session manager");
466 static void sm_shutdown_cancelled(SmcConn conn
, SmPointer data
)
468 g_message("got SHUTDOWN CANCELLED from session manager");
472 static void exit_with_error(gchar
*msg
)