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;
34 guint screen_num_monitors;
36 Size screen_physical_size;
37 gboolean screen_showing_desktop;
38 DesktopLayout screen_desktop_layout;
39 char **screen_desktop_names;
40 Window screen_support_win;
42 static Rect **area; /* array of desktop holding array of xinerama areas */
43 static Rect *monitor_area;
46 static SnMonitorContext *sn_context;
47 static int sn_busy_cnt;
48 static ObTimer *sn_timer;
50 static void sn_event_func(SnMonitorEvent *event, void *data);
53 static void set_root_cursor();
55 gboolean screen_annex()
57 XSetWindowAttributes attrib;
62 xerror_set_ignore(TRUE);
63 xerror_occured = FALSE;
64 XSelectInput(ob_display, ob_root, ROOT_EVENTMASK);
65 xerror_set_ignore(FALSE);
67 g_message("A window manager is already running on screen %d",
73 g_message("Managing screen %d", ob_screen);
77 /* set the OPENBOX_PID hint */
79 PROP_SET32(ob_root, openbox_pid, cardinal, pid);
81 /* create the netwm support window */
82 attrib.override_redirect = TRUE;
83 screen_support_win = XCreateWindow(ob_display, ob_root,
85 CopyFromParent, InputOutput,
87 CWOverrideRedirect, &attrib);
88 XMapRaised(ob_display, screen_support_win);
90 /* set supporting window */
91 PROP_SET32(ob_root, net_supporting_wm_check, window, screen_support_win);
93 /* set properties on the supporting window */
94 PROP_SETS(screen_support_win, net_wm_name, "Openbox");
95 PROP_SET32(screen_support_win, net_supporting_wm_check,
96 window, screen_support_win);
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 */
183 screen_desktop_names = g_new(char*,
184 g_slist_length(config_desktops_names) + 1);
185 for (i = 0, it = config_desktops_names; it; ++i, it = it->next)
186 screen_desktop_names[i] = it->data; /* dont strdup */
187 screen_desktop_names[i] = NULL;
188 PROP_SETSS(ob_root, net_desktop_names, screen_desktop_names);
189 g_free(screen_desktop_names); /* dont free the individual strings */
190 screen_desktop_names = NULL;
192 screen_num_desktops = 0;
193 screen_set_num_desktops(config_desktops_num);
194 if (startup_desktop >= screen_num_desktops)
196 screen_desktop = startup_desktop;
197 screen_set_desktop(startup_desktop);
199 /* don't start in showing-desktop mode */
200 screen_showing_desktop = FALSE;
201 PROP_SET32(ob_root, net_showing_desktop, cardinal, screen_showing_desktop);
203 screen_update_layout();
206 sn_context = sn_monitor_context_new(ob_sn_display, ob_screen,
207 sn_event_func, NULL, NULL);
212 void screen_shutdown()
216 XSelectInput(ob_display, ob_root, NoEventMask);
218 PROP_ERASE(ob_root, openbox_pid); /* we're not running here no more! */
219 PROP_ERASE(ob_root, net_supported); /* not without us */
220 PROP_ERASE(ob_root, net_showing_desktop); /* don't keep this mode */
222 XDestroyWindow(ob_display, screen_support_win);
224 g_strfreev(screen_desktop_names);
225 for (r = area; *r; ++r)
232 static int oldw = 0, oldh = 0;
237 w = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
238 h = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
240 if (w == oldw && h == oldh) return;
244 /* Set the _NET_DESKTOP_GEOMETRY hint */
245 screen_physical_size.width = geometry[0] = w;
246 screen_physical_size.height = geometry[1] = h;
247 PROP_SETA32(ob_root, net_desktop_geometry, cardinal, geometry, 2);
249 if (ob_state == OB_STATE_STARTING)
253 screen_update_areas();
255 for (it = client_list; it; it = it->next)
256 client_move_onscreen(it->data);
259 void screen_set_num_desktops(guint num)
267 old = screen_num_desktops;
268 screen_num_desktops = num;
269 PROP_SET32(ob_root, net_number_of_desktops, cardinal, num);
271 /* set the viewport hint */
272 viewport = g_new0(guint32, num * 2);
273 PROP_SETA32(ob_root, net_desktop_viewport, cardinal, viewport, num * 2);
276 /* the number of rows/columns will differ */
277 screen_update_layout();
279 /* may be some unnamed desktops that we need to fill in with names */
280 screen_update_desktop_names();
282 /* update the focus lists */
283 /* free our lists for the desktops which have disappeared */
284 for (i = num; i < old; ++i)
285 g_list_free(focus_order[i]);
286 /* realloc the array */
287 focus_order = g_renew(GList*, focus_order, num);
288 /* set the new lists to be empty */
289 for (i = old; i < num; ++i)
290 focus_order[i] = NULL;
292 /* move windows on desktops that will no longer exist! */
293 for (it = client_list; it != NULL; it = it->next) {
294 ObClient *c = it->data;
295 if (c->desktop >= num && c->desktop != DESKTOP_ALL)
296 client_set_desktop(c, num - 1, FALSE);
299 /* change our struts/area to match (after moving windows) */
300 screen_update_areas();
302 dispatch_ob(Event_Ob_NumDesktops, num, old);
304 /* change our desktop if we're on one that no longer exists! */
305 if (screen_desktop >= screen_num_desktops)
306 screen_set_desktop(num - 1);
309 void screen_set_desktop(guint num)
315 g_assert(num < screen_num_desktops);
317 old = screen_desktop;
318 screen_desktop = num;
319 PROP_SET32(ob_root, net_current_desktop, cardinal, num);
321 if (old == num) return;
323 g_message("Moving to desktop %d", num+1);
325 /* show windows before hiding the rest to lessen the enter/leave events */
327 /* show windows from top to bottom */
328 for (it = stacking_list; it != NULL; it = it->next) {
329 if (WINDOW_IS_CLIENT(it->data)) {
330 ObClient *c = it->data;
331 if (!c->frame->visible && client_should_show(c))
332 frame_show(c->frame);
336 /* hide windows from bottom to top */
337 for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
338 if (WINDOW_IS_CLIENT(it->data)) {
339 ObClient *c = it->data;
340 if (c->frame->visible && !client_should_show(c))
341 frame_hide(c->frame);
345 /* focus the last focused window on the desktop, and ignore enter events
346 from the switch so it doesnt mess with the focus */
347 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
349 g_message("switch fallback");
351 focus_fallback(OB_FOCUS_FALLBACK_DESKTOP);
353 g_message("/switch fallback");
356 dispatch_ob(Event_Ob_Desktop, num, old);
359 void screen_update_layout()
361 ObOrientation orient;
367 gboolean valid = FALSE;
369 if (PROP_GETA32(ob_root, net_desktop_layout, cardinal, &data, &num)) {
370 if (num == 3 || num == 4) {
372 if (data[0] == prop_atoms.net_wm_orientation_vert)
373 orient = OB_ORIENTATION_VERT;
374 else if (data[0] == prop_atoms.net_wm_orientation_horz)
375 orient = OB_ORIENTATION_HORZ;
377 goto screen_update_layout_bail;
380 corner = OB_CORNER_TOPLEFT;
382 if (data[3] == prop_atoms.net_wm_topright)
383 corner = OB_CORNER_TOPRIGHT;
384 else if (data[3] == prop_atoms.net_wm_bottomright)
385 corner = OB_CORNER_BOTTOMRIGHT;
386 else if (data[3] == prop_atoms.net_wm_bottomleft)
387 corner = OB_CORNER_BOTTOMLEFT;
389 goto screen_update_layout_bail;
392 /* fill in a zero rows/columns */
393 if ((data[1] == 0 && data[2] == 0) || /* both 0's is bad data.. */
394 (data[1] != 0 && data[2] != 0)) { /* no 0's is bad data.. */
395 goto screen_update_layout_bail;
398 data[1] = (screen_num_desktops +
399 screen_num_desktops % data[2]) / data[2];
400 } else if (data[2] == 0) {
401 data[2] = (screen_num_desktops +
402 screen_num_desktops % data[1]) / data[1];
408 /* bounds checking */
409 if (orient == OB_ORIENTATION_HORZ) {
410 rows = MIN(rows, screen_num_desktops);
411 cols = MIN(cols, ((screen_num_desktops +
412 (screen_num_desktops % rows)) / rows));
414 cols = MIN(cols, screen_num_desktops);
415 rows = MIN(rows, ((screen_num_desktops +
416 (screen_num_desktops % cols)) / cols));
421 screen_update_layout_bail:
427 orient = OB_ORIENTATION_HORZ;
428 corner = OB_CORNER_TOPLEFT;
430 cols = screen_num_desktops;
433 screen_desktop_layout.orientation = orient;
434 screen_desktop_layout.start_corner = corner;
435 screen_desktop_layout.rows = rows;
436 screen_desktop_layout.columns = cols;
439 void screen_update_desktop_names()
443 /* empty the array */
444 g_strfreev(screen_desktop_names);
445 screen_desktop_names = NULL;
447 if (PROP_GETSS(ob_root, net_desktop_names, utf8, &screen_desktop_names))
448 for (i = 0; screen_desktop_names[i] && i <= screen_num_desktops; ++i);
451 if (i <= screen_num_desktops) {
452 screen_desktop_names = g_renew(char*, screen_desktop_names,
453 screen_num_desktops + 1);
454 screen_desktop_names[screen_num_desktops] = NULL;
455 for (; i < screen_num_desktops; ++i)
456 screen_desktop_names[i] = g_strdup("Unnamed Desktop");
460 void screen_show_desktop(gboolean show)
464 if (show == screen_showing_desktop) return; /* no change */
466 screen_showing_desktop = show;
470 for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
471 if (WINDOW_IS_CLIENT(it->data)) {
472 ObClient *client = it->data;
473 if (client->frame->visible && !client_should_show(client))
474 frame_hide(client->frame);
479 for (it = stacking_list; it != NULL; it = it->next) {
480 if (WINDOW_IS_CLIENT(it->data)) {
481 ObClient *client = it->data;
482 if (!client->frame->visible && client_should_show(client))
483 frame_show(client->frame);
490 for (it = focus_order[screen_desktop]; it; it = it->next)
491 if (((ObClient*)it->data)->type == OB_CLIENT_TYPE_DESKTOP &&
492 client_focus(it->data))
495 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
498 show = !!show; /* make it boolean */
499 PROP_SET32(ob_root, net_showing_desktop, cardinal, show);
501 dispatch_ob(Event_Ob_ShowDesktop, show, 0);
504 void screen_install_colormap(ObClient *client, gboolean install)
506 XWindowAttributes wa;
508 if (client == NULL) {
510 XInstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
512 XUninstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
514 if (XGetWindowAttributes(ob_display, client->window, &wa) &&
515 wa.colormap != None) {
516 xerror_set_ignore(TRUE);
518 XInstallColormap(RrDisplay(ob_rr_inst), wa.colormap);
520 XUninstallColormap(RrDisplay(ob_rr_inst), wa.colormap);
521 xerror_set_ignore(FALSE);
526 void screen_update_areas()
530 Rect **old_area = area;
534 g_free(monitor_area);
535 extensions_xinerama_screens(&monitor_area, &screen_num_monitors);
538 for (i = 0; area[i]; ++i)
543 area = g_new(Rect*, screen_num_desktops + 2);
544 for (i = 0; i < screen_num_desktops + 1; ++i)
545 area[i] = g_new(Rect, screen_num_monitors + 1);
548 dims = g_new(guint32, 4 * screen_num_desktops);
551 for (i = 0; i < screen_num_desktops + 1; ++i) {
555 /* calc the xinerama areas */
556 for (x = 0; x < screen_num_monitors; ++x) {
557 area[i][x] = monitor_area[x];
559 l = monitor_area[x].x;
560 t = monitor_area[x].y;
561 r = monitor_area[x].x + monitor_area[x].width - 1;
562 b = monitor_area[x].y + monitor_area[x].height - 1;
564 l = MIN(l, monitor_area[x].x);
565 t = MIN(t, monitor_area[x].y);
566 r = MAX(r, monitor_area[x].x + monitor_area[x].width - 1);
567 b = MAX(b, monitor_area[x].y + monitor_area[x].height - 1);
570 RECT_SET(area[i][x], l, t, r - l + 1, b - t + 1);
573 STRUT_SET(s, 0, 0, 0, 0);
574 for (it = client_list; it; it = it->next)
575 STRUT_ADD(s, ((ObClient*)it->data)->strut);
576 STRUT_ADD(s, dock_strut);
581 /* find the left-most xin heads, i do this in 2 loops :| */
583 for (x = 1; x < screen_num_monitors; ++x)
584 o = MIN(o, area[i][x].x);
586 for (x = 0; x < screen_num_monitors; ++x) {
587 int edge = o + s.left - area[i][x].x;
589 area[i][x].x += edge;
590 area[i][x].width -= edge;
594 area[i][screen_num_monitors].x += s.left;
595 area[i][screen_num_monitors].width -= s.left;
600 /* find the left-most xin heads, i do this in 2 loops :| */
602 for (x = 1; x < screen_num_monitors; ++x)
603 o = MIN(o, area[i][x].y);
605 for (x = 0; x < screen_num_monitors; ++x) {
606 int edge = o + s.top - area[i][x].y;
608 area[i][x].y += edge;
609 area[i][x].height -= edge;
613 area[i][screen_num_monitors].y += s.top;
614 area[i][screen_num_monitors].height -= s.top;
619 /* find the bottom-most xin heads, i do this in 2 loops :| */
620 o = area[i][0].x + area[i][0].width - 1;
621 for (x = 1; x < screen_num_monitors; ++x)
622 o = MAX(o, area[i][x].x + area[i][x].width - 1);
624 for (x = 0; x < screen_num_monitors; ++x) {
625 int edge = (area[i][x].x + area[i][x].width - 1) -
628 area[i][x].width -= edge;
631 area[i][screen_num_monitors].width -= s.right;
636 /* find the bottom-most xin heads, i do this in 2 loops :| */
637 o = area[i][0].y + area[i][0].height - 1;
638 for (x = 1; x < screen_num_monitors; ++x)
639 o = MAX(o, area[i][x].y + area[i][x].height - 1);
641 for (x = 0; x < screen_num_monitors; ++x) {
642 int edge = (area[i][x].y + area[i][x].height - 1) -
645 area[i][x].height -= edge;
648 area[i][screen_num_monitors].height -= s.bottom;
651 /* XXX when dealing with partial struts, if its in a single
652 xinerama area, then only subtract it from that area's space
653 for (x = 0; x < screen_num_monitors; ++x) {
657 do something smart with it for the 'all xinerama areas' one...
659 for (it = client_list; it; it = it->next) {
661 XXX if gunna test this shit, then gotta worry about when
662 the client moves between xinerama heads..
664 if (RECT_CONTAINS_RECT(((ObClient*)it->data)->frame->area,
672 /* XXX optimize when this is run? */
674 /* the area has changed, adjust all the maximized
676 for (it = client_list; it; it = it->next) {
677 ObClient *c = it->data;
678 if (i < screen_num_desktops) {
680 client_reconfigure(c);
681 } else if (c->desktop == DESKTOP_ALL)
682 client_reconfigure(c);
684 if (i < screen_num_desktops) {
685 /* don't set these for the 'all desktops' area */
686 dims[(i * 4) + 0] = area[i][screen_num_monitors].x;
687 dims[(i * 4) + 1] = area[i][screen_num_monitors].y;
688 dims[(i * 4) + 2] = area[i][screen_num_monitors].width;
689 dims[(i * 4) + 3] = area[i][screen_num_monitors].height;
692 PROP_SETA32(ob_root, net_workarea, cardinal,
693 dims, 4 * screen_num_desktops);
698 Rect *screen_area(guint desktop)
700 return screen_area_monitor(desktop, screen_num_monitors);
703 Rect *screen_area_monitor(guint desktop, guint head)
705 if (head > screen_num_monitors)
707 if (desktop >= screen_num_desktops) {
708 if (desktop == DESKTOP_ALL)
709 return &area[screen_num_desktops][head];
712 return &area[desktop][head];
715 Rect *screen_physical_area()
717 return screen_physical_area_monitor(screen_num_monitors);
720 Rect *screen_physical_area_monitor(guint head)
722 if (head > screen_num_monitors)
724 return &monitor_area[head];
727 static void set_root_cursor()
731 XDefineCursor(ob_display, ob_root, ob_cursor(OB_CURSOR_BUSY));
734 XDefineCursor(ob_display, ob_root, ob_cursor(OB_CURSOR_POINTER));
738 static void sn_timeout(void *data)
740 timer_stop(sn_timer);
747 static void sn_event_func(SnMonitorEvent *ev, void *data)
749 SnStartupSequence *seq;
750 const char *seq_id, *bin_name;
751 int cnt = sn_busy_cnt;
753 if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
756 seq_id = sn_startup_sequence_get_id(seq);
757 bin_name = sn_startup_sequence_get_binary_name(seq);
759 if (!(seq_id && bin_name))
762 switch (sn_monitor_event_get_type(ev)) {
763 case SN_MONITOR_EVENT_INITIATED:
766 timer_stop(sn_timer);
767 /* 30 second timeout for apps to start */
768 sn_timer = timer_start(30 * 1000000, sn_timeout, NULL);
770 case SN_MONITOR_EVENT_CHANGED:
772 case SN_MONITOR_EVENT_COMPLETED:
773 if (sn_busy_cnt) --sn_busy_cnt;
775 timer_stop(sn_timer);
779 case SN_MONITOR_EVENT_CANCELED:
780 if (sn_busy_cnt) --sn_busy_cnt;
782 timer_stop(sn_timer);
787 if (sn_busy_cnt != cnt)