13 #include "extensions.h"
14 #include "render/render.h"
17 # define SN_API_NOT_YET_FROZEN
18 # include <libsn/sn.h>
23 # include <sys/types.h>
27 /*! The event mask to grab on the root window */
28 #define ROOT_EVENTMASK (StructureNotifyMask | PropertyChangeMask | \
29 EnterWindowMask | LeaveWindowMask | \
30 SubstructureNotifyMask | SubstructureRedirectMask | \
31 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
33 guint screen_num_desktops
= 0;
34 guint screen_desktop
= 0;
35 Size screen_physical_size
;
36 gboolean screen_showing_desktop
;
37 DesktopLayout screen_desktop_layout
;
38 char **screen_desktop_names
= NULL
;
40 static Rect
*area
= NULL
;
41 static Strut
*strut
= NULL
;
42 static Window support_window
= None
;
45 static SnMonitorContext
*sn_context
;
46 static int sn_busy_cnt
;
47 static Timer
*sn_timer
= NULL
;
49 static void sn_event_func(SnMonitorEvent
*event
, void *data
);
52 static void screen_update_area();
53 static void set_root_cursor();
55 static gboolean running
;
56 static int another_running(Display
*d
, XErrorEvent
*e
)
59 g_message("A window manager is already running on screen %d",
65 gboolean
screen_annex()
73 old
= XSetErrorHandler(another_running
);
74 XSelectInput(ob_display
, ob_root
, ROOT_EVENTMASK
);
75 XSync(ob_display
, FALSE
);
76 XSetErrorHandler(old
);
80 g_message("Managing screen %d", ob_screen
);
84 /* set the OPENBOX_PID hint */
86 PROP_SET32(ob_root
, openbox_pid
, cardinal
, pid
);
88 /* create the netwm support window */
89 support_window
= XCreateSimpleWindow(ob_display
, ob_root
, 0,0,1,1,0,0,0);
91 /* set supporting window */
92 PROP_SET32(ob_root
, net_supporting_wm_check
, window
, support_window
);
94 /* set properties on the supporting window */
95 PROP_SETS(support_window
, net_wm_name
, "Openbox3");
96 PROP_SET32(support_window
, net_supporting_wm_check
, window
,support_window
);
98 /* set the _NET_SUPPORTED_ATOMS hint */
101 supported
= g_new(guint32
, num_support
);
102 supported
[i
++] = prop_atoms
.net_current_desktop
;
103 supported
[i
++] = prop_atoms
.net_number_of_desktops
;
104 supported
[i
++] = prop_atoms
.net_desktop_geometry
;
105 supported
[i
++] = prop_atoms
.net_desktop_viewport
;
106 supported
[i
++] = prop_atoms
.net_active_window
;
107 supported
[i
++] = prop_atoms
.net_workarea
;
108 supported
[i
++] = prop_atoms
.net_client_list
;
109 supported
[i
++] = prop_atoms
.net_client_list_stacking
;
110 supported
[i
++] = prop_atoms
.net_desktop_names
;
111 supported
[i
++] = prop_atoms
.net_close_window
;
112 supported
[i
++] = prop_atoms
.net_desktop_layout
;
113 supported
[i
++] = prop_atoms
.net_showing_desktop
;
114 supported
[i
++] = prop_atoms
.net_wm_name
;
115 supported
[i
++] = prop_atoms
.net_wm_visible_name
;
116 supported
[i
++] = prop_atoms
.net_wm_icon_name
;
117 supported
[i
++] = prop_atoms
.net_wm_visible_icon_name
;
118 supported
[i
++] = prop_atoms
.net_wm_desktop
;
119 supported
[i
++] = prop_atoms
.net_wm_strut
;
120 supported
[i
++] = prop_atoms
.net_wm_window_type
;
121 supported
[i
++] = prop_atoms
.net_wm_window_type_desktop
;
122 supported
[i
++] = prop_atoms
.net_wm_window_type_dock
;
123 supported
[i
++] = prop_atoms
.net_wm_window_type_toolbar
;
124 supported
[i
++] = prop_atoms
.net_wm_window_type_menu
;
125 supported
[i
++] = prop_atoms
.net_wm_window_type_utility
;
126 supported
[i
++] = prop_atoms
.net_wm_window_type_splash
;
127 supported
[i
++] = prop_atoms
.net_wm_window_type_dialog
;
128 supported
[i
++] = prop_atoms
.net_wm_window_type_normal
;
129 supported
[i
++] = prop_atoms
.net_wm_allowed_actions
;
130 supported
[i
++] = prop_atoms
.net_wm_action_move
;
131 supported
[i
++] = prop_atoms
.net_wm_action_resize
;
132 supported
[i
++] = prop_atoms
.net_wm_action_minimize
;
133 supported
[i
++] = prop_atoms
.net_wm_action_shade
;
134 supported
[i
++] = prop_atoms
.net_wm_action_maximize_horz
;
135 supported
[i
++] = prop_atoms
.net_wm_action_maximize_vert
;
136 supported
[i
++] = prop_atoms
.net_wm_action_fullscreen
;
137 supported
[i
++] = prop_atoms
.net_wm_action_change_desktop
;
138 supported
[i
++] = prop_atoms
.net_wm_action_close
;
139 supported
[i
++] = prop_atoms
.net_wm_state
;
140 supported
[i
++] = prop_atoms
.net_wm_state_modal
;
141 supported
[i
++] = prop_atoms
.net_wm_state_maximized_vert
;
142 supported
[i
++] = prop_atoms
.net_wm_state_maximized_horz
;
143 supported
[i
++] = prop_atoms
.net_wm_state_shaded
;
144 supported
[i
++] = prop_atoms
.net_wm_state_skip_taskbar
;
145 supported
[i
++] = prop_atoms
.net_wm_state_skip_pager
;
146 supported
[i
++] = prop_atoms
.net_wm_state_hidden
;
147 supported
[i
++] = prop_atoms
.net_wm_state_fullscreen
;
148 supported
[i
++] = prop_atoms
.net_wm_state_above
;
149 supported
[i
++] = prop_atoms
.net_wm_state_below
;
150 supported
[i
++] = prop_atoms
.net_moveresize_window
;
151 supported
[i
++] = prop_atoms
.net_wm_moveresize
;
152 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_topleft
;
153 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_top
;
154 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_topright
;
155 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_right
;
156 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_bottomright
;
157 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_bottom
;
158 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_bottomleft
;
159 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_left
;
160 supported
[i
++] = prop_atoms
.net_wm_moveresize_move
;
161 supported
[i
++] = prop_atoms
.net_wm_moveresize_size_keyboard
;
162 supported
[i
++] = prop_atoms
.net_wm_moveresize_move_keyboard
;
163 g_assert(i
== num_support
);
165 supported[] = prop_atoms.net_wm_action_stick;
168 PROP_SETA32(ob_root
, net_supported
, atom
, supported
, num_support
);
174 void screen_startup()
179 /* get the initial size */
180 screen_resize(WidthOfScreen(ScreenOfDisplay(ob_display
, ob_screen
)),
181 HeightOfScreen(ScreenOfDisplay(ob_display
, ob_screen
)));
184 screen_desktop_names
= g_new(char*,
185 g_slist_length(config_desktops_names
) + 1);
186 for (i
= 0, it
= config_desktops_names
; it
; ++i
, it
= it
->next
)
187 screen_desktop_names
[i
] = it
->data
; /* dont strdup */
188 screen_desktop_names
[i
] = NULL
;
189 PROP_SETSS(ob_root
, net_desktop_names
, screen_desktop_names
);
190 g_free(screen_desktop_names
); /* dont free the individual strings */
191 screen_desktop_names
= NULL
;
193 screen_num_desktops
= 0;
194 screen_set_num_desktops(config_desktops_num
);
195 if (startup_desktop
>= screen_num_desktops
)
197 screen_desktop
= startup_desktop
;
198 screen_set_desktop(startup_desktop
);
200 /* don't start in showing-desktop mode */
201 screen_showing_desktop
= FALSE
;
202 PROP_SET32(ob_root
, net_showing_desktop
, cardinal
, screen_showing_desktop
);
204 screen_update_layout();
207 sn_context
= sn_monitor_context_new(ob_sn_display
, ob_screen
,
208 sn_event_func
, NULL
, NULL
);
213 void screen_shutdown()
215 XSelectInput(ob_display
, ob_root
, NoEventMask
);
217 PROP_ERASE(ob_root
, openbox_pid
); /* we're not running here no more! */
218 PROP_ERASE(ob_root
, net_supported
); /* not without us */
219 PROP_ERASE(ob_root
, net_showing_desktop
); /* don't keep this mode */
221 XDestroyWindow(ob_display
, support_window
);
223 g_strfreev(screen_desktop_names
);
228 void screen_resize(int w
, int h
)
233 /* Set the _NET_DESKTOP_GEOMETRY hint */
236 PROP_SETA32(ob_root
, net_desktop_geometry
, cardinal
, geometry
, 2);
237 screen_physical_size
.width
= geometry
[0];
238 screen_physical_size
.height
= geometry
[1];
240 if (ob_state
== State_Starting
)
244 screen_update_struts();
246 for (it
= client_list
; it
; it
= it
->next
)
247 client_move_onscreen(it
->data
);
250 void screen_set_num_desktops(guint num
)
258 old
= screen_num_desktops
;
259 screen_num_desktops
= num
;
260 PROP_SET32(ob_root
, net_number_of_desktops
, cardinal
, num
);
262 /* set the viewport hint */
263 viewport
= g_new0(guint32
, num
* 2);
264 PROP_SETA32(ob_root
, net_desktop_viewport
, cardinal
, viewport
, num
* 2);
267 /* the number of rows/columns will differ */
268 screen_update_layout();
270 /* may be some unnamed desktops that we need to fill in with names */
271 screen_update_desktop_names();
273 /* update the focus lists */
274 /* free our lists for the desktops which have disappeared */
275 for (i
= num
; i
< old
; ++i
)
276 g_list_free(focus_order
[i
]);
277 /* realloc the array */
278 focus_order
= g_renew(GList
*, focus_order
, num
);
279 /* set the new lists to be empty */
280 for (i
= old
; i
< num
; ++i
)
281 focus_order
[i
] = NULL
;
283 /* move windows on desktops that will no longer exist! */
284 for (it
= client_list
; it
!= NULL
; it
= it
->next
) {
285 Client
*c
= it
->data
;
286 if (c
->desktop
>= num
&& c
->desktop
!= DESKTOP_ALL
)
287 client_set_desktop(c
, num
- 1, FALSE
);
290 /* change our struts/area to match (after moving windows) */
291 screen_update_struts();
293 dispatch_ob(Event_Ob_NumDesktops
, num
, old
);
295 /* change our desktop if we're on one that no longer exists! */
296 if (screen_desktop
>= screen_num_desktops
)
297 screen_set_desktop(num
- 1);
300 void screen_set_desktop(guint num
)
306 g_assert(num
< screen_num_desktops
);
308 old
= screen_desktop
;
309 screen_desktop
= num
;
310 PROP_SET32(ob_root
, net_current_desktop
, cardinal
, num
);
312 if (old
== num
) return;
314 g_message("Moving to desktop %d", num
+1);
316 /* show windows before hiding the rest to lessen the enter/leave events */
318 /* show windows from top to bottom */
319 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
) {
320 if (WINDOW_IS_CLIENT(it
->data
)) {
321 Client
*c
= it
->data
;
322 if (!c
->frame
->visible
&& client_should_show(c
))
323 frame_show(c
->frame
);
327 /* hide windows from bottom to top */
328 for (it
= g_list_last(stacking_list
); it
!= NULL
; it
= it
->prev
) {
329 if (WINDOW_IS_CLIENT(it
->data
)) {
330 Client
*c
= it
->data
;
331 if (c
->frame
->visible
&& !client_should_show(c
))
332 frame_hide(c
->frame
);
336 /* focus the last focused window on the desktop, and ignore enter events
337 from the switch so it doesnt mess with the focus */
338 while (XCheckTypedEvent(ob_display
, EnterNotify
, &e
));
339 g_message("switch fallback");
340 focus_fallback(Fallback_Desktop
);
341 g_message("/switch fallback");
343 dispatch_ob(Event_Ob_Desktop
, num
, old
);
346 void screen_update_layout()
348 guint32
*data
= NULL
;
352 screen_desktop_layout
.orientation
= prop_atoms
.net_wm_orientation_horz
;
353 screen_desktop_layout
.start_corner
= prop_atoms
.net_wm_topleft
;
354 screen_desktop_layout
.rows
= 1;
355 screen_desktop_layout
.columns
= screen_num_desktops
;
357 if (PROP_GETA32(ob_root
, net_desktop_layout
, cardinal
, &data
, &num
)) {
358 if (num
== 3 || num
== 4) {
359 if (data
[0] == prop_atoms
.net_wm_orientation_vert
)
360 screen_desktop_layout
.orientation
= data
[0];
362 screen_desktop_layout
.start_corner
=
363 prop_atoms
.net_wm_topright
;
365 if (data
[3] == prop_atoms
.net_wm_topright
)
366 screen_desktop_layout
.start_corner
= data
[3];
367 else if (data
[3] == prop_atoms
.net_wm_bottomright
)
368 screen_desktop_layout
.start_corner
= data
[3];
369 else if (data
[3] == prop_atoms
.net_wm_bottomleft
)
370 screen_desktop_layout
.start_corner
= data
[3];
373 /* fill in a zero rows/columns */
374 if (!(data
[1] == 0 && data
[2] == 0)) { /* both 0's is bad data.. */
376 data
[1] = (screen_num_desktops
+
377 screen_num_desktops
% data
[2]) / data
[2];
378 } else if (data
[2] == 0) {
379 data
[2] = (screen_num_desktops
+
380 screen_num_desktops
% data
[1]) / data
[1];
382 screen_desktop_layout
.columns
= data
[1];
383 screen_desktop_layout
.rows
= data
[2];
386 /* bounds checking */
387 if (screen_desktop_layout
.orientation
==
388 prop_atoms
.net_wm_orientation_horz
) {
389 if (screen_desktop_layout
.rows
> screen_num_desktops
)
390 screen_desktop_layout
.rows
= screen_num_desktops
;
391 if (screen_desktop_layout
.columns
>
392 ((screen_num_desktops
+ screen_num_desktops
%
393 screen_desktop_layout
.rows
) /
394 screen_desktop_layout
.rows
))
395 screen_desktop_layout
.columns
=
396 (screen_num_desktops
+ screen_num_desktops
%
397 screen_desktop_layout
.rows
) /
398 screen_desktop_layout
.rows
;
400 if (screen_desktop_layout
.columns
> screen_num_desktops
)
401 screen_desktop_layout
.columns
= screen_num_desktops
;
402 if (screen_desktop_layout
.rows
>
403 ((screen_num_desktops
+ screen_num_desktops
%
404 screen_desktop_layout
.columns
) /
405 screen_desktop_layout
.columns
))
406 screen_desktop_layout
.rows
=
407 (screen_num_desktops
+ screen_num_desktops
%
408 screen_desktop_layout
.columns
) /
409 screen_desktop_layout
.columns
;
416 void screen_update_desktop_names()
420 /* empty the array */
421 g_strfreev(screen_desktop_names
);
422 screen_desktop_names
= NULL
;
424 if (PROP_GETSS(ob_root
, net_desktop_names
, utf8
, &screen_desktop_names
))
425 for (i
= 0; screen_desktop_names
[i
] && i
<= screen_num_desktops
; ++i
);
428 if (i
<= screen_num_desktops
) {
429 screen_desktop_names
= g_renew(char*, screen_desktop_names
,
430 screen_num_desktops
+ 1);
431 screen_desktop_names
[screen_num_desktops
] = NULL
;
432 for (; i
< screen_num_desktops
; ++i
)
433 screen_desktop_names
[i
] = g_strdup("Unnamed Desktop");
437 void screen_show_desktop(gboolean show
)
441 if (show
== screen_showing_desktop
) return; /* no change */
443 screen_showing_desktop
= show
;
447 for (it
= g_list_last(stacking_list
); it
!= NULL
; it
= it
->prev
) {
448 if (WINDOW_IS_CLIENT(it
->data
)) {
449 Client
*client
= it
->data
;
450 if (client
->frame
->visible
&& !client_should_show(client
))
451 frame_hide(client
->frame
);
456 for (it
= stacking_list
; it
!= NULL
; it
= it
->next
) {
457 if (WINDOW_IS_CLIENT(it
->data
)) {
458 Client
*client
= it
->data
;
459 if (!client
->frame
->visible
&& client_should_show(client
))
460 frame_show(client
->frame
);
467 for (it
= focus_order
[screen_desktop
]; it
; it
= it
->next
)
468 if (((Client
*)it
->data
)->type
== Type_Desktop
&&
469 client_focus(it
->data
))
472 focus_fallback(Fallback_NoFocus
);
475 show
= !!show
; /* make it boolean */
476 PROP_SET32(ob_root
, net_showing_desktop
, cardinal
, show
);
478 dispatch_ob(Event_Ob_ShowDesktop
, show
, 0);
481 void screen_install_colormap(Client
*client
, gboolean install
)
483 XWindowAttributes wa
;
485 if (client
== NULL
) {
487 XInstallColormap(RrDisplay(ob_rr_inst
), RrColormap(ob_rr_inst
));
489 XUninstallColormap(RrDisplay(ob_rr_inst
), RrColormap(ob_rr_inst
));
491 if (XGetWindowAttributes(ob_display
, client
->window
, &wa
) &&
492 wa
.colormap
!= None
) {
493 xerror_set_ignore(TRUE
);
495 XInstallColormap(RrDisplay(ob_rr_inst
), wa
.colormap
);
497 XUninstallColormap(RrDisplay(ob_rr_inst
), wa
.colormap
);
498 xerror_set_ignore(FALSE
);
503 void screen_update_struts()
509 strut
= g_new0(Strut
, screen_num_desktops
+ 1);
511 for (it
= client_list
; it
!= NULL
; it
= it
->next
) {
512 Client
*c
= it
->data
;
513 if (c
->iconic
) continue; /* these dont count in the strut */
515 if (c
->desktop
== 0xffffffff) {
516 for (i
= 0; i
< screen_num_desktops
; ++i
)
517 STRUT_ADD(strut
[i
], c
->strut
);
519 g_assert(c
->desktop
< screen_num_desktops
);
520 STRUT_ADD(strut
[c
->desktop
], c
->strut
);
522 /* apply to the 'all desktops' strut */
523 STRUT_ADD(strut
[screen_num_desktops
], c
->strut
);
526 for (i
= 0; i
< screen_num_desktops
; ++i
)
527 STRUT_ADD(strut
[i
], dock_strut
);
529 screen_update_area();
532 static void screen_update_area()
538 area
= g_new0(Rect
, screen_num_desktops
+ 1);
540 dims
= g_new(guint32
, 4 * screen_num_desktops
);
541 for (i
= 0; i
< screen_num_desktops
+ 1; ++i
) {
542 Rect old_area
= area
[i
];
545 // reset to the full areas
546 if (isXineramaActive())
547 xineramaUsableArea = getXineramaAreas();
551 RECT_SET(area
[i
], strut
[i
].left
, strut
[i
].top
,
552 screen_physical_size
.width
- (strut
[i
].left
+
554 screen_physical_size
.height
- (strut
[i
].top
+
559 if (isXineramaActive()) {
560 // keep each of the ximerama-defined areas inside the strut
561 RectList::iterator xit, xend = xineramaUsableArea.end();
562 for (xit = xineramaUsableArea.begin(); xit != xend; ++xit) {
563 if (xit->x() < usableArea.x()) {
564 xit->setX(usableArea.x());
565 xit->setWidth(xit->width() - usableArea.x());
567 if (xit->y() < usableArea.y()) {
568 xit->setY(usableArea.y());
569 xit->setHeight(xit->height() - usableArea.y());
571 if (xit->x() + xit->width() > usableArea.width())
572 xit->setWidth(usableArea.width() - xit->x());
573 if (xit->y() + xit->height() > usableArea.height())
574 xit->setHeight(usableArea.height() - xit->y());
579 if (!RECT_EQUAL(old_area
, area
[i
])) {
580 /* the area has changed, adjust all the maximized windows */
582 for (it
= client_list
; it
; it
= it
->next
) {
583 Client
*c
= it
->data
;
584 if (i
< screen_num_desktops
) {
586 client_reconfigure(c
);
588 /* the 'all desktops' size */
589 if (c
->desktop
== DESKTOP_ALL
)
590 client_reconfigure(c
);
595 /* don't set these for the 'all desktops' area */
596 if (i
< screen_num_desktops
) {
597 dims
[(i
* 4) + 0] = area
[i
].x
;
598 dims
[(i
* 4) + 1] = area
[i
].y
;
599 dims
[(i
* 4) + 2] = area
[i
].width
;
600 dims
[(i
* 4) + 3] = area
[i
].height
;
603 PROP_SETA32(ob_root
, net_workarea
, cardinal
,
604 dims
, 4 * screen_num_desktops
);
608 Rect
*screen_area(guint desktop
)
610 if (desktop
>= screen_num_desktops
) {
611 if (desktop
== DESKTOP_ALL
)
612 return &area
[screen_num_desktops
];
615 return &area
[desktop
];
618 Strut
*screen_strut(guint desktop
)
620 if (desktop
>= screen_num_desktops
) {
621 if (desktop
== DESKTOP_ALL
)
622 return &strut
[screen_num_desktops
];
625 return &strut
[desktop
];
628 static void set_root_cursor()
632 XDefineCursor(ob_display
, ob_root
, ob_cursors
.busy
);
635 XDefineCursor(ob_display
, ob_root
, ob_cursors
.ptr
);
639 static void sn_timeout(void *data
)
641 timer_stop(sn_timer
);
648 static void sn_event_func(SnMonitorEvent
*ev
, void *data
)
650 SnStartupSequence
*seq
;
651 const char *seq_id
, *bin_name
;
652 int cnt
= sn_busy_cnt
;
654 if (!(seq
= sn_monitor_event_get_startup_sequence(ev
)))
657 seq_id
= sn_startup_sequence_get_id(seq
);
658 bin_name
= sn_startup_sequence_get_binary_name(seq
);
660 if (!(seq_id
&& bin_name
))
663 switch (sn_monitor_event_get_type(ev
)) {
664 case SN_MONITOR_EVENT_INITIATED
:
667 timer_stop(sn_timer
);
668 /* 30 second timeout for apps to start */
669 sn_timer
= timer_start(30 * 1000000, sn_timeout
, NULL
);
671 case SN_MONITOR_EVENT_CHANGED
:
673 case SN_MONITOR_EVENT_COMPLETED
:
674 if (sn_busy_cnt
) --sn_busy_cnt
;
676 timer_stop(sn_timer
);
680 case SN_MONITOR_EVENT_CANCELED
:
681 if (sn_busy_cnt
) --sn_busy_cnt
;
683 timer_stop(sn_timer
);
688 if (sn_busy_cnt
!= cnt
)