9 #include "../render/render.h"
13 # include <sys/types.h>
17 /*! The event mask to grab on the root window */
18 #define ROOT_EVENTMASK (/*ColormapChangeMask |*/ PropertyChangeMask | \
19 EnterWindowMask | LeaveWindowMask | \
20 SubstructureNotifyMask | SubstructureRedirectMask | \
21 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
23 guint screen_num_desktops = 0;
24 guint screen_desktop = 0;
25 Size screen_physical_size;
26 gboolean screen_showing_desktop;
27 DesktopLayout screen_desktop_layout;
28 char **screen_desktop_names = NULL;
30 static Rect *area = NULL;
31 static Strut *strut = NULL;
32 static Window support_window = None;
34 static void screen_update_area();
36 static gboolean running;
37 static int another_running(Display *d, XErrorEvent *e)
40 g_message("A window manager is already running on screen %d",
46 gboolean screen_annex()
54 old = XSetErrorHandler(another_running);
55 XSelectInput(ob_display, ob_root, ROOT_EVENTMASK);
56 XSync(ob_display, FALSE);
57 XSetErrorHandler(old);
61 g_message("Managing screen %d", ob_screen);
63 /* set the mouse cursor for the root window (the default cursor) */
64 XDefineCursor(ob_display, ob_root, ob_cursors.ptr);
66 /* set the OPENBOX_PID hint */
68 PROP_SET32(ob_root, openbox_pid, cardinal, pid);
70 /* create the netwm support window */
71 support_window = XCreateSimpleWindow(ob_display, ob_root, 0,0,1,1,0,0,0);
73 /* set supporting window */
74 PROP_SET32(ob_root, net_supporting_wm_check, window, support_window);
76 /* set properties on the supporting window */
77 PROP_SETS(support_window, net_wm_name, "Openbox");
78 PROP_SET32(support_window, net_supporting_wm_check, window,support_window);
80 /* set the _NET_SUPPORTED_ATOMS hint */
83 supported = g_new(guint32, num_support);
84 supported[i++] = prop_atoms.net_current_desktop;
85 supported[i++] = prop_atoms.net_number_of_desktops;
86 supported[i++] = prop_atoms.net_desktop_geometry;
87 supported[i++] = prop_atoms.net_desktop_viewport;
88 supported[i++] = prop_atoms.net_active_window;
89 supported[i++] = prop_atoms.net_workarea;
90 supported[i++] = prop_atoms.net_client_list;
91 supported[i++] = prop_atoms.net_client_list_stacking;
92 supported[i++] = prop_atoms.net_desktop_names;
93 supported[i++] = prop_atoms.net_close_window;
94 supported[i++] = prop_atoms.net_desktop_layout;
95 supported[i++] = prop_atoms.net_showing_desktop;
96 supported[i++] = prop_atoms.net_wm_name;
97 supported[i++] = prop_atoms.net_wm_visible_name;
98 supported[i++] = prop_atoms.net_wm_icon_name;
99 supported[i++] = prop_atoms.net_wm_visible_icon_name;
100 supported[i++] = prop_atoms.net_wm_desktop;
101 supported[i++] = prop_atoms.net_wm_strut;
102 supported[i++] = prop_atoms.net_wm_window_type;
103 supported[i++] = prop_atoms.net_wm_window_type_desktop;
104 supported[i++] = prop_atoms.net_wm_window_type_dock;
105 supported[i++] = prop_atoms.net_wm_window_type_toolbar;
106 supported[i++] = prop_atoms.net_wm_window_type_menu;
107 supported[i++] = prop_atoms.net_wm_window_type_utility;
108 supported[i++] = prop_atoms.net_wm_window_type_splash;
109 supported[i++] = prop_atoms.net_wm_window_type_dialog;
110 supported[i++] = prop_atoms.net_wm_window_type_normal;
111 supported[i++] = prop_atoms.net_wm_allowed_actions;
112 supported[i++] = prop_atoms.net_wm_action_move;
113 supported[i++] = prop_atoms.net_wm_action_resize;
114 supported[i++] = prop_atoms.net_wm_action_minimize;
115 supported[i++] = prop_atoms.net_wm_action_shade;
116 supported[i++] = prop_atoms.net_wm_action_maximize_horz;
117 supported[i++] = prop_atoms.net_wm_action_maximize_vert;
118 supported[i++] = prop_atoms.net_wm_action_fullscreen;
119 supported[i++] = prop_atoms.net_wm_action_change_desktop;
120 supported[i++] = prop_atoms.net_wm_action_close;
121 supported[i++] = prop_atoms.net_wm_state;
122 supported[i++] = prop_atoms.net_wm_state_modal;
123 supported[i++] = prop_atoms.net_wm_state_maximized_vert;
124 supported[i++] = prop_atoms.net_wm_state_maximized_horz;
125 supported[i++] = prop_atoms.net_wm_state_shaded;
126 supported[i++] = prop_atoms.net_wm_state_skip_taskbar;
127 supported[i++] = prop_atoms.net_wm_state_skip_pager;
128 supported[i++] = prop_atoms.net_wm_state_hidden;
129 supported[i++] = prop_atoms.net_wm_state_fullscreen;
130 supported[i++] = prop_atoms.net_wm_state_above;
131 supported[i++] = prop_atoms.net_wm_state_below;
132 supported[i++] = prop_atoms.net_moveresize_window;
133 supported[i++] = prop_atoms.net_wm_moveresize;
134 supported[i++] = prop_atoms.net_wm_moveresize_size_topleft;
135 supported[i++] = prop_atoms.net_wm_moveresize_size_top;
136 supported[i++] = prop_atoms.net_wm_moveresize_size_topright;
137 supported[i++] = prop_atoms.net_wm_moveresize_size_right;
138 supported[i++] = prop_atoms.net_wm_moveresize_size_bottomright;
139 supported[i++] = prop_atoms.net_wm_moveresize_size_bottom;
140 supported[i++] = prop_atoms.net_wm_moveresize_size_bottomleft;
141 supported[i++] = prop_atoms.net_wm_moveresize_size_left;
142 supported[i++] = prop_atoms.net_wm_moveresize_move;
143 supported[i++] = prop_atoms.net_wm_moveresize_size_keyboard;
144 supported[i++] = prop_atoms.net_wm_moveresize_move_keyboard;
145 g_assert(i == num_support);
147 supported[] = prop_atoms.net_wm_action_stick;
150 PROP_SETA32(ob_root, net_supported, atom, supported, num_support);
156 void screen_startup()
161 /* get the initial size */
165 screen_desktop_names = g_new(char*,
166 g_slist_length(config_desktops_names) + 1);
167 for (i = 0, it = config_desktops_names; it; ++i, it = it->next)
168 screen_desktop_names[i] = it->data; /* dont strdup */
169 screen_desktop_names[i] = NULL;
170 PROP_SETSS(ob_root, net_desktop_names, screen_desktop_names);
171 g_free(screen_desktop_names); /* dont free the individual strings */
172 screen_desktop_names = NULL;
174 screen_num_desktops = 0;
175 screen_set_num_desktops(config_desktops_num);
177 screen_set_desktop(0);
179 /* don't start in showing-desktop mode */
180 screen_showing_desktop = FALSE;
181 PROP_SET32(ob_root, net_showing_desktop, cardinal, screen_showing_desktop);
183 screen_update_layout();
186 void screen_shutdown()
188 XSelectInput(ob_display, ob_root, NoEventMask);
190 PROP_ERASE(ob_root, openbox_pid); /* we're not running here no more! */
191 PROP_ERASE(ob_root, net_supported); /* not without us */
192 PROP_ERASE(ob_root, net_showing_desktop); /* don't keep this mode */
194 XDestroyWindow(ob_display, support_window);
196 g_strfreev(screen_desktop_names);
203 /* XXX RandR support here? */
206 /* Set the _NET_DESKTOP_GEOMETRY hint */
207 geometry[0] = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
208 geometry[1] = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
209 PROP_SETA32(ob_root, net_desktop_geometry, cardinal, geometry, 2);
210 screen_physical_size.width = geometry[0];
211 screen_physical_size.height = geometry[1];
213 if (ob_state == State_Starting)
216 screen_update_struts();
218 /* XXX adjust more stuff ? */
221 void screen_set_num_desktops(guint num)
229 old = screen_num_desktops;
230 screen_num_desktops = num;
231 PROP_SET32(ob_root, net_number_of_desktops, cardinal, num);
233 /* set the viewport hint */
234 viewport = g_new0(guint32, num * 2);
235 PROP_SETA32(ob_root, net_desktop_viewport, cardinal, viewport, num * 2);
238 /* change our struts/area to match */
239 screen_update_struts();
241 /* the number of rows/columns will differ */
242 screen_update_layout();
244 /* may be some unnamed desktops that we need to fill in with names */
245 screen_update_desktop_names();
247 /* update the focus lists */
248 /* free our lists for the desktops which have disappeared */
249 for (i = num; i < old; ++i)
250 g_list_free(focus_order[i]);
251 /* realloc the array */
252 focus_order = g_renew(GList*, focus_order, num);
253 /* set the new lists to be empty */
254 for (i = old; i < num; ++i)
255 focus_order[i] = NULL;
257 /* move windows on desktops that will no longer exist! */
258 for (it = client_list; it != NULL; it = it->next) {
259 Client *c = it->data;
260 if (c->desktop >= num)
261 client_set_desktop(c, num - 1, FALSE);
264 dispatch_ob(Event_Ob_NumDesktops, num, old);
266 /* change our desktop if we're on one that no longer exists! */
267 if (screen_desktop >= screen_num_desktops)
268 screen_set_desktop(num - 1);
271 void screen_set_desktop(guint num)
277 g_assert(num < screen_num_desktops);
279 old = screen_desktop;
280 screen_desktop = num;
281 PROP_SET32(ob_root, net_current_desktop, cardinal, num);
283 if (old == num) return;
285 g_message("Moving to desktop %d", num+1);
287 /* show windows before hiding the rest to lessen the enter/leave events */
289 /* show windows from top to bottom */
290 for (it = stacking_list; it != NULL; it = it->next) {
291 Client *c = it->data;
292 if (!c->frame->visible && client_should_show(c))
293 frame_show(c->frame);
296 /* hide windows from bottom to top */
297 for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
298 Client *c = it->data;
299 if (c->frame->visible && !client_should_show(c))
300 frame_hide(c->frame);
303 /* focus the last focused window on the desktop, and ignore enter events
304 from the switch so it doesnt mess with the focus */
305 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
306 focus_fallback(Fallback_Desktop);
308 dispatch_ob(Event_Ob_Desktop, num, old);
311 void screen_update_layout()
313 guint32 *data = NULL;
317 screen_desktop_layout.orientation = prop_atoms.net_wm_orientation_horz;
318 screen_desktop_layout.start_corner = prop_atoms.net_wm_topleft;
319 screen_desktop_layout.rows = 1;
320 screen_desktop_layout.columns = screen_num_desktops;
322 if (PROP_GETA32(ob_root, net_desktop_layout, cardinal, &data, &num)) {
323 if (num == 3 || num == 4) {
324 if (data[0] == prop_atoms.net_wm_orientation_vert)
325 screen_desktop_layout.orientation = data[0];
327 screen_desktop_layout.start_corner =
328 prop_atoms.net_wm_topright;
330 if (data[3] == prop_atoms.net_wm_topright)
331 screen_desktop_layout.start_corner = data[3];
332 else if (data[3] == prop_atoms.net_wm_bottomright)
333 screen_desktop_layout.start_corner = data[3];
334 else if (data[3] == prop_atoms.net_wm_bottomleft)
335 screen_desktop_layout.start_corner = data[3];
338 /* fill in a zero rows/columns */
339 if (!(data[1] == 0 && data[2] == 0)) { /* both 0's is bad data.. */
341 data[1] = (screen_num_desktops +
342 screen_num_desktops % data[2]) / data[2];
343 } else if (data[2] == 0) {
344 data[2] = (screen_num_desktops +
345 screen_num_desktops % data[1]) / data[1];
347 screen_desktop_layout.columns = data[1];
348 screen_desktop_layout.rows = data[2];
351 /* bounds checking */
352 if (screen_desktop_layout.orientation ==
353 prop_atoms.net_wm_orientation_horz) {
354 if (screen_desktop_layout.rows > screen_num_desktops)
355 screen_desktop_layout.rows = screen_num_desktops;
356 if (screen_desktop_layout.columns >
357 ((screen_num_desktops + screen_num_desktops %
358 screen_desktop_layout.rows) /
359 screen_desktop_layout.rows))
360 screen_desktop_layout.columns =
361 (screen_num_desktops + screen_num_desktops %
362 screen_desktop_layout.rows) /
363 screen_desktop_layout.rows;
365 if (screen_desktop_layout.columns > screen_num_desktops)
366 screen_desktop_layout.columns = screen_num_desktops;
367 if (screen_desktop_layout.rows >
368 ((screen_num_desktops + screen_num_desktops %
369 screen_desktop_layout.columns) /
370 screen_desktop_layout.columns))
371 screen_desktop_layout.rows =
372 (screen_num_desktops + screen_num_desktops %
373 screen_desktop_layout.columns) /
374 screen_desktop_layout.columns;
381 void screen_update_desktop_names()
385 /* empty the array */
386 g_strfreev(screen_desktop_names);
387 screen_desktop_names = NULL;
389 if (PROP_GETSS(ob_root, net_desktop_names, utf8, &screen_desktop_names))
390 for (i = 0; screen_desktop_names[i] && i <= screen_num_desktops; ++i);
393 if (i <= screen_num_desktops) {
394 screen_desktop_names = g_renew(char*, screen_desktop_names,
395 screen_num_desktops + 1);
396 screen_desktop_names[screen_num_desktops] = NULL;
397 for (; i < screen_num_desktops; ++i)
398 screen_desktop_names[i] = g_strdup("Unnamed Desktop");
402 void screen_show_desktop(gboolean show)
406 if (show == screen_showing_desktop) return; /* no change */
408 screen_showing_desktop = show;
412 for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
413 Client *client = it->data;
414 if (client->frame->visible && !client_should_show(client))
415 frame_hide(client->frame);
419 for (it = stacking_list; it != NULL; it = it->next) {
420 Client *client = it->data;
421 if (!client->frame->visible && client_should_show(client))
422 frame_show(client->frame);
428 for (it = focus_order[screen_desktop]; it; it = it->next)
429 if (((Client*)it->data)->type == Type_Desktop &&
430 client_focus(it->data))
433 focus_fallback(Fallback_NoFocus);
436 show = !!show; /* make it boolean */
437 PROP_SET32(ob_root, net_showing_desktop, cardinal, show);
439 dispatch_ob(Event_Ob_ShowDesktop, show, 0);
442 void screen_install_colormap(Client *client, gboolean install)
444 XWindowAttributes wa;
446 if (client == NULL) {
448 XInstallColormap(ob_display, render_colormap);
450 XUninstallColormap(ob_display, render_colormap);
452 if (XGetWindowAttributes(ob_display, client->window, &wa) &&
453 wa.colormap != None) {
455 XInstallColormap(ob_display, wa.colormap);
457 XUninstallColormap(ob_display, wa.colormap);
462 void screen_update_struts()
468 strut = g_new0(Strut, screen_num_desktops + 1);
470 for (it = client_list; it != NULL; it = it->next) {
471 Client *c = it->data;
472 if (c->iconic) continue; /* these dont count in the strut */
474 if (c->desktop == 0xffffffff) {
475 for (i = 0; i < screen_num_desktops; ++i)
476 STRUT_ADD(strut[i], c->strut);
478 g_assert(c->desktop < screen_num_desktops);
479 STRUT_ADD(strut[c->desktop], c->strut);
481 /* apply to the 'all desktops' strut */
482 STRUT_ADD(strut[screen_num_desktops], c->strut);
484 screen_update_area();
487 static void screen_update_area()
493 area = g_new0(Rect, screen_num_desktops + 1);
495 dims = g_new(guint32, 4 * screen_num_desktops);
496 for (i = 0; i < screen_num_desktops + 1; ++i) {
497 Rect old_area = area[i];
500 // reset to the full areas
501 if (isXineramaActive())
502 xineramaUsableArea = getXineramaAreas();
506 RECT_SET(area[i], strut[i].left, strut[i].top,
507 screen_physical_size.width - (strut[i].left +
509 screen_physical_size.height - (strut[i].top +
514 if (isXineramaActive()) {
515 // keep each of the ximerama-defined areas inside the strut
516 RectList::iterator xit, xend = xineramaUsableArea.end();
517 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
518 if (xit->x() < usableArea.x()) {
519 xit->setX(usableArea.x());
520 xit->setWidth(xit->width() - usableArea.x());
522 if (xit->y() < usableArea.y()) {
523 xit->setY(usableArea.y());
524 xit->setHeight(xit->height() - usableArea.y());
526 if (xit->x() + xit->width() > usableArea.width())
527 xit->setWidth(usableArea.width() - xit->x());
528 if (xit->y() + xit->height() > usableArea.height())
529 xit->setHeight(usableArea.height() - xit->y());
534 if (!RECT_EQUAL(old_area, area[i])) {
535 /* the area has changed, adjust all the maximized windows */
537 for (it = client_list; it; it = it->next) {
538 Client *c = it->data;
539 if (i < screen_num_desktops) {
541 client_remaximize(c);
543 /* the 'all desktops' size */
544 if (c->desktop == DESKTOP_ALL)
545 client_remaximize(c);
550 /* don't set these for the 'all desktops' area */
551 if (i < screen_num_desktops) {
552 dims[(i * 4) + 0] = area[i].x;
553 dims[(i * 4) + 1] = area[i].y;
554 dims[(i * 4) + 2] = area[i].width;
555 dims[(i * 4) + 3] = area[i].height;
558 PROP_SETA32(ob_root, net_workarea, cardinal,
559 dims, 4 * screen_num_desktops);
563 Rect *screen_area(guint desktop)
565 if (desktop >= screen_num_desktops) {
566 if (desktop == DESKTOP_ALL)
567 return &area[screen_num_desktops];
570 return &area[desktop];
573 Strut *screen_strut(guint desktop)
575 if (desktop >= screen_num_desktops) {
576 if (desktop == DESKTOP_ALL)
577 return &strut[screen_num_desktops];
580 return &strut[desktop];