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, RootWindow(ob_display, ob_screen),
66 xerror_set_ignore(FALSE);
68 g_message("A window manager is already running on screen %d",
74 g_message("Managing screen %d", ob_screen);
78 /* set the OPENBOX_PID hint */
80 PROP_SET32(RootWindow(ob_display, ob_screen),
81 openbox_pid, cardinal, pid);
83 /* create the netwm support window */
84 attrib.override_redirect = TRUE;
85 screen_support_win = XCreateWindow(ob_display,
86 RootWindow(ob_display, ob_screen),
88 CopyFromParent, InputOutput,
90 CWOverrideRedirect, &attrib);
91 XMapRaised(ob_display, screen_support_win);
93 /* set supporting window */
94 PROP_SET32(RootWindow(ob_display, ob_screen),
95 net_supporting_wm_check, window, screen_support_win);
97 /* set properties on the supporting window */
98 PROP_SETS(screen_support_win, net_wm_name, "Openbox");
99 PROP_SET32(screen_support_win, net_supporting_wm_check,
100 window, screen_support_win);
102 /* set the _NET_SUPPORTED_ATOMS hint */
105 supported = g_new(guint32, num_support);
106 supported[i++] = prop_atoms.net_current_desktop;
107 supported[i++] = prop_atoms.net_number_of_desktops;
108 supported[i++] = prop_atoms.net_desktop_geometry;
109 supported[i++] = prop_atoms.net_desktop_viewport;
110 supported[i++] = prop_atoms.net_active_window;
111 supported[i++] = prop_atoms.net_workarea;
112 supported[i++] = prop_atoms.net_client_list;
113 supported[i++] = prop_atoms.net_client_list_stacking;
114 supported[i++] = prop_atoms.net_desktop_names;
115 supported[i++] = prop_atoms.net_close_window;
116 supported[i++] = prop_atoms.net_desktop_layout;
117 supported[i++] = prop_atoms.net_showing_desktop;
118 supported[i++] = prop_atoms.net_wm_name;
119 supported[i++] = prop_atoms.net_wm_visible_name;
120 supported[i++] = prop_atoms.net_wm_icon_name;
121 supported[i++] = prop_atoms.net_wm_visible_icon_name;
122 supported[i++] = prop_atoms.net_wm_desktop;
123 supported[i++] = prop_atoms.net_wm_strut;
124 supported[i++] = prop_atoms.net_wm_window_type;
125 supported[i++] = prop_atoms.net_wm_window_type_desktop;
126 supported[i++] = prop_atoms.net_wm_window_type_dock;
127 supported[i++] = prop_atoms.net_wm_window_type_toolbar;
128 supported[i++] = prop_atoms.net_wm_window_type_menu;
129 supported[i++] = prop_atoms.net_wm_window_type_utility;
130 supported[i++] = prop_atoms.net_wm_window_type_splash;
131 supported[i++] = prop_atoms.net_wm_window_type_dialog;
132 supported[i++] = prop_atoms.net_wm_window_type_normal;
133 supported[i++] = prop_atoms.net_wm_allowed_actions;
134 supported[i++] = prop_atoms.net_wm_action_move;
135 supported[i++] = prop_atoms.net_wm_action_resize;
136 supported[i++] = prop_atoms.net_wm_action_minimize;
137 supported[i++] = prop_atoms.net_wm_action_shade;
138 supported[i++] = prop_atoms.net_wm_action_maximize_horz;
139 supported[i++] = prop_atoms.net_wm_action_maximize_vert;
140 supported[i++] = prop_atoms.net_wm_action_fullscreen;
141 supported[i++] = prop_atoms.net_wm_action_change_desktop;
142 supported[i++] = prop_atoms.net_wm_action_close;
143 supported[i++] = prop_atoms.net_wm_state;
144 supported[i++] = prop_atoms.net_wm_state_modal;
145 supported[i++] = prop_atoms.net_wm_state_maximized_vert;
146 supported[i++] = prop_atoms.net_wm_state_maximized_horz;
147 supported[i++] = prop_atoms.net_wm_state_shaded;
148 supported[i++] = prop_atoms.net_wm_state_skip_taskbar;
149 supported[i++] = prop_atoms.net_wm_state_skip_pager;
150 supported[i++] = prop_atoms.net_wm_state_hidden;
151 supported[i++] = prop_atoms.net_wm_state_fullscreen;
152 supported[i++] = prop_atoms.net_wm_state_above;
153 supported[i++] = prop_atoms.net_wm_state_below;
154 supported[i++] = prop_atoms.net_moveresize_window;
155 supported[i++] = prop_atoms.net_wm_moveresize;
156 supported[i++] = prop_atoms.net_wm_moveresize_size_topleft;
157 supported[i++] = prop_atoms.net_wm_moveresize_size_top;
158 supported[i++] = prop_atoms.net_wm_moveresize_size_topright;
159 supported[i++] = prop_atoms.net_wm_moveresize_size_right;
160 supported[i++] = prop_atoms.net_wm_moveresize_size_bottomright;
161 supported[i++] = prop_atoms.net_wm_moveresize_size_bottom;
162 supported[i++] = prop_atoms.net_wm_moveresize_size_bottomleft;
163 supported[i++] = prop_atoms.net_wm_moveresize_size_left;
164 supported[i++] = prop_atoms.net_wm_moveresize_move;
165 supported[i++] = prop_atoms.net_wm_moveresize_size_keyboard;
166 supported[i++] = prop_atoms.net_wm_moveresize_move_keyboard;
167 g_assert(i == num_support);
169 supported[] = prop_atoms.net_wm_action_stick;
172 PROP_SETA32(RootWindow(ob_display, ob_screen),
173 net_supported, atom, supported, num_support);
179 void screen_startup()
184 /* get the initial size */
188 screen_desktop_names = g_new(char*,
189 g_slist_length(config_desktops_names) + 1);
190 for (i = 0, it = config_desktops_names; it; ++i, it = it->next)
191 screen_desktop_names[i] = it->data; /* dont strdup */
192 screen_desktop_names[i] = NULL;
193 PROP_SETSS(RootWindow(ob_display, ob_screen),
194 net_desktop_names, screen_desktop_names);
195 g_free(screen_desktop_names); /* dont free the individual strings */
196 screen_desktop_names = NULL;
198 screen_num_desktops = 0;
199 screen_set_num_desktops(config_desktops_num);
200 if (startup_desktop >= screen_num_desktops)
202 screen_desktop = startup_desktop;
203 screen_set_desktop(startup_desktop);
205 /* don't start in showing-desktop mode */
206 screen_showing_desktop = FALSE;
207 PROP_SET32(RootWindow(ob_display, ob_screen),
208 net_showing_desktop, cardinal, screen_showing_desktop);
210 screen_update_layout();
213 sn_context = sn_monitor_context_new(ob_sn_display, ob_screen,
214 sn_event_func, NULL, NULL);
219 void screen_shutdown()
223 XSelectInput(ob_display, RootWindow(ob_display, ob_screen), NoEventMask);
225 /* we're not running here no more! */
226 PROP_ERASE(RootWindow(ob_display, ob_screen), openbox_pid);
228 PROP_ERASE(RootWindow(ob_display, ob_screen), net_supported);
229 /* don't keep this mode */
230 PROP_ERASE(RootWindow(ob_display, ob_screen), net_showing_desktop);
232 XDestroyWindow(ob_display, screen_support_win);
234 g_strfreev(screen_desktop_names);
235 for (r = area; *r; ++r)
242 static int oldw = 0, oldh = 0;
247 w = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
248 h = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
250 if (w == oldw && h == oldh) return;
254 /* Set the _NET_DESKTOP_GEOMETRY hint */
255 screen_physical_size.width = geometry[0] = w;
256 screen_physical_size.height = geometry[1] = h;
257 PROP_SETA32(RootWindow(ob_display, ob_screen),
258 net_desktop_geometry, cardinal, geometry, 2);
260 if (ob_state() == OB_STATE_STARTING)
264 screen_update_areas();
266 for (it = client_list; it; it = it->next)
267 client_move_onscreen(it->data);
270 void screen_set_num_desktops(guint num)
278 old = screen_num_desktops;
279 screen_num_desktops = num;
280 PROP_SET32(RootWindow(ob_display, ob_screen),
281 net_number_of_desktops, cardinal, num);
283 /* set the viewport hint */
284 viewport = g_new0(guint32, num * 2);
285 PROP_SETA32(RootWindow(ob_display, ob_screen),
286 net_desktop_viewport, cardinal, viewport, num * 2);
289 /* the number of rows/columns will differ */
290 screen_update_layout();
292 /* may be some unnamed desktops that we need to fill in with names */
293 screen_update_desktop_names();
295 /* update the focus lists */
296 /* free our lists for the desktops which have disappeared */
297 for (i = num; i < old; ++i)
298 g_list_free(focus_order[i]);
299 /* realloc the array */
300 focus_order = g_renew(GList*, focus_order, num);
301 /* set the new lists to be empty */
302 for (i = old; i < num; ++i)
303 focus_order[i] = NULL;
305 /* move windows on desktops that will no longer exist! */
306 for (it = client_list; it != NULL; it = it->next) {
307 ObClient *c = it->data;
308 if (c->desktop >= num && c->desktop != DESKTOP_ALL)
309 client_set_desktop(c, num - 1, FALSE);
312 /* change our struts/area to match (after moving windows) */
313 screen_update_areas();
315 dispatch_ob(Event_Ob_NumDesktops, num, old);
317 /* change our desktop if we're on one that no longer exists! */
318 if (screen_desktop >= screen_num_desktops)
319 screen_set_desktop(num - 1);
322 void screen_set_desktop(guint num)
328 g_assert(num < screen_num_desktops);
330 old = screen_desktop;
331 screen_desktop = num;
332 PROP_SET32(RootWindow(ob_display, ob_screen),
333 net_current_desktop, cardinal, num);
335 if (old == num) return;
337 g_message("Moving to desktop %d", num+1);
339 /* show windows before hiding the rest to lessen the enter/leave events */
341 /* show windows from top to bottom */
342 for (it = stacking_list; it != NULL; it = it->next) {
343 if (WINDOW_IS_CLIENT(it->data)) {
344 ObClient *c = it->data;
345 if (!c->frame->visible && client_should_show(c))
346 frame_show(c->frame);
350 /* hide windows from bottom to top */
351 for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
352 if (WINDOW_IS_CLIENT(it->data)) {
353 ObClient *c = it->data;
354 if (c->frame->visible && !client_should_show(c))
355 frame_hide(c->frame);
359 /* focus the last focused window on the desktop, and ignore enter events
360 from the switch so it doesnt mess with the focus */
361 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
363 g_message("switch fallback");
365 focus_fallback(OB_FOCUS_FALLBACK_DESKTOP);
367 g_message("/switch fallback");
370 dispatch_ob(Event_Ob_Desktop, num, old);
373 void screen_update_layout()
375 ObOrientation orient;
381 gboolean valid = FALSE;
383 if (PROP_GETA32(RootWindow(ob_display, ob_screen),
384 net_desktop_layout, cardinal, &data, &num)) {
385 if (num == 3 || num == 4) {
387 if (data[0] == prop_atoms.net_wm_orientation_vert)
388 orient = OB_ORIENTATION_VERT;
389 else if (data[0] == prop_atoms.net_wm_orientation_horz)
390 orient = OB_ORIENTATION_HORZ;
392 goto screen_update_layout_bail;
395 corner = OB_CORNER_TOPLEFT;
397 if (data[3] == prop_atoms.net_wm_topright)
398 corner = OB_CORNER_TOPRIGHT;
399 else if (data[3] == prop_atoms.net_wm_bottomright)
400 corner = OB_CORNER_BOTTOMRIGHT;
401 else if (data[3] == prop_atoms.net_wm_bottomleft)
402 corner = OB_CORNER_BOTTOMLEFT;
404 goto screen_update_layout_bail;
407 /* fill in a zero rows/columns */
408 if ((data[1] == 0 && data[2] == 0) || /* both 0's is bad data.. */
409 (data[1] != 0 && data[2] != 0)) { /* no 0's is bad data.. */
410 goto screen_update_layout_bail;
413 data[1] = (screen_num_desktops +
414 screen_num_desktops % data[2]) / data[2];
415 } else if (data[2] == 0) {
416 data[2] = (screen_num_desktops +
417 screen_num_desktops % data[1]) / data[1];
423 /* bounds checking */
424 if (orient == OB_ORIENTATION_HORZ) {
425 rows = MIN(rows, screen_num_desktops);
426 cols = MIN(cols, ((screen_num_desktops +
427 (screen_num_desktops % rows)) / rows));
429 cols = MIN(cols, screen_num_desktops);
430 rows = MIN(rows, ((screen_num_desktops +
431 (screen_num_desktops % cols)) / cols));
436 screen_update_layout_bail:
442 orient = OB_ORIENTATION_HORZ;
443 corner = OB_CORNER_TOPLEFT;
445 cols = screen_num_desktops;
448 screen_desktop_layout.orientation = orient;
449 screen_desktop_layout.start_corner = corner;
450 screen_desktop_layout.rows = rows;
451 screen_desktop_layout.columns = cols;
454 void screen_update_desktop_names()
458 /* empty the array */
459 g_strfreev(screen_desktop_names);
460 screen_desktop_names = NULL;
462 if (PROP_GETSS(RootWindow(ob_display, ob_screen),
463 net_desktop_names, utf8, &screen_desktop_names))
464 for (i = 0; screen_desktop_names[i] && i <= screen_num_desktops; ++i);
467 if (i <= screen_num_desktops) {
468 screen_desktop_names = g_renew(char*, screen_desktop_names,
469 screen_num_desktops + 1);
470 screen_desktop_names[screen_num_desktops] = NULL;
471 for (; i < screen_num_desktops; ++i)
472 screen_desktop_names[i] = g_strdup("Unnamed Desktop");
476 void screen_show_desktop(gboolean show)
480 if (show == screen_showing_desktop) return; /* no change */
482 screen_showing_desktop = show;
486 for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
487 if (WINDOW_IS_CLIENT(it->data)) {
488 ObClient *client = it->data;
489 if (client->frame->visible && !client_should_show(client))
490 frame_hide(client->frame);
495 for (it = stacking_list; it != NULL; it = it->next) {
496 if (WINDOW_IS_CLIENT(it->data)) {
497 ObClient *client = it->data;
498 if (!client->frame->visible && client_should_show(client))
499 frame_show(client->frame);
506 for (it = focus_order[screen_desktop]; it; it = it->next)
507 if (((ObClient*)it->data)->type == OB_CLIENT_TYPE_DESKTOP &&
508 client_focus(it->data))
511 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
514 show = !!show; /* make it boolean */
515 PROP_SET32(RootWindow(ob_display, ob_screen),
516 net_showing_desktop, cardinal, show);
518 dispatch_ob(Event_Ob_ShowDesktop, show, 0);
521 void screen_install_colormap(ObClient *client, gboolean install)
523 XWindowAttributes wa;
525 if (client == NULL) {
527 XInstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
529 XUninstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
531 if (XGetWindowAttributes(ob_display, client->window, &wa) &&
532 wa.colormap != None) {
533 xerror_set_ignore(TRUE);
535 XInstallColormap(RrDisplay(ob_rr_inst), wa.colormap);
537 XUninstallColormap(RrDisplay(ob_rr_inst), wa.colormap);
538 xerror_set_ignore(FALSE);
543 void screen_update_areas()
547 Rect **old_area = area;
551 g_free(monitor_area);
552 extensions_xinerama_screens(&monitor_area, &screen_num_monitors);
555 for (i = 0; area[i]; ++i)
560 area = g_new(Rect*, screen_num_desktops + 2);
561 for (i = 0; i < screen_num_desktops + 1; ++i)
562 area[i] = g_new(Rect, screen_num_monitors + 1);
565 dims = g_new(guint32, 4 * screen_num_desktops);
568 for (i = 0; i < screen_num_desktops + 1; ++i) {
572 /* calc the xinerama areas */
573 for (x = 0; x < screen_num_monitors; ++x) {
574 area[i][x] = monitor_area[x];
576 l = monitor_area[x].x;
577 t = monitor_area[x].y;
578 r = monitor_area[x].x + monitor_area[x].width - 1;
579 b = monitor_area[x].y + monitor_area[x].height - 1;
581 l = MIN(l, monitor_area[x].x);
582 t = MIN(t, monitor_area[x].y);
583 r = MAX(r, monitor_area[x].x + monitor_area[x].width - 1);
584 b = MAX(b, monitor_area[x].y + monitor_area[x].height - 1);
587 RECT_SET(area[i][x], l, t, r - l + 1, b - t + 1);
590 STRUT_SET(s, 0, 0, 0, 0);
591 for (it = client_list; it; it = it->next)
592 STRUT_ADD(s, ((ObClient*)it->data)->strut);
593 STRUT_ADD(s, dock_strut);
598 /* find the left-most xin heads, i do this in 2 loops :| */
600 for (x = 1; x < screen_num_monitors; ++x)
601 o = MIN(o, area[i][x].x);
603 for (x = 0; x < screen_num_monitors; ++x) {
604 int edge = o + s.left - area[i][x].x;
606 area[i][x].x += edge;
607 area[i][x].width -= edge;
611 area[i][screen_num_monitors].x += s.left;
612 area[i][screen_num_monitors].width -= s.left;
617 /* find the left-most xin heads, i do this in 2 loops :| */
619 for (x = 1; x < screen_num_monitors; ++x)
620 o = MIN(o, area[i][x].y);
622 for (x = 0; x < screen_num_monitors; ++x) {
623 int edge = o + s.top - area[i][x].y;
625 area[i][x].y += edge;
626 area[i][x].height -= edge;
630 area[i][screen_num_monitors].y += s.top;
631 area[i][screen_num_monitors].height -= s.top;
636 /* find the bottom-most xin heads, i do this in 2 loops :| */
637 o = area[i][0].x + area[i][0].width - 1;
638 for (x = 1; x < screen_num_monitors; ++x)
639 o = MAX(o, area[i][x].x + area[i][x].width - 1);
641 for (x = 0; x < screen_num_monitors; ++x) {
642 int edge = (area[i][x].x + area[i][x].width - 1) -
645 area[i][x].width -= edge;
648 area[i][screen_num_monitors].width -= s.right;
653 /* find the bottom-most xin heads, i do this in 2 loops :| */
654 o = area[i][0].y + area[i][0].height - 1;
655 for (x = 1; x < screen_num_monitors; ++x)
656 o = MAX(o, area[i][x].y + area[i][x].height - 1);
658 for (x = 0; x < screen_num_monitors; ++x) {
659 int edge = (area[i][x].y + area[i][x].height - 1) -
662 area[i][x].height -= edge;
665 area[i][screen_num_monitors].height -= s.bottom;
668 /* XXX when dealing with partial struts, if its in a single
669 xinerama area, then only subtract it from that area's space
670 for (x = 0; x < screen_num_monitors; ++x) {
674 do something smart with it for the 'all xinerama areas' one...
676 for (it = client_list; it; it = it->next) {
678 XXX if gunna test this shit, then gotta worry about when
679 the client moves between xinerama heads..
681 if (RECT_CONTAINS_RECT(((ObClient*)it->data)->frame->area,
689 /* XXX optimize when this is run? */
691 /* the area has changed, adjust all the maximized
693 for (it = client_list; it; it = it->next) {
694 ObClient *c = it->data;
695 if (i < screen_num_desktops) {
697 client_reconfigure(c);
698 } else if (c->desktop == DESKTOP_ALL)
699 client_reconfigure(c);
701 if (i < screen_num_desktops) {
702 /* don't set these for the 'all desktops' area */
703 dims[(i * 4) + 0] = area[i][screen_num_monitors].x;
704 dims[(i * 4) + 1] = area[i][screen_num_monitors].y;
705 dims[(i * 4) + 2] = area[i][screen_num_monitors].width;
706 dims[(i * 4) + 3] = area[i][screen_num_monitors].height;
709 PROP_SETA32(RootWindow(ob_display, ob_screen), net_workarea, cardinal,
710 dims, 4 * screen_num_desktops);
715 Rect *screen_area(guint desktop)
717 return screen_area_monitor(desktop, screen_num_monitors);
720 Rect *screen_area_monitor(guint desktop, guint head)
722 if (head > screen_num_monitors)
724 if (desktop >= screen_num_desktops) {
725 if (desktop == DESKTOP_ALL)
726 return &area[screen_num_desktops][head];
729 return &area[desktop][head];
732 Rect *screen_physical_area()
734 return screen_physical_area_monitor(screen_num_monitors);
737 Rect *screen_physical_area_monitor(guint head)
739 if (head > screen_num_monitors)
741 return &monitor_area[head];
744 static void set_root_cursor()
748 XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
749 ob_cursor(OB_CURSOR_BUSY));
752 XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
753 ob_cursor(OB_CURSOR_POINTER));
757 static void sn_timeout(void *data)
759 timer_stop(sn_timer);
766 static void sn_event_func(SnMonitorEvent *ev, void *data)
768 SnStartupSequence *seq;
769 const char *seq_id, *bin_name;
770 int cnt = sn_busy_cnt;
772 if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
775 seq_id = sn_startup_sequence_get_id(seq);
776 bin_name = sn_startup_sequence_get_binary_name(seq);
778 if (!(seq_id && bin_name))
781 switch (sn_monitor_event_get_type(ev)) {
782 case SN_MONITOR_EVENT_INITIATED:
785 timer_stop(sn_timer);
786 /* 30 second timeout for apps to start */
787 sn_timer = timer_start(30 * 1000000, sn_timeout, NULL);
789 case SN_MONITOR_EVENT_CHANGED:
791 case SN_MONITOR_EVENT_COMPLETED:
792 if (sn_busy_cnt) --sn_busy_cnt;
794 timer_stop(sn_timer);
798 case SN_MONITOR_EVENT_CANCELED:
799 if (sn_busy_cnt) --sn_busy_cnt;
801 timer_stop(sn_timer);
806 if (sn_busy_cnt != cnt)
811 gboolean screen_pointer_pos(int *x, int *y)
817 return !!XQueryPointer(ob_display, RootWindow(ob_display, ob_screen),
818 &w, &w, x, y, &i, &i, &u);