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_num_monitors = 0;
35 guint screen_desktop = 0;
36 Size screen_physical_size;
37 gboolean screen_showing_desktop;
38 DesktopLayout screen_desktop_layout;
39 char **screen_desktop_names = NULL;
41 static Rect **area = NULL; /* array of desktop holding array of
43 static Rect *monitor_area = NULL;
44 static Window support_window = None;
47 static SnMonitorContext *sn_context;
48 static int sn_busy_cnt;
49 static Timer *sn_timer = NULL;
51 static void sn_event_func(SnMonitorEvent *event, void *data);
54 static void set_root_cursor();
56 static gboolean running;
57 static int another_running(Display *d, XErrorEvent *e)
60 g_message("A window manager is already running on screen %d",
66 gboolean screen_annex()
74 old = XSetErrorHandler(another_running);
75 XSelectInput(ob_display, ob_root, ROOT_EVENTMASK);
76 XSync(ob_display, FALSE);
77 XSetErrorHandler(old);
81 g_message("Managing screen %d", ob_screen);
85 /* set the OPENBOX_PID hint */
87 PROP_SET32(ob_root, openbox_pid, cardinal, pid);
89 /* create the netwm support window */
90 support_window = XCreateSimpleWindow(ob_display, ob_root, 0,0,1,1,0,0,0);
92 /* set supporting window */
93 PROP_SET32(ob_root, net_supporting_wm_check, window, support_window);
95 /* set properties on the supporting window */
96 PROP_SETS(support_window, net_wm_name, "Openbox");
97 PROP_SET32(support_window, net_supporting_wm_check, window,support_window);
99 /* set the _NET_SUPPORTED_ATOMS hint */
102 supported = g_new(guint32, num_support);
103 supported[i++] = prop_atoms.net_current_desktop;
104 supported[i++] = prop_atoms.net_number_of_desktops;
105 supported[i++] = prop_atoms.net_desktop_geometry;
106 supported[i++] = prop_atoms.net_desktop_viewport;
107 supported[i++] = prop_atoms.net_active_window;
108 supported[i++] = prop_atoms.net_workarea;
109 supported[i++] = prop_atoms.net_client_list;
110 supported[i++] = prop_atoms.net_client_list_stacking;
111 supported[i++] = prop_atoms.net_desktop_names;
112 supported[i++] = prop_atoms.net_close_window;
113 supported[i++] = prop_atoms.net_desktop_layout;
114 supported[i++] = prop_atoms.net_showing_desktop;
115 supported[i++] = prop_atoms.net_wm_name;
116 supported[i++] = prop_atoms.net_wm_visible_name;
117 supported[i++] = prop_atoms.net_wm_icon_name;
118 supported[i++] = prop_atoms.net_wm_visible_icon_name;
119 supported[i++] = prop_atoms.net_wm_desktop;
120 supported[i++] = prop_atoms.net_wm_strut;
121 supported[i++] = prop_atoms.net_wm_window_type;
122 supported[i++] = prop_atoms.net_wm_window_type_desktop;
123 supported[i++] = prop_atoms.net_wm_window_type_dock;
124 supported[i++] = prop_atoms.net_wm_window_type_toolbar;
125 supported[i++] = prop_atoms.net_wm_window_type_menu;
126 supported[i++] = prop_atoms.net_wm_window_type_utility;
127 supported[i++] = prop_atoms.net_wm_window_type_splash;
128 supported[i++] = prop_atoms.net_wm_window_type_dialog;
129 supported[i++] = prop_atoms.net_wm_window_type_normal;
130 supported[i++] = prop_atoms.net_wm_allowed_actions;
131 supported[i++] = prop_atoms.net_wm_action_move;
132 supported[i++] = prop_atoms.net_wm_action_resize;
133 supported[i++] = prop_atoms.net_wm_action_minimize;
134 supported[i++] = prop_atoms.net_wm_action_shade;
135 supported[i++] = prop_atoms.net_wm_action_maximize_horz;
136 supported[i++] = prop_atoms.net_wm_action_maximize_vert;
137 supported[i++] = prop_atoms.net_wm_action_fullscreen;
138 supported[i++] = prop_atoms.net_wm_action_change_desktop;
139 supported[i++] = prop_atoms.net_wm_action_close;
140 supported[i++] = prop_atoms.net_wm_state;
141 supported[i++] = prop_atoms.net_wm_state_modal;
142 supported[i++] = prop_atoms.net_wm_state_maximized_vert;
143 supported[i++] = prop_atoms.net_wm_state_maximized_horz;
144 supported[i++] = prop_atoms.net_wm_state_shaded;
145 supported[i++] = prop_atoms.net_wm_state_skip_taskbar;
146 supported[i++] = prop_atoms.net_wm_state_skip_pager;
147 supported[i++] = prop_atoms.net_wm_state_hidden;
148 supported[i++] = prop_atoms.net_wm_state_fullscreen;
149 supported[i++] = prop_atoms.net_wm_state_above;
150 supported[i++] = prop_atoms.net_wm_state_below;
151 supported[i++] = prop_atoms.net_moveresize_window;
152 supported[i++] = prop_atoms.net_wm_moveresize;
153 supported[i++] = prop_atoms.net_wm_moveresize_size_topleft;
154 supported[i++] = prop_atoms.net_wm_moveresize_size_top;
155 supported[i++] = prop_atoms.net_wm_moveresize_size_topright;
156 supported[i++] = prop_atoms.net_wm_moveresize_size_right;
157 supported[i++] = prop_atoms.net_wm_moveresize_size_bottomright;
158 supported[i++] = prop_atoms.net_wm_moveresize_size_bottom;
159 supported[i++] = prop_atoms.net_wm_moveresize_size_bottomleft;
160 supported[i++] = prop_atoms.net_wm_moveresize_size_left;
161 supported[i++] = prop_atoms.net_wm_moveresize_move;
162 supported[i++] = prop_atoms.net_wm_moveresize_size_keyboard;
163 supported[i++] = prop_atoms.net_wm_moveresize_move_keyboard;
164 g_assert(i == num_support);
166 supported[] = prop_atoms.net_wm_action_stick;
169 PROP_SETA32(ob_root, net_supported, atom, supported, num_support);
175 void screen_startup()
180 /* get the initial size */
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()
217 XSelectInput(ob_display, ob_root, NoEventMask);
219 PROP_ERASE(ob_root, openbox_pid); /* we're not running here no more! */
220 PROP_ERASE(ob_root, net_supported); /* not without us */
221 PROP_ERASE(ob_root, net_showing_desktop); /* don't keep this mode */
223 XDestroyWindow(ob_display, support_window);
225 g_strfreev(screen_desktop_names);
226 for (r = area; *r; ++r)
233 static int oldw = 0, oldh = 0;
238 w = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
239 h = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
241 if (w == oldw && h == oldh) return;
245 /* Set the _NET_DESKTOP_GEOMETRY hint */
246 screen_physical_size.width = geometry[0] = w;
247 screen_physical_size.height = geometry[1] = h;
248 PROP_SETA32(ob_root, net_desktop_geometry, cardinal, geometry, 2);
250 if (ob_state == State_Starting)
254 screen_update_areas();
256 for (it = client_list; it; it = it->next)
257 client_move_onscreen(it->data);
260 void screen_set_num_desktops(guint num)
268 old = screen_num_desktops;
269 screen_num_desktops = num;
270 PROP_SET32(ob_root, net_number_of_desktops, cardinal, num);
272 /* set the viewport hint */
273 viewport = g_new0(guint32, num * 2);
274 PROP_SETA32(ob_root, net_desktop_viewport, cardinal, viewport, num * 2);
277 /* the number of rows/columns will differ */
278 screen_update_layout();
280 /* may be some unnamed desktops that we need to fill in with names */
281 screen_update_desktop_names();
283 /* update the focus lists */
284 /* free our lists for the desktops which have disappeared */
285 for (i = num; i < old; ++i)
286 g_list_free(focus_order[i]);
287 /* realloc the array */
288 focus_order = g_renew(GList*, focus_order, num);
289 /* set the new lists to be empty */
290 for (i = old; i < num; ++i)
291 focus_order[i] = NULL;
293 /* move windows on desktops that will no longer exist! */
294 for (it = client_list; it != NULL; it = it->next) {
295 Client *c = it->data;
296 if (c->desktop >= num && c->desktop != DESKTOP_ALL)
297 client_set_desktop(c, num - 1, FALSE);
300 /* change our struts/area to match (after moving windows) */
301 screen_update_areas();
303 dispatch_ob(Event_Ob_NumDesktops, num, old);
305 /* change our desktop if we're on one that no longer exists! */
306 if (screen_desktop >= screen_num_desktops)
307 screen_set_desktop(num - 1);
310 void screen_set_desktop(guint num)
316 g_assert(num < screen_num_desktops);
318 old = screen_desktop;
319 screen_desktop = num;
320 PROP_SET32(ob_root, net_current_desktop, cardinal, num);
322 if (old == num) return;
324 g_message("Moving to desktop %d", num+1);
326 /* show windows before hiding the rest to lessen the enter/leave events */
328 /* show windows from top to bottom */
329 for (it = stacking_list; it != NULL; it = it->next) {
330 if (WINDOW_IS_CLIENT(it->data)) {
331 Client *c = it->data;
332 if (!c->frame->visible && client_should_show(c))
333 frame_show(c->frame);
337 /* hide windows from bottom to top */
338 for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
339 if (WINDOW_IS_CLIENT(it->data)) {
340 Client *c = it->data;
341 if (c->frame->visible && !client_should_show(c))
342 frame_hide(c->frame);
346 /* focus the last focused window on the desktop, and ignore enter events
347 from the switch so it doesnt mess with the focus */
348 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
349 g_message("switch fallback");
350 focus_fallback(Fallback_Desktop);
351 g_message("/switch fallback");
353 dispatch_ob(Event_Ob_Desktop, num, old);
356 void screen_update_layout()
358 guint32 *data = NULL;
362 screen_desktop_layout.orientation = prop_atoms.net_wm_orientation_horz;
363 screen_desktop_layout.start_corner = prop_atoms.net_wm_topleft;
364 screen_desktop_layout.rows = 1;
365 screen_desktop_layout.columns = screen_num_desktops;
367 if (PROP_GETA32(ob_root, net_desktop_layout, cardinal, &data, &num)) {
368 if (num == 3 || num == 4) {
369 if (data[0] == prop_atoms.net_wm_orientation_vert)
370 screen_desktop_layout.orientation = data[0];
372 screen_desktop_layout.start_corner =
373 prop_atoms.net_wm_topright;
375 if (data[3] == prop_atoms.net_wm_topright)
376 screen_desktop_layout.start_corner = data[3];
377 else if (data[3] == prop_atoms.net_wm_bottomright)
378 screen_desktop_layout.start_corner = data[3];
379 else if (data[3] == prop_atoms.net_wm_bottomleft)
380 screen_desktop_layout.start_corner = data[3];
383 /* fill in a zero rows/columns */
384 if (!(data[1] == 0 && data[2] == 0)) { /* both 0's is bad data.. */
386 data[1] = (screen_num_desktops +
387 screen_num_desktops % data[2]) / data[2];
388 } else if (data[2] == 0) {
389 data[2] = (screen_num_desktops +
390 screen_num_desktops % data[1]) / data[1];
392 screen_desktop_layout.columns = data[1];
393 screen_desktop_layout.rows = data[2];
396 /* bounds checking */
397 if (screen_desktop_layout.orientation ==
398 prop_atoms.net_wm_orientation_horz) {
399 if (screen_desktop_layout.rows > screen_num_desktops)
400 screen_desktop_layout.rows = screen_num_desktops;
401 if (screen_desktop_layout.columns >
402 ((screen_num_desktops + screen_num_desktops %
403 screen_desktop_layout.rows) /
404 screen_desktop_layout.rows))
405 screen_desktop_layout.columns =
406 (screen_num_desktops + screen_num_desktops %
407 screen_desktop_layout.rows) /
408 screen_desktop_layout.rows;
410 if (screen_desktop_layout.columns > screen_num_desktops)
411 screen_desktop_layout.columns = screen_num_desktops;
412 if (screen_desktop_layout.rows >
413 ((screen_num_desktops + screen_num_desktops %
414 screen_desktop_layout.columns) /
415 screen_desktop_layout.columns))
416 screen_desktop_layout.rows =
417 (screen_num_desktops + screen_num_desktops %
418 screen_desktop_layout.columns) /
419 screen_desktop_layout.columns;
426 void screen_update_desktop_names()
430 /* empty the array */
431 g_strfreev(screen_desktop_names);
432 screen_desktop_names = NULL;
434 if (PROP_GETSS(ob_root, net_desktop_names, utf8, &screen_desktop_names))
435 for (i = 0; screen_desktop_names[i] && i <= screen_num_desktops; ++i);
438 if (i <= screen_num_desktops) {
439 screen_desktop_names = g_renew(char*, screen_desktop_names,
440 screen_num_desktops + 1);
441 screen_desktop_names[screen_num_desktops] = NULL;
442 for (; i < screen_num_desktops; ++i)
443 screen_desktop_names[i] = g_strdup("Unnamed Desktop");
447 void screen_show_desktop(gboolean show)
451 if (show == screen_showing_desktop) return; /* no change */
453 screen_showing_desktop = show;
457 for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
458 if (WINDOW_IS_CLIENT(it->data)) {
459 Client *client = it->data;
460 if (client->frame->visible && !client_should_show(client))
461 frame_hide(client->frame);
466 for (it = stacking_list; it != NULL; it = it->next) {
467 if (WINDOW_IS_CLIENT(it->data)) {
468 Client *client = it->data;
469 if (!client->frame->visible && client_should_show(client))
470 frame_show(client->frame);
477 for (it = focus_order[screen_desktop]; it; it = it->next)
478 if (((Client*)it->data)->type == Type_Desktop &&
479 client_focus(it->data))
482 focus_fallback(Fallback_NoFocus);
485 show = !!show; /* make it boolean */
486 PROP_SET32(ob_root, net_showing_desktop, cardinal, show);
488 dispatch_ob(Event_Ob_ShowDesktop, show, 0);
491 void screen_install_colormap(Client *client, gboolean install)
493 XWindowAttributes wa;
495 if (client == NULL) {
497 XInstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
499 XUninstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
501 if (XGetWindowAttributes(ob_display, client->window, &wa) &&
502 wa.colormap != None) {
503 xerror_set_ignore(TRUE);
505 XInstallColormap(RrDisplay(ob_rr_inst), wa.colormap);
507 XUninstallColormap(RrDisplay(ob_rr_inst), wa.colormap);
508 xerror_set_ignore(FALSE);
513 void screen_update_areas()
517 Rect **old_area = area;
521 g_free(monitor_area);
522 extensions_xinerama_screens(&monitor_area, &screen_num_monitors);
525 for (i = 0; area[i]; ++i)
530 area = g_new(Rect*, screen_num_desktops + 2);
531 for (i = 0; i < screen_num_desktops + 1; ++i)
532 area[i] = g_new(Rect, screen_num_monitors + 1);
535 dims = g_new(guint32, 4 * screen_num_desktops);
538 for (i = 0; i < screen_num_desktops + 1; ++i) {
542 /* calc the xinerama areas */
543 for (x = 0; x < screen_num_monitors; ++x) {
544 area[i][x] = monitor_area[x];
546 l = monitor_area[x].x;
547 t = monitor_area[x].y;
548 r = monitor_area[x].x + monitor_area[x].width - 1;
549 b = monitor_area[x].y + monitor_area[x].height - 1;
551 l = MIN(l, monitor_area[x].x);
552 t = MIN(t, monitor_area[x].y);
553 r = MAX(r, monitor_area[x].x + monitor_area[x].width - 1);
554 b = MAX(b, monitor_area[x].y + monitor_area[x].height - 1);
557 RECT_SET(area[i][x], l, t, r - l + 1, b - t + 1);
560 STRUT_SET(s, 0, 0, 0, 0);
561 for (it = client_list; it; it = it->next)
562 STRUT_ADD(s, ((Client*)it->data)->strut);
563 STRUT_ADD(s, dock_strut);
568 /* find the left-most xin heads, i do this in 2 loops :| */
570 for (x = 1; x < screen_num_monitors; ++x)
571 o = MIN(o, area[i][x].x);
573 for (x = 0; x < screen_num_monitors; ++x) {
574 int edge = o + s.left - area[i][x].x;
576 area[i][x].x += edge;
577 area[i][x].width -= edge;
581 area[i][screen_num_monitors].x += s.left;
582 area[i][screen_num_monitors].width -= s.left;
587 /* find the left-most xin heads, i do this in 2 loops :| */
589 for (x = 1; x < screen_num_monitors; ++x)
590 o = MIN(o, area[i][x].y);
592 for (x = 0; x < screen_num_monitors; ++x) {
593 int edge = o + s.top - area[i][x].y;
595 area[i][x].y += edge;
596 area[i][x].height -= edge;
600 area[i][screen_num_monitors].y += s.top;
601 area[i][screen_num_monitors].height -= s.top;
606 /* find the bottom-most xin heads, i do this in 2 loops :| */
607 o = area[i][0].x + area[i][0].width - 1;
608 for (x = 1; x < screen_num_monitors; ++x)
609 o = MAX(o, area[i][x].x + area[i][x].width - 1);
611 for (x = 0; x < screen_num_monitors; ++x) {
612 int edge = (area[i][x].x + area[i][x].width - 1) -
615 area[i][x].width -= edge;
618 area[i][screen_num_monitors].width -= s.right;
623 /* find the bottom-most xin heads, i do this in 2 loops :| */
624 o = area[i][0].y + area[i][0].height - 1;
625 for (x = 1; x < screen_num_monitors; ++x)
626 o = MAX(o, area[i][x].y + area[i][x].height - 1);
628 for (x = 0; x < screen_num_monitors; ++x) {
629 int edge = (area[i][x].y + area[i][x].height - 1) -
632 area[i][x].height -= edge;
635 area[i][screen_num_monitors].height -= s.bottom;
638 /* XXX when dealing with partial struts, if its in a single
639 xinerama area, then only subtract it from that area's space
640 for (x = 0; x < screen_num_monitors; ++x) {
644 do something smart with it for the 'all xinerama areas' one...
646 for (it = client_list; it; it = it->next) {
648 XXX if gunna test this shit, then gotta worry about when
649 the client moves between xinerama heads..
651 if (RECT_CONTAINS_RECT(((Client*)it->data)->frame->area,
659 /* XXX optimize when this is run? */
661 /* the area has changed, adjust all the maximized
663 for (it = client_list; it; it = it->next) {
664 Client *c = it->data;
665 if (i < screen_num_desktops) {
667 client_reconfigure(c);
668 } else if (c->desktop == DESKTOP_ALL)
669 client_reconfigure(c);
671 if (i < screen_num_desktops) {
672 /* don't set these for the 'all desktops' area */
673 dims[(i * 4) + 0] = area[i][screen_num_monitors].x;
674 dims[(i * 4) + 1] = area[i][screen_num_monitors].y;
675 dims[(i * 4) + 2] = area[i][screen_num_monitors].width;
676 dims[(i * 4) + 3] = area[i][screen_num_monitors].height;
679 PROP_SETA32(ob_root, net_workarea, cardinal,
680 dims, 4 * screen_num_desktops);
685 Rect *screen_area(guint desktop)
687 return screen_area_monitor(desktop, screen_num_monitors);
690 Rect *screen_area_monitor(guint desktop, guint head)
692 if (head > screen_num_monitors)
694 if (desktop >= screen_num_desktops) {
695 if (desktop == DESKTOP_ALL)
696 return &area[screen_num_desktops][head];
699 return &area[desktop][head];
702 Rect *screen_physical_area()
704 return screen_physical_area_monitor(screen_num_monitors);
707 Rect *screen_physical_area_monitor(guint head)
709 if (head > screen_num_monitors)
711 return &monitor_area[head];
714 static void set_root_cursor()
718 XDefineCursor(ob_display, ob_root, ob_cursors.busy);
721 XDefineCursor(ob_display, ob_root, ob_cursors.ptr);
725 static void sn_timeout(void *data)
727 timer_stop(sn_timer);
734 static void sn_event_func(SnMonitorEvent *ev, void *data)
736 SnStartupSequence *seq;
737 const char *seq_id, *bin_name;
738 int cnt = sn_busy_cnt;
740 if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
743 seq_id = sn_startup_sequence_get_id(seq);
744 bin_name = sn_startup_sequence_get_binary_name(seq);
746 if (!(seq_id && bin_name))
749 switch (sn_monitor_event_get_type(ev)) {
750 case SN_MONITOR_EVENT_INITIATED:
753 timer_stop(sn_timer);
754 /* 30 second timeout for apps to start */
755 sn_timer = timer_start(30 * 1000000, sn_timeout, NULL);
757 case SN_MONITOR_EVENT_CHANGED:
759 case SN_MONITOR_EVENT_COMPLETED:
760 if (sn_busy_cnt) --sn_busy_cnt;
762 timer_stop(sn_timer);
766 case SN_MONITOR_EVENT_CANCELED:
767 if (sn_busy_cnt) --sn_busy_cnt;
769 timer_stop(sn_timer);
774 if (sn_busy_cnt != cnt)