9 # include <sys/types.h>
13 /*! The event mask to grab on the root window */
14 #define ROOT_EVENTMASK (/*ColormapChangeMask |*/ PropertyChangeMask | \
15 EnterWindowMask | LeaveWindowMask | \
16 SubstructureNotifyMask | SubstructureRedirectMask | \
17 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
19 guint screen_num_desktops
= 1;
20 guint screen_desktop
= 0;
21 Size screen_physical_size
;
22 gboolean screen_showing_desktop
;
23 DesktopLayout screen_desktop_layout
;
24 GPtrArray
*screen_desktop_names
;
26 static Rect
*area
= NULL
;
27 static Strut
*strut
= NULL
;
29 static void screen_update_area();
31 static gboolean running
;
32 static int another_running(Display
*d
, XErrorEvent
*e
)
35 g_message("A window manager is already running on screen %d",
41 gboolean
screen_annex()
50 old
= XSetErrorHandler(another_running
);
51 XSelectInput(ob_display
, ob_root
, ROOT_EVENTMASK
);
52 XSync(ob_display
, FALSE
);
53 XSetErrorHandler(old
);
57 g_message("Managing screen %d", ob_screen
);
59 /* set the mouse cursor for the root window (the default cursor) */
60 XDefineCursor(ob_display
, ob_root
, ob_cursors
.left_ptr
);
62 /* set the OPENBOX_PID hint */
64 PROP_SET32(ob_root
, openbox_pid
, cardinal
, pid
);
66 /* create the netwm support window */
67 support
= XCreateSimpleWindow(ob_display
, ob_root
, 0, 0, 1, 1, 0, 0, 0);
69 /* set supporting window */
70 PROP_SET32(ob_root
, net_supporting_wm_check
, window
, support
);
72 /* set properties on the supporting window */
73 PROP_SETS(support
, net_wm_name
, utf8
, "Openbox");
74 PROP_SET32(support
, net_supporting_wm_check
, window
, support
);
76 /* set the _NET_SUPPORTED_ATOMS hint */
79 supported
= g_new(Atom
, num_support
);
80 supported
[i
++] = prop_atoms
.net_current_desktop
;
81 supported
[i
++] = prop_atoms
.net_number_of_desktops
;
82 supported
[i
++] = prop_atoms
.net_desktop_geometry
;
83 supported
[i
++] = prop_atoms
.net_desktop_viewport
;
84 supported
[i
++] = prop_atoms
.net_active_window
;
85 supported
[i
++] = prop_atoms
.net_workarea
;
86 supported
[i
++] = prop_atoms
.net_client_list
;
87 supported
[i
++] = prop_atoms
.net_client_list_stacking
;
88 supported
[i
++] = prop_atoms
.net_desktop_names
;
89 supported
[i
++] = prop_atoms
.net_close_window
;
90 supported
[i
++] = prop_atoms
.net_desktop_layout
;
91 supported
[i
++] = prop_atoms
.net_showing_desktop
;
92 supported
[i
++] = prop_atoms
.net_wm_name
;
93 supported
[i
++] = prop_atoms
.net_wm_visible_name
;
94 supported
[i
++] = prop_atoms
.net_wm_icon_name
;
95 supported
[i
++] = prop_atoms
.net_wm_visible_icon_name
;
96 supported
[i
++] = prop_atoms
.net_wm_desktop
;
97 supported
[i
++] = prop_atoms
.net_wm_strut
;
98 supported
[i
++] = prop_atoms
.net_wm_window_type
;
99 supported
[i
++] = prop_atoms
.net_wm_window_type_desktop
;
100 supported
[i
++] = prop_atoms
.net_wm_window_type_dock
;
101 supported
[i
++] = prop_atoms
.net_wm_window_type_toolbar
;
102 supported
[i
++] = prop_atoms
.net_wm_window_type_menu
;
103 supported
[i
++] = prop_atoms
.net_wm_window_type_utility
;
104 supported
[i
++] = prop_atoms
.net_wm_window_type_splash
;
105 supported
[i
++] = prop_atoms
.net_wm_window_type_dialog
;
106 supported
[i
++] = prop_atoms
.net_wm_window_type_normal
;
107 supported
[i
++] = prop_atoms
.net_wm_allowed_actions
;
108 supported
[i
++] = prop_atoms
.net_wm_action_move
;
109 supported
[i
++] = prop_atoms
.net_wm_action_resize
;
110 supported
[i
++] = prop_atoms
.net_wm_action_minimize
;
111 supported
[i
++] = prop_atoms
.net_wm_action_shade
;
112 supported
[i
++] = prop_atoms
.net_wm_action_maximize_horz
;
113 supported
[i
++] = prop_atoms
.net_wm_action_maximize_vert
;
114 supported
[i
++] = prop_atoms
.net_wm_action_fullscreen
;
115 supported
[i
++] = prop_atoms
.net_wm_action_change_desktop
;
116 supported
[i
++] = prop_atoms
.net_wm_action_close
;
117 supported
[i
++] = prop_atoms
.net_wm_state
;
118 supported
[i
++] = prop_atoms
.net_wm_state_modal
;
119 supported
[i
++] = prop_atoms
.net_wm_state_maximized_vert
;
120 supported
[i
++] = prop_atoms
.net_wm_state_maximized_horz
;
121 supported
[i
++] = prop_atoms
.net_wm_state_shaded
;
122 supported
[i
++] = prop_atoms
.net_wm_state_skip_taskbar
;
123 supported
[i
++] = prop_atoms
.net_wm_state_skip_pager
;
124 supported
[i
++] = prop_atoms
.net_wm_state_hidden
;
125 supported
[i
++] = prop_atoms
.net_wm_state_fullscreen
;
126 supported
[i
++] = prop_atoms
.net_wm_state_above
;
127 supported
[i
++] = prop_atoms
.net_wm_state_below
;
128 g_assert(i
== num_support
);
130 supported[] = prop_atoms.net_wm_moveresize;
131 supported[] = prop_atoms.net_wm_moveresize_size_topleft;
132 supported[] = prop_atoms.net_wm_moveresize_size_topright;
133 supported[] = prop_atoms.net_wm_moveresize_size_bottomleft;
134 supported[] = prop_atoms.net_wm_moveresize_size_bottomright;
135 supported[] = prop_atoms.net_wm_moveresize_move;
136 supported[] = prop_atoms.net_wm_action_stick;
139 PROP_SET32A(ob_root
, net_supported
, atom
, supported
, num_support
);
145 void screen_startup()
147 screen_desktop_names
= g_ptr_array_new();
149 /* get the initial size */
152 screen_set_num_desktops(4);
153 screen_set_desktop(0);
155 /* don't start in showing-desktop mode */
156 screen_showing_desktop
= FALSE
;
157 PROP_SET32(ob_root
, net_showing_desktop
, cardinal
, screen_showing_desktop
);
159 screen_update_layout();
162 void screen_shutdown()
165 for (i
= 0; i
< screen_desktop_names
->len
; ++i
)
166 g_free(g_ptr_array_index(screen_desktop_names
, i
));
167 g_ptr_array_free(screen_desktop_names
, TRUE
);
174 /* Set the _NET_DESKTOP_GEOMETRY hint */
175 /* XXX RandR support here? */
178 geometry
[0] = WidthOfScreen(ScreenOfDisplay(ob_display
, ob_screen
));
179 geometry
[1] = HeightOfScreen(ScreenOfDisplay(ob_display
, ob_screen
));
180 PROP_SET32A(ob_root
, net_desktop_geometry
, cardinal
, geometry
, 2);
181 screen_physical_size
.width
= geometry
[0];
182 screen_physical_size
.height
= geometry
[1];
184 if (ob_state
== State_Starting
)
187 screen_update_struts();
189 /* XXX adjust more stuff ? */
192 void screen_set_num_desktops(guint num
)
194 unsigned long *viewport
;
198 /* move windows on desktops that will no longer exist! */
200 std::list<Client*>::iterator it, end = clients.end();
201 for (it = clients.begin(); it != end; ++it) {
202 unsigned int d = (*it)->desktop();
203 if (d >= num && d != 0xffffffff) {
205 ce.xclient.type = ClientMessage;
206 ce.xclient.message_type = otk::Property::atoms.net_wm_desktop;
207 ce.xclient.display = **otk::display;
208 ce.xclient.window = (*it)->window();
209 ce.xclient.format = 32;
210 ce.xclient.data.l[0] = num - 1;
211 XSendEvent(**otk::display, _info->rootWindow(), false,
212 SubstructureNotifyMask | SubstructureRedirectMask, &ce);
217 screen_num_desktops
= num
;
218 PROP_SET32(ob_root
, net_number_of_desktops
, cardinal
, num
);
220 /* set the viewport hint */
221 viewport
= g_new0(unsigned long, num
* 2);
222 PROP_SET32A(ob_root
, net_desktop_viewport
, cardinal
, viewport
, num
* 2);
225 /* change our struts/area to match */
226 screen_update_struts();
228 /* the number of rows/columns will differ */
229 screen_update_layout();
231 /* may be some unnamed desktops that we need to fill in with names */
232 screen_update_desktop_names();
234 /* change our desktop if we're on one that no longer exists! */
235 if (screen_desktop
>= screen_num_desktops
)
236 screen_set_desktop(num
- 1);
239 void screen_set_desktop(guint num
)
241 guint old
= screen_desktop
;
243 g_assert(num
< screen_num_desktops
);
245 g_message("Moving to desktop %u", num
);
247 screen_desktop
= num
;
248 PROP_SET32(ob_root
, net_current_desktop
, cardinal
, num
);
250 if (old
== num
) return;
252 /* XXX show/hide all the clients
253 std::list<Client*>::iterator it, end = clients.end();
254 for (it = clients.begin(); it != end; ++it)
255 (*it)->showhide(); */
257 /* XXX force the callbacks to fire
258 if (!openbox->focusedClient())
259 openbox->setFocusedClient(0); */
262 void screen_update_layout()
264 unsigned long *data
= NULL
;
267 screen_desktop_layout
.orientation
= prop_atoms
.net_wm_orientation_horz
;
268 screen_desktop_layout
.start_corner
= prop_atoms
.net_wm_topleft
;
269 screen_desktop_layout
.rows
= 1;
270 screen_desktop_layout
.columns
= screen_num_desktops
;
272 if (PROP_GET32A(ob_root
, net_desktop_layout
, cardinal
, data
, 4)) {
273 if (data
[0] == prop_atoms
.net_wm_orientation_vert
)
274 screen_desktop_layout
.orientation
= data
[0];
275 if (data
[3] == prop_atoms
.net_wm_topright
)
276 screen_desktop_layout
.start_corner
= data
[3];
277 else if (data
[3] == prop_atoms
.net_wm_bottomright
)
278 screen_desktop_layout
.start_corner
= data
[3];
279 else if (data
[3] == prop_atoms
.net_wm_bottomleft
)
280 screen_desktop_layout
.start_corner
= data
[3];
282 /* fill in a zero rows/columns */
283 if (!(data
[1] == 0 && data
[2] == 0)) { /* both 0's is bad data.. */
285 data
[1] = (screen_num_desktops
+
286 screen_num_desktops
% data
[2]) / data
[2];
287 } else if (data
[2] == 0) {
288 data
[2] = (screen_num_desktops
+
289 screen_num_desktops
% data
[1]) / data
[1];
291 screen_desktop_layout
.columns
= data
[1];
292 screen_desktop_layout
.rows
= data
[2];
295 /* bounds checking */
296 if (screen_desktop_layout
.orientation
==
297 prop_atoms
.net_wm_orientation_horz
) {
298 if (screen_desktop_layout
.rows
> screen_num_desktops
)
299 screen_desktop_layout
.rows
= screen_num_desktops
;
300 if (screen_desktop_layout
.columns
> ((screen_num_desktops
+
301 screen_num_desktops
%
302 screen_desktop_layout
.rows
) /
303 screen_desktop_layout
.rows
))
304 screen_desktop_layout
.columns
=
305 (screen_num_desktops
+ screen_num_desktops
%
306 screen_desktop_layout
.rows
) /
307 screen_desktop_layout
.rows
;
309 if (screen_desktop_layout
.columns
> screen_num_desktops
)
310 screen_desktop_layout
.columns
= screen_num_desktops
;
311 if (screen_desktop_layout
.rows
> ((screen_num_desktops
+
312 screen_num_desktops
%
313 screen_desktop_layout
.columns
) /
314 screen_desktop_layout
.columns
))
315 screen_desktop_layout
.rows
=
316 (screen_num_desktops
+ screen_num_desktops
%
317 screen_desktop_layout
.columns
) /
318 screen_desktop_layout
.columns
;
324 void screen_update_desktop_names()
328 /* empty the array */
329 for (i
= 0; i
< screen_desktop_names
->len
; ++i
)
330 g_free(g_ptr_array_index(screen_desktop_names
, i
));
331 g_ptr_array_set_size(screen_desktop_names
, 0);
333 PROP_GETSA(ob_root
, net_desktop_names
, utf8
, screen_desktop_names
);
335 while (screen_desktop_names
->len
< screen_num_desktops
)
336 g_ptr_array_add(screen_desktop_names
, g_strdup("Unnamed Desktop"));
339 void screen_show_desktop(gboolean show
)
342 static Window saved_focus
= 0;
344 if (show
== screen_showing_desktop
) return; /* no change */
346 /* save the window focus, and restore it when leaving the show-desktop
348 if (show
&& focus_client
)
349 saved_focus
= focus_client
->window
;
351 screen_showing_desktop
= show
;
353 for (it
= client_list
; it
; it
= g_slist_next(it
)) {
354 Client
*client
= it
->data
;
355 if (client
->type
== Type_Desktop
) {
356 if (show
) client_focus(client
);
358 client_showhide(client
);
362 Client
*f
= focus_client
;
363 if (!f
|| f
->type
== Type_Desktop
) {
364 Client
*c
= g_hash_table_lookup(client_map
,
365 (gpointer
)saved_focus
);
366 if (c
) client_focus(c
);
370 show
= show
? 1 : 0; /* make it boolean */
371 PROP_SET32(ob_root
, net_showing_desktop
, cardinal
, show
);
374 void screen_install_colormap(Client
*client
, gboolean install
)
376 if (client
== NULL
) {
377 /* XXX DONT USE THE DEFAULT SHIT HERE */
379 XInstallColormap(ob_display
,
380 DefaultColormap(ob_display
, ob_screen
));
382 XUninstallColormap(ob_display
,
383 DefaultColormap(ob_display
, ob_screen
));
385 XWindowAttributes wa
;
386 if (XGetWindowAttributes(ob_display
, client
->window
, &wa
)) {
388 XInstallColormap(ob_display
, wa
.colormap
);
390 XUninstallColormap(ob_display
, wa
.colormap
);
395 void screen_update_struts()
402 strut
= g_new0(Strut
, screen_num_desktops
+ 1);
404 for (it
= client_list
; it
; it
= it
->next
) {
405 Client
*c
= it
->data
;
406 if (c
->iconic
) continue; /* these dont count in the strut */
408 if (c
->desktop
== 0xffffffff) {
409 for (i
= 0; i
< screen_num_desktops
; ++i
)
410 STRUT_ADD(strut
[i
], c
->strut
);
412 g_assert(c
->desktop
< screen_num_desktops
);
413 STRUT_ADD(strut
[c
->desktop
], c
->strut
);
415 /* apply to the 'all desktops' strut */
416 STRUT_ADD(strut
[screen_num_desktops
], c
->strut
);
418 screen_update_area();
421 static void screen_update_area()
428 area
= g_new0(Rect
, screen_num_desktops
+ 1);
430 dims
= g_new(unsigned long, 4 * screen_num_desktops
);
431 for (i
= 0; i
< screen_num_desktops
+ 1; ++i
) {
432 Rect old_area
= area
[i
];
435 // reset to the full areas
436 if (isXineramaActive())
437 xineramaUsableArea = getXineramaAreas();
441 RECT_SET(area
[i
], strut
[i
].left
, strut
[i
].top
,
442 screen_physical_size
.width
- (strut
[i
].left
+
444 screen_physical_size
.height
- (strut
[i
].top
+
449 if (isXineramaActive()) {
450 // keep each of the ximerama-defined areas inside the strut
451 RectList::iterator xit, xend = xineramaUsableArea.end();
452 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
453 if (xit->x() < usableArea.x()) {
454 xit->setX(usableArea.x());
455 xit->setWidth(xit->width() - usableArea.x());
457 if (xit->y() < usableArea.y()) {
458 xit->setY(usableArea.y());
459 xit->setHeight(xit->height() - usableArea.y());
461 if (xit->x() + xit->width() > usableArea.width())
462 xit->setWidth(usableArea.width() - xit->x());
463 if (xit->y() + xit->height() > usableArea.height())
464 xit->setHeight(usableArea.height() - xit->y());
469 if (!RECT_EQUAL(old_area
, area
[i
])) {
470 /* the area has changed, adjust all the maximized windows */
472 for (it
= client_list
; it
; it
= it
->next
) {
473 Client
*c
= it
->data
;
474 if (i
< screen_num_desktops
) {
476 client_remaximize(c
);
478 /* the 'all desktops' size */
479 if (c
->desktop
== DESKTOP_ALL
)
480 client_remaximize(c
);
485 /* don't set these for the 'all desktops' area */
486 if (i
< screen_num_desktops
) {
487 dims
[(i
* 4) + 0] = area
[i
].x
;
488 dims
[(i
* 4) + 1] = area
[i
].y
;
489 dims
[(i
* 4) + 2] = area
[i
].width
;
490 dims
[(i
* 4) + 3] = area
[i
].height
;
493 PROP_SET32A(ob_root
, net_workarea
, cardinal
,
494 dims
, 4 * screen_num_desktops
);
498 Rect
*screen_area(guint desktop
)
500 if (desktop
>= screen_num_desktops
) {
501 if (desktop
== DESKTOP_ALL
)
502 return &area
[screen_num_desktops
];
505 return &area
[desktop
];
508 Strut
*screen_strut(guint desktop
)
510 if (desktop
>= screen_num_desktops
) {
511 if (desktop
== DESKTOP_ALL
)
512 return &strut
[screen_num_desktops
];
515 return &strut
[desktop
];