13 #include "extensions.h"
14 #include "../render/render.h"
17 # define SN_API_NOT_YET_FROZEN
18 # include <libsn/sn.h>
23 # include <sys/types.h>
27 /*! The event mask to grab on the root window */
28 #define ROOT_EVENTMASK (StructureNotifyMask | PropertyChangeMask | \
29 EnterWindowMask | LeaveWindowMask | \
30 SubstructureNotifyMask | SubstructureRedirectMask | \
31 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
33 guint screen_num_desktops = 0;
34 guint screen_desktop = 0;
35 Size screen_physical_size;
36 gboolean screen_showing_desktop;
37 DesktopLayout screen_desktop_layout;
38 char **screen_desktop_names = NULL;
40 static Rect *area = NULL;
41 static Strut *strut = NULL;
42 static Window support_window = None;
45 static SnMonitorContext *sn_context;
46 static int sn_busy_cnt;
47 static Timer *sn_timer = NULL;
49 static void sn_event_func(SnMonitorEvent *event, void *data);
52 static void screen_update_area();
53 static void set_root_cursor();
55 static gboolean running;
56 static int another_running(Display *d, XErrorEvent *e)
59 g_message("A window manager is already running on screen %d",
65 gboolean screen_annex()
73 old = XSetErrorHandler(another_running);
74 XSelectInput(ob_display, ob_root, ROOT_EVENTMASK);
75 XSync(ob_display, FALSE);
76 XSetErrorHandler(old);
80 g_message("Managing screen %d", ob_screen);
84 /* set the OPENBOX_PID hint */
86 PROP_SET32(ob_root, openbox_pid, cardinal, pid);
88 /* create the netwm support window */
89 support_window = XCreateSimpleWindow(ob_display, ob_root, 0,0,1,1,0,0,0);
91 /* set supporting window */
92 PROP_SET32(ob_root, net_supporting_wm_check, window, support_window);
94 /* set properties on the supporting window */
95 PROP_SETS(support_window, net_wm_name, "Openbox3");
96 PROP_SET32(support_window, net_supporting_wm_check, window,support_window);
98 /* set the _NET_SUPPORTED_ATOMS hint */
101 supported = g_new(guint32, num_support);
102 supported[i++] = prop_atoms.net_current_desktop;
103 supported[i++] = prop_atoms.net_number_of_desktops;
104 supported[i++] = prop_atoms.net_desktop_geometry;
105 supported[i++] = prop_atoms.net_desktop_viewport;
106 supported[i++] = prop_atoms.net_active_window;
107 supported[i++] = prop_atoms.net_workarea;
108 supported[i++] = prop_atoms.net_client_list;
109 supported[i++] = prop_atoms.net_client_list_stacking;
110 supported[i++] = prop_atoms.net_desktop_names;
111 supported[i++] = prop_atoms.net_close_window;
112 supported[i++] = prop_atoms.net_desktop_layout;
113 supported[i++] = prop_atoms.net_showing_desktop;
114 supported[i++] = prop_atoms.net_wm_name;
115 supported[i++] = prop_atoms.net_wm_visible_name;
116 supported[i++] = prop_atoms.net_wm_icon_name;
117 supported[i++] = prop_atoms.net_wm_visible_icon_name;
118 supported[i++] = prop_atoms.net_wm_desktop;
119 supported[i++] = prop_atoms.net_wm_strut;
120 supported[i++] = prop_atoms.net_wm_window_type;
121 supported[i++] = prop_atoms.net_wm_window_type_desktop;
122 supported[i++] = prop_atoms.net_wm_window_type_dock;
123 supported[i++] = prop_atoms.net_wm_window_type_toolbar;
124 supported[i++] = prop_atoms.net_wm_window_type_menu;
125 supported[i++] = prop_atoms.net_wm_window_type_utility;
126 supported[i++] = prop_atoms.net_wm_window_type_splash;
127 supported[i++] = prop_atoms.net_wm_window_type_dialog;
128 supported[i++] = prop_atoms.net_wm_window_type_normal;
129 supported[i++] = prop_atoms.net_wm_allowed_actions;
130 supported[i++] = prop_atoms.net_wm_action_move;
131 supported[i++] = prop_atoms.net_wm_action_resize;
132 supported[i++] = prop_atoms.net_wm_action_minimize;
133 supported[i++] = prop_atoms.net_wm_action_shade;
134 supported[i++] = prop_atoms.net_wm_action_maximize_horz;
135 supported[i++] = prop_atoms.net_wm_action_maximize_vert;
136 supported[i++] = prop_atoms.net_wm_action_fullscreen;
137 supported[i++] = prop_atoms.net_wm_action_change_desktop;
138 supported[i++] = prop_atoms.net_wm_action_close;
139 supported[i++] = prop_atoms.net_wm_state;
140 supported[i++] = prop_atoms.net_wm_state_modal;
141 supported[i++] = prop_atoms.net_wm_state_maximized_vert;
142 supported[i++] = prop_atoms.net_wm_state_maximized_horz;
143 supported[i++] = prop_atoms.net_wm_state_shaded;
144 supported[i++] = prop_atoms.net_wm_state_skip_taskbar;
145 supported[i++] = prop_atoms.net_wm_state_skip_pager;
146 supported[i++] = prop_atoms.net_wm_state_hidden;
147 supported[i++] = prop_atoms.net_wm_state_fullscreen;
148 supported[i++] = prop_atoms.net_wm_state_above;
149 supported[i++] = prop_atoms.net_wm_state_below;
150 supported[i++] = prop_atoms.net_moveresize_window;
151 supported[i++] = prop_atoms.net_wm_moveresize;
152 supported[i++] = prop_atoms.net_wm_moveresize_size_topleft;
153 supported[i++] = prop_atoms.net_wm_moveresize_size_top;
154 supported[i++] = prop_atoms.net_wm_moveresize_size_topright;
155 supported[i++] = prop_atoms.net_wm_moveresize_size_right;
156 supported[i++] = prop_atoms.net_wm_moveresize_size_bottomright;
157 supported[i++] = prop_atoms.net_wm_moveresize_size_bottom;
158 supported[i++] = prop_atoms.net_wm_moveresize_size_bottomleft;
159 supported[i++] = prop_atoms.net_wm_moveresize_size_left;
160 supported[i++] = prop_atoms.net_wm_moveresize_move;
161 supported[i++] = prop_atoms.net_wm_moveresize_size_keyboard;
162 supported[i++] = prop_atoms.net_wm_moveresize_move_keyboard;
163 g_assert(i == num_support);
165 supported[] = prop_atoms.net_wm_action_stick;
168 PROP_SETA32(ob_root, net_supported, atom, supported, num_support);
174 void screen_startup()
179 /* get the initial size */
180 screen_resize(WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen)),
181 HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen)));
184 screen_desktop_names = g_new(char*,
185 g_slist_length(config_desktops_names) + 1);
186 for (i = 0, it = config_desktops_names; it; ++i, it = it->next)
187 screen_desktop_names[i] = it->data; /* dont strdup */
188 screen_desktop_names[i] = NULL;
189 PROP_SETSS(ob_root, net_desktop_names, screen_desktop_names);
190 g_free(screen_desktop_names); /* dont free the individual strings */
191 screen_desktop_names = NULL;
193 screen_num_desktops = 0;
194 screen_set_num_desktops(config_desktops_num);
195 if (startup_desktop >= screen_num_desktops)
197 screen_desktop = startup_desktop;
198 screen_set_desktop(startup_desktop);
200 /* don't start in showing-desktop mode */
201 screen_showing_desktop = FALSE;
202 PROP_SET32(ob_root, net_showing_desktop, cardinal, screen_showing_desktop);
204 screen_update_layout();
207 sn_context = sn_monitor_context_new(ob_sn_display, ob_screen,
208 sn_event_func, NULL, NULL);
213 void screen_shutdown()
215 XSelectInput(ob_display, ob_root, NoEventMask);
217 PROP_ERASE(ob_root, openbox_pid); /* we're not running here no more! */
218 PROP_ERASE(ob_root, net_supported); /* not without us */
219 PROP_ERASE(ob_root, net_showing_desktop); /* don't keep this mode */
221 XDestroyWindow(ob_display, support_window);
223 g_strfreev(screen_desktop_names);
228 void screen_resize(int w, int h)
233 /* Set the _NET_DESKTOP_GEOMETRY hint */
236 PROP_SETA32(ob_root, net_desktop_geometry, cardinal, geometry, 2);
237 screen_physical_size.width = geometry[0];
238 screen_physical_size.height = geometry[1];
240 if (ob_state == State_Starting)
244 screen_update_struts();
246 for (it = client_list; it; it = it->next)
247 client_move_onscreen(it->data);
250 void screen_set_num_desktops(guint num)
258 old = screen_num_desktops;
259 screen_num_desktops = num;
260 PROP_SET32(ob_root, net_number_of_desktops, cardinal, num);
262 /* set the viewport hint */
263 viewport = g_new0(guint32, num * 2);
264 PROP_SETA32(ob_root, net_desktop_viewport, cardinal, viewport, num * 2);
267 /* the number of rows/columns will differ */
268 screen_update_layout();
270 /* may be some unnamed desktops that we need to fill in with names */
271 screen_update_desktop_names();
273 /* update the focus lists */
274 /* free our lists for the desktops which have disappeared */
275 for (i = num; i < old; ++i)
276 g_list_free(focus_order[i]);
277 /* realloc the array */
278 focus_order = g_renew(GList*, focus_order, num);
279 /* set the new lists to be empty */
280 for (i = old; i < num; ++i)
281 focus_order[i] = NULL;
283 /* move windows on desktops that will no longer exist! */
284 for (it = client_list; it != NULL; it = it->next) {
285 Client *c = it->data;
286 if (c->desktop >= num && c->desktop != DESKTOP_ALL)
287 client_set_desktop(c, num - 1, FALSE);
290 /* change our struts/area to match (after moving windows) */
291 screen_update_struts();
293 dispatch_ob(Event_Ob_NumDesktops, num, old);
295 /* change our desktop if we're on one that no longer exists! */
296 if (screen_desktop >= screen_num_desktops)
297 screen_set_desktop(num - 1);
300 void screen_set_desktop(guint num)
306 g_assert(num < screen_num_desktops);
308 old = screen_desktop;
309 screen_desktop = num;
310 PROP_SET32(ob_root, net_current_desktop, cardinal, num);
312 if (old == num) return;
314 g_message("Moving to desktop %d", num+1);
316 /* show windows before hiding the rest to lessen the enter/leave events */
318 /* show windows from top to bottom */
319 for (it = stacking_list; it != NULL; it = it->next) {
320 if (WINDOW_IS_CLIENT(it->data)) {
321 Client *c = it->data;
322 if (!c->frame->visible && client_should_show(c))
323 frame_show(c->frame);
327 /* hide windows from bottom to top */
328 for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
329 if (WINDOW_IS_CLIENT(it->data)) {
330 Client *c = it->data;
331 if (c->frame->visible && !client_should_show(c))
332 frame_hide(c->frame);
336 /* focus the last focused window on the desktop, and ignore enter events
337 from the switch so it doesnt mess with the focus */
338 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
339 focus_fallback(Fallback_Desktop);
341 dispatch_ob(Event_Ob_Desktop, num, old);
344 void screen_update_layout()
346 guint32 *data = NULL;
350 screen_desktop_layout.orientation = prop_atoms.net_wm_orientation_horz;
351 screen_desktop_layout.start_corner = prop_atoms.net_wm_topleft;
352 screen_desktop_layout.rows = 1;
353 screen_desktop_layout.columns = screen_num_desktops;
355 if (PROP_GETA32(ob_root, net_desktop_layout, cardinal, &data, &num)) {
356 if (num == 3 || num == 4) {
357 if (data[0] == prop_atoms.net_wm_orientation_vert)
358 screen_desktop_layout.orientation = data[0];
360 screen_desktop_layout.start_corner =
361 prop_atoms.net_wm_topright;
363 if (data[3] == prop_atoms.net_wm_topright)
364 screen_desktop_layout.start_corner = data[3];
365 else if (data[3] == prop_atoms.net_wm_bottomright)
366 screen_desktop_layout.start_corner = data[3];
367 else if (data[3] == prop_atoms.net_wm_bottomleft)
368 screen_desktop_layout.start_corner = data[3];
371 /* fill in a zero rows/columns */
372 if (!(data[1] == 0 && data[2] == 0)) { /* both 0's is bad data.. */
374 data[1] = (screen_num_desktops +
375 screen_num_desktops % data[2]) / data[2];
376 } else if (data[2] == 0) {
377 data[2] = (screen_num_desktops +
378 screen_num_desktops % data[1]) / data[1];
380 screen_desktop_layout.columns = data[1];
381 screen_desktop_layout.rows = data[2];
384 /* bounds checking */
385 if (screen_desktop_layout.orientation ==
386 prop_atoms.net_wm_orientation_horz) {
387 if (screen_desktop_layout.rows > screen_num_desktops)
388 screen_desktop_layout.rows = screen_num_desktops;
389 if (screen_desktop_layout.columns >
390 ((screen_num_desktops + screen_num_desktops %
391 screen_desktop_layout.rows) /
392 screen_desktop_layout.rows))
393 screen_desktop_layout.columns =
394 (screen_num_desktops + screen_num_desktops %
395 screen_desktop_layout.rows) /
396 screen_desktop_layout.rows;
398 if (screen_desktop_layout.columns > screen_num_desktops)
399 screen_desktop_layout.columns = screen_num_desktops;
400 if (screen_desktop_layout.rows >
401 ((screen_num_desktops + screen_num_desktops %
402 screen_desktop_layout.columns) /
403 screen_desktop_layout.columns))
404 screen_desktop_layout.rows =
405 (screen_num_desktops + screen_num_desktops %
406 screen_desktop_layout.columns) /
407 screen_desktop_layout.columns;
414 void screen_update_desktop_names()
418 /* empty the array */
419 g_strfreev(screen_desktop_names);
420 screen_desktop_names = NULL;
422 if (PROP_GETSS(ob_root, net_desktop_names, utf8, &screen_desktop_names))
423 for (i = 0; screen_desktop_names[i] && i <= screen_num_desktops; ++i);
426 if (i <= screen_num_desktops) {
427 screen_desktop_names = g_renew(char*, screen_desktop_names,
428 screen_num_desktops + 1);
429 screen_desktop_names[screen_num_desktops] = NULL;
430 for (; i < screen_num_desktops; ++i)
431 screen_desktop_names[i] = g_strdup("Unnamed Desktop");
435 void screen_show_desktop(gboolean show)
439 if (show == screen_showing_desktop) return; /* no change */
441 screen_showing_desktop = show;
445 for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
446 if (WINDOW_IS_CLIENT(it->data)) {
447 Client *client = it->data;
448 if (client->frame->visible && !client_should_show(client))
449 frame_hide(client->frame);
454 for (it = stacking_list; it != NULL; it = it->next) {
455 if (WINDOW_IS_CLIENT(it->data)) {
456 Client *client = it->data;
457 if (!client->frame->visible && client_should_show(client))
458 frame_show(client->frame);
465 for (it = focus_order[screen_desktop]; it; it = it->next)
466 if (((Client*)it->data)->type == Type_Desktop &&
467 client_focus(it->data))
470 focus_fallback(Fallback_NoFocus);
473 show = !!show; /* make it boolean */
474 PROP_SET32(ob_root, net_showing_desktop, cardinal, show);
476 dispatch_ob(Event_Ob_ShowDesktop, show, 0);
479 void screen_install_colormap(Client *client, gboolean install)
481 XWindowAttributes wa;
483 if (client == NULL) {
485 XInstallColormap(ob_display, render_colormap);
487 XUninstallColormap(ob_display, render_colormap);
489 if (XGetWindowAttributes(ob_display, client->window, &wa) &&
490 wa.colormap != None) {
491 xerror_set_ignore(TRUE);
493 XInstallColormap(ob_display, wa.colormap);
495 XUninstallColormap(ob_display, wa.colormap);
496 xerror_set_ignore(FALSE);
501 void screen_update_struts()
507 strut = g_new0(Strut, screen_num_desktops + 1);
509 for (it = client_list; it != NULL; it = it->next) {
510 Client *c = it->data;
511 if (c->iconic) continue; /* these dont count in the strut */
513 if (c->desktop == 0xffffffff) {
514 for (i = 0; i < screen_num_desktops; ++i)
515 STRUT_ADD(strut[i], c->strut);
517 g_assert(c->desktop < screen_num_desktops);
518 STRUT_ADD(strut[c->desktop], c->strut);
520 /* apply to the 'all desktops' strut */
521 STRUT_ADD(strut[screen_num_desktops], c->strut);
524 for (i = 0; i < screen_num_desktops; ++i)
525 STRUT_ADD(strut[i], dock_strut);
527 screen_update_area();
530 static void screen_update_area()
536 area = g_new0(Rect, screen_num_desktops + 1);
538 dims = g_new(guint32, 4 * screen_num_desktops);
539 for (i = 0; i < screen_num_desktops + 1; ++i) {
540 Rect old_area = area[i];
543 // reset to the full areas
544 if (isXineramaActive())
545 xineramaUsableArea = getXineramaAreas();
549 RECT_SET(area[i], strut[i].left, strut[i].top,
550 screen_physical_size.width - (strut[i].left +
552 screen_physical_size.height - (strut[i].top +
557 if (isXineramaActive()) {
558 // keep each of the ximerama-defined areas inside the strut
559 RectList::iterator xit, xend = xineramaUsableArea.end();
560 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
561 if (xit->x() < usableArea.x()) {
562 xit->setX(usableArea.x());
563 xit->setWidth(xit->width() - usableArea.x());
565 if (xit->y() < usableArea.y()) {
566 xit->setY(usableArea.y());
567 xit->setHeight(xit->height() - usableArea.y());
569 if (xit->x() + xit->width() > usableArea.width())
570 xit->setWidth(usableArea.width() - xit->x());
571 if (xit->y() + xit->height() > usableArea.height())
572 xit->setHeight(usableArea.height() - xit->y());
577 if (!RECT_EQUAL(old_area, area[i])) {
578 /* the area has changed, adjust all the maximized windows */
580 for (it = client_list; it; it = it->next) {
581 Client *c = it->data;
582 if (i < screen_num_desktops) {
584 client_remaximize(c);
586 /* the 'all desktops' size */
587 if (c->desktop == DESKTOP_ALL)
588 client_remaximize(c);
593 /* don't set these for the 'all desktops' area */
594 if (i < screen_num_desktops) {
595 dims[(i * 4) + 0] = area[i].x;
596 dims[(i * 4) + 1] = area[i].y;
597 dims[(i * 4) + 2] = area[i].width;
598 dims[(i * 4) + 3] = area[i].height;
601 PROP_SETA32(ob_root, net_workarea, cardinal,
602 dims, 4 * screen_num_desktops);
606 Rect *screen_area(guint desktop)
608 if (desktop >= screen_num_desktops) {
609 if (desktop == DESKTOP_ALL)
610 return &area[screen_num_desktops];
613 return &area[desktop];
616 Strut *screen_strut(guint desktop)
618 if (desktop >= screen_num_desktops) {
619 if (desktop == DESKTOP_ALL)
620 return &strut[screen_num_desktops];
623 return &strut[desktop];
626 static void set_root_cursor()
630 XDefineCursor(ob_display, ob_root, ob_cursors.busy);
633 XDefineCursor(ob_display, ob_root, ob_cursors.ptr);
637 static void sn_timeout(void *data)
639 timer_stop(sn_timer);
646 static void sn_event_func(SnMonitorEvent *ev, void *data)
648 SnStartupSequence *seq;
649 const char *seq_id, *bin_name;
650 int cnt = sn_busy_cnt;
652 if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
655 seq_id = sn_startup_sequence_get_id(seq);
656 bin_name = sn_startup_sequence_get_binary_name(seq);
658 if (!(seq_id && bin_name))
661 switch (sn_monitor_event_get_type(ev)) {
662 case SN_MONITOR_EVENT_INITIATED:
665 timer_stop(sn_timer);
666 /* 30 second timeout for apps to start */
667 sn_timer = timer_start(30 * 1000000, sn_timeout, NULL);
669 case SN_MONITOR_EVENT_CHANGED:
671 case SN_MONITOR_EVENT_COMPLETED:
672 if (sn_busy_cnt) --sn_busy_cnt;
674 timer_stop(sn_timer);
678 case SN_MONITOR_EVENT_CANCELED:
679 if (sn_busy_cnt) --sn_busy_cnt;
681 timer_stop(sn_timer);
686 if (sn_busy_cnt != cnt)