11 #include "extensions.h"
12 #include "../render/render.h"
16 # include <sys/types.h>
20 /*! The event mask to grab on the root window */
21 #define ROOT_EVENTMASK (StructureNotifyMask | PropertyChangeMask | \
22 EnterWindowMask | LeaveWindowMask | \
23 SubstructureNotifyMask | SubstructureRedirectMask | \
24 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
26 guint screen_num_desktops
= 0;
27 guint screen_desktop
= 0;
28 Size screen_physical_size
;
29 gboolean screen_showing_desktop
;
30 DesktopLayout screen_desktop_layout
;
31 char **screen_desktop_names
= NULL
;
33 static Rect
*area
= NULL
;
34 static Strut
*strut
= NULL
;
35 static Window support_window
= None
;
37 static void screen_update_area();
39 static gboolean running
;
40 static int another_running(Display
*d
, XErrorEvent
*e
)
43 g_message("A window manager is already running on screen %d",
49 gboolean
screen_annex()
57 old
= XSetErrorHandler(another_running
);
58 XSelectInput(ob_display
, ob_root
, ROOT_EVENTMASK
);
59 XSync(ob_display
, FALSE
);
60 XSetErrorHandler(old
);
64 g_message("Managing screen %d", ob_screen
);
66 /* set the mouse cursor for the root window (the default cursor) */
67 XDefineCursor(ob_display
, ob_root
, ob_cursors
.ptr
);
69 /* set the OPENBOX_PID hint */
71 PROP_SET32(ob_root
, openbox_pid
, cardinal
, pid
);
73 /* create the netwm support window */
74 support_window
= XCreateSimpleWindow(ob_display
, ob_root
, 0,0,1,1,0,0,0);
76 /* set supporting window */
77 PROP_SET32(ob_root
, net_supporting_wm_check
, window
, support_window
);
79 /* set properties on the supporting window */
80 PROP_SETS(support_window
, net_wm_name
, "Openbox");
81 PROP_SET32(support_window
, net_supporting_wm_check
, window
,support_window
);
83 /* set the _NET_SUPPORTED_ATOMS hint */
86 supported
= g_new(guint32
, num_support
);
87 supported
[i
++] = prop_atoms
.net_current_desktop
;
88 supported
[i
++] = prop_atoms
.net_number_of_desktops
;
89 supported
[i
++] = prop_atoms
.net_desktop_geometry
;
90 supported
[i
++] = prop_atoms
.net_desktop_viewport
;
91 supported
[i
++] = prop_atoms
.net_active_window
;
92 supported
[i
++] = prop_atoms
.net_workarea
;
93 supported
[i
++] = prop_atoms
.net_client_list
;
94 supported
[i
++] = prop_atoms
.net_client_list_stacking
;
95 supported
[i
++] = prop_atoms
.net_desktop_names
;
96 supported
[i
++] = prop_atoms
.net_close_window
;
97 supported
[i
++] = prop_atoms
.net_desktop_layout
;
98 supported
[i
++] = prop_atoms
.net_showing_desktop
;
99 supported
[i
++] = prop_atoms
.net_wm_name
;
100 supported
[i
++] = prop_atoms
.net_wm_visible_name
;
101 supported
[i
++] = prop_atoms
.net_wm_icon_name
;
102 supported
[i
++] = prop_atoms
.net_wm_visible_icon_name
;
103 supported
[i
++] = prop_atoms
.net_wm_desktop
;
104 supported
[i
++] = prop_atoms
.net_wm_strut
;
105 supported
[i
++] = prop_atoms
.net_wm_window_type
;
106 supported
[i
++] = prop_atoms
.net_wm_window_type_desktop
;
107 supported
[i
++] = prop_atoms
.net_wm_window_type_dock
;
108 supported
[i
++] = prop_atoms
.net_wm_window_type_toolbar
;
109 supported
[i
++] = prop_atoms
.net_wm_window_type_menu
;
110 supported
[i
++] = prop_atoms
.net_wm_window_type_utility
;
111 supported
[i
++] = prop_atoms
.net_wm_window_type_splash
;
112 supported
[i
++] = prop_atoms
.net_wm_window_type_dialog
;
113 supported
[i
++] = prop_atoms
.net_wm_window_type_normal
;
114 supported
[i
++] = prop_atoms
.net_wm_allowed_actions
;
115 supported
[i
++] = prop_atoms
.net_wm_action_move
;
116 supported
[i
++] = prop_atoms
.net_wm_action_resize
;
117 supported
[i
++] = prop_atoms
.net_wm_action_minimize
;
118 supported
[i
++] = prop_atoms
.net_wm_action_shade
;
119 supported
[i
++] = prop_atoms
.net_wm_action_maximize_horz
;
120 supported
[i
++] = prop_atoms
.net_wm_action_maximize_vert
;
121 supported
[i
++] = prop_atoms
.net_wm_action_fullscreen
;
122 supported
[i
++] = prop_atoms
.net_wm_action_change_desktop
;
123 supported
[i
++] = prop_atoms
.net_wm_action_close
;
124 supported
[i
++] = prop_atoms
.net_wm_state
;
125 supported
[i
++] = prop_atoms
.net_wm_state_modal
;
126 supported
[i
++] = prop_atoms
.net_wm_state_maximized_vert
;
127 supported
[i
++] = prop_atoms
.net_wm_state_maximized_horz
;
128 supported
[i
++] = prop_atoms
.net_wm_state_shaded
;
129 supported
[i
++] = prop_atoms
.net_wm_state_skip_taskbar
;
130 supported
[i
++] = prop_atoms
.net_wm_state_skip_pager
;
131 supported
[i
++] = prop_atoms
.net_wm_state_hidden
;
132 supported
[i
++] = prop_atoms
.net_wm_state_fullscreen
;
133 supported
[i
++] = prop_atoms
.net_wm_state_above
;
134 supported
[i
++] = prop_atoms
.net_wm_state_below
;
135 supported
[i
++] = prop_atoms
.net_moveresize_window
;
136 supported
[i
++] = prop_atoms
.net_wm_moveresize
;
137 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_topleft
;
138 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_top
;
139 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_topright
;
140 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_right
;
141 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_bottomright
;
142 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_bottom
;
143 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_bottomleft
;
144 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_left
;
145 supported
[i
++] = prop_atoms
.net_wm_moveresize_move
;
146 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_keyboard
;
147 supported
[i
++] = prop_atoms
.net_wm_moveresize_move_keyboard
;
148 g_assert(i
== num_support
);
150 supported[] = prop_atoms.net_wm_action_stick;
153 PROP_SETA32(ob_root
, net_supported
, atom
, supported
, num_support
);
159 void screen_startup()
164 /* get the initial size */
165 screen_resize(WidthOfScreen(ScreenOfDisplay(ob_display
, ob_screen
)),
166 HeightOfScreen(ScreenOfDisplay(ob_display
, ob_screen
)));
169 screen_desktop_names
= g_new(char*,
170 g_slist_length(config_desktops_names
) + 1);
171 for (i
= 0, it
= config_desktops_names
; it
; ++i
, it
= it
->next
)
172 screen_desktop_names
[i
] = it
->data
; /* dont strdup */
173 screen_desktop_names
[i
] = NULL
;
174 PROP_SETSS(ob_root
, net_desktop_names
, screen_desktop_names
);
175 g_free(screen_desktop_names
); /* dont free the individual strings */
176 screen_desktop_names
= NULL
;
178 screen_num_desktops
= 0;
179 screen_set_num_desktops(config_desktops_num
);
180 if (startup_desktop
>= screen_num_desktops
)
182 screen_desktop
= startup_desktop
;
183 screen_set_desktop(startup_desktop
);
185 /* don't start in showing-desktop mode */
186 screen_showing_desktop
= FALSE
;
187 PROP_SET32(ob_root
, net_showing_desktop
, cardinal
, screen_showing_desktop
);
189 screen_update_layout();
192 void screen_shutdown()
194 XSelectInput(ob_display
, ob_root
, NoEventMask
);
196 PROP_ERASE(ob_root
, openbox_pid
); /* we're not running here no more! */
197 PROP_ERASE(ob_root
, net_supported
); /* not without us */
198 PROP_ERASE(ob_root
, net_showing_desktop
); /* don't keep this mode */
200 XDestroyWindow(ob_display
, support_window
);
202 g_strfreev(screen_desktop_names
);
207 void screen_resize(int w
, int h
)
212 /* Set the _NET_DESKTOP_GEOMETRY hint */
215 PROP_SETA32(ob_root
, net_desktop_geometry
, cardinal
, geometry
, 2);
216 screen_physical_size
.width
= geometry
[0];
217 screen_physical_size
.height
= geometry
[1];
219 if (ob_state
== State_Starting
)
223 screen_update_struts();
225 for (it
= client_list
; it
; it
= it
->next
)
226 client_move_onscreen(it
->data
);
229 void screen_set_num_desktops(guint num
)
237 old
= screen_num_desktops
;
238 screen_num_desktops
= num
;
239 PROP_SET32(ob_root
, net_number_of_desktops
, cardinal
, num
);
241 /* set the viewport hint */
242 viewport
= g_new0(guint32
, num
* 2);
243 PROP_SETA32(ob_root
, net_desktop_viewport
, cardinal
, viewport
, num
* 2);
246 /* the number of rows/columns will differ */
247 screen_update_layout();
249 /* may be some unnamed desktops that we need to fill in with names */
250 screen_update_desktop_names();
252 /* update the focus lists */
253 /* free our lists for the desktops which have disappeared */
254 for (i
= num
; i
< old
; ++i
)
255 g_list_free(focus_order
[i
]);
256 /* realloc the array */
257 focus_order
= g_renew(GList
*, focus_order
, num
);
258 /* set the new lists to be empty */
259 for (i
= old
; i
< num
; ++i
)
260 focus_order
[i
] = NULL
;
262 /* move windows on desktops that will no longer exist! */
263 for (it
= client_list
; it
!= NULL
; it
= it
->next
) {
264 Client
*c
= it
->data
;
265 if (c
->desktop
>= num
&& c
->desktop
!= DESKTOP_ALL
)
266 client_set_desktop(c
, num
- 1, FALSE
);
269 /* change our struts/area to match (after moving windows) */
270 screen_update_struts();
272 dispatch_ob(Event_Ob_NumDesktops
, num
, old
);
274 /* change our desktop if we're on one that no longer exists! */
275 if (screen_desktop
>= screen_num_desktops
)
276 screen_set_desktop(num
- 1);
279 void screen_set_desktop(guint num
)
285 g_assert(num
< screen_num_desktops
);
287 old
= screen_desktop
;
288 screen_desktop
= num
;
289 PROP_SET32(ob_root
, net_current_desktop
, cardinal
, num
);
291 if (old
== num
) return;
293 g_message("Moving to desktop %d", num
+1);
295 /* show windows before hiding the rest to lessen the enter/leave events */
297 /* show windows from top to bottom */
298 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
) {
299 if (WINDOW_IS_CLIENT(it
->data
)) {
300 Client
*c
= it
->data
;
301 if (!c
->frame
->visible
&& client_should_show(c
))
302 frame_show(c
->frame
);
306 /* hide windows from bottom to top */
307 for (it
= g_list_last(stacking_list
); it
!= NULL
; it
= it
->prev
) {
308 if (WINDOW_IS_CLIENT(it
->data
)) {
309 Client
*c
= it
->data
;
310 if (c
->frame
->visible
&& !client_should_show(c
))
311 frame_hide(c
->frame
);
315 /* focus the last focused window on the desktop, and ignore enter events
316 from the switch so it doesnt mess with the focus */
317 while (XCheckTypedEvent(ob_display
, EnterNotify
, &e
));
318 focus_fallback(Fallback_Desktop
);
320 dispatch_ob(Event_Ob_Desktop
, num
, old
);
323 void screen_update_layout()
325 guint32
*data
= NULL
;
329 screen_desktop_layout
.orientation
= prop_atoms
.net_wm_orientation_horz
;
330 screen_desktop_layout
.start_corner
= prop_atoms
.net_wm_topleft
;
331 screen_desktop_layout
.rows
= 1;
332 screen_desktop_layout
.columns
= screen_num_desktops
;
334 if (PROP_GETA32(ob_root
, net_desktop_layout
, cardinal
, &data
, &num
)) {
335 if (num
== 3 || num
== 4) {
336 if (data
[0] == prop_atoms
.net_wm_orientation_vert
)
337 screen_desktop_layout
.orientation
= data
[0];
339 screen_desktop_layout
.start_corner
=
340 prop_atoms
.net_wm_topright
;
342 if (data
[3] == prop_atoms
.net_wm_topright
)
343 screen_desktop_layout
.start_corner
= data
[3];
344 else if (data
[3] == prop_atoms
.net_wm_bottomright
)
345 screen_desktop_layout
.start_corner
= data
[3];
346 else if (data
[3] == prop_atoms
.net_wm_bottomleft
)
347 screen_desktop_layout
.start_corner
= data
[3];
350 /* fill in a zero rows/columns */
351 if (!(data
[1] == 0 && data
[2] == 0)) { /* both 0's is bad data.. */
353 data
[1] = (screen_num_desktops
+
354 screen_num_desktops
% data
[2]) / data
[2];
355 } else if (data
[2] == 0) {
356 data
[2] = (screen_num_desktops
+
357 screen_num_desktops
% data
[1]) / data
[1];
359 screen_desktop_layout
.columns
= data
[1];
360 screen_desktop_layout
.rows
= data
[2];
363 /* bounds checking */
364 if (screen_desktop_layout
.orientation
==
365 prop_atoms
.net_wm_orientation_horz
) {
366 if (screen_desktop_layout
.rows
> screen_num_desktops
)
367 screen_desktop_layout
.rows
= screen_num_desktops
;
368 if (screen_desktop_layout
.columns
>
369 ((screen_num_desktops
+ screen_num_desktops
%
370 screen_desktop_layout
.rows
) /
371 screen_desktop_layout
.rows
))
372 screen_desktop_layout
.columns
=
373 (screen_num_desktops
+ screen_num_desktops
%
374 screen_desktop_layout
.rows
) /
375 screen_desktop_layout
.rows
;
377 if (screen_desktop_layout
.columns
> screen_num_desktops
)
378 screen_desktop_layout
.columns
= screen_num_desktops
;
379 if (screen_desktop_layout
.rows
>
380 ((screen_num_desktops
+ screen_num_desktops
%
381 screen_desktop_layout
.columns
) /
382 screen_desktop_layout
.columns
))
383 screen_desktop_layout
.rows
=
384 (screen_num_desktops
+ screen_num_desktops
%
385 screen_desktop_layout
.columns
) /
386 screen_desktop_layout
.columns
;
393 void screen_update_desktop_names()
397 /* empty the array */
398 g_strfreev(screen_desktop_names
);
399 screen_desktop_names
= NULL
;
401 if (PROP_GETSS(ob_root
, net_desktop_names
, utf8
, &screen_desktop_names
))
402 for (i
= 0; screen_desktop_names
[i
] && i
<= screen_num_desktops
; ++i
);
405 if (i
<= screen_num_desktops
) {
406 screen_desktop_names
= g_renew(char*, screen_desktop_names
,
407 screen_num_desktops
+ 1);
408 screen_desktop_names
[screen_num_desktops
] = NULL
;
409 for (; i
< screen_num_desktops
; ++i
)
410 screen_desktop_names
[i
] = g_strdup("Unnamed Desktop");
414 void screen_show_desktop(gboolean show
)
418 if (show
== screen_showing_desktop
) return; /* no change */
420 screen_showing_desktop
= show
;
424 for (it
= g_list_last(stacking_list
); it
!= NULL
; it
= it
->prev
) {
425 if (WINDOW_IS_CLIENT(it
->data
)) {
426 Client
*client
= it
->data
;
427 if (client
->frame
->visible
&& !client_should_show(client
))
428 frame_hide(client
->frame
);
433 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
) {
434 if (WINDOW_IS_CLIENT(it
->data
)) {
435 Client
*client
= it
->data
;
436 if (!client
->frame
->visible
&& client_should_show(client
))
437 frame_show(client
->frame
);
444 for (it
= focus_order
[screen_desktop
]; it
; it
= it
->next
)
445 if (((Client
*)it
->data
)->type
== Type_Desktop
&&
446 client_focus(it
->data
))
449 focus_fallback(Fallback_NoFocus
);
452 show
= !!show
; /* make it boolean */
453 PROP_SET32(ob_root
, net_showing_desktop
, cardinal
, show
);
455 dispatch_ob(Event_Ob_ShowDesktop
, show
, 0);
458 void screen_install_colormap(Client
*client
, gboolean install
)
460 XWindowAttributes wa
;
462 if (client
== NULL
) {
464 XInstallColormap(ob_display
, render_colormap
);
466 XUninstallColormap(ob_display
, render_colormap
);
468 if (XGetWindowAttributes(ob_display
, client
->window
, &wa
) &&
469 wa
.colormap
!= None
) {
471 XInstallColormap(ob_display
, wa
.colormap
);
473 XUninstallColormap(ob_display
, wa
.colormap
);
478 void screen_update_struts()
484 strut
= g_new0(Strut
, screen_num_desktops
+ 1);
486 for (it
= client_list
; it
!= NULL
; it
= it
->next
) {
487 Client
*c
= it
->data
;
488 if (c
->iconic
) continue; /* these dont count in the strut */
490 if (c
->desktop
== 0xffffffff) {
491 for (i
= 0; i
< screen_num_desktops
; ++i
)
492 STRUT_ADD(strut
[i
], c
->strut
);
494 g_assert(c
->desktop
< screen_num_desktops
);
495 STRUT_ADD(strut
[c
->desktop
], c
->strut
);
497 /* apply to the 'all desktops' strut */
498 STRUT_ADD(strut
[screen_num_desktops
], c
->strut
);
500 screen_update_area();
503 static void screen_update_area()
509 area
= g_new0(Rect
, screen_num_desktops
+ 1);
511 dims
= g_new(guint32
, 4 * screen_num_desktops
);
512 for (i
= 0; i
< screen_num_desktops
+ 1; ++i
) {
513 Rect old_area
= area
[i
];
516 // reset to the full areas
517 if (isXineramaActive())
518 xineramaUsableArea = getXineramaAreas();
522 RECT_SET(area
[i
], strut
[i
].left
, strut
[i
].top
,
523 screen_physical_size
.width
- (strut
[i
].left
+
525 screen_physical_size
.height
- (strut
[i
].top
+
530 if (isXineramaActive()) {
531 // keep each of the ximerama-defined areas inside the strut
532 RectList::iterator xit, xend = xineramaUsableArea.end();
533 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
534 if (xit->x() < usableArea.x()) {
535 xit->setX(usableArea.x());
536 xit->setWidth(xit->width() - usableArea.x());
538 if (xit->y() < usableArea.y()) {
539 xit->setY(usableArea.y());
540 xit->setHeight(xit->height() - usableArea.y());
542 if (xit->x() + xit->width() > usableArea.width())
543 xit->setWidth(usableArea.width() - xit->x());
544 if (xit->y() + xit->height() > usableArea.height())
545 xit->setHeight(usableArea.height() - xit->y());
550 if (!RECT_EQUAL(old_area
, area
[i
])) {
551 /* the area has changed, adjust all the maximized windows */
553 for (it
= client_list
; it
; it
= it
->next
) {
554 Client
*c
= it
->data
;
555 if (i
< screen_num_desktops
) {
557 client_remaximize(c
);
559 /* the 'all desktops' size */
560 if (c
->desktop
== DESKTOP_ALL
)
561 client_remaximize(c
);
566 /* don't set these for the 'all desktops' area */
567 if (i
< screen_num_desktops
) {
568 dims
[(i
* 4) + 0] = area
[i
].x
;
569 dims
[(i
* 4) + 1] = area
[i
].y
;
570 dims
[(i
* 4) + 2] = area
[i
].width
;
571 dims
[(i
* 4) + 3] = area
[i
].height
;
574 PROP_SETA32(ob_root
, net_workarea
, cardinal
,
575 dims
, 4 * screen_num_desktops
);
579 Rect
*screen_area(guint desktop
)
581 if (desktop
>= screen_num_desktops
) {
582 if (desktop
== DESKTOP_ALL
)
583 return &area
[screen_num_desktops
];
586 return &area
[desktop
];
589 Strut
*screen_strut(guint desktop
)
591 if (desktop
>= screen_num_desktops
) {
592 if (desktop
== DESKTOP_ALL
)
593 return &strut
[screen_num_desktops
];
596 return &strut
[desktop
];