Improve dockapp recognition
[wmaker-crm.git] / src / window.c
1 /* window.c - client window managing stuffs
2  *
3  *  Window Maker window manager
4  *
5  *  Copyright (c) 1997-2003 Alfredo K. Kojima
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20  *  USA.
21  */
22
23 #include "wconfig.h"
24
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #ifdef SHAPE
28 #include <X11/extensions/shape.h>
29 #endif
30 #ifdef KEEP_XKB_LOCK_STATUS
31 #include <X11/XKBlib.h>
32 #endif    /* KEEP_XKB_LOCK_STATUS */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <math.h>
38
39 /* For getting mouse wheel mappings from WINGs */
40 #include <WINGs/WINGsP.h>
41
42 #include "WindowMaker.h"
43 #include "GNUstep.h"
44 #include "wcore.h"
45 #include "framewin.h"
46 #include "texture.h"
47 #include "window.h"
48 #include "winspector.h"
49 #include "icon.h"
50 #include "properties.h"
51 #include "actions.h"
52 #include "client.h"
53 #include "funcs.h"
54 #include "keybind.h"
55 #include "stacking.h"
56 #include "defaults.h"
57 #include "workspace.h"
58 #include "xinerama.h"
59
60 #ifdef MWM_HINTS
61 # include "motif.h"
62 #endif
63 #include "wmspec.h"
64
65 #define MOD_MASK wPreferences.modifier_mask
66
67 /****** Global Variables ******/
68 extern WShortKey wKeyBindings[WKBD_LAST];
69
70 #ifdef SHAPE
71 extern Bool wShapeSupported;
72 #endif
73
74 /* contexts */
75 extern XContext wWinContext;
76
77 /* protocol atoms */
78 extern Atom _XA_WM_DELETE_WINDOW;
79 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
80 extern Atom _XA_WINDOWMAKER_STATE;
81 extern WPreferences wPreferences;
82 extern Time LastTimestamp;
83
84 /* superfluous... */
85 extern void DoWindowBirth(WWindow *wwin);
86
87 /***** Local Stuff *****/
88 static WWindowState *windowState = NULL;
89 static FocusMode getFocusMode(WWindow *wwin);
90 static int getSavedState(Window window, WSavedState **state);
91 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
92
93 /* frame window (during window grabs) */
94 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
95
96 /* close button */
97 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
98 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
99
100 /* iconify button */
101 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
102
103 #ifdef XKB_BUTTON_HINT
104 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
105 #endif
106
107 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
108 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
109 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
110
111 /****** Notification Observers ******/
112
113 static void appearanceObserver(void *self, WMNotification * notif)
114 {
115         WWindow *wwin = (WWindow *) self;
116         uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
117
118         if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
119                 return;
120
121         if (flags & WFontSettings) {
122                 wWindowConfigureBorders(wwin);
123                 if (wwin->flags.shaded) {
124                         wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
125
126                         wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
127                         wWindowSynthConfigureNotify(wwin);
128                 }
129         }
130         if (flags & WTextureSettings) {
131                 wwin->frame->flags.need_texture_remake = 1;
132         }
133         if (flags & (WTextureSettings | WColorSettings)) {
134                 if (wwin->frame->titlebar)
135                         XClearWindow(dpy, wwin->frame->titlebar->window);
136
137                 wFrameWindowPaint(wwin->frame);
138         }
139 }
140
141
142 WWindow *wWindowFor(Window window)
143 {
144         WObjDescriptor *desc;
145
146         if (window == None)
147                 return NULL;
148
149         if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
150                 return NULL;
151
152         if (desc->parent_type == WCLASS_WINDOW)
153                 return desc->parent;
154         else if (desc->parent_type == WCLASS_FRAME) {
155                 WFrameWindow *frame = (WFrameWindow *) desc->parent;
156                 if (frame->flags.is_client_window_frame)
157                         return frame->child;
158         }
159
160         return NULL;
161 }
162
163 WWindow *wWindowCreate(void)
164 {
165         WWindow *wwin;
166
167         wwin = wmalloc(sizeof(WWindow));
168         wretain(wwin);
169
170         memset(wwin, 0, sizeof(WWindow));
171
172         wwin->client_descriptor.handle_mousedown = frameMouseDown;
173         wwin->client_descriptor.parent = wwin;
174         wwin->client_descriptor.self = wwin;
175         wwin->client_descriptor.parent_type = WCLASS_WINDOW;
176
177         return wwin;
178 }
179
180 void wWindowDestroy(WWindow *wwin)
181 {
182         int i;
183
184         if (wwin->screen_ptr->cmap_window == wwin)
185                 wwin->screen_ptr->cmap_window = NULL;
186
187         WMRemoveNotificationObserver(wwin);
188
189         wwin->flags.destroyed = 1;
190
191         for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
192                 if (!wwin->screen_ptr->shortcutWindows[i])
193                         continue;
194
195                 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
196
197                 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
198                         WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
199                         wwin->screen_ptr->shortcutWindows[i] = NULL;
200                 }
201         }
202
203         if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
204                 wwin->fake_group->retainCount--;
205                 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
206                         XDestroyWindow(dpy, wwin->fake_group->leader);
207                         wwin->fake_group->leader = None;
208                         wwin->fake_group->origLeader = None;
209                         XFlush(dpy);
210                 }
211         }
212
213         if (wwin->normal_hints)
214                 XFree(wwin->normal_hints);
215
216         if (wwin->wm_hints)
217                 XFree(wwin->wm_hints);
218
219         if (wwin->wm_instance)
220                 XFree(wwin->wm_instance);
221
222         if (wwin->wm_class)
223                 XFree(wwin->wm_class);
224
225         if (wwin->wm_gnustep_attr)
226                 wfree(wwin->wm_gnustep_attr);
227
228         if (wwin->cmap_windows)
229                 XFree(wwin->cmap_windows);
230
231         XDeleteContext(dpy, wwin->client_win, wWinContext);
232
233         if (wwin->frame)
234                 wFrameWindowDestroy(wwin->frame);
235
236         if (wwin->icon) {
237                 RemoveFromStackList(wwin->icon->core);
238                 wIconDestroy(wwin->icon);
239                 if (wPreferences.auto_arrange_icons)
240                         wArrangeIcons(wwin->screen_ptr, True);
241         }
242         if (wwin->net_icon_image)
243                 RReleaseImage(wwin->net_icon_image);
244
245         wrelease(wwin);
246 }
247
248 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
249 {
250         if (gs_hints->flags & GSWindowStyleAttr) {
251                 if (gs_hints->window_style == WMBorderlessWindowMask) {
252                         wwin->client_flags.no_border = 1;
253                         wwin->client_flags.no_titlebar = 1;
254                         wwin->client_flags.no_closable = 1;
255                         wwin->client_flags.no_miniaturizable = 1;
256                         wwin->client_flags.no_resizable = 1;
257                         wwin->client_flags.no_close_button = 1;
258                         wwin->client_flags.no_miniaturize_button = 1;
259                         wwin->client_flags.no_resizebar = 1;
260                 } else {
261                         wwin->client_flags.no_close_button =
262                             ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
263
264                         wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
265
266                         wwin->client_flags.no_miniaturize_button =
267                             ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
268
269                         wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
270
271                         wwin->client_flags.no_resizebar =
272                             ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
273
274                         wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
275
276                         /* these attributes supposedly imply in the existence
277                          * of a titlebar */
278                         if (gs_hints->window_style & (WMResizableWindowMask |
279                                                       WMClosableWindowMask | WMMiniaturizableWindowMask)) {
280                                 wwin->client_flags.no_titlebar = 0;
281                         } else {
282                                 wwin->client_flags.no_titlebar =
283                                     ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
284                         }
285
286                 }
287         } else {
288                 /* setup the defaults */
289                 wwin->client_flags.no_border = 0;
290                 wwin->client_flags.no_titlebar = 0;
291                 wwin->client_flags.no_closable = 0;
292                 wwin->client_flags.no_miniaturizable = 0;
293                 wwin->client_flags.no_resizable = 0;
294                 wwin->client_flags.no_close_button = 0;
295                 wwin->client_flags.no_miniaturize_button = 0;
296                 wwin->client_flags.no_resizebar = 0;
297         }
298         if (gs_hints->extra_flags & GSNoApplicationIconFlag)
299                 wwin->client_flags.no_appicon = 1;
300 }
301
302 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
303 {
304         WScreen *scr = wwin->screen_ptr;
305
306         /* sets global default stuff */
307         wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class, &wwin->client_flags, NULL, True);
308         /*
309          * Decoration setting is done in this precedence (lower to higher)
310          * - use global default in the resource database
311          * - guess some settings
312          * - use GNUstep/external window attributes
313          * - set hints specified for the app in the resource DB
314          *
315          */
316         WSETUFLAG(wwin, broken_close, 0);
317
318         if (wwin->protocols.DELETE_WINDOW)
319                 WSETUFLAG(wwin, kill_close, 0);
320         else
321                 WSETUFLAG(wwin, kill_close, 1);
322
323         /* transients can't be iconified or maximized */
324         if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
325                 WSETUFLAG(wwin, no_miniaturizable, 1);
326                 WSETUFLAG(wwin, no_miniaturize_button, 1);
327         }
328
329         /* if the window can't be resized, remove the resizebar */
330         if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
331             && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
332             && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
333                 WSETUFLAG(wwin, no_resizable, 1);
334                 WSETUFLAG(wwin, no_resizebar, 1);
335         }
336
337         /* set GNUstep window attributes */
338         if (wwin->wm_gnustep_attr) {
339                 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
340
341                 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
342
343                         *level = wwin->wm_gnustep_attr->window_level;
344                         /*
345                          * INT_MIN is the only illegal window level.
346                          */
347                         if (*level == INT_MIN)
348                                 *level = INT_MIN + 1;
349                 } else {
350                         /* setup defaults */
351                         *level = WMNormalLevel;
352                 }
353         } else {
354                 int tmp_workspace = -1;
355                 int tmp_level = INT_MIN;        /* INT_MIN is never used by the window levels */
356                 Bool check;
357
358                 check = False;
359
360 #ifdef MWM_HINTS
361                 wMWMCheckClientHints(wwin);
362 #endif                          /* MWM_HINTS */
363
364                 if (!check)
365                         check = wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
366
367                 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
368                  * have INT_MIN that means that no window level was requested. -Dan
369                  */
370                 if (tmp_level == INT_MIN) {
371                         if (WFLAGP(wwin, floating))
372                                 *level = WMFloatingLevel;
373                         else if (WFLAGP(wwin, sunken))
374                                 *level = WMSunkenLevel;
375                         else
376                                 *level = WMNormalLevel;
377                 } else {
378                         *level = tmp_level;
379                 }
380
381                 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
382                         WWindow *transientOwner = wWindowFor(wwin->transient_for);
383                         if (transientOwner) {
384                                 int ownerLevel = transientOwner->frame->core->stacking->window_level;
385                                 if (ownerLevel > *level)
386                                         *level = ownerLevel;
387                         }
388                 }
389
390                 if (tmp_workspace >= 0)
391                         *workspace = tmp_workspace % scr->workspace_count;
392         }
393
394         /*
395          * Set attributes specified only for that window/class.
396          * This might do duplicate work with the 1st wDefaultFillAttributes().
397          */
398         wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
399                                &wwin->user_flags, &wwin->defined_user_flags, False);
400         /*
401          * Sanity checks for attributes that depend on other attributes
402          */
403         if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
404                 wwin->user_flags.emulate_appicon = 0;
405
406         if (wwin->main_window != None) {
407                 WApplication *wapp = wApplicationOf(wwin->main_window);
408                 if (wapp && !wapp->flags.emulated)
409                         wwin->user_flags.emulate_appicon = 0;
410         }
411
412         if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
413                 wwin->user_flags.emulate_appicon = 0;
414
415         if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
416             && wwin->user_flags.floating && wwin->defined_user_flags.floating)
417                 wwin->user_flags.sunken = 0;
418
419         WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
420
421         /* windows that have takefocus=False shouldn't take focus at all */
422         if (wwin->focus_mode == WFM_NO_INPUT)
423                 wwin->client_flags.no_focusable = 1;
424 }
425
426 Bool wWindowCanReceiveFocus(WWindow *wwin)
427 {
428         if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
429                 return False;
430         if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
431                 return False;
432         if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
433                 return False;
434
435         return True;
436 }
437
438 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
439 {
440         int w1, h1, w2, h2;
441
442         w1 = wwin->frame->core->width;
443         h1 = wwin->frame->core->height;
444         w2 = obscured->frame->core->width;
445         h2 = obscured->frame->core->height;
446
447         if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
448             && wwin->frame->workspace != obscured->frame->workspace)
449                 return False;
450
451         if (wwin->frame_x + w1 < obscured->frame_x
452             || wwin->frame_y + h1 < obscured->frame_y
453             || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
454                 return False;
455
456         return True;
457 }
458
459 static void fixLeaderProperties(WWindow *wwin)
460 {
461         XClassHint *classHint;
462         XWMHints *hints, *clientHints;
463         Window leaders[2], window;
464         char **argv, *command;
465         int argc, i, pid;
466         Bool haveCommand;
467
468         classHint = XAllocClassHint();
469         clientHints = XGetWMHints(dpy, wwin->client_win);
470         pid = wNETWMGetPidForWindow(wwin->client_win);
471         if (pid > 0)
472                 haveCommand = GetCommandForPid(pid, &argv, &argc);
473         else
474                 haveCommand = False;
475
476         leaders[0] = wwin->client_leader;
477         leaders[1] = wwin->group_id;
478
479         if (haveCommand) {
480                 command = GetCommandForWindow(wwin->client_win);
481                 if (command) {
482                         /* command already set. nothing to do. */
483                         wfree(command);
484                 } else {
485                         XSetCommand(dpy, wwin->client_win, argv, argc);
486                 }
487         }
488
489         for (i = 0; i < 2; i++) {
490                 window = leaders[i];
491                 if (window) {
492                         if (XGetClassHint(dpy, window, classHint) == 0) {
493                                 classHint->res_name = wwin->wm_instance;
494                                 classHint->res_class = wwin->wm_class;
495                                 XSetClassHint(dpy, window, classHint);
496                         }
497                         hints = XGetWMHints(dpy, window);
498                         if (hints) {
499                                 XFree(hints);
500                         } else if (clientHints) {
501                                 /* set window group leader to self */
502                                 clientHints->window_group = window;
503                                 clientHints->flags |= WindowGroupHint;
504                                 XSetWMHints(dpy, window, clientHints);
505                         }
506
507                         if (haveCommand) {
508                                 command = GetCommandForWindow(window);
509                                 if (command) {
510                                         /* command already set. nothing to do. */
511                                         wfree(command);
512                                 } else {
513                                         XSetCommand(dpy, window, argv, argc);
514                                 }
515                         }
516                 }
517         }
518
519         XFree(classHint);
520         if (clientHints)
521                 XFree(clientHints);
522         if (haveCommand)
523                 wfree(argv);
524 }
525
526 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
527 {
528         XClassHint *classHint;
529         XWMHints *hints;
530         Window leader;
531         int argc;
532         char **argv;
533
534         leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
535         /* set class hint */
536         classHint = XAllocClassHint();
537         classHint->res_name = instance;
538         classHint->res_class = class;
539         XSetClassHint(dpy, leader, classHint);
540         XFree(classHint);
541
542         /* inherit these from the original leader if available */
543         hints = XGetWMHints(dpy, win);
544         if (!hints) {
545                 hints = XAllocWMHints();
546                 hints->flags = 0;
547         }
548         /* set window group leader to self */
549         hints->window_group = leader;
550         hints->flags |= WindowGroupHint;
551         XSetWMHints(dpy, leader, hints);
552         XFree(hints);
553
554         if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
555                 XSetCommand(dpy, leader, argv, argc);
556                 XFreeStringList(argv);
557         }
558
559         return leader;
560 }
561
562 static int matchIdentifier(const void *item, const void *cdata)
563 {
564         return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
565 }
566
567 /*
568  *----------------------------------------------------------------
569  * wManageWindow--
570  *      reparents the window and allocates a descriptor for it.
571  * Window manager hints and other hints are fetched to configure
572  * the window decoration attributes and others. User preferences
573  * for the window are used if available, to configure window
574  * decorations and some behaviour.
575  *      If in startup, windows that are override redirect,
576  * unmapped and never were managed and are Withdrawn are not
577  * managed.
578  *
579  * Returns:
580  *      the new window descriptor
581  *
582  * Side effects:
583  *      The window is reparented and appropriate notification
584  * is done to the client. Input mask for the window is setup.
585  *      The window descriptor is also associated with various window
586  * contexts and inserted in the head of the window list.
587  * Event handler contexts are associated for some objects
588  * (buttons, titlebar and resizebar)
589  *
590  *----------------------------------------------------------------
591  */
592 WWindow *wManageWindow(WScreen *scr, Window window)
593 {
594         WWindow *wwin;
595         int x, y;
596         unsigned width, height;
597         XWindowAttributes wattribs;
598         XSetWindowAttributes attribs;
599         WWindowState *win_state;
600         WWindow *transientOwner = NULL;
601         int window_level;
602         int wm_state;
603         int foo;
604         int workspace = -1;
605         char *title;
606         Bool withdraw = False;
607         Bool raise = False;
608
609         /* mutex. */
610         XGrabServer(dpy);
611         XSync(dpy, False);
612         /* make sure the window is still there */
613         if (!XGetWindowAttributes(dpy, window, &wattribs)) {
614                 XUngrabServer(dpy);
615                 return NULL;
616         }
617
618         /* if it's an override-redirect, ignore it */
619         if (wattribs.override_redirect) {
620                 XUngrabServer(dpy);
621                 return NULL;
622         }
623
624         wm_state = PropGetWindowState(window);
625
626         /* if it's startup and the window is unmapped, don't manage it */
627         if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
628                 XUngrabServer(dpy);
629                 return NULL;
630         }
631
632         wwin = wWindowCreate();
633
634         title = wNETWMGetWindowName(window);
635         if (title)
636                 wwin->flags.net_has_title = 1;
637         if (!title && !wFetchName(dpy, window, &title))
638                 title = NULL;
639
640         XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
641
642 #ifdef SHAPE
643         if (wShapeSupported) {
644                 int junk;
645                 unsigned int ujunk;
646                 int b_shaped;
647
648                 XShapeSelectInput(dpy, window, ShapeNotifyMask);
649                 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
650                                    &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
651                 wwin->flags.shaped = b_shaped;
652         }
653 #endif
654
655         /*
656          * Get hints and other information in properties
657          */
658         PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
659
660         /* setup descriptor */
661         wwin->client_win = window;
662         wwin->screen_ptr = scr;
663         wwin->old_border_width = wattribs.border_width;
664         wwin->event_mask = CLIENT_EVENTS;
665         attribs.event_mask = CLIENT_EVENTS;
666         attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
667         attribs.save_under = False;
668
669         XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
670         XSetWindowBorderWidth(dpy, window, 0);
671
672         /* get hints from GNUstep app */
673         if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0) {
674                 wwin->flags.is_gnustep = 1;
675         }
676         if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
677                 wwin->wm_gnustep_attr = NULL;
678         }
679
680         if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
681                 wwin->flags.is_dockapp = 1;
682                 withdraw = True;
683         }
684
685         wwin->client_leader = PropGetClientLeader(window);
686         if (wwin->client_leader != None)
687                 wwin->main_window = wwin->client_leader;
688
689         wwin->wm_hints = XGetWMHints(dpy, window);
690
691         if (wwin->wm_hints) {
692                 if (wwin->wm_hints->flags & StateHint) {
693
694                         if (wwin->wm_hints->initial_state == IconicState) {
695
696                                 wwin->flags.miniaturized = 1;
697
698                         } else if (wwin->wm_hints->initial_state == WithdrawnState) {
699
700                                 wwin->flags.is_dockapp = 1;
701                                 withdraw = True;
702                         }
703                 }
704
705                 if (wwin->wm_hints->flags & WindowGroupHint) {
706                         wwin->group_id = wwin->wm_hints->window_group;
707                         /* window_group has priority over CLIENT_LEADER */
708                         wwin->main_window = wwin->group_id;
709                 } else {
710                         wwin->group_id = None;
711                 }
712
713                 if (wwin->wm_hints->flags & UrgencyHint)
714                         wwin->flags.urgent = 1;
715         } else {
716                 wwin->group_id = None;
717         }
718
719         PropGetProtocols(window, &wwin->protocols);
720
721         if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
722                 wwin->transient_for = None;
723         } else {
724                 if (wwin->transient_for == None || wwin->transient_for == window) {
725                         wwin->transient_for = scr->root_win;
726                 } else {
727                         transientOwner = wWindowFor(wwin->transient_for);
728                         if (transientOwner && transientOwner->main_window != None)
729                                 wwin->main_window = transientOwner->main_window;
730                 }
731         }
732
733         /* guess the focus mode */
734         wwin->focus_mode = getFocusMode(wwin);
735
736         /* get geometry stuff */
737         wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
738
739         /* get colormap windows */
740         GetColormapWindows(wwin);
741
742         /*
743          * Setup the decoration/window attributes and
744          * geometry
745          */
746         wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
747
748         /* Make broken apps behave as a nice app. */
749         if (WFLAGP(wwin, emulate_appicon))
750                 wwin->main_window = wwin->client_win;
751
752         fixLeaderProperties(wwin);
753
754         wwin->orig_main_window = wwin->main_window;
755
756         if (wwin->flags.is_gnustep)
757                 WSETUFLAG(wwin, shared_appicon, 0);
758
759         if (wwin->main_window) {
760                 extern Atom _XA_WINDOWMAKER_MENU;
761                 XTextProperty text_prop;
762
763                 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU)) {
764                         WSETUFLAG(wwin, shared_appicon, 0);
765                 }
766         }
767
768         if (wwin->flags.is_dockapp)
769                 WSETUFLAG(wwin, shared_appicon, 0);
770
771         if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
772                 char *buffer, *instance, *class;
773                 WFakeGroupLeader *fPtr;
774                 int index;
775
776 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
777
778                 /* // only enter here if PropGetWMClass() succeds */
779                 PropGetWMClass(wwin->main_window, &class, &instance);
780                 buffer = StrConcatDot(instance, class);
781
782                 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
783                 if (index != WANotFound) {
784                         fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
785                         if (fPtr->retainCount == 0) {
786                                 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
787                                                                            instance, class);
788                         }
789                         fPtr->retainCount++;
790                         if (fPtr->origLeader == None) {
791                                 if (ADEQUATE(wwin->main_window)) {
792                                         fPtr->retainCount++;
793                                         fPtr->origLeader = wwin->main_window;
794                                 }
795                         }
796                         wwin->fake_group = fPtr;
797                         /*wwin->group_id = fPtr->leader; */
798                         wwin->main_window = fPtr->leader;
799                         wfree(buffer);
800                 } else {
801                         fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
802
803                         fPtr->identifier = buffer;
804                         fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
805                         fPtr->origLeader = None;
806                         fPtr->retainCount = 1;
807
808                         WMAddToArray(scr->fakeGroupLeaders, fPtr);
809
810                         if (ADEQUATE(wwin->main_window)) {
811                                 fPtr->retainCount++;
812                                 fPtr->origLeader = wwin->main_window;
813                         }
814                         wwin->fake_group = fPtr;
815                         /*wwin->group_id = fPtr->leader; */
816                         wwin->main_window = fPtr->leader;
817                 }
818                 if (instance)
819                         XFree(instance);
820                 if (class)
821                         XFree(class);
822 #undef ADEQUATE
823         }
824
825         /*
826          * Setup the initial state of the window
827          */
828         if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
829                 wwin->flags.miniaturized = 1;
830
831         if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
832                 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
833
834         wNETWMCheckInitialClientState(wwin);
835
836         /* apply previous state if it exists and we're in startup */
837         if (scr->flags.startup && wm_state >= 0) {
838
839                 if (wm_state == IconicState) {
840
841                         wwin->flags.miniaturized = 1;
842
843                 } else if (wm_state == WithdrawnState) {
844
845                         withdraw = True;
846                 }
847         }
848
849         /* if there is a saved state (from file), restore it */
850         win_state = NULL;
851         if (wwin->main_window != None)
852                 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
853         else
854                 win_state = (WWindowState *) wWindowGetSavedState(window);
855
856         if (win_state && !withdraw) {
857                 if (win_state->state->hidden > 0)
858                         wwin->flags.hidden = win_state->state->hidden;
859
860                 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
861                         wwin->flags.shaded = win_state->state->shaded;
862
863                 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable)) {
864                         wwin->flags.miniaturized = win_state->state->miniaturized;
865                 }
866
867                 if (!IS_OMNIPRESENT(wwin)) {
868                         int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
869                                                           wwin->wm_class);
870                         if (w < 0 || w >= scr->workspace_count) {
871                                 workspace = win_state->state->workspace;
872                                 if (workspace >= scr->workspace_count)
873                                         workspace = scr->current_workspace;
874                         } else {
875                                 workspace = w;
876                         }
877                 } else {
878                         workspace = scr->current_workspace;
879                 }
880         }
881
882         /* if we're restarting, restore saved state (from hints).
883          * This will overwrite previous */
884         {
885                 WSavedState *wstate;
886
887                 if (getSavedState(window, &wstate)) {
888                         wwin->flags.shaded = wstate->shaded;
889                         wwin->flags.hidden = wstate->hidden;
890                         wwin->flags.miniaturized = wstate->miniaturized;
891                         wwin->flags.maximized = wstate->maximized;
892                         if (wwin->flags.maximized) {
893                                 wwin->old_geometry.x = wstate->x;
894                                 wwin->old_geometry.y = wstate->y;
895                                 wwin->old_geometry.width = wstate->w;
896                                 wwin->old_geometry.height = wstate->h;
897                         }
898
899                         workspace = wstate->workspace;
900                 } else {
901                         wstate = NULL;
902                 }
903
904                 /* restore window shortcut */
905                 if (wstate != NULL || win_state != NULL) {
906                         unsigned mask = 0;
907
908                         if (win_state != NULL)
909                                 mask = win_state->state->window_shortcuts;
910
911                         if (wstate != NULL && mask == 0)
912                                 mask = wstate->window_shortcuts;
913
914                         if (mask > 0) {
915                                 int i;
916
917                                 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
918                                         if (mask & (1 << i)) {
919                                                 if (!scr->shortcutWindows[i])
920                                                         scr->shortcutWindows[i] = WMCreateArray(4);
921
922                                                 WMAddToArray(scr->shortcutWindows[i], wwin);
923                                         }
924                                 }
925                         }
926                 }
927                 if (wstate != NULL)
928                         wfree(wstate);
929         }
930
931         /* don't let transients start miniaturized if their owners are not */
932         if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
933                 wwin->flags.miniaturized = 0;
934                 if (wwin->wm_hints)
935                         wwin->wm_hints->initial_state = NormalState;
936         }
937
938         /* set workspace on which the window starts */
939         if (workspace >= 0) {
940                 if (workspace > scr->workspace_count - 1) {
941                         workspace = workspace % scr->workspace_count;
942                 }
943         } else {
944                 int w;
945
946                 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
947
948                 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
949
950                         workspace = w;
951
952                 } else {
953                         if (wPreferences.open_transients_with_parent && transientOwner) {
954
955                                 workspace = transientOwner->frame->workspace;
956
957                         } else {
958
959                                 workspace = scr->current_workspace;
960                         }
961                 }
962         }
963
964         /* setup window geometry */
965         if (win_state && win_state->state->w > 0) {
966                 width = win_state->state->w;
967                 height = win_state->state->h;
968         }
969         wWindowConstrainSize(wwin, &width, &height);
970
971         /* do not ask for window placement if the window is
972          * transient, during startup, if the initial workspace is another one
973          * or if the window wants to start iconic.
974          * If geometry was saved, restore it. */
975         {
976                 Bool dontBring = False;
977
978                 if (win_state && win_state->state->w > 0) {
979                         x = win_state->state->x;
980                         y = win_state->state->y;
981                 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
982                            && !scr->flags.startup
983                            && workspace == scr->current_workspace
984                            && !wwin->flags.miniaturized
985                            && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
986
987                         if (transientOwner && transientOwner->flags.mapped) {
988                                 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
989                                 WMRect rect;
990                                 int head;
991
992                                 x = transientOwner->frame_x +
993                                     abs((transientOwner->frame->core->width - width) / 2) + offs;
994                                 y = transientOwner->frame_y +
995                                     abs((transientOwner->frame->core->height - height) / 3) + offs;
996
997                                 /*
998                                  * limit transient windows to be inside their parent's head
999                                  */
1000                                 rect.pos.x = transientOwner->frame_x;
1001                                 rect.pos.y = transientOwner->frame_y;
1002                                 rect.size.width = transientOwner->frame->core->width;
1003                                 rect.size.height = transientOwner->frame->core->height;
1004
1005                                 head = wGetHeadForRect(scr, rect);
1006                                 rect = wGetRectForHead(scr, head);
1007
1008                                 if (x < rect.pos.x)
1009                                         x = rect.pos.x;
1010                                 else if (x + width > rect.pos.x + rect.size.width)
1011                                         x = rect.pos.x + rect.size.width - width;
1012
1013                                 if (y < rect.pos.y)
1014                                         y = rect.pos.y;
1015                                 else if (y + height > rect.pos.y + rect.size.height)
1016                                         y = rect.pos.y + rect.size.height - height;
1017
1018                         } else {
1019                                 PlaceWindow(wwin, &x, &y, width, height);
1020                         }
1021                         if (wPreferences.window_placement == WPM_MANUAL) {
1022                                 dontBring = True;
1023                         }
1024                 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1025                         int head, flags;
1026                         WMRect rect;
1027                         int reposition = 0;
1028
1029                         /*
1030                          * Make spash screens come out in the center of a head
1031                          * trouble is that most splashies never get here
1032                          * they are managed trough atoms but god knows where.
1033                          * Dan, do you know ? -peter
1034                          *
1035                          * Most of them are not managed, they have set
1036                          * OverrideRedirect, which means we can't do anything about
1037                          * them. -alfredo
1038                          */
1039                         {
1040                                 /*
1041                                  * xinerama checks for: across head and dead space
1042                                  */
1043                                 rect.pos.x = x;
1044                                 rect.pos.y = y;
1045                                 rect.size.width = width;
1046                                 rect.size.height = height;
1047
1048                                 head = wGetRectPlacementInfo(scr, rect, &flags);
1049
1050                                 if (flags & XFLAG_DEAD)
1051                                         reposition = 1;
1052
1053                                 if (flags & XFLAG_MULTIPLE)
1054                                         reposition = 2;
1055                         }
1056
1057                         switch (reposition) {
1058                         case 1:
1059                                 head = wGetHeadForPointerLocation(scr);
1060                                 rect = wGetRectForHead(scr, head);
1061
1062                                 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1063                                 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1064                                 break;
1065
1066                         case 2:
1067                                 rect = wGetRectForHead(scr, head);
1068
1069                                 if (x < rect.pos.x)
1070                                         x = rect.pos.x;
1071                                 else if (x + width > rect.pos.x + rect.size.width)
1072                                         x = rect.pos.x + rect.size.width - width;
1073
1074                                 if (y < rect.pos.y)
1075                                         y = rect.pos.y;
1076                                 else if (y + height > rect.pos.y + rect.size.height)
1077                                         y = rect.pos.y + rect.size.height - height;
1078
1079                                 break;
1080
1081                         default:
1082                                 break;
1083                         }
1084                 }
1085
1086                 if (WFLAGP(wwin, dont_move_off) && dontBring)
1087                         wScreenBringInside(scr, &x, &y, width, height);
1088         }
1089
1090         wNETWMPositionSplash(wwin, &x, &y, width, height);
1091
1092         if (wwin->flags.urgent) {
1093                 if (!IS_OMNIPRESENT(wwin))
1094                         wwin->flags.omnipresent ^= 1;
1095         }
1096
1097         /*
1098          * Create frame, borders and do reparenting
1099          */
1100         foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1101 #ifdef XKB_BUTTON_HINT
1102         if (wPreferences.modelock)
1103                 foo |= WFF_LANGUAGE_BUTTON;
1104 #endif
1105         if (HAS_TITLEBAR(wwin))
1106                 foo |= WFF_TITLEBAR;
1107         if (HAS_RESIZEBAR(wwin))
1108                 foo |= WFF_RESIZEBAR;
1109         if (HAS_BORDER(wwin))
1110                 foo |= WFF_BORDER;
1111
1112         wwin->frame = wFrameWindowCreate(scr, window_level,
1113                                          x, y, width, height,
1114                                          &wPreferences.window_title_clearance, foo,
1115                                          scr->window_title_texture,
1116                                          scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1117
1118         wwin->frame->flags.is_client_window_frame = 1;
1119         wwin->frame->flags.justification = wPreferences.title_justification;
1120
1121         /* setup button images */
1122         wWindowUpdateButtonImages(wwin);
1123
1124         /* hide unused buttons */
1125         foo = 0;
1126         if (WFLAGP(wwin, no_close_button))
1127                 foo |= WFF_RIGHT_BUTTON;
1128         if (WFLAGP(wwin, no_miniaturize_button))
1129                 foo |= WFF_LEFT_BUTTON;
1130 #ifdef XKB_BUTTON_HINT
1131         if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1132                 foo |= WFF_LANGUAGE_BUTTON;
1133 #endif
1134         if (foo != 0)
1135                 wFrameWindowHideButton(wwin->frame, foo);
1136
1137         wwin->frame->child = wwin;
1138         wwin->frame->workspace = workspace;
1139         wwin->frame->on_click_left = windowIconifyClick;
1140
1141 #ifdef XKB_BUTTON_HINT
1142         if (wPreferences.modelock)
1143                 wwin->frame->on_click_language = windowLanguageClick;
1144 #endif
1145
1146         wwin->frame->on_click_right = windowCloseClick;
1147         wwin->frame->on_dblclick_right = windowCloseDblClick;
1148         wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1149         wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1150         wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1151
1152         XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1153         XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1154         XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1155
1156         {
1157                 int gx, gy;
1158
1159                 wClientGetGravityOffsets(wwin, &gx, &gy);
1160
1161                 /* if gravity is to the south, account for the border sizes */
1162                 if (gy > 0)
1163                         y -= wwin->frame->top_width + wwin->frame->bottom_width;
1164         }
1165
1166         /*
1167          * wWindowConfigure() will init the client window's size
1168          * (wwin->client.{width,height}) and all other geometry
1169          * related variables (frame_x,frame_y)
1170          */
1171         wWindowConfigure(wwin, x, y, width, height);
1172
1173         /* to make sure the window receives it's new position after reparenting */
1174         wWindowSynthConfigureNotify(wwin);
1175
1176         /*
1177          * Setup descriptors and save window to internal
1178          * lists
1179          */
1180         if (wwin->main_window != None) {
1181                 WApplication *app;
1182                 WWindow *leader;
1183
1184                 /* Leader windows do not necessary set themselves as leaders.
1185                  * If this is the case, point the leader of this window to
1186                  * itself */
1187                 leader = wWindowFor(wwin->main_window);
1188                 if (leader && leader->main_window == None) {
1189                         leader->main_window = leader->client_win;
1190                 }
1191                 app = wApplicationCreate(wwin);
1192                 if (app) {
1193                         app->last_workspace = workspace;
1194
1195                         /*
1196                          * Do application specific stuff, like setting application
1197                          * wide attributes.
1198                          */
1199
1200                         if (wwin->flags.hidden) {
1201                                 /* if the window was set to hidden because it was hidden
1202                                  * in a previous incarnation and that state was restored */
1203                                 app->flags.hidden = 1;
1204                         } else if (app->flags.hidden) {
1205                                 if (WFLAGP(app->main_window_desc, start_hidden)) {
1206                                         wwin->flags.hidden = 1;
1207                                 } else {
1208                                         wUnhideApplication(app, False, False);
1209                                         raise = True;
1210                                 }
1211                         }
1212                         wAppBounce(app);
1213                 }
1214         }
1215
1216         /* setup the frame descriptor */
1217         wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1218         wwin->frame->core->descriptor.parent = wwin;
1219         wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1220
1221         /* don't let windows go away if we die */
1222         XAddToSaveSet(dpy, window);
1223
1224         XLowerWindow(dpy, window);
1225
1226         /* if window is in this workspace and should be mapped, then  map it */
1227         if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1228             && !wwin->flags.hidden && !withdraw) {
1229
1230                 /* The following "if" is to avoid crashing of clients that expect
1231                  * WM_STATE set before they get mapped. Else WM_STATE is set later,
1232                  * after the return from this function.
1233                  */
1234                 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1235                         wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1236                 } else {
1237                         wClientSetState(wwin, NormalState, None);
1238                 }
1239
1240 #if 0
1241                 /* if not auto focus, then map the window under the currently
1242                  * focused window */
1243 #define _WIDTH(w) (w)->frame->core->width
1244 #define _HEIGHT(w) (w)->frame->core->height
1245                 if (!wPreferences.auto_focus && scr->focused_window
1246                     && !scr->flags.startup && !transientOwner && ((wWindowObscuresWindow(wwin, scr->focused_window)
1247                                                                    && (_WIDTH(wwin) >
1248                                                                        (_WIDTH(scr->focused_window) * 5) / 3
1249                                                                        || _HEIGHT(wwin) >
1250                                                                        (_HEIGHT(scr->focused_window) * 5) / 3)
1251                                                                    && WINDOW_LEVEL(scr->focused_window) ==
1252                                                                    WINDOW_LEVEL(wwin))
1253                                                                   || wwin->flags.maximized)) {
1254                         MoveInStackListUnder(scr->focused_window->frame->core, wwin->frame->core);
1255                 }
1256 #undef _WIDTH
1257 #undef _HEIGHT
1258
1259 #endif
1260
1261                 if (wPreferences.superfluous && !wPreferences.no_animations
1262                     && !scr->flags.startup && (wwin->transient_for == None || wwin->transient_for == scr->root_win)
1263                     /*
1264                      * The brain damaged idiotic non-click to focus modes will
1265                      * have trouble with this because:
1266                      *
1267                      * 1. window is created and mapped by the client
1268                      * 2. window is mapped by wmaker in small size
1269                      * 3. window is animated to grow to normal size
1270                      * 4. this function returns to normal event loop
1271                      * 5. eventually, the EnterNotify event that would trigger
1272                      * the window focusing (if the mouse is over that window)
1273                      * will be processed by wmaker.
1274                      * But since this event will be rather delayed
1275                      * (step 3 has a large delay) the time when the event ocurred
1276                      * and when it is processed, the client that owns that window
1277                      * will reject the XSetInputFocus() for it.
1278                      */
1279                     && (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus)) {
1280                         DoWindowBirth(wwin);
1281                 }
1282
1283                 wWindowMap(wwin);
1284         }
1285
1286         /* setup stacking descriptor */
1287         if (transientOwner) {
1288                 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1289         } else {
1290                 wwin->frame->core->stacking->child_of = NULL;
1291         }
1292
1293         if (!scr->focused_window) {
1294                 /* first window on the list */
1295                 wwin->next = NULL;
1296                 wwin->prev = NULL;
1297                 scr->focused_window = wwin;
1298         } else {
1299                 WWindow *tmp;
1300
1301                 /* add window at beginning of focus window list */
1302                 tmp = scr->focused_window;
1303                 while (tmp->prev)
1304                         tmp = tmp->prev;
1305                 tmp->prev = wwin;
1306                 wwin->next = tmp;
1307                 wwin->prev = NULL;
1308         }
1309
1310         /* raise is set to true if we un-hid the app when this window was born.
1311          * we raise, else old windows of this app will be above this new one. */
1312         if (raise) {
1313                 wRaiseFrame(wwin->frame->core);
1314         }
1315
1316         /* Update name must come after WApplication stuff is done */
1317         wWindowUpdateName(wwin, title);
1318         if (title)
1319                 XFree(title);
1320
1321         XUngrabServer(dpy);
1322
1323         /*
1324          * Final preparations before window is ready to go
1325          */
1326         wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1327
1328         if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1329                 if (((transientOwner && transientOwner->flags.focused)
1330                      || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1331
1332                         /* only auto_focus if on same screen as mouse
1333                          * (and same head for xinerama mode)
1334                          * TODO: make it an option */
1335
1336                         /*TODO add checking the head of the window, is it available? */
1337                         short same_screen = 0, same_head = 1;
1338
1339                         int foo;
1340                         unsigned int bar;
1341                         Window dummy;
1342
1343                         if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1344                                           &foo, &foo, &foo, &foo, &bar) != False) {
1345                                 same_screen = 1;
1346                         }
1347
1348                         if (same_screen == 1 && same_head == 1) {
1349                                 wSetFocusTo(scr, wwin);
1350                         }
1351                 }
1352         }
1353         wWindowResetMouseGrabs(wwin);
1354
1355         if (!WFLAGP(wwin, no_bind_keys))
1356                 wWindowSetKeyGrabs(wwin);
1357
1358         WMPostNotificationName(WMNManaged, wwin, NULL);
1359         wColormapInstallForWindow(scr, scr->cmap_window);
1360
1361         /* Setup Notification Observers */
1362         WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1363
1364         /*  Cleanup temporary stuff */
1365         if (win_state)
1366                 wWindowDeleteSavedState(win_state);
1367
1368         /* If the window must be withdrawed, then do it now.
1369          * Must do some optimization, 'though */
1370         if (withdraw) {
1371                 wwin->flags.mapped = 0;
1372                 wClientSetState(wwin, WithdrawnState, None);
1373                 wUnmanageWindow(wwin, True, False);
1374                 wwin = NULL;
1375         }
1376
1377         return wwin;
1378 }
1379
1380 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1381                                char *title, int x, int y, int width, int height)
1382 {
1383         WWindow *wwin;
1384         int foo;
1385
1386         wwin = wWindowCreate();
1387
1388         WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1389
1390         wwin->flags.internal_window = 1;
1391
1392         WSETUFLAG(wwin, omnipresent, 1);
1393         WSETUFLAG(wwin, no_shadeable, 1);
1394         WSETUFLAG(wwin, no_resizable, 1);
1395         WSETUFLAG(wwin, no_miniaturizable, 1);
1396
1397         wwin->focus_mode = WFM_PASSIVE;
1398         wwin->client_win = window;
1399         wwin->screen_ptr = scr;
1400         wwin->transient_for = owner;
1401         wwin->client.x = x;
1402         wwin->client.y = y;
1403         wwin->client.width = width;
1404         wwin->client.height = height;
1405         wwin->frame_x = wwin->client.x;
1406         wwin->frame_y = wwin->client.y;
1407
1408         foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1409         foo |= WFF_TITLEBAR;
1410 #ifdef XKB_BUTTON_HINT
1411         foo |= WFF_LANGUAGE_BUTTON;
1412 #endif
1413
1414         wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1415                                          wwin->frame_x, wwin->frame_y,
1416                                          width, height,
1417                                          &wPreferences.window_title_clearance, foo,
1418                                          scr->window_title_texture,
1419                                          scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1420
1421         XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1422
1423         wwin->frame->flags.is_client_window_frame = 1;
1424         wwin->frame->flags.justification = wPreferences.title_justification;
1425
1426         wFrameWindowChangeTitle(wwin->frame, title);
1427
1428         /* setup button images */
1429         wWindowUpdateButtonImages(wwin);
1430
1431         /* hide buttons */
1432         wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1433
1434         wwin->frame->child = wwin;
1435         wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1436
1437 #ifdef XKB_BUTTON_HINT
1438         if (wPreferences.modelock)
1439                 wwin->frame->on_click_language = windowLanguageClick;
1440 #endif
1441
1442         wwin->frame->on_click_right = windowCloseClick;
1443         wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1444         wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1445         wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1446         wwin->client.y += wwin->frame->top_width;
1447
1448         XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1449         wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1450
1451         /* setup the frame descriptor */
1452         wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1453         wwin->frame->core->descriptor.parent = wwin;
1454         wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1455
1456         XLowerWindow(dpy, window);
1457         XMapSubwindows(dpy, wwin->frame->core->window);
1458
1459         /* setup stacking descriptor */
1460         if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1461                 WWindow *tmp;
1462                 tmp = wWindowFor(wwin->transient_for);
1463                 if (tmp)
1464                         wwin->frame->core->stacking->child_of = tmp->frame->core;
1465         } else {
1466                 wwin->frame->core->stacking->child_of = NULL;
1467         }
1468
1469         if (!scr->focused_window) {
1470                 /* first window on the list */
1471                 wwin->next = NULL;
1472                 wwin->prev = NULL;
1473                 scr->focused_window = wwin;
1474         } else {
1475                 WWindow *tmp;
1476
1477                 /* add window at beginning of focus window list */
1478                 tmp = scr->focused_window;
1479                 while (tmp->prev)
1480                         tmp = tmp->prev;
1481                 tmp->prev = wwin;
1482                 wwin->next = tmp;
1483                 wwin->prev = NULL;
1484         }
1485
1486         if (wwin->flags.is_gnustep == 0)
1487                 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1488
1489         /*    if (wPreferences.auto_focus) */
1490         wSetFocusTo(scr, wwin);
1491         wWindowResetMouseGrabs(wwin);
1492         wWindowSetKeyGrabs(wwin);
1493
1494         return wwin;
1495 }
1496
1497 /*
1498  *----------------------------------------------------------------------
1499  * wUnmanageWindow--
1500  *      Removes the frame window from a window and destroys all data
1501  * related to it. The window will be reparented back to the root window
1502  * if restore is True.
1503  *
1504  * Side effects:
1505  *      Everything related to the window is destroyed and the window
1506  * is removed from the window lists. Focus is set to the previous on the
1507  * window list.
1508  *----------------------------------------------------------------------
1509  */
1510 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1511 {
1512         WCoreWindow *frame = wwin->frame->core;
1513         WWindow *owner = NULL;
1514         WWindow *newFocusedWindow = NULL;
1515         int wasFocused;
1516         WScreen *scr = wwin->screen_ptr;
1517
1518         /* First close attribute editor window if open */
1519         if (wwin->flags.inspector_open) {
1520                 wCloseInspectorForWindow(wwin);
1521         }
1522
1523         /* Close window menu if it's open for this window */
1524         if (wwin->flags.menu_open_for_me) {
1525                 CloseWindowMenu(scr);
1526         }
1527
1528         if (!destroyed) {
1529                 if (!wwin->flags.internal_window)
1530                         XRemoveFromSaveSet(dpy, wwin->client_win);
1531
1532                 XSelectInput(dpy, wwin->client_win, NoEventMask);
1533
1534                 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1535                 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1536         }
1537
1538         XUnmapWindow(dpy, frame->window);
1539
1540         XUnmapWindow(dpy, wwin->client_win);
1541
1542         /* deselect window */
1543         wSelectWindow(wwin, False);
1544
1545         /* remove all pending events on window */
1546         /* I think this only matters for autoraise */
1547         if (wPreferences.raise_delay)
1548                 WMDeleteTimerWithClientData(wwin->frame->core);
1549
1550         XFlush(dpy);
1551
1552         /* reparent the window back to the root */
1553         if (restore)
1554                 wClientRestore(wwin);
1555
1556         if (wwin->transient_for != scr->root_win) {
1557                 owner = wWindowFor(wwin->transient_for);
1558                 if (owner) {
1559                         if (!owner->flags.semi_focused) {
1560                                 owner = NULL;
1561                         } else {
1562                                 owner->flags.semi_focused = 0;
1563                         }
1564                 }
1565         }
1566
1567         wasFocused = wwin->flags.focused;
1568
1569         /* remove from window focus list */
1570         if (!wwin->prev && !wwin->next) {
1571                 /* was the only window */
1572                 scr->focused_window = NULL;
1573                 newFocusedWindow = NULL;
1574         } else {
1575                 WWindow *tmp;
1576
1577                 if (wwin->prev)
1578                         wwin->prev->next = wwin->next;
1579                 if (wwin->next)
1580                         wwin->next->prev = wwin->prev;
1581                 else {
1582                         scr->focused_window = wwin->prev;
1583                         scr->focused_window->next = NULL;
1584                 }
1585
1586                 if (wPreferences.focus_mode == WKF_CLICK) {
1587
1588                         /* if in click to focus mode and the window
1589                          * was a transient, focus the owner window
1590                          */
1591                         tmp = NULL;
1592                         if (wPreferences.focus_mode == WKF_CLICK) {
1593                                 tmp = wWindowFor(wwin->transient_for);
1594                                 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1595                                         tmp = NULL;
1596                                 }
1597                         }
1598                         /* otherwise, focus the next one in the focus list */
1599                         if (!tmp) {
1600                                 tmp = scr->focused_window;
1601                                 while (tmp) {   /* look for one in the window list first */
1602                                         if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1603                                             && (tmp->flags.mapped || tmp->flags.shaded))
1604                                                 break;
1605                                         tmp = tmp->prev;
1606                                 }
1607                                 if (!tmp) {     /* if unsuccessful, choose any focusable window */
1608                                         tmp = scr->focused_window;
1609                                         while (tmp) {
1610                                                 if (!WFLAGP(tmp, no_focusable)
1611                                                     && (tmp->flags.mapped || tmp->flags.shaded))
1612                                                         break;
1613                                                 tmp = tmp->prev;
1614                                         }
1615                                 }
1616                         }
1617
1618                         newFocusedWindow = tmp;
1619
1620                 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1621                         unsigned int mask;
1622                         int foo;
1623                         Window bar, win;
1624
1625                         /*  This is to let the root window get the keyboard input
1626                          * if Sloppy focus mode and no other window get focus.
1627                          * This way keybindings will not freeze.
1628                          */
1629                         tmp = NULL;
1630                         if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1631                                 tmp = wWindowFor(win);
1632                         if (tmp == wwin)
1633                                 tmp = NULL;
1634                         newFocusedWindow = tmp;
1635                 } else {
1636                         newFocusedWindow = NULL;
1637                 }
1638         }
1639
1640         if (!wwin->flags.internal_window)
1641                 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1642         if (wasFocused) {
1643                 if (newFocusedWindow != owner && owner) {
1644                         if (wwin->flags.is_gnustep == 0)
1645                                 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1646                 }
1647                 wSetFocusTo(scr, newFocusedWindow);
1648         }
1649         wWindowDestroy(wwin);
1650         XFlush(dpy);
1651 }
1652
1653 void wWindowMap(WWindow *wwin)
1654 {
1655         XMapWindow(dpy, wwin->frame->core->window);
1656         if (!wwin->flags.shaded) {
1657                 /* window will be remapped when getting MapNotify */
1658                 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1659                 XMapWindow(dpy, wwin->client_win);
1660                 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1661
1662                 wwin->flags.mapped = 1;
1663         }
1664 }
1665
1666 void wWindowUnmap(WWindow *wwin)
1667 {
1668         wwin->flags.mapped = 0;
1669
1670         /* prevent window withdrawal when getting UnmapNotify */
1671         XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1672         XUnmapWindow(dpy, wwin->client_win);
1673         XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1674
1675         XUnmapWindow(dpy, wwin->frame->core->window);
1676 }
1677
1678 void wWindowFocus(WWindow *wwin, WWindow *owin)
1679 {
1680         WWindow *nowner;
1681         WWindow *oowner;
1682
1683 #ifdef KEEP_XKB_LOCK_STATUS
1684         if (wPreferences.modelock) {
1685                 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1686         }
1687 #endif /* KEEP_XKB_LOCK_STATUS */
1688
1689         wwin->flags.semi_focused = 0;
1690
1691         if (wwin->flags.is_gnustep == 0)
1692                 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1693
1694         wwin->flags.focused = 1;
1695
1696         wWindowResetMouseGrabs(wwin);
1697
1698         WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1699
1700         if (owin == wwin || !owin)
1701                 return;
1702
1703         nowner = wWindowFor(wwin->transient_for);
1704
1705         /* new window is a transient for the old window */
1706         if (nowner == owin) {
1707                 owin->flags.semi_focused = 1;
1708                 wWindowUnfocus(nowner);
1709                 return;
1710         }
1711
1712         oowner = wWindowFor(owin->transient_for);
1713
1714         /* new window is owner of old window */
1715         if (wwin == oowner) {
1716                 wWindowUnfocus(owin);
1717                 return;
1718         }
1719
1720         if (!nowner) {
1721                 wWindowUnfocus(owin);
1722                 return;
1723         }
1724
1725         /* new window has same owner of old window */
1726         if (oowner == nowner) {
1727                 /* prevent unfocusing of owner */
1728                 oowner->flags.semi_focused = 0;
1729                 wWindowUnfocus(owin);
1730                 oowner->flags.semi_focused = 1;
1731
1732                 return;
1733         }
1734
1735         /* nowner != NULL && oowner != nowner */
1736         nowner->flags.semi_focused = 1;
1737         wWindowUnfocus(nowner);
1738         wWindowUnfocus(owin);
1739 }
1740
1741 void wWindowUnfocus(WWindow *wwin)
1742 {
1743         CloseWindowMenu(wwin->screen_ptr);
1744
1745         if (wwin->flags.is_gnustep == 0)
1746                 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1747
1748         if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1749                 WWindow *owner;
1750                 owner = wWindowFor(wwin->transient_for);
1751                 if (owner && owner->flags.semi_focused) {
1752                         owner->flags.semi_focused = 0;
1753                         if (owner->flags.mapped || owner->flags.shaded) {
1754                                 wWindowUnfocus(owner);
1755                                 wFrameWindowPaint(owner->frame);
1756                         }
1757                 }
1758         }
1759         wwin->flags.focused = 0;
1760         wWindowResetMouseGrabs(wwin);
1761         WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1762 }
1763
1764 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1765 {
1766         char *title;
1767
1768         if (!wwin->frame)
1769                 return;
1770
1771         wwin->flags.wm_name_changed = 1;
1772
1773         if (!newTitle) {
1774                 /* the hint was removed */
1775                 title = DEF_WINDOW_TITLE;
1776         } else {
1777                 title = newTitle;
1778         }
1779
1780         if (wFrameWindowChangeTitle(wwin->frame, title)) {
1781                 WMPostNotificationName(WMNChangedName, wwin, NULL);
1782         }
1783 }
1784
1785 /*
1786  *----------------------------------------------------------------------
1787  *
1788  * wWindowConstrainSize--
1789  *      Constrains size for the client window, taking the maximal size,
1790  * window resize increments and other size hints into account.
1791  *
1792  * Returns:
1793  *      The closest size to what was given that the client window can
1794  * have.
1795  *
1796  *----------------------------------------------------------------------
1797  */
1798 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1799 {
1800         int width = (int)*nwidth;
1801         int height = (int)*nheight;
1802         int winc = 1;
1803         int hinc = 1;
1804         int minW = 1, minH = 1;
1805         int maxW = wwin->screen_ptr->scr_width * 2;
1806         int maxH = wwin->screen_ptr->scr_height * 2;
1807         int minAX = -1, minAY = -1;
1808         int maxAX = -1, maxAY = -1;
1809         int baseW = 0;
1810         int baseH = 0;
1811
1812         if (wwin->normal_hints) {
1813                 winc = wwin->normal_hints->width_inc;
1814                 hinc = wwin->normal_hints->height_inc;
1815                 minW = wwin->normal_hints->min_width;
1816                 minH = wwin->normal_hints->min_height;
1817                 maxW = wwin->normal_hints->max_width;
1818                 maxH = wwin->normal_hints->max_height;
1819                 if (wwin->normal_hints->flags & PAspect) {
1820                         minAX = wwin->normal_hints->min_aspect.x;
1821                         minAY = wwin->normal_hints->min_aspect.y;
1822                         maxAX = wwin->normal_hints->max_aspect.x;
1823                         maxAY = wwin->normal_hints->max_aspect.y;
1824                 }
1825
1826                 baseW = wwin->normal_hints->base_width;
1827                 baseH = wwin->normal_hints->base_height;
1828         }
1829
1830         if (width < minW)
1831                 width = minW;
1832         if (height < minH)
1833                 height = minH;
1834
1835         if (width > maxW)
1836                 width = maxW;
1837         if (height > maxH)
1838                 height = maxH;
1839
1840         /* aspect ratio code borrowed from olwm */
1841         if (minAX > 0) {
1842                 /* adjust max aspect ratio */
1843                 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1844                         if (maxAX > maxAY) {
1845                                 height = (width * maxAY) / maxAX;
1846                                 if (height > maxH) {
1847                                         height = maxH;
1848                                         width = (height * maxAX) / maxAY;
1849                                 }
1850                         } else {
1851                                 width = (height * maxAX) / maxAY;
1852                                 if (width > maxW) {
1853                                         width = maxW;
1854                                         height = (width * maxAY) / maxAX;
1855                                 }
1856                         }
1857                 }
1858
1859                 /* adjust min aspect ratio */
1860                 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1861                         if (minAX > minAY) {
1862                                 height = (width * minAY) / minAX;
1863                                 if (height < minH) {
1864                                         height = minH;
1865                                         width = (height * minAX) / minAY;
1866                                 }
1867                         } else {
1868                                 width = (height * minAX) / minAY;
1869                                 if (width < minW) {
1870                                         width = minW;
1871                                         height = (width * minAY) / minAX;
1872                                 }
1873                         }
1874                 }
1875         }
1876
1877         if (baseW != 0)
1878                 width = (((width - baseW) / winc) * winc) + baseW;
1879         else
1880                 width = (((width - minW) / winc) * winc) + minW;
1881
1882         if (baseH != 0)
1883                 height = (((height - baseH) / hinc) * hinc) + baseH;
1884         else
1885                 height = (((height - minH) / hinc) * hinc) + minH;
1886
1887         /* broken stupid apps may cause preposterous values for these.. */
1888         if (width > 0)
1889                 *nwidth = width;
1890         if (height > 0)
1891                 *nheight = height;
1892 }
1893
1894 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1895                      unsigned int *width, unsigned int *height)
1896 {
1897         int baseW = 0, baseH = 0;
1898         int winc = 1, hinc = 1;
1899
1900         if (wwin->normal_hints) {
1901                 baseW = wwin->normal_hints->base_width;
1902                 baseH = wwin->normal_hints->base_height;
1903
1904                 winc = wwin->normal_hints->width_inc;
1905                 hinc = wwin->normal_hints->height_inc;
1906         }
1907
1908         if (*width > maxW)
1909                 *width = maxW - (maxW - baseW) % winc;
1910
1911         if (*height > maxH)
1912                 *height = maxH - (maxH - baseH) % hinc;
1913 }
1914
1915 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1916 {
1917         WScreen *scr = wwin->screen_ptr;
1918         WApplication *wapp;
1919         int unmap = 0;
1920
1921         if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1922                 return;
1923
1924         if (workspace != scr->current_workspace) {
1925                 /* Sent to other workspace. Unmap window */
1926                 if ((wwin->flags.mapped
1927                      || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1928                     && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1929
1930                         wapp = wApplicationOf(wwin->main_window);
1931                         if (wapp) {
1932                                 wapp->last_workspace = workspace;
1933                         }
1934                         if (wwin->flags.miniaturized) {
1935                                 if (wwin->icon) {
1936                                         XUnmapWindow(dpy, wwin->icon->core->window);
1937                                         wwin->icon->mapped = 0;
1938                                 }
1939                         } else {
1940                                 unmap = 1;
1941                                 wSetFocusTo(scr, NULL);
1942                         }
1943                 }
1944         } else {
1945                 /* brought to current workspace. Map window */
1946                 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1947                         if (wwin->icon) {
1948                                 XMapWindow(dpy, wwin->icon->core->window);
1949                                 wwin->icon->mapped = 1;
1950                         }
1951                 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1952                         wWindowMap(wwin);
1953                 }
1954         }
1955         if (!IS_OMNIPRESENT(wwin)) {
1956                 int oldWorkspace = wwin->frame->workspace;
1957
1958                 wwin->frame->workspace = workspace;
1959
1960                 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1961         }
1962
1963         if (unmap)
1964                 wWindowUnmap(wwin);
1965 }
1966
1967 void wWindowSynthConfigureNotify(WWindow *wwin)
1968 {
1969         XEvent sevent;
1970
1971         sevent.type = ConfigureNotify;
1972         sevent.xconfigure.display = dpy;
1973         sevent.xconfigure.event = wwin->client_win;
1974         sevent.xconfigure.window = wwin->client_win;
1975
1976         sevent.xconfigure.x = wwin->client.x;
1977         sevent.xconfigure.y = wwin->client.y;
1978         sevent.xconfigure.width = wwin->client.width;
1979         sevent.xconfigure.height = wwin->client.height;
1980
1981         sevent.xconfigure.border_width = wwin->old_border_width;
1982         if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
1983                 sevent.xconfigure.above = wwin->frame->titlebar->window;
1984         else
1985                 sevent.xconfigure.above = None;
1986
1987         sevent.xconfigure.override_redirect = False;
1988         XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1989         XFlush(dpy);
1990 }
1991
1992 /*
1993  *----------------------------------------------------------------------
1994  * wWindowConfigure()
1995  *
1996  * req_x, req_y: new requested positions for the frame
1997  * req_width, req_height: new requested sizes for the client
1998  *
1999  * Configures the frame, decorations and client window to the specified
2000  * geometry, whose validity is not checked --  wWindowConstrainSize()
2001  * must be used for that.
2002  *      The size parameters are for the client window, but the position is
2003  * for the frame.
2004  *      The client window receives a ConfigureNotify event, according
2005  * to what ICCCM says.
2006  *
2007  * Returns:
2008  *      None
2009  *
2010  * Side effects:
2011  *      Window size and position are changed and client window receives
2012  * a ConfigureNotify event.
2013  *----------------------------------------------------------------------
2014  */
2015 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2016 {
2017         int synth_notify = False;
2018         int resize;
2019
2020         resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2021         /*
2022          * if the window is being moved but not resized then
2023          * send a synthetic ConfigureNotify
2024          */
2025         if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize) {
2026                 synth_notify = True;
2027         }
2028
2029         if (WFLAGP(wwin, dont_move_off))
2030                 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2031         if (resize) {
2032                 if (req_width < MIN_WINDOW_SIZE)
2033                         req_width = MIN_WINDOW_SIZE;
2034                 if (req_height < MIN_WINDOW_SIZE)
2035                         req_height = MIN_WINDOW_SIZE;
2036
2037                 /* If growing, resize inner part before frame,
2038                  * if shrinking, resize frame before.
2039                  * This will prevent the frame (that can have a different color)
2040                  * to be exposed, causing flicker */
2041                 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2042                         XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2043
2044                 if (wwin->flags.shaded) {
2045                         wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2046                         wwin->old_geometry.height = req_height;
2047                 } else {
2048                         int h;
2049
2050                         h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2051
2052                         wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2053                 }
2054
2055                 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2056                         XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2057
2058                 wwin->client.x = req_x;
2059                 wwin->client.y = req_y + wwin->frame->top_width;
2060                 wwin->client.width = req_width;
2061                 wwin->client.height = req_height;
2062         } else {
2063                 wwin->client.x = req_x;
2064                 wwin->client.y = req_y + wwin->frame->top_width;
2065
2066                 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2067         }
2068         wwin->frame_x = req_x;
2069         wwin->frame_y = req_y;
2070         if (HAS_BORDER(wwin)) {
2071                 wwin->client.x += FRAME_BORDER_WIDTH;
2072                 wwin->client.y += FRAME_BORDER_WIDTH;
2073         }
2074 #ifdef SHAPE
2075         if (wShapeSupported && wwin->flags.shaped && resize) {
2076                 wWindowSetShape(wwin);
2077         }
2078 #endif
2079
2080         if (synth_notify)
2081                 wWindowSynthConfigureNotify(wwin);
2082         XFlush(dpy);
2083 }
2084
2085 /* req_x, req_y:  new position of the frame */
2086 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2087 {
2088 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2089         int synth_notify = False;
2090
2091         /* Send a synthetic ConfigureNotify event for every window movement. */
2092         if ((req_x != wwin->frame_x || req_y != wwin->frame_y)) {
2093                 synth_notify = True;
2094         }
2095 #else
2096         /* A single synthetic ConfigureNotify event is sent at the end of
2097          * a completed (opaque) movement in moveres.c */
2098 #endif
2099
2100         if (WFLAGP(wwin, dont_move_off))
2101                 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2102                                    wwin->frame->core->width, wwin->frame->core->height);
2103
2104         wwin->client.x = req_x;
2105         wwin->client.y = req_y + wwin->frame->top_width;
2106         if (HAS_BORDER(wwin)) {
2107                 wwin->client.x += FRAME_BORDER_WIDTH;
2108                 wwin->client.y += FRAME_BORDER_WIDTH;
2109         }
2110
2111         XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2112
2113         wwin->frame_x = req_x;
2114         wwin->frame_y = req_y;
2115
2116 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2117         if (synth_notify)
2118                 wWindowSynthConfigureNotify(wwin);
2119 #endif
2120 }
2121
2122 void wWindowUpdateButtonImages(WWindow *wwin)
2123 {
2124         WScreen *scr = wwin->screen_ptr;
2125         Pixmap pixmap, mask;
2126         WFrameWindow *fwin = wwin->frame;
2127
2128         if (!HAS_TITLEBAR(wwin))
2129                 return;
2130
2131         /* miniaturize button */
2132         if (!WFLAGP(wwin, no_miniaturize_button)) {
2133                 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2134                         pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2135
2136                         if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr) {
2137                                 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2138                         } else {
2139                                 mask = None;
2140                         }
2141
2142                         if (fwin->lbutton_image
2143                             && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2144                                 wPixmapDestroy(fwin->lbutton_image);
2145                                 fwin->lbutton_image = NULL;
2146                         }
2147
2148                         if (!fwin->lbutton_image) {
2149                                 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2150                                 fwin->lbutton_image->client_owned = 1;
2151                                 fwin->lbutton_image->client_owned_mask = 1;
2152                         }
2153                 } else {
2154                         if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2155                                 wPixmapDestroy(fwin->lbutton_image);
2156                         }
2157                         fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2158                 }
2159         }
2160 #ifdef XKB_BUTTON_HINT
2161         if (!WFLAGP(wwin, no_language_button)) {
2162                 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared) {
2163                         wPixmapDestroy(fwin->languagebutton_image);
2164                 }
2165                 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2166         }
2167 #endif
2168
2169         /* close button */
2170
2171         /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2172 #define MGFLAGP(wwin, FLAG)     (wwin)->client_flags.FLAG
2173
2174         if (!WFLAGP(wwin, no_close_button)) {
2175                 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2176                         pixmap = wwin->wm_gnustep_attr->close_pixmap;
2177
2178                         if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2179                                 mask = wwin->wm_gnustep_attr->close_mask;
2180                         else
2181                                 mask = None;
2182
2183                         if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2184                                                     || fwin->rbutton_image->mask != mask)) {
2185                                 wPixmapDestroy(fwin->rbutton_image);
2186                                 fwin->rbutton_image = NULL;
2187                         }
2188
2189                         if (!fwin->rbutton_image) {
2190                                 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2191                                 fwin->rbutton_image->client_owned = 1;
2192                                 fwin->rbutton_image->client_owned_mask = 1;
2193                         }
2194
2195                 } else if (WFLAGP(wwin, kill_close)) {
2196
2197                         if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2198                                 wPixmapDestroy(fwin->rbutton_image);
2199
2200                         fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2201
2202                 } else if (MGFLAGP(wwin, broken_close)) {
2203
2204                         if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2205                                 wPixmapDestroy(fwin->rbutton_image);
2206
2207                         fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2208
2209                 } else {
2210
2211                         if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2212                                 wPixmapDestroy(fwin->rbutton_image);
2213
2214                         fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2215                 }
2216         }
2217
2218         /* force buttons to be redrawn */
2219         fwin->flags.need_texture_change = 1;
2220         wFrameWindowPaint(fwin);
2221 }
2222
2223 /*
2224  *---------------------------------------------------------------------------
2225  * wWindowConfigureBorders--
2226  *      Update window border configuration according to attribute flags.
2227  *
2228  *---------------------------------------------------------------------------
2229  */
2230 void wWindowConfigureBorders(WWindow *wwin)
2231 {
2232         if (wwin->frame) {
2233                 int flags;
2234                 int newy, oldh;
2235
2236                 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2237
2238 #ifdef XKB_BUTTON_HINT
2239         if (wPreferences.modelock)
2240                 flags |= WFF_LANGUAGE_BUTTON;
2241 #endif
2242
2243                 if (HAS_TITLEBAR(wwin))
2244                         flags |= WFF_TITLEBAR;
2245                 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2246                         flags |= WFF_RESIZEBAR;
2247                 if (HAS_BORDER(wwin))
2248                         flags |= WFF_BORDER;
2249                 if (wwin->flags.shaded)
2250                         flags |= WFF_IS_SHADED;
2251
2252                 oldh = wwin->frame->top_width;
2253                 wFrameWindowUpdateBorders(wwin->frame, flags);
2254                 if (oldh != wwin->frame->top_width) {
2255                         newy = wwin->frame_y + oldh - wwin->frame->top_width;
2256
2257                         XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2258                         wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2259                 }
2260
2261                 flags = 0;
2262                 if (!WFLAGP(wwin, no_miniaturize_button)
2263                     && wwin->frame->flags.hide_left_button)
2264                         flags |= WFF_LEFT_BUTTON;
2265
2266 #ifdef XKB_BUTTON_HINT
2267                 if (!WFLAGP(wwin, no_language_button)
2268                     && wwin->frame->flags.hide_language_button) {
2269                         flags |= WFF_LANGUAGE_BUTTON;
2270                 }
2271 #endif
2272
2273                 if (!WFLAGP(wwin, no_close_button)
2274                     && wwin->frame->flags.hide_right_button)
2275                         flags |= WFF_RIGHT_BUTTON;
2276
2277                 if (flags != 0) {
2278                         wWindowUpdateButtonImages(wwin);
2279                         wFrameWindowShowButton(wwin->frame, flags);
2280                 }
2281
2282                 flags = 0;
2283                 if (WFLAGP(wwin, no_miniaturize_button)
2284                     && !wwin->frame->flags.hide_left_button)
2285                         flags |= WFF_LEFT_BUTTON;
2286
2287 #ifdef XKB_BUTTON_HINT
2288                 if (WFLAGP(wwin, no_language_button)
2289                     && !wwin->frame->flags.hide_language_button)
2290                         flags |= WFF_LANGUAGE_BUTTON;
2291 #endif
2292
2293                 if (WFLAGP(wwin, no_close_button)
2294                     && !wwin->frame->flags.hide_right_button)
2295                         flags |= WFF_RIGHT_BUTTON;
2296
2297                 if (flags != 0)
2298                         wFrameWindowHideButton(wwin->frame, flags);
2299
2300 #ifdef SHAPE
2301                 if (wShapeSupported && wwin->flags.shaped) {
2302                         wWindowSetShape(wwin);
2303                 }
2304 #endif
2305         }
2306 }
2307
2308 void wWindowSaveState(WWindow * wwin)
2309 {
2310         long data[10];
2311         int i;
2312
2313         memset(data, 0, sizeof(long) * 10);
2314         data[0] = wwin->frame->workspace;
2315         data[1] = wwin->flags.miniaturized;
2316         data[2] = wwin->flags.shaded;
2317         data[3] = wwin->flags.hidden;
2318         data[4] = wwin->flags.maximized;
2319         if (wwin->flags.maximized == 0) {
2320                 data[5] = wwin->frame_x;
2321                 data[6] = wwin->frame_y;
2322                 data[7] = wwin->frame->core->width;
2323                 data[8] = wwin->frame->core->height;
2324         } else {
2325                 data[5] = wwin->old_geometry.x;
2326                 data[6] = wwin->old_geometry.y;
2327                 data[7] = wwin->old_geometry.width;
2328                 data[8] = wwin->old_geometry.height;
2329         }
2330
2331         for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2332                 if (wwin->screen_ptr->shortcutWindows[i] &&
2333                     WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2334                         data[9] |= 1 << i;
2335         }
2336         XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2337                         _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2338 }
2339
2340 static int getSavedState(Window window, WSavedState ** state)
2341 {
2342         Atom type_ret;
2343         int fmt_ret;
2344         unsigned long nitems_ret;
2345         unsigned long bytes_after_ret;
2346         long *data;
2347
2348         if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2349                                True, _XA_WINDOWMAKER_STATE,
2350                                &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2351                                (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2352                 return 0;
2353
2354         *state = wmalloc(sizeof(WSavedState));
2355
2356         (*state)->workspace = data[0];
2357         (*state)->miniaturized = data[1];
2358         (*state)->shaded = data[2];
2359         (*state)->hidden = data[3];
2360         (*state)->maximized = data[4];
2361         (*state)->x = data[5];
2362         (*state)->y = data[6];
2363         (*state)->w = data[7];
2364         (*state)->h = data[8];
2365         (*state)->window_shortcuts = data[9];
2366
2367         XFree(data);
2368
2369         if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2370                 return 1;
2371         else
2372                 return 0;
2373 }
2374
2375 #ifdef SHAPE
2376 void wWindowClearShape(WWindow * wwin)
2377 {
2378         XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2379                           0, wwin->frame->top_width, None, ShapeSet);
2380         XFlush(dpy);
2381 }
2382
2383 void wWindowSetShape(WWindow * wwin)
2384 {
2385         XRectangle rect[2];
2386         int count;
2387 #ifdef OPTIMIZE_SHAPE
2388         XRectangle *rects;
2389         XRectangle *urec;
2390         int ordering;
2391
2392         /* only shape is the client's */
2393         if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin)) {
2394                 goto alt_code;
2395         }
2396
2397         /* Get array of rectangles describing the shape mask */
2398         rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2399         if (!rects) {
2400                 goto alt_code;
2401         }
2402
2403         urec = malloc(sizeof(XRectangle) * (count + 2));
2404         if (!urec) {
2405                 XFree(rects);
2406                 goto alt_code;
2407         }
2408
2409         /* insert our decoration rectangles in the rect list */
2410         memcpy(urec, rects, sizeof(XRectangle) * count);
2411         XFree(rects);
2412
2413         if (HAS_TITLEBAR(wwin)) {
2414                 urec[count].x = -1;
2415                 urec[count].y = -1 - wwin->frame->top_width;
2416                 urec[count].width = wwin->frame->core->width + 2;
2417                 urec[count].height = wwin->frame->top_width + 1;
2418                 count++;
2419         }
2420         if (HAS_RESIZEBAR(wwin)) {
2421                 urec[count].x = -1;
2422                 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2423                 urec[count].width = wwin->frame->core->width + 2;
2424                 urec[count].height = wwin->frame->bottom_width + 1;
2425                 count++;
2426         }
2427
2428         /* shape our frame window */
2429         XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2430                                 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2431         XFlush(dpy);
2432         wfree(urec);
2433         return;
2434
2435  alt_code:
2436 #endif                          /* OPTIMIZE_SHAPE */
2437         count = 0;
2438         if (HAS_TITLEBAR(wwin)) {
2439                 rect[count].x = -1;
2440                 rect[count].y = -1;
2441                 rect[count].width = wwin->frame->core->width + 2;
2442                 rect[count].height = wwin->frame->top_width + 1;
2443                 count++;
2444         }
2445         if (HAS_RESIZEBAR(wwin)) {
2446                 rect[count].x = -1;
2447                 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2448                 rect[count].width = wwin->frame->core->width + 2;
2449                 rect[count].height = wwin->frame->bottom_width + 1;
2450                 count++;
2451         }
2452         if (count > 0) {
2453                 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2454                                         0, 0, rect, count, ShapeSet, Unsorted);
2455         }
2456         XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2457                            0, wwin->frame->top_width, wwin->client_win,
2458                            ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2459         XFlush(dpy);
2460 }
2461 #endif                          /* SHAPE */
2462
2463 /* ====================================================================== */
2464
2465 static FocusMode getFocusMode(WWindow * wwin)
2466 {
2467         FocusMode mode;
2468
2469         if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2470                 if (wwin->wm_hints->input == True) {
2471                         if (wwin->protocols.TAKE_FOCUS)
2472                                 mode = WFM_LOCALLY_ACTIVE;
2473                         else
2474                                 mode = WFM_PASSIVE;
2475                 } else {
2476                         if (wwin->protocols.TAKE_FOCUS)
2477                                 mode = WFM_GLOBALLY_ACTIVE;
2478                         else
2479                                 mode = WFM_NO_INPUT;
2480                 }
2481         } else {
2482                 mode = WFM_PASSIVE;
2483         }
2484         return mode;
2485 }
2486
2487 void wWindowSetKeyGrabs(WWindow * wwin)
2488 {
2489         int i;
2490         WShortKey *key;
2491
2492         for (i = 0; i < WKBD_LAST; i++) {
2493                 key = &wKeyBindings[i];
2494
2495                 if (key->keycode == 0)
2496                         continue;
2497                 if (key->modifier != AnyModifier) {
2498                         XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2499                                  wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2500 #ifdef NUMLOCK_HACK
2501                         /* Also grab all modifier combinations possible that include,
2502                          * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2503                          * work even if the NumLock/ScrollLock key is on.
2504                          */
2505                         wHackedGrabKey(key->keycode, key->modifier,
2506                                        wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2507 #endif
2508                 }
2509                 XGrabKey(dpy, key->keycode, key->modifier,
2510                          wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2511         }
2512
2513         wRootMenuBindShortcuts(wwin->frame->core->window);
2514 }
2515
2516 void wWindowResetMouseGrabs(WWindow * wwin)
2517 {
2518         /* Mouse grabs can't be done on the client window because of
2519          * ICCCM and because clients that try to do the same will crash.
2520          *
2521          * But there is a problem wich makes tbar buttons of unfocused
2522          * windows not usable as the click goes to the frame window instead
2523          * of the button itself. Must figure a way to fix that.
2524          */
2525
2526         XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2527
2528         if (!WFLAGP(wwin, no_bind_mouse)) {
2529                 /* grabs for Meta+drag */
2530                 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2531                                   True, ButtonPressMask | ButtonReleaseMask,
2532                                   GrabModeSync, GrabModeAsync, None, None);
2533
2534                 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2535                  * but we only grab it for Button4 and Button 5 since a lot of apps
2536                  * use CTRL+Button1-3 for app related functionality
2537                  */
2538                 if (wPreferences.resize_increment > 0) {
2539                         wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2540                                                           True, ButtonPressMask | ButtonReleaseMask,
2541                                                           GrabModeSync, GrabModeAsync, None, None);
2542                         wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2543                                                           True, ButtonPressMask | ButtonReleaseMask,
2544                                                           GrabModeSync, GrabModeAsync, None, None);
2545
2546                         wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2547                                                           True, ButtonPressMask | ButtonReleaseMask,
2548                                                           GrabModeSync, GrabModeAsync, None, None);
2549                         wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2550                                                           True, ButtonPressMask | ButtonReleaseMask,
2551                                                           GrabModeSync, GrabModeAsync, None, None);
2552                 }
2553         }
2554
2555         if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2556             && !wwin->flags.is_gnustep) {
2557                 /* the passive grabs to focus the window */
2558                 /* if (wPreferences.focus_mode == WKF_CLICK) */
2559                 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2560                             True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2561         }
2562         XFlush(dpy);
2563 }
2564
2565 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2566 {
2567         if (attr->flags & GSExtraFlagsAttr) {
2568                 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2569                         wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2570                         wWindowUpdateButtonImages(wwin);
2571                 }
2572         }
2573 }
2574
2575 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2576 {
2577         WWindowState *wstate;
2578
2579         wstate = malloc(sizeof(WWindowState));
2580         if (!wstate)
2581                 return NULL;
2582
2583         memset(wstate, 0, sizeof(WWindowState));
2584         wstate->pid = pid;
2585         if (instance)
2586                 wstate->instance = wstrdup(instance);
2587         if (class)
2588                 wstate->class = wstrdup(class);
2589         if (command)
2590                 wstate->command = wstrdup(command);
2591         wstate->state = state;
2592
2593         wstate->next = windowState;
2594         windowState = wstate;
2595
2596         return wstate;
2597 }
2598
2599 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2600
2601 WMagicNumber wWindowGetSavedState(Window win)
2602 {
2603         char *instance, *class, *command = NULL;
2604         WWindowState *wstate = windowState;
2605
2606         if (!wstate)
2607                 return NULL;
2608
2609         command = GetCommandForWindow(win);
2610         if (!command)
2611                 return NULL;
2612
2613         if (PropGetWMClass(win, &class, &instance)) {
2614                 while (wstate) {
2615                         if (SAME(instance, wstate->instance) &&
2616                             SAME(class, wstate->class) && SAME(command, wstate->command)) {
2617                                 break;
2618                         }
2619                         wstate = wstate->next;
2620                 }
2621         } else {
2622                 wstate = NULL;
2623         }
2624
2625         if (command)
2626                 wfree(command);
2627         if (instance)
2628                 XFree(instance);
2629         if (class)
2630                 XFree(class);
2631
2632         return wstate;
2633 }
2634
2635 void wWindowDeleteSavedState(WMagicNumber id)
2636 {
2637         WWindowState *tmp, *wstate = (WWindowState *) id;
2638
2639         if (!wstate || !windowState)
2640                 return;
2641
2642         tmp = windowState;
2643         if (tmp == wstate) {
2644                 windowState = wstate->next;
2645                 if (wstate->instance)
2646                         wfree(wstate->instance);
2647                 if (wstate->class)
2648                         wfree(wstate->class);
2649                 if (wstate->command)
2650                         wfree(wstate->command);
2651                 wfree(wstate->state);
2652                 wfree(wstate);
2653         } else {
2654                 while (tmp->next) {
2655                         if (tmp->next == wstate) {
2656                                 tmp->next = wstate->next;
2657                                 if (wstate->instance)
2658                                         wfree(wstate->instance);
2659                                 if (wstate->class)
2660                                         wfree(wstate->class);
2661                                 if (wstate->command)
2662                                         wfree(wstate->command);
2663                                 wfree(wstate->state);
2664                                 wfree(wstate);
2665                                 break;
2666                         }
2667                         tmp = tmp->next;
2668                 }
2669         }
2670 }
2671
2672 void wWindowDeleteSavedStatesForPID(pid_t pid)
2673 {
2674         WWindowState *tmp, *wstate;
2675
2676         if (!windowState)
2677                 return;
2678
2679         tmp = windowState;
2680         if (tmp->pid == pid) {
2681                 wstate = windowState;
2682                 windowState = tmp->next;
2683                 if (wstate->instance)
2684                         wfree(wstate->instance);
2685                 if (wstate->class)
2686                         wfree(wstate->class);
2687                 if (wstate->command)
2688                         wfree(wstate->command);
2689                 wfree(wstate->state);
2690                 wfree(wstate);
2691         } else {
2692                 while (tmp->next) {
2693                         if (tmp->next->pid == pid) {
2694                                 wstate = tmp->next;
2695                                 tmp->next = wstate->next;
2696                                 if (wstate->instance)
2697                                         wfree(wstate->instance);
2698                                 if (wstate->class)
2699                                         wfree(wstate->class);
2700                                 if (wstate->command)
2701                                         wfree(wstate->command);
2702                                 wfree(wstate->state);
2703                                 wfree(wstate);
2704                                 break;
2705                         }
2706                         tmp = tmp->next;
2707                 }
2708         }
2709 }
2710
2711 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2712 {
2713         if (wwin->flags.omnipresent == flag)
2714                 return;
2715
2716         wwin->flags.omnipresent = flag;
2717         WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2718 }
2719
2720 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2721 {
2722         WWindow *wwin = data;
2723
2724 #ifndef NUMLOCK_HACK
2725         if ((event->xbutton.state & ValidModMask)
2726             != (event->xbutton.state & ~LockMask)) {
2727                 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"
2728                            "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2729         }
2730 #endif
2731
2732         event->xbutton.state &= ValidModMask;
2733
2734         CloseWindowMenu(wwin->screen_ptr);
2735
2736         if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2737             && !WFLAGP(wwin, no_focusable)) {
2738                 wSetFocusTo(wwin->screen_ptr, wwin);
2739         }
2740
2741         if (event->xbutton.button == Button1)
2742                 wRaiseFrame(wwin->frame->core);
2743
2744         if (event->xbutton.window != wwin->frame->resizebar->window) {
2745                 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2746                                  ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2747                                  GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2748                         return;
2749                 }
2750         }
2751
2752         if (event->xbutton.state & MOD_MASK) {
2753                 /* move the window */
2754                 wMouseMoveWindow(wwin, event);
2755                 XUngrabPointer(dpy, CurrentTime);
2756         } else {
2757                 wMouseResizeWindow(wwin, event);
2758                 XUngrabPointer(dpy, CurrentTime);
2759         }
2760 }
2761
2762 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2763 {
2764         WWindow *wwin = data;
2765
2766         event->xbutton.state &= ValidModMask;
2767
2768         if (event->xbutton.button == Button1) {
2769                 if (event->xbutton.state == 0) {
2770                         if (!WFLAGP(wwin, no_shadeable)) {
2771                                 /* shade window */
2772                                 if (wwin->flags.shaded)
2773                                         wUnshadeWindow(wwin);
2774                                 else
2775                                         wShadeWindow(wwin);
2776                         }
2777                 } else {
2778                         int dir = 0;
2779
2780                         if (event->xbutton.state & ControlMask)
2781                                 dir |= MAX_VERTICAL;
2782
2783                         if (event->xbutton.state & ShiftMask) {
2784                                 dir |= MAX_HORIZONTAL;
2785                                 if (!(event->xbutton.state & ControlMask))
2786                                         wSelectWindow(wwin, !wwin->flags.selected);
2787                         }
2788
2789                         /* maximize window */
2790                         if (dir != 0 && IS_RESIZABLE(wwin)) {
2791                                 int ndir = dir ^ wwin->flags.maximized;
2792
2793                                 if (ndir != 0)
2794                                         wMaximizeWindow(wwin, ndir);
2795                                 else
2796                                         wUnmaximizeWindow(wwin);
2797                         }
2798                 }
2799         } else if (event->xbutton.button == Button3) {
2800                 if (event->xbutton.state & MOD_MASK) {
2801                         wHideOtherApplications(wwin);
2802                 }
2803         } else if (event->xbutton.button == Button2) {
2804                 wSelectWindow(wwin, !wwin->flags.selected);
2805         } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2806                 wShadeWindow(wwin);
2807         } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2808                 wUnshadeWindow(wwin);
2809         }
2810 }
2811
2812 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2813 {
2814         WWindow *wwin = desc->parent;
2815         unsigned int new_width, w_scale;
2816         unsigned int new_height, h_scale;
2817         unsigned int resize_width_increment = 0;
2818         unsigned int resize_height_increment = 0;
2819
2820         if (wwin->normal_hints) {
2821                 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2822                 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2823                 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2824                 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2825         }
2826         if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2827                 resize_width_increment = wPreferences.resize_increment;
2828                 resize_height_increment = wPreferences.resize_increment;
2829         }
2830
2831         event->xbutton.state &= ValidModMask;
2832
2833         CloseWindowMenu(wwin->screen_ptr);
2834
2835         if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2836                 wSetFocusTo(wwin->screen_ptr, wwin);
2837
2838         if (event->xbutton.button == Button1)
2839                 wRaiseFrame(wwin->frame->core);
2840
2841         if (event->xbutton.state & ControlMask) {
2842                 if (event->xbutton.button == Button4) {
2843                         new_width = wwin->client.width - resize_width_increment;
2844                         wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2845                         wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2846                 }
2847                 if (event->xbutton.button == Button5) {
2848                         new_width = wwin->client.width + resize_width_increment;
2849                         wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2850                         wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2851                 }
2852         }
2853
2854         if (event->xbutton.state & MOD_MASK) {
2855                 /* move the window */
2856                 if (XGrabPointer(dpy, wwin->client_win, False,
2857                                  ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2858                                  GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2859                         return;
2860                 }
2861                 if (event->xbutton.button == Button3) {
2862                         wMouseResizeWindow(wwin, event);
2863                 } else if (event->xbutton.button == Button4) {
2864                         new_height = wwin->client.height - resize_height_increment;
2865                         wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2866                         wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2867                 } else if (event->xbutton.button == Button5) {
2868                         new_height = wwin->client.height + resize_height_increment;
2869                         wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2870                         wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2871                 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2872                         wMouseMoveWindow(wwin, event);
2873                 }
2874                 XUngrabPointer(dpy, CurrentTime);
2875         }
2876 }
2877
2878 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2879 {
2880         WWindow *wwin = (WWindow *) data;
2881
2882 #ifndef NUMLOCK_HACK
2883         if ((event->xbutton.state & ValidModMask)
2884             != (event->xbutton.state & ~LockMask)) {
2885                 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"
2886                            "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2887         }
2888 #endif
2889         event->xbutton.state &= ValidModMask;
2890
2891         CloseWindowMenu(wwin->screen_ptr);
2892
2893         if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2894             && !WFLAGP(wwin, no_focusable)) {
2895                 wSetFocusTo(wwin->screen_ptr, wwin);
2896         }
2897
2898         if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2899
2900                 if (event->xbutton.button == Button1) {
2901                         if (event->xbutton.state & MOD_MASK) {
2902                                 wLowerFrame(wwin->frame->core);
2903                         } else {
2904                                 wRaiseFrame(wwin->frame->core);
2905                         }
2906                 }
2907                 if ((event->xbutton.state & ShiftMask)
2908                     && !(event->xbutton.state & ControlMask)) {
2909                         wSelectWindow(wwin, !wwin->flags.selected);
2910                         return;
2911                 }
2912                 if (event->xbutton.window != wwin->frame->titlebar->window
2913                     && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2914                                     ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2915                                     GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2916                         return;
2917                 }
2918
2919                 /* move the window */
2920                 wMouseMoveWindow(wwin, event);
2921
2922                 XUngrabPointer(dpy, CurrentTime);
2923         } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2924                    && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2925                 WObjDescriptor *desc;
2926
2927                 if (event->xbutton.window != wwin->frame->titlebar->window
2928                     && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2929                                     ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2930                                     GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2931                         return;
2932                 }
2933
2934                 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2935
2936                 /* allow drag select */
2937                 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2938                 event->xany.send_event = True;
2939                 (*desc->handle_mousedown) (desc, event);
2940
2941                 XUngrabPointer(dpy, CurrentTime);
2942         }
2943 }
2944
2945 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2946 {
2947         WWindow *wwin = data;
2948
2949         event->xbutton.state &= ValidModMask;
2950
2951         CloseWindowMenu(wwin->screen_ptr);
2952
2953         if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2954                 return;
2955
2956         /* if control-click, kill the client */
2957         if (event->xbutton.state & ControlMask) {
2958                 wClientKill(wwin);
2959         } else {
2960                 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
2961                         /* send delete message */
2962                         wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2963                 }
2964         }
2965 }
2966
2967 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2968 {
2969         WWindow *wwin = data;
2970
2971         CloseWindowMenu(wwin->screen_ptr);
2972
2973         if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2974                 return;
2975
2976         /* send delete message */
2977         if (wwin->protocols.DELETE_WINDOW) {
2978                 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2979         } else {
2980                 wClientKill(wwin);
2981         }
2982 }
2983
2984 #ifdef XKB_BUTTON_HINT
2985 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2986 {
2987         WWindow *wwin = data;
2988         WFrameWindow *fwin = wwin->frame;
2989         WScreen *scr = fwin->screen_ptr;
2990         int tl;
2991
2992         if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2993                 return;
2994         tl = wwin->frame->languagemode;
2995         wwin->frame->languagemode = wwin->frame->last_languagemode;
2996         wwin->frame->last_languagemode = tl;
2997         wSetFocusTo(scr, wwin);
2998         wwin->frame->languagebutton_image =
2999             wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
3000         wFrameWindowUpdateLanguageButton(wwin->frame);
3001         if (event->xbutton.button == Button3)
3002                 return;
3003         wRaiseFrame(fwin->core);
3004 }
3005 #endif
3006
3007 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3008 {
3009         WWindow *wwin = data;
3010
3011         event->xbutton.state &= ValidModMask;
3012
3013         CloseWindowMenu(wwin->screen_ptr);
3014
3015         if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3016                 return;
3017
3018         if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
3019                 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
3020         } else {
3021                 WApplication *wapp;
3022                 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
3023
3024                         wapp = wApplicationOf(wwin->main_window);
3025                         if (wapp && !WFLAGP(wwin, no_appicon))
3026                                 wHideApplication(wapp);
3027                 } else if (event->xbutton.state == 0) {
3028                         wIconifyWindow(wwin);
3029                 }
3030         }
3031 }