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 GPtrArray
*screen_desktop_names
;
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
.left_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
, utf8
, "Openbox");
78 PROP_SET32(support_window
, net_supporting_wm_check
, window
,support_window
);
80 /* set the _NET_SUPPORTED_ATOMS hint */
83 supported
= g_new(Atom
, 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 g_assert(i
== num_support
);
134 supported[] = prop_atoms.net_wm_moveresize;
135 supported[] = prop_atoms.net_wm_moveresize_size_topleft;
136 supported[] = prop_atoms.net_wm_moveresize_size_topright;
137 supported[] = prop_atoms.net_wm_moveresize_size_bottomleft;
138 supported[] = prop_atoms.net_wm_moveresize_size_bottomright;
139 supported[] = prop_atoms.net_wm_moveresize_move;
140 supported[] = prop_atoms.net_wm_action_stick;
143 PROP_SET32A(ob_root
, net_supported
, atom
, supported
, num_support
);
149 void screen_startup()
151 screen_desktop_names
= g_ptr_array_new();
153 /* get the initial size */
156 screen_set_num_desktops(4);
157 screen_set_desktop(0);
159 /* don't start in showing-desktop mode */
160 screen_showing_desktop
= FALSE
;
161 PROP_SET32(ob_root
, net_showing_desktop
, cardinal
, screen_showing_desktop
);
163 screen_update_layout();
166 void screen_shutdown()
170 XSelectInput(ob_display
, ob_root
, NoEventMask
);
172 PROP_ERASE(ob_root
, openbox_pid
); /* we're not running here no more! */
173 PROP_ERASE(ob_root
, net_supported
); /* not without us */
174 PROP_ERASE(ob_root
, net_showing_desktop
); /* don't keep this mode */
176 XDestroyWindow(ob_display
, support_window
);
178 for (i
= 0; i
< screen_desktop_names
->len
; ++i
)
179 g_free(g_ptr_array_index(screen_desktop_names
, i
));
180 g_ptr_array_free(screen_desktop_names
, TRUE
);
187 /* XXX RandR support here? */
190 /* Set the _NET_DESKTOP_GEOMETRY hint */
191 geometry
[0] = WidthOfScreen(ScreenOfDisplay(ob_display
, ob_screen
));
192 geometry
[1] = HeightOfScreen(ScreenOfDisplay(ob_display
, ob_screen
));
193 PROP_SET32A(ob_root
, net_desktop_geometry
, cardinal
, geometry
, 2);
194 screen_physical_size
.width
= geometry
[0];
195 screen_physical_size
.height
= geometry
[1];
197 if (ob_state
== State_Starting
)
200 screen_update_struts();
202 /* XXX adjust more stuff ? */
205 void screen_set_num_desktops(guint num
)
212 /* move windows on desktops that will no longer exist! */
214 std::list<Client*>::iterator it, end = clients.end();
215 for (it = clients.begin(); it != end; ++it) {
216 unsigned int d = (*it)->desktop();
217 if (d >= num && d != 0xffffffff) {
219 ce.xclient.type = ClientMessage;
220 ce.xclient.message_type = otk::Property::atoms.net_wm_desktop;
221 ce.xclient.display = **otk::display;
222 ce.xclient.window = (*it)->window();
223 ce.xclient.format = 32;
224 ce.xclient.data.l[0] = num - 1;
225 XSendEvent(**otk::display, _info->rootWindow(), false,
226 SubstructureNotifyMask | SubstructureRedirectMask, &ce);
231 old
= screen_num_desktops
;
232 screen_num_desktops
= num
;
233 PROP_SET32(ob_root
, net_number_of_desktops
, cardinal
, num
);
235 /* set the viewport hint */
236 viewport
= g_new0(gulong
, num
* 2);
237 PROP_SET32A(ob_root
, net_desktop_viewport
, cardinal
, viewport
, num
* 2);
240 /* change our struts/area to match */
241 screen_update_struts();
243 /* the number of rows/columns will differ */
244 screen_update_layout();
246 /* may be some unnamed desktops that we need to fill in with names */
247 screen_update_desktop_names();
249 /* update the focus lists */
250 /* free our lists for the desktops which have disappeared */
251 for (i
= num
; i
< old
; ++i
)
252 g_list_free(focus_order
[i
]);
253 /* realloc the array */
254 focus_order
= g_renew(GList
*, focus_order
, num
);
255 /* set the new lists to be empty */
256 for (i
= old
; i
< num
; ++i
)
257 focus_order
[i
] = NULL
;
259 dispatch_ob(Event_Ob_NumDesktops
, num
, old
);
261 /* change our desktop if we're on one that no longer exists! */
262 if (screen_desktop
>= screen_num_desktops
)
263 screen_set_desktop(num
- 1);
266 void screen_set_desktop(guint num
)
271 g_assert(num
< screen_num_desktops
);
273 g_message("Moving to desktop %u", num
);
275 old
= screen_desktop
;
276 screen_desktop
= num
;
277 PROP_SET32(ob_root
, net_current_desktop
, cardinal
, num
);
279 if (old
== num
) return;
281 /* show windows before hiding the rest to lessen the enter/leave events */
283 /* show windows from top to bottom */
284 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
) {
285 Client
*c
= it
->data
;
286 if (!c
->frame
->visible
&& client_should_show(c
))
287 engine_frame_show(c
->frame
);
290 /* hide windows from bottom to top */
291 for (it
= g_list_last(stacking_list
); it
!= NULL
; it
= it
->prev
) {
292 Client
*c
= it
->data
;
293 if (c
->frame
->visible
&& !client_should_show(c
))
294 engine_frame_hide(c
->frame
);
297 dispatch_ob(Event_Ob_Desktop
, num
, old
);
300 void screen_update_layout()
302 unsigned long *data
= NULL
;
305 screen_desktop_layout
.orientation
= prop_atoms
.net_wm_orientation_horz
;
306 screen_desktop_layout
.start_corner
= prop_atoms
.net_wm_topleft
;
307 screen_desktop_layout
.rows
= 1;
308 screen_desktop_layout
.columns
= screen_num_desktops
;
310 if (PROP_GET32A(ob_root
, net_desktop_layout
, cardinal
, data
, 4)) {
311 if (data
[0] == prop_atoms
.net_wm_orientation_vert
)
312 screen_desktop_layout
.orientation
= data
[0];
313 if (data
[3] == prop_atoms
.net_wm_topright
)
314 screen_desktop_layout
.start_corner
= data
[3];
315 else if (data
[3] == prop_atoms
.net_wm_bottomright
)
316 screen_desktop_layout
.start_corner
= data
[3];
317 else if (data
[3] == prop_atoms
.net_wm_bottomleft
)
318 screen_desktop_layout
.start_corner
= data
[3];
320 /* fill in a zero rows/columns */
321 if (!(data
[1] == 0 && data
[2] == 0)) { /* both 0's is bad data.. */
323 data
[1] = (screen_num_desktops
+
324 screen_num_desktops
% data
[2]) / data
[2];
325 } else if (data
[2] == 0) {
326 data
[2] = (screen_num_desktops
+
327 screen_num_desktops
% data
[1]) / data
[1];
329 screen_desktop_layout
.columns
= data
[1];
330 screen_desktop_layout
.rows
= data
[2];
333 /* bounds checking */
334 if (screen_desktop_layout
.orientation
==
335 prop_atoms
.net_wm_orientation_horz
) {
336 if (screen_desktop_layout
.rows
> screen_num_desktops
)
337 screen_desktop_layout
.rows
= screen_num_desktops
;
338 if (screen_desktop_layout
.columns
> ((screen_num_desktops
+
339 screen_num_desktops
%
340 screen_desktop_layout
.rows
) /
341 screen_desktop_layout
.rows
))
342 screen_desktop_layout
.columns
=
343 (screen_num_desktops
+ screen_num_desktops
%
344 screen_desktop_layout
.rows
) /
345 screen_desktop_layout
.rows
;
347 if (screen_desktop_layout
.columns
> screen_num_desktops
)
348 screen_desktop_layout
.columns
= screen_num_desktops
;
349 if (screen_desktop_layout
.rows
> ((screen_num_desktops
+
350 screen_num_desktops
%
351 screen_desktop_layout
.columns
) /
352 screen_desktop_layout
.columns
))
353 screen_desktop_layout
.rows
=
354 (screen_num_desktops
+ screen_num_desktops
%
355 screen_desktop_layout
.columns
) /
356 screen_desktop_layout
.columns
;
362 void screen_update_desktop_names()
366 /* empty the array */
367 for (i
= 0; i
< screen_desktop_names
->len
; ++i
)
368 g_free(g_ptr_array_index(screen_desktop_names
, i
));
369 g_ptr_array_set_size(screen_desktop_names
, 0);
371 PROP_GETSA(ob_root
, net_desktop_names
, utf8
, screen_desktop_names
);
373 while (screen_desktop_names
->len
< screen_num_desktops
)
374 g_ptr_array_add(screen_desktop_names
, g_strdup("Unnamed Desktop"));
377 void screen_show_desktop(gboolean show
)
380 static Window saved_focus
= 0;
382 if (show
== screen_showing_desktop
) return; /* no change */
384 /* save the window focus, and restore it when leaving the show-desktop
386 if (show
&& focus_client
)
387 saved_focus
= focus_client
->window
;
389 screen_showing_desktop
= show
;
393 for (it
= g_list_last(stacking_list
); it
!= NULL
; it
= it
->prev
) {
394 Client
*client
= it
->data
;
395 if (client
->type
== Type_Desktop
)
396 client_focus(client
);
397 else if (client
->frame
->visible
&& !client_should_show(client
))
398 engine_frame_hide(client
->frame
);
402 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
) {
403 Client
*client
= it
->data
;
404 if (!client
->frame
->visible
&& client_should_show(client
))
405 engine_frame_show(client
->frame
);
410 Client
*f
= focus_client
;
411 if (!f
|| f
->type
== Type_Desktop
) {
412 Client
*c
= g_hash_table_lookup(client_map
,
413 (gpointer
)saved_focus
);
414 if (c
) client_focus(c
);
418 show
= !!show
; /* make it boolean */
419 PROP_SET32(ob_root
, net_showing_desktop
, cardinal
, show
);
421 dispatch_ob(Event_Ob_ShowDesktop
, show
, 0);
424 void screen_install_colormap(Client
*client
, gboolean install
)
426 if (client
== NULL
) {
428 XInstallColormap(ob_display
, render_colormap
);
430 XUninstallColormap(ob_display
, render_colormap
);
432 XWindowAttributes wa
;
433 if (XGetWindowAttributes(ob_display
, client
->window
, &wa
) &&
434 wa
.colormap
!= None
) {
436 XInstallColormap(ob_display
, wa
.colormap
);
438 XUninstallColormap(ob_display
, wa
.colormap
);
443 void screen_update_struts()
450 strut
= g_new0(Strut
, screen_num_desktops
+ 1);
452 for (it
= client_list
; it
; it
= it
->next
) {
453 Client
*c
= it
->data
;
454 if (c
->iconic
) continue; /* these dont count in the strut */
456 if (c
->desktop
== 0xffffffff) {
457 for (i
= 0; i
< screen_num_desktops
; ++i
)
458 STRUT_ADD(strut
[i
], c
->strut
);
460 g_assert(c
->desktop
< screen_num_desktops
);
461 STRUT_ADD(strut
[c
->desktop
], c
->strut
);
463 /* apply to the 'all desktops' strut */
464 STRUT_ADD(strut
[screen_num_desktops
], c
->strut
);
466 screen_update_area();
469 static void screen_update_area()
476 area
= g_new0(Rect
, screen_num_desktops
+ 1);
478 dims
= g_new(unsigned long, 4 * screen_num_desktops
);
479 for (i
= 0; i
< screen_num_desktops
+ 1; ++i
) {
480 Rect old_area
= area
[i
];
483 // reset to the full areas
484 if (isXineramaActive())
485 xineramaUsableArea = getXineramaAreas();
489 RECT_SET(area
[i
], strut
[i
].left
, strut
[i
].top
,
490 screen_physical_size
.width
- (strut
[i
].left
+
492 screen_physical_size
.height
- (strut
[i
].top
+
497 if (isXineramaActive()) {
498 // keep each of the ximerama-defined areas inside the strut
499 RectList::iterator xit, xend = xineramaUsableArea.end();
500 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
501 if (xit->x() < usableArea.x()) {
502 xit->setX(usableArea.x());
503 xit->setWidth(xit->width() - usableArea.x());
505 if (xit->y() < usableArea.y()) {
506 xit->setY(usableArea.y());
507 xit->setHeight(xit->height() - usableArea.y());
509 if (xit->x() + xit->width() > usableArea.width())
510 xit->setWidth(usableArea.width() - xit->x());
511 if (xit->y() + xit->height() > usableArea.height())
512 xit->setHeight(usableArea.height() - xit->y());
517 if (!RECT_EQUAL(old_area
, area
[i
])) {
518 /* the area has changed, adjust all the maximized windows */
520 for (it
= client_list
; it
; it
= it
->next
) {
521 Client
*c
= it
->data
;
522 if (i
< screen_num_desktops
) {
524 client_remaximize(c
);
526 /* the 'all desktops' size */
527 if (c
->desktop
== DESKTOP_ALL
)
528 client_remaximize(c
);
533 /* don't set these for the 'all desktops' area */
534 if (i
< screen_num_desktops
) {
535 dims
[(i
* 4) + 0] = area
[i
].x
;
536 dims
[(i
* 4) + 1] = area
[i
].y
;
537 dims
[(i
* 4) + 2] = area
[i
].width
;
538 dims
[(i
* 4) + 3] = area
[i
].height
;
541 PROP_SET32A(ob_root
, net_workarea
, cardinal
,
542 dims
, 4 * screen_num_desktops
);
546 Rect
*screen_area(guint desktop
)
548 if (desktop
>= screen_num_desktops
) {
549 if (desktop
== DESKTOP_ALL
)
550 return &area
[screen_num_desktops
];
553 return &area
[desktop
];
556 Strut
*screen_strut(guint desktop
)
558 if (desktop
>= screen_num_desktops
) {
559 if (desktop
== DESKTOP_ALL
)
560 return &strut
[screen_num_desktops
];
563 return &strut
[desktop
];