11 # include <sys/types.h>
15 /*! The event mask to grab on the root window */
16 #define ROOT_EVENTMASK (/*ColormapChangeMask |*/ PropertyChangeMask | \
17 EnterWindowMask | LeaveWindowMask | \
18 SubstructureNotifyMask | SubstructureRedirectMask | \
19 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
21 guint screen_num_desktops = 1;
22 guint screen_desktop = 0;
23 Size screen_physical_size;
24 gboolean screen_showing_desktop;
25 DesktopLayout screen_desktop_layout;
26 GPtrArray *screen_desktop_names;
28 static Rect *area = NULL;
29 static Strut *strut = NULL;
31 static void screen_update_area();
33 static gboolean running;
34 static int another_running(Display *d, XErrorEvent *e)
37 g_message("A window manager is already running on screen %d",
43 gboolean screen_annex()
52 old = XSetErrorHandler(another_running);
53 XSelectInput(ob_display, ob_root, ROOT_EVENTMASK);
54 XSync(ob_display, FALSE);
55 XSetErrorHandler(old);
59 g_message("Managing screen %d", ob_screen);
61 /* set the mouse cursor for the root window (the default cursor) */
62 XDefineCursor(ob_display, ob_root, ob_cursors.left_ptr);
64 /* set the OPENBOX_PID hint */
66 PROP_SET32(ob_root, openbox_pid, cardinal, pid);
68 /* create the netwm support window */
69 support = XCreateSimpleWindow(ob_display, ob_root, 0, 0, 1, 1, 0, 0, 0);
71 /* set supporting window */
72 PROP_SET32(ob_root, net_supporting_wm_check, window, support);
74 /* set properties on the supporting window */
75 PROP_SETS(support, net_wm_name, utf8, "Openbox");
76 PROP_SET32(support, net_supporting_wm_check, window, support);
78 /* set the _NET_SUPPORTED_ATOMS hint */
81 supported = g_new(Atom, num_support);
82 supported[i++] = prop_atoms.net_current_desktop;
83 supported[i++] = prop_atoms.net_number_of_desktops;
84 supported[i++] = prop_atoms.net_desktop_geometry;
85 supported[i++] = prop_atoms.net_desktop_viewport;
86 supported[i++] = prop_atoms.net_active_window;
87 supported[i++] = prop_atoms.net_workarea;
88 supported[i++] = prop_atoms.net_client_list;
89 supported[i++] = prop_atoms.net_client_list_stacking;
90 supported[i++] = prop_atoms.net_desktop_names;
91 supported[i++] = prop_atoms.net_close_window;
92 supported[i++] = prop_atoms.net_desktop_layout;
93 supported[i++] = prop_atoms.net_showing_desktop;
94 supported[i++] = prop_atoms.net_wm_name;
95 supported[i++] = prop_atoms.net_wm_visible_name;
96 supported[i++] = prop_atoms.net_wm_icon_name;
97 supported[i++] = prop_atoms.net_wm_visible_icon_name;
98 supported[i++] = prop_atoms.net_wm_desktop;
99 supported[i++] = prop_atoms.net_wm_strut;
100 supported[i++] = prop_atoms.net_wm_window_type;
101 supported[i++] = prop_atoms.net_wm_window_type_desktop;
102 supported[i++] = prop_atoms.net_wm_window_type_dock;
103 supported[i++] = prop_atoms.net_wm_window_type_toolbar;
104 supported[i++] = prop_atoms.net_wm_window_type_menu;
105 supported[i++] = prop_atoms.net_wm_window_type_utility;
106 supported[i++] = prop_atoms.net_wm_window_type_splash;
107 supported[i++] = prop_atoms.net_wm_window_type_dialog;
108 supported[i++] = prop_atoms.net_wm_window_type_normal;
109 supported[i++] = prop_atoms.net_wm_allowed_actions;
110 supported[i++] = prop_atoms.net_wm_action_move;
111 supported[i++] = prop_atoms.net_wm_action_resize;
112 supported[i++] = prop_atoms.net_wm_action_minimize;
113 supported[i++] = prop_atoms.net_wm_action_shade;
114 supported[i++] = prop_atoms.net_wm_action_maximize_horz;
115 supported[i++] = prop_atoms.net_wm_action_maximize_vert;
116 supported[i++] = prop_atoms.net_wm_action_fullscreen;
117 supported[i++] = prop_atoms.net_wm_action_change_desktop;
118 supported[i++] = prop_atoms.net_wm_action_close;
119 supported[i++] = prop_atoms.net_wm_state;
120 supported[i++] = prop_atoms.net_wm_state_modal;
121 supported[i++] = prop_atoms.net_wm_state_maximized_vert;
122 supported[i++] = prop_atoms.net_wm_state_maximized_horz;
123 supported[i++] = prop_atoms.net_wm_state_shaded;
124 supported[i++] = prop_atoms.net_wm_state_skip_taskbar;
125 supported[i++] = prop_atoms.net_wm_state_skip_pager;
126 supported[i++] = prop_atoms.net_wm_state_hidden;
127 supported[i++] = prop_atoms.net_wm_state_fullscreen;
128 supported[i++] = prop_atoms.net_wm_state_above;
129 supported[i++] = prop_atoms.net_wm_state_below;
130 g_assert(i == num_support);
132 supported[] = prop_atoms.net_wm_moveresize;
133 supported[] = prop_atoms.net_wm_moveresize_size_topleft;
134 supported[] = prop_atoms.net_wm_moveresize_size_topright;
135 supported[] = prop_atoms.net_wm_moveresize_size_bottomleft;
136 supported[] = prop_atoms.net_wm_moveresize_size_bottomright;
137 supported[] = prop_atoms.net_wm_moveresize_move;
138 supported[] = prop_atoms.net_wm_action_stick;
141 PROP_SET32A(ob_root, net_supported, atom, supported, num_support);
147 void screen_startup()
149 screen_desktop_names = g_ptr_array_new();
151 /* get the initial size */
154 screen_set_num_desktops(4);
155 screen_set_desktop(0);
157 /* don't start in showing-desktop mode */
158 screen_showing_desktop = FALSE;
159 PROP_SET32(ob_root, net_showing_desktop, cardinal, screen_showing_desktop);
161 screen_update_layout();
164 void screen_shutdown()
167 for (i = 0; i < screen_desktop_names->len; ++i)
168 g_free(g_ptr_array_index(screen_desktop_names, i));
169 g_ptr_array_free(screen_desktop_names, TRUE);
176 /* Set the _NET_DESKTOP_GEOMETRY hint */
177 /* XXX RandR support here? */
180 geometry[0] = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
181 geometry[1] = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
182 PROP_SET32A(ob_root, net_desktop_geometry, cardinal, geometry, 2);
183 screen_physical_size.width = geometry[0];
184 screen_physical_size.height = geometry[1];
186 if (ob_state == State_Starting)
189 screen_update_struts();
191 /* XXX adjust more stuff ? */
194 void screen_set_num_desktops(guint num)
196 unsigned long *viewport;
200 /* move windows on desktops that will no longer exist! */
202 std::list<Client*>::iterator it, end = clients.end();
203 for (it = clients.begin(); it != end; ++it) {
204 unsigned int d = (*it)->desktop();
205 if (d >= num && d != 0xffffffff) {
207 ce.xclient.type = ClientMessage;
208 ce.xclient.message_type = otk::Property::atoms.net_wm_desktop;
209 ce.xclient.display = **otk::display;
210 ce.xclient.window = (*it)->window();
211 ce.xclient.format = 32;
212 ce.xclient.data.l[0] = num - 1;
213 XSendEvent(**otk::display, _info->rootWindow(), false,
214 SubstructureNotifyMask | SubstructureRedirectMask, &ce);
219 screen_num_desktops = num;
220 PROP_SET32(ob_root, net_number_of_desktops, cardinal, num);
222 /* set the viewport hint */
223 viewport = g_new0(unsigned long, num * 2);
224 PROP_SET32A(ob_root, net_desktop_viewport, cardinal, viewport, num * 2);
227 /* change our struts/area to match */
228 screen_update_struts();
230 /* the number of rows/columns will differ */
231 screen_update_layout();
233 /* may be some unnamed desktops that we need to fill in with names */
234 screen_update_desktop_names();
236 /* change our desktop if we're on one that no longer exists! */
237 if (screen_desktop >= screen_num_desktops)
238 screen_set_desktop(num - 1);
241 void screen_set_desktop(guint num)
245 guint old = screen_desktop;
247 g_assert(num < screen_num_desktops);
249 g_message("Moving to desktop %u", num);
251 screen_desktop = num;
252 PROP_SET32(ob_root, net_current_desktop, cardinal, num);
254 if (old == num) return;
256 /* hide windows from bottom to top */
257 for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
258 Client *c = it->data;
259 if (c->frame->visible && !client_should_show(c))
260 engine_frame_hide(c->frame);
263 /* show windows from top to bottom */
264 for (it = stacking_list; it != NULL; it = it->next) {
265 Client *c = it->data;
266 if (!c->frame->visible && client_should_show(c))
267 engine_frame_show(c->frame);
270 /* force the callbacks to fire */
271 if (focus_client == NULL)
272 focus_set_client(NULL);
275 void screen_update_layout()
277 unsigned long *data = NULL;
280 screen_desktop_layout.orientation = prop_atoms.net_wm_orientation_horz;
281 screen_desktop_layout.start_corner = prop_atoms.net_wm_topleft;
282 screen_desktop_layout.rows = 1;
283 screen_desktop_layout.columns = screen_num_desktops;
285 if (PROP_GET32A(ob_root, net_desktop_layout, cardinal, data, 4)) {
286 if (data[0] == prop_atoms.net_wm_orientation_vert)
287 screen_desktop_layout.orientation = data[0];
288 if (data[3] == prop_atoms.net_wm_topright)
289 screen_desktop_layout.start_corner = data[3];
290 else if (data[3] == prop_atoms.net_wm_bottomright)
291 screen_desktop_layout.start_corner = data[3];
292 else if (data[3] == prop_atoms.net_wm_bottomleft)
293 screen_desktop_layout.start_corner = data[3];
295 /* fill in a zero rows/columns */
296 if (!(data[1] == 0 && data[2] == 0)) { /* both 0's is bad data.. */
298 data[1] = (screen_num_desktops +
299 screen_num_desktops % data[2]) / data[2];
300 } else if (data[2] == 0) {
301 data[2] = (screen_num_desktops +
302 screen_num_desktops % data[1]) / data[1];
304 screen_desktop_layout.columns = data[1];
305 screen_desktop_layout.rows = data[2];
308 /* bounds checking */
309 if (screen_desktop_layout.orientation ==
310 prop_atoms.net_wm_orientation_horz) {
311 if (screen_desktop_layout.rows > screen_num_desktops)
312 screen_desktop_layout.rows = screen_num_desktops;
313 if (screen_desktop_layout.columns > ((screen_num_desktops +
314 screen_num_desktops %
315 screen_desktop_layout.rows) /
316 screen_desktop_layout.rows))
317 screen_desktop_layout.columns =
318 (screen_num_desktops + screen_num_desktops %
319 screen_desktop_layout.rows) /
320 screen_desktop_layout.rows;
322 if (screen_desktop_layout.columns > screen_num_desktops)
323 screen_desktop_layout.columns = screen_num_desktops;
324 if (screen_desktop_layout.rows > ((screen_num_desktops +
325 screen_num_desktops %
326 screen_desktop_layout.columns) /
327 screen_desktop_layout.columns))
328 screen_desktop_layout.rows =
329 (screen_num_desktops + screen_num_desktops %
330 screen_desktop_layout.columns) /
331 screen_desktop_layout.columns;
337 void screen_update_desktop_names()
341 /* empty the array */
342 for (i = 0; i < screen_desktop_names->len; ++i)
343 g_free(g_ptr_array_index(screen_desktop_names, i));
344 g_ptr_array_set_size(screen_desktop_names, 0);
346 PROP_GETSA(ob_root, net_desktop_names, utf8, screen_desktop_names);
348 while (screen_desktop_names->len < screen_num_desktops)
349 g_ptr_array_add(screen_desktop_names, g_strdup("Unnamed Desktop"));
352 void screen_show_desktop(gboolean show)
355 static Window saved_focus = 0;
357 if (show == screen_showing_desktop) return; /* no change */
359 /* save the window focus, and restore it when leaving the show-desktop
361 if (show && focus_client)
362 saved_focus = focus_client->window;
364 screen_showing_desktop = show;
368 for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
369 Client *client = it->data;
370 if (client->type == Type_Desktop)
371 client_focus(client);
372 else if (client->frame->visible && !client_should_show(client))
373 engine_frame_hide(client->frame);
377 for (it = stacking_list; it != NULL; it = it->next) {
378 Client *client = it->data;
379 if (!client->frame->visible && client_should_show(client))
380 engine_frame_show(client->frame);
385 Client *f = focus_client;
386 if (!f || f->type == Type_Desktop) {
387 Client *c = g_hash_table_lookup(client_map,
388 (gpointer)saved_focus);
389 if (c) client_focus(c);
393 show = show ? 1 : 0; /* make it boolean */
394 PROP_SET32(ob_root, net_showing_desktop, cardinal, show);
397 void screen_install_colormap(Client *client, gboolean install)
399 if (client == NULL) {
400 /* XXX DONT USE THE DEFAULT SHIT HERE */
402 XInstallColormap(ob_display,
403 DefaultColormap(ob_display, ob_screen));
405 XUninstallColormap(ob_display,
406 DefaultColormap(ob_display, ob_screen));
408 XWindowAttributes wa;
409 if (XGetWindowAttributes(ob_display, client->window, &wa)) {
411 XInstallColormap(ob_display, wa.colormap);
413 XUninstallColormap(ob_display, wa.colormap);
418 void screen_update_struts()
425 strut = g_new0(Strut, screen_num_desktops + 1);
427 for (it = client_list; it; it = it->next) {
428 Client *c = it->data;
429 if (c->iconic) continue; /* these dont count in the strut */
431 if (c->desktop == 0xffffffff) {
432 for (i = 0; i < screen_num_desktops; ++i)
433 STRUT_ADD(strut[i], c->strut);
435 g_assert(c->desktop < screen_num_desktops);
436 STRUT_ADD(strut[c->desktop], c->strut);
438 /* apply to the 'all desktops' strut */
439 STRUT_ADD(strut[screen_num_desktops], c->strut);
441 screen_update_area();
444 static void screen_update_area()
451 area = g_new0(Rect, screen_num_desktops + 1);
453 dims = g_new(unsigned long, 4 * screen_num_desktops);
454 for (i = 0; i < screen_num_desktops + 1; ++i) {
455 Rect old_area = area[i];
458 // reset to the full areas
459 if (isXineramaActive())
460 xineramaUsableArea = getXineramaAreas();
464 RECT_SET(area[i], strut[i].left, strut[i].top,
465 screen_physical_size.width - (strut[i].left +
467 screen_physical_size.height - (strut[i].top +
472 if (isXineramaActive()) {
473 // keep each of the ximerama-defined areas inside the strut
474 RectList::iterator xit, xend = xineramaUsableArea.end();
475 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
476 if (xit->x() < usableArea.x()) {
477 xit->setX(usableArea.x());
478 xit->setWidth(xit->width() - usableArea.x());
480 if (xit->y() < usableArea.y()) {
481 xit->setY(usableArea.y());
482 xit->setHeight(xit->height() - usableArea.y());
484 if (xit->x() + xit->width() > usableArea.width())
485 xit->setWidth(usableArea.width() - xit->x());
486 if (xit->y() + xit->height() > usableArea.height())
487 xit->setHeight(usableArea.height() - xit->y());
492 if (!RECT_EQUAL(old_area, area[i])) {
493 /* the area has changed, adjust all the maximized windows */
495 for (it = client_list; it; it = it->next) {
496 Client *c = it->data;
497 if (i < screen_num_desktops) {
499 client_remaximize(c);
501 /* the 'all desktops' size */
502 if (c->desktop == DESKTOP_ALL)
503 client_remaximize(c);
508 /* don't set these for the 'all desktops' area */
509 if (i < screen_num_desktops) {
510 dims[(i * 4) + 0] = area[i].x;
511 dims[(i * 4) + 1] = area[i].y;
512 dims[(i * 4) + 2] = area[i].width;
513 dims[(i * 4) + 3] = area[i].height;
516 PROP_SET32A(ob_root, net_workarea, cardinal,
517 dims, 4 * screen_num_desktops);
521 Rect *screen_area(guint desktop)
523 if (desktop >= screen_num_desktops) {
524 if (desktop == DESKTOP_ALL)
525 return &area[screen_num_desktops];
528 return &area[desktop];
531 Strut *screen_strut(guint desktop)
533 if (desktop >= screen_num_desktops) {
534 if (desktop == DESKTOP_ALL)
535 return &strut[screen_num_desktops];
538 return &strut[desktop];