12 #include "extensions.h"
13 #include "../render/render.h"
16 # define SN_API_NOT_YET_FROZEN
17 # include <libsn/sn.h>
22 # include <sys/types.h>
26 /*! The event mask to grab on the root window */
27 #define ROOT_EVENTMASK (StructureNotifyMask | PropertyChangeMask | \
28 EnterWindowMask | LeaveWindowMask | \
29 SubstructureNotifyMask | SubstructureRedirectMask | \
30 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
32 guint screen_num_desktops
= 0;
33 guint screen_desktop
= 0;
34 Size screen_physical_size
;
35 gboolean screen_showing_desktop
;
36 DesktopLayout screen_desktop_layout
;
37 char **screen_desktop_names
= NULL
;
39 static Rect
*area
= NULL
;
40 static Strut
*strut
= NULL
;
41 static Window support_window
= None
;
44 static SnMonitorContext
*sn_context
;
45 static int sn_busy_cnt
;
46 static Timer
*sn_timer
= NULL
;
48 static void sn_event_func(SnMonitorEvent
*event
, void *data
);
51 static void screen_update_area();
52 static void set_root_cursor();
54 static gboolean running
;
55 static int another_running(Display
*d
, XErrorEvent
*e
)
58 g_message("A window manager is already running on screen %d",
64 gboolean
screen_annex()
72 old
= XSetErrorHandler(another_running
);
73 XSelectInput(ob_display
, ob_root
, ROOT_EVENTMASK
);
74 XSync(ob_display
, FALSE
);
75 XSetErrorHandler(old
);
79 g_message("Managing screen %d", ob_screen
);
83 /* set the OPENBOX_PID hint */
85 PROP_SET32(ob_root
, openbox_pid
, cardinal
, pid
);
87 /* create the netwm support window */
88 support_window
= XCreateSimpleWindow(ob_display
, ob_root
, 0,0,1,1,0,0,0);
90 /* set supporting window */
91 PROP_SET32(ob_root
, net_supporting_wm_check
, window
, support_window
);
93 /* set properties on the supporting window */
94 PROP_SETS(support_window
, net_wm_name
, "Openbox3");
95 PROP_SET32(support_window
, net_supporting_wm_check
, window
,support_window
);
97 /* set the _NET_SUPPORTED_ATOMS hint */
100 supported
= g_new(guint32
, num_support
);
101 supported
[i
++] = prop_atoms
.net_current_desktop
;
102 supported
[i
++] = prop_atoms
.net_number_of_desktops
;
103 supported
[i
++] = prop_atoms
.net_desktop_geometry
;
104 supported
[i
++] = prop_atoms
.net_desktop_viewport
;
105 supported
[i
++] = prop_atoms
.net_active_window
;
106 supported
[i
++] = prop_atoms
.net_workarea
;
107 supported
[i
++] = prop_atoms
.net_client_list
;
108 supported
[i
++] = prop_atoms
.net_client_list_stacking
;
109 supported
[i
++] = prop_atoms
.net_desktop_names
;
110 supported
[i
++] = prop_atoms
.net_close_window
;
111 supported
[i
++] = prop_atoms
.net_desktop_layout
;
112 supported
[i
++] = prop_atoms
.net_showing_desktop
;
113 supported
[i
++] = prop_atoms
.net_wm_name
;
114 supported
[i
++] = prop_atoms
.net_wm_visible_name
;
115 supported
[i
++] = prop_atoms
.net_wm_icon_name
;
116 supported
[i
++] = prop_atoms
.net_wm_visible_icon_name
;
117 supported
[i
++] = prop_atoms
.net_wm_desktop
;
118 supported
[i
++] = prop_atoms
.net_wm_strut
;
119 supported
[i
++] = prop_atoms
.net_wm_window_type
;
120 supported
[i
++] = prop_atoms
.net_wm_window_type_desktop
;
121 supported
[i
++] = prop_atoms
.net_wm_window_type_dock
;
122 supported
[i
++] = prop_atoms
.net_wm_window_type_toolbar
;
123 supported
[i
++] = prop_atoms
.net_wm_window_type_menu
;
124 supported
[i
++] = prop_atoms
.net_wm_window_type_utility
;
125 supported
[i
++] = prop_atoms
.net_wm_window_type_splash
;
126 supported
[i
++] = prop_atoms
.net_wm_window_type_dialog
;
127 supported
[i
++] = prop_atoms
.net_wm_window_type_normal
;
128 supported
[i
++] = prop_atoms
.net_wm_allowed_actions
;
129 supported
[i
++] = prop_atoms
.net_wm_action_move
;
130 supported
[i
++] = prop_atoms
.net_wm_action_resize
;
131 supported
[i
++] = prop_atoms
.net_wm_action_minimize
;
132 supported
[i
++] = prop_atoms
.net_wm_action_shade
;
133 supported
[i
++] = prop_atoms
.net_wm_action_maximize_horz
;
134 supported
[i
++] = prop_atoms
.net_wm_action_maximize_vert
;
135 supported
[i
++] = prop_atoms
.net_wm_action_fullscreen
;
136 supported
[i
++] = prop_atoms
.net_wm_action_change_desktop
;
137 supported
[i
++] = prop_atoms
.net_wm_action_close
;
138 supported
[i
++] = prop_atoms
.net_wm_state
;
139 supported
[i
++] = prop_atoms
.net_wm_state_modal
;
140 supported
[i
++] = prop_atoms
.net_wm_state_maximized_vert
;
141 supported
[i
++] = prop_atoms
.net_wm_state_maximized_horz
;
142 supported
[i
++] = prop_atoms
.net_wm_state_shaded
;
143 supported
[i
++] = prop_atoms
.net_wm_state_skip_taskbar
;
144 supported
[i
++] = prop_atoms
.net_wm_state_skip_pager
;
145 supported
[i
++] = prop_atoms
.net_wm_state_hidden
;
146 supported
[i
++] = prop_atoms
.net_wm_state_fullscreen
;
147 supported
[i
++] = prop_atoms
.net_wm_state_above
;
148 supported
[i
++] = prop_atoms
.net_wm_state_below
;
149 supported
[i
++] = prop_atoms
.net_moveresize_window
;
150 supported
[i
++] = prop_atoms
.net_wm_moveresize
;
151 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_topleft
;
152 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_top
;
153 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_topright
;
154 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_right
;
155 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_bottomright
;
156 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_bottom
;
157 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_bottomleft
;
158 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_left
;
159 supported
[i
++] = prop_atoms
.net_wm_moveresize_move
;
160 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_keyboard
;
161 supported
[i
++] = prop_atoms
.net_wm_moveresize_move_keyboard
;
162 g_assert(i
== num_support
);
164 supported[] = prop_atoms.net_wm_action_stick;
167 PROP_SETA32(ob_root
, net_supported
, atom
, supported
, num_support
);
173 void screen_startup()
178 /* get the initial size */
179 screen_resize(WidthOfScreen(ScreenOfDisplay(ob_display
, ob_screen
)),
180 HeightOfScreen(ScreenOfDisplay(ob_display
, ob_screen
)));
183 screen_desktop_names
= g_new(char*,
184 g_slist_length(config_desktops_names
) + 1);
185 for (i
= 0, it
= config_desktops_names
; it
; ++i
, it
= it
->next
)
186 screen_desktop_names
[i
] = it
->data
; /* dont strdup */
187 screen_desktop_names
[i
] = NULL
;
188 PROP_SETSS(ob_root
, net_desktop_names
, screen_desktop_names
);
189 g_free(screen_desktop_names
); /* dont free the individual strings */
190 screen_desktop_names
= NULL
;
192 screen_num_desktops
= 0;
193 screen_set_num_desktops(config_desktops_num
);
194 if (startup_desktop
>= screen_num_desktops
)
196 screen_desktop
= startup_desktop
;
197 screen_set_desktop(startup_desktop
);
199 /* don't start in showing-desktop mode */
200 screen_showing_desktop
= FALSE
;
201 PROP_SET32(ob_root
, net_showing_desktop
, cardinal
, screen_showing_desktop
);
203 screen_update_layout();
206 sn_context
= sn_monitor_context_new(ob_sn_display
, ob_screen
,
207 sn_event_func
, NULL
, NULL
);
212 void screen_shutdown()
214 XSelectInput(ob_display
, ob_root
, NoEventMask
);
216 PROP_ERASE(ob_root
, openbox_pid
); /* we're not running here no more! */
217 PROP_ERASE(ob_root
, net_supported
); /* not without us */
218 PROP_ERASE(ob_root
, net_showing_desktop
); /* don't keep this mode */
220 XDestroyWindow(ob_display
, support_window
);
222 g_strfreev(screen_desktop_names
);
227 void screen_resize(int w
, int h
)
232 /* Set the _NET_DESKTOP_GEOMETRY hint */
235 PROP_SETA32(ob_root
, net_desktop_geometry
, cardinal
, geometry
, 2);
236 screen_physical_size
.width
= geometry
[0];
237 screen_physical_size
.height
= geometry
[1];
239 if (ob_state
== State_Starting
)
243 screen_update_struts();
245 for (it
= client_list
; it
; it
= it
->next
)
246 client_move_onscreen(it
->data
);
249 void screen_set_num_desktops(guint num
)
257 old
= screen_num_desktops
;
258 screen_num_desktops
= num
;
259 PROP_SET32(ob_root
, net_number_of_desktops
, cardinal
, num
);
261 /* set the viewport hint */
262 viewport
= g_new0(guint32
, num
* 2);
263 PROP_SETA32(ob_root
, net_desktop_viewport
, cardinal
, viewport
, num
* 2);
266 /* the number of rows/columns will differ */
267 screen_update_layout();
269 /* may be some unnamed desktops that we need to fill in with names */
270 screen_update_desktop_names();
272 /* update the focus lists */
273 /* free our lists for the desktops which have disappeared */
274 for (i
= num
; i
< old
; ++i
)
275 g_list_free(focus_order
[i
]);
276 /* realloc the array */
277 focus_order
= g_renew(GList
*, focus_order
, num
);
278 /* set the new lists to be empty */
279 for (i
= old
; i
< num
; ++i
)
280 focus_order
[i
] = NULL
;
282 /* move windows on desktops that will no longer exist! */
283 for (it
= client_list
; it
!= NULL
; it
= it
->next
) {
284 Client
*c
= it
->data
;
285 if (c
->desktop
>= num
&& c
->desktop
!= DESKTOP_ALL
)
286 client_set_desktop(c
, num
- 1, FALSE
);
289 /* change our struts/area to match (after moving windows) */
290 screen_update_struts();
292 dispatch_ob(Event_Ob_NumDesktops
, num
, old
);
294 /* change our desktop if we're on one that no longer exists! */
295 if (screen_desktop
>= screen_num_desktops
)
296 screen_set_desktop(num
- 1);
299 void screen_set_desktop(guint num
)
305 g_assert(num
< screen_num_desktops
);
307 old
= screen_desktop
;
308 screen_desktop
= num
;
309 PROP_SET32(ob_root
, net_current_desktop
, cardinal
, num
);
311 if (old
== num
) return;
313 g_message("Moving to desktop %d", num
+1);
315 /* show windows before hiding the rest to lessen the enter/leave events */
317 /* show windows from top to bottom */
318 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
) {
319 if (WINDOW_IS_CLIENT(it
->data
)) {
320 Client
*c
= it
->data
;
321 if (!c
->frame
->visible
&& client_should_show(c
))
322 frame_show(c
->frame
);
326 /* hide windows from bottom to top */
327 for (it
= g_list_last(stacking_list
); it
!= NULL
; it
= it
->prev
) {
328 if (WINDOW_IS_CLIENT(it
->data
)) {
329 Client
*c
= it
->data
;
330 if (c
->frame
->visible
&& !client_should_show(c
))
331 frame_hide(c
->frame
);
335 /* focus the last focused window on the desktop, and ignore enter events
336 from the switch so it doesnt mess with the focus */
337 while (XCheckTypedEvent(ob_display
, EnterNotify
, &e
));
338 focus_fallback(Fallback_Desktop
);
340 dispatch_ob(Event_Ob_Desktop
, num
, old
);
343 void screen_update_layout()
345 guint32
*data
= NULL
;
349 screen_desktop_layout
.orientation
= prop_atoms
.net_wm_orientation_horz
;
350 screen_desktop_layout
.start_corner
= prop_atoms
.net_wm_topleft
;
351 screen_desktop_layout
.rows
= 1;
352 screen_desktop_layout
.columns
= screen_num_desktops
;
354 if (PROP_GETA32(ob_root
, net_desktop_layout
, cardinal
, &data
, &num
)) {
355 if (num
== 3 || num
== 4) {
356 if (data
[0] == prop_atoms
.net_wm_orientation_vert
)
357 screen_desktop_layout
.orientation
= data
[0];
359 screen_desktop_layout
.start_corner
=
360 prop_atoms
.net_wm_topright
;
362 if (data
[3] == prop_atoms
.net_wm_topright
)
363 screen_desktop_layout
.start_corner
= data
[3];
364 else if (data
[3] == prop_atoms
.net_wm_bottomright
)
365 screen_desktop_layout
.start_corner
= data
[3];
366 else if (data
[3] == prop_atoms
.net_wm_bottomleft
)
367 screen_desktop_layout
.start_corner
= data
[3];
370 /* fill in a zero rows/columns */
371 if (!(data
[1] == 0 && data
[2] == 0)) { /* both 0's is bad data.. */
373 data
[1] = (screen_num_desktops
+
374 screen_num_desktops
% data
[2]) / data
[2];
375 } else if (data
[2] == 0) {
376 data
[2] = (screen_num_desktops
+
377 screen_num_desktops
% data
[1]) / data
[1];
379 screen_desktop_layout
.columns
= data
[1];
380 screen_desktop_layout
.rows
= data
[2];
383 /* bounds checking */
384 if (screen_desktop_layout
.orientation
==
385 prop_atoms
.net_wm_orientation_horz
) {
386 if (screen_desktop_layout
.rows
> screen_num_desktops
)
387 screen_desktop_layout
.rows
= screen_num_desktops
;
388 if (screen_desktop_layout
.columns
>
389 ((screen_num_desktops
+ screen_num_desktops
%
390 screen_desktop_layout
.rows
) /
391 screen_desktop_layout
.rows
))
392 screen_desktop_layout
.columns
=
393 (screen_num_desktops
+ screen_num_desktops
%
394 screen_desktop_layout
.rows
) /
395 screen_desktop_layout
.rows
;
397 if (screen_desktop_layout
.columns
> screen_num_desktops
)
398 screen_desktop_layout
.columns
= screen_num_desktops
;
399 if (screen_desktop_layout
.rows
>
400 ((screen_num_desktops
+ screen_num_desktops
%
401 screen_desktop_layout
.columns
) /
402 screen_desktop_layout
.columns
))
403 screen_desktop_layout
.rows
=
404 (screen_num_desktops
+ screen_num_desktops
%
405 screen_desktop_layout
.columns
) /
406 screen_desktop_layout
.columns
;
413 void screen_update_desktop_names()
417 /* empty the array */
418 g_strfreev(screen_desktop_names
);
419 screen_desktop_names
= NULL
;
421 if (PROP_GETSS(ob_root
, net_desktop_names
, utf8
, &screen_desktop_names
))
422 for (i
= 0; screen_desktop_names
[i
] && i
<= screen_num_desktops
; ++i
);
425 if (i
<= screen_num_desktops
) {
426 screen_desktop_names
= g_renew(char*, screen_desktop_names
,
427 screen_num_desktops
+ 1);
428 screen_desktop_names
[screen_num_desktops
] = NULL
;
429 for (; i
< screen_num_desktops
; ++i
)
430 screen_desktop_names
[i
] = g_strdup("Unnamed Desktop");
434 void screen_show_desktop(gboolean show
)
438 if (show
== screen_showing_desktop
) return; /* no change */
440 screen_showing_desktop
= show
;
444 for (it
= g_list_last(stacking_list
); it
!= NULL
; it
= it
->prev
) {
445 if (WINDOW_IS_CLIENT(it
->data
)) {
446 Client
*client
= it
->data
;
447 if (client
->frame
->visible
&& !client_should_show(client
))
448 frame_hide(client
->frame
);
453 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
) {
454 if (WINDOW_IS_CLIENT(it
->data
)) {
455 Client
*client
= it
->data
;
456 if (!client
->frame
->visible
&& client_should_show(client
))
457 frame_show(client
->frame
);
464 for (it
= focus_order
[screen_desktop
]; it
; it
= it
->next
)
465 if (((Client
*)it
->data
)->type
== Type_Desktop
&&
466 client_focus(it
->data
))
469 focus_fallback(Fallback_NoFocus
);
472 show
= !!show
; /* make it boolean */
473 PROP_SET32(ob_root
, net_showing_desktop
, cardinal
, show
);
475 dispatch_ob(Event_Ob_ShowDesktop
, show
, 0);
478 void screen_install_colormap(Client
*client
, gboolean install
)
480 XWindowAttributes wa
;
482 if (client
== NULL
) {
484 XInstallColormap(ob_display
, render_colormap
);
486 XUninstallColormap(ob_display
, render_colormap
);
488 if (XGetWindowAttributes(ob_display
, client
->window
, &wa
) &&
489 wa
.colormap
!= None
) {
490 xerror_set_ignore(TRUE
);
492 XInstallColormap(ob_display
, wa
.colormap
);
494 XUninstallColormap(ob_display
, wa
.colormap
);
495 xerror_set_ignore(FALSE
);
500 void screen_update_struts()
506 strut
= g_new0(Strut
, screen_num_desktops
+ 1);
508 for (it
= client_list
; it
!= NULL
; it
= it
->next
) {
509 Client
*c
= it
->data
;
510 if (c
->iconic
) continue; /* these dont count in the strut */
512 if (c
->desktop
== 0xffffffff) {
513 for (i
= 0; i
< screen_num_desktops
; ++i
)
514 STRUT_ADD(strut
[i
], c
->strut
);
516 g_assert(c
->desktop
< screen_num_desktops
);
517 STRUT_ADD(strut
[c
->desktop
], c
->strut
);
519 /* apply to the 'all desktops' strut */
520 STRUT_ADD(strut
[screen_num_desktops
], c
->strut
);
523 for (i
= 0; i
< screen_num_desktops
; ++i
)
524 STRUT_ADD(strut
[i
], dock_strut
);
526 screen_update_area();
529 static void screen_update_area()
535 area
= g_new0(Rect
, screen_num_desktops
+ 1);
537 dims
= g_new(guint32
, 4 * screen_num_desktops
);
538 for (i
= 0; i
< screen_num_desktops
+ 1; ++i
) {
539 Rect old_area
= area
[i
];
542 // reset to the full areas
543 if (isXineramaActive())
544 xineramaUsableArea = getXineramaAreas();
548 RECT_SET(area
[i
], strut
[i
].left
, strut
[i
].top
,
549 screen_physical_size
.width
- (strut
[i
].left
+
551 screen_physical_size
.height
- (strut
[i
].top
+
556 if (isXineramaActive()) {
557 // keep each of the ximerama-defined areas inside the strut
558 RectList::iterator xit, xend = xineramaUsableArea.end();
559 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
560 if (xit->x() < usableArea.x()) {
561 xit->setX(usableArea.x());
562 xit->setWidth(xit->width() - usableArea.x());
564 if (xit->y() < usableArea.y()) {
565 xit->setY(usableArea.y());
566 xit->setHeight(xit->height() - usableArea.y());
568 if (xit->x() + xit->width() > usableArea.width())
569 xit->setWidth(usableArea.width() - xit->x());
570 if (xit->y() + xit->height() > usableArea.height())
571 xit->setHeight(usableArea.height() - xit->y());
576 if (!RECT_EQUAL(old_area
, area
[i
])) {
577 /* the area has changed, adjust all the maximized windows */
579 for (it
= client_list
; it
; it
= it
->next
) {
580 Client
*c
= it
->data
;
581 if (i
< screen_num_desktops
) {
583 client_remaximize(c
);
585 /* the 'all desktops' size */
586 if (c
->desktop
== DESKTOP_ALL
)
587 client_remaximize(c
);
592 /* don't set these for the 'all desktops' area */
593 if (i
< screen_num_desktops
) {
594 dims
[(i
* 4) + 0] = area
[i
].x
;
595 dims
[(i
* 4) + 1] = area
[i
].y
;
596 dims
[(i
* 4) + 2] = area
[i
].width
;
597 dims
[(i
* 4) + 3] = area
[i
].height
;
600 PROP_SETA32(ob_root
, net_workarea
, cardinal
,
601 dims
, 4 * screen_num_desktops
);
605 Rect
*screen_area(guint desktop
)
607 if (desktop
>= screen_num_desktops
) {
608 if (desktop
== DESKTOP_ALL
)
609 return &area
[screen_num_desktops
];
612 return &area
[desktop
];
615 Strut
*screen_strut(guint desktop
)
617 if (desktop
>= screen_num_desktops
) {
618 if (desktop
== DESKTOP_ALL
)
619 return &strut
[screen_num_desktops
];
622 return &strut
[desktop
];
625 static void set_root_cursor()
629 XDefineCursor(ob_display
, ob_root
, ob_cursors
.busy
);
632 XDefineCursor(ob_display
, ob_root
, ob_cursors
.ptr
);
636 static void sn_timeout(void *data
)
638 timer_stop(sn_timer
);
645 static void sn_event_func(SnMonitorEvent
*ev
, void *data
)
647 SnStartupSequence
*seq
;
648 const char *seq_id
, *bin_name
;
649 int cnt
= sn_busy_cnt
;
651 if (!(seq
= sn_monitor_event_get_startup_sequence(ev
)))
654 seq_id
= sn_startup_sequence_get_id(seq
);
655 bin_name
= sn_startup_sequence_get_binary_name(seq
);
657 if (!(seq_id
&& bin_name
))
660 switch (sn_monitor_event_get_type(ev
)) {
661 case SN_MONITOR_EVENT_INITIATED
:
664 timer_stop(sn_timer
);
665 /* 30 second timeout for apps to start */
666 sn_timer
= timer_start(30 * 1000000, sn_timeout
, NULL
);
668 case SN_MONITOR_EVENT_CHANGED
:
670 case SN_MONITOR_EVENT_COMPLETED
:
671 if (sn_busy_cnt
) --sn_busy_cnt
;
673 timer_stop(sn_timer
);
677 case SN_MONITOR_EVENT_CANCELED
:
678 if (sn_busy_cnt
) --sn_busy_cnt
;
680 timer_stop(sn_timer
);
685 if (sn_busy_cnt
!= cnt
)