Set missing WM_COMMAND using pid given from libXRes
[wmaker-crm.git] / src / window.c
blobef85f3d40cbc0f40d44dcec28b152f8fb9655db8
1 /* window.c - client window managing stuffs
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
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.
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.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #ifdef USE_XSHAPE
27 #include <X11/extensions/shape.h>
28 #endif
29 #ifdef KEEP_XKB_LOCK_STATUS
30 #include <X11/XKBlib.h>
31 #endif /* KEEP_XKB_LOCK_STATUS */
32 #ifdef USE_XRES
33 #include <X11/extensions/XRes.h>
34 #endif
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdint.h>
39 #include <math.h>
41 /* For getting mouse wheel mappings from WINGs */
42 #include <WINGs/WINGs.h>
44 #include "WindowMaker.h"
45 #include "GNUstep.h"
46 #include "wcore.h"
47 #include "framewin.h"
48 #include "texture.h"
49 #include "window.h"
50 #include "winspector.h"
51 #include "icon.h"
52 #include "properties.h"
53 #include "actions.h"
54 #include "client.h"
55 #include "colormap.h"
56 #include "keybind.h"
57 #include "stacking.h"
58 #include "defaults.h"
59 #include "workspace.h"
60 #include "xinerama.h"
61 #include "appmenu.h"
62 #include "appicon.h"
63 #include "superfluous.h"
64 #include "rootmenu.h"
65 #include "placement.h"
66 #include "misc.h"
67 #include "startup.h"
68 #include "winmenu.h"
69 #include "osdep.h"
71 #ifdef USE_MWM_HINTS
72 # include "motif.h"
73 #endif
74 #include "wmspec.h"
76 #define MOD_MASK wPreferences.modifier_mask
79 /***** Local Stuff *****/
80 static WWindowState *windowState = NULL;
81 static FocusMode getFocusMode(WWindow *wwin);
82 static int getSavedState(Window window, WSavedState **state);
83 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
85 /* frame window (during window grabs) */
86 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
88 /* close button */
89 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
90 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
92 /* iconify button */
93 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
95 #ifdef XKB_BUTTON_HINT
96 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
97 #endif
99 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
100 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
101 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
103 static void release_wwindowstate(WWindowState *wstate);
105 /****** Notification Observers ******/
107 static void appearanceObserver(void *self, WMNotification * notif)
109 WWindow *wwin = (WWindow *) self;
110 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
112 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
113 return;
115 if (flags & WFontSettings) {
116 wWindowConfigureBorders(wwin);
117 if (wwin->flags.shaded) {
118 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
119 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
120 wWindowSynthConfigureNotify(wwin);
123 if (flags & WTextureSettings)
124 wwin->frame->flags.need_texture_remake = 1;
126 if (flags & (WTextureSettings | WColorSettings)) {
127 if (wwin->frame->titlebar)
128 XClearWindow(dpy, wwin->frame->titlebar->window);
130 wFrameWindowPaint(wwin->frame);
135 /* Return the WWindow associated with a given (Xlib) Window. */
136 WWindow *wWindowFor(Window window)
138 WObjDescriptor *desc;
140 if (window == None)
141 return NULL;
143 if (XFindContext(dpy, window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT)
144 return NULL;
146 if (desc->parent_type == WCLASS_WINDOW)
147 return desc->parent;
148 else if (desc->parent_type == WCLASS_FRAME) {
149 WFrameWindow *frame = (WFrameWindow *) desc->parent;
150 if (frame->flags.is_client_window_frame)
151 return frame->child;
154 return NULL;
157 WWindow *wWindowCreate(void)
159 WWindow *wwin;
161 wwin = wmalloc(sizeof(WWindow));
162 wretain(wwin);
164 wwin->client_descriptor.handle_mousedown = frameMouseDown;
165 wwin->client_descriptor.parent = wwin;
166 wwin->client_descriptor.self = wwin;
167 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
169 return wwin;
172 void wWindowDestroy(WWindow *wwin)
174 int i;
176 if (wwin->screen_ptr->cmap_window == wwin)
177 wwin->screen_ptr->cmap_window = NULL;
179 WMRemoveNotificationObserver(wwin);
181 wwin->flags.destroyed = 1;
183 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
184 if (!wwin->screen_ptr->shortcutWindows[i])
185 continue;
187 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
189 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
190 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
191 wwin->screen_ptr->shortcutWindows[i] = NULL;
195 if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
196 wwin->fake_group->retainCount--;
197 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
198 XDestroyWindow(dpy, wwin->fake_group->leader);
199 wwin->fake_group->leader = None;
200 wwin->fake_group->origLeader = None;
201 XFlush(dpy);
205 if (wwin->normal_hints)
206 XFree(wwin->normal_hints);
208 if (wwin->wm_hints)
209 XFree(wwin->wm_hints);
211 if (wwin->wm_instance)
212 XFree(wwin->wm_instance);
214 if (wwin->wm_class)
215 XFree(wwin->wm_class);
217 if (wwin->wm_gnustep_attr)
218 wfree(wwin->wm_gnustep_attr);
220 if (wwin->cmap_windows)
221 XFree(wwin->cmap_windows);
223 XDeleteContext(dpy, wwin->client_win, w_global.context.client_win);
225 if (wwin->frame)
226 wFrameWindowDestroy(wwin->frame);
228 if (wwin->icon) {
229 RemoveFromStackList(wwin->icon->core);
230 wIconDestroy(wwin->icon);
231 if (wPreferences.auto_arrange_icons)
232 wArrangeIcons(wwin->screen_ptr, True);
234 if (wwin->net_icon_image)
235 RReleaseImage(wwin->net_icon_image);
237 wrelease(wwin);
240 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
242 if (gs_hints->flags & GSWindowStyleAttr) {
243 if (gs_hints->window_style == WMBorderlessWindowMask) {
244 wwin->client_flags.no_border = 1;
245 wwin->client_flags.no_titlebar = 1;
246 wwin->client_flags.no_closable = 1;
247 wwin->client_flags.no_miniaturizable = 1;
248 wwin->client_flags.no_resizable = 1;
249 wwin->client_flags.no_close_button = 1;
250 wwin->client_flags.no_miniaturize_button = 1;
251 wwin->client_flags.no_resizebar = 1;
252 } else {
253 wwin->client_flags.no_close_button =
254 ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
256 wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
258 wwin->client_flags.no_miniaturize_button =
259 ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
261 wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
263 wwin->client_flags.no_resizebar =
264 ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
266 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
268 /* these attributes supposedly imply in the existence
269 * of a titlebar */
270 if (gs_hints->window_style & (WMResizableWindowMask |
271 WMClosableWindowMask | WMMiniaturizableWindowMask)) {
272 wwin->client_flags.no_titlebar = 0;
273 } else {
274 wwin->client_flags.no_titlebar =
275 ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
279 } else {
280 /* setup the defaults */
281 wwin->client_flags.no_border = 0;
282 wwin->client_flags.no_titlebar = 0;
283 wwin->client_flags.no_closable = 0;
284 wwin->client_flags.no_miniaturizable = 0;
285 wwin->client_flags.no_resizable = 0;
286 wwin->client_flags.no_close_button = 0;
287 wwin->client_flags.no_miniaturize_button = 0;
288 wwin->client_flags.no_resizebar = 0;
290 if (gs_hints->extra_flags & GSNoApplicationIconFlag)
291 wwin->client_flags.no_appicon = 1;
294 static void discard_hints_from_gtk(WWindow *wwin)
296 Atom type;
297 int format;
298 unsigned long nb_item, nb_remain;
299 unsigned char *result;
300 int status;
302 status = XGetWindowProperty(dpy, wwin->client_win, w_global.atom.desktop.gtk_object_path, 0, 16, False,
303 AnyPropertyType, &type, &format, &nb_item, &nb_remain, &result);
304 if (status != Success)
305 return;
307 /* If we're here, that means the Property exists. We don't care what is inside, it means it is a GTK-based application */
309 if (result != NULL)
310 XFree(result);
312 /* GTK is asking to remove these decorations: */
313 wwin->client_flags.no_titlebar = 0;
314 wwin->client_flags.no_close_button = 0;
315 wwin->client_flags.no_miniaturize_button = 0;
316 wwin->client_flags.no_resizebar = 0;
319 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
321 WScreen *scr = wwin->screen_ptr;
323 /* sets global default stuff */
324 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags, NULL, True);
325 wwin->defined_user_flags = wwin->user_flags;
328 * Decoration setting is done in this precedence (lower to higher)
329 * - use global default in the resource database
330 * - guess some settings
331 * - use GNUstep/external window attributes
332 * - set hints specified for the app in the resource DB
335 wwin->client_flags.broken_close = 0;
337 if (wwin->protocols.DELETE_WINDOW)
338 wwin->client_flags.kill_close = 0;
339 else
340 wwin->client_flags.kill_close = 1;
342 /* transients can't be iconified or maximized */
343 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
344 wwin->client_flags.no_miniaturizable = 1;
345 wwin->client_flags.no_miniaturize_button = 1;
348 /* if the window can't be resized, remove the resizebar */
349 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
350 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
351 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
352 wwin->client_flags.no_resizable = 1;
353 wwin->client_flags.no_resizebar = 1;
356 /* set GNUstep window attributes */
357 if (wwin->wm_gnustep_attr) {
358 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
360 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
362 *level = wwin->wm_gnustep_attr->window_level;
364 * INT_MIN is the only illegal window level.
366 if (*level == INT_MIN)
367 *level = INT_MIN + 1;
368 } else {
369 /* setup defaults */
370 *level = WMNormalLevel;
372 } else {
373 int tmp_workspace = -1;
374 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
376 #ifdef USE_MWM_HINTS
377 wMWMCheckClientHints(wwin);
378 #endif /* USE_MWM_HINTS */
380 wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
382 if (wPreferences.ignore_gtk_decoration_hints)
383 discard_hints_from_gtk(wwin);
385 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
386 * have INT_MIN that means that no window level was requested. -Dan
388 if (tmp_level == INT_MIN) {
389 if (WFLAGP(wwin, floating))
390 *level = WMFloatingLevel;
391 else if (WFLAGP(wwin, sunken))
392 *level = WMSunkenLevel;
393 else
394 *level = WMNormalLevel;
395 } else {
396 *level = tmp_level;
399 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
400 WWindow *transientOwner = wWindowFor(wwin->transient_for);
401 if (transientOwner) {
402 int ownerLevel = transientOwner->frame->core->stacking->window_level;
403 if (ownerLevel > *level)
404 *level = ownerLevel;
408 if (tmp_workspace >= 0)
409 *workspace = tmp_workspace % scr->workspace_count;
413 * Set attributes specified only for that window/class.
414 * This might do duplicate work with the 1st wDefaultFillAttributes().
416 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags,
417 &wwin->defined_user_flags, False);
419 /* Restore decoration if the user has enabled the
420 * IgnoreDecorationChanges option */
421 if (wwin->user_flags.ignore_decoration_changes) {
422 WSETUFLAG(wwin, no_titlebar, 0);
423 WSETUFLAG(wwin, no_resizable, 0);
424 WSETUFLAG(wwin, no_miniaturizable, 0);
425 WSETUFLAG(wwin, no_resizebar, 0);
426 WSETUFLAG(wwin, no_close_button, 0);
427 WSETUFLAG(wwin, no_miniaturize_button, 0);
428 WSETUFLAG(wwin, no_border, 0);
429 WSETUFLAG(wwin, no_movable, 0);
433 * Sanity checks for attributes that depend on other attributes
435 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
436 wwin->user_flags.emulate_appicon = 0;
438 if (wwin->main_window != None) {
439 WApplication *wapp = wApplicationOf(wwin->main_window);
440 if (wapp && !wapp->flags.emulated)
441 wwin->user_flags.emulate_appicon = 0;
444 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
445 wwin->user_flags.emulate_appicon = 0;
447 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
448 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
449 wwin->user_flags.sunken = 0;
451 /* A window that does not have a title cannot be Shaded and we don't let user override this */
452 wwin->client_flags.no_shadeable = WFLAGP(wwin, no_titlebar);
453 wwin->defined_user_flags.no_shadeable = 0;
455 /* windows that have takefocus=False shouldn't take focus at all */
456 if (wwin->focus_mode == WFM_NO_INPUT)
457 wwin->client_flags.no_focusable = 1;
460 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
462 int w1, h1, w2, h2;
464 w1 = wwin->frame->core->width;
465 h1 = wwin->frame->core->height;
466 w2 = obscured->frame->core->width;
467 h2 = obscured->frame->core->height;
469 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
470 && wwin->frame->workspace != obscured->frame->workspace)
471 return False;
473 if (wwin->frame_x + w1 < obscured->frame_x
474 || wwin->frame_y + h1 < obscured->frame_y
475 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
476 return False;
478 return True;
481 /* Get the corresponding Process Identification Number of the active window */
482 static pid_t getWindowPid(Window win)
484 pid_t pid = -1;
486 pid = wNETWMGetPidForWindow(win);
487 #ifdef USE_XRES
488 if (pid > 0)
489 return pid;
490 else {
491 XResClientIdSpec spec;
492 int status;
493 long i, num_ids = 0;
494 XResClientIdValue *client_ids = NULL;
496 spec.client = win;
497 spec.mask = XRES_CLIENT_ID_PID_MASK;
499 status = XResQueryClientIds(dpy, 1, &spec, &num_ids, &client_ids);
501 if (status != Success)
502 return -1;
504 for (i = 0; i < num_ids; i++) {
505 if (client_ids[i].spec.mask == XRES_CLIENT_ID_PID_MASK) {
506 pid = XResGetClientPid(&client_ids[i]);
507 break;
510 XResClientIdsDestroy(num_ids, client_ids);
512 #endif
513 return pid;
516 static void fixLeaderProperties(WWindow *wwin)
518 XClassHint *classHint;
519 XWMHints *hints, *clientHints;
520 XWindowAttributes attr;
521 Window leaders[2], window;
522 char **argv, *command;
523 int argc, i, pid;
524 Bool haveCommand;
526 classHint = XAllocClassHint();
527 clientHints = XGetWMHints(dpy, wwin->client_win);
528 pid = getWindowPid(wwin->client_win);
529 if (pid > 0)
530 haveCommand = GetCommandForPid(pid, &argv, &argc);
531 else
532 haveCommand = False;
534 leaders[0] = wwin->client_leader;
535 leaders[1] = wwin->group_id;
537 if (haveCommand) {
538 command = GetCommandForWindow(wwin->client_win);
539 if (command)
540 wfree(command); /* command already set. nothing to do. */
541 else
542 XSetCommand(dpy, wwin->client_win, argv, argc);
545 for (i = 0; i < 2; i++) {
546 window = leaders[i];
547 if (window) {
548 if (XGetClassHint(dpy, window, classHint) == 0) {
549 classHint->res_name = wwin->wm_instance;
550 classHint->res_class = wwin->wm_class;
551 XSetClassHint(dpy, window, classHint);
553 hints = XGetWMHints(dpy, window);
554 if (hints) {
555 XFree(hints);
556 } else if (clientHints) {
557 /* set window group leader to self */
558 clientHints->window_group = window;
559 clientHints->flags |= WindowGroupHint;
560 XSetWMHints(dpy, window, clientHints);
563 if (haveCommand) {
564 command = GetCommandForWindow(window);
565 if (command)
566 wfree(command); /* command already set. nothing to do. */
567 else
568 XSetCommand(dpy, window, argv, argc);
571 /* Make sure we get notification when this window is destroyed */
572 if (XGetWindowAttributes(dpy, window, &attr))
573 XSelectInput(dpy, window, attr.your_event_mask | StructureNotifyMask | PropertyChangeMask);
577 XFree(classHint);
578 if (clientHints)
579 XFree(clientHints);
580 if (haveCommand)
581 wfree(argv);
584 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
586 XClassHint *classHint;
587 XWMHints *hints;
588 Window leader;
589 int argc;
590 char **argv;
592 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
593 /* set class hint */
594 classHint = XAllocClassHint();
595 classHint->res_name = instance;
596 classHint->res_class = class;
597 XSetClassHint(dpy, leader, classHint);
598 XFree(classHint);
600 /* inherit these from the original leader if available */
601 hints = XGetWMHints(dpy, win);
602 if (!hints) {
603 hints = XAllocWMHints();
604 hints->flags = 0;
606 /* set window group leader to self */
607 hints->window_group = leader;
608 hints->flags |= WindowGroupHint;
609 XSetWMHints(dpy, leader, hints);
610 XFree(hints);
612 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
613 XSetCommand(dpy, leader, argv, argc);
614 XFreeStringList(argv);
617 return leader;
620 static int matchIdentifier(const void *item, const void *cdata)
622 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
626 *----------------------------------------------------------------
627 * wManageWindow--
628 * reparents the window and allocates a descriptor for it.
629 * Window manager hints and other hints are fetched to configure
630 * the window decoration attributes and others. User preferences
631 * for the window are used if available, to configure window
632 * decorations and some behaviour.
633 * If in startup, windows that are override redirect,
634 * unmapped and never were managed and are Withdrawn are not
635 * managed.
637 * Returns:
638 * the new window descriptor
640 * Side effects:
641 * The window is reparented and appropriate notification
642 * is done to the client. Input mask for the window is setup.
643 * The window descriptor is also associated with various window
644 * contexts and inserted in the head of the window list.
645 * Event handler contexts are associated for some objects
646 * (buttons, titlebar and resizebar)
648 *----------------------------------------------------------------
650 WWindow *wManageWindow(WScreen *scr, Window window)
652 WWindow *wwin;
653 int x, y;
654 unsigned width, height;
655 XWindowAttributes wattribs;
656 XSetWindowAttributes attribs;
657 WWindowState *win_state;
658 WWindow *transientOwner = NULL;
659 int window_level;
660 int wm_state;
661 int foo;
662 int workspace = -1;
663 char *title;
664 Bool withdraw = False;
665 Bool raise = False;
667 /* mutex. */
668 XGrabServer(dpy);
669 XSync(dpy, False);
671 /* make sure the window is still there */
672 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
673 XUngrabServer(dpy);
674 return NULL;
677 /* if it's an override-redirect, ignore it */
678 if (wattribs.override_redirect) {
679 XUngrabServer(dpy);
680 return NULL;
683 wm_state = PropGetWindowState(window);
685 /* if it's startup and the window is unmapped, don't manage it */
686 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
687 XUngrabServer(dpy);
688 return NULL;
691 wwin = wWindowCreate();
693 title = wNETWMGetWindowName(window);
694 if (title)
695 wwin->flags.net_has_title = 1;
696 else if (!wFetchName(dpy, window, &title))
697 title = NULL;
699 XSaveContext(dpy, window, w_global.context.client_win, (XPointer) & wwin->client_descriptor);
701 #ifdef USE_XSHAPE
702 if (w_global.xext.shape.supported) {
703 int junk;
704 unsigned int ujunk;
705 int b_shaped;
707 XShapeSelectInput(dpy, window, ShapeNotifyMask);
708 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
709 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
710 wwin->flags.shaped = b_shaped;
712 #endif
714 /* Get hints and other information in properties */
715 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
717 /* setup descriptor */
718 wwin->client_win = window;
719 wwin->screen_ptr = scr;
720 wwin->old_border_width = wattribs.border_width;
721 wwin->event_mask = CLIENT_EVENTS;
722 attribs.event_mask = CLIENT_EVENTS;
723 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
724 attribs.save_under = False;
726 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
727 XSetWindowBorderWidth(dpy, window, 0);
729 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") != 0)
730 wwin->flags.fullscreen_monitors[0] = -1;
732 /* get hints from GNUstep app */
733 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0)
734 wwin->flags.is_gnustep = 1;
736 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr))
737 wwin->wm_gnustep_attr = NULL;
739 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
740 wwin->flags.is_dockapp = 1;
741 withdraw = True;
744 wwin->client_leader = PropGetClientLeader(window);
745 if (wwin->client_leader != None)
746 wwin->main_window = wwin->client_leader;
748 wwin->wm_hints = XGetWMHints(dpy, window);
750 if (wwin->wm_hints) {
751 if (wwin->wm_hints->flags & StateHint) {
752 if (wwin->wm_hints->initial_state == IconicState) {
753 wwin->flags.miniaturized = 1;
754 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
755 wwin->flags.is_dockapp = 1;
756 withdraw = True;
760 if (wwin->wm_hints->flags & WindowGroupHint) {
761 wwin->group_id = wwin->wm_hints->window_group;
762 /* window_group has priority over CLIENT_LEADER */
763 wwin->main_window = wwin->group_id;
764 } else {
765 wwin->group_id = None;
768 if (wwin->wm_hints->flags & UrgencyHint) {
769 wwin->flags.urgent = 1;
770 wAppBounceWhileUrgent(wApplicationOf(wwin->main_window));
772 } else {
773 wwin->group_id = None;
776 PropGetProtocols(window, &wwin->protocols);
778 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
779 wwin->transient_for = None;
780 } else {
781 if (wwin->transient_for == None || wwin->transient_for == window) {
782 wwin->transient_for = scr->root_win;
783 } else {
784 transientOwner = wWindowFor(wwin->transient_for);
785 if (transientOwner && transientOwner->main_window != None)
786 wwin->main_window = transientOwner->main_window;
790 /* guess the focus mode */
791 wwin->focus_mode = getFocusMode(wwin);
793 /* get geometry stuff */
794 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
796 /* get colormap windows */
797 GetColormapWindows(wwin);
800 * Setup the decoration/window attributes and
801 * geometry
803 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
805 /* Make broken apps behave as a nice app. */
806 if (WFLAGP(wwin, emulate_appicon))
807 wwin->main_window = wwin->client_win;
809 fixLeaderProperties(wwin);
811 wwin->orig_main_window = wwin->main_window;
813 if (wwin->flags.is_gnustep)
814 wwin->client_flags.shared_appicon = 0;
816 if (wwin->main_window) {
817 XTextProperty text_prop;
819 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, w_global.atom.wmaker.menu))
820 wwin->client_flags.shared_appicon = 0;
823 if (wwin->flags.is_dockapp)
824 wwin->client_flags.shared_appicon = 0;
826 if (wwin->main_window) {
827 WApplication *app = wApplicationOf(wwin->main_window);
828 if (app && app->app_icon)
829 wwin->client_flags.shared_appicon = 0;
832 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
833 char *buffer, *instance, *class;
834 WFakeGroupLeader *fPtr;
835 int index;
837 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
839 /* // only enter here if PropGetWMClass() succeeds */
840 PropGetWMClass(wwin->main_window, &class, &instance);
841 buffer = StrConcatDot(instance, class);
843 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
844 if (index != WANotFound) {
845 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
846 if (fPtr->retainCount == 0)
847 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
848 instance, class);
850 fPtr->retainCount++;
851 if (fPtr->origLeader == None) {
852 if (ADEQUATE(wwin->main_window)) {
853 fPtr->retainCount++;
854 fPtr->origLeader = wwin->main_window;
857 wwin->fake_group = fPtr;
858 wwin->main_window = fPtr->leader;
859 wfree(buffer);
860 } else {
861 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
863 fPtr->identifier = buffer;
864 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
865 fPtr->origLeader = None;
866 fPtr->retainCount = 1;
868 WMAddToArray(scr->fakeGroupLeaders, fPtr);
870 if (ADEQUATE(wwin->main_window)) {
871 fPtr->retainCount++;
872 fPtr->origLeader = wwin->main_window;
874 wwin->fake_group = fPtr;
875 wwin->main_window = fPtr->leader;
878 if (instance)
879 free(instance);
881 if (class)
882 free(class);
883 #undef ADEQUATE
887 * Setup the initial state of the window
889 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
890 wwin->flags.miniaturized = 1;
892 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
893 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
895 wNETWMCheckInitialClientState(wwin);
897 /* apply previous state if it exists and we're in startup */
898 if (scr->flags.startup && wm_state >= 0) {
899 if (wm_state == IconicState)
900 wwin->flags.miniaturized = 1;
901 else if (wm_state == WithdrawnState)
902 withdraw = True;
905 /* if there is a saved state (from file), restore it */
906 win_state = NULL;
907 if (wwin->main_window != None)
908 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
909 else
910 win_state = (WWindowState *) wWindowGetSavedState(window);
912 if (win_state && !withdraw) {
913 if (win_state->state->hidden > 0)
914 wwin->flags.hidden = win_state->state->hidden;
916 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
917 wwin->flags.shaded = win_state->state->shaded;
919 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable))
920 wwin->flags.miniaturized = win_state->state->miniaturized;
922 if (!IS_OMNIPRESENT(wwin)) {
923 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
924 wwin->wm_class);
925 if (w < 0 || w >= scr->workspace_count) {
926 workspace = win_state->state->workspace;
927 if (workspace >= scr->workspace_count)
928 workspace = scr->current_workspace;
929 } else {
930 workspace = w;
932 } else {
933 workspace = scr->current_workspace;
937 /* if we're restarting, restore saved state (from hints).
938 * This will overwrite previous */
940 WSavedState *wstate;
942 if (getSavedState(window, &wstate)) {
943 wwin->flags.shaded = wstate->shaded;
944 wwin->flags.hidden = wstate->hidden;
945 wwin->flags.miniaturized = wstate->miniaturized;
946 wwin->flags.maximized = wstate->maximized;
947 if (wwin->flags.maximized) {
948 wwin->old_geometry.x = wstate->x;
949 wwin->old_geometry.y = wstate->y;
950 wwin->old_geometry.width = wstate->w;
951 wwin->old_geometry.height = wstate->h;
954 workspace = wstate->workspace;
955 } else {
956 wstate = NULL;
959 /* restore window shortcut */
960 if (wstate != NULL || win_state != NULL) {
961 unsigned mask = 0;
963 if (win_state != NULL)
964 mask = win_state->state->window_shortcuts;
966 if (wstate != NULL && mask == 0)
967 mask = wstate->window_shortcuts;
969 if (mask > 0) {
970 int i;
972 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
973 if (mask & (1 << i)) {
974 if (!scr->shortcutWindows[i])
975 scr->shortcutWindows[i] = WMCreateArray(4);
977 WMAddToArray(scr->shortcutWindows[i], wwin);
983 if (wstate != NULL)
984 wfree(wstate);
987 /* don't let transients start miniaturized if their owners are not */
988 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
989 wwin->flags.miniaturized = 0;
990 if (wwin->wm_hints)
991 wwin->wm_hints->initial_state = NormalState;
994 /* set workspace on which the window starts */
995 if (workspace >= 0) {
996 if (workspace > scr->workspace_count - 1)
997 workspace = workspace % scr->workspace_count;
998 } else {
999 int w;
1001 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
1003 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
1004 workspace = w;
1005 } else {
1006 if (wPreferences.open_transients_with_parent && transientOwner)
1007 workspace = transientOwner->frame->workspace;
1008 else
1009 workspace = scr->current_workspace;
1013 /* setup window geometry */
1014 if (win_state && win_state->state->w > 0) {
1015 width = win_state->state->w;
1016 height = win_state->state->h;
1019 wWindowConstrainSize(wwin, &width, &height);
1021 /* do not ask for window placement if the window is
1022 * transient, during startup, or if the window wants
1023 * to start iconic. If geometry was saved, restore it. */
1025 Bool dontBring = False;
1027 if (win_state && win_state->state->w > 0) {
1028 x = win_state->state->x;
1029 y = win_state->state->y;
1030 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
1031 && !scr->flags.startup
1032 && !wwin->flags.miniaturized
1033 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
1035 if (transientOwner && transientOwner->flags.mapped) {
1036 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
1037 WMRect rect;
1038 int head;
1040 x = transientOwner->frame_x +
1041 abs(((int)transientOwner->frame->core->width - (int)width) / 2) + offs;
1042 y = transientOwner->frame_y +
1043 abs(((int)transientOwner->frame->core->height - (int)height) / 3) + offs;
1045 /* limit transient windows to be inside their parent's head */
1046 rect.pos.x = transientOwner->frame_x;
1047 rect.pos.y = transientOwner->frame_y;
1048 rect.size.width = transientOwner->frame->core->width;
1049 rect.size.height = transientOwner->frame->core->height;
1051 head = wGetHeadForRect(scr, rect);
1052 rect = wGetRectForHead(scr, head);
1054 if (x < rect.pos.x)
1055 x = rect.pos.x;
1056 else if (x + width > rect.pos.x + rect.size.width)
1057 x = rect.pos.x + rect.size.width - width;
1059 if (y < rect.pos.y)
1060 y = rect.pos.y;
1061 else if (y + height > rect.pos.y + rect.size.height)
1062 y = rect.pos.y + rect.size.height - height;
1064 } else {
1065 PlaceWindow(wwin, &x, &y, width, height);
1068 if (wPreferences.window_placement == WPM_MANUAL)
1069 dontBring = True;
1071 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1072 int head, flags;
1073 WMRect rect;
1074 int reposition = 0;
1076 /* Make spash screens come out in the center of a head
1077 * trouble is that most splashies never get here
1078 * they are managed trough atoms but god knows where.
1079 * Dan, do you know ? -peter
1081 * Most of them are not managed, they have set
1082 * OverrideRedirect, which means we can't do anything about
1083 * them. -alfredo */
1085 /* xinerama checks for: across head and dead space */
1086 rect.pos.x = x;
1087 rect.pos.y = y;
1088 rect.size.width = width;
1089 rect.size.height = height;
1091 head = wGetRectPlacementInfo(scr, rect, &flags);
1093 if (flags & XFLAG_DEAD)
1094 reposition = 1;
1096 if (flags & XFLAG_MULTIPLE)
1097 reposition = 2;
1100 switch (reposition) {
1101 case 1:
1102 head = wGetHeadForPointerLocation(scr);
1103 rect = wGetRectForHead(scr, head);
1105 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1106 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1107 break;
1109 case 2:
1110 rect = wGetRectForHead(scr, head);
1112 if (x < rect.pos.x)
1113 x = rect.pos.x;
1114 else if (x + width > rect.pos.x + rect.size.width)
1115 x = rect.pos.x + rect.size.width - width;
1117 if (y < rect.pos.y)
1118 y = rect.pos.y;
1119 else if (y + height > rect.pos.y + rect.size.height)
1120 y = rect.pos.y + rect.size.height - height;
1122 break;
1124 default:
1125 break;
1129 if (WFLAGP(wwin, dont_move_off) && dontBring)
1130 wScreenBringInside(scr, &x, &y, width, height);
1133 wNETWMPositionSplash(wwin, &x, &y, width, height);
1135 if (wwin->flags.urgent) {
1136 if (!IS_OMNIPRESENT(wwin))
1137 wwin->flags.omnipresent ^= 1;
1140 /* Create frame, borders and do reparenting */
1141 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1142 #ifdef XKB_BUTTON_HINT
1143 if (wPreferences.modelock)
1144 foo |= WFF_LANGUAGE_BUTTON;
1145 #endif
1146 if (HAS_TITLEBAR(wwin))
1147 foo |= WFF_TITLEBAR;
1149 if (HAS_RESIZEBAR(wwin))
1150 foo |= WFF_RESIZEBAR;
1152 if (HAS_BORDER(wwin))
1153 foo |= WFF_BORDER;
1155 wwin->frame = wFrameWindowCreate(scr, window_level,
1156 x, y, width, height,
1157 &wPreferences.window_title_clearance,
1158 &wPreferences.window_title_min_height,
1159 &wPreferences.window_title_max_height,
1160 foo,
1161 scr->window_title_texture,
1162 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1163 wattribs.depth, wattribs.visual, wattribs.colormap);
1165 wwin->frame->flags.is_client_window_frame = 1;
1166 wwin->frame->flags.justification = wPreferences.title_justification;
1168 wNETWMCheckInitialFrameState(wwin);
1170 /* setup button images */
1171 wWindowUpdateButtonImages(wwin);
1173 /* hide unused buttons */
1174 foo = 0;
1175 if (WFLAGP(wwin, no_close_button))
1176 foo |= WFF_RIGHT_BUTTON;
1178 if (WFLAGP(wwin, no_miniaturize_button))
1179 foo |= WFF_LEFT_BUTTON;
1181 #ifdef XKB_BUTTON_HINT
1182 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1183 foo |= WFF_LANGUAGE_BUTTON;
1184 #endif
1185 if (foo != 0)
1186 wFrameWindowHideButton(wwin->frame, foo);
1188 wwin->frame->child = wwin;
1189 wwin->frame->workspace = workspace;
1190 wwin->frame->on_click_left = windowIconifyClick;
1192 #ifdef XKB_BUTTON_HINT
1193 if (wPreferences.modelock)
1194 wwin->frame->on_click_language = windowLanguageClick;
1195 #endif
1197 wwin->frame->on_click_right = windowCloseClick;
1198 wwin->frame->on_dblclick_right = windowCloseDblClick;
1199 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1200 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1201 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1203 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1204 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1205 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1208 int gx, gy;
1210 wClientGetGravityOffsets(wwin, &gx, &gy);
1212 /* if gravity is to the south, account for the border sizes */
1213 if (gy > 0)
1214 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1218 * wWindowConfigure() will init the client window's size
1219 * (wwin->client.{width,height}) and all other geometry
1220 * related variables (frame_x,frame_y) */
1221 wWindowConfigure(wwin, x, y, width, height);
1223 /* to make sure the window receives it's new position after reparenting */
1224 wWindowSynthConfigureNotify(wwin);
1226 /* Setup descriptors and save window to internal lists */
1227 if (wwin->main_window != None) {
1228 WApplication *app;
1229 WWindow *leader;
1231 /* Leader windows do not necessary set themselves as leaders.
1232 * If this is the case, point the leader of this window to
1233 * itself */
1234 leader = wWindowFor(wwin->main_window);
1235 if (leader && leader->main_window == None)
1236 leader->main_window = leader->client_win;
1238 app = wApplicationCreate(wwin);
1239 if (app) {
1240 app->last_workspace = workspace;
1242 /* Do application specific stuff, like setting application
1243 * wide attributes. */
1245 if (wwin->flags.hidden) {
1246 /* if the window was set to hidden because it was hidden
1247 * in a previous incarnation and that state was restored */
1248 app->flags.hidden = 1;
1249 } else if (app->flags.hidden) {
1250 if (WFLAGP(app->main_window_desc, start_hidden)) {
1251 wwin->flags.hidden = 1;
1252 } else {
1253 wUnhideApplication(app, False, False);
1254 raise = True;
1257 wAppBounce(app);
1261 /* setup the frame descriptor */
1262 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1263 wwin->frame->core->descriptor.parent = wwin;
1264 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1266 /* don't let windows go away if we die */
1267 XAddToSaveSet(dpy, window);
1269 XLowerWindow(dpy, window);
1271 /* if window is in this workspace and should be mapped, then map it */
1272 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1273 && !wwin->flags.hidden && !withdraw) {
1275 /* The following "if" is to avoid crashing of clients that expect
1276 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1277 * after the return from this function. */
1278 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint))
1279 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1280 else
1281 wClientSetState(wwin, NormalState, None);
1283 if (wPreferences.superfluous && !wPreferences.no_animations && !scr->flags.startup &&
1284 (wwin->transient_for == None || wwin->transient_for == scr->root_win) &&
1286 * The brain damaged idiotic non-click to focus modes will
1287 * have trouble with this because:
1289 * 1. window is created and mapped by the client
1290 * 2. window is mapped by wmaker in small size
1291 * 3. window is animated to grow to normal size
1292 * 4. this function returns to normal event loop
1293 * 5. eventually, the EnterNotify event that would trigger
1294 * the window focusing (if the mouse is over that window)
1295 * will be processed by wmaker.
1296 * But since this event will be rather delayed
1297 * (step 3 has a large delay) the time when the event occurred
1298 * and when it is processed, the client that owns that window
1299 * will reject the XSetInputFocus() for it.
1301 (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus))
1302 DoWindowBirth(wwin);
1304 wWindowMap(wwin);
1307 /* setup stacking descriptor */
1308 if (transientOwner)
1309 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1310 else
1311 wwin->frame->core->stacking->child_of = NULL;
1313 if (!scr->focused_window) {
1314 /* first window on the list */
1315 wwin->next = NULL;
1316 wwin->prev = NULL;
1317 scr->focused_window = wwin;
1318 } else {
1319 WWindow *tmp;
1321 /* add window at beginning of focus window list */
1322 tmp = scr->focused_window;
1323 while (tmp->prev)
1324 tmp = tmp->prev;
1326 tmp->prev = wwin;
1327 wwin->next = tmp;
1328 wwin->prev = NULL;
1331 /* raise is set to true if we un-hid the app when this window was born.
1332 * we raise, else old windows of this app will be above this new one. */
1333 if (raise)
1334 wRaiseFrame(wwin->frame->core);
1336 /* Update name must come after WApplication stuff is done */
1337 wWindowUpdateName(wwin, title);
1338 if (title)
1339 XFree(title);
1341 XUngrabServer(dpy);
1343 /* Final preparations before window is ready to go */
1344 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1346 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1347 if (((transientOwner && transientOwner->flags.focused)
1348 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1350 /* only auto_focus if on same screen as mouse
1351 * (and same head for xinerama mode)
1352 * TODO: make it an option */
1354 /*TODO add checking the head of the window, is it available? */
1355 short same_screen = 0, same_head = 1;
1357 int foo;
1358 unsigned int bar;
1359 Window dummy;
1361 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1362 &foo, &foo, &foo, &foo, &bar) != False)
1363 same_screen = 1;
1365 if (same_screen == 1 && same_head == 1)
1366 wSetFocusTo(scr, wwin);
1369 wWindowResetMouseGrabs(wwin);
1371 if (!WFLAGP(wwin, no_bind_keys))
1372 wWindowSetKeyGrabs(wwin);
1374 WMPostNotificationName(WMNManaged, wwin, NULL);
1375 wColormapInstallForWindow(scr, scr->cmap_window);
1377 /* Setup Notification Observers */
1378 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1380 /* Cleanup temporary stuff */
1381 if (win_state)
1382 wWindowDeleteSavedState(win_state);
1384 /* If the window must be withdrawed, then do it now.
1385 * Must do some optimization, 'though */
1386 if (withdraw) {
1387 wwin->flags.mapped = 0;
1388 wClientSetState(wwin, WithdrawnState, None);
1389 wUnmanageWindow(wwin, True, False);
1390 wwin = NULL;
1393 return wwin;
1396 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1397 const char *title, int x, int y, int width, int height)
1399 WWindow *wwin;
1400 int foo;
1402 wwin = wWindowCreate();
1404 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1406 wwin->flags.internal_window = 1;
1408 wwin->client_flags.omnipresent = 1;
1409 wwin->client_flags.no_shadeable = 1;
1410 wwin->client_flags.no_resizable = 1;
1411 wwin->client_flags.no_miniaturizable = 1;
1413 wwin->focus_mode = WFM_PASSIVE;
1414 wwin->client_win = window;
1415 wwin->screen_ptr = scr;
1416 wwin->transient_for = owner;
1417 wwin->client.x = x;
1418 wwin->client.y = y;
1419 wwin->client.width = width;
1420 wwin->client.height = height;
1421 wwin->frame_x = wwin->client.x;
1422 wwin->frame_y = wwin->client.y;
1424 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1425 foo |= WFF_TITLEBAR;
1426 #ifdef XKB_BUTTON_HINT
1427 foo |= WFF_LANGUAGE_BUTTON;
1428 #endif
1430 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1431 wwin->frame_x, wwin->frame_y,
1432 width, height,
1433 &wPreferences.window_title_clearance,
1434 &wPreferences.window_title_min_height,
1435 &wPreferences.window_title_max_height,
1436 foo,
1437 scr->window_title_texture,
1438 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1439 scr->w_depth, scr->w_visual, scr->w_colormap);
1441 XSaveContext(dpy, window, w_global.context.client_win, (XPointer) & wwin->client_descriptor);
1443 wwin->frame->flags.is_client_window_frame = 1;
1444 wwin->frame->flags.justification = wPreferences.title_justification;
1446 wFrameWindowChangeTitle(wwin->frame, title);
1448 /* setup button images */
1449 wWindowUpdateButtonImages(wwin);
1451 /* hide buttons */
1452 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1454 wwin->frame->child = wwin;
1455 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1457 #ifdef XKB_BUTTON_HINT
1458 if (wPreferences.modelock)
1459 wwin->frame->on_click_language = windowLanguageClick;
1460 #endif
1462 wwin->frame->on_click_right = windowCloseClick;
1463 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1464 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1465 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1466 wwin->client.y += wwin->frame->top_width;
1468 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1469 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1471 /* setup the frame descriptor */
1472 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1473 wwin->frame->core->descriptor.parent = wwin;
1474 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1476 XLowerWindow(dpy, window);
1477 XMapSubwindows(dpy, wwin->frame->core->window);
1479 /* setup stacking descriptor */
1480 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1481 WWindow *tmp;
1482 tmp = wWindowFor(wwin->transient_for);
1483 if (tmp)
1484 wwin->frame->core->stacking->child_of = tmp->frame->core;
1485 } else {
1486 wwin->frame->core->stacking->child_of = NULL;
1489 if (!scr->focused_window) {
1490 /* first window on the list */
1491 wwin->next = NULL;
1492 wwin->prev = NULL;
1493 scr->focused_window = wwin;
1494 } else {
1495 WWindow *tmp;
1497 /* add window at beginning of focus window list */
1498 tmp = scr->focused_window;
1499 while (tmp->prev)
1500 tmp = tmp->prev;
1501 tmp->prev = wwin;
1502 wwin->next = tmp;
1503 wwin->prev = NULL;
1506 if (wwin->flags.is_gnustep == 0)
1507 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1509 /* if (wPreferences.auto_focus) */
1510 wSetFocusTo(scr, wwin);
1511 wWindowResetMouseGrabs(wwin);
1512 wWindowSetKeyGrabs(wwin);
1514 return wwin;
1518 *----------------------------------------------------------------------
1519 * wUnmanageWindow--
1520 * Removes the frame window from a window and destroys all data
1521 * related to it. The window will be reparented back to the root window
1522 * if restore is True.
1524 * Side effects:
1525 * Everything related to the window is destroyed and the window
1526 * is removed from the window lists. Focus is set to the previous on the
1527 * window list.
1528 *----------------------------------------------------------------------
1530 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1532 WCoreWindow *frame = wwin->frame->core;
1533 WWindow *owner = NULL;
1534 WWindow *newFocusedWindow = NULL;
1535 int wasFocused;
1536 WScreen *scr = wwin->screen_ptr;
1538 /* First close attribute editor window if open */
1539 if (wwin->flags.inspector_open)
1540 wCloseInspectorForWindow(wwin);
1542 /* Close window menu if it's open for this window */
1543 if (wwin->flags.menu_open_for_me)
1544 CloseWindowMenu(scr);
1546 /* Don't restore focus to this window after a window exits
1547 * fullscreen mode */
1548 if (scr->bfs_focused_window == wwin)
1549 scr->bfs_focused_window = NULL;
1551 if (!destroyed) {
1552 if (!wwin->flags.internal_window)
1553 XRemoveFromSaveSet(dpy, wwin->client_win);
1555 /* If this is a leader window, we still need to listen for
1556 * DestroyNotify and PropertyNotify. */
1557 if (wApplicationOf(wwin->client_win))
1558 XSelectInput(dpy, wwin->client_win, StructureNotifyMask | PropertyChangeMask);
1559 else
1560 XSelectInput(dpy, wwin->client_win, NoEventMask);
1562 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1563 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1566 XUnmapWindow(dpy, frame->window);
1568 XUnmapWindow(dpy, wwin->client_win);
1570 /* deselect window */
1571 wSelectWindow(wwin, False);
1573 /* remove all pending events on window */
1574 /* I think this only matters for autoraise */
1575 if (wPreferences.raise_delay)
1576 WMDeleteTimerWithClientData(wwin->frame->core);
1578 XFlush(dpy);
1580 /* reparent the window back to the root */
1581 if (restore)
1582 wClientRestore(wwin);
1584 if (wwin->transient_for != scr->root_win) {
1585 owner = wWindowFor(wwin->transient_for);
1586 if (owner) {
1587 if (!owner->flags.semi_focused)
1588 owner = NULL;
1589 else
1590 owner->flags.semi_focused = 0;
1594 wasFocused = wwin->flags.focused;
1596 /* remove from window focus list */
1597 if (!wwin->prev && !wwin->next) {
1598 /* was the only window */
1599 scr->focused_window = NULL;
1600 newFocusedWindow = NULL;
1601 } else {
1602 WWindow *tmp;
1604 if (wwin->prev)
1605 wwin->prev->next = wwin->next;
1607 if (wwin->next)
1608 wwin->next->prev = wwin->prev;
1609 else {
1610 scr->focused_window = wwin->prev;
1611 scr->focused_window->next = NULL;
1614 if (wPreferences.focus_mode == WKF_CLICK) {
1616 /* if in click to focus mode and the window
1617 * was a transient, focus the owner window
1619 tmp = NULL;
1620 if (wPreferences.focus_mode == WKF_CLICK) {
1621 tmp = wWindowFor(wwin->transient_for);
1622 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1623 tmp = NULL;
1626 /* otherwise, focus the next one in the focus list */
1627 if (!tmp) {
1628 tmp = scr->focused_window;
1629 while (tmp) { /* look for one in the window list first */
1630 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1631 && (tmp->flags.mapped || tmp->flags.shaded))
1632 break;
1633 tmp = tmp->prev;
1635 if (!tmp) { /* if unsuccessful, choose any focusable window */
1636 tmp = scr->focused_window;
1637 while (tmp) {
1638 if (!WFLAGP(tmp, no_focusable)
1639 && (tmp->flags.mapped || tmp->flags.shaded))
1640 break;
1641 tmp = tmp->prev;
1646 newFocusedWindow = tmp;
1648 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1649 unsigned int mask;
1650 int foo;
1651 Window bar, win;
1653 /* This is to let the root window get the keyboard input
1654 * if Sloppy focus mode and no other window get focus.
1655 * This way keybindings will not freeze.
1657 tmp = NULL;
1658 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1659 tmp = wWindowFor(win);
1660 if (tmp == wwin)
1661 tmp = NULL;
1662 newFocusedWindow = tmp;
1663 } else {
1664 newFocusedWindow = NULL;
1668 if (!wwin->flags.internal_window)
1669 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1670 if (wasFocused) {
1671 if (newFocusedWindow != owner && owner) {
1672 if (wwin->flags.is_gnustep == 0)
1673 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1675 wSetFocusTo(scr, newFocusedWindow);
1678 /* Close menu and unhighlight */
1679 WApplication *oapp = wApplicationOf(wwin->main_window);
1680 WApplication *napp = scr->focused_window ? wApplicationOf(scr->focused_window->main_window) : NULL;
1681 if (oapp && oapp != napp) {
1682 wAppMenuUnmap(oapp->menu);
1683 if (wPreferences.highlight_active_app)
1684 wApplicationDeactivate(oapp);
1687 wNETCleanupFrameExtents(wwin);
1689 wWindowDestroy(wwin);
1690 XFlush(dpy);
1693 void wWindowMap(WWindow *wwin)
1695 XMapWindow(dpy, wwin->frame->core->window);
1696 if (!wwin->flags.shaded) {
1697 /* window will be remapped when getting MapNotify */
1698 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1699 XMapWindow(dpy, wwin->client_win);
1700 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1702 wwin->flags.mapped = 1;
1706 void wWindowUnmap(WWindow *wwin)
1708 wwin->flags.mapped = 0;
1710 /* prevent window withdrawal when getting UnmapNotify */
1711 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1712 XUnmapWindow(dpy, wwin->client_win);
1713 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1715 XUnmapWindow(dpy, wwin->frame->core->window);
1718 void wWindowSingleFocus(WWindow *wwin)
1720 WScreen *scr;
1721 int x, y, move = 0;
1723 if (!wwin)
1724 return;
1726 scr = wwin->screen_ptr;
1727 wMakeWindowVisible(wwin);
1729 x = wwin->frame_x;
1730 y = wwin->frame_y;
1732 /* bring window back to visible area */
1733 move = wScreenBringInside(scr, &x, &y, wwin->frame->core->width, wwin->frame->core->height);
1735 if (move) {
1736 wWindowConfigure(wwin, x, y, wwin->client.width, wwin->client.height);
1740 void wWindowFocusPrev(WWindow *wwin, Bool inSameWorkspace)
1742 WWindow *tmp;
1744 if (!wwin || !wwin->prev)
1745 return;
1747 tmp = wwin;
1748 while (tmp->prev)
1749 tmp = tmp->prev;
1751 if (inSameWorkspace)
1752 while (tmp && (tmp->frame->workspace != wwin->frame->workspace))
1753 tmp = tmp->next;
1755 wWindowSingleFocus(tmp);
1758 void wWindowFocusNext(WWindow *wwin, Bool inSameWorkspace)
1760 WWindow *tmp;
1762 if (!wwin || !wwin->prev)
1763 return;
1765 tmp = wwin->prev;
1766 if (inSameWorkspace)
1767 while (tmp && (tmp->frame->workspace != wwin->frame->workspace))
1768 tmp = tmp->prev;
1770 wWindowSingleFocus(tmp);
1773 void wWindowFocus(WWindow *wwin, WWindow *owin)
1775 WWindow *nowner;
1776 WWindow *oowner;
1778 #ifdef KEEP_XKB_LOCK_STATUS
1779 if (wPreferences.modelock)
1780 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1781 #endif /* KEEP_XKB_LOCK_STATUS */
1783 wwin->flags.semi_focused = 0;
1785 if (wwin->flags.is_gnustep == 0)
1786 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1788 wwin->flags.focused = 1;
1790 wWindowResetMouseGrabs(wwin);
1792 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1794 if (owin == wwin || !owin)
1795 return;
1797 nowner = wWindowFor(wwin->transient_for);
1799 /* new window is a transient for the old window */
1800 if (nowner == owin) {
1801 owin->flags.semi_focused = 1;
1802 wWindowUnfocus(nowner);
1803 return;
1806 oowner = wWindowFor(owin->transient_for);
1808 /* new window is owner of old window */
1809 if (wwin == oowner) {
1810 wWindowUnfocus(owin);
1811 return;
1814 if (!nowner) {
1815 wWindowUnfocus(owin);
1816 return;
1819 /* new window has same owner of old window */
1820 if (oowner == nowner) {
1821 /* prevent unfocusing of owner */
1822 oowner->flags.semi_focused = 0;
1823 wWindowUnfocus(owin);
1824 oowner->flags.semi_focused = 1;
1826 return;
1829 /* nowner != NULL && oowner != nowner */
1830 nowner->flags.semi_focused = 1;
1831 wWindowUnfocus(nowner);
1832 wWindowUnfocus(owin);
1835 void wWindowUnfocus(WWindow *wwin)
1837 CloseWindowMenu(wwin->screen_ptr);
1839 if (wwin->flags.is_gnustep == 0)
1840 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1842 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1843 WWindow *owner;
1844 owner = wWindowFor(wwin->transient_for);
1845 if (owner && owner->flags.semi_focused) {
1846 owner->flags.semi_focused = 0;
1847 if (owner->flags.mapped || owner->flags.shaded) {
1848 wWindowUnfocus(owner);
1849 wFrameWindowPaint(owner->frame);
1853 wwin->flags.focused = 0;
1854 wWindowResetMouseGrabs(wwin);
1855 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1858 void wWindowUpdateName(WWindow *wwin, const char *newTitle)
1860 const char *title;
1862 if (!wwin->frame)
1863 return;
1865 if (!newTitle)
1866 title = DEF_WINDOW_TITLE; /* the hint was removed */
1867 else
1868 title = newTitle;
1870 if (wFrameWindowChangeTitle(wwin->frame, title))
1871 WMPostNotificationName(WMNChangedName, wwin, NULL);
1875 *----------------------------------------------------------------------
1877 * wWindowConstrainSize--
1878 * Constrains size for the client window, taking the maximal size,
1879 * window resize increments and other size hints into account.
1881 * Returns:
1882 * The closest size to what was given that the client window can
1883 * have.
1885 *----------------------------------------------------------------------
1887 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1889 int width = (int)*nwidth;
1890 int height = (int)*nheight;
1891 int winc = 1;
1892 int hinc = 1;
1893 int minW = 1, minH = 1;
1894 int maxW = wwin->screen_ptr->scr_width * 2;
1895 int maxH = wwin->screen_ptr->scr_height * 2;
1896 int minAX = -1, minAY = -1;
1897 int maxAX = -1, maxAY = -1;
1898 int baseW = 0;
1899 int baseH = 0;
1901 if (wwin->normal_hints) {
1902 if (!wwin->flags.maximized) {
1903 winc = wwin->normal_hints->width_inc;
1904 hinc = wwin->normal_hints->height_inc;
1906 minW = wwin->normal_hints->min_width;
1907 minH = wwin->normal_hints->min_height;
1908 maxW = wwin->normal_hints->max_width;
1909 maxH = wwin->normal_hints->max_height;
1910 if (wwin->normal_hints->flags & PAspect) {
1911 minAX = wwin->normal_hints->min_aspect.x;
1912 minAY = wwin->normal_hints->min_aspect.y;
1913 maxAX = wwin->normal_hints->max_aspect.x;
1914 maxAY = wwin->normal_hints->max_aspect.y;
1917 baseW = wwin->normal_hints->base_width;
1918 baseH = wwin->normal_hints->base_height;
1921 if (width < minW)
1922 width = minW;
1923 if (height < minH)
1924 height = minH;
1926 if (width > maxW)
1927 width = maxW;
1928 if (height > maxH)
1929 height = maxH;
1931 /* aspect ratio code borrowed from olwm */
1932 if (minAX > 0) {
1933 /* adjust max aspect ratio */
1934 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1935 if (maxAX > maxAY) {
1936 height = (width * maxAY) / maxAX;
1937 if (height > maxH) {
1938 height = maxH;
1939 width = (height * maxAX) / maxAY;
1941 } else {
1942 width = (height * maxAX) / maxAY;
1943 if (width > maxW) {
1944 width = maxW;
1945 height = (width * maxAY) / maxAX;
1950 /* adjust min aspect ratio */
1951 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1952 if (minAX > minAY) {
1953 height = (width * minAY) / minAX;
1954 if (height < minH) {
1955 height = minH;
1956 width = (height * minAX) / minAY;
1958 } else {
1959 width = (height * minAX) / minAY;
1960 if (width < minW) {
1961 width = minW;
1962 height = (width * minAY) / minAX;
1968 if (!wwin->flags.maximized) {
1969 if (baseW != 0)
1970 width = (((width - baseW) / winc) * winc) + baseW;
1971 else
1972 width = (((width - minW) / winc) * winc) + minW;
1974 if (baseH != 0)
1975 height = (((height - baseH) / hinc) * hinc) + baseH;
1976 else
1977 height = (((height - minH) / hinc) * hinc) + minH;
1980 /* broken stupid apps may cause preposterous values for these.. */
1981 if (width > 0)
1982 *nwidth = width;
1983 if (height > 0)
1984 *nheight = height;
1987 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1988 unsigned int *width, unsigned int *height)
1990 int baseW = 0, baseH = 0;
1991 int winc = 1, hinc = 1;
1993 if (wwin->normal_hints) {
1994 baseW = wwin->normal_hints->base_width;
1995 baseH = wwin->normal_hints->base_height;
1997 winc = wwin->normal_hints->width_inc;
1998 hinc = wwin->normal_hints->height_inc;
2001 if (*width > maxW)
2002 *width = maxW - (maxW - baseW) % winc;
2004 if (*height > maxH)
2005 *height = maxH - (maxH - baseH) % hinc;
2008 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
2010 WScreen *scr = wwin->screen_ptr;
2011 WApplication *wapp;
2012 int unmap = 0;
2014 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
2015 return;
2017 if (workspace != scr->current_workspace) {
2018 /* Sent to other workspace. Unmap window */
2019 if ((wwin->flags.mapped
2020 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
2021 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
2023 wapp = wApplicationOf(wwin->main_window);
2024 if (wapp)
2025 wapp->last_workspace = workspace;
2027 if (wwin->flags.miniaturized) {
2028 if (wwin->icon) {
2029 XUnmapWindow(dpy, wwin->icon->core->window);
2030 wwin->icon->mapped = 0;
2032 } else {
2033 unmap = 1;
2034 wSetFocusTo(scr, NULL);
2037 } else {
2038 /* brought to current workspace. Map window */
2039 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
2040 if (wwin->icon) {
2041 XMapWindow(dpy, wwin->icon->core->window);
2042 wwin->icon->mapped = 1;
2044 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
2045 wWindowMap(wwin);
2048 if (!IS_OMNIPRESENT(wwin)) {
2049 int oldWorkspace = wwin->frame->workspace;
2050 wwin->frame->workspace = workspace;
2051 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
2054 if (unmap)
2055 wWindowUnmap(wwin);
2058 void wWindowChangeWorkspaceRelative(WWindow *wwin, int amount)
2060 WScreen *scr = wwin->screen_ptr;
2061 int w = scr->current_workspace + amount;
2063 if (amount < 0) {
2064 if (w >= 0) {
2065 wWindowChangeWorkspace(wwin, w);
2066 } else if (wPreferences.ws_cycle) {
2067 wWindowChangeWorkspace(wwin, scr->workspace_count + w);
2069 } else if (amount > 0) {
2070 if (w < scr->workspace_count) {
2071 wWindowChangeWorkspace(wwin, w);
2072 } else if (wPreferences.ws_advance) {
2073 int workspace = WMIN(w, MAX_WORKSPACES - 1);
2074 wWorkspaceMake(scr, workspace);
2075 wWindowChangeWorkspace(wwin, workspace);
2076 } else if (wPreferences.ws_cycle) {
2077 wWindowChangeWorkspace(wwin, w % scr->workspace_count);
2082 void wWindowSynthConfigureNotify(WWindow *wwin)
2084 XEvent sevent;
2086 sevent.type = ConfigureNotify;
2087 sevent.xconfigure.display = dpy;
2088 sevent.xconfigure.event = wwin->client_win;
2089 sevent.xconfigure.window = wwin->client_win;
2091 sevent.xconfigure.x = wwin->client.x;
2092 sevent.xconfigure.y = wwin->client.y;
2093 sevent.xconfigure.width = wwin->client.width;
2094 sevent.xconfigure.height = wwin->client.height;
2096 sevent.xconfigure.border_width = wwin->old_border_width;
2097 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
2098 sevent.xconfigure.above = wwin->frame->titlebar->window;
2099 else
2100 sevent.xconfigure.above = None;
2102 sevent.xconfigure.override_redirect = False;
2103 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2104 XFlush(dpy);
2108 *----------------------------------------------------------------------
2109 * wWindowConfigure()
2111 * req_x, req_y: new requested positions for the frame
2112 * req_width, req_height: new requested sizes for the client
2114 * Configures the frame, decorations and client window to the specified
2115 * geometry, whose validity is not checked -- wWindowConstrainSize()
2116 * must be used for that.
2117 * The size parameters are for the client window, but the position is
2118 * for the frame.
2119 * The client window receives a ConfigureNotify event, according
2120 * to what ICCCM says.
2122 * Returns:
2123 * None
2125 * Side effects:
2126 * Window size and position are changed and client window receives
2127 * a ConfigureNotify event.
2128 *----------------------------------------------------------------------
2130 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2132 int synth_notify = False;
2133 int resize;
2135 /* if window size is guaranteed to fail - fix it to some reasonable
2136 * defaults */
2137 if (req_height > SHRT_MAX)
2138 req_height = 480;
2140 if (req_width > SHRT_MAX)
2141 req_height = 640;
2143 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2145 * if the window is being moved but not resized then
2146 * send a synthetic ConfigureNotify
2148 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize)
2149 synth_notify = True;
2151 if (WFLAGP(wwin, dont_move_off))
2152 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2154 if (resize) {
2155 if (req_width < MIN_WINDOW_SIZE)
2156 req_width = MIN_WINDOW_SIZE;
2157 if (req_height < MIN_WINDOW_SIZE)
2158 req_height = MIN_WINDOW_SIZE;
2160 /* If growing, resize inner part before frame,
2161 * if shrinking, resize frame before.
2162 * This will prevent the frame (that can have a different color)
2163 * to be exposed, causing flicker */
2164 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2165 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2167 if (wwin->flags.shaded) {
2168 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2169 wwin->old_geometry.height = req_height;
2170 } else {
2171 int h;
2173 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2175 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2178 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2179 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2181 wwin->client.x = req_x;
2182 wwin->client.y = req_y + wwin->frame->top_width;
2183 wwin->client.width = req_width;
2184 wwin->client.height = req_height;
2185 } else {
2186 wwin->client.x = req_x;
2187 wwin->client.y = req_y + wwin->frame->top_width;
2189 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2191 wwin->frame_x = req_x;
2192 wwin->frame_y = req_y;
2193 if (HAS_BORDER(wwin)) {
2194 wwin->client.x += wwin->screen_ptr->frame_border_width;
2195 wwin->client.y += wwin->screen_ptr->frame_border_width;
2197 #ifdef USE_XSHAPE
2198 if (w_global.xext.shape.supported && wwin->flags.shaped && resize)
2199 wWindowSetShape(wwin);
2200 #endif
2202 if (synth_notify)
2203 wWindowSynthConfigureNotify(wwin);
2205 wNETFrameExtents(wwin);
2207 XFlush(dpy);
2210 /* req_x, req_y: new position of the frame */
2211 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2213 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2214 int synth_notify = False;
2216 /* Send a synthetic ConfigureNotify event for every window movement. */
2217 if ((req_x != wwin->frame_x || req_y != wwin->frame_y))
2218 synth_notify = True;
2219 #else
2220 /* A single synthetic ConfigureNotify event is sent at the end of
2221 * a completed (opaque) movement in moveres.c */
2222 #endif
2224 if (WFLAGP(wwin, dont_move_off))
2225 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2226 wwin->frame->core->width, wwin->frame->core->height);
2228 wwin->client.x = req_x;
2229 wwin->client.y = req_y + wwin->frame->top_width;
2230 if (HAS_BORDER(wwin)) {
2231 wwin->client.x += wwin->screen_ptr->frame_border_width;
2232 wwin->client.y += wwin->screen_ptr->frame_border_width;
2235 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2237 wwin->frame_x = req_x;
2238 wwin->frame_y = req_y;
2240 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2241 if (synth_notify)
2242 wWindowSynthConfigureNotify(wwin);
2243 #endif
2246 void wWindowUpdateButtonImages(WWindow *wwin)
2248 WScreen *scr = wwin->screen_ptr;
2249 Pixmap pixmap, mask;
2250 WFrameWindow *fwin = wwin->frame;
2252 if (!HAS_TITLEBAR(wwin))
2253 return;
2255 /* miniaturize button */
2256 if (!WFLAGP(wwin, no_miniaturize_button)) {
2257 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2258 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2260 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr)
2261 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2262 else
2263 mask = None;
2265 if (fwin->lbutton_image
2266 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2267 wPixmapDestroy(fwin->lbutton_image);
2268 fwin->lbutton_image = NULL;
2271 if (!fwin->lbutton_image) {
2272 fwin->lbutton_image = wPixmapCreate(pixmap, mask);
2273 fwin->lbutton_image->client_owned = 1;
2274 fwin->lbutton_image->client_owned_mask = 1;
2276 } else {
2277 if (fwin->lbutton_image && !fwin->lbutton_image->shared)
2278 wPixmapDestroy(fwin->lbutton_image);
2280 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2283 #ifdef XKB_BUTTON_HINT
2284 if (!WFLAGP(wwin, no_language_button)) {
2285 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared)
2286 wPixmapDestroy(fwin->languagebutton_image);
2288 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2290 #endif
2292 /* close button */
2294 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2295 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2297 if (!WFLAGP(wwin, no_close_button)) {
2298 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2299 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2301 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2302 mask = wwin->wm_gnustep_attr->close_mask;
2303 else
2304 mask = None;
2306 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2307 || fwin->rbutton_image->mask != mask)) {
2308 wPixmapDestroy(fwin->rbutton_image);
2309 fwin->rbutton_image = NULL;
2312 if (!fwin->rbutton_image) {
2313 fwin->rbutton_image = wPixmapCreate(pixmap, mask);
2314 fwin->rbutton_image->client_owned = 1;
2315 fwin->rbutton_image->client_owned_mask = 1;
2318 } else if (WFLAGP(wwin, kill_close)) {
2319 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2320 wPixmapDestroy(fwin->rbutton_image);
2322 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2324 } else if (MGFLAGP(wwin, broken_close)) {
2325 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2326 wPixmapDestroy(fwin->rbutton_image);
2328 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2330 } else {
2331 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2332 wPixmapDestroy(fwin->rbutton_image);
2334 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2338 /* force buttons to be redrawn */
2339 fwin->flags.need_texture_change = 1;
2340 wFrameWindowPaint(fwin);
2344 *---------------------------------------------------------------------------
2345 * wWindowConfigureBorders--
2346 * Update window border configuration according to attribute flags.
2348 *---------------------------------------------------------------------------
2350 void wWindowConfigureBorders(WWindow *wwin)
2352 if (wwin->frame) {
2353 int flags;
2354 int newy, oldh;
2356 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2358 #ifdef XKB_BUTTON_HINT
2359 if (wPreferences.modelock)
2360 flags |= WFF_LANGUAGE_BUTTON;
2361 #endif
2363 if (HAS_TITLEBAR(wwin))
2364 flags |= WFF_TITLEBAR;
2365 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2366 flags |= WFF_RESIZEBAR;
2367 if (HAS_BORDER(wwin))
2368 flags |= WFF_BORDER;
2369 if (wwin->flags.shaded)
2370 flags |= WFF_IS_SHADED;
2371 if (wwin->flags.selected)
2372 flags |= WFF_SELECTED;
2374 oldh = wwin->frame->top_width;
2375 wFrameWindowUpdateBorders(wwin->frame, flags);
2376 if (oldh != wwin->frame->top_width) {
2377 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2379 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2380 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2383 flags = 0;
2384 if (!WFLAGP(wwin, no_miniaturize_button)
2385 && wwin->frame->flags.hide_left_button)
2386 flags |= WFF_LEFT_BUTTON;
2388 #ifdef XKB_BUTTON_HINT
2389 if (!WFLAGP(wwin, no_language_button)
2390 && wwin->frame->flags.hide_language_button)
2391 flags |= WFF_LANGUAGE_BUTTON;
2392 #endif
2394 if (!WFLAGP(wwin, no_close_button)
2395 && wwin->frame->flags.hide_right_button)
2396 flags |= WFF_RIGHT_BUTTON;
2398 if (flags != 0) {
2399 wWindowUpdateButtonImages(wwin);
2400 wFrameWindowShowButton(wwin->frame, flags);
2403 flags = 0;
2404 if (WFLAGP(wwin, no_miniaturize_button)
2405 && !wwin->frame->flags.hide_left_button)
2406 flags |= WFF_LEFT_BUTTON;
2408 #ifdef XKB_BUTTON_HINT
2409 if (WFLAGP(wwin, no_language_button)
2410 && !wwin->frame->flags.hide_language_button)
2411 flags |= WFF_LANGUAGE_BUTTON;
2412 #endif
2414 if (WFLAGP(wwin, no_close_button)
2415 && !wwin->frame->flags.hide_right_button)
2416 flags |= WFF_RIGHT_BUTTON;
2418 if (flags != 0)
2419 wFrameWindowHideButton(wwin->frame, flags);
2421 #ifdef USE_XSHAPE
2422 if (w_global.xext.shape.supported && wwin->flags.shaped)
2423 wWindowSetShape(wwin);
2424 #endif
2428 void wWindowSaveState(WWindow *wwin)
2430 long data[10];
2431 int i;
2433 memset(data, 0, sizeof(long) * 10);
2434 data[0] = wwin->frame->workspace;
2435 data[1] = wwin->flags.miniaturized;
2436 data[2] = wwin->flags.shaded;
2437 data[3] = wwin->flags.hidden;
2438 data[4] = wwin->flags.maximized;
2439 if (wwin->flags.maximized == 0) {
2440 data[5] = wwin->frame_x;
2441 data[6] = wwin->frame_y;
2442 data[7] = wwin->frame->core->width;
2443 data[8] = wwin->frame->core->height;
2444 } else {
2445 data[5] = wwin->old_geometry.x;
2446 data[6] = wwin->old_geometry.y;
2447 data[7] = wwin->old_geometry.width;
2448 data[8] = wwin->old_geometry.height;
2451 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2452 if (wwin->screen_ptr->shortcutWindows[i] &&
2453 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2454 data[9] |= 1 << i;
2457 XChangeProperty(dpy, wwin->client_win, w_global.atom.wmaker.state,
2458 w_global.atom.wmaker.state, 32, PropModeReplace, (unsigned char *)data, 10);
2461 static int getSavedState(Window window, WSavedState ** state)
2463 Atom type_ret;
2464 int fmt_ret;
2465 unsigned long nitems_ret;
2466 unsigned long bytes_after_ret;
2467 long *data;
2469 if (XGetWindowProperty(dpy, window, w_global.atom.wmaker.state, 0, 10,
2470 True, w_global.atom.wmaker.state,
2471 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2472 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2473 return 0;
2475 if (type_ret != w_global.atom.wmaker.state) {
2476 XFree(data);
2477 return 0;
2480 *state = wmalloc(sizeof(WSavedState));
2482 (*state)->workspace = data[0];
2483 (*state)->miniaturized = data[1];
2484 (*state)->shaded = data[2];
2485 (*state)->hidden = data[3];
2486 (*state)->maximized = data[4];
2487 (*state)->x = data[5];
2488 (*state)->y = data[6];
2489 (*state)->w = data[7];
2490 (*state)->h = data[8];
2491 (*state)->window_shortcuts = data[9];
2493 XFree(data);
2495 return 1;
2498 #ifdef USE_XSHAPE
2499 void wWindowClearShape(WWindow * wwin)
2501 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2502 0, wwin->frame->top_width, None, ShapeSet);
2503 XFlush(dpy);
2506 void wWindowSetShape(WWindow * wwin)
2508 XRectangle rect[2];
2509 int count;
2510 #ifdef OPTIMIZE_SHAPE
2511 XRectangle *rects;
2512 XRectangle *urec;
2513 int ordering;
2515 /* only shape is the client's */
2516 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin))
2517 goto alt_code;
2519 /* Get array of rectangles describing the shape mask */
2520 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2521 if (!rects)
2522 goto alt_code;
2524 urec = malloc(sizeof(XRectangle) * (count + 2));
2525 if (!urec) {
2526 XFree(rects);
2527 goto alt_code;
2530 /* insert our decoration rectangles in the rect list */
2531 memcpy(urec, rects, sizeof(XRectangle) * count);
2532 XFree(rects);
2534 if (HAS_TITLEBAR(wwin)) {
2535 urec[count].x = -1;
2536 urec[count].y = -1 - wwin->frame->top_width;
2537 urec[count].width = wwin->frame->core->width + 2;
2538 urec[count].height = wwin->frame->top_width + 1;
2539 count++;
2541 if (HAS_RESIZEBAR(wwin)) {
2542 urec[count].x = -1;
2543 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2544 urec[count].width = wwin->frame->core->width + 2;
2545 urec[count].height = wwin->frame->bottom_width + 1;
2546 count++;
2549 /* shape our frame window */
2550 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2551 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2552 XFlush(dpy);
2553 wfree(urec);
2554 return;
2556 alt_code:
2557 #endif /* OPTIMIZE_SHAPE */
2558 count = 0;
2559 if (HAS_TITLEBAR(wwin)) {
2560 rect[count].x = -1;
2561 rect[count].y = -1;
2562 rect[count].width = wwin->frame->core->width + 2;
2563 rect[count].height = wwin->frame->top_width + 1;
2564 count++;
2566 if (HAS_RESIZEBAR(wwin)) {
2567 rect[count].x = -1;
2568 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2569 rect[count].width = wwin->frame->core->width + 2;
2570 rect[count].height = wwin->frame->bottom_width + 1;
2571 count++;
2573 if (count > 0) {
2574 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2575 0, 0, rect, count, ShapeSet, Unsorted);
2577 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2578 0, wwin->frame->top_width, wwin->client_win,
2579 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2580 XFlush(dpy);
2582 #endif /* USE_XSHAPE */
2584 /* ====================================================================== */
2586 static FocusMode getFocusMode(WWindow * wwin)
2588 FocusMode mode;
2590 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2591 if (wwin->wm_hints->input == True) {
2592 if (wwin->protocols.TAKE_FOCUS)
2593 mode = WFM_LOCALLY_ACTIVE;
2594 else
2595 mode = WFM_PASSIVE;
2596 } else {
2597 if (wwin->protocols.TAKE_FOCUS)
2598 mode = WFM_GLOBALLY_ACTIVE;
2599 else
2600 mode = WFM_NO_INPUT;
2602 } else {
2603 mode = WFM_PASSIVE;
2605 return mode;
2608 void wWindowSetKeyGrabs(WWindow * wwin)
2610 int i;
2611 WShortKey *key;
2613 for (i = 0; i < WKBD_LAST; i++) {
2614 key = &wKeyBindings[i];
2616 if (key->keycode == 0)
2617 continue;
2618 if (key->modifier != AnyModifier) {
2619 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2620 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2621 #ifdef NUMLOCK_HACK
2622 /* Also grab all modifier combinations possible that include,
2623 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2624 * work even if the NumLock/ScrollLock key is on.
2626 wHackedGrabKey(key->keycode, key->modifier,
2627 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2628 #endif
2630 XGrabKey(dpy, key->keycode, key->modifier,
2631 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2634 wRootMenuBindShortcuts(wwin->frame->core->window);
2637 void wWindowResetMouseGrabs(WWindow * wwin)
2639 /* Mouse grabs can't be done on the client window because of
2640 * ICCCM and because clients that try to do the same will crash.
2642 * But there is a problem which makes tbar buttons of unfocused
2643 * windows not usable as the click goes to the frame window instead
2644 * of the button itself. Must figure a way to fix that.
2647 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2649 if (!WFLAGP(wwin, no_bind_mouse)) {
2650 /* grabs for Meta+drag */
2651 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2652 True, ButtonPressMask | ButtonReleaseMask,
2653 GrabModeSync, GrabModeAsync, None, None);
2655 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2656 * but we only grab it for Button4 and Button 5 since a lot of apps
2657 * use CTRL+Button1-3 for app related functionality
2659 if (wPreferences.resize_increment > 0) {
2660 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2661 True, ButtonPressMask | ButtonReleaseMask,
2662 GrabModeSync, GrabModeAsync, None, None);
2663 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2664 True, ButtonPressMask | ButtonReleaseMask,
2665 GrabModeSync, GrabModeAsync, None, None);
2667 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2668 True, ButtonPressMask | ButtonReleaseMask,
2669 GrabModeSync, GrabModeAsync, None, None);
2670 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2671 True, ButtonPressMask | ButtonReleaseMask,
2672 GrabModeSync, GrabModeAsync, None, None);
2676 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2677 && !wwin->flags.is_gnustep) {
2678 /* the passive grabs to focus the window */
2679 /* if (wPreferences.focus_mode == WKF_CLICK) */
2680 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2681 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2683 XFlush(dpy);
2686 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2688 if (attr->flags & GSExtraFlagsAttr) {
2689 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2690 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2691 wWindowUpdateButtonImages(wwin);
2696 WMagicNumber wWindowAddSavedState(const char *instance, const char *class,
2697 const char *command, pid_t pid, WSavedState * state)
2699 WWindowState *wstate;
2701 wstate = malloc(sizeof(WWindowState));
2702 if (!wstate)
2703 return NULL;
2705 memset(wstate, 0, sizeof(WWindowState));
2706 wstate->pid = pid;
2707 if (instance)
2708 wstate->instance = wstrdup(instance);
2709 if (class)
2710 wstate->class = wstrdup(class);
2711 if (command)
2712 wstate->command = wstrdup(command);
2713 wstate->state = state;
2715 wstate->next = windowState;
2716 windowState = wstate;
2718 return wstate;
2721 static inline int is_same(const char *x, const char *y)
2723 if ((x == NULL) && (y == NULL))
2724 return 1;
2726 if ((x == NULL) || (y == NULL))
2727 return 0;
2729 if (strcmp(x, y) == 0)
2730 return 1;
2731 else
2732 return 0;
2735 WMagicNumber wWindowGetSavedState(Window win)
2737 char *instance, *class, *command = NULL;
2738 WWindowState *wstate = windowState;
2740 if (!wstate)
2741 return NULL;
2743 command = GetCommandForWindow(win);
2744 if (!command)
2745 return NULL;
2747 if (PropGetWMClass(win, &class, &instance)) {
2748 while (wstate) {
2749 if (is_same(instance, wstate->instance) &&
2750 is_same(class, wstate->class) &&
2751 is_same(command, wstate->command)) {
2752 break;
2754 wstate = wstate->next;
2756 } else {
2757 wstate = NULL;
2760 if (command)
2761 wfree(command);
2762 if (instance)
2763 free(instance);
2764 if (class)
2765 free(class);
2767 return wstate;
2770 void wWindowDeleteSavedState(WMagicNumber id)
2772 WWindowState *tmp, *wstate = (WWindowState *) id;
2774 if (!wstate || !windowState)
2775 return;
2777 tmp = windowState;
2778 if (tmp == wstate) {
2779 windowState = wstate->next;
2780 release_wwindowstate(wstate);
2781 } else {
2782 while (tmp->next) {
2783 if (tmp->next == wstate) {
2784 tmp->next = wstate->next;
2785 release_wwindowstate(wstate);
2786 break;
2788 tmp = tmp->next;
2793 void wWindowDeleteSavedStatesForPID(pid_t pid)
2795 WWindowState *tmp, *wstate;
2797 if (!windowState)
2798 return;
2800 tmp = windowState;
2801 if (tmp->pid == pid) {
2802 wstate = windowState;
2803 windowState = tmp->next;
2805 release_wwindowstate(wstate);
2806 } else {
2807 while (tmp->next) {
2808 if (tmp->next->pid == pid) {
2809 wstate = tmp->next;
2810 tmp->next = wstate->next;
2811 release_wwindowstate(wstate);
2812 break;
2814 tmp = tmp->next;
2819 static void release_wwindowstate(WWindowState *wstate)
2821 if (wstate->instance)
2822 wfree(wstate->instance);
2824 if (wstate->class)
2825 wfree(wstate->class);
2827 if (wstate->command)
2828 wfree(wstate->command);
2830 wfree(wstate->state);
2831 wfree(wstate);
2834 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2836 if (wwin->flags.omnipresent == flag)
2837 return;
2839 wwin->flags.omnipresent = flag;
2840 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2843 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2845 WWindow *wwin = data;
2847 /* Parameter not used, but tell the compiler that it is ok */
2848 (void) sender;
2850 #ifndef NUMLOCK_HACK
2851 if ((event->xbutton.state & ValidModMask)
2852 != (event->xbutton.state & ~LockMask)) {
2853 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2854 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2856 #endif
2858 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2860 CloseWindowMenu(wwin->screen_ptr);
2862 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2863 && !WFLAGP(wwin, no_focusable)) {
2864 wSetFocusTo(wwin->screen_ptr, wwin);
2867 if (event->xbutton.button == Button1)
2868 wRaiseFrame(wwin->frame->core);
2870 if (event->xbutton.window != wwin->frame->resizebar->window) {
2871 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2872 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2873 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2874 return;
2878 if (event->xbutton.state & MOD_MASK) {
2879 /* move the window */
2880 wMouseMoveWindow(wwin, event);
2881 XUngrabPointer(dpy, CurrentTime);
2882 } else {
2883 wMouseResizeWindow(wwin, event);
2884 XUngrabPointer(dpy, CurrentTime);
2888 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2890 WWindow *wwin = data;
2892 /* Parameter not used, but tell the compiler that it is ok */
2893 (void) sender;
2895 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2897 if (event->xbutton.button == Button1 ) {
2898 if (event->xbutton.state == 0 ) {
2899 if (!WFLAGP(wwin, no_shadeable) & !wPreferences.double_click_fullscreen) {
2900 /* shade window */
2901 if (wwin->flags.shaded)
2902 wUnshadeWindow(wwin);
2903 else
2904 wShadeWindow(wwin);
2908 if (wPreferences.double_click_fullscreen){
2909 int dir = 0;
2910 if (event->xbutton.state == 0) {
2911 /* maximize window full screen*/
2912 dir |= (MAX_VERTICAL|MAX_HORIZONTAL);
2913 int ndir = dir ^ wwin->flags.maximized;
2914 wMaximizeWindow(wwin, ndir, wGetHeadForWindow(wwin));
2919 } else {
2920 int dir = 0;
2922 if (event->xbutton.state & ControlMask)
2923 dir |= MAX_VERTICAL;
2925 if (event->xbutton.state & ShiftMask) {
2926 dir |= MAX_HORIZONTAL;
2927 if (!(event->xbutton.state & ControlMask))
2928 wSelectWindow(wwin, !wwin->flags.selected);
2931 /* maximize window */
2933 if (dir != 0 && IS_RESIZABLE(wwin)) {
2934 int ndir = dir ^ wwin->flags.maximized;
2936 if (ndir != 0)
2937 wMaximizeWindow(wwin, ndir, wGetHeadForWindow(wwin));
2938 else
2939 wUnmaximizeWindow(wwin);
2942 } else if (event->xbutton.button == Button3) {
2943 if (event->xbutton.state & MOD_MASK)
2944 wHideOtherApplications(wwin);
2945 } else if (event->xbutton.button == Button2) {
2946 wSelectWindow(wwin, !wwin->flags.selected);
2947 } else if (event->xbutton.button == W_getconf_mouseWheelUp()) {
2948 wShadeWindow(wwin);
2949 } else if (event->xbutton.button == W_getconf_mouseWheelDown()) {
2950 wUnshadeWindow(wwin);
2954 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2956 WWindow *wwin = desc->parent;
2957 unsigned int new_width, w_scale;
2958 unsigned int new_height, h_scale;
2959 unsigned int resize_width_increment = 0;
2960 unsigned int resize_height_increment = 0;
2962 if (wwin->normal_hints) {
2963 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2964 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2965 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2966 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2968 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2969 resize_width_increment = wPreferences.resize_increment;
2970 resize_height_increment = wPreferences.resize_increment;
2973 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2975 CloseWindowMenu(wwin->screen_ptr);
2977 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2978 wSetFocusTo(wwin->screen_ptr, wwin);
2980 if (event->xbutton.button == Button1)
2981 wRaiseFrame(wwin->frame->core);
2983 if (event->xbutton.state & ControlMask) {
2984 if (event->xbutton.button == Button4) {
2985 new_width = wwin->client.width - resize_width_increment;
2986 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2987 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2989 if (event->xbutton.button == Button5) {
2990 new_width = wwin->client.width + resize_width_increment;
2991 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2992 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2996 if (event->xbutton.state & MOD_MASK) {
2997 /* move the window */
2998 if (XGrabPointer(dpy, wwin->client_win, False,
2999 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
3000 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3001 return;
3003 if (event->xbutton.button == Button3) {
3004 wMouseResizeWindow(wwin, event);
3005 } else if (event->xbutton.button == Button4) {
3006 new_height = wwin->client.height - resize_height_increment;
3007 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
3008 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
3009 } else if (event->xbutton.button == Button5) {
3010 new_height = wwin->client.height + resize_height_increment;
3011 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
3012 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
3013 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
3014 wMouseMoveWindow(wwin, event);
3016 XUngrabPointer(dpy, CurrentTime);
3020 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
3022 WWindow *wwin = (WWindow *) data;
3024 /* Parameter not used, but tell the compiler that it is ok */
3025 (void) sender;
3027 #ifndef NUMLOCK_HACK
3028 if ((event->xbutton.state & ValidModMask) != (event->xbutton.state & ~LockMask))
3029 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
3030 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
3031 #endif
3032 event->xbutton.state &= w_global.shortcut.modifiers_mask;
3034 CloseWindowMenu(wwin->screen_ptr);
3036 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
3037 && !WFLAGP(wwin, no_focusable))
3038 wSetFocusTo(wwin->screen_ptr, wwin);
3040 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
3042 if (event->xbutton.button == Button1) {
3043 if (event->xbutton.state & MOD_MASK)
3044 wLowerFrame(wwin->frame->core);
3045 else
3046 wRaiseFrame(wwin->frame->core);
3048 if ((event->xbutton.state & ShiftMask)
3049 && !(event->xbutton.state & ControlMask)) {
3050 wSelectWindow(wwin, !wwin->flags.selected);
3051 return;
3053 if (event->xbutton.window != wwin->frame->titlebar->window
3054 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3055 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
3056 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3057 return;
3060 /* move the window */
3061 wMouseMoveWindow(wwin, event);
3063 XUngrabPointer(dpy, CurrentTime);
3064 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
3065 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
3066 WObjDescriptor *desc;
3068 if (event->xbutton.window != wwin->frame->titlebar->window
3069 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3070 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
3071 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3072 return;
3075 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
3077 /* allow drag select */
3078 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
3079 event->xany.send_event = True;
3080 (*desc->handle_mousedown) (desc, event);
3082 XUngrabPointer(dpy, CurrentTime);
3086 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
3088 WWindow *wwin = data;
3090 /* Parameter not used, but tell the compiler that it is ok */
3091 (void) sender;
3093 event->xbutton.state &= w_global.shortcut.modifiers_mask;
3095 CloseWindowMenu(wwin->screen_ptr);
3097 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3098 return;
3100 /* if control-click, kill the client */
3101 if (event->xbutton.state & ControlMask) {
3102 wClientKill(wwin);
3103 } else {
3104 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
3105 /* send delete message */
3106 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
3107 w_global.timestamp.last_event);
3112 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
3114 WWindow *wwin = data;
3116 /* Parameter not used, but tell the compiler that it is ok */
3117 (void) sender;
3119 CloseWindowMenu(wwin->screen_ptr);
3121 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3122 return;
3124 /* send delete message */
3125 if (wwin->protocols.DELETE_WINDOW)
3126 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
3127 w_global.timestamp.last_event);
3128 else
3129 wClientKill(wwin);
3132 #ifdef XKB_BUTTON_HINT
3133 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3135 WWindow *wwin = data;
3136 WFrameWindow *fwin = wwin->frame;
3137 WScreen *scr = fwin->screen_ptr;
3138 int tl;
3140 /* Parameter not used, but tell the compiler that it is ok */
3141 (void) sender;
3143 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3144 return;
3145 tl = wwin->frame->languagemode;
3146 wwin->frame->languagemode = wwin->frame->last_languagemode;
3147 wwin->frame->last_languagemode = tl;
3148 wSetFocusTo(scr, wwin);
3149 wwin->frame->languagebutton_image =
3150 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
3151 wFrameWindowUpdateLanguageButton(wwin->frame);
3152 if (event->xbutton.button == Button3)
3153 return;
3154 wRaiseFrame(fwin->core);
3156 #endif
3158 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3160 WWindow *wwin = data;
3162 /* Parameter not used, but tell the compiler that it is ok */
3163 (void) sender;
3165 event->xbutton.state &= w_global.shortcut.modifiers_mask;
3167 CloseWindowMenu(wwin->screen_ptr);
3169 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3170 return;
3172 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
3173 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window,
3174 w_global.timestamp.last_event);
3175 } else {
3176 WApplication *wapp;
3177 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
3179 wapp = wApplicationOf(wwin->main_window);
3180 if (wapp && !WFLAGP(wwin, no_appicon))
3181 wHideApplication(wapp);
3182 } else if (event->xbutton.state == 0) {
3183 wIconifyWindow(wwin);