]> Dogcows Code - chaz/openbox/blob - openbox/client.c
break focus. or maybe make it better.
[chaz/openbox] / openbox / client.c
1 #include "client.h"
2 #include "debug.h"
3 #include "startupnotify.h"
4 #include "dock.h"
5 #include "xerror.h"
6 #include "screen.h"
7 #include "moveresize.h"
8 #include "place.h"
9 #include "prop.h"
10 #include "extensions.h"
11 #include "frame.h"
12 #include "session.h"
13 #include "event.h"
14 #include "grab.h"
15 #include "focus.h"
16 #include "stacking.h"
17 #include "openbox.h"
18 #include "group.h"
19 #include "config.h"
20 #include "menuframe.h"
21 #include "keyboard.h"
22 #include "mouse.h"
23 #include "render/render.h"
24
25 #include <glib.h>
26 #include <X11/Xutil.h>
27
28 /*! The event mask to grab on client windows */
29 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
30 StructureNotifyMask)
31
32 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
33 ButtonMotionMask)
34
35 GList *client_list = NULL;
36 GSList *client_destructors = NULL;
37
38 static void client_get_all(ObClient *self);
39 static void client_toggle_border(ObClient *self, gboolean show);
40 static void client_get_startup_id(ObClient *self);
41 static void client_get_area(ObClient *self);
42 static void client_get_desktop(ObClient *self);
43 static void client_get_state(ObClient *self);
44 static void client_get_shaped(ObClient *self);
45 static void client_get_mwm_hints(ObClient *self);
46 static void client_get_gravity(ObClient *self);
47 static void client_showhide(ObClient *self);
48 static void client_change_allowed_actions(ObClient *self);
49 static void client_change_state(ObClient *self);
50 static void client_apply_startup_state(ObClient *self);
51 static void client_restore_session_state(ObClient *self);
52 static void client_restore_session_stacking(ObClient *self);
53 static void client_urgent_notify(ObClient *self);
54
55 void client_startup(gboolean reconfig)
56 {
57 if (reconfig) return;
58
59 client_set_list();
60 }
61
62 void client_shutdown(gboolean reconfig)
63 {
64 }
65
66 void client_add_destructor(GDestroyNotify func)
67 {
68 client_destructors = g_slist_prepend(client_destructors, (gpointer)func);
69 }
70
71 void client_remove_destructor(GDestroyNotify func)
72 {
73 client_destructors = g_slist_remove(client_destructors, (gpointer)func);
74 }
75
76 void client_set_list()
77 {
78 Window *windows, *win_it;
79 GList *it;
80 guint size = g_list_length(client_list);
81
82 /* create an array of the window ids */
83 if (size > 0) {
84 windows = g_new(Window, size);
85 win_it = windows;
86 for (it = client_list; it != NULL; it = it->next, ++win_it)
87 *win_it = ((ObClient*)it->data)->window;
88 } else
89 windows = NULL;
90
91 PROP_SETA32(RootWindow(ob_display, ob_screen),
92 net_client_list, window, (guint32*)windows, size);
93
94 if (windows)
95 g_free(windows);
96
97 stacking_set_list();
98 }
99
100 /*
101 void client_foreach_transient(ObClient *self, ObClientForeachFunc func, void *data)
102 {
103 GSList *it;
104
105 for (it = self->transients; it; it = it->next) {
106 if (!func(it->data, data)) return;
107 client_foreach_transient(it->data, func, data);
108 }
109 }
110
111 void client_foreach_ancestor(ObClient *self, ObClientForeachFunc func, void *data)
112 {
113 if (self->transient_for) {
114 if (self->transient_for != OB_TRAN_GROUP) {
115 if (!func(self->transient_for, data)) return;
116 client_foreach_ancestor(self->transient_for, func, data);
117 } else {
118 GSList *it;
119
120 for (it = self->group->members; it; it = it->next)
121 if (it->data != self &&
122 !((ObClient*)it->data)->transient_for) {
123 if (!func(it->data, data)) return;
124 client_foreach_ancestor(it->data, func, data);
125 }
126 }
127 }
128 }
129 */
130
131 void client_manage_all()
132 {
133 unsigned int i, j, nchild;
134 Window w, *children;
135 XWMHints *wmhints;
136 XWindowAttributes attrib;
137
138 XQueryTree(ob_display, RootWindow(ob_display, ob_screen),
139 &w, &w, &children, &nchild);
140
141 /* remove all icon windows from the list */
142 for (i = 0; i < nchild; i++) {
143 if (children[i] == None) continue;
144 wmhints = XGetWMHints(ob_display, children[i]);
145 if (wmhints) {
146 if ((wmhints->flags & IconWindowHint) &&
147 (wmhints->icon_window != children[i]))
148 for (j = 0; j < nchild; j++)
149 if (children[j] == wmhints->icon_window) {
150 children[j] = None;
151 break;
152 }
153 XFree(wmhints);
154 }
155 }
156
157 for (i = 0; i < nchild; ++i) {
158 if (children[i] == None)
159 continue;
160 if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
161 if (attrib.override_redirect) continue;
162
163 if (attrib.map_state != IsUnmapped)
164 client_manage(children[i]);
165 }
166 }
167 XFree(children);
168
169 if (config_focus_new)
170 focus_fallback(OB_FOCUS_FALLBACK_NOFOCUS);
171 }
172
173 void client_manage(Window window)
174 {
175 ObClient *self;
176 XEvent e;
177 XWindowAttributes attrib;
178 XSetWindowAttributes attrib_set;
179 XWMHints *wmhint;
180 gboolean activate = FALSE;
181
182 grab_server(TRUE);
183
184 /* check if it has already been unmapped by the time we started mapping
185 the grab does a sync so we don't have to here */
186 if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
187 XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
188 XPutBackEvent(ob_display, &e);
189
190 grab_server(FALSE);
191 return; /* don't manage it */
192 }
193
194 /* make sure it isn't an override-redirect window */
195 if (!XGetWindowAttributes(ob_display, window, &attrib) ||
196 attrib.override_redirect) {
197 grab_server(FALSE);
198 return; /* don't manage it */
199 }
200
201 /* is the window a docking app */
202 if ((wmhint = XGetWMHints(ob_display, window))) {
203 if ((wmhint->flags & StateHint) &&
204 wmhint->initial_state == WithdrawnState) {
205 dock_add(window, wmhint);
206 grab_server(FALSE);
207 XFree(wmhint);
208 return;
209 }
210 XFree(wmhint);
211 }
212
213 ob_debug("Managing window: %lx\n", window);
214
215 /* choose the events we want to receive on the CLIENT window */
216 attrib_set.event_mask = CLIENT_EVENTMASK;
217 attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
218 XChangeWindowAttributes(ob_display, window,
219 CWEventMask|CWDontPropagate, &attrib_set);
220
221
222 /* create the ObClient struct, and populate it from the hints on the
223 window */
224 self = g_new0(ObClient, 1);
225 self->obwin.type = Window_Client;
226 self->window = window;
227
228 /* non-zero defaults */
229 self->title_count = 1;
230 self->wmstate = NormalState;
231 self->layer = -1;
232 self->decorate = TRUE;
233 self->desktop = screen_num_desktops; /* always an invalid value */
234
235 client_get_all(self);
236 client_restore_session_state(self);
237
238 sn_app_started(self->class);
239
240 client_change_state(self);
241
242 /* remove the client's border (and adjust re gravity) */
243 client_toggle_border(self, FALSE);
244
245 /* specify that if we exit, the window should not be destroyed and should
246 be reparented back to root automatically */
247 XChangeSaveSet(ob_display, window, SetModeInsert);
248
249 /* create the decoration frame for the client window */
250 self->frame = frame_new();
251
252 frame_grab_client(self->frame, self);
253
254 grab_server(FALSE);
255
256 client_apply_startup_state(self);
257
258 /* update the focus lists */
259 focus_order_add_new(self);
260
261 stacking_add(CLIENT_AS_WINDOW(self));
262 client_restore_session_stacking(self);
263
264 /* focus the new window? */
265 if (ob_state() != OB_STATE_STARTING && config_focus_new &&
266 /* note the check against Type_Normal/Dialog, not client_normal(self),
267 which would also include other types. in this case we want more
268 strict rules for focus */
269 (self->type == OB_CLIENT_TYPE_NORMAL ||
270 self->type == OB_CLIENT_TYPE_DIALOG))
271 {
272 activate = TRUE;
273 #if 0
274 if (self->desktop != screen_desktop) {
275 /* activate the window */
276 activate = TRUE;
277 } else {
278 gboolean group_foc = FALSE;
279
280 if (self->group) {
281 GSList *it;
282
283 for (it = self->group->members; it; it = it->next)
284 {
285 if (client_focused(it->data))
286 {
287 group_foc = TRUE;
288 break;
289 }
290 }
291 }
292 if ((group_foc ||
293 (!self->transient_for && (!self->group ||
294 !self->group->members->next))) ||
295 client_search_focus_tree_full(self) ||
296 !focus_client ||
297 !client_normal(focus_client))
298 {
299 /* activate the window */
300 activate = TRUE;
301 }
302 }
303 #endif
304 }
305
306 if (ob_state() == OB_STATE_RUNNING) {
307 int x = self->area.x, ox = x;
308 int y = self->area.y, oy = y;
309
310 place_client(self, &x, &y);
311
312 /* make sure the window is visible */
313 client_find_onscreen(self, &x, &y,
314 self->frame->area.width,
315 self->frame->area.height,
316 client_normal(self));
317
318 if (x != ox || y != oy)
319 client_move(self, x, y);
320 }
321
322 client_showhide(self);
323
324 /* use client_focus instead of client_activate cuz client_activate does
325 stuff like switch desktops etc and I'm not interested in all that when
326 a window maps since its not based on an action from the user like
327 clicking a window to activate is. so keep the new window out of the way
328 but do focus it. */
329 if (activate) client_focus(self);
330
331 /* client_activate does this but we aret using it so we have to do it
332 here as well */
333 if (screen_showing_desktop)
334 screen_show_desktop(FALSE);
335
336 /* add to client list/map */
337 client_list = g_list_append(client_list, self);
338 g_hash_table_insert(window_map, &self->window, self);
339
340 /* this has to happen after we're in the client_list */
341 screen_update_areas();
342
343 /* update the list hints */
344 client_set_list();
345
346 keyboard_grab_for_client(self, TRUE);
347 mouse_grab_for_client(self, TRUE);
348
349 ob_debug("Managed window 0x%lx (%s)\n", window, self->class);
350 }
351
352 void client_unmanage_all()
353 {
354 while (client_list != NULL)
355 client_unmanage(client_list->data);
356 }
357
358 void client_unmanage(ObClient *self)
359 {
360 guint j;
361 GSList *it;
362
363 ob_debug("Unmanaging window: %lx (%s)\n", self->window, self->class);
364
365 g_assert(self != NULL);
366
367 keyboard_grab_for_client(self, FALSE);
368 mouse_grab_for_client(self, FALSE);
369
370 /* remove the window from our save set */
371 XChangeSaveSet(ob_display, self->window, SetModeDelete);
372
373 /* we dont want events no more */
374 XSelectInput(ob_display, self->window, NoEventMask);
375
376 frame_hide(self->frame);
377
378 client_list = g_list_remove(client_list, self);
379 stacking_remove(self);
380 g_hash_table_remove(window_map, &self->window);
381
382 /* update the focus lists */
383 focus_order_remove(self);
384
385 /* once the client is out of the list, update the struts to remove it's
386 influence */
387 screen_update_areas();
388
389 /* tell our parent(s) that we're gone */
390 if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
391 GSList *it;
392
393 for (it = self->group->members; it; it = it->next)
394 if (it->data != self)
395 ((ObClient*)it->data)->transients =
396 g_slist_remove(((ObClient*)it->data)->transients, self);
397 } else if (self->transient_for) { /* transient of window */
398 self->transient_for->transients =
399 g_slist_remove(self->transient_for->transients, self);
400 }
401
402 /* tell our transients that we're gone */
403 for (it = self->transients; it != NULL; it = it->next) {
404 if (((ObClient*)it->data)->transient_for != OB_TRAN_GROUP) {
405 ((ObClient*)it->data)->transient_for = NULL;
406 client_calc_layer(it->data);
407 }
408 }
409
410 for (it = client_destructors; it; it = g_slist_next(it)) {
411 GDestroyNotify func = (GDestroyNotify) it->data;
412 func(self);
413 }
414
415 if (focus_client == self) {
416 XEvent e;
417
418 /* focus the last focused window on the desktop, and ignore enter
419 events from the unmap so it doesnt mess with the focus */
420 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
421 client_unfocus(self);
422 }
423
424 /* remove from its group */
425 if (self->group) {
426 group_remove(self->group, self);
427 self->group = NULL;
428 }
429
430 /* give the client its border back */
431 client_toggle_border(self, TRUE);
432
433 /* reparent the window out of the frame, and free the frame */
434 frame_release_client(self->frame, self);
435 self->frame = NULL;
436
437 if (ob_state() != OB_STATE_EXITING) {
438 /* these values should not be persisted across a window
439 unmapping/mapping */
440 PROP_ERASE(self->window, net_wm_desktop);
441 PROP_ERASE(self->window, net_wm_state);
442 PROP_ERASE(self->window, wm_state);
443 } else {
444 /* if we're left in an iconic state, the client wont be mapped. this is
445 bad, since we will no longer be managing the window on restart */
446 if (self->iconic)
447 XMapWindow(ob_display, self->window);
448 }
449
450
451 ob_debug("Unmanaged window 0x%lx\n", self->window);
452
453 /* free all data allocated in the client struct */
454 g_slist_free(self->transients);
455 for (j = 0; j < self->nicons; ++j)
456 g_free(self->icons[j].data);
457 if (self->nicons > 0)
458 g_free(self->icons);
459 g_free(self->title);
460 g_free(self->icon_title);
461 g_free(self->name);
462 g_free(self->class);
463 g_free(self->role);
464 g_free(self->sm_client_id);
465 g_free(self);
466
467 /* update the list hints */
468 client_set_list();
469 }
470
471 static void client_urgent_notify(ObClient *self)
472 {
473 if (self->urgent)
474 frame_flash_start(self->frame);
475 else
476 frame_flash_stop(self->frame);
477 }
478
479 static void client_restore_session_state(ObClient *self)
480 {
481 GList *it;
482
483 if (!(it = session_state_find(self)))
484 return;
485
486 self->session = it->data;
487
488 RECT_SET(self->area, self->session->x, self->session->y,
489 self->session->w, self->session->h);
490 self->positioned = TRUE;
491 XResizeWindow(ob_display, self->window,
492 self->session->w, self->session->h);
493
494 self->desktop = (self->session->desktop == DESKTOP_ALL ?
495 self->session->desktop :
496 MIN(screen_num_desktops - 1, self->session->desktop));
497 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
498
499 self->shaded = self->session->shaded;
500 self->iconic = self->session->iconic;
501 self->skip_pager = self->session->skip_pager;
502 self->skip_taskbar = self->session->skip_taskbar;
503 self->fullscreen = self->session->fullscreen;
504 self->above = self->session->above;
505 self->below = self->session->below;
506 self->max_horz = self->session->max_horz;
507 self->max_vert = self->session->max_vert;
508 }
509
510 static void client_restore_session_stacking(ObClient *self)
511 {
512 GList *it;
513
514 if (!self->session) return;
515
516 it = g_list_find(session_saved_state, self->session);
517 for (it = g_list_previous(it); it; it = g_list_previous(it)) {
518 GList *cit;
519
520 for (cit = client_list; cit; cit = g_list_next(cit))
521 if (session_state_cmp(it->data, cit->data))
522 break;
523 if (cit) {
524 client_calc_layer(self);
525 stacking_below(CLIENT_AS_WINDOW(self),
526 CLIENT_AS_WINDOW(cit->data));
527 break;
528 }
529 }
530 }
531
532 void client_move_onscreen(ObClient *self, gboolean rude)
533 {
534 int x = self->area.x;
535 int y = self->area.y;
536 if (client_find_onscreen(self, &x, &y,
537 self->frame->area.width,
538 self->frame->area.height, rude)) {
539 client_move(self, x, y);
540 }
541 }
542
543 gboolean client_find_onscreen(ObClient *self, int *x, int *y, int w, int h,
544 gboolean rude)
545 {
546 Rect *a;
547 int ox = *x, oy = *y;
548
549 frame_client_gravity(self->frame, x, y); /* get where the frame
550 would be */
551
552 /* XXX watch for xinerama dead areas */
553
554 a = screen_area(self->desktop);
555 if (client_normal(self)) {
556 if (!self->strut.right && *x >= a->x + a->width - 1)
557 *x = a->x + a->width - self->frame->area.width;
558 if (!self->strut.bottom && *y >= a->y + a->height - 1)
559 *y = a->y + a->height - self->frame->area.height;
560 if (!self->strut.left && *x + self->frame->area.width - 1 < a->x)
561 *x = a->x;
562 if (!self->strut.top && *y + self->frame->area.height - 1 < a->y)
563 *y = a->y;
564 }
565
566 if (rude) {
567 /* this is my MOZILLA BITCHSLAP. oh ya it fucking feels good.
568 Java can suck it too. */
569
570 /* dont let windows map/move into the strut unless they
571 are bigger than the available area */
572 if (w <= a->width) {
573 if (!self->strut.left && *x < a->x) *x = a->x;
574 if (!self->strut.right && *x + w > a->x + a->width)
575 *x = a->x + a->width - w;
576 }
577 if (h <= a->height) {
578 if (!self->strut.top && *y < a->y) *y = a->y;
579 if (!self->strut.bottom && *y + h > a->y + a->height)
580 *y = a->y + a->height - h;
581 }
582 }
583
584 frame_frame_gravity(self->frame, x, y); /* get where the client
585 should be */
586
587 return ox != *x || oy != *y;
588 }
589
590 static void client_toggle_border(ObClient *self, gboolean show)
591 {
592 /* adjust our idea of where the client is, based on its border. When the
593 border is removed, the client should now be considered to be in a
594 different position.
595 when re-adding the border to the client, the same operation needs to be
596 reversed. */
597 int oldx = self->area.x, oldy = self->area.y;
598 int x = oldx, y = oldy;
599 switch(self->gravity) {
600 default:
601 case NorthWestGravity:
602 case WestGravity:
603 case SouthWestGravity:
604 break;
605 case NorthEastGravity:
606 case EastGravity:
607 case SouthEastGravity:
608 if (show) x -= self->border_width * 2;
609 else x += self->border_width * 2;
610 break;
611 case NorthGravity:
612 case SouthGravity:
613 case CenterGravity:
614 case ForgetGravity:
615 case StaticGravity:
616 if (show) x -= self->border_width;
617 else x += self->border_width;
618 break;
619 }
620 switch(self->gravity) {
621 default:
622 case NorthWestGravity:
623 case NorthGravity:
624 case NorthEastGravity:
625 break;
626 case SouthWestGravity:
627 case SouthGravity:
628 case SouthEastGravity:
629 if (show) y -= self->border_width * 2;
630 else y += self->border_width * 2;
631 break;
632 case WestGravity:
633 case EastGravity:
634 case CenterGravity:
635 case ForgetGravity:
636 case StaticGravity:
637 if (show) y -= self->border_width;
638 else y += self->border_width;
639 break;
640 }
641 self->area.x = x;
642 self->area.y = y;
643
644 if (show) {
645 XSetWindowBorderWidth(ob_display, self->window, self->border_width);
646
647 /* move the client so it is back it the right spot _with_ its
648 border! */
649 if (x != oldx || y != oldy)
650 XMoveWindow(ob_display, self->window, x, y);
651 } else
652 XSetWindowBorderWidth(ob_display, self->window, 0);
653 }
654
655
656 static void client_get_all(ObClient *self)
657 {
658 client_get_area(self);
659 client_update_transient_for(self);
660 client_update_wmhints(self);
661 client_get_startup_id(self);
662 client_get_desktop(self);
663 client_get_state(self);
664 client_get_shaped(self);
665
666 client_get_mwm_hints(self);
667 client_get_type(self);/* this can change the mwmhints for special cases */
668
669 client_update_protocols(self);
670
671 client_get_gravity(self); /* get the attribute gravity */
672 client_update_normal_hints(self); /* this may override the attribute
673 gravity */
674
675 /* got the type, the mwmhints, the protocols, and the normal hints
676 (min/max sizes), so we're ready to set up the decorations/functions */
677 client_setup_decor_and_functions(self);
678
679 client_update_title(self);
680 client_update_class(self);
681 client_update_sm_client_id(self);
682 client_update_strut(self);
683 client_update_icons(self);
684 }
685
686 static void client_get_startup_id(ObClient *self)
687 {
688 if (!(PROP_GETS(self->window, net_startup_id, utf8, &self->startup_id)))
689 if (self->group)
690 PROP_GETS(self->group->leader,
691 net_startup_id, utf8, &self->startup_id);
692 }
693
694 static void client_get_area(ObClient *self)
695 {
696 XWindowAttributes wattrib;
697 Status ret;
698
699 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
700 g_assert(ret != BadWindow);
701
702 RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
703 self->border_width = wattrib.border_width;
704 }
705
706 static void client_get_desktop(ObClient *self)
707 {
708 guint32 d = screen_num_desktops; /* an always-invalid value */
709
710 if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
711 if (d >= screen_num_desktops && d != DESKTOP_ALL)
712 self->desktop = screen_num_desktops - 1;
713 else
714 self->desktop = d;
715 } else {
716 gboolean trdesk = FALSE;
717
718 if (self->transient_for) {
719 if (self->transient_for != OB_TRAN_GROUP) {
720 self->desktop = self->transient_for->desktop;
721 trdesk = TRUE;
722 } else {
723 GSList *it;
724
725 for (it = self->group->members; it; it = it->next)
726 if (it->data != self &&
727 !((ObClient*)it->data)->transient_for) {
728 self->desktop = ((ObClient*)it->data)->desktop;
729 trdesk = TRUE;
730 break;
731 }
732 }
733 }
734 if (!trdesk) {
735 /* try get from the startup-notification protocol */
736 if (sn_get_desktop(self->startup_id, &self->desktop)) {
737 if (self->desktop >= screen_num_desktops &&
738 self->desktop != DESKTOP_ALL)
739 self->desktop = screen_num_desktops - 1;
740 } else
741 /* defaults to the current desktop */
742 self->desktop = screen_desktop;
743 }
744 }
745 if (self->desktop != d) {
746 /* set the desktop hint, to make sure that it always exists */
747 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
748 }
749 }
750
751 static void client_get_state(ObClient *self)
752 {
753 guint32 *state;
754 guint num;
755
756 if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
757 gulong i;
758 for (i = 0; i < num; ++i) {
759 if (state[i] == prop_atoms.net_wm_state_modal)
760 self->modal = TRUE;
761 else if (state[i] == prop_atoms.net_wm_state_shaded)
762 self->shaded = TRUE;
763 else if (state[i] == prop_atoms.net_wm_state_hidden)
764 self->iconic = TRUE;
765 else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
766 self->skip_taskbar = TRUE;
767 else if (state[i] == prop_atoms.net_wm_state_skip_pager)
768 self->skip_pager = TRUE;
769 else if (state[i] == prop_atoms.net_wm_state_fullscreen)
770 self->fullscreen = TRUE;
771 else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
772 self->max_vert = TRUE;
773 else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
774 self->max_horz = TRUE;
775 else if (state[i] == prop_atoms.net_wm_state_above)
776 self->above = TRUE;
777 else if (state[i] == prop_atoms.net_wm_state_below)
778 self->below = TRUE;
779 }
780
781 g_free(state);
782 }
783 }
784
785 static void client_get_shaped(ObClient *self)
786 {
787 self->shaped = FALSE;
788 #ifdef SHAPE
789 if (extensions_shape) {
790 int foo;
791 guint ufoo;
792 int s;
793
794 XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
795
796 XShapeQueryExtents(ob_display, self->window, &s, &foo,
797 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
798 &ufoo);
799 self->shaped = (s != 0);
800 }
801 #endif
802 }
803
804 void client_update_transient_for(ObClient *self)
805 {
806 Window t = None;
807 ObClient *target = NULL;
808
809 if (XGetTransientForHint(ob_display, self->window, &t)) {
810 self->transient = TRUE;
811 if (t != self->window) { /* cant be transient to itself! */
812 target = g_hash_table_lookup(window_map, &t);
813 /* if this happens then we need to check for it*/
814 g_assert(target != self);
815 if (target && !WINDOW_IS_CLIENT(target)) {
816 /* this can happen when a dialog is a child of
817 a dockapp, for example */
818 target = NULL;
819 }
820
821 if (!target && self->group) {
822 /* not transient to a client, see if it is transient for a
823 group */
824 if (t == self->group->leader ||
825 t == None ||
826 t == RootWindow(ob_display, ob_screen)) {
827 /* window is a transient for its group! */
828 target = OB_TRAN_GROUP;
829 }
830 }
831 }
832 } else
833 self->transient = FALSE;
834
835 /* if anything has changed... */
836 if (target != self->transient_for) {
837 if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
838 GSList *it;
839
840 /* remove from old parents */
841 for (it = self->group->members; it; it = g_slist_next(it)) {
842 ObClient *c = it->data;
843 if (c != self && !c->transient_for)
844 c->transients = g_slist_remove(c->transients, self);
845 }
846 } else if (self->transient_for != NULL) { /* transient of window */
847 /* remove from old parent */
848 self->transient_for->transients =
849 g_slist_remove(self->transient_for->transients, self);
850 }
851 self->transient_for = target;
852 if (self->transient_for == OB_TRAN_GROUP) { /* transient of group */
853 GSList *it;
854
855 /* add to new parents */
856 for (it = self->group->members; it; it = g_slist_next(it)) {
857 ObClient *c = it->data;
858 if (c != self && !c->transient_for)
859 c->transients = g_slist_append(c->transients, self);
860 }
861
862 /* remove all transients which are in the group, that causes
863 circlular pointer hell of doom */
864 for (it = self->group->members; it; it = g_slist_next(it)) {
865 GSList *sit, *next;
866 for (sit = self->transients; sit; sit = next) {
867 next = g_slist_next(sit);
868 if (sit->data == it->data)
869 self->transients =
870 g_slist_delete_link(self->transients, sit);
871 }
872 }
873 } else if (self->transient_for != NULL) { /* transient of window */
874 /* add to new parent */
875 self->transient_for->transients =
876 g_slist_append(self->transient_for->transients, self);
877 }
878 }
879 }
880
881 static void client_get_mwm_hints(ObClient *self)
882 {
883 guint num;
884 guint32 *hints;
885
886 self->mwmhints.flags = 0; /* default to none */
887
888 if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
889 &hints, &num)) {
890 if (num >= OB_MWM_ELEMENTS) {
891 self->mwmhints.flags = hints[0];
892 self->mwmhints.functions = hints[1];
893 self->mwmhints.decorations = hints[2];
894 }
895 g_free(hints);
896 }
897 }
898
899 void client_get_type(ObClient *self)
900 {
901 guint num, i;
902 guint32 *val;
903
904 self->type = -1;
905
906 if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
907 /* use the first value that we know about in the array */
908 for (i = 0; i < num; ++i) {
909 if (val[i] == prop_atoms.net_wm_window_type_desktop)
910 self->type = OB_CLIENT_TYPE_DESKTOP;
911 else if (val[i] == prop_atoms.net_wm_window_type_dock)
912 self->type = OB_CLIENT_TYPE_DOCK;
913 else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
914 self->type = OB_CLIENT_TYPE_TOOLBAR;
915 else if (val[i] == prop_atoms.net_wm_window_type_menu)
916 self->type = OB_CLIENT_TYPE_MENU;
917 else if (val[i] == prop_atoms.net_wm_window_type_utility)
918 self->type = OB_CLIENT_TYPE_UTILITY;
919 else if (val[i] == prop_atoms.net_wm_window_type_splash)
920 self->type = OB_CLIENT_TYPE_SPLASH;
921 else if (val[i] == prop_atoms.net_wm_window_type_dialog)
922 self->type = OB_CLIENT_TYPE_DIALOG;
923 else if (val[i] == prop_atoms.net_wm_window_type_normal)
924 self->type = OB_CLIENT_TYPE_NORMAL;
925 else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
926 /* prevent this window from getting any decor or
927 functionality */
928 self->mwmhints.flags &= (OB_MWM_FLAG_FUNCTIONS |
929 OB_MWM_FLAG_DECORATIONS);
930 self->mwmhints.decorations = 0;
931 self->mwmhints.functions = 0;
932 }
933 if (self->type != (ObClientType) -1)
934 break; /* grab the first legit type */
935 }
936 g_free(val);
937 }
938
939 if (self->type == (ObClientType) -1) {
940 /*the window type hint was not set, which means we either classify
941 ourself as a normal window or a dialog, depending on if we are a
942 transient. */
943 if (self->transient)
944 self->type = OB_CLIENT_TYPE_DIALOG;
945 else
946 self->type = OB_CLIENT_TYPE_NORMAL;
947 }
948 }
949
950 void client_update_protocols(ObClient *self)
951 {
952 guint32 *proto;
953 guint num_return, i;
954
955 self->focus_notify = FALSE;
956 self->delete_window = FALSE;
957
958 if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
959 for (i = 0; i < num_return; ++i) {
960 if (proto[i] == prop_atoms.wm_delete_window) {
961 /* this means we can request the window to close */
962 self->delete_window = TRUE;
963 } else if (proto[i] == prop_atoms.wm_take_focus)
964 /* if this protocol is requested, then the window will be
965 notified whenever we want it to receive focus */
966 self->focus_notify = TRUE;
967 }
968 g_free(proto);
969 }
970 }
971
972 static void client_get_gravity(ObClient *self)
973 {
974 XWindowAttributes wattrib;
975 Status ret;
976
977 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
978 g_assert(ret != BadWindow);
979 self->gravity = wattrib.win_gravity;
980 }
981
982 void client_update_normal_hints(ObClient *self)
983 {
984 XSizeHints size;
985 long ret;
986 int oldgravity = self->gravity;
987
988 /* defaults */
989 self->min_ratio = 0.0f;
990 self->max_ratio = 0.0f;
991 SIZE_SET(self->size_inc, 1, 1);
992 SIZE_SET(self->base_size, 0, 0);
993 SIZE_SET(self->min_size, 0, 0);
994 SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
995
996 /* get the hints from the window */
997 if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
998 self->positioned = !!(size.flags & (PPosition|USPosition));
999
1000 if (size.flags & PWinGravity) {
1001 self->gravity = size.win_gravity;
1002
1003 /* if the client has a frame, i.e. has already been mapped and
1004 is changing its gravity */
1005 if (self->frame && self->gravity != oldgravity) {
1006 /* move our idea of the client's position based on its new
1007 gravity */
1008 self->area.x = self->frame->area.x;
1009 self->area.y = self->frame->area.y;
1010 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
1011 }
1012 }
1013
1014 if (size.flags & PAspect) {
1015 if (size.min_aspect.y)
1016 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
1017 if (size.max_aspect.y)
1018 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
1019 }
1020
1021 if (size.flags & PMinSize)
1022 SIZE_SET(self->min_size, size.min_width, size.min_height);
1023
1024 if (size.flags & PMaxSize)
1025 SIZE_SET(self->max_size, size.max_width, size.max_height);
1026
1027 if (size.flags & PBaseSize)
1028 SIZE_SET(self->base_size, size.base_width, size.base_height);
1029
1030 if (size.flags & PResizeInc)
1031 SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
1032 }
1033 }
1034
1035 void client_setup_decor_and_functions(ObClient *self)
1036 {
1037 /* start with everything (cept fullscreen) */
1038 self->decorations =
1039 (OB_FRAME_DECOR_TITLEBAR |
1040 (ob_rr_theme->show_handle ? OB_FRAME_DECOR_HANDLE : 0) |
1041 OB_FRAME_DECOR_GRIPS |
1042 OB_FRAME_DECOR_BORDER |
1043 OB_FRAME_DECOR_ICON |
1044 OB_FRAME_DECOR_ALLDESKTOPS |
1045 OB_FRAME_DECOR_ICONIFY |
1046 OB_FRAME_DECOR_MAXIMIZE |
1047 OB_FRAME_DECOR_SHADE);
1048 self->functions =
1049 (OB_CLIENT_FUNC_RESIZE |
1050 OB_CLIENT_FUNC_MOVE |
1051 OB_CLIENT_FUNC_ICONIFY |
1052 OB_CLIENT_FUNC_MAXIMIZE |
1053 OB_CLIENT_FUNC_SHADE);
1054 if (self->delete_window) {
1055 self->functions |= OB_CLIENT_FUNC_CLOSE;
1056 self->decorations |= OB_FRAME_DECOR_CLOSE;
1057 }
1058
1059 if (!(self->min_size.width < self->max_size.width ||
1060 self->min_size.height < self->max_size.height))
1061 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1062
1063 switch (self->type) {
1064 case OB_CLIENT_TYPE_NORMAL:
1065 /* normal windows retain all of the possible decorations and
1066 functionality, and are the only windows that you can fullscreen */
1067 self->functions |= OB_CLIENT_FUNC_FULLSCREEN;
1068 break;
1069
1070 case OB_CLIENT_TYPE_DIALOG:
1071 case OB_CLIENT_TYPE_UTILITY:
1072 /* these windows cannot be maximized */
1073 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1074 break;
1075
1076 case OB_CLIENT_TYPE_MENU:
1077 case OB_CLIENT_TYPE_TOOLBAR:
1078 /* these windows get less functionality */
1079 self->functions &= ~(OB_CLIENT_FUNC_ICONIFY | OB_CLIENT_FUNC_RESIZE);
1080 break;
1081
1082 case OB_CLIENT_TYPE_DESKTOP:
1083 case OB_CLIENT_TYPE_DOCK:
1084 case OB_CLIENT_TYPE_SPLASH:
1085 /* none of these windows are manipulated by the window manager */
1086 self->decorations = 0;
1087 self->functions = 0;
1088 break;
1089 }
1090
1091 /* Mwm Hints are applied subtractively to what has already been chosen for
1092 decor and functionality */
1093 if (self->mwmhints.flags & OB_MWM_FLAG_DECORATIONS) {
1094 if (! (self->mwmhints.decorations & OB_MWM_DECOR_ALL)) {
1095 if (! ((self->mwmhints.decorations & OB_MWM_DECOR_HANDLE) ||
1096 (self->mwmhints.decorations & OB_MWM_DECOR_TITLE)))
1097 /* if the mwm hints request no handle or title, then all
1098 decorations are disabled */
1099 self->decorations = 0;
1100 }
1101 }
1102
1103 if (self->mwmhints.flags & OB_MWM_FLAG_FUNCTIONS) {
1104 if (! (self->mwmhints.functions & OB_MWM_FUNC_ALL)) {
1105 if (! (self->mwmhints.functions & OB_MWM_FUNC_RESIZE))
1106 self->functions &= ~OB_CLIENT_FUNC_RESIZE;
1107 if (! (self->mwmhints.functions & OB_MWM_FUNC_MOVE))
1108 self->functions &= ~OB_CLIENT_FUNC_MOVE;
1109 /* dont let mwm hints kill any buttons
1110 if (! (self->mwmhints.functions & OB_MWM_FUNC_ICONIFY))
1111 self->functions &= ~OB_CLIENT_FUNC_ICONIFY;
1112 if (! (self->mwmhints.functions & OB_MWM_FUNC_MAXIMIZE))
1113 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1114 */
1115 /* dont let mwm hints kill the close button
1116 if (! (self->mwmhints.functions & MwmFunc_Close))
1117 self->functions &= ~OB_CLIENT_FUNC_CLOSE; */
1118 }
1119 }
1120
1121 if (!(self->functions & OB_CLIENT_FUNC_SHADE))
1122 self->decorations &= ~OB_FRAME_DECOR_SHADE;
1123 if (!(self->functions & OB_CLIENT_FUNC_ICONIFY))
1124 self->decorations &= ~OB_FRAME_DECOR_ICONIFY;
1125 if (!(self->functions & OB_CLIENT_FUNC_RESIZE))
1126 self->decorations &= ~OB_FRAME_DECOR_GRIPS;
1127
1128 /* can't maximize without moving/resizing */
1129 if (!((self->functions & OB_CLIENT_FUNC_MAXIMIZE) &&
1130 (self->functions & OB_CLIENT_FUNC_MOVE) &&
1131 (self->functions & OB_CLIENT_FUNC_RESIZE))) {
1132 self->functions &= ~OB_CLIENT_FUNC_MAXIMIZE;
1133 self->decorations &= ~OB_FRAME_DECOR_MAXIMIZE;
1134 }
1135
1136 /* kill the handle on fully maxed windows */
1137 if (self->max_vert && self->max_horz)
1138 self->decorations &= ~OB_FRAME_DECOR_HANDLE;
1139
1140 /* finally, the user can have requested no decorations, which overrides
1141 everything */
1142 if (!self->decorate)
1143 self->decorations = OB_FRAME_DECOR_BORDER;
1144
1145 /* if we don't have a titlebar, then we cannot shade! */
1146 if (!(self->decorations & OB_FRAME_DECOR_TITLEBAR))
1147 self->functions &= ~OB_CLIENT_FUNC_SHADE;
1148
1149 /* now we need to check against rules for the client's current state */
1150 if (self->fullscreen) {
1151 self->functions &= (OB_CLIENT_FUNC_CLOSE |
1152 OB_CLIENT_FUNC_FULLSCREEN |
1153 OB_CLIENT_FUNC_ICONIFY);
1154 self->decorations = 0;
1155 }
1156
1157 client_change_allowed_actions(self);
1158
1159 if (self->frame) {
1160 /* adjust the client's decorations, etc. */
1161 client_reconfigure(self);
1162 } else {
1163 /* this makes sure that these windows appear on all desktops */
1164 if (self->type == OB_CLIENT_TYPE_DESKTOP &&
1165 self->desktop != DESKTOP_ALL)
1166 {
1167 self->desktop = DESKTOP_ALL;
1168 }
1169 }
1170 }
1171
1172 static void client_change_allowed_actions(ObClient *self)
1173 {
1174 guint32 actions[9];
1175 int num = 0;
1176
1177 /* desktop windows are kept on all desktops */
1178 if (self->type != OB_CLIENT_TYPE_DESKTOP)
1179 actions[num++] = prop_atoms.net_wm_action_change_desktop;
1180
1181 if (self->functions & OB_CLIENT_FUNC_SHADE)
1182 actions[num++] = prop_atoms.net_wm_action_shade;
1183 if (self->functions & OB_CLIENT_FUNC_CLOSE)
1184 actions[num++] = prop_atoms.net_wm_action_close;
1185 if (self->functions & OB_CLIENT_FUNC_MOVE)
1186 actions[num++] = prop_atoms.net_wm_action_move;
1187 if (self->functions & OB_CLIENT_FUNC_ICONIFY)
1188 actions[num++] = prop_atoms.net_wm_action_minimize;
1189 if (self->functions & OB_CLIENT_FUNC_RESIZE)
1190 actions[num++] = prop_atoms.net_wm_action_resize;
1191 if (self->functions & OB_CLIENT_FUNC_FULLSCREEN)
1192 actions[num++] = prop_atoms.net_wm_action_fullscreen;
1193 if (self->functions & OB_CLIENT_FUNC_MAXIMIZE) {
1194 actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1195 actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1196 }
1197
1198 PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1199
1200 /* make sure the window isn't breaking any rules now */
1201
1202 if (!(self->functions & OB_CLIENT_FUNC_SHADE) && self->shaded) {
1203 if (self->frame) client_shade(self, FALSE);
1204 else self->shaded = FALSE;
1205 }
1206 if (!(self->functions & OB_CLIENT_FUNC_ICONIFY) && self->iconic) {
1207 if (self->frame) client_iconify(self, FALSE, TRUE);
1208 else self->iconic = FALSE;
1209 }
1210 if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) && self->fullscreen) {
1211 if (self->frame) client_fullscreen(self, FALSE, TRUE);
1212 else self->fullscreen = FALSE;
1213 }
1214 if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE) && (self->max_horz ||
1215 self->max_vert)) {
1216 if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1217 else self->max_vert = self->max_horz = FALSE;
1218 }
1219 }
1220
1221 void client_reconfigure(ObClient *self)
1222 {
1223 /* by making this pass FALSE for user, we avoid the emacs event storm where
1224 every configurenotify causes an update in its normal hints, i think this
1225 is generally what we want anyways... */
1226 client_configure(self, OB_CORNER_TOPLEFT, self->area.x, self->area.y,
1227 self->area.width, self->area.height, FALSE, TRUE);
1228 }
1229
1230 void client_update_wmhints(ObClient *self)
1231 {
1232 XWMHints *hints;
1233 gboolean ur = FALSE;
1234 GSList *it;
1235
1236 /* assume a window takes input if it doesnt specify */
1237 self->can_focus = TRUE;
1238
1239 if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1240 if (hints->flags & InputHint)
1241 self->can_focus = hints->input;
1242
1243 /* only do this when first managing the window *AND* when we aren't
1244 starting up! */
1245 if (ob_state() != OB_STATE_STARTING && self->frame == NULL)
1246 if (hints->flags & StateHint)
1247 self->iconic = hints->initial_state == IconicState;
1248
1249 if (hints->flags & XUrgencyHint)
1250 ur = TRUE;
1251
1252 if (!(hints->flags & WindowGroupHint))
1253 hints->window_group = None;
1254
1255 /* did the group state change? */
1256 if (hints->window_group !=
1257 (self->group ? self->group->leader : None)) {
1258 /* remove from the old group if there was one */
1259 if (self->group != NULL) {
1260 /* remove transients of the group */
1261 for (it = self->group->members; it; it = it->next)
1262 self->transients = g_slist_remove(self->transients,
1263 it->data);
1264 group_remove(self->group, self);
1265 self->group = NULL;
1266 }
1267 if (hints->window_group != None) {
1268 self->group = group_add(hints->window_group, self);
1269
1270 /* i can only have transients from the group if i am not
1271 transient myself */
1272 if (!self->transient_for) {
1273 /* add other transients of the group that are already
1274 set up */
1275 for (it = self->group->members; it; it = it->next) {
1276 ObClient *c = it->data;
1277 if (c != self && c->transient_for == OB_TRAN_GROUP)
1278 self->transients =
1279 g_slist_append(self->transients, c);
1280 }
1281 }
1282 }
1283
1284 /* because the self->transient flag wont change from this call,
1285 we don't need to update the window's type and such, only its
1286 transient_for, and the transients lists of other windows in
1287 the group may be affected */
1288 client_update_transient_for(self);
1289 }
1290
1291 /* the WM_HINTS can contain an icon */
1292 client_update_icons(self);
1293
1294 XFree(hints);
1295 }
1296
1297 if (ur != self->urgent) {
1298 self->urgent = ur;
1299 ob_debug("Urgent Hint for 0x%lx: %s\n", self->window,
1300 ur ? "ON" : "OFF");
1301 /* fire the urgent callback if we're mapped, otherwise, wait until
1302 after we're mapped */
1303 if (self->frame)
1304 client_urgent_notify(self);
1305 }
1306 }
1307
1308 void client_update_title(ObClient *self)
1309 {
1310 GList *it;
1311 guint32 nums;
1312 guint i;
1313 gchar *data = NULL;
1314 gboolean read_title;
1315 gchar *old_title;
1316
1317 old_title = self->title;
1318
1319 /* try netwm */
1320 if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1321 /* try old x stuff */
1322 if (!PROP_GETS(self->window, wm_name, locale, &data))
1323 data = g_strdup("Unnamed Window");
1324
1325 /* did the title change? then reset the title_count */
1326 if (old_title && 0 != strncmp(old_title, data, strlen(data)))
1327 self->title_count = 1;
1328
1329 /* look for duplicates and append a number */
1330 nums = 0;
1331 for (it = client_list; it; it = it->next)
1332 if (it->data != self) {
1333 ObClient *c = it->data;
1334 if (0 == strncmp(c->title, data, strlen(data)))
1335 nums |= 1 << c->title_count;
1336 }
1337 /* find first free number */
1338 for (i = 1; i <= 32; ++i)
1339 if (!(nums & (1 << i))) {
1340 if (self->title_count == 1 || i == 1)
1341 self->title_count = i;
1342 break;
1343 }
1344 /* dont display the number for the first window */
1345 if (self->title_count > 1) {
1346 char *ndata;
1347 ndata = g_strdup_printf("%s - [%u]", data, self->title_count);
1348 g_free(data);
1349 data = ndata;
1350 }
1351
1352 PROP_SETS(self->window, net_wm_visible_name, data);
1353
1354 self->title = data;
1355
1356 if (self->frame)
1357 frame_adjust_title(self->frame);
1358
1359 g_free(old_title);
1360
1361 /* update the icon title */
1362 data = NULL;
1363 g_free(self->icon_title);
1364
1365 read_title = TRUE;
1366 /* try netwm */
1367 if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1368 /* try old x stuff */
1369 if (!PROP_GETS(self->window, wm_icon_name, locale, &data)) {
1370 data = g_strdup(self->title);
1371 read_title = FALSE;
1372 }
1373
1374 /* append the title count, dont display the number for the first window */
1375 if (read_title && self->title_count > 1) {
1376 char *vdata, *ndata;
1377 ndata = g_strdup_printf(" - [%u]", self->title_count);
1378 vdata = g_strconcat(data, ndata, NULL);
1379 g_free(ndata);
1380 g_free(data);
1381 data = vdata;
1382 }
1383
1384 PROP_SETS(self->window, net_wm_visible_icon_name, data);
1385
1386 self->icon_title = data;
1387 }
1388
1389 void client_update_class(ObClient *self)
1390 {
1391 char **data;
1392 char *s;
1393
1394 if (self->name) g_free(self->name);
1395 if (self->class) g_free(self->class);
1396 if (self->role) g_free(self->role);
1397
1398 self->name = self->class = self->role = NULL;
1399
1400 if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1401 if (data[0]) {
1402 self->name = g_strdup(data[0]);
1403 if (data[1])
1404 self->class = g_strdup(data[1]);
1405 }
1406 g_strfreev(data);
1407 }
1408
1409 if (PROP_GETS(self->window, wm_window_role, locale, &s))
1410 self->role = s;
1411
1412 if (self->name == NULL) self->name = g_strdup("");
1413 if (self->class == NULL) self->class = g_strdup("");
1414 if (self->role == NULL) self->role = g_strdup("");
1415 }
1416
1417 void client_update_strut(ObClient *self)
1418 {
1419 guint num;
1420 guint32 *data;
1421 gboolean got = FALSE;
1422 StrutPartial strut;
1423
1424 if (PROP_GETA32(self->window, net_wm_strut_partial, cardinal,
1425 &data, &num)) {
1426 if (num == 12) {
1427 got = TRUE;
1428 STRUT_PARTIAL_SET(strut,
1429 data[0], data[2], data[1], data[3],
1430 data[4], data[5], data[8], data[9],
1431 data[6], data[7], data[10], data[11]);
1432 }
1433 g_free(data);
1434 }
1435
1436 if (!got &&
1437 PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1438 if (num == 4) {
1439 got = TRUE;
1440 STRUT_PARTIAL_SET(strut,
1441 data[0], data[2], data[1], data[3],
1442 0, 0, 0, 0, 0, 0, 0, 0);
1443 }
1444 g_free(data);
1445 }
1446
1447 if (!got)
1448 STRUT_PARTIAL_SET(strut, 0, 0, 0, 0,
1449 0, 0, 0, 0, 0, 0, 0, 0);
1450
1451 if (!STRUT_EQUAL(strut, self->strut)) {
1452 self->strut = strut;
1453
1454 /* updating here is pointless while we're being mapped cuz we're not in
1455 the client list yet */
1456 if (self->frame)
1457 screen_update_areas();
1458 }
1459 }
1460
1461 void client_update_icons(ObClient *self)
1462 {
1463 guint num;
1464 guint32 *data;
1465 guint w, h, i, j;
1466
1467 for (i = 0; i < self->nicons; ++i)
1468 g_free(self->icons[i].data);
1469 if (self->nicons > 0)
1470 g_free(self->icons);
1471 self->nicons = 0;
1472
1473 if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1474 /* figure out how many valid icons are in here */
1475 i = 0;
1476 while (num - i > 2) {
1477 w = data[i++];
1478 h = data[i++];
1479 i += w * h;
1480 if (i > num || w*h == 0) break;
1481 ++self->nicons;
1482 }
1483
1484 self->icons = g_new(ObClientIcon, self->nicons);
1485
1486 /* store the icons */
1487 i = 0;
1488 for (j = 0; j < self->nicons; ++j) {
1489 guint x, y, t;
1490
1491 w = self->icons[j].width = data[i++];
1492 h = self->icons[j].height = data[i++];
1493
1494 if (w*h == 0) continue;
1495
1496 self->icons[j].data = g_new(RrPixel32, w * h);
1497 for (x = 0, y = 0, t = 0; t < w * h; ++t, ++x, ++i) {
1498 if (x >= w) {
1499 x = 0;
1500 ++y;
1501 }
1502 self->icons[j].data[t] =
1503 (((data[i] >> 24) & 0xff) << RrDefaultAlphaOffset) +
1504 (((data[i] >> 16) & 0xff) << RrDefaultRedOffset) +
1505 (((data[i] >> 8) & 0xff) << RrDefaultGreenOffset) +
1506 (((data[i] >> 0) & 0xff) << RrDefaultBlueOffset);
1507 }
1508 g_assert(i <= num);
1509 }
1510
1511 g_free(data);
1512 } else if (PROP_GETA32(self->window, kwm_win_icon,
1513 kwm_win_icon, &data, &num)) {
1514 if (num == 2) {
1515 self->nicons++;
1516 self->icons = g_new(ObClientIcon, self->nicons);
1517 xerror_set_ignore(TRUE);
1518 if (!RrPixmapToRGBA(ob_rr_inst,
1519 data[0], data[1],
1520 &self->icons[self->nicons-1].width,
1521 &self->icons[self->nicons-1].height,
1522 &self->icons[self->nicons-1].data)) {
1523 g_free(&self->icons[self->nicons-1]);
1524 self->nicons--;
1525 }
1526 xerror_set_ignore(FALSE);
1527 }
1528 g_free(data);
1529 } else {
1530 XWMHints *hints;
1531
1532 if ((hints = XGetWMHints(ob_display, self->window))) {
1533 if (hints->flags & IconPixmapHint) {
1534 self->nicons++;
1535 self->icons = g_new(ObClientIcon, self->nicons);
1536 xerror_set_ignore(TRUE);
1537 if (!RrPixmapToRGBA(ob_rr_inst,
1538 hints->icon_pixmap,
1539 (hints->flags & IconMaskHint ?
1540 hints->icon_mask : None),
1541 &self->icons[self->nicons-1].width,
1542 &self->icons[self->nicons-1].height,
1543 &self->icons[self->nicons-1].data)){
1544 g_free(&self->icons[self->nicons-1]);
1545 self->nicons--;
1546 }
1547 xerror_set_ignore(FALSE);
1548 }
1549 XFree(hints);
1550 }
1551 }
1552
1553 if (!self->nicons) {
1554 self->nicons++;
1555 self->icons = g_new(ObClientIcon, self->nicons);
1556 self->icons[self->nicons-1].width = 48;
1557 self->icons[self->nicons-1].height = 48;
1558 self->icons[self->nicons-1].data = g_memdup(ob_rr_theme->def_win_icon,
1559 sizeof(RrPixel32)
1560 * 48 * 48);
1561 }
1562
1563 if (self->frame)
1564 frame_adjust_icon(self->frame);
1565 }
1566
1567 static void client_change_state(ObClient *self)
1568 {
1569 guint32 state[2];
1570 guint32 netstate[10];
1571 guint num;
1572
1573 state[0] = self->wmstate;
1574 state[1] = None;
1575 PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1576
1577 num = 0;
1578 if (self->modal)
1579 netstate[num++] = prop_atoms.net_wm_state_modal;
1580 if (self->shaded)
1581 netstate[num++] = prop_atoms.net_wm_state_shaded;
1582 if (self->iconic)
1583 netstate[num++] = prop_atoms.net_wm_state_hidden;
1584 if (self->skip_taskbar)
1585 netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1586 if (self->skip_pager)
1587 netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1588 if (self->fullscreen)
1589 netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1590 if (self->max_vert)
1591 netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1592 if (self->max_horz)
1593 netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1594 if (self->above)
1595 netstate[num++] = prop_atoms.net_wm_state_above;
1596 if (self->below)
1597 netstate[num++] = prop_atoms.net_wm_state_below;
1598 PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1599
1600 client_calc_layer(self);
1601
1602 if (self->frame)
1603 frame_adjust_state(self->frame);
1604 }
1605
1606 ObClient *client_search_focus_tree(ObClient *self)
1607 {
1608 GSList *it;
1609 ObClient *ret;
1610
1611 for (it = self->transients; it != NULL; it = it->next) {
1612 if (client_focused(it->data)) return it->data;
1613 if ((ret = client_search_focus_tree(it->data))) return ret;
1614 }
1615 return NULL;
1616 }
1617
1618 ObClient *client_search_focus_tree_full(ObClient *self)
1619 {
1620 if (self->transient_for) {
1621 if (self->transient_for != OB_TRAN_GROUP) {
1622 return client_search_focus_tree_full(self->transient_for);
1623 } else {
1624 GSList *it;
1625 gboolean recursed = FALSE;
1626
1627 for (it = self->group->members; it; it = it->next)
1628 if (!((ObClient*)it->data)->transient_for) {
1629 ObClient *c;
1630 if ((c = client_search_focus_tree_full(it->data)))
1631 return c;
1632 recursed = TRUE;
1633 }
1634 if (recursed)
1635 return NULL;
1636 }
1637 }
1638
1639 /* this function checks the whole tree, the client_search_focus_tree~
1640 does not, so we need to check this window */
1641 if (client_focused(self))
1642 return self;
1643 return client_search_focus_tree(self);
1644 }
1645
1646 static ObStackingLayer calc_layer(ObClient *self)
1647 {
1648 ObStackingLayer l;
1649
1650 if (self->fullscreen &&
1651 (client_focused(self) || client_search_focus_tree(self)))
1652 l = OB_STACKING_LAYER_FULLSCREEN;
1653 else if (self->type == OB_CLIENT_TYPE_DESKTOP)
1654 l = OB_STACKING_LAYER_DESKTOP;
1655 else if (self->type == OB_CLIENT_TYPE_DOCK) {
1656 if (!self->below) l = OB_STACKING_LAYER_TOP;
1657 else l = OB_STACKING_LAYER_NORMAL;
1658 }
1659 else if (self->above) l = OB_STACKING_LAYER_ABOVE;
1660 else if (self->below) l = OB_STACKING_LAYER_BELOW;
1661 else l = OB_STACKING_LAYER_NORMAL;
1662
1663 return l;
1664 }
1665
1666 static void client_calc_layer_recursive(ObClient *self, ObClient *orig,
1667 ObStackingLayer l, gboolean raised)
1668 {
1669 ObStackingLayer old, own;
1670 GSList *it;
1671
1672 old = self->layer;
1673 own = calc_layer(self);
1674 self->layer = MAX(l, own);
1675
1676 g_message("calc for 0x%x %d %d", self->window, old, self->layer);
1677
1678 for (it = self->transients; it; it = it->next)
1679 client_calc_layer_recursive(it->data, orig,
1680 l, raised ? raised : l != old);
1681
1682 if (!raised && l != old)
1683 if (orig->frame) { /* only restack if the original window is managed */
1684 stacking_remove(CLIENT_AS_WINDOW(self));
1685 stacking_add(CLIENT_AS_WINDOW(self));
1686 }
1687 }
1688
1689 void client_calc_layer(ObClient *self)
1690 {
1691 ObStackingLayer l;
1692 ObClient *orig;
1693
1694 orig = self;
1695
1696 /* transients take on the layer of their parents */
1697 self = client_search_top_transient(self);
1698
1699 l = calc_layer(self);
1700
1701 client_calc_layer_recursive(self, orig, l, FALSE);
1702 }
1703
1704 gboolean client_should_show(ObClient *self)
1705 {
1706 if (self->iconic) return FALSE;
1707 else if (!(self->desktop == screen_desktop ||
1708 self->desktop == DESKTOP_ALL)) return FALSE;
1709 else if (client_normal(self) && screen_showing_desktop) return FALSE;
1710
1711 return TRUE;
1712 }
1713
1714 static void client_showhide(ObClient *self)
1715 {
1716
1717 if (client_should_show(self))
1718 frame_show(self->frame);
1719 else
1720 frame_hide(self->frame);
1721 }
1722
1723 gboolean client_normal(ObClient *self) {
1724 return ! (self->type == OB_CLIENT_TYPE_DESKTOP ||
1725 self->type == OB_CLIENT_TYPE_DOCK ||
1726 self->type == OB_CLIENT_TYPE_SPLASH);
1727 }
1728
1729 static void client_apply_startup_state(ObClient *self)
1730 {
1731 /* these are in a carefully crafted order.. */
1732
1733 if (self->iconic) {
1734 self->iconic = FALSE;
1735 client_iconify(self, TRUE, FALSE);
1736 }
1737 if (self->fullscreen) {
1738 self->fullscreen = FALSE;
1739 client_fullscreen(self, TRUE, FALSE);
1740 }
1741 if (self->shaded) {
1742 self->shaded = FALSE;
1743 client_shade(self, TRUE);
1744 }
1745 if (self->urgent)
1746 client_urgent_notify(self);
1747
1748 if (self->max_vert && self->max_horz) {
1749 self->max_vert = self->max_horz = FALSE;
1750 client_maximize(self, TRUE, 0, FALSE);
1751 } else if (self->max_vert) {
1752 self->max_vert = FALSE;
1753 client_maximize(self, TRUE, 2, FALSE);
1754 } else if (self->max_horz) {
1755 self->max_horz = FALSE;
1756 client_maximize(self, TRUE, 1, FALSE);
1757 }
1758
1759 /* nothing to do for the other states:
1760 skip_taskbar
1761 skip_pager
1762 modal
1763 above
1764 below
1765 */
1766 }
1767
1768 void client_configure_full(ObClient *self, ObCorner anchor,
1769 int x, int y, int w, int h,
1770 gboolean user, gboolean final,
1771 gboolean force_reply)
1772 {
1773 gint oldw, oldh;
1774 gboolean send_resize_client;
1775 gboolean moved = FALSE, resized = FALSE;
1776 guint fdecor = self->frame->decorations;
1777 gboolean fhorz = self->frame->max_horz;
1778
1779 /* make the frame recalculate its dimentions n shit without changing
1780 anything visible for real, this way the constraints below can work with
1781 the updated frame dimensions. */
1782 frame_adjust_area(self->frame, TRUE, TRUE, TRUE);
1783
1784 /* gets the frame's position */
1785 frame_client_gravity(self->frame, &x, &y);
1786
1787 /* these positions are frame positions, not client positions */
1788
1789 /* set the size and position if fullscreen */
1790 if (self->fullscreen) {
1791 #ifdef VIDMODE
1792 int dot;
1793 XF86VidModeModeLine mode;
1794 #endif
1795 Rect *a;
1796 guint i;
1797
1798 i = client_monitor(self);
1799 a = screen_physical_area_monitor(i);
1800
1801 #ifdef VIDMODE
1802 if (i == 0 && /* primary head */
1803 extensions_vidmode &&
1804 XF86VidModeGetViewPort(ob_display, ob_screen, &x, &y) &&
1805 /* get the mode last so the mode.privsize isnt freed incorrectly */
1806 XF86VidModeGetModeLine(ob_display, ob_screen, &dot, &mode)) {
1807 x += a->x;
1808 y += a->y;
1809 w = mode.hdisplay;
1810 h = mode.vdisplay;
1811 if (mode.privsize) XFree(mode.private);
1812 } else
1813 #endif
1814 {
1815 x = a->x;
1816 y = a->y;
1817 w = a->width;
1818 h = a->height;
1819 }
1820
1821 user = FALSE; /* ignore that increment etc shit when in fullscreen */
1822 } else {
1823 Rect *a;
1824
1825 a = screen_area_monitor(self->desktop, client_monitor(self));
1826
1827 /* set the size and position if maximized */
1828 if (self->max_horz) {
1829 x = a->x;
1830 w = a->width - self->frame->size.left - self->frame->size.right;
1831 }
1832 if (self->max_vert) {
1833 y = a->y;
1834 h = a->height - self->frame->size.top - self->frame->size.bottom;
1835 }
1836 }
1837
1838 /* gets the client's position */
1839 frame_frame_gravity(self->frame, &x, &y);
1840
1841 /* these override the above states! if you cant move you can't move! */
1842 if (user) {
1843 if (!(self->functions & OB_CLIENT_FUNC_MOVE)) {
1844 x = self->area.x;
1845 y = self->area.y;
1846 }
1847 if (!(self->functions & OB_CLIENT_FUNC_RESIZE)) {
1848 w = self->area.width;
1849 h = self->area.height;
1850 }
1851 }
1852
1853 if (!(w == self->area.width && h == self->area.height)) {
1854 int basew, baseh, minw, minh;
1855
1856 /* base size is substituted with min size if not specified */
1857 if (self->base_size.width || self->base_size.height) {
1858 basew = self->base_size.width;
1859 baseh = self->base_size.height;
1860 } else {
1861 basew = self->min_size.width;
1862 baseh = self->min_size.height;
1863 }
1864 /* min size is substituted with base size if not specified */
1865 if (self->min_size.width || self->min_size.height) {
1866 minw = self->min_size.width;
1867 minh = self->min_size.height;
1868 } else {
1869 minw = self->base_size.width;
1870 minh = self->base_size.height;
1871 }
1872
1873 /* if this is a user-requested resize, then check against min/max
1874 sizes */
1875
1876 /* smaller than min size or bigger than max size? */
1877 if (w > self->max_size.width) w = self->max_size.width;
1878 if (w < minw) w = minw;
1879 if (h > self->max_size.height) h = self->max_size.height;
1880 if (h < minh) h = minh;
1881
1882 w -= basew;
1883 h -= baseh;
1884
1885 /* keep to the increments */
1886 w /= self->size_inc.width;
1887 h /= self->size_inc.height;
1888
1889 /* you cannot resize to nothing */
1890 if (basew + w < 1) w = 1 - basew;
1891 if (baseh + h < 1) h = 1 - baseh;
1892
1893 /* store the logical size */
1894 SIZE_SET(self->logical_size,
1895 self->size_inc.width > 1 ? w : w + basew,
1896 self->size_inc.height > 1 ? h : h + baseh);
1897
1898 w *= self->size_inc.width;
1899 h *= self->size_inc.height;
1900
1901 w += basew;
1902 h += baseh;
1903
1904 /* adjust the height to match the width for the aspect ratios.
1905 for this, min size is not substituted for base size ever. */
1906 w -= self->base_size.width;
1907 h -= self->base_size.height;
1908
1909 if (self->min_ratio)
1910 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1911 if (self->max_ratio)
1912 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1913
1914 w += self->base_size.width;
1915 h += self->base_size.height;
1916 }
1917
1918 switch (anchor) {
1919 case OB_CORNER_TOPLEFT:
1920 break;
1921 case OB_CORNER_TOPRIGHT:
1922 x -= w - self->area.width;
1923 break;
1924 case OB_CORNER_BOTTOMLEFT:
1925 y -= h - self->area.height;
1926 break;
1927 case OB_CORNER_BOTTOMRIGHT:
1928 x -= w - self->area.width;
1929 y -= h - self->area.height;
1930 break;
1931 }
1932
1933 moved = x != self->area.x || y != self->area.y;
1934 resized = w != self->area.width || h != self->area.height;
1935
1936 oldw = self->area.width;
1937 oldh = self->area.height;
1938 RECT_SET(self->area, x, y, w, h);
1939
1940 /* for app-requested resizes, always resize if 'resized' is true.
1941 for user-requested ones, only resize if final is true, or when
1942 resizing in redraw mode */
1943 send_resize_client = ((!user && resized) ||
1944 (user && (final ||
1945 (resized && config_redraw_resize))));
1946
1947 /* if the client is enlarging, the resize the client before the frame */
1948 if (send_resize_client && (w > oldw || h > oldh))
1949 XResizeWindow(ob_display, self->window, MAX(w, oldw), MAX(h, oldh));
1950
1951 /* move/resize the frame to match the request */
1952 if (self->frame) {
1953 if (self->decorations != fdecor || self->max_horz != fhorz)
1954 moved = resized = TRUE;
1955
1956 if (moved || resized)
1957 frame_adjust_area(self->frame, moved, resized, FALSE);
1958
1959 if (!resized && (force_reply || ((!user && moved) || (user && final))))
1960 {
1961 XEvent event;
1962 event.type = ConfigureNotify;
1963 event.xconfigure.display = ob_display;
1964 event.xconfigure.event = self->window;
1965 event.xconfigure.window = self->window;
1966
1967 /* root window real coords */
1968 event.xconfigure.x = self->frame->area.x + self->frame->size.left -
1969 self->border_width;
1970 event.xconfigure.y = self->frame->area.y + self->frame->size.top -
1971 self->border_width;
1972 event.xconfigure.width = w;
1973 event.xconfigure.height = h;
1974 event.xconfigure.border_width = 0;
1975 event.xconfigure.above = self->frame->plate;
1976 event.xconfigure.override_redirect = FALSE;
1977 XSendEvent(event.xconfigure.display, event.xconfigure.window,
1978 FALSE, StructureNotifyMask, &event);
1979 }
1980 }
1981
1982 /* if the client is shrinking, then resize the frame before the client */
1983 if (send_resize_client && (w <= oldw || h <= oldh))
1984 XResizeWindow(ob_display, self->window, w, h);
1985
1986 XFlush(ob_display);
1987 }
1988
1989 void client_fullscreen(ObClient *self, gboolean fs, gboolean savearea)
1990 {
1991 int x, y, w, h;
1992
1993 if (!(self->functions & OB_CLIENT_FUNC_FULLSCREEN) || /* can't */
1994 self->fullscreen == fs) return; /* already done */
1995
1996 self->fullscreen = fs;
1997 client_change_state(self); /* change the state hints on the client,
1998 and adjust out layer/stacking */
1999
2000 if (fs) {
2001 if (savearea) {
2002 guint32 dimensions[4];
2003 dimensions[0] = self->area.x;
2004 dimensions[1] = self->area.y;
2005 dimensions[2] = self->area.width;
2006 dimensions[3] = self->area.height;
2007
2008 PROP_SETA32(self->window, openbox_premax, cardinal,
2009 dimensions, 4);
2010 }
2011
2012 /* these are not actually used cuz client_configure will set them
2013 as appropriate when the window is fullscreened */
2014 x = y = w = h = 0;
2015 } else {
2016 guint num;
2017 gint32 *dimensions;
2018 Rect *a;
2019
2020 /* pick some fallbacks... */
2021 a = screen_area_monitor(self->desktop, 0);
2022 x = a->x + a->width / 4;
2023 y = a->y + a->height / 4;
2024 w = a->width / 2;
2025 h = a->height / 2;
2026
2027 if (PROP_GETA32(self->window, openbox_premax, cardinal,
2028 (guint32**)&dimensions, &num)) {
2029 if (num == 4) {
2030 x = dimensions[0];
2031 y = dimensions[1];
2032 w = dimensions[2];
2033 h = dimensions[3];
2034 }
2035 g_free(dimensions);
2036 }
2037 }
2038
2039 client_setup_decor_and_functions(self);
2040
2041 client_move_resize(self, x, y, w, h);
2042
2043 /* try focus us when we go into fullscreen mode */
2044 client_focus(self);
2045 }
2046
2047 static void client_iconify_recursive(ObClient *self,
2048 gboolean iconic, gboolean curdesk)
2049 {
2050 GSList *it;
2051 gboolean changed = FALSE;
2052
2053
2054 if (self->iconic != iconic) {
2055 ob_debug("%sconifying window: 0x%lx\n", (iconic ? "I" : "Uni"),
2056 self->window);
2057
2058 self->iconic = iconic;
2059
2060 if (iconic) {
2061 if (self->functions & OB_CLIENT_FUNC_ICONIFY) {
2062 self->wmstate = IconicState;
2063 self->ignore_unmaps++;
2064 /* we unmap the client itself so that we can get MapRequest
2065 events, and because the ICCCM tells us to! */
2066 XUnmapWindow(ob_display, self->window);
2067
2068 /* update the focus lists.. iconic windows go to the bottom of
2069 the list, put the new iconic window at the 'top of the
2070 bottom'. */
2071 focus_order_to_top(self);
2072
2073 changed = TRUE;
2074 }
2075 } else {
2076 if (curdesk)
2077 client_set_desktop(self, screen_desktop, FALSE);
2078 self->wmstate = self->shaded ? IconicState : NormalState;
2079 XMapWindow(ob_display, self->window);
2080
2081 /* this puts it after the current focused window */
2082 focus_order_remove(self);
2083 focus_order_add_new(self);
2084
2085 /* this is here cuz with the VIDMODE extension, the viewport can
2086 change while a fullscreen window is iconic, and when it
2087 uniconifies, it would be nice if it did so to the new position
2088 of the viewport */
2089 client_reconfigure(self);
2090
2091 changed = TRUE;
2092 }
2093 }
2094
2095 if (changed) {
2096 client_change_state(self);
2097 client_showhide(self);
2098 screen_update_areas();
2099 }
2100
2101 /* iconify all transients */
2102 for (it = self->transients; it != NULL; it = it->next)
2103 if (it->data != self) client_iconify_recursive(it->data,
2104 iconic, curdesk);
2105 }
2106
2107 void client_iconify(ObClient *self, gboolean iconic, gboolean curdesk)
2108 {
2109 /* move up the transient chain as far as possible first */
2110 self = client_search_top_transient(self);
2111
2112 client_iconify_recursive(client_search_top_transient(self),
2113 iconic, curdesk);
2114 }
2115
2116 void client_maximize(ObClient *self, gboolean max, int dir, gboolean savearea)
2117 {
2118 int x, y, w, h;
2119
2120 g_assert(dir == 0 || dir == 1 || dir == 2);
2121 if (!(self->functions & OB_CLIENT_FUNC_MAXIMIZE)) return; /* can't */
2122
2123 /* check if already done */
2124 if (max) {
2125 if (dir == 0 && self->max_horz && self->max_vert) return;
2126 if (dir == 1 && self->max_horz) return;
2127 if (dir == 2 && self->max_vert) return;
2128 } else {
2129 if (dir == 0 && !self->max_horz && !self->max_vert) return;
2130 if (dir == 1 && !self->max_horz) return;
2131 if (dir == 2 && !self->max_vert) return;
2132 }
2133
2134 /* work with the frame's coords */
2135 x = self->frame->area.x;
2136 y = self->frame->area.y;
2137 w = self->area.width;
2138 h = self->area.height;
2139
2140 if (max) {
2141 if (savearea) {
2142 gint32 dimensions[4];
2143 gint32 *readdim;
2144 guint num;
2145
2146 dimensions[0] = x;
2147 dimensions[1] = y;
2148 dimensions[2] = w;
2149 dimensions[3] = h;
2150
2151 /* get the property off the window and use it for the dimensions
2152 we are already maxed on */
2153 if (PROP_GETA32(self->window, openbox_premax, cardinal,
2154 (guint32**)&readdim, &num)) {
2155 if (num == 4) {
2156 if (self->max_horz) {
2157 dimensions[0] = readdim[0];
2158 dimensions[2] = readdim[2];
2159 }
2160 if (self->max_vert) {
2161 dimensions[1] = readdim[1];
2162 dimensions[3] = readdim[3];
2163 }
2164 }
2165 g_free(readdim);
2166 }
2167
2168 PROP_SETA32(self->window, openbox_premax, cardinal,
2169 (guint32*)dimensions, 4);
2170 }
2171 } else {
2172 guint num;
2173 gint32 *dimensions;
2174 Rect *a;
2175
2176 /* pick some fallbacks... */
2177 a = screen_area_monitor(self->desktop, 0);
2178 if (dir == 0 || dir == 1) { /* horz */
2179 x = a->x + a->width / 4;
2180 w = a->width / 2;
2181 }
2182 if (dir == 0 || dir == 2) { /* vert */
2183 y = a->y + a->height / 4;
2184 h = a->height / 2;
2185 }
2186
2187 if (PROP_GETA32(self->window, openbox_premax, cardinal,
2188 (guint32**)&dimensions, &num)) {
2189 if (num == 4) {
2190 if (dir == 0 || dir == 1) { /* horz */
2191 x = dimensions[0];
2192 w = dimensions[2];
2193 }
2194 if (dir == 0 || dir == 2) { /* vert */
2195 y = dimensions[1];
2196 h = dimensions[3];
2197 }
2198 }
2199 g_free(dimensions);
2200 }
2201 }
2202
2203 if (dir == 0 || dir == 1) /* horz */
2204 self->max_horz = max;
2205 if (dir == 0 || dir == 2) /* vert */
2206 self->max_vert = max;
2207
2208 if (!self->max_horz && !self->max_vert)
2209 PROP_ERASE(self->window, openbox_premax);
2210
2211 client_change_state(self); /* change the state hints on the client */
2212
2213 client_setup_decor_and_functions(self);
2214
2215 /* figure out where the client should be going */
2216 frame_frame_gravity(self->frame, &x, &y);
2217 client_move_resize(self, x, y, w, h);
2218 }
2219
2220 void client_shade(ObClient *self, gboolean shade)
2221 {
2222 if ((!(self->functions & OB_CLIENT_FUNC_SHADE) &&
2223 shade) || /* can't shade */
2224 self->shaded == shade) return; /* already done */
2225
2226 /* when we're iconic, don't change the wmstate */
2227 if (!self->iconic)
2228 self->wmstate = shade ? IconicState : NormalState;
2229 self->shaded = shade;
2230 client_change_state(self);
2231 /* resize the frame to just the titlebar */
2232 frame_adjust_area(self->frame, FALSE, FALSE, FALSE);
2233 }
2234
2235 void client_close(ObClient *self)
2236 {
2237 XEvent ce;
2238
2239 if (!(self->functions & OB_CLIENT_FUNC_CLOSE)) return;
2240
2241 /*
2242 XXX: itd be cool to do timeouts and shit here for killing the client's
2243 process off
2244 like... if the window is around after 5 seconds, then the close button
2245 turns a nice red, and if this function is called again, the client is
2246 explicitly killed.
2247 */
2248
2249 ce.xclient.type = ClientMessage;
2250 ce.xclient.message_type = prop_atoms.wm_protocols;
2251 ce.xclient.display = ob_display;
2252 ce.xclient.window = self->window;
2253 ce.xclient.format = 32;
2254 ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
2255 ce.xclient.data.l[1] = event_lasttime;
2256 ce.xclient.data.l[2] = 0l;
2257 ce.xclient.data.l[3] = 0l;
2258 ce.xclient.data.l[4] = 0l;
2259 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2260 }
2261
2262 void client_kill(ObClient *self)
2263 {
2264 XKillClient(ob_display, self->window);
2265 }
2266
2267 void client_set_desktop_recursive(ObClient *self,
2268 guint target, gboolean donthide)
2269 {
2270 guint old;
2271 GSList *it;
2272
2273 if (target != self->desktop) {
2274
2275 ob_debug("Setting desktop %u\n", target+1);
2276
2277 g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
2278
2279 /* remove from the old desktop(s) */
2280 focus_order_remove(self);
2281
2282 old = self->desktop;
2283 self->desktop = target;
2284 PROP_SET32(self->window, net_wm_desktop, cardinal, target);
2285 /* the frame can display the current desktop state */
2286 frame_adjust_state(self->frame);
2287 /* 'move' the window to the new desktop */
2288 if (!donthide)
2289 client_showhide(self);
2290 /* raise if it was not already on the desktop */
2291 if (old != DESKTOP_ALL)
2292 stacking_raise(CLIENT_AS_WINDOW(self));
2293 screen_update_areas();
2294
2295 /* add to the new desktop(s) */
2296 if (config_focus_new)
2297 focus_order_to_top(self);
2298 else
2299 focus_order_to_bottom(self);
2300 }
2301
2302 /* move all transients */
2303 for (it = self->transients; it != NULL; it = it->next)
2304 if (it->data != self) client_set_desktop_recursive(it->data,
2305 target, donthide);
2306 }
2307
2308 void client_set_desktop(ObClient *self, guint target, gboolean donthide)
2309 {
2310 client_set_desktop_recursive(client_search_top_transient(self),
2311 target, donthide);
2312 }
2313
2314 ObClient *client_search_modal_child(ObClient *self)
2315 {
2316 GSList *it;
2317 ObClient *ret;
2318
2319 for (it = self->transients; it != NULL; it = it->next) {
2320 ObClient *c = it->data;
2321 if ((ret = client_search_modal_child(c))) return ret;
2322 if (c->modal) return c;
2323 }
2324 return NULL;
2325 }
2326
2327 gboolean client_validate(ObClient *self)
2328 {
2329 XEvent e;
2330
2331 XSync(ob_display, FALSE); /* get all events on the server */
2332
2333 if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
2334 XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
2335 XPutBackEvent(ob_display, &e);
2336 return FALSE;
2337 }
2338
2339 return TRUE;
2340 }
2341
2342 void client_set_wm_state(ObClient *self, long state)
2343 {
2344 if (state == self->wmstate) return; /* no change */
2345
2346 switch (state) {
2347 case IconicState:
2348 client_iconify(self, TRUE, TRUE);
2349 break;
2350 case NormalState:
2351 client_iconify(self, FALSE, TRUE);
2352 break;
2353 }
2354 }
2355
2356 void client_set_state(ObClient *self, Atom action, long data1, long data2)
2357 {
2358 gboolean shaded = self->shaded;
2359 gboolean fullscreen = self->fullscreen;
2360 gboolean max_horz = self->max_horz;
2361 gboolean max_vert = self->max_vert;
2362 int i;
2363
2364 if (!(action == prop_atoms.net_wm_state_add ||
2365 action == prop_atoms.net_wm_state_remove ||
2366 action == prop_atoms.net_wm_state_toggle))
2367 /* an invalid action was passed to the client message, ignore it */
2368 return;
2369
2370 for (i = 0; i < 2; ++i) {
2371 Atom state = i == 0 ? data1 : data2;
2372
2373 if (!state) continue;
2374
2375 /* if toggling, then pick whether we're adding or removing */
2376 if (action == prop_atoms.net_wm_state_toggle) {
2377 if (state == prop_atoms.net_wm_state_modal)
2378 action = self->modal ? prop_atoms.net_wm_state_remove :
2379 prop_atoms.net_wm_state_add;
2380 else if (state == prop_atoms.net_wm_state_maximized_vert)
2381 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2382 prop_atoms.net_wm_state_add;
2383 else if (state == prop_atoms.net_wm_state_maximized_horz)
2384 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2385 prop_atoms.net_wm_state_add;
2386 else if (state == prop_atoms.net_wm_state_shaded)
2387 action = self->shaded ? prop_atoms.net_wm_state_remove :
2388 prop_atoms.net_wm_state_add;
2389 else if (state == prop_atoms.net_wm_state_skip_taskbar)
2390 action = self->skip_taskbar ?
2391 prop_atoms.net_wm_state_remove :
2392 prop_atoms.net_wm_state_add;
2393 else if (state == prop_atoms.net_wm_state_skip_pager)
2394 action = self->skip_pager ?
2395 prop_atoms.net_wm_state_remove :
2396 prop_atoms.net_wm_state_add;
2397 else if (state == prop_atoms.net_wm_state_fullscreen)
2398 action = self->fullscreen ?
2399 prop_atoms.net_wm_state_remove :
2400 prop_atoms.net_wm_state_add;
2401 else if (state == prop_atoms.net_wm_state_above)
2402 action = self->above ? prop_atoms.net_wm_state_remove :
2403 prop_atoms.net_wm_state_add;
2404 else if (state == prop_atoms.net_wm_state_below)
2405 action = self->below ? prop_atoms.net_wm_state_remove :
2406 prop_atoms.net_wm_state_add;
2407 }
2408
2409 if (action == prop_atoms.net_wm_state_add) {
2410 if (state == prop_atoms.net_wm_state_modal) {
2411 /* XXX raise here or something? */
2412 self->modal = TRUE;
2413 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2414 max_vert = TRUE;
2415 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2416 max_horz = TRUE;
2417 } else if (state == prop_atoms.net_wm_state_shaded) {
2418 shaded = TRUE;
2419 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2420 self->skip_taskbar = TRUE;
2421 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2422 self->skip_pager = TRUE;
2423 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2424 fullscreen = TRUE;
2425 } else if (state == prop_atoms.net_wm_state_above) {
2426 self->above = TRUE;
2427 } else if (state == prop_atoms.net_wm_state_below) {
2428 self->below = TRUE;
2429 }
2430
2431 } else { /* action == prop_atoms.net_wm_state_remove */
2432 if (state == prop_atoms.net_wm_state_modal) {
2433 self->modal = FALSE;
2434 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2435 max_vert = FALSE;
2436 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2437 max_horz = FALSE;
2438 } else if (state == prop_atoms.net_wm_state_shaded) {
2439 shaded = FALSE;
2440 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2441 self->skip_taskbar = FALSE;
2442 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2443 self->skip_pager = FALSE;
2444 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2445 fullscreen = FALSE;
2446 } else if (state == prop_atoms.net_wm_state_above) {
2447 self->above = FALSE;
2448 } else if (state == prop_atoms.net_wm_state_below) {
2449 self->below = FALSE;
2450 }
2451 }
2452 }
2453 if (max_horz != self->max_horz || max_vert != self->max_vert) {
2454 if (max_horz != self->max_horz && max_vert != self->max_vert) {
2455 /* toggling both */
2456 if (max_horz == max_vert) { /* both going the same way */
2457 client_maximize(self, max_horz, 0, TRUE);
2458 } else {
2459 client_maximize(self, max_horz, 1, TRUE);
2460 client_maximize(self, max_vert, 2, TRUE);
2461 }
2462 } else {
2463 /* toggling one */
2464 if (max_horz != self->max_horz)
2465 client_maximize(self, max_horz, 1, TRUE);
2466 else
2467 client_maximize(self, max_vert, 2, TRUE);
2468 }
2469 }
2470 /* change fullscreen state before shading, as it will affect if the window
2471 can shade or not */
2472 if (fullscreen != self->fullscreen)
2473 client_fullscreen(self, fullscreen, TRUE);
2474 if (shaded != self->shaded)
2475 client_shade(self, shaded);
2476 client_calc_layer(self);
2477 client_change_state(self); /* change the hint to reflect these changes */
2478 }
2479
2480 ObClient *client_focus_target(ObClient *self)
2481 {
2482 ObClient *child;
2483
2484 /* if we have a modal child, then focus it, not us */
2485 child = client_search_modal_child(self);
2486 if (child) return child;
2487 return self;
2488 }
2489
2490 gboolean client_can_focus(ObClient *self)
2491 {
2492 XEvent ev;
2493
2494 /* choose the correct target */
2495 self = client_focus_target(self);
2496
2497 if (!self->frame->visible)
2498 return FALSE;
2499
2500 if (!((self->can_focus || self->focus_notify) &&
2501 (self->desktop == screen_desktop ||
2502 self->desktop == DESKTOP_ALL) &&
2503 !self->iconic))
2504 return FALSE;
2505
2506 /* do a check to see if the window has already been unmapped or destroyed
2507 do this intelligently while watching out for unmaps we've generated
2508 (ignore_unmaps > 0) */
2509 if (XCheckTypedWindowEvent(ob_display, self->window,
2510 DestroyNotify, &ev)) {
2511 XPutBackEvent(ob_display, &ev);
2512 return FALSE;
2513 }
2514 while (XCheckTypedWindowEvent(ob_display, self->window,
2515 UnmapNotify, &ev)) {
2516 if (self->ignore_unmaps) {
2517 self->ignore_unmaps--;
2518 } else {
2519 XPutBackEvent(ob_display, &ev);
2520 return FALSE;
2521 }
2522 }
2523
2524 return TRUE;
2525 }
2526
2527 gboolean client_focus(ObClient *self)
2528 {
2529 /* choose the correct target */
2530 self = client_focus_target(self);
2531
2532 if (!self->frame->visible) {
2533 /* update the focus lists */
2534 focus_order_to_top(self);
2535 return FALSE;
2536 }
2537
2538 if (!client_can_focus(self))
2539 return FALSE;
2540
2541 if (self->can_focus) {
2542 /* RevertToPointerRoot causes much more headache than RevertToNone, so
2543 I choose to use it always, hopefully to find errors quicker, if any
2544 are left. (I hate X. I hate focus events.)
2545
2546 Update: Changing this to RevertToNone fixed a bug with mozilla (bug
2547 #799. So now it is RevertToNone again.
2548 */
2549 XSetInputFocus(ob_display, self->window, RevertToNone,
2550 event_lasttime);
2551 }
2552
2553 if (self->focus_notify) {
2554 XEvent ce;
2555 ce.xclient.type = ClientMessage;
2556 ce.xclient.message_type = prop_atoms.wm_protocols;
2557 ce.xclient.display = ob_display;
2558 ce.xclient.window = self->window;
2559 ce.xclient.format = 32;
2560 ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2561 ce.xclient.data.l[1] = event_lasttime;
2562 ce.xclient.data.l[2] = 0l;
2563 ce.xclient.data.l[3] = 0l;
2564 ce.xclient.data.l[4] = 0l;
2565 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2566 }
2567
2568 focus_set_client(self);
2569
2570 #ifdef DEBUG_FOCUS
2571 ob_debug("%sively focusing %lx at %d\n",
2572 (self->can_focus ? "act" : "pass"),
2573 self->window, (int) event_lasttime);
2574 #endif
2575
2576 /* Cause the FocusIn to come back to us. Important for desktop switches,
2577 since otherwise we'll have no FocusIn on the queue and send it off to
2578 the focus_backup. */
2579 XSync(ob_display, FALSE);
2580 return TRUE;
2581 }
2582
2583 void client_unfocus(ObClient *self)
2584 {
2585 g_assert(focus_client == self);
2586 #ifdef DEBUG_FOCUS
2587 ob_debug("client_unfocus for %lx\n", self->window);
2588 #endif
2589 focus_fallback(OB_FOCUS_FALLBACK_UNFOCUSING);
2590 }
2591
2592 void client_activate(ObClient *self, gboolean here)
2593 {
2594 if (client_normal(self) && screen_showing_desktop)
2595 screen_show_desktop(FALSE);
2596 if (self->iconic)
2597 client_iconify(self, FALSE, FALSE);
2598 if (self->desktop != DESKTOP_ALL &&
2599 self->desktop != screen_desktop) {
2600 if (here)
2601 client_set_desktop(self, screen_desktop, FALSE);
2602 else
2603 screen_set_desktop(self->desktop);
2604 } else if (!self->frame->visible)
2605 /* if its not visible for other reasons, then don't mess
2606 with it */
2607 return;
2608 if (self->shaded)
2609 client_shade(self, FALSE);
2610 client_focus(self);
2611 stacking_raise(CLIENT_AS_WINDOW(self));
2612 }
2613
2614 gboolean client_focused(ObClient *self)
2615 {
2616 return self == focus_client;
2617 }
2618
2619 ObClientIcon *client_icon(ObClient *self, int w, int h)
2620 {
2621 guint i;
2622 /* si is the smallest image >= req */
2623 /* li is the largest image < req */
2624 unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2625
2626 for (i = 0; i < self->nicons; ++i) {
2627 size = self->icons[i].width * self->icons[i].height;
2628 if (size < smallest && size >= (unsigned)(w * h)) {
2629 smallest = size;
2630 si = i;
2631 }
2632 if (size > largest && size <= (unsigned)(w * h)) {
2633 largest = size;
2634 li = i;
2635 }
2636 }
2637 if (largest == 0) /* didnt find one smaller than the requested size */
2638 return &self->icons[si];
2639 return &self->icons[li];
2640 }
2641
2642 /* this be mostly ripped from fvwm */
2643 ObClient *client_find_directional(ObClient *c, ObDirection dir)
2644 {
2645 int my_cx, my_cy, his_cx, his_cy;
2646 int offset = 0;
2647 int distance = 0;
2648 int score, best_score;
2649 ObClient *best_client, *cur;
2650 GList *it;
2651
2652 if(!client_list)
2653 return NULL;
2654
2655 /* first, find the centre coords of the currently focused window */
2656 my_cx = c->frame->area.x + c->frame->area.width / 2;
2657 my_cy = c->frame->area.y + c->frame->area.height / 2;
2658
2659 best_score = -1;
2660 best_client = NULL;
2661
2662 for(it = g_list_first(client_list); it; it = it->next) {
2663 cur = it->data;
2664
2665 /* the currently selected window isn't interesting */
2666 if(cur == c)
2667 continue;
2668 if (!client_normal(cur))
2669 continue;
2670 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2671 continue;
2672 if(cur->iconic)
2673 continue;
2674 if(client_focus_target(cur) == cur &&
2675 !(cur->can_focus || cur->focus_notify))
2676 continue;
2677
2678 /* find the centre coords of this window, from the
2679 * currently focused window's point of view */
2680 his_cx = (cur->frame->area.x - my_cx)
2681 + cur->frame->area.width / 2;
2682 his_cy = (cur->frame->area.y - my_cy)
2683 + cur->frame->area.height / 2;
2684
2685 if(dir == OB_DIRECTION_NORTHEAST || dir == OB_DIRECTION_SOUTHEAST ||
2686 dir == OB_DIRECTION_SOUTHWEST || dir == OB_DIRECTION_NORTHWEST) {
2687 int tx;
2688 /* Rotate the diagonals 45 degrees counterclockwise.
2689 * To do this, multiply the matrix /+h +h\ with the
2690 * vector (x y). \-h +h/
2691 * h = sqrt(0.5). We can set h := 1 since absolute
2692 * distance doesn't matter here. */
2693 tx = his_cx + his_cy;
2694 his_cy = -his_cx + his_cy;
2695 his_cx = tx;
2696 }
2697
2698 switch(dir) {
2699 case OB_DIRECTION_NORTH:
2700 case OB_DIRECTION_SOUTH:
2701 case OB_DIRECTION_NORTHEAST:
2702 case OB_DIRECTION_SOUTHWEST:
2703 offset = (his_cx < 0) ? -his_cx : his_cx;
2704 distance = ((dir == OB_DIRECTION_NORTH ||
2705 dir == OB_DIRECTION_NORTHEAST) ?
2706 -his_cy : his_cy);
2707 break;
2708 case OB_DIRECTION_EAST:
2709 case OB_DIRECTION_WEST:
2710 case OB_DIRECTION_SOUTHEAST:
2711 case OB_DIRECTION_NORTHWEST:
2712 offset = (his_cy < 0) ? -his_cy : his_cy;
2713 distance = ((dir == OB_DIRECTION_WEST ||
2714 dir == OB_DIRECTION_NORTHWEST) ?
2715 -his_cx : his_cx);
2716 break;
2717 }
2718
2719 /* the target must be in the requested direction */
2720 if(distance <= 0)
2721 continue;
2722
2723 /* Calculate score for this window. The smaller the better. */
2724 score = distance + offset;
2725
2726 /* windows more than 45 degrees off the direction are
2727 * heavily penalized and will only be chosen if nothing
2728 * else within a million pixels */
2729 if(offset > distance)
2730 score += 1000000;
2731
2732 if(best_score == -1 || score < best_score)
2733 best_client = cur,
2734 best_score = score;
2735 }
2736
2737 return best_client;
2738 }
2739
2740 void client_set_layer(ObClient *self, int layer)
2741 {
2742 if (layer < 0) {
2743 self->below = TRUE;
2744 self->above = FALSE;
2745 } else if (layer == 0) {
2746 self->below = self->above = FALSE;
2747 } else {
2748 self->below = FALSE;
2749 self->above = TRUE;
2750 }
2751 client_calc_layer(self);
2752 client_change_state(self); /* reflect this in the state hints */
2753 }
2754
2755 guint client_monitor(ObClient *self)
2756 {
2757 guint i;
2758
2759 for (i = 0; i < screen_num_monitors; ++i) {
2760 Rect *area = screen_physical_area_monitor(i);
2761 if (RECT_INTERSECTS_RECT(*area, self->frame->area))
2762 break;
2763 }
2764 if (i == screen_num_monitors) i = 0;
2765 g_assert(i < screen_num_monitors);
2766 return i;
2767 }
2768
2769 ObClient *client_search_top_transient(ObClient *self)
2770 {
2771 /* move up the transient chain as far as possible */
2772 if (self->transient_for) {
2773 if (self->transient_for != OB_TRAN_GROUP) {
2774 return client_search_top_transient(self->transient_for);
2775 } else {
2776 GSList *it;
2777
2778 for (it = self->group->members; it; it = it->next) {
2779 ObClient *c = it->data;
2780
2781 /* checking transient_for prevents infinate loops! */
2782 if (c != self && !c->transient_for)
2783 break;
2784 }
2785 if (it)
2786 return it->data;
2787 }
2788 }
2789
2790 return self;
2791 }
2792
2793 ObClient *client_search_transient(ObClient *self, ObClient *search)
2794 {
2795 GSList *sit;
2796
2797 for (sit = self->transients; sit; sit = g_slist_next(sit)) {
2798 if (sit->data == search)
2799 return search;
2800 if (client_search_transient(sit->data, search))
2801 return search;
2802 }
2803 return NULL;
2804 }
2805
2806 void client_update_sm_client_id(ObClient *self)
2807 {
2808 g_free(self->sm_client_id);
2809 self->sm_client_id = NULL;
2810
2811 if (!PROP_GETS(self->window, sm_client_id, locale, &self->sm_client_id) &&
2812 self->group)
2813 PROP_GETS(self->group->leader, sm_client_id, locale,
2814 &self->sm_client_id);
2815 }
2816
2817 /* finds the nearest edge in the given direction from the current client
2818 * note to self: the edge is the -frame- edge (the actual one), not the
2819 * client edge.
2820 */
2821 int client_directional_edge_search(ObClient *c, ObDirection dir)
2822 {
2823 int dest;
2824 int my_edge_start, my_edge_end, my_offset;
2825 GList *it;
2826 Rect *a;
2827
2828 if(!client_list)
2829 return -1;
2830
2831 a = screen_area(c->desktop);
2832
2833 switch(dir) {
2834 case OB_DIRECTION_NORTH:
2835 my_edge_start = c->frame->area.x;
2836 my_edge_end = c->frame->area.x + c->frame->area.width;
2837 my_offset = c->frame->area.y;
2838
2839 dest = a->y; /* default: top of screen */
2840
2841 for(it = g_list_first(client_list); it; it = it->next) {
2842 int his_edge_start, his_edge_end, his_offset;
2843 ObClient *cur = it->data;
2844
2845 if(cur == c)
2846 continue;
2847 if(!client_normal(cur))
2848 continue;
2849 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2850 continue;
2851 if(cur->iconic)
2852 continue;
2853
2854 his_edge_start = cur->frame->area.x;
2855 his_edge_end = cur->frame->area.x + cur->frame->area.width;
2856 his_offset = cur->frame->area.y + cur->frame->area.height;
2857
2858 if(his_offset + 1 > my_offset)
2859 continue;
2860
2861 if(his_offset < dest)
2862 continue;
2863
2864 if(his_edge_start >= my_edge_start &&
2865 his_edge_start <= my_edge_end)
2866 dest = his_offset;
2867
2868 if(my_edge_start >= his_edge_start &&
2869 my_edge_start <= his_edge_end)
2870 dest = his_offset;
2871
2872 }
2873 break;
2874 case OB_DIRECTION_SOUTH:
2875 my_edge_start = c->frame->area.x;
2876 my_edge_end = c->frame->area.x + c->frame->area.width;
2877 my_offset = c->frame->area.y + c->frame->area.height;
2878
2879 dest = a->y + a->height; /* default: bottom of screen */
2880
2881 for(it = g_list_first(client_list); it; it = it->next) {
2882 int his_edge_start, his_edge_end, his_offset;
2883 ObClient *cur = it->data;
2884
2885 if(cur == c)
2886 continue;
2887 if(!client_normal(cur))
2888 continue;
2889 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2890 continue;
2891 if(cur->iconic)
2892 continue;
2893
2894 his_edge_start = cur->frame->area.x;
2895 his_edge_end = cur->frame->area.x + cur->frame->area.width;
2896 his_offset = cur->frame->area.y;
2897
2898
2899 if(his_offset - 1 < my_offset)
2900 continue;
2901
2902 if(his_offset > dest)
2903 continue;
2904
2905 if(his_edge_start >= my_edge_start &&
2906 his_edge_start <= my_edge_end)
2907 dest = his_offset;
2908
2909 if(my_edge_start >= his_edge_start &&
2910 my_edge_start <= his_edge_end)
2911 dest = his_offset;
2912
2913 }
2914 break;
2915 case OB_DIRECTION_WEST:
2916 my_edge_start = c->frame->area.y;
2917 my_edge_end = c->frame->area.y + c->frame->area.height;
2918 my_offset = c->frame->area.x;
2919
2920 dest = a->x; /* default: leftmost egde of screen */
2921
2922 for(it = g_list_first(client_list); it; it = it->next) {
2923 int his_edge_start, his_edge_end, his_offset;
2924 ObClient *cur = it->data;
2925
2926 if(cur == c)
2927 continue;
2928 if(!client_normal(cur))
2929 continue;
2930 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2931 continue;
2932 if(cur->iconic)
2933 continue;
2934
2935 his_edge_start = cur->frame->area.y;
2936 his_edge_end = cur->frame->area.y + cur->frame->area.height;
2937 his_offset = cur->frame->area.x + cur->frame->area.width;
2938
2939 if(his_offset + 1 > my_offset)
2940 continue;
2941
2942 if(his_offset < dest)
2943 continue;
2944
2945 if(his_edge_start >= my_edge_start &&
2946 his_edge_start <= my_edge_end)
2947 dest = his_offset;
2948
2949 if(my_edge_start >= his_edge_start &&
2950 my_edge_start <= his_edge_end)
2951 dest = his_offset;
2952
2953
2954 }
2955 break;
2956 case OB_DIRECTION_EAST:
2957 my_edge_start = c->frame->area.y;
2958 my_edge_end = c->frame->area.y + c->frame->area.height;
2959 my_offset = c->frame->area.x + c->frame->area.width;
2960
2961 dest = a->x + a->width; /* default: rightmost edge of screen */
2962
2963 for(it = g_list_first(client_list); it; it = it->next) {
2964 int his_edge_start, his_edge_end, his_offset;
2965 ObClient *cur = it->data;
2966
2967 if(cur == c)
2968 continue;
2969 if(!client_normal(cur))
2970 continue;
2971 if(c->desktop != cur->desktop && cur->desktop != DESKTOP_ALL)
2972 continue;
2973 if(cur->iconic)
2974 continue;
2975
2976 his_edge_start = cur->frame->area.y;
2977 his_edge_end = cur->frame->area.y + cur->frame->area.height;
2978 his_offset = cur->frame->area.x;
2979
2980 if(his_offset - 1 < my_offset)
2981 continue;
2982
2983 if(his_offset > dest)
2984 continue;
2985
2986 if(his_edge_start >= my_edge_start &&
2987 his_edge_start <= my_edge_end)
2988 dest = his_offset;
2989
2990 if(my_edge_start >= his_edge_start &&
2991 my_edge_start <= his_edge_end)
2992 dest = his_offset;
2993
2994 }
2995 break;
2996 case OB_DIRECTION_NORTHEAST:
2997 case OB_DIRECTION_SOUTHEAST:
2998 case OB_DIRECTION_NORTHWEST:
2999 case OB_DIRECTION_SOUTHWEST:
3000 /* not implemented */
3001 break;
3002 default:
3003 g_assert_not_reached();
3004 }
3005 return dest;
3006 }
This page took 0.167042 seconds and 4 git commands to generate.