]> Dogcows Code - chaz/openbox/blob - openbox/client.c
ef6a0e1a112936eda697ac165f27dd5cba5a9364
[chaz/openbox] / openbox / client.c
1 #include "client.h"
2 #include "startup.h"
3 #include "screen.h"
4 #include "moveresize.h"
5 #include "prop.h"
6 #include "extensions.h"
7 #include "frame.h"
8 #include "event.h"
9 #include "grab.h"
10 #include "focus.h"
11 #include "stacking.h"
12 #include "dispatch.h"
13 #include "openbox.h"
14 #include "group.h"
15 #include "config.h"
16
17 #include <glib.h>
18 #include <X11/Xutil.h>
19
20 /*! The event mask to grab on client windows */
21 #define CLIENT_EVENTMASK (PropertyChangeMask | FocusChangeMask | \
22 StructureNotifyMask)
23
24 #define CLIENT_NOPROPAGATEMASK (ButtonPressMask | ButtonReleaseMask | \
25 ButtonMotionMask)
26
27 GList *client_list = NULL;
28 GHashTable *client_map = NULL;
29
30 static void client_get_all(Client *self);
31 static void client_toggle_border(Client *self, gboolean show);
32 static void client_get_area(Client *self);
33 static void client_get_desktop(Client *self);
34 static void client_get_state(Client *self);
35 static void client_get_shaped(Client *self);
36 static void client_get_mwm_hints(Client *self);
37 static void client_get_gravity(Client *self);
38 static void client_showhide(Client *self);
39 static void client_change_allowed_actions(Client *self);
40 static void client_change_state(Client *self);
41 static void client_move_onscreen(Client *self);
42 static Client *search_focus_tree(Client *node, Client *skip);
43 static void client_apply_startup_state(Client *self);
44 static Client *search_modal_tree(Client *node, Client *skip);
45
46 static guint map_hash(Window *w) { return *w; }
47 static gboolean map_key_comp(Window *w1, Window *w2) { return *w1 == *w2; }
48
49 void client_startup()
50 {
51 client_map = g_hash_table_new((GHashFunc)map_hash,
52 (GEqualFunc)map_key_comp);
53
54 client_set_list();
55 }
56
57 void client_shutdown()
58 {
59 g_hash_table_destroy(client_map);
60 }
61
62 void client_set_list()
63 {
64 Window *windows, *win_it;
65 GList *it;
66 guint size = g_list_length(client_list);
67
68 /* create an array of the window ids */
69 if (size > 0) {
70 windows = g_new(Window, size);
71 win_it = windows;
72 for (it = client_list; it != NULL; it = it->next, ++win_it)
73 *win_it = ((Client*)it->data)->window;
74 } else
75 windows = NULL;
76
77 PROP_SETA32(ob_root, net_client_list, window, (guint32*)windows, size);
78
79 if (windows)
80 g_free(windows);
81
82 stacking_set_list();
83 }
84
85 void client_manage_all()
86 {
87 unsigned int i, j, nchild;
88 Window w, *children;
89 XWMHints *wmhints;
90 XWindowAttributes attrib;
91 Client *active;
92
93 XQueryTree(ob_display, ob_root, &w, &w, &children, &nchild);
94
95 /* remove all icon windows from the list */
96 for (i = 0; i < nchild; i++) {
97 if (children[i] == None) continue;
98 wmhints = XGetWMHints(ob_display, children[i]);
99 if (wmhints) {
100 if ((wmhints->flags & IconWindowHint) &&
101 (wmhints->icon_window != children[i]))
102 for (j = 0; j < nchild; j++)
103 if (children[j] == wmhints->icon_window) {
104 children[j] = None;
105 break;
106 }
107 XFree(wmhints);
108 }
109 }
110
111 for (i = 0; i < nchild; ++i) {
112 if (children[i] == None)
113 continue;
114 if (XGetWindowAttributes(ob_display, children[i], &attrib)) {
115 if (attrib.override_redirect) continue;
116
117 if (attrib.map_state != IsUnmapped)
118 client_manage(children[i]);
119 }
120 }
121 XFree(children);
122
123 /* stack them as they were on startup!
124 why with stacking_lower? Why, because then windows who aren't in the
125 stacking list are on the top where you can see them instead of buried
126 at the bottom! */
127 for (i = startup_stack_size; i > 0; --i) {
128 Client *c;
129
130 w = startup_stack_order[i-1];
131 c = g_hash_table_lookup(client_map, &w);
132 if (c) stacking_lower(c);
133 }
134 g_free(startup_stack_order);
135 startup_stack_order = NULL;
136 startup_stack_size = 0;
137
138 active = g_hash_table_lookup(client_map, &startup_active);
139 if (!active || !client_focus(active))
140 if (config_focus_new)
141 focus_fallback(Fallback_NoFocus);
142 }
143
144 void client_manage(Window window)
145 {
146 Client *self;
147 XEvent e;
148 XWindowAttributes attrib;
149 XSetWindowAttributes attrib_set;
150 /* XWMHints *wmhint; */
151
152 grab_server(TRUE);
153
154 /* check if it has already been unmapped by the time we started mapping
155 the grab does a sync so we don't have to here */
156 if (XCheckTypedWindowEvent(ob_display, window, DestroyNotify, &e) ||
157 XCheckTypedWindowEvent(ob_display, window, UnmapNotify, &e)) {
158 XPutBackEvent(ob_display, &e);
159
160 grab_server(FALSE);
161 return; /* don't manage it */
162 }
163
164 /* make sure it isn't an override-redirect window */
165 if (!XGetWindowAttributes(ob_display, window, &attrib) ||
166 attrib.override_redirect) {
167 grab_server(FALSE);
168 return; /* don't manage it */
169 }
170
171 /* /\* is the window a docking app *\/
172 if ((wmhint = XGetWMHints(ob_display, window))) {
173 if ((wmhint->flags & StateHint) &&
174 wmhint->initial_state == WithdrawnState) {
175 /\* XXX: make dock apps work! *\/
176 grab_server(FALSE);
177 XFree(wmhint);
178 return;
179 }
180 XFree(wmhint);
181 }
182 */
183 g_message("Managing window: %lx", window);
184
185 /* choose the events we want to receive on the CLIENT window */
186 attrib_set.event_mask = CLIENT_EVENTMASK;
187 attrib_set.do_not_propagate_mask = CLIENT_NOPROPAGATEMASK;
188 XChangeWindowAttributes(ob_display, window,
189 CWEventMask|CWDontPropagate, &attrib_set);
190
191
192 /* create the Client struct, and populate it from the hints on the
193 window */
194 self = g_new(Client, 1);
195 self->window = window;
196 client_get_all(self);
197
198 /* remove the client's border (and adjust re gravity) */
199 client_toggle_border(self, FALSE);
200
201 /* specify that if we exit, the window should not be destroyed and should
202 be reparented back to root automatically */
203 XChangeSaveSet(ob_display, window, SetModeInsert);
204
205 /* create the decoration frame for the client window */
206 self->frame = frame_new();
207
208 frame_grab_client(self->frame, self);
209
210 client_apply_startup_state(self);
211
212 grab_server(FALSE);
213
214 client_list = g_list_append(client_list, self);
215 stacking_list = g_list_append(stacking_list, self);
216 g_assert(!g_hash_table_lookup(client_map, &self->window));
217 g_hash_table_insert(client_map, &self->window, self);
218
219 /* update the focus lists */
220 focus_order_add_new(self);
221
222 stacking_raise(self);
223
224 screen_update_struts();
225
226 dispatch_client(Event_Client_New, self, 0, 0);
227
228 client_showhide(self);
229
230 /* focus the new window? */
231 if (ob_state != State_Starting) {
232 Client *parent;
233 gboolean group_foc = FALSE;
234
235 parent = NULL;
236
237 if (self->group) {
238 GSList *it;
239
240 for (it = self->group->members; it; it = it->next)
241 if (client_focused(it->data)) {
242 group_foc = TRUE;
243 break;
244 }
245 }
246 if (!group_foc && self->transient_for) {
247 if (self->transient_for != TRAN_GROUP) {/* transient of a window */
248 parent = self->transient_for;
249 } else { /* transient of a group */
250 GSList *it;
251
252 for (it = self->group->members; it; it = it->next)
253 if (it->data != self &&
254 ((Client*)it->data)->transient_for != TRAN_GROUP)
255 parent = it->data;
256 }
257 }
258 /* note the check against Type_Normal, not client_normal(self), which
259 would also include dialog types. in this case we want more strict
260 rules for focus */
261 if ((config_focus_new &&
262 (self->type == Type_Normal ||
263 (self->type == Type_Dialog &&
264 (group_foc ||
265 (!parent && (!self->group ||
266 !self->group->members->next)))))) ||
267 (parent && (client_focused(parent) ||
268 search_focus_tree(parent, parent)))) {
269 client_focus(self);
270 }
271 }
272
273 /* update the list hints */
274 client_set_list();
275
276 /* make sure the window is visible */
277 client_move_onscreen(self);
278
279 dispatch_client(Event_Client_Mapped, self, 0, 0);
280
281 g_message("Managed window 0x%lx", window);
282 }
283
284 void client_unmanage_all()
285 {
286 while (client_list != NULL)
287 client_unmanage(client_list->data);
288 }
289
290 void client_unmanage(Client *self)
291 {
292 int j;
293 GSList *it;
294
295 g_message("Unmanaging window: %lx", self->window);
296
297 dispatch_client(Event_Client_Destroy, self, 0, 0);
298 g_assert(self != NULL);
299
300 /* remove the window from our save set */
301 XChangeSaveSet(ob_display, self->window, SetModeDelete);
302
303 /* we dont want events no more */
304 XSelectInput(ob_display, self->window, NoEventMask);
305
306 frame_hide(self->frame);
307
308 client_list = g_list_remove(client_list, self);
309 stacking_list = g_list_remove(stacking_list, self);
310 g_hash_table_remove(client_map, &self->window);
311
312 /* update the focus lists */
313 focus_order_remove(self);
314
315 /* once the client is out of the list, update the struts to remove it's
316 influence */
317 screen_update_struts();
318
319 /* tell our parent(s) that we're gone */
320 if (self->transient_for == TRAN_GROUP) { /* transient of group */
321 GSList *it;
322
323 for (it = self->group->members; it; it = it->next)
324 if (it->data != self)
325 ((Client*)it->data)->transients =
326 g_slist_remove(((Client*)it->data)->transients, self);
327 } else if (self->transient_for) { /* transient of window */
328 self->transient_for->transients =
329 g_slist_remove(self->transient_for->transients, self);
330 }
331
332 /* tell our transients that we're gone */
333 for (it = self->transients; it != NULL; it = it->next) {
334 if (((Client*)it->data)->transient_for != TRAN_GROUP) {
335 ((Client*)it->data)->transient_for = NULL;
336 client_calc_layer(it->data);
337 }
338 }
339
340 if (moveresize_client == self)
341 moveresize_end(TRUE);
342
343 if (focus_client == self) {
344 XEvent e;
345
346 /* focus the last focused window on the desktop, and ignore enter
347 events from the unmap so it doesnt mess with the focus */
348 while (XCheckTypedEvent(ob_display, EnterNotify, &e));
349 client_unfocus(self);
350 }
351
352 /* remove from its group */
353 if (self->group) {
354 group_remove(self->group, self);
355 self->group = NULL;
356 }
357
358 /* dispatch the unmapped event */
359 dispatch_client(Event_Client_Unmapped, self, 0, 0);
360 g_assert(self != NULL);
361
362 /* give the client its border back */
363 client_toggle_border(self, TRUE);
364
365 /* reparent the window out of the frame, and free the frame */
366 frame_release_client(self->frame, self);
367 self->frame = NULL;
368
369 if (ob_state != State_Exiting) {
370 /* these values should not be persisted across a window
371 unmapping/mapping */
372 prop_erase(self->window, prop_atoms.net_wm_desktop);
373 prop_erase(self->window, prop_atoms.net_wm_state);
374 } else {
375 /* if we're left in an iconic state, the client wont be mapped. this is
376 bad, since we will no longer be managing the window on restart */
377 if (self->iconic)
378 XMapWindow(ob_display, self->window);
379 }
380
381
382 g_message("Unmanaged window 0x%lx", self->window);
383
384 /* free all data allocated in the client struct */
385 g_slist_free(self->transients);
386 for (j = 0; j < self->nicons; ++j)
387 g_free(self->icons[j].data);
388 if (self->nicons > 0)
389 g_free(self->icons);
390 g_free(self->title);
391 g_free(self->icon_title);
392 g_free(self->name);
393 g_free(self->class);
394 g_free(self->role);
395 g_free(self);
396
397 /* update the list hints */
398 client_set_list();
399 }
400
401 static void client_move_onscreen(Client *self)
402 {
403 Rect *a;
404 int x = self->frame->area.x, y = self->frame->area.y;
405
406 a = screen_area(self->desktop);
407 if (x >= a->x + a->width - 1)
408 x = a->x + a->width - self->frame->area.width;
409 if (y >= a->y + a->height - 1)
410 y = a->y + a->height - self->frame->area.height;
411 if (x + self->frame->area.width - 1 < a->x)
412 x = a->x;
413 if (y + self->frame->area.height - 1< a->y)
414 y = a->y;
415
416 frame_frame_gravity(self->frame, &x, &y); /* get where the client
417 should be */
418 client_configure(self , Corner_TopLeft, x, y,
419 self->area.width, self->area.height,
420 TRUE, TRUE);
421 }
422
423 static void client_toggle_border(Client *self, gboolean show)
424 {
425 /* adjust our idea of where the client is, based on its border. When the
426 border is removed, the client should now be considered to be in a
427 different position.
428 when re-adding the border to the client, the same operation needs to be
429 reversed. */
430 int oldx = self->area.x, oldy = self->area.y;
431 int x = oldx, y = oldy;
432 switch(self->gravity) {
433 default:
434 case NorthWestGravity:
435 case WestGravity:
436 case SouthWestGravity:
437 break;
438 case NorthEastGravity:
439 case EastGravity:
440 case SouthEastGravity:
441 if (show) x -= self->border_width * 2;
442 else x += self->border_width * 2;
443 break;
444 case NorthGravity:
445 case SouthGravity:
446 case CenterGravity:
447 case ForgetGravity:
448 case StaticGravity:
449 if (show) x -= self->border_width;
450 else x += self->border_width;
451 break;
452 }
453 switch(self->gravity) {
454 default:
455 case NorthWestGravity:
456 case NorthGravity:
457 case NorthEastGravity:
458 break;
459 case SouthWestGravity:
460 case SouthGravity:
461 case SouthEastGravity:
462 if (show) y -= self->border_width * 2;
463 else y += self->border_width * 2;
464 break;
465 case WestGravity:
466 case EastGravity:
467 case CenterGravity:
468 case ForgetGravity:
469 case StaticGravity:
470 if (show) y -= self->border_width;
471 else y += self->border_width;
472 break;
473 }
474 self->area.x = x;
475 self->area.y = y;
476
477 if (show) {
478 XSetWindowBorderWidth(ob_display, self->window, self->border_width);
479
480 /* move the client so it is back it the right spot _with_ its
481 border! */
482 if (x != oldx || y != oldy)
483 XMoveWindow(ob_display, self->window, x, y);
484 } else
485 XSetWindowBorderWidth(ob_display, self->window, 0);
486 }
487
488
489 static void client_get_all(Client *self)
490 {
491 /* update EVERYTHING!! */
492
493 self->ignore_unmaps = 0;
494
495 /* defaults */
496 self->frame = NULL;
497 self->title = self->icon_title = NULL;
498 self->name = self->class = self->role = NULL;
499 self->wmstate = NormalState;
500 self->transient = FALSE;
501 self->transients = NULL;
502 self->transient_for = NULL;
503 self->layer = -1;
504 self->urgent = FALSE;
505 self->positioned = FALSE;
506 self->disabled_decorations = 0;
507 self->group = NULL;
508 self->nicons = 0;
509
510 client_get_area(self);
511 client_update_transient_for(self);
512 client_update_wmhints(self);
513 client_get_desktop(self);
514 client_get_state(self);
515 client_get_shaped(self);
516
517 client_get_mwm_hints(self);
518 client_get_type(self);/* this can change the mwmhints for special cases */
519
520 client_update_protocols(self);
521
522 client_get_gravity(self); /* get the attribute gravity */
523 client_update_normal_hints(self); /* this may override the attribute
524 gravity */
525
526 /* got the type, the mwmhints, the protocols, and the normal hints
527 (min/max sizes), so we're ready to set up the decorations/functions */
528 client_setup_decor_and_functions(self);
529
530 client_update_title(self);
531 client_update_icon_title(self);
532 client_update_class(self);
533 client_update_strut(self);
534 client_update_icons(self);
535 client_update_kwm_icon(self);
536
537 client_change_state(self);
538 }
539
540 static void client_get_area(Client *self)
541 {
542 XWindowAttributes wattrib;
543 Status ret;
544
545 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
546 g_assert(ret != BadWindow);
547
548 RECT_SET(self->area, wattrib.x, wattrib.y, wattrib.width, wattrib.height);
549 self->border_width = wattrib.border_width;
550 }
551
552 static void client_get_desktop(Client *self)
553 {
554 guint32 d;
555
556 if (PROP_GET32(self->window, net_wm_desktop, cardinal, &d)) {
557 if (d >= screen_num_desktops && d != DESKTOP_ALL)
558 d = screen_num_desktops - 1;
559 self->desktop = d;
560 } else {
561 gboolean trdesk = FALSE;
562
563 if (self->transient_for) {
564 if (self->transient_for != TRAN_GROUP) {
565 self->desktop = self->transient_for->desktop;
566 trdesk = TRUE;
567 } else {
568 GSList *it;
569
570 for (it = self->group->members; it; it = it->next)
571 if (it->data != self &&
572 ((Client*)it->data)->transient_for != TRAN_GROUP) {
573 self->desktop = ((Client*)it->data)->desktop;
574 trdesk = TRUE;
575 break;
576 }
577 }
578 }
579 if (!trdesk)
580 /* defaults to the current desktop */
581 self->desktop = screen_desktop;
582
583 /* set the desktop hint, to make sure that it always exists */
584 PROP_SET32(self->window, net_wm_desktop, cardinal, self->desktop);
585 }
586 }
587
588 static void client_get_state(Client *self)
589 {
590 guint32 *state;
591 guint num;
592
593 self->modal = self->shaded = self->max_horz = self->max_vert =
594 self->fullscreen = self->above = self->below = self->iconic =
595 self->skip_taskbar = self->skip_pager = FALSE;
596
597 if (PROP_GETA32(self->window, net_wm_state, atom, &state, &num)) {
598 gulong i;
599 for (i = 0; i < num; ++i) {
600 if (state[i] == prop_atoms.net_wm_state_modal)
601 self->modal = TRUE;
602 else if (state[i] == prop_atoms.net_wm_state_shaded)
603 self->shaded = TRUE;
604 else if (state[i] == prop_atoms.net_wm_state_hidden)
605 self->iconic = TRUE;
606 else if (state[i] == prop_atoms.net_wm_state_skip_taskbar)
607 self->skip_taskbar = TRUE;
608 else if (state[i] == prop_atoms.net_wm_state_skip_pager)
609 self->skip_pager = TRUE;
610 else if (state[i] == prop_atoms.net_wm_state_fullscreen)
611 self->fullscreen = TRUE;
612 else if (state[i] == prop_atoms.net_wm_state_maximized_vert)
613 self->max_vert = TRUE;
614 else if (state[i] == prop_atoms.net_wm_state_maximized_horz)
615 self->max_horz = TRUE;
616 else if (state[i] == prop_atoms.net_wm_state_above)
617 self->above = TRUE;
618 else if (state[i] == prop_atoms.net_wm_state_below)
619 self->below = TRUE;
620 }
621
622 g_free(state);
623 }
624 }
625
626 static void client_get_shaped(Client *self)
627 {
628 self->shaped = FALSE;
629 #ifdef SHAPE
630 if (extensions_shape) {
631 int foo;
632 guint ufoo;
633 int s;
634
635 XShapeSelectInput(ob_display, self->window, ShapeNotifyMask);
636
637 XShapeQueryExtents(ob_display, self->window, &s, &foo,
638 &foo, &ufoo, &ufoo, &foo, &foo, &foo, &ufoo,
639 &ufoo);
640 self->shaped = (s != 0);
641 }
642 #endif
643 }
644
645 void client_update_transient_for(Client *self)
646 {
647 Window t = None;
648 Client *c = NULL;
649
650 if (XGetTransientForHint(ob_display, self->window, &t)) {
651 self->transient = TRUE;
652 if (t != self->window) { /* cant be transient to itself! */
653 c = g_hash_table_lookup(client_map, &t);
654 /* if this happens then we need to check for it*/
655 g_assert(c != self);
656
657 if (!c && self->group) {
658 /* not transient to a client, see if it is transient for a
659 group */
660 if (t == self->group->leader ||
661 t == None ||
662 t == ob_root) {
663 /* window is a transient for its group! */
664 c = TRAN_GROUP;
665 }
666 }
667 }
668 } else
669 self->transient = FALSE;
670
671 /* if anything has changed... */
672 if (c != self->transient_for) {
673 if (self->transient_for == TRAN_GROUP) { /* transient of group */
674 GSList *it;
675
676 /* remove from old parents */
677 for (it = self->group->members; it; it = it->next)
678 if (it->data != self &&
679 (((Client*)it->data)->transient_for != TRAN_GROUP))
680 ((Client*)it->data)->transients =
681 g_slist_remove(((Client*)it->data)->transients, self);
682 } else if (self->transient_for != NULL) { /* transient of window */
683 /* remove from old parent */
684 self->transient_for->transients =
685 g_slist_remove(self->transient_for->transients, self);
686 }
687 self->transient_for = c;
688 if (self->transient_for == TRAN_GROUP) { /* transient of group */
689 GSList *it;
690
691 /* add to new parents */
692 for (it = self->group->members; it; it = it->next)
693 if (it->data != self &&
694 (((Client*)it->data)->transient_for != TRAN_GROUP))
695 ((Client*)it->data)->transients =
696 g_slist_append(((Client*)it->data)->transients, self);
697
698 /* remove all transients which are in the group, that causes
699 circlular pointer hell of doom */
700 for (it = self->group->members; it; it = it->next) {
701 GSList *sit, *next;
702 for (sit = self->transients; sit; sit = next) {
703 next = sit->next;
704 if (sit->data == it->data)
705 self->transients = g_slist_remove(self->transients,
706 sit->data);
707 }
708 }
709 } else if (self->transient_for != NULL) { /* transient of window */
710 /* add to new parent */
711 self->transient_for->transients =
712 g_slist_append(self->transient_for->transients, self);
713 }
714 }
715 }
716
717 static void client_get_mwm_hints(Client *self)
718 {
719 guint num;
720 guint32 *hints;
721
722 self->mwmhints.flags = 0; /* default to none */
723
724 if (PROP_GETA32(self->window, motif_wm_hints, motif_wm_hints,
725 &hints, &num)) {
726 if (num >= MWM_ELEMENTS) {
727 self->mwmhints.flags = hints[0];
728 self->mwmhints.functions = hints[1];
729 self->mwmhints.decorations = hints[2];
730 }
731 g_free(hints);
732 }
733 }
734
735 void client_get_type(Client *self)
736 {
737 guint num, i;
738 guint32 *val;
739
740 self->type = -1;
741
742 if (PROP_GETA32(self->window, net_wm_window_type, atom, &val, &num)) {
743 /* use the first value that we know about in the array */
744 for (i = 0; i < num; ++i) {
745 if (val[i] == prop_atoms.net_wm_window_type_desktop)
746 self->type = Type_Desktop;
747 else if (val[i] == prop_atoms.net_wm_window_type_dock)
748 self->type = Type_Dock;
749 else if (val[i] == prop_atoms.net_wm_window_type_toolbar)
750 self->type = Type_Toolbar;
751 else if (val[i] == prop_atoms.net_wm_window_type_menu)
752 self->type = Type_Menu;
753 else if (val[i] == prop_atoms.net_wm_window_type_utility)
754 self->type = Type_Utility;
755 else if (val[i] == prop_atoms.net_wm_window_type_splash)
756 self->type = Type_Splash;
757 else if (val[i] == prop_atoms.net_wm_window_type_dialog)
758 self->type = Type_Dialog;
759 else if (val[i] == prop_atoms.net_wm_window_type_normal)
760 self->type = Type_Normal;
761 else if (val[i] == prop_atoms.kde_net_wm_window_type_override) {
762 /* prevent this window from getting any decor or
763 functionality */
764 self->mwmhints.flags &= (MwmFlag_Functions |
765 MwmFlag_Decorations);
766 self->mwmhints.decorations = 0;
767 self->mwmhints.functions = 0;
768 }
769 if (self->type != (WindowType) -1)
770 break; /* grab the first legit type */
771 }
772 g_free(val);
773 }
774
775 if (self->type == (WindowType) -1) {
776 /*the window type hint was not set, which means we either classify
777 ourself as a normal window or a dialog, depending on if we are a
778 transient. */
779 if (self->transient)
780 self->type = Type_Dialog;
781 else
782 self->type = Type_Normal;
783 }
784
785 /* this makes sure that these windows appear on all desktops */
786 if (self->type == Type_Desktop)
787 self->desktop = DESKTOP_ALL;
788 }
789
790 void client_update_protocols(Client *self)
791 {
792 guint32 *proto;
793 guint num_return, i;
794
795 self->focus_notify = FALSE;
796 self->delete_window = FALSE;
797
798 if (PROP_GETA32(self->window, wm_protocols, atom, &proto, &num_return)) {
799 for (i = 0; i < num_return; ++i) {
800 if (proto[i] == prop_atoms.wm_delete_window) {
801 /* this means we can request the window to close */
802 self->delete_window = TRUE;
803 } else if (proto[i] == prop_atoms.wm_take_focus)
804 /* if this protocol is requested, then the window will be
805 notified whenever we want it to receive focus */
806 self->focus_notify = TRUE;
807 }
808 g_free(proto);
809 }
810 }
811
812 static void client_get_gravity(Client *self)
813 {
814 XWindowAttributes wattrib;
815 Status ret;
816
817 ret = XGetWindowAttributes(ob_display, self->window, &wattrib);
818 g_assert(ret != BadWindow);
819 self->gravity = wattrib.win_gravity;
820 }
821
822 void client_update_normal_hints(Client *self)
823 {
824 XSizeHints size;
825 long ret;
826 int oldgravity = self->gravity;
827
828 /* defaults */
829 self->min_ratio = 0.0f;
830 self->max_ratio = 0.0f;
831 SIZE_SET(self->size_inc, 1, 1);
832 SIZE_SET(self->base_size, 0, 0);
833 SIZE_SET(self->min_size, 0, 0);
834 SIZE_SET(self->max_size, G_MAXINT, G_MAXINT);
835
836 /* get the hints from the window */
837 if (XGetWMNormalHints(ob_display, self->window, &size, &ret)) {
838 self->positioned = !!(size.flags & (PPosition|USPosition));
839
840 if (size.flags & PWinGravity) {
841 self->gravity = size.win_gravity;
842
843 /* if the client has a frame, i.e. has already been mapped and
844 is changing its gravity */
845 if (self->frame && self->gravity != oldgravity) {
846 /* move our idea of the client's position based on its new
847 gravity */
848 self->area.x = self->frame->area.x;
849 self->area.y = self->frame->area.y;
850 frame_frame_gravity(self->frame, &self->area.x, &self->area.y);
851 }
852 }
853
854 if (size.flags & PAspect) {
855 if (size.min_aspect.y)
856 self->min_ratio = (float)size.min_aspect.x / size.min_aspect.y;
857 if (size.max_aspect.y)
858 self->max_ratio = (float)size.max_aspect.x / size.max_aspect.y;
859 }
860
861 if (size.flags & PMinSize)
862 SIZE_SET(self->min_size, size.min_width, size.min_height);
863
864 if (size.flags & PMaxSize)
865 SIZE_SET(self->max_size, size.max_width, size.max_height);
866
867 if (size.flags & PBaseSize)
868 SIZE_SET(self->base_size, size.base_width, size.base_height);
869
870 if (size.flags & PResizeInc)
871 SIZE_SET(self->size_inc, size.width_inc, size.height_inc);
872 }
873 }
874
875 void client_setup_decor_and_functions(Client *self)
876 {
877 /* start with everything (cept fullscreen) */
878 self->decorations = Decor_Titlebar | Decor_Handle | Decor_Border |
879 Decor_Icon | Decor_AllDesktops | Decor_Iconify | Decor_Maximize |
880 Decor_Shade;
881 self->functions = Func_Resize | Func_Move | Func_Iconify | Func_Maximize |
882 Func_Shade;
883 if (self->delete_window) {
884 self->decorations |= Decor_Close;
885 self->functions |= Func_Close;
886 }
887
888 if (!(self->min_size.width < self->max_size.width ||
889 self->min_size.height < self->max_size.height)) {
890 self->decorations &= ~(Decor_Maximize | Decor_Handle);
891 self->functions &= ~(Func_Resize | Func_Maximize);
892 }
893
894 switch (self->type) {
895 case Type_Normal:
896 /* normal windows retain all of the possible decorations and
897 functionality, and are the only windows that you can fullscreen */
898 self->functions |= Func_Fullscreen;
899 break;
900
901 case Type_Dialog:
902 case Type_Utility:
903 /* these windows cannot be maximized */
904 self->decorations &= ~Decor_Maximize;
905 self->functions &= ~Func_Maximize;
906 break;
907
908 case Type_Menu:
909 case Type_Toolbar:
910 /* these windows get less functionality */
911 self->decorations &= ~(Decor_Iconify | Decor_Handle);
912 self->functions &= ~(Func_Iconify | Func_Resize);
913 break;
914
915 case Type_Desktop:
916 case Type_Dock:
917 case Type_Splash:
918 /* none of these windows are manipulated by the window manager */
919 self->decorations = 0;
920 self->functions = 0;
921 break;
922 }
923
924 /* Mwm Hints are applied subtractively to what has already been chosen for
925 decor and functionality */
926 if (self->mwmhints.flags & MwmFlag_Decorations) {
927 if (! (self->mwmhints.decorations & MwmDecor_All)) {
928 if (! (self->mwmhints.decorations & MwmDecor_Border))
929 self->decorations &= ~Decor_Border;
930 if (! (self->mwmhints.decorations & MwmDecor_Handle))
931 self->decorations &= ~Decor_Handle;
932 if (! (self->mwmhints.decorations & MwmDecor_Title))
933 self->decorations &= ~Decor_Titlebar;
934 if (! (self->mwmhints.decorations & MwmDecor_Iconify))
935 self->decorations &= ~Decor_Iconify;
936 if (! (self->mwmhints.decorations & MwmDecor_Maximize))
937 self->decorations &= ~Decor_Maximize;
938 }
939 }
940
941 if (self->mwmhints.flags & MwmFlag_Functions) {
942 if (! (self->mwmhints.functions & MwmFunc_All)) {
943 if (! (self->mwmhints.functions & MwmFunc_Resize))
944 self->functions &= ~Func_Resize;
945 if (! (self->mwmhints.functions & MwmFunc_Move))
946 self->functions &= ~Func_Move;
947 if (! (self->mwmhints.functions & MwmFunc_Iconify))
948 self->functions &= ~Func_Iconify;
949 if (! (self->mwmhints.functions & MwmFunc_Maximize))
950 self->functions &= ~Func_Maximize;
951 /* dont let mwm hints kill the close button
952 if (! (self->mwmhints.functions & MwmFunc_Close))
953 self->functions &= ~Func_Close; */
954 }
955 }
956
957 /* can't maximize without moving/resizing */
958 if (!((self->functions & Func_Move) && (self->functions & Func_Resize)))
959 self->functions &= ~(Func_Maximize | Func_Fullscreen);
960
961 /* finally, user specified disabled decorations are applied to subtract
962 decorations */
963 if (self->disabled_decorations & Decor_Titlebar)
964 self->decorations &= ~Decor_Titlebar;
965 if (self->disabled_decorations & Decor_Handle)
966 self->decorations &= ~Decor_Handle;
967 if (self->disabled_decorations & Decor_Border)
968 self->decorations &= ~Decor_Border;
969 if (self->disabled_decorations & Decor_Iconify)
970 self->decorations &= ~Decor_Iconify;
971 if (self->disabled_decorations & Decor_Maximize)
972 self->decorations &= ~Decor_Maximize;
973 if (self->disabled_decorations & Decor_AllDesktops)
974 self->decorations &= ~Decor_AllDesktops;
975 if (self->disabled_decorations & Decor_Shade)
976 self->decorations &= ~Decor_Shade;
977 if (self->disabled_decorations & Decor_Close)
978 self->decorations &= ~Decor_Close;
979
980 /* if we don't have a titlebar, then we cannot shade! */
981 if (!(self->decorations & Decor_Titlebar))
982 self->functions &= ~Func_Shade;
983
984 /* now we need to check against rules for the client's current state */
985 if (self->fullscreen) {
986 self->functions &= (Func_Close | Func_Fullscreen | Func_Iconify);
987 self->decorations = 0;
988 }
989
990 client_change_allowed_actions(self);
991
992 if (self->frame) {
993 /* change the decors on the frame, and with more/less decorations,
994 we may also need to be repositioned */
995 frame_adjust_area(self->frame, TRUE, TRUE);
996 /* with new decor, the window's maximized size may change */
997 client_remaximize(self);
998 }
999 }
1000
1001 static void client_change_allowed_actions(Client *self)
1002 {
1003 guint32 actions[9];
1004 int num = 0;
1005
1006 actions[num++] = prop_atoms.net_wm_action_change_desktop;
1007
1008 if (self->functions & Func_Shade)
1009 actions[num++] = prop_atoms.net_wm_action_shade;
1010 if (self->functions & Func_Close)
1011 actions[num++] = prop_atoms.net_wm_action_close;
1012 if (self->functions & Func_Move)
1013 actions[num++] = prop_atoms.net_wm_action_move;
1014 if (self->functions & Func_Iconify)
1015 actions[num++] = prop_atoms.net_wm_action_minimize;
1016 if (self->functions & Func_Resize)
1017 actions[num++] = prop_atoms.net_wm_action_resize;
1018 if (self->functions & Func_Fullscreen)
1019 actions[num++] = prop_atoms.net_wm_action_fullscreen;
1020 if (self->functions & Func_Maximize) {
1021 actions[num++] = prop_atoms.net_wm_action_maximize_horz;
1022 actions[num++] = prop_atoms.net_wm_action_maximize_vert;
1023 }
1024
1025 PROP_SETA32(self->window, net_wm_allowed_actions, atom, actions, num);
1026
1027 /* make sure the window isn't breaking any rules now */
1028
1029 if (!(self->functions & Func_Shade) && self->shaded) {
1030 if (self->frame) client_shade(self, FALSE);
1031 else self->shaded = FALSE;
1032 }
1033 if (!(self->functions & Func_Iconify) && self->iconic) {
1034 g_message("UNSETTING ICONIC");
1035 if (self->frame) client_iconify(self, FALSE, TRUE);
1036 else self->iconic = FALSE;
1037 }
1038 if (!(self->functions & Func_Fullscreen) && self->fullscreen) {
1039 if (self->frame) client_fullscreen(self, FALSE, TRUE);
1040 else self->fullscreen = FALSE;
1041 }
1042 if (!(self->functions & Func_Maximize) && (self->max_horz ||
1043 self->max_vert)) {
1044 if (self->frame) client_maximize(self, FALSE, 0, TRUE);
1045 else self->max_vert = self->max_horz = FALSE;
1046 }
1047 }
1048
1049 void client_remaximize(Client *self)
1050 {
1051 int dir;
1052 if (self->max_horz && self->max_vert)
1053 dir = 0;
1054 else if (self->max_horz)
1055 dir = 1;
1056 else if (self->max_vert)
1057 dir = 2;
1058 else
1059 return; /* not maximized */
1060 self->max_horz = self->max_vert = FALSE;
1061 client_maximize(self, TRUE, dir, FALSE);
1062 }
1063
1064 void client_update_wmhints(Client *self)
1065 {
1066 XWMHints *hints;
1067 gboolean ur = FALSE;
1068 GSList *it;
1069
1070 /* assume a window takes input if it doesnt specify */
1071 self->can_focus = TRUE;
1072
1073 if ((hints = XGetWMHints(ob_display, self->window)) != NULL) {
1074 if (hints->flags & InputHint)
1075 self->can_focus = hints->input;
1076
1077 /* only do this when first managing the window *AND* when we aren't
1078 starting up! */
1079 if (ob_state != State_Starting && self->frame == NULL)
1080 if (hints->flags & StateHint)
1081 self->iconic = hints->initial_state == IconicState;
1082
1083 if (hints->flags & XUrgencyHint)
1084 ur = TRUE;
1085
1086 if (!(hints->flags & WindowGroupHint))
1087 hints->window_group = None;
1088
1089 /* did the group state change? */
1090 if (hints->window_group !=
1091 (self->group ? self->group->leader : None)) {
1092 /* remove from the old group if there was one */
1093 if (self->group != NULL) {
1094 /* remove transients of the group */
1095 for (it = self->group->members; it; it = it->next)
1096 if (it->data != self &&
1097 ((Client*)it->data)->transient_for == TRAN_GROUP) {
1098 self->transients = g_slist_remove(self->transients,
1099 it->data);
1100 }
1101 group_remove(self->group, self);
1102 self->group = NULL;
1103 }
1104 if (hints->window_group != None) {
1105 self->group = group_add(hints->window_group, self);
1106
1107 /* add other transients of the group that are already
1108 set up */
1109 for (it = self->group->members; it; it = it->next)
1110 if (it->data != self &&
1111 ((Client*)it->data)->transient_for == TRAN_GROUP)
1112 self->transients = g_slist_append(self->transients,
1113 it->data);
1114 }
1115
1116 /* because the self->transient flag wont change from this call,
1117 we don't need to update the window's type and such, only its
1118 transient_for, and the transients lists of other windows in
1119 the group may be affected */
1120 client_update_transient_for(self);
1121 }
1122
1123 client_update_kwm_icon(self);
1124 /* try get the kwm icon first, this is a fallback only */
1125 if (self->pixmap_icon == None) {
1126 if (hints->flags & IconPixmapHint) {
1127 if (self->pixmap_icon == None) {
1128 self->pixmap_icon = hints->icon_pixmap;
1129 if (hints->flags & IconMaskHint)
1130 self->pixmap_icon_mask = hints->icon_mask;
1131 else
1132 self->pixmap_icon_mask = None;
1133
1134 if (self->frame)
1135 frame_adjust_icon(self->frame);
1136 }
1137 }
1138 }
1139
1140 XFree(hints);
1141 }
1142
1143 if (ur != self->urgent) {
1144 self->urgent = ur;
1145 g_message("Urgent Hint for 0x%lx: %s", self->window,
1146 ur ? "ON" : "OFF");
1147 /* fire the urgent callback if we're mapped, otherwise, wait until
1148 after we're mapped */
1149 if (self->frame)
1150 dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1151 }
1152 }
1153
1154 void client_update_title(Client *self)
1155 {
1156 char *data = NULL;
1157
1158 g_free(self->title);
1159
1160 /* try netwm */
1161 if (!PROP_GETS(self->window, net_wm_name, utf8, &data))
1162 /* try old x stuff */
1163 if (!PROP_GETS(self->window, wm_name, locale, &data))
1164 data = g_strdup("Unnamed Window");
1165
1166 /* look for duplicates and append a number */
1167
1168 PROP_SETS(self->window, net_wm_visible_name, data);
1169
1170 self->title = data;
1171
1172 if (self->frame)
1173 frame_adjust_title(self->frame);
1174 }
1175
1176 void client_update_icon_title(Client *self)
1177 {
1178 char *data = NULL;
1179
1180 g_free(self->icon_title);
1181
1182 /* try netwm */
1183 if (!PROP_GETS(self->window, net_wm_icon_name, utf8, &data))
1184 /* try old x stuff */
1185 if (!PROP_GETS(self->window, wm_icon_name, locale, &data))
1186 data = g_strdup("Unnamed Window");
1187
1188 PROP_SETS(self->window, net_wm_visible_icon_name, data);
1189
1190 self->icon_title = data;
1191 }
1192
1193 void client_update_class(Client *self)
1194 {
1195 char **data;
1196 char *s;
1197
1198 if (self->name) g_free(self->name);
1199 if (self->class) g_free(self->class);
1200 if (self->role) g_free(self->role);
1201
1202 self->name = self->class = self->role = NULL;
1203
1204 if (PROP_GETSS(self->window, wm_class, locale, &data)) {
1205 if (data[0]) {
1206 self->name = g_strdup(data[0]);
1207 if (data[1])
1208 self->class = g_strdup(data[1]);
1209 }
1210 g_strfreev(data);
1211 }
1212
1213 if (PROP_GETS(self->window, wm_window_role, locale, &s))
1214 self->role = g_strdup(s);
1215
1216 if (self->name == NULL) self->name = g_strdup("");
1217 if (self->class == NULL) self->class = g_strdup("");
1218 if (self->role == NULL) self->role = g_strdup("");
1219 }
1220
1221 void client_update_strut(Client *self)
1222 {
1223 guint num;
1224 guint32 *data;
1225
1226 if (!PROP_GETA32(self->window, net_wm_strut, cardinal, &data, &num)) {
1227 STRUT_SET(self->strut, 0, 0, 0, 0);
1228 } else {
1229 if (num == 4)
1230 STRUT_SET(self->strut, data[0], data[2], data[1], data[3]);
1231 else
1232 STRUT_SET(self->strut, 0, 0, 0, 0);
1233 g_free(data);
1234 }
1235
1236 /* updating here is pointless while we're being mapped cuz we're not in
1237 the client list yet */
1238 if (self->frame)
1239 screen_update_struts();
1240 }
1241
1242 void client_update_icons(Client *self)
1243 {
1244 guint num;
1245 guint32 *data;
1246 guint w, h, i;
1247 int j;
1248
1249 for (j = 0; j < self->nicons; ++j)
1250 g_free(self->icons[j].data);
1251 if (self->nicons > 0)
1252 g_free(self->icons);
1253 self->nicons = 0;
1254
1255 if (PROP_GETA32(self->window, net_wm_icon, cardinal, &data, &num)) {
1256 /* figure out how many valid icons are in here */
1257 i = 0;
1258 while (num - i > 2) {
1259 w = data[i++];
1260 h = data[i++];
1261 i += w * h;
1262 if (i > num) break;
1263 ++self->nicons;
1264 }
1265
1266 self->icons = g_new(Icon, self->nicons);
1267
1268 /* store the icons */
1269 i = 0;
1270 for (j = 0; j < self->nicons; ++j) {
1271 w = self->icons[j].width = data[i++];
1272 h = self->icons[j].height = data[i++];
1273 self->icons[j].data =
1274 g_memdup(&data[i], w * h * sizeof(gulong));
1275 i += w * h;
1276 g_assert(i <= num);
1277 }
1278
1279 g_free(data);
1280 }
1281
1282 if (self->frame)
1283 frame_adjust_icon(self->frame);
1284 }
1285
1286 void client_update_kwm_icon(Client *self)
1287 {
1288 guint num;
1289 guint32 *data;
1290
1291 if (!PROP_GETA32(self->window, kwm_win_icon, kwm_win_icon, &data, &num)) {
1292 self->pixmap_icon = self->pixmap_icon_mask = None;
1293 } else {
1294 if (num == 2) {
1295 self->pixmap_icon = data[0];
1296 self->pixmap_icon_mask = data[1];
1297 } else
1298 self->pixmap_icon = self->pixmap_icon_mask = None;
1299 g_free(data);
1300 }
1301 if (self->frame)
1302 frame_adjust_icon(self->frame);
1303 }
1304
1305 static void client_change_state(Client *self)
1306 {
1307 guint32 state[2];
1308 guint32 netstate[10];
1309 guint num;
1310
1311 state[0] = self->wmstate;
1312 state[1] = None;
1313 PROP_SETA32(self->window, wm_state, wm_state, state, 2);
1314
1315 num = 0;
1316 if (self->modal)
1317 netstate[num++] = prop_atoms.net_wm_state_modal;
1318 if (self->shaded)
1319 netstate[num++] = prop_atoms.net_wm_state_shaded;
1320 if (self->iconic)
1321 netstate[num++] = prop_atoms.net_wm_state_hidden;
1322 if (self->skip_taskbar)
1323 netstate[num++] = prop_atoms.net_wm_state_skip_taskbar;
1324 if (self->skip_pager)
1325 netstate[num++] = prop_atoms.net_wm_state_skip_pager;
1326 if (self->fullscreen)
1327 netstate[num++] = prop_atoms.net_wm_state_fullscreen;
1328 if (self->max_vert)
1329 netstate[num++] = prop_atoms.net_wm_state_maximized_vert;
1330 if (self->max_horz)
1331 netstate[num++] = prop_atoms.net_wm_state_maximized_horz;
1332 if (self->above)
1333 netstate[num++] = prop_atoms.net_wm_state_above;
1334 if (self->below)
1335 netstate[num++] = prop_atoms.net_wm_state_below;
1336 PROP_SETA32(self->window, net_wm_state, atom, netstate, num);
1337
1338 client_calc_layer(self);
1339
1340 if (self->frame)
1341 frame_adjust_state(self->frame);
1342 }
1343
1344 static Client *search_focus_tree(Client *node, Client *skip)
1345 {
1346 GSList *it;
1347 Client *ret;
1348
1349 for (it = node->transients; it != NULL; it = it->next) {
1350 Client *c = it->data;
1351 if (c == skip) continue; /* circular? */
1352 if ((ret = search_focus_tree(c, skip))) return ret;
1353 if (client_focused(c)) return c;
1354 }
1355 return NULL;
1356 }
1357
1358 static void calc_recursive(Client *self, StackLayer l, gboolean raised)
1359 {
1360 StackLayer old;
1361 GSList *it;
1362
1363 old = self->layer;
1364 self->layer = l;
1365
1366 for (it = self->transients; it; it = it->next)
1367 calc_recursive(it->data, l, raised ? raised : l != old);
1368
1369 if (!raised && l != old)
1370 if (self->frame)
1371 stacking_raise(self);
1372 }
1373
1374 void client_calc_layer(Client *self)
1375 {
1376 StackLayer l;
1377 gboolean f;
1378
1379 /* transients take on the layer of their parents */
1380 if (self->transient_for) {
1381 if (self->transient_for != TRAN_GROUP) {
1382 self = self->transient_for;
1383 } else {
1384 GSList *it;
1385
1386 for (it = self->group->members; it; it = it->next)
1387 if (it->data != self &&
1388 ((Client*)it->data)->transient_for != TRAN_GROUP) {
1389 self = it->data;
1390 break;
1391 }
1392 }
1393 }
1394
1395 /* is us or one of our transients focused? */
1396 if (client_focused(self))
1397 f = TRUE;
1398 else if (search_focus_tree(self, self))
1399 f = TRUE;
1400 else
1401 f = FALSE;
1402
1403 if (self->iconic) l = Layer_Icon;
1404 /* fullscreen windows are only in the fullscreen layer while focused */
1405 else if (self->fullscreen && f) l = Layer_Fullscreen;
1406 else if (self->type == Type_Desktop) l = Layer_Desktop;
1407 else if (self->type == Type_Dock) {
1408 if (!self->below) l = Layer_Top;
1409 else l = Layer_Normal;
1410 }
1411 else if (self->above) l = Layer_Above;
1412 else if (self->below) l = Layer_Below;
1413 else l = Layer_Normal;
1414
1415 calc_recursive(self, l, FALSE);
1416 }
1417
1418 gboolean client_should_show(Client *self)
1419 {
1420 if (self->iconic) return FALSE;
1421 else if (!(self->desktop == screen_desktop ||
1422 self->desktop == DESKTOP_ALL)) return FALSE;
1423 else if (client_normal(self) && screen_showing_desktop) return FALSE;
1424
1425 return TRUE;
1426 }
1427
1428 static void client_showhide(Client *self)
1429 {
1430
1431 if (client_should_show(self))
1432 frame_show(self->frame);
1433 else
1434 frame_hide(self->frame);
1435 }
1436
1437 gboolean client_normal(Client *self) {
1438 return ! (self->type == Type_Desktop || self->type == Type_Dock ||
1439 self->type == Type_Splash);
1440 }
1441
1442 static void client_apply_startup_state(Client *self)
1443 {
1444 /* these are in a carefully crafted order.. */
1445
1446 if (self->iconic) {
1447 self->iconic = FALSE;
1448 client_iconify(self, TRUE, FALSE);
1449 }
1450 if (self->fullscreen) {
1451 self->fullscreen = FALSE;
1452 client_fullscreen(self, TRUE, FALSE);
1453 }
1454 if (self->shaded) {
1455 self->shaded = FALSE;
1456 client_shade(self, TRUE);
1457 }
1458 if (self->urgent)
1459 dispatch_client(Event_Client_Urgent, self, self->urgent, 0);
1460
1461 if (self->max_vert && self->max_horz) {
1462 self->max_vert = self->max_horz = FALSE;
1463 client_maximize(self, TRUE, 0, FALSE);
1464 } else if (self->max_vert) {
1465 self->max_vert = FALSE;
1466 client_maximize(self, TRUE, 2, FALSE);
1467 } else if (self->max_horz) {
1468 self->max_horz = FALSE;
1469 client_maximize(self, TRUE, 1, FALSE);
1470 }
1471
1472 /* nothing to do for the other states:
1473 skip_taskbar
1474 skip_pager
1475 modal
1476 above
1477 below
1478 */
1479 }
1480
1481 void client_configure(Client *self, Corner anchor, int x, int y, int w, int h,
1482 gboolean user, gboolean final)
1483 {
1484 gboolean moved = FALSE, resized = FALSE;
1485
1486 /* gets the frame's position */
1487 frame_client_gravity(self->frame, &x, &y);
1488
1489 /* these positions are frame positions, not client positions */
1490
1491 /* set the size and position if fullscreen */
1492 if (self->fullscreen) {
1493 x = 0;
1494 y = 0;
1495 w = screen_physical_size.width;
1496 h = screen_physical_size.height;
1497 user = FALSE; /* ignore that increment etc shit when in fullscreen */
1498 } else {
1499 /* set the size and position if maximized */
1500 if (self->max_horz) {
1501 x = screen_area(self->desktop)->x - self->frame->size.left;
1502 w = screen_area(self->desktop)->width;
1503 }
1504 if (self->max_vert) {
1505 y = screen_area(self->desktop)->y;
1506 h = screen_area(self->desktop)->height -
1507 self->frame->size.top - self->frame->size.bottom;
1508 }
1509 }
1510
1511 /* gets the client's position */
1512 frame_frame_gravity(self->frame, &x, &y);
1513
1514 /* these override the above states! if you cant move you can't move! */
1515 if (user) {
1516 if (!(self->functions & Func_Move)) {
1517 x = self->area.x;
1518 y = self->area.y;
1519 }
1520 if (!(self->functions & Func_Resize)) {
1521 w = self->area.width;
1522 h = self->area.height;
1523 }
1524 }
1525
1526 if (!(w == self->area.width && h == self->area.height)) {
1527 w -= self->base_size.width;
1528 h -= self->base_size.height;
1529
1530 if (user) {
1531 /* for interactive resizing. have to move half an increment in each
1532 direction. */
1533
1534 /* how far we are towards the next size inc */
1535 int mw = w % self->size_inc.width;
1536 int mh = h % self->size_inc.height;
1537 /* amount to add */
1538 int aw = self->size_inc.width / 2;
1539 int ah = self->size_inc.height / 2;
1540 /* don't let us move into a new size increment */
1541 if (mw + aw >= self->size_inc.width)
1542 aw = self->size_inc.width - mw - 1;
1543 if (mh + ah >= self->size_inc.height)
1544 ah = self->size_inc.height - mh - 1;
1545 w += aw;
1546 h += ah;
1547
1548 /* if this is a user-requested resize, then check against min/max
1549 sizes and aspect ratios */
1550
1551 /* smaller than min size or bigger than max size? */
1552 if (w > self->max_size.width) w = self->max_size.width;
1553 if (w < self->min_size.width) w = self->min_size.width;
1554 if (h > self->max_size.height) h = self->max_size.height;
1555 if (h < self->min_size.height) h = self->min_size.height;
1556
1557 /* adjust the height ot match the width for the aspect ratios */
1558 if (self->min_ratio)
1559 if (h * self->min_ratio > w) h = (int)(w / self->min_ratio);
1560 if (self->max_ratio)
1561 if (h * self->max_ratio < w) h = (int)(w / self->max_ratio);
1562 }
1563
1564 /* keep to the increments */
1565 w /= self->size_inc.width;
1566 h /= self->size_inc.height;
1567
1568 /* you cannot resize to nothing */
1569 if (w < 1) w = 1;
1570 if (h < 1) h = 1;
1571
1572 /* store the logical size */
1573 SIZE_SET(self->logical_size, w, h);
1574
1575 w *= self->size_inc.width;
1576 h *= self->size_inc.height;
1577
1578 w += self->base_size.width;
1579 h += self->base_size.height;
1580 }
1581
1582 switch (anchor) {
1583 case Corner_TopLeft:
1584 break;
1585 case Corner_TopRight:
1586 x -= w - self->area.width;
1587 break;
1588 case Corner_BottomLeft:
1589 y -= h - self->area.height;
1590 break;
1591 case Corner_BottomRight:
1592 x -= w - self->area.width;
1593 y -= h - self->area.height;
1594 break;
1595 }
1596
1597 moved = x != self->area.x || y != self->area.y;
1598 resized = w != self->area.width || h != self->area.height;
1599
1600 RECT_SET(self->area, x, y, w, h);
1601
1602 if (resized)
1603 XResizeWindow(ob_display, self->window, w, h);
1604
1605 /* move/resize the frame to match the request */
1606 if (self->frame) {
1607 if (moved || resized)
1608 frame_adjust_area(self->frame, moved, resized);
1609
1610 if (!user || final) {
1611 XEvent event;
1612 event.type = ConfigureNotify;
1613 event.xconfigure.display = ob_display;
1614 event.xconfigure.event = self->window;
1615 event.xconfigure.window = self->window;
1616
1617 /* root window coords with border in mind */
1618 event.xconfigure.x = x - self->border_width +
1619 self->frame->size.left;
1620 event.xconfigure.y = y - self->border_width +
1621 self->frame->size.top;
1622
1623 event.xconfigure.width = self->area.width;
1624 event.xconfigure.height = self->area.height;
1625 event.xconfigure.border_width = self->border_width;
1626 event.xconfigure.above = self->frame->plate;
1627 event.xconfigure.override_redirect = FALSE;
1628 XSendEvent(event.xconfigure.display, event.xconfigure.window,
1629 FALSE, StructureNotifyMask, &event);
1630 }
1631 }
1632 }
1633
1634 void client_fullscreen(Client *self, gboolean fs, gboolean savearea)
1635 {
1636 int x, y, w, h;
1637
1638 if (!(self->functions & Func_Fullscreen) || /* can't */
1639 self->fullscreen == fs) return; /* already done */
1640
1641 self->fullscreen = fs;
1642 client_change_state(self); /* change the state hints on the client,
1643 and adjust out layer/stacking */
1644
1645 if (fs) {
1646 if (savearea) {
1647 guint32 dimensions[4];
1648 dimensions[0] = self->area.x;
1649 dimensions[1] = self->area.y;
1650 dimensions[2] = self->area.width;
1651 dimensions[3] = self->area.height;
1652
1653 PROP_SETA32(self->window, openbox_premax, cardinal,
1654 dimensions, 4);
1655 }
1656
1657 /* these are not actually used cuz client_configure will set them
1658 as appropriate when the window is fullscreened */
1659 x = y = w = h = 0;
1660 } else {
1661 guint num;
1662 gint32 *dimensions;
1663
1664 /* pick some fallbacks... */
1665 x = screen_area(self->desktop)->x +
1666 screen_area(self->desktop)->width / 4;
1667 y = screen_area(self->desktop)->y +
1668 screen_area(self->desktop)->height / 4;
1669 w = screen_area(self->desktop)->width / 2;
1670 h = screen_area(self->desktop)->height / 2;
1671
1672 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1673 (guint32**)&dimensions, &num)) {
1674 if (num == 4) {
1675 x = dimensions[0];
1676 y = dimensions[1];
1677 w = dimensions[2];
1678 h = dimensions[3];
1679 }
1680 g_free(dimensions);
1681 }
1682 }
1683
1684 client_setup_decor_and_functions(self);
1685
1686 client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1687
1688 /* try focus us when we go into fullscreen mode */
1689 client_focus(self);
1690 }
1691
1692 void client_iconify(Client *self, gboolean iconic, gboolean curdesk)
1693 {
1694 GSList *it;
1695
1696 /* move up the transient chain as far as possible first if deiconifying */
1697 if (!iconic)
1698 while (self->transient_for) {
1699 if (self->transient_for != TRAN_GROUP) {
1700 if (self->transient_for->iconic == iconic)
1701 break;
1702 self = self->transient_for;
1703 } else {
1704 GSList *it;
1705
1706 /* the check for TRAN_GROUP is to prevent an infinate loop with
1707 2 transients of the same group at the head of the group's
1708 members list */
1709 for (it = self->group->members; it; it = it->next) {
1710 Client *c = it->data;
1711
1712 if (c != self && c->transient_for->iconic != iconic &&
1713 c->transient_for != TRAN_GROUP) {
1714 self = it->data;
1715 break;
1716 }
1717 }
1718 if (it == NULL) break;
1719 }
1720 }
1721
1722 if (self->iconic == iconic) return; /* nothing to do */
1723
1724 g_message("%sconifying window: 0x%lx", (iconic ? "I" : "Uni"),
1725 self->window);
1726
1727 self->iconic = iconic;
1728
1729 if (iconic) {
1730 self->wmstate = IconicState;
1731 self->ignore_unmaps++;
1732 /* we unmap the client itself so that we can get MapRequest events,
1733 and because the ICCCM tells us to! */
1734 XUnmapWindow(ob_display, self->window);
1735
1736 /* update the focus lists.. iconic windows go to the bottom */
1737 focus_order_to_bottom(self);
1738 } else {
1739 if (curdesk)
1740 client_set_desktop(self, screen_desktop, FALSE);
1741 self->wmstate = self->shaded ? IconicState : NormalState;
1742 XMapWindow(ob_display, self->window);
1743 }
1744 client_change_state(self);
1745 client_showhide(self);
1746 screen_update_struts();
1747
1748 dispatch_client(iconic ? Event_Client_Unmapped : Event_Client_Mapped,
1749 self, 0, 0);
1750
1751 /* iconify all transients */
1752 for (it = self->transients; it != NULL; it = it->next)
1753 if (it->data != self) client_iconify(it->data, iconic, curdesk);
1754 }
1755
1756 void client_maximize(Client *self, gboolean max, int dir, gboolean savearea)
1757 {
1758 int x, y, w, h;
1759
1760 g_assert(dir == 0 || dir == 1 || dir == 2);
1761 if (!(self->functions & Func_Maximize)) return; /* can't */
1762
1763 /* check if already done */
1764 if (max) {
1765 if (dir == 0 && self->max_horz && self->max_vert) return;
1766 if (dir == 1 && self->max_horz) return;
1767 if (dir == 2 && self->max_vert) return;
1768 } else {
1769 if (dir == 0 && !self->max_horz && !self->max_vert) return;
1770 if (dir == 1 && !self->max_horz) return;
1771 if (dir == 2 && !self->max_vert) return;
1772 }
1773
1774 /* work with the frame's coords */
1775 x = self->frame->area.x;
1776 y = self->frame->area.y;
1777 w = self->area.width;
1778 h = self->area.height;
1779
1780 if (max) {
1781 if (savearea) {
1782 gint32 dimensions[4];
1783 gint32 *readdim;
1784 guint num;
1785
1786 dimensions[0] = x;
1787 dimensions[1] = y;
1788 dimensions[2] = w;
1789 dimensions[3] = h;
1790
1791 /* get the property off the window and use it for the dimensions
1792 we are already maxed on */
1793 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1794 (guint32**)&readdim, &num)) {
1795 if (num == 4) {
1796 if (self->max_horz) {
1797 dimensions[0] = readdim[0];
1798 dimensions[2] = readdim[2];
1799 }
1800 if (self->max_vert) {
1801 dimensions[1] = readdim[1];
1802 dimensions[3] = readdim[3];
1803 }
1804 }
1805 g_free(readdim);
1806 }
1807
1808 PROP_SETA32(self->window, openbox_premax, cardinal,
1809 (guint32*)dimensions, 4);
1810 }
1811 } else {
1812 guint num;
1813 gint32 *dimensions;
1814
1815 /* pick some fallbacks... */
1816 if (dir == 0 || dir == 1) { /* horz */
1817 x = screen_area(self->desktop)->x +
1818 screen_area(self->desktop)->width / 4;
1819 w = screen_area(self->desktop)->width / 2;
1820 }
1821 if (dir == 0 || dir == 2) { /* vert */
1822 y = screen_area(self->desktop)->y +
1823 screen_area(self->desktop)->height / 4;
1824 h = screen_area(self->desktop)->height / 2;
1825 }
1826
1827 if (PROP_GETA32(self->window, openbox_premax, cardinal,
1828 (guint32**)&dimensions, &num)) {
1829 if (num == 4) {
1830 if (dir == 0 || dir == 1) { /* horz */
1831 x = dimensions[0];
1832 w = dimensions[2];
1833 }
1834 if (dir == 0 || dir == 2) { /* vert */
1835 y = dimensions[1];
1836 h = dimensions[3];
1837 }
1838 }
1839 g_free(dimensions);
1840 }
1841 }
1842
1843 if (dir == 0 || dir == 1) /* horz */
1844 self->max_horz = max;
1845 if (dir == 0 || dir == 2) /* vert */
1846 self->max_vert = max;
1847
1848 if (!self->max_horz && !self->max_vert)
1849 PROP_ERASE(self->window, openbox_premax);
1850
1851 client_change_state(self); /* change the state hints on the client */
1852
1853 /* figure out where the client should be going */
1854 frame_frame_gravity(self->frame, &x, &y);
1855 client_configure(self, Corner_TopLeft, x, y, w, h, TRUE, TRUE);
1856 }
1857
1858 void client_shade(Client *self, gboolean shade)
1859 {
1860 if ((!(self->functions & Func_Shade) && shade) || /* can't shade */
1861 self->shaded == shade) return; /* already done */
1862
1863 /* when we're iconic, don't change the wmstate */
1864 if (!self->iconic)
1865 self->wmstate = shade ? IconicState : NormalState;
1866 self->shaded = shade;
1867 client_change_state(self);
1868 /* resize the frame to just the titlebar */
1869 frame_adjust_area(self->frame, FALSE, FALSE);
1870 }
1871
1872 void client_close(Client *self)
1873 {
1874 XEvent ce;
1875
1876 if (!(self->functions & Func_Close)) return;
1877
1878 /*
1879 XXX: itd be cool to do timeouts and shit here for killing the client's
1880 process off
1881 like... if the window is around after 5 seconds, then the close button
1882 turns a nice red, and if this function is called again, the client is
1883 explicitly killed.
1884 */
1885
1886 ce.xclient.type = ClientMessage;
1887 ce.xclient.message_type = prop_atoms.wm_protocols;
1888 ce.xclient.display = ob_display;
1889 ce.xclient.window = self->window;
1890 ce.xclient.format = 32;
1891 ce.xclient.data.l[0] = prop_atoms.wm_delete_window;
1892 ce.xclient.data.l[1] = event_lasttime;
1893 ce.xclient.data.l[2] = 0l;
1894 ce.xclient.data.l[3] = 0l;
1895 ce.xclient.data.l[4] = 0l;
1896 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
1897 }
1898
1899 void client_kill(Client *self)
1900 {
1901 XKillClient(ob_display, self->window);
1902 }
1903
1904 void client_set_desktop(Client *self, guint target, gboolean donthide)
1905 {
1906 guint old;
1907
1908 if (target == self->desktop) return;
1909
1910 g_message("Setting desktop %u", target);
1911
1912 g_assert(target < screen_num_desktops || target == DESKTOP_ALL);
1913
1914 /* remove from the old desktop(s) */
1915 focus_order_remove(self);
1916
1917 old = self->desktop;
1918 self->desktop = target;
1919 PROP_SET32(self->window, net_wm_desktop, cardinal, target);
1920 /* the frame can display the current desktop state */
1921 frame_adjust_state(self->frame);
1922 /* 'move' the window to the new desktop */
1923 if (!donthide)
1924 client_showhide(self);
1925 /* raise if it was not already on the desktop */
1926 if (old != DESKTOP_ALL)
1927 stacking_raise(self);
1928 screen_update_struts();
1929
1930 /* add to the new desktop(s) */
1931 if (config_focus_new)
1932 focus_order_to_top(self);
1933 else
1934 focus_order_to_bottom(self);
1935
1936 dispatch_client(Event_Client_Desktop, self, target, old);
1937 }
1938
1939 static Client *search_modal_tree(Client *node, Client *skip)
1940 {
1941 GSList *it;
1942 Client *ret;
1943
1944 for (it = node->transients; it != NULL; it = it->next) {
1945 Client *c = it->data;
1946 if (c == skip) continue; /* circular? */
1947 if ((ret = search_modal_tree(c, skip))) return ret;
1948 if (c->modal) return c;
1949 }
1950 return NULL;
1951 }
1952
1953 Client *client_find_modal_child(Client *self)
1954 {
1955 return search_modal_tree(self, self);
1956 }
1957
1958 gboolean client_validate(Client *self)
1959 {
1960 XEvent e;
1961
1962 XSync(ob_display, FALSE); /* get all events on the server */
1963
1964 if (XCheckTypedWindowEvent(ob_display, self->window, DestroyNotify, &e) ||
1965 XCheckTypedWindowEvent(ob_display, self->window, UnmapNotify, &e)) {
1966 XPutBackEvent(ob_display, &e);
1967 return FALSE;
1968 }
1969
1970 return TRUE;
1971 }
1972
1973 void client_set_wm_state(Client *self, long state)
1974 {
1975 if (state == self->wmstate) return; /* no change */
1976
1977 switch (state) {
1978 case IconicState:
1979 client_iconify(self, TRUE, TRUE);
1980 break;
1981 case NormalState:
1982 client_iconify(self, FALSE, TRUE);
1983 break;
1984 }
1985 }
1986
1987 void client_set_state(Client *self, Atom action, long data1, long data2)
1988 {
1989 gboolean shaded = self->shaded;
1990 gboolean fullscreen = self->fullscreen;
1991 gboolean max_horz = self->max_horz;
1992 gboolean max_vert = self->max_vert;
1993 int i;
1994
1995 if (!(action == prop_atoms.net_wm_state_add ||
1996 action == prop_atoms.net_wm_state_remove ||
1997 action == prop_atoms.net_wm_state_toggle))
1998 /* an invalid action was passed to the client message, ignore it */
1999 return;
2000
2001 for (i = 0; i < 2; ++i) {
2002 Atom state = i == 0 ? data1 : data2;
2003
2004 if (!state) continue;
2005
2006 /* if toggling, then pick whether we're adding or removing */
2007 if (action == prop_atoms.net_wm_state_toggle) {
2008 if (state == prop_atoms.net_wm_state_modal)
2009 action = self->modal ? prop_atoms.net_wm_state_remove :
2010 prop_atoms.net_wm_state_add;
2011 else if (state == prop_atoms.net_wm_state_maximized_vert)
2012 action = self->max_vert ? prop_atoms.net_wm_state_remove :
2013 prop_atoms.net_wm_state_add;
2014 else if (state == prop_atoms.net_wm_state_maximized_horz)
2015 action = self->max_horz ? prop_atoms.net_wm_state_remove :
2016 prop_atoms.net_wm_state_add;
2017 else if (state == prop_atoms.net_wm_state_shaded)
2018 action = self->shaded ? prop_atoms.net_wm_state_remove :
2019 prop_atoms.net_wm_state_add;
2020 else if (state == prop_atoms.net_wm_state_skip_taskbar)
2021 action = self->skip_taskbar ?
2022 prop_atoms.net_wm_state_remove :
2023 prop_atoms.net_wm_state_add;
2024 else if (state == prop_atoms.net_wm_state_skip_pager)
2025 action = self->skip_pager ?
2026 prop_atoms.net_wm_state_remove :
2027 prop_atoms.net_wm_state_add;
2028 else if (state == prop_atoms.net_wm_state_fullscreen)
2029 action = self->fullscreen ?
2030 prop_atoms.net_wm_state_remove :
2031 prop_atoms.net_wm_state_add;
2032 else if (state == prop_atoms.net_wm_state_above)
2033 action = self->above ? prop_atoms.net_wm_state_remove :
2034 prop_atoms.net_wm_state_add;
2035 else if (state == prop_atoms.net_wm_state_below)
2036 action = self->below ? prop_atoms.net_wm_state_remove :
2037 prop_atoms.net_wm_state_add;
2038 }
2039
2040 if (action == prop_atoms.net_wm_state_add) {
2041 if (state == prop_atoms.net_wm_state_modal) {
2042 /* XXX raise here or something? */
2043 self->modal = TRUE;
2044 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2045 max_vert = TRUE;
2046 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2047 max_horz = TRUE;
2048 } else if (state == prop_atoms.net_wm_state_shaded) {
2049 shaded = TRUE;
2050 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2051 self->skip_taskbar = TRUE;
2052 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2053 self->skip_pager = TRUE;
2054 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2055 fullscreen = TRUE;
2056 } else if (state == prop_atoms.net_wm_state_above) {
2057 self->above = TRUE;
2058 } else if (state == prop_atoms.net_wm_state_below) {
2059 self->below = TRUE;
2060 }
2061
2062 } else { /* action == prop_atoms.net_wm_state_remove */
2063 if (state == prop_atoms.net_wm_state_modal) {
2064 self->modal = FALSE;
2065 } else if (state == prop_atoms.net_wm_state_maximized_vert) {
2066 max_vert = FALSE;
2067 } else if (state == prop_atoms.net_wm_state_maximized_horz) {
2068 max_horz = FALSE;
2069 } else if (state == prop_atoms.net_wm_state_shaded) {
2070 shaded = FALSE;
2071 } else if (state == prop_atoms.net_wm_state_skip_taskbar) {
2072 self->skip_taskbar = FALSE;
2073 } else if (state == prop_atoms.net_wm_state_skip_pager) {
2074 self->skip_pager = FALSE;
2075 } else if (state == prop_atoms.net_wm_state_fullscreen) {
2076 fullscreen = FALSE;
2077 } else if (state == prop_atoms.net_wm_state_above) {
2078 self->above = FALSE;
2079 } else if (state == prop_atoms.net_wm_state_below) {
2080 self->below = FALSE;
2081 }
2082 }
2083 }
2084 if (max_horz != self->max_horz || max_vert != self->max_vert) {
2085 if (max_horz != self->max_horz && max_vert != self->max_vert) {
2086 /* toggling both */
2087 if (max_horz == max_vert) { /* both going the same way */
2088 client_maximize(self, max_horz, 0, TRUE);
2089 } else {
2090 client_maximize(self, max_horz, 1, TRUE);
2091 client_maximize(self, max_vert, 2, TRUE);
2092 }
2093 } else {
2094 /* toggling one */
2095 if (max_horz != self->max_horz)
2096 client_maximize(self, max_horz, 1, TRUE);
2097 else
2098 client_maximize(self, max_vert, 2, TRUE);
2099 }
2100 }
2101 /* change fullscreen state before shading, as it will affect if the window
2102 can shade or not */
2103 if (fullscreen != self->fullscreen)
2104 client_fullscreen(self, fullscreen, TRUE);
2105 if (shaded != self->shaded)
2106 client_shade(self, shaded);
2107 client_calc_layer(self);
2108 client_change_state(self); /* change the hint to relect these changes */
2109 }
2110
2111 Client *client_focus_target(Client *self)
2112 {
2113 Client *child;
2114
2115 /* if we have a modal child, then focus it, not us */
2116 child = client_find_modal_child(self);
2117 if (child) return child;
2118 return self;
2119 }
2120
2121 gboolean client_focus(Client *self)
2122 {
2123 XEvent ev;
2124
2125 /* choose the correct target */
2126 self = client_focus_target(self);
2127
2128 if (self->desktop != DESKTOP_ALL && self->desktop != screen_desktop) {
2129 /* update the focus lists */
2130 focus_order_to_top(self);
2131 return FALSE;
2132 }
2133
2134 if (!((self->can_focus || self->focus_notify) &&
2135 (self->desktop == screen_desktop ||
2136 self->desktop == DESKTOP_ALL) &&
2137 !self->iconic))
2138 return FALSE;
2139
2140 /* do a check to see if the window has already been unmapped or destroyed
2141 do this intelligently while watching out for unmaps we've generated
2142 (ignore_unmaps > 0) */
2143 if (XCheckTypedWindowEvent(ob_display, self->window,
2144 DestroyNotify, &ev)) {
2145 XPutBackEvent(ob_display, &ev);
2146 return FALSE;
2147 }
2148 while (XCheckTypedWindowEvent(ob_display, self->window,
2149 UnmapNotify, &ev)) {
2150 if (self->ignore_unmaps) {
2151 self->ignore_unmaps--;
2152 } else {
2153 XPutBackEvent(ob_display, &ev);
2154 return FALSE;
2155 }
2156 }
2157
2158 if (self->can_focus)
2159 /* RevertToPointerRoot causes much more headache than RevertToNone, so
2160 I choose to use it always, hopefully to find errors quicker, if any
2161 are left. (I hate X. I hate focus events.) */
2162 XSetInputFocus(ob_display, self->window, RevertToPointerRoot,
2163 event_lasttime);
2164
2165 if (self->focus_notify) {
2166 XEvent ce;
2167 ce.xclient.type = ClientMessage;
2168 ce.xclient.message_type = prop_atoms.wm_protocols;
2169 ce.xclient.display = ob_display;
2170 ce.xclient.window = self->window;
2171 ce.xclient.format = 32;
2172 ce.xclient.data.l[0] = prop_atoms.wm_take_focus;
2173 ce.xclient.data.l[1] = event_lasttime;
2174 ce.xclient.data.l[2] = 0l;
2175 ce.xclient.data.l[3] = 0l;
2176 ce.xclient.data.l[4] = 0l;
2177 XSendEvent(ob_display, self->window, FALSE, NoEventMask, &ce);
2178 }
2179
2180 #ifdef DEBUG_FOCUS
2181 g_message("%sively focusing %lx at %d", (self->can_focus ? "act" : "pass"),
2182 self->window, (int)
2183 event_lasttime);
2184 #endif
2185
2186 /* Cause the FocusIn to come back to us. Important for desktop switches,
2187 since otherwise we'll have no FocusIn on the queue and send it off to
2188 the focus_backup. */
2189 XSync(ob_display, FALSE);
2190 return TRUE;
2191 }
2192
2193 void client_unfocus(Client *self)
2194 {
2195 g_assert(focus_client == self);
2196 #ifdef DEBUG_FOCUS
2197 g_message("client_unfocus for %lx", self->window);
2198 #endif
2199 focus_fallback(Fallback_Unfocusing);
2200 }
2201
2202 gboolean client_focused(Client *self)
2203 {
2204 return self == focus_client;
2205 }
2206
2207 Icon *client_icon(Client *self, int w, int h)
2208 {
2209 int i;
2210 /* si is the smallest image >= req */
2211 /* li is the largest image < req */
2212 unsigned long size, smallest = 0xffffffff, largest = 0, si = 0, li = 0;
2213
2214 if (!self->nicons) return NULL;
2215
2216 for (i = 0; i < self->nicons; ++i) {
2217 size = self->icons[i].width * self->icons[i].height;
2218 if (size < smallest && size >= (unsigned)(w * h)) {
2219 smallest = size;
2220 si = i;
2221 }
2222 if (size > largest && size <= (unsigned)(w * h)) {
2223 largest = size;
2224 li = i;
2225 }
2226 }
2227 if (largest == 0) /* didnt find one smaller than the requested size */
2228 return &self->icons[si];
2229 return &self->icons[li];
2230 }
This page took 0.142076 seconds and 4 git commands to generate.