]> Dogcows Code - chaz/openbox/blob - openbox/screen.c
fix a bunch of memleaks from valgrind and stuff
[chaz/openbox] / openbox / screen.c
1 #include "debug.h"
2 #include "openbox.h"
3 #include "dock.h"
4 #include "xerror.h"
5 #include "prop.h"
6 #include "startup.h"
7 #include "grab.h"
8 #include "timer.h"
9 #include "config.h"
10 #include "screen.h"
11 #include "client.h"
12 #include "frame.h"
13 #include "focus.h"
14 #include "popup.h"
15 #include "dispatch.h"
16 #include "extensions.h"
17 #include "render/render.h"
18
19 #ifdef USE_LIBSN
20 # define SN_API_NOT_YET_FROZEN
21 # include <libsn/sn.h>
22 #endif
23
24 #include <X11/Xlib.h>
25 #ifdef HAVE_UNISTD_H
26 # include <sys/types.h>
27 # include <unistd.h>
28 #endif
29 #include <assert.h>
30
31 /*! The event mask to grab on the root window */
32 #define ROOT_EVENTMASK (StructureNotifyMask | PropertyChangeMask | \
33 EnterWindowMask | LeaveWindowMask | \
34 SubstructureNotifyMask | SubstructureRedirectMask | \
35 ButtonPressMask | ButtonReleaseMask | ButtonMotionMask)
36
37 guint screen_num_desktops;
38 guint screen_num_monitors;
39 guint screen_desktop;
40 Size screen_physical_size;
41 gboolean screen_showing_desktop;
42 DesktopLayout screen_desktop_layout;
43 char **screen_desktop_names;
44 Window screen_support_win;
45
46 static Rect **area; /* array of desktop holding array of xinerama areas */
47 static Rect *monitor_area;
48
49 static Popup *desktop_cycle_popup;
50
51 #ifdef USE_LIBSN
52 static SnMonitorContext *sn_context;
53 static int sn_busy_cnt;
54 static ObTimer *sn_timer;
55
56 static void sn_event_func(SnMonitorEvent *event, void *data);
57 #endif
58
59 static void set_root_cursor();
60
61 static gboolean replace_wm()
62 {
63 char *wm_sn;
64 Atom wm_sn_atom;
65 Window current_wm_sn_owner;
66 Time timestamp;
67
68 wm_sn = g_strdup_printf("WM_S%d", ob_screen);
69 wm_sn_atom = XInternAtom(ob_display, wm_sn, FALSE);
70 g_free(wm_sn);
71
72 current_wm_sn_owner = XGetSelectionOwner(ob_display, wm_sn_atom);
73 if (current_wm_sn_owner) {
74 if (!ob_replace_wm) {
75 g_warning("A window manager is already running on screen %d",
76 ob_screen);
77 return FALSE;
78 }
79 xerror_set_ignore(TRUE);
80 xerror_occured = FALSE;
81
82 /* We want to find out when the current selection owner dies */
83 XSelectInput(ob_display, current_wm_sn_owner, StructureNotifyMask);
84 XSync(ob_display, FALSE);
85
86 xerror_set_ignore(FALSE);
87 if (xerror_occured)
88 current_wm_sn_owner = None;
89 }
90
91 {
92 /* Generate a timestamp */
93 XEvent event;
94
95 XSelectInput(ob_display, screen_support_win, PropertyChangeMask);
96
97 XChangeProperty(ob_display, screen_support_win,
98 prop_atoms.wm_class, prop_atoms.string,
99 8, PropModeAppend, NULL, 0);
100 XWindowEvent(ob_display, screen_support_win,
101 PropertyChangeMask, &event);
102
103 XSelectInput(ob_display, screen_support_win, NoEventMask);
104
105 timestamp = event.xproperty.time;
106 }
107
108 XSetSelectionOwner(ob_display, wm_sn_atom, screen_support_win,
109 timestamp);
110
111 if (XGetSelectionOwner(ob_display, wm_sn_atom) != screen_support_win) {
112 g_warning("Could not acquire window manager selection on screen %d",
113 ob_screen);
114 return FALSE;
115 }
116
117 /* Wait for old window manager to go away */
118 if (current_wm_sn_owner) {
119 XEvent event;
120 gulong wait = 0;
121 const gulong timeout = G_USEC_PER_SEC * 15; /* wait for 15s max */
122
123 while (wait < timeout) {
124 if (XCheckWindowEvent(ob_display, current_wm_sn_owner,
125 StructureNotifyMask, &event) &&
126 event.type == DestroyNotify)
127 break;
128 g_usleep(G_USEC_PER_SEC / 10);
129 wait += G_USEC_PER_SEC / 10;
130 }
131
132 if (wait >= timeout) {
133 g_warning("Timeout expired while waiting for the current WM to die "
134 "on screen %d", ob_screen);
135 return FALSE;
136 }
137 }
138
139 /* Send client message indicating that we are now the WM */
140 prop_message(RootWindow(ob_display, ob_screen), prop_atoms.manager,
141 timestamp, wm_sn_atom, 0, 0, SubstructureNotifyMask);
142
143
144 return TRUE;
145 }
146
147 gboolean screen_annex()
148 {
149 XSetWindowAttributes attrib;
150 pid_t pid;
151 gint i, num_support;
152 guint32 *supported;
153
154 /* create the netwm support window */
155 attrib.override_redirect = TRUE;
156 screen_support_win = XCreateWindow(ob_display,
157 RootWindow(ob_display, ob_screen),
158 -100, -100, 1, 1, 0,
159 CopyFromParent, InputOutput,
160 CopyFromParent,
161 CWOverrideRedirect, &attrib);
162 XMapRaised(ob_display, screen_support_win);
163
164 if (!replace_wm()) {
165 XDestroyWindow(ob_display, screen_support_win);
166 return FALSE;
167 }
168
169 xerror_set_ignore(TRUE);
170 xerror_occured = FALSE;
171 XSelectInput(ob_display, RootWindow(ob_display, ob_screen),
172 ROOT_EVENTMASK);
173 xerror_set_ignore(FALSE);
174 if (xerror_occured) {
175 g_warning("A window manager is already running on screen %d",
176 ob_screen);
177
178 XDestroyWindow(ob_display, screen_support_win);
179 return FALSE;
180 }
181
182
183 set_root_cursor();
184
185 /* set the OPENBOX_PID hint */
186 pid = getpid();
187 PROP_SET32(RootWindow(ob_display, ob_screen),
188 openbox_pid, cardinal, pid);
189
190 /* set supporting window */
191 PROP_SET32(RootWindow(ob_display, ob_screen),
192 net_supporting_wm_check, window, screen_support_win);
193
194 /* set properties on the supporting window */
195 PROP_SETS(screen_support_win, net_wm_name, "Openbox");
196 PROP_SET32(screen_support_win, net_supporting_wm_check,
197 window, screen_support_win);
198
199 /* set the _NET_SUPPORTED_ATOMS hint */
200 num_support = 50;
201 i = 0;
202 supported = g_new(guint32, num_support);
203 supported[i++] = prop_atoms.net_current_desktop;
204 supported[i++] = prop_atoms.net_number_of_desktops;
205 supported[i++] = prop_atoms.net_desktop_geometry;
206 supported[i++] = prop_atoms.net_desktop_viewport;
207 supported[i++] = prop_atoms.net_active_window;
208 supported[i++] = prop_atoms.net_workarea;
209 supported[i++] = prop_atoms.net_client_list;
210 supported[i++] = prop_atoms.net_client_list_stacking;
211 supported[i++] = prop_atoms.net_desktop_names;
212 supported[i++] = prop_atoms.net_close_window;
213 supported[i++] = prop_atoms.net_desktop_layout;
214 supported[i++] = prop_atoms.net_showing_desktop;
215 supported[i++] = prop_atoms.net_wm_name;
216 supported[i++] = prop_atoms.net_wm_visible_name;
217 supported[i++] = prop_atoms.net_wm_icon_name;
218 supported[i++] = prop_atoms.net_wm_visible_icon_name;
219 supported[i++] = prop_atoms.net_wm_desktop;
220 supported[i++] = prop_atoms.net_wm_strut;
221 supported[i++] = prop_atoms.net_wm_window_type;
222 supported[i++] = prop_atoms.net_wm_window_type_desktop;
223 supported[i++] = prop_atoms.net_wm_window_type_dock;
224 supported[i++] = prop_atoms.net_wm_window_type_toolbar;
225 supported[i++] = prop_atoms.net_wm_window_type_menu;
226 supported[i++] = prop_atoms.net_wm_window_type_utility;
227 supported[i++] = prop_atoms.net_wm_window_type_splash;
228 supported[i++] = prop_atoms.net_wm_window_type_dialog;
229 supported[i++] = prop_atoms.net_wm_window_type_normal;
230 supported[i++] = prop_atoms.net_wm_allowed_actions;
231 supported[i++] = prop_atoms.net_wm_action_move;
232 supported[i++] = prop_atoms.net_wm_action_resize;
233 supported[i++] = prop_atoms.net_wm_action_minimize;
234 supported[i++] = prop_atoms.net_wm_action_shade;
235 supported[i++] = prop_atoms.net_wm_action_maximize_horz;
236 supported[i++] = prop_atoms.net_wm_action_maximize_vert;
237 supported[i++] = prop_atoms.net_wm_action_fullscreen;
238 supported[i++] = prop_atoms.net_wm_action_change_desktop;
239 supported[i++] = prop_atoms.net_wm_action_close;
240 supported[i++] = prop_atoms.net_wm_state;
241 supported[i++] = prop_atoms.net_wm_state_modal;
242 supported[i++] = prop_atoms.net_wm_state_maximized_vert;
243 supported[i++] = prop_atoms.net_wm_state_maximized_horz;
244 supported[i++] = prop_atoms.net_wm_state_shaded;
245 supported[i++] = prop_atoms.net_wm_state_skip_taskbar;
246 supported[i++] = prop_atoms.net_wm_state_skip_pager;
247 supported[i++] = prop_atoms.net_wm_state_hidden;
248 supported[i++] = prop_atoms.net_wm_state_fullscreen;
249 supported[i++] = prop_atoms.net_wm_state_above;
250 supported[i++] = prop_atoms.net_wm_state_below;
251 supported[i++] = prop_atoms.net_moveresize_window;
252 supported[i++] = prop_atoms.net_wm_moveresize;
253 g_assert(i == num_support);
254 /*
255 supported[] = prop_atoms.net_wm_action_stick;
256 */
257
258 PROP_SETA32(RootWindow(ob_display, ob_screen),
259 net_supported, atom, supported, num_support);
260 g_free(supported);
261
262 return TRUE;
263 }
264
265 void screen_startup()
266 {
267 GSList *it;
268 guint i;
269
270 desktop_cycle_popup = popup_new(FALSE);
271
272 /* get the initial size */
273 screen_resize();
274
275 /* set the names */
276 screen_desktop_names = g_new(char*,
277 g_slist_length(config_desktops_names) + 1);
278 for (i = 0, it = config_desktops_names; it; ++i, it = it->next)
279 screen_desktop_names[i] = it->data; /* dont strdup */
280 screen_desktop_names[i] = NULL;
281 PROP_SETSS(RootWindow(ob_display, ob_screen),
282 net_desktop_names, screen_desktop_names);
283 g_free(screen_desktop_names); /* dont free the individual strings */
284 screen_desktop_names = NULL;
285
286 screen_num_desktops = 0;
287 screen_set_num_desktops(config_desktops_num);
288 if (startup_desktop >= screen_num_desktops)
289 startup_desktop = 0;
290 screen_desktop = startup_desktop;
291 screen_set_desktop(startup_desktop);
292
293 /* don't start in showing-desktop mode */
294 screen_showing_desktop = FALSE;
295 PROP_SET32(RootWindow(ob_display, ob_screen),
296 net_showing_desktop, cardinal, screen_showing_desktop);
297
298 screen_update_layout();
299
300 #ifdef USE_LIBSN
301 sn_context = sn_monitor_context_new(ob_sn_display, ob_screen,
302 sn_event_func, NULL, NULL);
303 sn_busy_cnt = 0;
304 #endif
305 }
306
307 void screen_shutdown()
308 {
309 Rect **r;
310
311 popup_free(desktop_cycle_popup);
312
313 XSelectInput(ob_display, RootWindow(ob_display, ob_screen), NoEventMask);
314
315 /* we're not running here no more! */
316 PROP_ERASE(RootWindow(ob_display, ob_screen), openbox_pid);
317 /* not without us */
318 PROP_ERASE(RootWindow(ob_display, ob_screen), net_supported);
319 /* don't keep this mode */
320 PROP_ERASE(RootWindow(ob_display, ob_screen), net_showing_desktop);
321
322 XDestroyWindow(ob_display, screen_support_win);
323
324 g_strfreev(screen_desktop_names);
325 for (r = area; *r; ++r)
326 g_free(*r);
327 g_free(area);
328 }
329
330 void screen_resize()
331 {
332 static int oldw = 0, oldh = 0;
333 int w, h;
334 GList *it;
335 guint32 geometry[2];
336
337 w = WidthOfScreen(ScreenOfDisplay(ob_display, ob_screen));
338 h = HeightOfScreen(ScreenOfDisplay(ob_display, ob_screen));
339
340 if (w == oldw && h == oldh) return;
341
342 oldw = w; oldh = h;
343
344 /* Set the _NET_DESKTOP_GEOMETRY hint */
345 screen_physical_size.width = geometry[0] = w;
346 screen_physical_size.height = geometry[1] = h;
347 PROP_SETA32(RootWindow(ob_display, ob_screen),
348 net_desktop_geometry, cardinal, geometry, 2);
349
350 if (ob_state() == OB_STATE_STARTING)
351 return;
352
353 screen_update_areas();
354 dock_configure();
355
356 for (it = client_list; it; it = it->next)
357 client_move_onscreen(it->data, FALSE);
358 }
359
360 void screen_set_num_desktops(guint num)
361 {
362 guint i, old;
363 guint32 *viewport;
364 GList *it;
365
366 g_assert(num > 0);
367
368 old = screen_num_desktops;
369 screen_num_desktops = num;
370 PROP_SET32(RootWindow(ob_display, ob_screen),
371 net_number_of_desktops, cardinal, num);
372
373 /* set the viewport hint */
374 viewport = g_new0(guint32, num * 2);
375 PROP_SETA32(RootWindow(ob_display, ob_screen),
376 net_desktop_viewport, cardinal, viewport, num * 2);
377 g_free(viewport);
378
379 /* the number of rows/columns will differ */
380 screen_update_layout();
381
382 /* may be some unnamed desktops that we need to fill in with names */
383 screen_update_desktop_names();
384
385 /* update the focus lists */
386 /* free our lists for the desktops which have disappeared */
387 for (i = num; i < old; ++i)
388 g_list_free(focus_order[i]);
389 /* realloc the array */
390 focus_order = g_renew(GList*, focus_order, num);
391 /* set the new lists to be empty */
392 for (i = old; i < num; ++i)
393 focus_order[i] = NULL;
394
395 /* move windows on desktops that will no longer exist! */
396 for (it = client_list; it != NULL; it = it->next) {
397 ObClient *c = it->data;
398 if (c->desktop >= num && c->desktop != DESKTOP_ALL)
399 client_set_desktop(c, num - 1, FALSE);
400 }
401
402 /* change our struts/area to match (after moving windows) */
403 screen_update_areas();
404
405 dispatch_ob(Event_Ob_NumDesktops, num, old);
406
407 /* change our desktop if we're on one that no longer exists! */
408 if (screen_desktop >= screen_num_desktops)
409 screen_set_desktop(num - 1);
410 }
411
412 void screen_set_desktop(guint num)
413 {
414 GList *it;
415 guint old;
416 XEvent e;
417
418 g_assert(num < screen_num_desktops);
419
420 old = screen_desktop;
421 screen_desktop = num;
422 PROP_SET32(RootWindow(ob_display, ob_screen),
423 net_current_desktop, cardinal, num);
424
425 if (old == num) return;
426
427 ob_debug("Moving to desktop %d\n", num+1);
428
429 /* show windows before hiding the rest to lessen the enter/leave events */
430
431 /* show windows from top to bottom */
432 for (it = stacking_list; it != NULL; it = it->next) {
433 if (WINDOW_IS_CLIENT(it->data)) {
434 ObClient *c = it->data;
435 if (!c->frame->visible && client_should_show(c))
436 frame_show(c->frame);
437 }
438 }
439
440 /* hide windows from bottom to top */
441 for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
442 if (WINDOW_IS_CLIENT(it->data)) {
443 ObClient *c = it->data;
444 if (c->frame->visible && !client_should_show(c))
445 frame_hide(c->frame);
446 }
447 }
448
449 /* focus the last focused window on the desktop, and ignore enter events
450 from the switch so it doesnt mess with the focus */
451 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
452 #ifdef DEBUG_FOCUS
453 ob_debug("switch fallback\n");
454 #endif
455 focus_fallback(OB_FOCUS_FALLBACK_DESKTOP);
456 #ifdef DEBUG_FOCUS
457 ob_debug("/switch fallback\n");
458 #endif
459
460 dispatch_ob(Event_Ob_Desktop, num, old);
461 }
462
463 static void get_row_col(guint d, guint *r, guint *c)
464 {
465 switch (screen_desktop_layout.orientation) {
466 case OB_ORIENTATION_HORZ:
467 switch (screen_desktop_layout.start_corner) {
468 case OB_CORNER_TOPLEFT:
469 *r = d / screen_desktop_layout.columns;
470 *c = d % screen_desktop_layout.columns;
471 break;
472 case OB_CORNER_BOTTOMLEFT:
473 *r = screen_desktop_layout.rows - 1 -
474 d / screen_desktop_layout.columns;
475 *c = d % screen_desktop_layout.columns;
476 break;
477 case OB_CORNER_TOPRIGHT:
478 *r = d / screen_desktop_layout.columns;
479 *c = screen_desktop_layout.columns - 1 -
480 d % screen_desktop_layout.columns;
481 break;
482 case OB_CORNER_BOTTOMRIGHT:
483 *r = screen_desktop_layout.rows - 1 -
484 d / screen_desktop_layout.columns;
485 *c = screen_desktop_layout.columns - 1 -
486 d % screen_desktop_layout.columns;
487 break;
488 }
489 break;
490 case OB_ORIENTATION_VERT:
491 switch (screen_desktop_layout.start_corner) {
492 case OB_CORNER_TOPLEFT:
493 *r = d % screen_desktop_layout.rows;
494 *c = d / screen_desktop_layout.rows;
495 break;
496 case OB_CORNER_BOTTOMLEFT:
497 *r = screen_desktop_layout.rows - 1 -
498 d % screen_desktop_layout.rows;
499 *c = d / screen_desktop_layout.rows;
500 break;
501 case OB_CORNER_TOPRIGHT:
502 *r = d % screen_desktop_layout.rows;
503 *c = screen_desktop_layout.columns - 1 -
504 d / screen_desktop_layout.rows;
505 break;
506 case OB_CORNER_BOTTOMRIGHT:
507 *r = screen_desktop_layout.rows - 1 -
508 d % screen_desktop_layout.rows;
509 *c = screen_desktop_layout.columns - 1 -
510 d / screen_desktop_layout.rows;
511 break;
512 }
513 break;
514 }
515 }
516
517 static guint translate_row_col(guint r, guint c)
518 {
519 switch (screen_desktop_layout.orientation) {
520 case OB_ORIENTATION_HORZ:
521 switch (screen_desktop_layout.start_corner) {
522 case OB_CORNER_TOPLEFT:
523 return r % screen_desktop_layout.rows *
524 screen_desktop_layout.columns +
525 c % screen_desktop_layout.columns;
526 case OB_CORNER_BOTTOMLEFT:
527 return (screen_desktop_layout.rows - 1 -
528 r % screen_desktop_layout.rows) *
529 screen_desktop_layout.columns +
530 c % screen_desktop_layout.columns;
531 case OB_CORNER_TOPRIGHT:
532 return r % screen_desktop_layout.rows *
533 screen_desktop_layout.columns +
534 (screen_desktop_layout.columns - 1 -
535 c % screen_desktop_layout.columns);
536 case OB_CORNER_BOTTOMRIGHT:
537 return (screen_desktop_layout.rows - 1 -
538 r % screen_desktop_layout.rows) *
539 screen_desktop_layout.columns +
540 (screen_desktop_layout.columns - 1 -
541 c % screen_desktop_layout.columns);
542 }
543 case OB_ORIENTATION_VERT:
544 switch (screen_desktop_layout.start_corner) {
545 case OB_CORNER_TOPLEFT:
546 return c % screen_desktop_layout.columns *
547 screen_desktop_layout.rows +
548 r % screen_desktop_layout.rows;
549 case OB_CORNER_BOTTOMLEFT:
550 return c % screen_desktop_layout.columns *
551 screen_desktop_layout.rows +
552 (screen_desktop_layout.rows - 1 -
553 r % screen_desktop_layout.rows);
554 case OB_CORNER_TOPRIGHT:
555 return (screen_desktop_layout.columns - 1 -
556 c % screen_desktop_layout.columns) *
557 screen_desktop_layout.rows +
558 r % screen_desktop_layout.rows;
559 case OB_CORNER_BOTTOMRIGHT:
560 return (screen_desktop_layout.columns - 1 -
561 c % screen_desktop_layout.columns) *
562 screen_desktop_layout.rows +
563 (screen_desktop_layout.rows - 1 -
564 r % screen_desktop_layout.rows);
565 }
566 }
567 g_assert_not_reached();
568 return 0;
569 }
570
571 static void popup_cycle(guint d, gboolean show)
572 {
573 Rect *a;
574
575 if (!show) {
576 popup_hide(desktop_cycle_popup);
577 } else {
578 a = screen_physical_area_monitor(0);
579 popup_position(desktop_cycle_popup, CenterGravity,
580 a->x + a->width / 2, a->y + a->height / 2);
581 /* XXX the size and the font extents need to be related on some level
582 */
583 popup_size(desktop_cycle_popup, POPUP_WIDTH, POPUP_HEIGHT);
584
585 popup_set_text_align(desktop_cycle_popup, RR_JUSTIFY_CENTER);
586
587 popup_show(desktop_cycle_popup,
588 screen_desktop_names[d], NULL);
589 }
590 }
591
592 guint screen_cycle_desktop(ObDirection dir, gboolean wrap, gboolean linear,
593 gboolean done, gboolean cancel)
594 {
595 static gboolean first = TRUE;
596 static gboolean lin;
597 static guint origd, d;
598 guint r, c;
599
600 if (cancel) {
601 d = origd;
602 goto done_cycle;
603 } else if (done) {
604 screen_set_desktop(d);
605 goto done_cycle;
606 }
607 if (first) {
608 first = FALSE;
609 lin = linear;
610 d = origd = screen_desktop;
611 }
612
613 get_row_col(d, &r, &c);
614
615 if (lin) {
616 switch (dir) {
617 case OB_DIRECTION_EAST:
618 if (d < screen_num_desktops - 1)
619 ++d;
620 else if (wrap)
621 d = 0;
622 break;
623 case OB_DIRECTION_WEST:
624 if (d > 0)
625 --d;
626 else if (wrap)
627 d = screen_num_desktops - 1;
628 break;
629 default:
630 assert(0);
631 return screen_desktop;
632 }
633 } else {
634 switch (dir) {
635 case OB_DIRECTION_EAST:
636 ++c;
637 if (c >= screen_desktop_layout.columns) {
638 if (!wrap) return d = screen_desktop;
639 c = 0;
640 }
641 d = translate_row_col(r, c);
642 if (d >= screen_num_desktops) {
643 if (!wrap) return d = screen_desktop;
644 ++c;
645 }
646 break;
647 case OB_DIRECTION_WEST:
648 --c;
649 if (c >= screen_desktop_layout.columns) {
650 if (!wrap) return d = screen_desktop;
651 c = screen_desktop_layout.columns - 1;
652 }
653 d = translate_row_col(r, c);
654 if (d >= screen_num_desktops) {
655 if (!wrap) return d = screen_desktop;
656 --c;
657 }
658 break;
659 case OB_DIRECTION_SOUTH:
660 ++r;
661 if (r >= screen_desktop_layout.rows) {
662 if (!wrap) return d = screen_desktop;
663 r = 0;
664 }
665 d = translate_row_col(r, c);
666 if (d >= screen_num_desktops) {
667 if (!wrap) return d = screen_desktop;
668 ++r;
669 }
670 break;
671 case OB_DIRECTION_NORTH:
672 --r;
673 if (r >= screen_desktop_layout.rows) {
674 if (!wrap) return d = screen_desktop;
675 r = screen_desktop_layout.rows - 1;
676 }
677 d = translate_row_col(r, c);
678 if (d >= screen_num_desktops) {
679 if (!wrap) return d = screen_desktop;
680 --r;
681 }
682 break;
683 default:
684 assert(0);
685 return d = screen_desktop;
686 }
687
688 d = translate_row_col(r, c);
689 }
690
691 if (config_desktop_popup) {
692 popup_cycle(d, TRUE);
693 return d;
694 }
695
696 done_cycle:
697 first = TRUE;
698
699 popup_cycle(0, FALSE);
700
701 return d;
702 }
703
704 void screen_update_layout()
705 {
706 ObOrientation orient;
707 ObCorner corner;
708 guint rows;
709 guint cols;
710 guint32 *data;
711 guint num;
712 gboolean valid = FALSE;
713
714 if (PROP_GETA32(RootWindow(ob_display, ob_screen),
715 net_desktop_layout, cardinal, &data, &num)) {
716 if (num == 3 || num == 4) {
717
718 if (data[0] == prop_atoms.net_wm_orientation_vert)
719 orient = OB_ORIENTATION_VERT;
720 else if (data[0] == prop_atoms.net_wm_orientation_horz)
721 orient = OB_ORIENTATION_HORZ;
722 else
723 goto screen_update_layout_bail;
724
725 if (num < 4)
726 corner = OB_CORNER_TOPLEFT;
727 else {
728 if (data[3] == prop_atoms.net_wm_topleft)
729 corner = OB_CORNER_TOPLEFT;
730 else if (data[3] == prop_atoms.net_wm_topright)
731 corner = OB_CORNER_TOPRIGHT;
732 else if (data[3] == prop_atoms.net_wm_bottomright)
733 corner = OB_CORNER_BOTTOMRIGHT;
734 else if (data[3] == prop_atoms.net_wm_bottomleft)
735 corner = OB_CORNER_BOTTOMLEFT;
736 else
737 goto screen_update_layout_bail;
738 }
739
740 /* fill in a zero rows/columns */
741 if ((data[1] == 0 && data[2] == 0) || /* both 0's is bad data.. */
742 (data[1] != 0 && data[2] != 0)) { /* no 0's is bad data.. */
743 goto screen_update_layout_bail;
744 } else {
745 if (data[1] == 0) {
746 data[1] = (screen_num_desktops +
747 screen_num_desktops % data[2]) / data[2];
748 } else if (data[2] == 0) {
749 data[2] = (screen_num_desktops +
750 screen_num_desktops % data[1]) / data[1];
751 }
752 cols = data[1];
753 rows = data[2];
754 }
755
756 /* bounds checking */
757 if (orient == OB_ORIENTATION_HORZ) {
758 rows = MIN(rows, screen_num_desktops);
759 cols = MIN(cols, ((screen_num_desktops +
760 (screen_num_desktops % rows)) / rows));
761 } else {
762 cols = MIN(cols, screen_num_desktops);
763 rows = MIN(rows, ((screen_num_desktops +
764 (screen_num_desktops % cols)) / cols));
765 }
766
767 valid = TRUE;
768 }
769 screen_update_layout_bail:
770 g_free(data);
771 }
772
773 if (!valid) {
774 /* defaults */
775 orient = OB_ORIENTATION_HORZ;
776 corner = OB_CORNER_TOPLEFT;
777 rows = 1;
778 cols = screen_num_desktops;
779 }
780
781 screen_desktop_layout.orientation = orient;
782 screen_desktop_layout.start_corner = corner;
783 screen_desktop_layout.rows = rows;
784 screen_desktop_layout.columns = cols;
785 }
786
787 void screen_update_desktop_names()
788 {
789 guint i;
790
791 /* empty the array */
792 g_strfreev(screen_desktop_names);
793 screen_desktop_names = NULL;
794
795 if (PROP_GETSS(RootWindow(ob_display, ob_screen),
796 net_desktop_names, utf8, &screen_desktop_names))
797 for (i = 0; screen_desktop_names[i] && i <= screen_num_desktops; ++i);
798 else
799 i = 0;
800 if (i <= screen_num_desktops) {
801 screen_desktop_names = g_renew(char*, screen_desktop_names,
802 screen_num_desktops + 1);
803 screen_desktop_names[screen_num_desktops] = NULL;
804 for (; i < screen_num_desktops; ++i)
805 screen_desktop_names[i] = g_strdup("Unnamed Desktop");
806 }
807 }
808
809 void screen_show_desktop(gboolean show)
810 {
811 GList *it;
812
813 if (show == screen_showing_desktop) return; /* no change */
814
815 screen_showing_desktop = show;
816
817 if (show) {
818 /* bottom to top */
819 for (it = g_list_last(stacking_list); it != NULL; it = it->prev) {
820 if (WINDOW_IS_CLIENT(it->data)) {
821 ObClient *client = it->data;
822 if (client->frame->visible && !client_should_show(client))
823 frame_hide(client->frame);
824 }
825 }
826 } else {
827 /* top to bottom */
828 for (it = stacking_list; it != NULL; it = it->next) {
829 if (WINDOW_IS_CLIENT(it->data)) {
830 ObClient *client = it->data;
831 if (!client->frame->visible && client_should_show(client))
832 frame_show(client->frame);
833 }
834 }
835 }
836
837 if (show) {
838 /* focus desktop */
839 for (it = focus_order[screen_desktop]; it; it = it->next)
840 if (((ObClient*)it->data)->type == OB_CLIENT_TYPE_DESKTOP &&
841 client_focus(it->data))
842 break;
843 } else {
844 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
845 }
846
847 show = !!show; /* make it boolean */
848 PROP_SET32(RootWindow(ob_display, ob_screen),
849 net_showing_desktop, cardinal, show);
850
851 dispatch_ob(Event_Ob_ShowDesktop, show, 0);
852 }
853
854 void screen_install_colormap(ObClient *client, gboolean install)
855 {
856 XWindowAttributes wa;
857
858 if (client == NULL) {
859 if (install)
860 XInstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
861 else
862 XUninstallColormap(RrDisplay(ob_rr_inst), RrColormap(ob_rr_inst));
863 } else {
864 if (XGetWindowAttributes(ob_display, client->window, &wa) &&
865 wa.colormap != None) {
866 xerror_set_ignore(TRUE);
867 if (install)
868 XInstallColormap(RrDisplay(ob_rr_inst), wa.colormap);
869 else
870 XUninstallColormap(RrDisplay(ob_rr_inst), wa.colormap);
871 xerror_set_ignore(FALSE);
872 }
873 }
874 }
875
876 void screen_update_areas()
877 {
878 guint i, x;
879 guint32 *dims;
880 GList *it;
881
882 g_free(monitor_area);
883 extensions_xinerama_screens(&monitor_area, &screen_num_monitors);
884
885 if (area) {
886 for (i = 0; area[i]; ++i)
887 g_free(area[i]);
888 g_free(area);
889 }
890
891 area = g_new(Rect*, screen_num_desktops + 2);
892 for (i = 0; i < screen_num_desktops + 1; ++i)
893 area[i] = g_new(Rect, screen_num_monitors + 1);
894 area[i] = NULL;
895
896 dims = g_new(guint32, 4 * screen_num_desktops);
897
898 for (i = 0; i < screen_num_desktops + 1; ++i) {
899 Strut s;
900 int l, r, t, b;
901
902 /* calc the xinerama areas */
903 for (x = 0; x < screen_num_monitors; ++x) {
904 area[i][x] = monitor_area[x];
905 if (x == 0) {
906 l = monitor_area[x].x;
907 t = monitor_area[x].y;
908 r = monitor_area[x].x + monitor_area[x].width - 1;
909 b = monitor_area[x].y + monitor_area[x].height - 1;
910 } else {
911 l = MIN(l, monitor_area[x].x);
912 t = MIN(t, monitor_area[x].y);
913 r = MAX(r, monitor_area[x].x + monitor_area[x].width - 1);
914 b = MAX(b, monitor_area[x].y + monitor_area[x].height - 1);
915 }
916 }
917 RECT_SET(area[i][x], l, t, r - l + 1, b - t + 1);
918
919 /* apply struts */
920 STRUT_SET(s, 0, 0, 0, 0);
921 for (it = client_list; it; it = it->next)
922 STRUT_ADD(s, ((ObClient*)it->data)->strut);
923 STRUT_ADD(s, dock_strut);
924
925 if (s.left) {
926 int o;
927
928 /* find the left-most xin heads, i do this in 2 loops :| */
929 o = area[i][0].x;
930 for (x = 1; x < screen_num_monitors; ++x)
931 o = MIN(o, area[i][x].x);
932
933 for (x = 0; x < screen_num_monitors; ++x) {
934 int edge = o + s.left - area[i][x].x;
935 if (edge > 0) {
936 area[i][x].x += edge;
937 area[i][x].width -= edge;
938 }
939 }
940
941 area[i][screen_num_monitors].x += s.left;
942 area[i][screen_num_monitors].width -= s.left;
943 }
944 if (s.top) {
945 int o;
946
947 /* find the left-most xin heads, i do this in 2 loops :| */
948 o = area[i][0].y;
949 for (x = 1; x < screen_num_monitors; ++x)
950 o = MIN(o, area[i][x].y);
951
952 for (x = 0; x < screen_num_monitors; ++x) {
953 int edge = o + s.top - area[i][x].y;
954 if (edge > 0) {
955 area[i][x].y += edge;
956 area[i][x].height -= edge;
957 }
958 }
959
960 area[i][screen_num_monitors].y += s.top;
961 area[i][screen_num_monitors].height -= s.top;
962 }
963 if (s.right) {
964 int o;
965
966 /* find the bottom-most xin heads, i do this in 2 loops :| */
967 o = area[i][0].x + area[i][0].width - 1;
968 for (x = 1; x < screen_num_monitors; ++x)
969 o = MAX(o, area[i][x].x + area[i][x].width - 1);
970
971 for (x = 0; x < screen_num_monitors; ++x) {
972 int edge = (area[i][x].x + area[i][x].width - 1) -
973 (o - s.right);
974 if (edge > 0)
975 area[i][x].width -= edge;
976 }
977
978 area[i][screen_num_monitors].width -= s.right;
979 }
980 if (s.bottom) {
981 int o;
982
983 /* find the bottom-most xin heads, i do this in 2 loops :| */
984 o = area[i][0].y + area[i][0].height - 1;
985 for (x = 1; x < screen_num_monitors; ++x)
986 o = MAX(o, area[i][x].y + area[i][x].height - 1);
987
988 for (x = 0; x < screen_num_monitors; ++x) {
989 int edge = (area[i][x].y + area[i][x].height - 1) -
990 (o - s.bottom);
991 if (edge > 0)
992 area[i][x].height -= edge;
993 }
994
995 area[i][screen_num_monitors].height -= s.bottom;
996 }
997
998 /* XXX when dealing with partial struts, if its in a single
999 xinerama area, then only subtract it from that area's space
1000 for (x = 0; x < screen_num_monitors; ++x) {
1001 GList *it;
1002
1003
1004 do something smart with it for the 'all xinerama areas' one...
1005
1006 for (it = client_list; it; it = it->next) {
1007
1008 XXX if gunna test this shit, then gotta worry about when
1009 the client moves between xinerama heads..
1010
1011 if (RECT_CONTAINS_RECT(((ObClient*)it->data)->frame->area,
1012 area[i][x])) {
1013
1014 }
1015 }
1016 }
1017 */
1018
1019 /* XXX optimize when this is run? */
1020
1021 /* the area has changed, adjust all the maximized
1022 windows */
1023 for (it = client_list; it; it = it->next) {
1024 ObClient *c = it->data;
1025 if (i < screen_num_desktops) {
1026 if (c->desktop == i)
1027 client_reconfigure(c);
1028 } else if (c->desktop == DESKTOP_ALL)
1029 client_reconfigure(c);
1030 }
1031 if (i < screen_num_desktops) {
1032 /* don't set these for the 'all desktops' area */
1033 dims[(i * 4) + 0] = area[i][screen_num_monitors].x;
1034 dims[(i * 4) + 1] = area[i][screen_num_monitors].y;
1035 dims[(i * 4) + 2] = area[i][screen_num_monitors].width;
1036 dims[(i * 4) + 3] = area[i][screen_num_monitors].height;
1037 }
1038 }
1039 PROP_SETA32(RootWindow(ob_display, ob_screen), net_workarea, cardinal,
1040 dims, 4 * screen_num_desktops);
1041
1042 g_free(dims);
1043 }
1044
1045 Rect *screen_area(guint desktop)
1046 {
1047 return screen_area_monitor(desktop, screen_num_monitors);
1048 }
1049
1050 Rect *screen_area_monitor(guint desktop, guint head)
1051 {
1052 if (head > screen_num_monitors)
1053 return NULL;
1054 if (desktop >= screen_num_desktops) {
1055 if (desktop == DESKTOP_ALL)
1056 return &area[screen_num_desktops][head];
1057 return NULL;
1058 }
1059 return &area[desktop][head];
1060 }
1061
1062 Rect *screen_physical_area()
1063 {
1064 return screen_physical_area_monitor(screen_num_monitors);
1065 }
1066
1067 Rect *screen_physical_area_monitor(guint head)
1068 {
1069 if (head > screen_num_monitors)
1070 return NULL;
1071 return &monitor_area[head];
1072 }
1073
1074 static void set_root_cursor()
1075 {
1076 #ifdef USE_LIBSN
1077 if (sn_busy_cnt)
1078 XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
1079 ob_cursor(OB_CURSOR_BUSY));
1080 else
1081 #endif
1082 XDefineCursor(ob_display, RootWindow(ob_display, ob_screen),
1083 ob_cursor(OB_CURSOR_POINTER));
1084 }
1085
1086 #ifdef USE_LIBSN
1087 static void sn_timeout(ObTimer *t, void *data)
1088 {
1089 timer_stop(sn_timer);
1090 sn_timer = NULL;
1091 sn_busy_cnt = 0;
1092
1093 set_root_cursor();
1094 }
1095
1096 static void sn_event_func(SnMonitorEvent *ev, void *data)
1097 {
1098 SnStartupSequence *seq;
1099 const char *seq_id, *bin_name;
1100 int cnt = sn_busy_cnt;
1101
1102 if (!(seq = sn_monitor_event_get_startup_sequence(ev)))
1103 return;
1104
1105 seq_id = sn_startup_sequence_get_id(seq);
1106 bin_name = sn_startup_sequence_get_binary_name(seq);
1107
1108 if (!(seq_id && bin_name))
1109 return;
1110
1111 switch (sn_monitor_event_get_type(ev)) {
1112 case SN_MONITOR_EVENT_INITIATED:
1113 ++sn_busy_cnt;
1114 if (sn_timer)
1115 timer_stop(sn_timer);
1116 /* 30 second timeout for apps to start */
1117 sn_timer = timer_start(30 * 1000000, sn_timeout, NULL);
1118 break;
1119 case SN_MONITOR_EVENT_CHANGED:
1120 break;
1121 case SN_MONITOR_EVENT_COMPLETED:
1122 if (sn_busy_cnt) --sn_busy_cnt;
1123 if (sn_timer) {
1124 timer_stop(sn_timer);
1125 sn_timer = NULL;
1126 }
1127 break;
1128 case SN_MONITOR_EVENT_CANCELED:
1129 if (sn_busy_cnt) --sn_busy_cnt;
1130 if (sn_timer) {
1131 timer_stop(sn_timer);
1132 sn_timer = NULL;
1133 }
1134 };
1135
1136 if (sn_busy_cnt != cnt)
1137 set_root_cursor();
1138 }
1139 #endif
1140
1141 gboolean screen_pointer_pos(int *x, int *y)
1142 {
1143 Window w;
1144 int i;
1145 guint u;
1146
1147 return !!XQueryPointer(ob_display, RootWindow(ob_display, ob_screen),
1148 &w, &w, x, y, &i, &i, &u);
1149 }
This page took 0.082411 seconds and 5 git commands to generate.