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