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