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