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
)
243 guint old
= screen_desktop
;
245 g_assert(num
< screen_num_desktops
);
247 g_message("Moving to desktop %u", num
);
249 screen_desktop
= num
;
250 PROP_SET32(ob_root
, net_current_desktop
, cardinal
, num
);
252 if (old
== num
) return;
254 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
)
255 client_showhide(it
->data
, FALSE
);
257 /* force the callbacks to fire */
258 if (focus_client
== NULL
)
259 focus_set_client(NULL
);
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
;
355 for (it
= g_list_last(stacking_list
); it
!= NULL
; it
= it
->prev
) {
356 Client
*client
= it
->data
;
357 if (client
->type
== Type_Desktop
)
358 client_focus(client
);
360 client_showhide(client
, FALSE
);
364 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
) {
365 Client
*client
= it
->data
;
366 if (client
->type
!= Type_Desktop
)
367 client_showhide(client
, FALSE
);
372 Client
*f
= focus_client
;
373 if (!f
|| f
->type
== Type_Desktop
) {
374 Client
*c
= g_hash_table_lookup(client_map
,
375 (gpointer
)saved_focus
);
376 if (c
) client_focus(c
);
380 show
= show
? 1 : 0; /* make it boolean */
381 PROP_SET32(ob_root
, net_showing_desktop
, cardinal
, show
);
384 void screen_install_colormap(Client
*client
, gboolean install
)
386 if (client
== NULL
) {
387 /* XXX DONT USE THE DEFAULT SHIT HERE */
389 XInstallColormap(ob_display
,
390 DefaultColormap(ob_display
, ob_screen
));
392 XUninstallColormap(ob_display
,
393 DefaultColormap(ob_display
, ob_screen
));
395 XWindowAttributes wa
;
396 if (XGetWindowAttributes(ob_display
, client
->window
, &wa
)) {
398 XInstallColormap(ob_display
, wa
.colormap
);
400 XUninstallColormap(ob_display
, wa
.colormap
);
405 void screen_update_struts()
412 strut
= g_new0(Strut
, screen_num_desktops
+ 1);
414 for (it
= client_list
; it
; it
= it
->next
) {
415 Client
*c
= it
->data
;
416 if (c
->iconic
) continue; /* these dont count in the strut */
418 if (c
->desktop
== 0xffffffff) {
419 for (i
= 0; i
< screen_num_desktops
; ++i
)
420 STRUT_ADD(strut
[i
], c
->strut
);
422 g_assert(c
->desktop
< screen_num_desktops
);
423 STRUT_ADD(strut
[c
->desktop
], c
->strut
);
425 /* apply to the 'all desktops' strut */
426 STRUT_ADD(strut
[screen_num_desktops
], c
->strut
);
428 screen_update_area();
431 static void screen_update_area()
438 area
= g_new0(Rect
, screen_num_desktops
+ 1);
440 dims
= g_new(unsigned long, 4 * screen_num_desktops
);
441 for (i
= 0; i
< screen_num_desktops
+ 1; ++i
) {
442 Rect old_area
= area
[i
];
445 // reset to the full areas
446 if (isXineramaActive())
447 xineramaUsableArea = getXineramaAreas();
451 RECT_SET(area
[i
], strut
[i
].left
, strut
[i
].top
,
452 screen_physical_size
.width
- (strut
[i
].left
+
454 screen_physical_size
.height
- (strut
[i
].top
+
459 if (isXineramaActive()) {
460 // keep each of the ximerama-defined areas inside the strut
461 RectList::iterator xit, xend = xineramaUsableArea.end();
462 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
463 if (xit->x() < usableArea.x()) {
464 xit->setX(usableArea.x());
465 xit->setWidth(xit->width() - usableArea.x());
467 if (xit->y() < usableArea.y()) {
468 xit->setY(usableArea.y());
469 xit->setHeight(xit->height() - usableArea.y());
471 if (xit->x() + xit->width() > usableArea.width())
472 xit->setWidth(usableArea.width() - xit->x());
473 if (xit->y() + xit->height() > usableArea.height())
474 xit->setHeight(usableArea.height() - xit->y());
479 if (!RECT_EQUAL(old_area
, area
[i
])) {
480 /* the area has changed, adjust all the maximized windows */
482 for (it
= client_list
; it
; it
= it
->next
) {
483 Client
*c
= it
->data
;
484 if (i
< screen_num_desktops
) {
486 client_remaximize(c
);
488 /* the 'all desktops' size */
489 if (c
->desktop
== DESKTOP_ALL
)
490 client_remaximize(c
);
495 /* don't set these for the 'all desktops' area */
496 if (i
< screen_num_desktops
) {
497 dims
[(i
* 4) + 0] = area
[i
].x
;
498 dims
[(i
* 4) + 1] = area
[i
].y
;
499 dims
[(i
* 4) + 2] = area
[i
].width
;
500 dims
[(i
* 4) + 3] = area
[i
].height
;
503 PROP_SET32A(ob_root
, net_workarea
, cardinal
,
504 dims
, 4 * screen_num_desktops
);
508 Rect
*screen_area(guint desktop
)
510 if (desktop
>= screen_num_desktops
) {
511 if (desktop
== DESKTOP_ALL
)
512 return &area
[screen_num_desktops
];
515 return &area
[desktop
];
518 Strut
*screen_strut(guint desktop
)
520 if (desktop
>= screen_num_desktops
) {
521 if (desktop
== DESKTOP_ALL
)
522 return &strut
[screen_num_desktops
];
525 return &strut
[desktop
];