10 #include "../render/render.h"
14 # include <sys/types.h>
18 /*! The event mask to grab on the root window */
19 #define ROOT_EVENTMASK (/*ColormapChangeMask |*/ PropertyChangeMask | \
20 EnterWindowMask | LeaveWindowMask | \
21 SubstructureNotifyMask | SubstructureRedirectMask | \
22 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
24 guint screen_num_desktops
= 0;
25 guint screen_desktop
= 0;
26 Size screen_physical_size
;
27 gboolean screen_showing_desktop
;
28 DesktopLayout screen_desktop_layout
;
29 GPtrArray
*screen_desktop_names
;
31 static Rect
*area
= NULL
;
32 static Strut
*strut
= NULL
;
33 static Window support_window
= None
;
35 static void screen_update_area();
37 static gboolean running
;
38 static int another_running(Display
*d
, XErrorEvent
*e
)
41 g_message("A window manager is already running on screen %d",
47 gboolean
screen_annex()
55 old
= XSetErrorHandler(another_running
);
56 XSelectInput(ob_display
, ob_root
, ROOT_EVENTMASK
);
57 XSync(ob_display
, FALSE
);
58 XSetErrorHandler(old
);
62 g_message("Managing screen %d", ob_screen
);
64 /* set the mouse cursor for the root window (the default cursor) */
65 XDefineCursor(ob_display
, ob_root
, ob_cursors
.left_ptr
);
67 /* set the OPENBOX_PID hint */
69 PROP_SET32(ob_root
, openbox_pid
, cardinal
, pid
);
71 /* create the netwm support window */
72 support_window
= XCreateSimpleWindow(ob_display
, ob_root
, 0,0,1,1,0,0,0);
74 /* set supporting window */
75 PROP_SET32(ob_root
, net_supporting_wm_check
, window
, support_window
);
77 /* set properties on the supporting window */
78 PROP_SETS(support_window
, net_wm_name
, utf8
, "Openbox");
79 PROP_SET32(support_window
, net_supporting_wm_check
, window
,support_window
);
81 /* set the _NET_SUPPORTED_ATOMS hint */
84 supported
= g_new(Atom
, num_support
);
85 supported
[i
++] = prop_atoms
.net_current_desktop
;
86 supported
[i
++] = prop_atoms
.net_number_of_desktops
;
87 supported
[i
++] = prop_atoms
.net_desktop_geometry
;
88 supported
[i
++] = prop_atoms
.net_desktop_viewport
;
89 supported
[i
++] = prop_atoms
.net_active_window
;
90 supported
[i
++] = prop_atoms
.net_workarea
;
91 supported
[i
++] = prop_atoms
.net_client_list
;
92 supported
[i
++] = prop_atoms
.net_client_list_stacking
;
93 supported
[i
++] = prop_atoms
.net_desktop_names
;
94 supported
[i
++] = prop_atoms
.net_close_window
;
95 supported
[i
++] = prop_atoms
.net_desktop_layout
;
96 supported
[i
++] = prop_atoms
.net_showing_desktop
;
97 supported
[i
++] = prop_atoms
.net_wm_name
;
98 supported
[i
++] = prop_atoms
.net_wm_visible_name
;
99 supported
[i
++] = prop_atoms
.net_wm_icon_name
;
100 supported
[i
++] = prop_atoms
.net_wm_visible_icon_name
;
101 supported
[i
++] = prop_atoms
.net_wm_desktop
;
102 supported
[i
++] = prop_atoms
.net_wm_strut
;
103 supported
[i
++] = prop_atoms
.net_wm_window_type
;
104 supported
[i
++] = prop_atoms
.net_wm_window_type_desktop
;
105 supported
[i
++] = prop_atoms
.net_wm_window_type_dock
;
106 supported
[i
++] = prop_atoms
.net_wm_window_type_toolbar
;
107 supported
[i
++] = prop_atoms
.net_wm_window_type_menu
;
108 supported
[i
++] = prop_atoms
.net_wm_window_type_utility
;
109 supported
[i
++] = prop_atoms
.net_wm_window_type_splash
;
110 supported
[i
++] = prop_atoms
.net_wm_window_type_dialog
;
111 supported
[i
++] = prop_atoms
.net_wm_window_type_normal
;
112 supported
[i
++] = prop_atoms
.net_wm_allowed_actions
;
113 supported
[i
++] = prop_atoms
.net_wm_action_move
;
114 supported
[i
++] = prop_atoms
.net_wm_action_resize
;
115 supported
[i
++] = prop_atoms
.net_wm_action_minimize
;
116 supported
[i
++] = prop_atoms
.net_wm_action_shade
;
117 supported
[i
++] = prop_atoms
.net_wm_action_maximize_horz
;
118 supported
[i
++] = prop_atoms
.net_wm_action_maximize_vert
;
119 supported
[i
++] = prop_atoms
.net_wm_action_fullscreen
;
120 supported
[i
++] = prop_atoms
.net_wm_action_change_desktop
;
121 supported
[i
++] = prop_atoms
.net_wm_action_close
;
122 supported
[i
++] = prop_atoms
.net_wm_state
;
123 supported
[i
++] = prop_atoms
.net_wm_state_modal
;
124 supported
[i
++] = prop_atoms
.net_wm_state_maximized_vert
;
125 supported
[i
++] = prop_atoms
.net_wm_state_maximized_horz
;
126 supported
[i
++] = prop_atoms
.net_wm_state_shaded
;
127 supported
[i
++] = prop_atoms
.net_wm_state_skip_taskbar
;
128 supported
[i
++] = prop_atoms
.net_wm_state_skip_pager
;
129 supported
[i
++] = prop_atoms
.net_wm_state_hidden
;
130 supported
[i
++] = prop_atoms
.net_wm_state_fullscreen
;
131 supported
[i
++] = prop_atoms
.net_wm_state_above
;
132 supported
[i
++] = prop_atoms
.net_wm_state_below
;
133 g_assert(i
== num_support
);
135 supported[] = prop_atoms.net_wm_moveresize;
136 supported[] = prop_atoms.net_wm_moveresize_size_topleft;
137 supported[] = prop_atoms.net_wm_moveresize_size_topright;
138 supported[] = prop_atoms.net_wm_moveresize_size_bottomleft;
139 supported[] = prop_atoms.net_wm_moveresize_size_bottomright;
140 supported[] = prop_atoms.net_wm_moveresize_move;
141 supported[] = prop_atoms.net_wm_action_stick;
144 PROP_SET32A(ob_root
, net_supported
, atom
, supported
, num_support
);
150 void screen_startup()
154 screen_desktop_names
= g_ptr_array_new();
156 /* get the initial size */
160 for (it
= config_desktops_names
; it
; it
= it
->next
)
161 g_ptr_array_add(screen_desktop_names
, it
->data
); /* dont strdup */
162 PROP_SETSA(ob_root
, net_desktop_names
, utf8
, screen_desktop_names
);
163 g_ptr_array_set_size(screen_desktop_names
, 0); /* rm the ptrs so they dont
167 screen_num_desktops
= 0;
168 screen_set_num_desktops(config_desktops_num
);
170 screen_set_desktop(0);
172 /* don't start in showing-desktop mode */
173 screen_showing_desktop
= FALSE
;
174 PROP_SET32(ob_root
, net_showing_desktop
, cardinal
, screen_showing_desktop
);
176 screen_update_layout();
179 void screen_shutdown()
183 XSelectInput(ob_display
, ob_root
, NoEventMask
);
185 PROP_ERASE(ob_root
, openbox_pid
); /* we're not running here no more! */
186 PROP_ERASE(ob_root
, net_supported
); /* not without us */
187 PROP_ERASE(ob_root
, net_showing_desktop
); /* don't keep this mode */
189 XDestroyWindow(ob_display
, support_window
);
191 for (i
= 0; i
< screen_desktop_names
->len
; ++i
)
192 g_free(g_ptr_array_index(screen_desktop_names
, i
));
193 g_ptr_array_free(screen_desktop_names
, TRUE
);
200 /* XXX RandR support here? */
203 /* Set the _NET_DESKTOP_GEOMETRY hint */
204 geometry
[0] = WidthOfScreen(ScreenOfDisplay(ob_display
, ob_screen
));
205 geometry
[1] = HeightOfScreen(ScreenOfDisplay(ob_display
, ob_screen
));
206 PROP_SET32A(ob_root
, net_desktop_geometry
, cardinal
, geometry
, 2);
207 screen_physical_size
.width
= geometry
[0];
208 screen_physical_size
.height
= geometry
[1];
210 if (ob_state
== State_Starting
)
213 screen_update_struts();
215 /* XXX adjust more stuff ? */
218 void screen_set_num_desktops(guint num
)
226 old
= screen_num_desktops
;
227 screen_num_desktops
= num
;
228 PROP_SET32(ob_root
, net_number_of_desktops
, cardinal
, num
);
230 /* set the viewport hint */
231 viewport
= g_new0(gulong
, num
* 2);
232 PROP_SET32A(ob_root
, net_desktop_viewport
, cardinal
, viewport
, num
* 2);
235 /* change our struts/area to match */
236 screen_update_struts();
238 /* the number of rows/columns will differ */
239 screen_update_layout();
241 /* may be some unnamed desktops that we need to fill in with names */
242 screen_update_desktop_names();
244 /* update the focus lists */
245 /* free our lists for the desktops which have disappeared */
246 for (i
= num
; i
< old
; ++i
)
247 g_list_free(focus_order
[i
]);
248 /* realloc the array */
249 focus_order
= g_renew(GList
*, focus_order
, num
);
250 /* set the new lists to be empty */
251 for (i
= old
; i
< num
; ++i
)
252 focus_order
[i
] = NULL
;
254 /* move windows on desktops that will no longer exist! */
255 for (it
= client_list
; it
!= NULL
; it
= it
->next
) {
256 Client
*c
= it
->data
;
257 if (c
->desktop
>= num
)
258 client_set_desktop(c
, num
- 1, FALSE
);
261 dispatch_ob(Event_Ob_NumDesktops
, num
, old
);
263 /* change our desktop if we're on one that no longer exists! */
264 if (screen_desktop
>= screen_num_desktops
)
265 screen_set_desktop(num
- 1);
268 void screen_set_desktop(guint num
)
274 g_assert(num
< screen_num_desktops
);
276 old
= screen_desktop
;
277 screen_desktop
= num
;
278 PROP_SET32(ob_root
, net_current_desktop
, cardinal
, num
);
280 if (old
== num
) return;
282 g_message("Moving to desktop %d", num
+1);
284 /* show windows before hiding the rest to lessen the enter/leave events */
286 /* show windows from top to bottom */
287 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
) {
288 Client
*c
= it
->data
;
289 if (!c
->frame
->visible
&& client_should_show(c
))
290 engine_frame_show(c
->frame
);
293 /* hide windows from bottom to top */
294 for (it
= g_list_last(stacking_list
); it
!= NULL
; it
= it
->prev
) {
295 Client
*c
= it
->data
;
296 if (c
->frame
->visible
&& !client_should_show(c
))
297 engine_frame_hide(c
->frame
);
300 /* focus the last focused window on the desktop, and ignore enter events
301 from the switch so it doesnt mess with the focus */
302 XSync(ob_display
, FALSE
);
303 while (XCheckTypedEvent(ob_display
, EnterNotify
, &e
));
304 focus_fallback(Fallback_Desktop
);
306 dispatch_ob(Event_Ob_Desktop
, num
, old
);
309 void screen_update_layout()
311 unsigned long *data
= NULL
;
314 screen_desktop_layout
.orientation
= prop_atoms
.net_wm_orientation_horz
;
315 screen_desktop_layout
.start_corner
= prop_atoms
.net_wm_topleft
;
316 screen_desktop_layout
.rows
= 1;
317 screen_desktop_layout
.columns
= screen_num_desktops
;
319 if (PROP_GET32A(ob_root
, net_desktop_layout
, cardinal
, data
, 4)) {
320 if (data
[0] == prop_atoms
.net_wm_orientation_vert
)
321 screen_desktop_layout
.orientation
= data
[0];
322 if (data
[3] == prop_atoms
.net_wm_topright
)
323 screen_desktop_layout
.start_corner
= data
[3];
324 else if (data
[3] == prop_atoms
.net_wm_bottomright
)
325 screen_desktop_layout
.start_corner
= data
[3];
326 else if (data
[3] == prop_atoms
.net_wm_bottomleft
)
327 screen_desktop_layout
.start_corner
= data
[3];
329 /* fill in a zero rows/columns */
330 if (!(data
[1] == 0 && data
[2] == 0)) { /* both 0's is bad data.. */
332 data
[1] = (screen_num_desktops
+
333 screen_num_desktops
% data
[2]) / data
[2];
334 } else if (data
[2] == 0) {
335 data
[2] = (screen_num_desktops
+
336 screen_num_desktops
% data
[1]) / data
[1];
338 screen_desktop_layout
.columns
= data
[1];
339 screen_desktop_layout
.rows
= data
[2];
342 /* bounds checking */
343 if (screen_desktop_layout
.orientation
==
344 prop_atoms
.net_wm_orientation_horz
) {
345 if (screen_desktop_layout
.rows
> screen_num_desktops
)
346 screen_desktop_layout
.rows
= screen_num_desktops
;
347 if (screen_desktop_layout
.columns
> ((screen_num_desktops
+
348 screen_num_desktops
%
349 screen_desktop_layout
.rows
) /
350 screen_desktop_layout
.rows
))
351 screen_desktop_layout
.columns
=
352 (screen_num_desktops
+ screen_num_desktops
%
353 screen_desktop_layout
.rows
) /
354 screen_desktop_layout
.rows
;
356 if (screen_desktop_layout
.columns
> screen_num_desktops
)
357 screen_desktop_layout
.columns
= screen_num_desktops
;
358 if (screen_desktop_layout
.rows
> ((screen_num_desktops
+
359 screen_num_desktops
%
360 screen_desktop_layout
.columns
) /
361 screen_desktop_layout
.columns
))
362 screen_desktop_layout
.rows
=
363 (screen_num_desktops
+ screen_num_desktops
%
364 screen_desktop_layout
.columns
) /
365 screen_desktop_layout
.columns
;
371 void screen_update_desktop_names()
375 /* empty the array */
376 for (i
= 0; i
< screen_desktop_names
->len
; ++i
)
377 g_free(g_ptr_array_index(screen_desktop_names
, i
));
378 g_ptr_array_set_size(screen_desktop_names
, 0);
380 PROP_GETSA(ob_root
, net_desktop_names
, utf8
, screen_desktop_names
);
382 while (screen_desktop_names
->len
< screen_num_desktops
)
383 g_ptr_array_add(screen_desktop_names
, g_strdup("Unnamed Desktop"));
386 void screen_show_desktop(gboolean show
)
390 if (show
== screen_showing_desktop
) return; /* no change */
392 screen_showing_desktop
= show
;
396 for (it
= g_list_last(stacking_list
); it
!= NULL
; it
= it
->prev
) {
397 Client
*client
= it
->data
;
398 if (client
->frame
->visible
&& !client_should_show(client
))
399 engine_frame_hide(client
->frame
);
403 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
) {
404 Client
*client
= it
->data
;
405 if (!client
->frame
->visible
&& client_should_show(client
))
406 engine_frame_show(client
->frame
);
410 show
= !!show
; /* make it boolean */
411 PROP_SET32(ob_root
, net_showing_desktop
, cardinal
, show
);
413 dispatch_ob(Event_Ob_ShowDesktop
, show
, 0);
416 void screen_install_colormap(Client
*client
, gboolean install
)
418 XWindowAttributes wa
;
420 if (client
== NULL
) {
422 XInstallColormap(ob_display
, render_colormap
);
424 XUninstallColormap(ob_display
, render_colormap
);
426 if (XGetWindowAttributes(ob_display
, client
->window
, &wa
) &&
427 wa
.colormap
!= None
) {
429 XInstallColormap(ob_display
, wa
.colormap
);
431 XUninstallColormap(ob_display
, wa
.colormap
);
436 void screen_update_struts()
442 strut
= g_new0(Strut
, screen_num_desktops
+ 1);
444 for (it
= client_list
; it
!= NULL
; it
= it
->next
) {
445 Client
*c
= it
->data
;
446 if (c
->iconic
) continue; /* these dont count in the strut */
448 if (c
->desktop
== 0xffffffff) {
449 for (i
= 0; i
< screen_num_desktops
; ++i
)
450 STRUT_ADD(strut
[i
], c
->strut
);
452 g_assert(c
->desktop
< screen_num_desktops
);
453 STRUT_ADD(strut
[c
->desktop
], c
->strut
);
455 /* apply to the 'all desktops' strut */
456 STRUT_ADD(strut
[screen_num_desktops
], c
->strut
);
458 screen_update_area();
461 static void screen_update_area()
467 area
= g_new0(Rect
, screen_num_desktops
+ 1);
469 dims
= g_new(unsigned long, 4 * screen_num_desktops
);
470 for (i
= 0; i
< screen_num_desktops
+ 1; ++i
) {
471 Rect old_area
= area
[i
];
474 // reset to the full areas
475 if (isXineramaActive())
476 xineramaUsableArea = getXineramaAreas();
480 RECT_SET(area
[i
], strut
[i
].left
, strut
[i
].top
,
481 screen_physical_size
.width
- (strut
[i
].left
+
483 screen_physical_size
.height
- (strut
[i
].top
+
488 if (isXineramaActive()) {
489 // keep each of the ximerama-defined areas inside the strut
490 RectList::iterator xit, xend = xineramaUsableArea.end();
491 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
492 if (xit->x() < usableArea.x()) {
493 xit->setX(usableArea.x());
494 xit->setWidth(xit->width() - usableArea.x());
496 if (xit->y() < usableArea.y()) {
497 xit->setY(usableArea.y());
498 xit->setHeight(xit->height() - usableArea.y());
500 if (xit->x() + xit->width() > usableArea.width())
501 xit->setWidth(usableArea.width() - xit->x());
502 if (xit->y() + xit->height() > usableArea.height())
503 xit->setHeight(usableArea.height() - xit->y());
508 if (!RECT_EQUAL(old_area
, area
[i
])) {
509 /* the area has changed, adjust all the maximized windows */
511 for (it
= client_list
; it
; it
= it
->next
) {
512 Client
*c
= it
->data
;
513 if (i
< screen_num_desktops
) {
515 client_remaximize(c
);
517 /* the 'all desktops' size */
518 if (c
->desktop
== DESKTOP_ALL
)
519 client_remaximize(c
);
524 /* don't set these for the 'all desktops' area */
525 if (i
< screen_num_desktops
) {
526 dims
[(i
* 4) + 0] = area
[i
].x
;
527 dims
[(i
* 4) + 1] = area
[i
].y
;
528 dims
[(i
* 4) + 2] = area
[i
].width
;
529 dims
[(i
* 4) + 3] = area
[i
].height
;
532 PROP_SET32A(ob_root
, net_workarea
, cardinal
,
533 dims
, 4 * screen_num_desktops
);
537 Rect
*screen_area(guint desktop
)
539 if (desktop
>= screen_num_desktops
) {
540 if (desktop
== DESKTOP_ALL
)
541 return &area
[screen_num_desktops
];
544 return &area
[desktop
];
547 Strut
*screen_strut(guint desktop
)
549 if (desktop
>= screen_num_desktops
) {
550 if (desktop
== DESKTOP_ALL
)
551 return &strut
[screen_num_desktops
];
554 return &strut
[desktop
];