Handle NULL pointer as good as possible (Coverity #50099)
[wmaker-crm.git] / src / actions.c
blob4e8774655bbad0e8925efa7cb2e9290b5977e7ce
1 /* action.c- misc. window commands (miniaturize, hide etc.)
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
7 * Copyright (c) 2014 Window Maker Team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <time.h>
34 #include "WindowMaker.h"
35 #include "framewin.h"
36 #include "window.h"
37 #include "client.h"
38 #include "icon.h"
39 #include "colormap.h"
40 #include "application.h"
41 #include "actions.h"
42 #include "stacking.h"
43 #include "appicon.h"
44 #include "dock.h"
45 #include "appmenu.h"
46 #include "winspector.h"
47 #include "workspace.h"
48 #include "xinerama.h"
49 #include "usermenu.h"
50 #include "placement.h"
51 #include "misc.h"
52 #include "event.h"
55 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
56 unsigned int *new_width, unsigned int *new_height);
57 static void save_old_geometry(WWindow *wwin, int directions);
59 /******* Local Variables *******/
60 static struct {
61 int steps;
62 int delay;
63 } shadePars[5] = {
64 { SHADE_STEPS_UF, SHADE_DELAY_UF },
65 { SHADE_STEPS_F, SHADE_DELAY_F },
66 { SHADE_STEPS_M, SHADE_DELAY_M },
67 { SHADE_STEPS_S, SHADE_DELAY_S },
68 { SHADE_STEPS_US, SHADE_DELAY_US }
71 #define UNSHADE 0
72 #define SHADE 1
73 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
74 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
76 static int compareTimes(Time t1, Time t2)
78 Time diff;
79 if (t1 == t2)
80 return 0;
81 diff = t1 - t2;
82 return (diff < 60000) ? 1 : -1;
85 #ifdef ANIMATIONS
86 static void shade_animate(WWindow *wwin, Bool what);
87 #else
88 static inline void shade_animate(WWindow *wwin, Bool what)
91 * This function is empty on purpose, so tell the compiler
92 * to not warn about parameters being not used
94 (void) wwin;
95 (void) what;
97 #endif
100 *----------------------------------------------------------------------
101 * wSetFocusTo--
102 * Changes the window focus to the one passed as argument.
103 * If the window to focus is not already focused, it will be brought
104 * to the head of the list of windows. Previously focused window is
105 * unfocused.
107 * Side effects:
108 * Window list may be reordered and the window focus is changed.
110 *----------------------------------------------------------------------
112 void wSetFocusTo(WScreen *scr, WWindow *wwin)
114 static WScreen *old_scr = NULL;
116 WWindow *old_focused;
117 WWindow *focused = scr->focused_window;
118 Time timestamp = w_global.timestamp.last_event;
119 WApplication *oapp = NULL, *napp = NULL;
120 int wasfocused;
122 if (scr->flags.ignore_focus_events || compareTimes(w_global.timestamp.focus_change, timestamp) > 0)
123 return;
125 if (!old_scr)
126 old_scr = scr;
128 old_focused = old_scr->focused_window;
130 w_global.timestamp.focus_change = timestamp;
132 if (old_focused)
133 oapp = wApplicationOf(old_focused->main_window);
135 if (wwin == NULL) {
136 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
137 if (old_focused)
138 wWindowUnfocus(old_focused);
140 if (oapp) {
141 wAppMenuUnmap(oapp->menu);
142 if (wPreferences.highlight_active_app)
143 wApplicationDeactivate(oapp);
146 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
147 return;
150 if (old_scr != scr && old_focused)
151 wWindowUnfocus(old_focused);
153 wasfocused = wwin->flags.focused;
154 napp = wApplicationOf(wwin->main_window);
156 /* remember last workspace where the app has been */
157 if (napp)
158 napp->last_workspace = wwin->frame->workspace;
160 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
161 /* install colormap if colormap mode is lock mode */
162 if (wPreferences.colormap_mode == WCM_CLICK)
163 wColormapInstallForWindow(scr, wwin);
165 /* set input focus */
166 switch (wwin->focus_mode) {
167 case WFM_NO_INPUT:
168 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
169 break;
170 case WFM_PASSIVE:
171 case WFM_LOCALLY_ACTIVE:
172 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
173 break;
174 case WFM_GLOBALLY_ACTIVE:
175 break;
178 XFlush(dpy);
179 if (wwin->protocols.TAKE_FOCUS)
180 wClientSendProtocol(wwin, w_global.atom.wm.take_focus, timestamp);
182 XSync(dpy, False);
183 } else {
184 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
187 if (WFLAGP(wwin, no_focusable))
188 return;
190 /* if this is not the focused window focus it */
191 if (focused != wwin) {
192 /* change the focus window list order */
193 if (wwin->prev)
194 wwin->prev->next = wwin->next;
196 if (wwin->next)
197 wwin->next->prev = wwin->prev;
199 wwin->prev = focused;
200 focused->next = wwin;
201 wwin->next = NULL;
202 scr->focused_window = wwin;
204 if (oapp && oapp != napp) {
205 wAppMenuUnmap(oapp->menu);
206 if (wPreferences.highlight_active_app)
207 wApplicationDeactivate(oapp);
211 wWindowFocus(wwin, focused);
213 if (napp && !wasfocused) {
214 #ifdef USER_MENU
215 wUserMenuRefreshInstances(napp->menu, wwin);
216 #endif /* USER_MENU */
218 if (wwin->flags.mapped)
219 wAppMenuMap(napp->menu, wwin);
221 if (napp && wPreferences.highlight_active_app)
222 wApplicationActivate(napp);
224 XFlush(dpy);
225 old_scr = scr;
228 void wShadeWindow(WWindow *wwin)
231 if (wwin->flags.shaded)
232 return;
234 XLowerWindow(dpy, wwin->client_win);
235 shade_animate(wwin, SHADE);
237 wwin->flags.skip_next_animation = 0;
238 wwin->flags.shaded = 1;
239 wwin->flags.mapped = 0;
240 /* prevent window withdrawal when getting UnmapNotify */
241 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
242 XUnmapWindow(dpy, wwin->client_win);
243 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
245 /* for the client it's just like iconification */
246 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
248 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
249 wWindowSynthConfigureNotify(wwin);
252 wClientSetState(wwin, IconicState, None);
255 WMPostNotificationName(WMNChangedState, wwin, "shade");
257 #ifdef ANIMATIONS
258 if (!wwin->screen_ptr->flags.startup) {
259 /* Catch up with events not processed while animation was running */
260 ProcessPendingEvents();
262 #endif
265 void wUnshadeWindow(WWindow *wwin)
268 if (!wwin->flags.shaded)
269 return;
271 wwin->flags.shaded = 0;
272 wwin->flags.mapped = 1;
273 XMapWindow(dpy, wwin->client_win);
275 shade_animate(wwin, UNSHADE);
277 wwin->flags.skip_next_animation = 0;
278 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
279 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
281 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
282 wWindowSynthConfigureNotify(wwin);
284 /* if the window is focused, set the focus again as it was disabled during
285 * shading */
286 if (wwin->flags.focused)
287 wSetFocusTo(wwin->screen_ptr, wwin);
289 WMPostNotificationName(WMNChangedState, wwin, "shade");
292 /* Set the old coordinates using the current values */
293 static void save_old_geometry(WWindow *wwin, int directions)
295 /* never been saved? */
296 if (!wwin->old_geometry.width)
297 directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
298 if (!wwin->old_geometry.height)
299 directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
301 if (directions & SAVE_GEOMETRY_X)
302 wwin->old_geometry.x = wwin->frame_x;
303 if (directions & SAVE_GEOMETRY_Y)
304 wwin->old_geometry.y = wwin->frame_y;
305 if (directions & SAVE_GEOMETRY_WIDTH)
306 wwin->old_geometry.width = wwin->client.width;
307 if (directions & SAVE_GEOMETRY_HEIGHT)
308 wwin->old_geometry.height = wwin->client.height;
311 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
313 WMRect old_geom_rect;
314 int old_head;
315 Bool same_head;
317 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
318 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
319 same_head = (wGetHeadForWindow(wwin) == old_head);
320 *x = ((wwin->old_geometry.x || wwin->old_geometry.width) && same_head) ? wwin->old_geometry.x : wwin->frame_x;
321 *y = ((wwin->old_geometry.y || wwin->old_geometry.height) && same_head) ? wwin->old_geometry.y : wwin->frame_y;
322 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
323 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
326 /* Remember geometry for unmaximizing */
327 void update_saved_geometry(WWindow *wwin)
329 /* NOT if we aren't already maximized
330 * we'll save geometry when maximizing */
331 if (!wwin->flags.maximized)
332 return;
334 /* NOT if we are fully maximized */
335 if ((wwin->flags.maximized & MAX_MAXIMUS) ||
336 ((wwin->flags.maximized & MAX_HORIZONTAL) &&
337 (wwin->flags.maximized & MAX_VERTICAL)))
338 return;
340 /* save the co-ordinate in the axis in which we AREN'T maximized */
341 if (wwin->flags.maximized & MAX_HORIZONTAL)
342 save_old_geometry(wwin, SAVE_GEOMETRY_Y);
343 if (wwin->flags.maximized & MAX_VERTICAL)
344 save_old_geometry(wwin, SAVE_GEOMETRY_X);
347 void wMaximizeWindow(WWindow *wwin, int directions)
349 unsigned int new_width, new_height, half_scr_width, half_scr_height;
350 int new_x = 0;
351 int new_y = 0;
352 int maximus_x = 0;
353 int maximus_y = 0;
354 unsigned int maximus_width = 0;
355 unsigned int maximus_height = 0;
356 WArea usableArea, totalArea;
357 Bool has_border = 1;
358 int adj_size;
359 WScreen *scr = wwin->screen_ptr;
361 if (!IS_RESIZABLE(wwin))
362 return;
364 if (!HAS_BORDER(wwin))
365 has_border = 0;
367 if (wPreferences.drag_maximized_window == DRAGMAX_NOMOVE)
368 wwin->client_flags.no_movable = 1;
370 /* the size to adjust the geometry */
371 adj_size = scr->frame_border_width * 2 * has_border;
373 /* save old coordinates before we change the current values */
374 if (!wwin->flags.maximized)
375 save_old_geometry(wwin, SAVE_GEOMETRY_ALL);
377 totalArea.x2 = scr->scr_width;
378 totalArea.y2 = scr->scr_height;
379 totalArea.x1 = 0;
380 totalArea.y1 = 0;
381 usableArea = totalArea;
383 if (!(directions & MAX_IGNORE_XINERAMA)) {
384 WScreen *scr = wwin->screen_ptr;
385 int head;
387 if (directions & MAX_KEYBOARD)
388 head = wGetHeadForWindow(wwin);
389 else
390 head = wGetHeadForPointerLocation(scr);
392 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
396 /* Only save directions, not kbd or xinerama hints */
397 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
399 if (WFLAGP(wwin, full_maximize))
400 usableArea = totalArea;
401 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
402 half_scr_height = (usableArea.y2 - usableArea.y1)/2;
404 if (wwin->flags.shaded) {
405 wwin->flags.skip_next_animation = 1;
406 wUnshadeWindow(wwin);
409 if (directions & MAX_MAXIMUS) {
410 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
411 new_width = maximus_width - adj_size;
412 new_height = maximus_height - adj_size;
413 new_x = maximus_x;
414 new_y = maximus_y;
415 if (WFLAGP(wwin, full_maximize) && (new_y == 0)) {
416 new_height += wwin->frame->bottom_width - 1;
417 new_y -= wwin->frame->top_width;
420 wwin->maximus_x = new_x;
421 wwin->maximus_y = new_y;
422 wwin->flags.old_maximized |= MAX_MAXIMUS;
423 } else {
424 /* set default values if no option set then */
425 if (!(directions & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS))) {
426 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
427 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
429 if (!(directions & (MAX_VERTICAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS))) {
430 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
431 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
434 /* left|right position */
435 if (directions & MAX_LEFTHALF) {
436 new_width = half_scr_width - adj_size;
437 new_x = usableArea.x1;
438 } else if (directions & MAX_RIGHTHALF) {
439 new_width = half_scr_width - adj_size;
440 new_x = usableArea.x1 + half_scr_width;
442 /* top|bottom position */
443 if (directions & MAX_TOPHALF) {
444 new_height = half_scr_height - adj_size;
445 new_y = usableArea.y1;
446 } else if (directions & MAX_BOTTOMHALF) {
447 new_height = half_scr_height - adj_size;
448 new_y = usableArea.y1 + half_scr_height;
451 /* vertical|horizontal position */
452 if (directions & MAX_HORIZONTAL) {
453 new_width = usableArea.x2 - usableArea.x1 - adj_size;
454 new_x = usableArea.x1;
456 if (directions & MAX_VERTICAL) {
457 new_height = usableArea.y2 - usableArea.y1 - adj_size;
458 new_y = usableArea.y1;
459 if (WFLAGP(wwin, full_maximize) && (new_y == 0))
460 new_y -= wwin->frame->top_width;
464 if (!WFLAGP(wwin, full_maximize) && !(directions == MAX_MAXIMUS || directions == MAX_HORIZONTAL))
465 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
467 /* set maximization state */
468 wwin->flags.maximized = directions;
469 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
470 wwin->flags.maximized = MAX_MAXIMUS;
472 wWindowConstrainSize(wwin, &new_width, &new_height);
474 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
475 usableArea.y2 - usableArea.y1, &new_width, &new_height);
477 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
478 wWindowSynthConfigureNotify(wwin);
480 WMPostNotificationName(WMNChangedState, wwin, "maximize");
483 /* generic (un)maximizer */
484 void handleMaximize(WWindow *wwin, int directions)
486 int current = wwin->flags.maximized;
487 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
488 int effective = requested ^ current;
489 int flags = directions & ~requested;
491 if (!effective) {
492 /* allow wMaximizeWindow to restore the Maximusized size */
493 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
494 !(requested & MAX_MAXIMUS))
495 wMaximizeWindow(wwin, MAX_MAXIMUS | flags);
496 else
497 wUnmaximizeWindow(wwin);
498 /* these alone mean vertical|horizontal toggle */
499 } else if ((effective == MAX_LEFTHALF) ||
500 (effective == MAX_RIGHTHALF) ||
501 (effective == MAX_TOPHALF) ||
502 (effective == MAX_BOTTOMHALF))
503 wUnmaximizeWindow(wwin);
504 else {
505 if ((requested == (MAX_HORIZONTAL | MAX_VERTICAL)) ||
506 (requested == MAX_MAXIMUS))
507 effective = requested;
508 else {
509 if (requested & MAX_LEFTHALF) {
510 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
511 effective |= MAX_VERTICAL;
512 else
513 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
514 effective |= MAX_LEFTHALF;
515 effective &= ~(MAX_HORIZONTAL | MAX_RIGHTHALF);
516 } else if (requested & MAX_RIGHTHALF) {
517 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
518 effective |= MAX_VERTICAL;
519 else
520 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
521 effective |= MAX_RIGHTHALF;
522 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF);
524 if (requested & MAX_TOPHALF) {
525 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
526 effective |= MAX_HORIZONTAL;
527 else
528 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
529 effective |= MAX_TOPHALF;
530 effective &= ~(MAX_VERTICAL | MAX_BOTTOMHALF);
531 } else if (requested & MAX_BOTTOMHALF) {
532 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
533 effective |= MAX_HORIZONTAL;
534 else
535 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
536 effective |= MAX_BOTTOMHALF;
537 effective &= ~(MAX_VERTICAL | MAX_TOPHALF);
539 if (requested & MAX_HORIZONTAL)
540 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
541 if (requested & MAX_VERTICAL)
542 effective &= ~(MAX_TOPHALF | MAX_BOTTOMHALF);
543 effective &= ~MAX_MAXIMUS;
545 wMaximizeWindow(wwin, effective | flags);
549 /* the window boundary coordinates */
550 typedef struct {
551 int left;
552 int right;
553 int bottom;
554 int top;
555 int width;
556 int height;
557 } win_coords;
559 static void set_window_coords(WWindow *wwin, win_coords *obs)
561 obs->left = wwin->frame_x;
562 obs->top = wwin->frame_y;
563 obs->width = wwin->frame->core->width;
564 obs->height = wwin->frame->core->height;
565 obs->bottom = obs->top + obs->height;
566 obs->right = obs->left + obs->width;
570 * Maximus: tiled maximization (maximize without overlapping other windows)
572 * The original window 'orig' will be maximized to new coordinates 'new'.
573 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
575 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
576 unsigned int *new_width, unsigned int *new_height)
578 WWindow *tmp;
579 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
580 short int adjust_height;
581 int x_intsect, y_intsect;
582 /* the obstructing, original and new windows */
583 win_coords obs, orig, new;
585 /* set the original coordinate positions of the window to be Maximumized */
586 if (wwin->flags.maximized) {
587 /* window is already maximized; consider original geometry */
588 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
589 orig.bottom = orig.top + orig.height;
590 orig.right = orig.left + orig.width;
591 } else
592 set_window_coords(wwin, &orig);
594 /* Try to fully maximize first, then readjust later */
595 new.left = usableArea.x1;
596 new.right = usableArea.x2;
597 new.top = usableArea.y1;
598 new.bottom = usableArea.y2;
600 if (HAS_TITLEBAR(wwin))
601 tbar_height_0 = TITLEBAR_HEIGHT;
602 if (HAS_RESIZEBAR(wwin))
603 rbar_height_0 = RESIZEBAR_HEIGHT;
604 if (HAS_BORDER(wwin))
605 bd_width_0 = wwin->screen_ptr->frame_border_width;
607 /* the length to be subtracted if the window has titlebar, etc */
608 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
610 tmp = wwin;
611 /* The focused window is always the last in the list */
612 while (tmp->prev) {
613 /* ignore windows in other workspaces etc */
614 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
615 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
616 tmp = tmp->prev;
617 continue;
619 tmp = tmp->prev;
621 /* Set the coordinates of obstructing window */
622 set_window_coords(tmp, &obs);
624 /* Try to maximize in the y direction first */
625 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
626 if (x_intsect != 0) {
627 /* TODO: Consider the case when coords are equal */
628 if (obs.bottom < orig.top && obs.bottom > new.top) {
629 /* w_0 is below the bottom of w_j */
630 new.top = obs.bottom + 1;
632 if (orig.bottom < obs.top && obs.top < new.bottom) {
633 /* The bottom of w_0 is above the top of w_j */
634 new.bottom = obs.top - 1;
639 tmp = wwin;
640 while (tmp->prev) {
641 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
642 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
643 tmp = tmp->prev;
644 continue;
646 tmp = tmp->prev;
648 set_window_coords(tmp, &obs);
651 * Use the new.top and new.height instead of original values
652 * as they may have different intersections with the obstructing windows
654 new.height = new.bottom - new.top - adjust_height;
655 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
656 if (y_intsect != 0) {
657 if (obs.right < orig.left && obs.right > new.left) {
658 /* w_0 is completely to the right of w_j */
659 new.left = obs.right + 1;
661 if (orig.right < obs.left && obs.left < new.right) {
662 /* w_0 is completely to the left of w_j */
663 new.right = obs.left - 1;
668 *new_x = new.left;
669 *new_y = new.top;
670 /* xcalc needs -7 here, but other apps don't */
671 *new_height = new.bottom - new.top - adjust_height - 1;
672 *new_width = new.right - new.left;
675 void wUnmaximizeWindow(WWindow *wwin)
677 int x, y, w, h;
679 if (!wwin->flags.maximized)
680 return;
682 if (wwin->flags.shaded) {
683 wwin->flags.skip_next_animation = 1;
684 wUnshadeWindow(wwin);
687 if (wPreferences.drag_maximized_window == DRAGMAX_NOMOVE)
688 wwin->client_flags.no_movable = 0;
690 /* Use old coordinates if they are set, current values otherwise */
691 remember_geometry(wwin, &x, &y, &w, &h);
693 /* unMaximusize relative to original position */
694 if (wwin->flags.maximized & MAX_MAXIMUS) {
695 x += wwin->frame_x - wwin->maximus_x;
696 y += wwin->frame_y - wwin->maximus_y;
699 wwin->flags.maximized = 0;
700 wwin->flags.old_maximized = 0;
701 wWindowConfigure(wwin, x, y, w, h);
702 wWindowSynthConfigureNotify(wwin);
704 WMPostNotificationName(WMNChangedState, wwin, "maximize");
707 void wFullscreenWindow(WWindow *wwin)
709 int head;
710 WMRect rect;
712 if (wwin->flags.fullscreen)
713 return;
715 wwin->flags.fullscreen = True;
717 wWindowConfigureBorders(wwin);
719 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
720 wRaiseFrame(wwin->frame->core);
722 wwin->bfs_geometry.x = wwin->frame_x;
723 wwin->bfs_geometry.y = wwin->frame_y;
724 wwin->bfs_geometry.width = wwin->frame->core->width;
725 wwin->bfs_geometry.height = wwin->frame->core->height;
727 head = wGetHeadForWindow(wwin);
728 rect = wGetRectForHead(wwin->screen_ptr, head);
729 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
731 wwin->screen_ptr->bfs_focused_window = wwin->screen_ptr->focused_window;
732 wSetFocusTo(wwin->screen_ptr, wwin);
734 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
737 void wUnfullscreenWindow(WWindow *wwin)
739 if (!wwin->flags.fullscreen)
740 return;
742 wwin->flags.fullscreen = False;
744 if (WFLAGP(wwin, sunken))
745 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
746 else if (WFLAGP(wwin, floating))
747 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
749 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
750 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
752 wWindowConfigureBorders(wwin);
754 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
755 wFrameWindowPaint(wwin->frame);
758 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
760 if (wwin->screen_ptr->bfs_focused_window) {
761 wSetFocusTo(wwin->screen_ptr, wwin->screen_ptr->bfs_focused_window);
762 wwin->screen_ptr->bfs_focused_window = NULL;
766 #ifdef ANIMATIONS
767 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
769 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
770 float cx, cy, cw, ch;
771 float xstep, ystep, wstep, hstep;
772 XPoint points[5];
773 float dx, dch, midy;
774 float angle, final_angle, delta;
776 xstep = (float)(fx - x) / steps;
777 ystep = (float)(fy - y) / steps;
778 wstep = (float)(fw - w) / steps;
779 hstep = (float)(fh - h) / steps;
781 cx = (float)x;
782 cy = (float)y;
783 cw = (float)w;
784 ch = (float)h;
786 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
787 delta = (float)(final_angle / FRAMES);
788 for (angle = 0;; angle += delta) {
789 if (angle > final_angle)
790 angle = final_angle;
792 dx = (cw / 10) - ((cw / 5) * sin(angle));
793 dch = (ch / 2) * cos(angle);
794 midy = cy + (ch / 2);
796 points[0].x = cx + dx;
797 points[0].y = midy - dch;
798 points[1].x = cx + cw - dx;
799 points[1].y = points[0].y;
800 points[2].x = cx + cw + dx;
801 points[2].y = midy + dch;
802 points[3].x = cx - dx;
803 points[3].y = points[2].y;
804 points[4].x = points[0].x;
805 points[4].y = points[0].y;
807 XGrabServer(dpy);
808 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
809 XFlush(dpy);
810 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
812 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
813 XUngrabServer(dpy);
814 cx += xstep;
815 cy += ystep;
816 cw += wstep;
817 ch += hstep;
818 if (angle >= final_angle)
819 break;
822 XFlush(dpy);
825 #undef FRAMES
827 static void
828 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
830 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
831 float cx, cy, cw, ch;
832 float xstep, ystep, wstep, hstep;
833 XPoint points[5];
834 float angle, final_angle, a, d, delta;
836 x += w / 2;
837 y += h / 2;
838 fx += fw / 2;
839 fy += fh / 2;
841 xstep = (float)(fx - x) / steps;
842 ystep = (float)(fy - y) / steps;
843 wstep = (float)(fw - w) / steps;
844 hstep = (float)(fh - h) / steps;
846 cx = (float)x;
847 cy = (float)y;
848 cw = (float)w;
849 ch = (float)h;
851 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
852 delta = (float)(final_angle / FRAMES);
853 for (angle = 0;; angle += delta) {
854 if (angle > final_angle)
855 angle = final_angle;
857 a = atan(ch / cw);
858 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
860 points[0].x = cx + cos(angle - a) * d;
861 points[0].y = cy + sin(angle - a) * d;
862 points[1].x = cx + cos(angle + a) * d;
863 points[1].y = cy + sin(angle + a) * d;
864 points[2].x = cx + cos(angle - a + WM_PI) * d;
865 points[2].y = cy + sin(angle - a + WM_PI) * d;
866 points[3].x = cx + cos(angle + a + WM_PI) * d;
867 points[3].y = cy + sin(angle + a + WM_PI) * d;
868 points[4].x = cx + cos(angle - a) * d;
869 points[4].y = cy + sin(angle - a) * d;
870 XGrabServer(dpy);
871 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
872 XFlush(dpy);
873 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
875 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
876 XUngrabServer(dpy);
877 cx += xstep;
878 cy += ystep;
879 cw += wstep;
880 ch += hstep;
881 if (angle >= final_angle)
882 break;
885 XFlush(dpy);
888 #undef FRAMES
890 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
892 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
893 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
894 float xstep, ystep, wstep, hstep;
895 int i, j;
897 xstep = (float)(fx - x) / steps;
898 ystep = (float)(fy - y) / steps;
899 wstep = (float)(fw - w) / steps;
900 hstep = (float)(fh - h) / steps;
902 for (j = 0; j < FRAMES; j++) {
903 cx[j] = (float)x;
904 cy[j] = (float)y;
905 cw[j] = (float)w;
906 ch[j] = (float)h;
908 XGrabServer(dpy);
909 for (i = 0; i < steps; i++) {
910 for (j = 0; j < FRAMES; j++) {
911 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
912 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
914 XFlush(dpy);
915 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
917 for (j = 0; j < FRAMES; j++) {
918 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
919 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
920 if (j < FRAMES - 1) {
921 cx[j] = cx[j + 1];
922 cy[j] = cy[j + 1];
923 cw[j] = cw[j + 1];
924 ch[j] = ch[j + 1];
925 } else {
926 cx[j] += xstep;
927 cy[j] += ystep;
928 cw[j] += wstep;
929 ch[j] += hstep;
934 for (j = 0; j < FRAMES; j++)
935 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
936 XFlush(dpy);
937 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
939 for (j = 0; j < FRAMES; j++)
940 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
942 XUngrabServer(dpy);
945 #undef FRAMES
947 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
949 int style = wPreferences.iconification_style; /* Catch the value */
950 int steps;
952 if (style == WIS_NONE)
953 return;
955 if (style == WIS_RANDOM)
956 style = rand() % 3;
958 switch (style) {
959 case WIS_TWIST:
960 steps = MINIATURIZE_ANIMATION_STEPS_T;
961 if (steps > 0)
962 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
963 break;
964 case WIS_FLIP:
965 steps = MINIATURIZE_ANIMATION_STEPS_F;
966 if (steps > 0)
967 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
968 break;
969 case WIS_ZOOM:
970 default:
971 steps = MINIATURIZE_ANIMATION_STEPS_Z;
972 if (steps > 0)
973 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
974 break;
977 #endif /* ANIMATIONS */
979 static void flushExpose(void)
981 XEvent tmpev;
983 while (XCheckTypedEvent(dpy, Expose, &tmpev))
984 WMHandleEvent(&tmpev);
985 XSync(dpy, 0);
988 static void unmapTransientsFor(WWindow *wwin)
990 WWindow *tmp;
992 tmp = wwin->screen_ptr->focused_window;
993 while (tmp) {
994 /* unmap the transients for this transient */
995 if (tmp != wwin && tmp->transient_for == wwin->client_win
996 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
997 unmapTransientsFor(tmp);
998 tmp->flags.miniaturized = 1;
999 if (!tmp->flags.shaded)
1000 wWindowUnmap(tmp);
1001 else
1002 XUnmapWindow(dpy, tmp->frame->core->window);
1004 if (!tmp->flags.shaded)
1006 wClientSetState(tmp, IconicState, None);
1008 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1010 tmp = tmp->prev;
1014 static void mapTransientsFor(WWindow *wwin)
1016 WWindow *tmp;
1018 tmp = wwin->screen_ptr->focused_window;
1019 while (tmp) {
1020 /* recursively map the transients for this transient */
1021 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1022 && tmp->icon == NULL) {
1023 mapTransientsFor(tmp);
1024 tmp->flags.miniaturized = 0;
1025 if (!tmp->flags.shaded)
1026 wWindowMap(tmp);
1027 else
1028 XMapWindow(dpy, tmp->frame->core->window);
1029 tmp->flags.semi_focused = 0;
1031 if (!tmp->flags.shaded)
1033 wClientSetState(tmp, NormalState, None);
1035 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1037 tmp = tmp->prev;
1041 static WWindow *recursiveTransientFor(WWindow *wwin)
1043 int i;
1045 if (!wwin)
1046 return None;
1048 /* hackish way to detect transient_for cycle */
1049 i = wwin->screen_ptr->window_count + 1;
1051 while (wwin && wwin->transient_for != None && i > 0) {
1052 wwin = wWindowFor(wwin->transient_for);
1053 i--;
1055 if (i == 0 && wwin) {
1056 wwarning(_("window \"%s\" has a severely broken WM_TRANSIENT_FOR hint"),
1057 wwin->frame->title);
1058 return NULL;
1061 return wwin;
1064 static int getAnimationGeometry(WWindow *wwin, int *ix, int *iy, int *iw, int *ih)
1066 if (wwin->screen_ptr->flags.startup || wPreferences.no_animations
1067 || wwin->flags.skip_next_animation || wwin->icon == NULL)
1068 return 0;
1070 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1071 *ix = wwin->icon_x;
1072 *iy = wwin->icon_y;
1073 *iw = wwin->icon->core->width;
1074 *ih = wwin->icon->core->height;
1075 } else {
1076 if (wwin->flags.net_handle_icon) {
1077 *ix = wwin->icon_x;
1078 *iy = wwin->icon_y;
1079 *iw = wwin->icon_w;
1080 *ih = wwin->icon_h;
1081 } else {
1082 *ix = 0;
1083 *iy = 0;
1084 *iw = wwin->screen_ptr->scr_width;
1085 *ih = wwin->screen_ptr->scr_height;
1088 return 1;
1091 void wIconifyWindow(WWindow *wwin)
1093 XWindowAttributes attribs;
1094 int present;
1096 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1097 return; /* the window doesn't exist anymore */
1099 if (wwin->flags.miniaturized)
1100 return; /* already miniaturized */
1102 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1103 WWindow *owner = wWindowFor(wwin->transient_for);
1105 if (owner && owner->flags.miniaturized)
1106 return;
1109 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1111 /* if the window is in another workspace, simplify process */
1112 if (present) {
1113 /* icon creation may take a while */
1114 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1115 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1116 GrabModeAsync, None, None, CurrentTime);
1119 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1120 if (!wwin->flags.icon_moved)
1121 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1123 wwin->icon = icon_create_for_wwindow(wwin);
1124 wwin->icon->mapped = 1;
1126 /* extract the window screenshot everytime, as the option can be enable anytime */
1127 if (wwin->client_win && wwin->flags.mapped) {
1128 RImage *mini_preview;
1129 XImage *pimg;
1130 unsigned int w, h;
1131 int x, y;
1132 Window baz;
1134 XRaiseWindow(dpy, wwin->frame->core->window);
1135 XTranslateCoordinates(dpy, wwin->client_win, wwin->screen_ptr->root_win, 0, 0, &x, &y, &baz);
1137 w = attribs.width;
1138 h = attribs.height;
1140 if (x - attribs.x + attribs.width > wwin->screen_ptr->scr_width)
1141 w = wwin->screen_ptr->scr_width - x + attribs.x;
1143 if (y - attribs.y + attribs.height > wwin->screen_ptr->scr_height)
1144 h = wwin->screen_ptr->scr_height - y + attribs.y;
1146 pimg = XGetImage(dpy, wwin->client_win, 0, 0, w, h, AllPlanes, ZPixmap);
1147 if (pimg) {
1148 mini_preview = RCreateImageFromXImage(wwin->screen_ptr->rcontext, pimg, NULL);
1149 XDestroyImage(pimg);
1151 if (mini_preview) {
1152 set_icon_minipreview(wwin->icon, mini_preview);
1153 RReleaseImage(mini_preview);
1154 } else {
1155 const char *title;
1156 char title_buf[32];
1158 if (wwin->frame && wwin->frame->title) {
1159 title = wwin->frame->title;
1160 } else {
1161 snprintf(title_buf, sizeof(title_buf), "(id=0x%lx)", wwin->client_win);
1162 title = title_buf;
1164 wwarning(_("creation of mini-preview failed for window \"%s\""), title);
1170 wwin->flags.miniaturized = 1;
1171 wwin->flags.mapped = 0;
1173 /* unmap transients */
1174 unmapTransientsFor(wwin);
1176 if (present) {
1177 #ifdef ANIMATIONS
1178 int ix, iy, iw, ih;
1179 #endif
1180 XUngrabPointer(dpy, CurrentTime);
1181 wWindowUnmap(wwin);
1182 /* let all Expose events arrive so that we can repaint
1183 * something before the animation starts (and the server is grabbed) */
1184 XSync(dpy, 0);
1186 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1187 wClientSetState(wwin, IconicState, None);
1188 else
1189 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1191 flushExpose();
1192 #ifdef ANIMATIONS
1193 if (getAnimationGeometry(wwin, &ix, &iy, &iw, &ih))
1194 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1195 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1196 #endif
1199 wwin->flags.skip_next_animation = 0;
1201 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1202 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1203 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1204 XMapWindow(dpy, wwin->icon->core->window);
1206 AddToStackList(wwin->icon->core);
1207 wLowerFrame(wwin->icon->core);
1210 if (present) {
1211 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1214 * It doesn't seem to be working and causes button event hangup
1215 * when deiconifying a transient window.
1216 setupIconGrabs(wwin->icon);
1218 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1219 && wPreferences.focus_mode == WKF_CLICK) {
1220 WWindow *tmp;
1222 tmp = wwin->prev;
1223 while (tmp) {
1224 if (!WFLAGP(tmp, no_focusable)
1225 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1226 && (wwin->frame->workspace == tmp->frame->workspace))
1227 break;
1228 tmp = tmp->prev;
1230 wSetFocusTo(wwin->screen_ptr, tmp);
1231 } else if (wPreferences.focus_mode != WKF_CLICK) {
1232 wSetFocusTo(wwin->screen_ptr, NULL);
1234 #ifdef ANIMATIONS
1235 if (!wwin->screen_ptr->flags.startup) {
1236 /* Catch up with events not processed while animation was running */
1237 Window clientwin = wwin->client_win;
1239 ProcessPendingEvents();
1241 /* the window can disappear while ProcessPendingEvents() runs */
1242 if (!wWindowFor(clientwin))
1243 return;
1245 #endif
1248 /* maybe we want to do this regardless of net_handle_icon
1249 * it seems to me we might break behaviour this way.
1251 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1252 && !wwin->flags.net_handle_icon)
1253 wIconSelect(wwin->icon);
1255 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1257 if (wPreferences.auto_arrange_icons)
1258 wArrangeIcons(wwin->screen_ptr, True);
1261 void wDeiconifyWindow(WWindow *wwin)
1263 /* Let's avoid changing workspace while deiconifying */
1264 w_global.ignore_workspace_change = True;
1266 /* we're hiding for show_desktop */
1267 int netwm_hidden = wwin->flags.net_show_desktop &&
1268 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1270 if (!netwm_hidden)
1271 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1273 if (!wwin->flags.miniaturized) {
1274 w_global.ignore_workspace_change = False;
1275 return;
1278 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1279 WWindow *owner = recursiveTransientFor(wwin);
1281 if (owner && owner->flags.miniaturized) {
1282 wDeiconifyWindow(owner);
1283 wSetFocusTo(wwin->screen_ptr, wwin);
1284 wRaiseFrame(wwin->frame->core);
1285 w_global.ignore_workspace_change = False;
1286 return;
1290 wwin->flags.miniaturized = 0;
1292 if (!netwm_hidden && !wwin->flags.shaded)
1293 wwin->flags.mapped = 1;
1295 if (!netwm_hidden || wPreferences.sticky_icons) {
1296 /* maybe we want to do this regardless of net_handle_icon
1297 * it seems to me we might break behaviour this way.
1299 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1300 && wwin->icon != NULL) {
1301 if (wwin->icon->selected)
1302 wIconSelect(wwin->icon);
1304 XUnmapWindow(dpy, wwin->icon->core->window);
1308 /* if the window is in another workspace, do it silently */
1309 if (!netwm_hidden) {
1310 #ifdef ANIMATIONS
1311 int ix, iy, iw, ih;
1312 if (getAnimationGeometry(wwin, &ix, &iy, &iw, &ih))
1313 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1314 wwin->frame_x, wwin->frame_y,
1315 wwin->frame->core->width, wwin->frame->core->height);
1316 #endif
1317 wwin->flags.skip_next_animation = 0;
1318 XGrabServer(dpy);
1319 if (!wwin->flags.shaded)
1320 XMapWindow(dpy, wwin->client_win);
1322 XMapWindow(dpy, wwin->frame->core->window);
1323 wRaiseFrame(wwin->frame->core);
1324 if (!wwin->flags.shaded)
1325 wClientSetState(wwin, NormalState, None);
1327 mapTransientsFor(wwin);
1330 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1331 && !wwin->flags.net_handle_icon) {
1332 RemoveFromStackList(wwin->icon->core);
1333 wSetFocusTo(wwin->screen_ptr, wwin);
1334 wIconDestroy(wwin->icon);
1335 wwin->icon = NULL;
1338 if (!netwm_hidden) {
1339 XUngrabServer(dpy);
1341 wSetFocusTo(wwin->screen_ptr, wwin);
1343 #ifdef ANIMATIONS
1344 if (!wwin->screen_ptr->flags.startup) {
1345 /* Catch up with events not processed while animation was running */
1346 Window clientwin = wwin->client_win;
1348 ProcessPendingEvents();
1350 /* the window can disappear while ProcessPendingEvents() runs */
1351 if (!wWindowFor(clientwin)) {
1352 w_global.ignore_workspace_change = False;
1353 return;
1356 #endif
1359 if (wPreferences.auto_arrange_icons)
1360 wArrangeIcons(wwin->screen_ptr, True);
1362 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1364 /* In case we were shaded and iconified, also unshade */
1365 if (!netwm_hidden)
1366 wUnshadeWindow(wwin);
1368 w_global.ignore_workspace_change = False;
1371 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1373 if (wwin->flags.miniaturized) {
1374 if (wwin->icon) {
1375 XUnmapWindow(dpy, wwin->icon->core->window);
1376 wwin->icon->mapped = 0;
1378 wwin->flags.hidden = 1;
1380 WMPostNotificationName(WMNChangedState, wwin, "hide");
1381 return;
1384 if (wwin->flags.inspector_open)
1385 wHideInspectorForWindow(wwin);
1387 wwin->flags.hidden = 1;
1388 wWindowUnmap(wwin);
1390 wClientSetState(wwin, IconicState, icon->icon_win);
1391 flushExpose();
1393 #ifdef ANIMATIONS
1394 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1395 !wwin->flags.skip_next_animation && animate) {
1396 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1397 wwin->frame->core->width, wwin->frame->core->height,
1398 icon_x, icon_y, icon->core->width, icon->core->height);
1400 #endif
1401 wwin->flags.skip_next_animation = 0;
1403 WMPostNotificationName(WMNChangedState, wwin, "hide");
1406 void wHideAll(WScreen *scr)
1408 WWindow *wwin;
1409 WWindow **windows;
1410 WMenu *menu;
1411 unsigned int wcount = 0;
1412 int i;
1414 if (!scr)
1415 return;
1417 menu = scr->switch_menu;
1419 windows = wmalloc(sizeof(WWindow *));
1421 if (menu != NULL) {
1422 for (i = 0; i < menu->entry_no; i++) {
1423 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1424 wcount++;
1425 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1427 } else {
1428 wwin = scr->focused_window;
1430 while (wwin) {
1431 windows[wcount] = wwin;
1432 wcount++;
1433 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1434 wwin = wwin->prev;
1439 for (i = 0; i < wcount; i++) {
1440 wwin = windows[i];
1441 if (wwin->frame->workspace == scr->current_workspace
1442 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1443 && !wwin->flags.internal_window
1444 && !WFLAGP(wwin, no_miniaturizable)
1446 wwin->flags.skip_next_animation = 1;
1447 wIconifyWindow(wwin);
1451 wfree(windows);
1454 void wHideOtherApplications(WWindow *awin)
1456 WWindow *wwin;
1457 WApplication *tapp;
1459 if (!awin)
1460 return;
1461 wwin = awin->screen_ptr->focused_window;
1463 while (wwin) {
1464 if (wwin != awin
1465 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1466 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1467 && !wwin->flags.internal_window
1468 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1470 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1471 if (!WFLAGP(wwin, no_miniaturizable)) {
1472 wwin->flags.skip_next_animation = 1;
1473 wIconifyWindow(wwin);
1475 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1476 tapp = wApplicationOf(wwin->main_window);
1477 if (tapp) {
1478 tapp->flags.skip_next_animation = 1;
1479 wHideApplication(tapp);
1480 } else {
1481 if (!WFLAGP(wwin, no_miniaturizable)) {
1482 wwin->flags.skip_next_animation = 1;
1483 wIconifyWindow(wwin);
1488 wwin = wwin->prev;
1491 wSetFocusTo(awin->screen_ptr, awin);
1495 void wHideApplication(WApplication *wapp)
1497 WScreen *scr;
1498 WWindow *wlist;
1499 int hadfocus;
1500 int animate;
1502 if (!wapp) {
1503 wwarning("trying to hide a non grouped window");
1504 return;
1506 if (!wapp->main_window_desc) {
1507 wwarning("group leader not found for window group");
1508 return;
1510 scr = wapp->main_window_desc->screen_ptr;
1511 hadfocus = 0;
1512 wlist = scr->focused_window;
1513 if (!wlist)
1514 return;
1516 if (wlist->main_window == wapp->main_window)
1517 wapp->last_focused = wlist;
1518 else
1519 wapp->last_focused = NULL;
1521 animate = !wapp->flags.skip_next_animation;
1523 while (wlist) {
1524 if (wlist->main_window == wapp->main_window) {
1525 if (wlist->flags.focused)
1526 hadfocus = 1;
1527 if (wapp->app_icon) {
1528 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1529 wapp->app_icon->y_pos, wlist, animate);
1530 animate = False;
1533 wlist = wlist->prev;
1536 wapp->flags.skip_next_animation = 0;
1538 if (hadfocus) {
1539 if (wPreferences.focus_mode == WKF_CLICK) {
1540 wlist = scr->focused_window;
1541 while (wlist) {
1542 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1543 && (wlist->flags.mapped || wlist->flags.shaded))
1544 break;
1545 wlist = wlist->prev;
1547 wSetFocusTo(scr, wlist);
1548 } else {
1549 wSetFocusTo(scr, NULL);
1553 wapp->flags.hidden = 1;
1555 if (wPreferences.auto_arrange_icons)
1556 wArrangeIcons(scr, True);
1558 #ifdef HIDDENDOT
1559 if (wapp->app_icon)
1560 wAppIconPaint(wapp->app_icon);
1561 #endif
1564 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1566 if (bringToCurrentWS)
1567 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1569 wwin->flags.hidden = 0;
1571 #ifdef ANIMATIONS
1572 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1573 animateResize(wwin->screen_ptr, icon_x, icon_y,
1574 icon->core->width, icon->core->height,
1575 wwin->frame_x, wwin->frame_y,
1576 wwin->frame->core->width, wwin->frame->core->height);
1578 #endif
1579 wwin->flags.skip_next_animation = 0;
1580 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1581 XMapWindow(dpy, wwin->client_win);
1582 XMapWindow(dpy, wwin->frame->core->window);
1583 wClientSetState(wwin, NormalState, None);
1584 wwin->flags.mapped = 1;
1585 wRaiseFrame(wwin->frame->core);
1587 if (wwin->flags.inspector_open)
1588 wUnhideInspectorForWindow(wwin);
1590 WMPostNotificationName(WMNChangedState, wwin, "hide");
1593 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1595 WScreen *scr;
1596 WWindow *wlist, *next;
1597 WWindow *focused = NULL;
1598 int animate;
1600 if (!wapp)
1601 return;
1603 scr = wapp->main_window_desc->screen_ptr;
1604 wlist = scr->focused_window;
1605 if (!wlist)
1606 return;
1608 /* goto beginning of list */
1609 while (wlist->prev)
1610 wlist = wlist->prev;
1612 animate = !wapp->flags.skip_next_animation;
1614 while (wlist) {
1615 next = wlist->next;
1617 if (wlist->main_window == wapp->main_window) {
1618 if (wlist->flags.focused)
1619 focused = wlist;
1620 else if (!focused || !focused->flags.focused)
1621 focused = wlist;
1623 if (wlist->flags.miniaturized) {
1624 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1625 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1626 if (!wlist->icon->mapped) {
1627 int x, y;
1629 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1630 if (wlist->icon_x != x || wlist->icon_y != y)
1631 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1632 wlist->icon_x = x;
1633 wlist->icon_y = y;
1634 XMapWindow(dpy, wlist->icon->core->window);
1635 wlist->icon->mapped = 1;
1637 wRaiseFrame(wlist->icon->core);
1639 if (bringToCurrentWS)
1640 wWindowChangeWorkspace(wlist, scr->current_workspace);
1641 wlist->flags.hidden = 0;
1642 if (miniwindows && wlist->frame->workspace == scr->current_workspace)
1643 wDeiconifyWindow(wlist);
1644 WMPostNotificationName(WMNChangedState, wlist, "hide");
1645 } else if (wlist->flags.shaded) {
1646 if (bringToCurrentWS)
1647 wWindowChangeWorkspace(wlist, scr->current_workspace);
1648 wlist->flags.hidden = 0;
1649 wRaiseFrame(wlist->frame->core);
1650 if (wlist->frame->workspace == scr->current_workspace) {
1651 XMapWindow(dpy, wlist->frame->core->window);
1652 if (miniwindows)
1653 wUnshadeWindow(wlist);
1655 WMPostNotificationName(WMNChangedState, wlist, "hide");
1656 } else if (wlist->flags.hidden) {
1657 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1658 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1659 animate = False;
1660 } else {
1661 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace)
1662 wWindowChangeWorkspace(wlist, scr->current_workspace);
1664 wRaiseFrame(wlist->frame->core);
1667 wlist = next;
1670 wapp->flags.skip_next_animation = 0;
1671 wapp->flags.hidden = 0;
1673 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1674 wRaiseFrame(wapp->last_focused->frame->core);
1675 wSetFocusTo(scr, wapp->last_focused);
1676 } else if (focused) {
1677 wSetFocusTo(scr, focused);
1679 wapp->last_focused = NULL;
1680 if (wPreferences.auto_arrange_icons)
1681 wArrangeIcons(scr, True);
1683 #ifdef HIDDENDOT
1684 wAppIconPaint(wapp->app_icon);
1685 #endif
1688 void wShowAllWindows(WScreen *scr)
1690 WWindow *wwin, *old_foc;
1691 WApplication *wapp;
1693 old_foc = wwin = scr->focused_window;
1694 while (wwin) {
1695 if (!wwin->flags.internal_window &&
1696 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1697 if (wwin->flags.miniaturized) {
1698 wwin->flags.skip_next_animation = 1;
1699 wDeiconifyWindow(wwin);
1700 } else if (wwin->flags.hidden) {
1701 wapp = wApplicationOf(wwin->main_window);
1702 if (wapp) {
1703 wUnhideApplication(wapp, False, False);
1704 } else {
1705 wwin->flags.skip_next_animation = 1;
1706 wDeiconifyWindow(wwin);
1710 wwin = wwin->prev;
1712 wSetFocusTo(scr, old_foc);
1713 /*wRaiseFrame(old_foc->frame->core); */
1716 void wRefreshDesktop(WScreen *scr)
1718 Window win;
1719 XSetWindowAttributes attr;
1721 attr.backing_store = NotUseful;
1722 attr.save_under = False;
1723 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1724 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1725 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1726 XMapRaised(dpy, win);
1727 XDestroyWindow(dpy, win);
1728 XFlush(dpy);
1731 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1733 WWindow *wwin;
1734 WAppIcon *aicon;
1736 int head;
1737 const int heads = wXineramaHeads(scr);
1739 struct HeadVars {
1740 int pf; /* primary axis */
1741 int sf; /* secondary axis */
1742 int fullW;
1743 int fullH;
1744 int pi, si;
1745 int sx1, sx2, sy1, sy2; /* screen boundary */
1746 int sw, sh;
1747 int xo, yo;
1748 int xs, ys;
1749 } *vars;
1751 int isize = wPreferences.icon_size;
1753 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1755 for (head = 0; head < heads; ++head) {
1756 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1757 WMRect rect;
1759 if (scr->dock) {
1760 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
1762 if (scr->dock->on_right_side)
1763 area.x2 -= offset;
1764 else
1765 area.x1 += offset;
1768 rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1770 vars[head].pi = vars[head].si = 0;
1771 vars[head].sx1 = rect.pos.x;
1772 vars[head].sy1 = rect.pos.y;
1773 vars[head].sw = rect.size.width;
1774 vars[head].sh = rect.size.height;
1775 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1776 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1777 vars[head].sw = isize * (vars[head].sw / isize);
1778 vars[head].sh = isize * (vars[head].sh / isize);
1779 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1780 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1782 /* icon yard boundaries */
1783 if (wPreferences.icon_yard & IY_VERT) {
1784 vars[head].pf = vars[head].fullH;
1785 vars[head].sf = vars[head].fullW;
1786 } else {
1787 vars[head].pf = vars[head].fullW;
1788 vars[head].sf = vars[head].fullH;
1790 if (wPreferences.icon_yard & IY_RIGHT) {
1791 vars[head].xo = vars[head].sx2 - isize;
1792 vars[head].xs = -1;
1793 } else {
1794 vars[head].xo = vars[head].sx1;
1795 vars[head].xs = 1;
1797 if (wPreferences.icon_yard & IY_TOP) {
1798 vars[head].yo = vars[head].sy1;
1799 vars[head].ys = 1;
1800 } else {
1801 vars[head].yo = vars[head].sy2 - isize;
1802 vars[head].ys = -1;
1806 #define X ((wPreferences.icon_yard & IY_VERT) \
1807 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1808 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1810 #define Y ((wPreferences.icon_yard & IY_VERT) \
1811 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1812 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1814 /* arrange application icons */
1815 aicon = scr->app_icon_list;
1816 /* reverse them to avoid unnecessarily sliding of icons */
1817 while (aicon && aicon->next)
1818 aicon = aicon->next;
1820 while (aicon) {
1821 if (!aicon->docked) {
1822 /* CHECK: can icon be NULL here ? */
1823 /* The intention here is to place the AppIcon on the head that
1824 * contains most of the applications _main_ window. */
1825 head = wGetHeadForWindow(aicon->icon->owner);
1827 if (aicon->x_pos != X || aicon->y_pos != Y) {
1828 #ifdef ANIMATIONS
1829 if (!wPreferences.no_animations)
1830 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1831 #endif /* ANIMATIONS */
1833 wAppIconMove(aicon, X, Y);
1834 vars[head].pi++;
1835 if (vars[head].pi >= vars[head].pf) {
1836 vars[head].pi = 0;
1837 vars[head].si++;
1840 aicon = aicon->prev;
1843 /* arrange miniwindows */
1844 wwin = scr->focused_window;
1845 /* reverse them to avoid unnecessarily shuffling */
1846 while (wwin && wwin->prev)
1847 wwin = wwin->prev;
1849 while (wwin) {
1850 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1851 (wwin->frame->workspace == scr->current_workspace ||
1852 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1854 head = wGetHeadForWindow(wwin);
1856 if (arrangeAll || !wwin->flags.icon_moved) {
1857 if (wwin->icon_x != X || wwin->icon_y != Y)
1858 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1860 wwin->icon_x = X;
1861 wwin->icon_y = Y;
1863 vars[head].pi++;
1864 if (vars[head].pi >= vars[head].pf) {
1865 vars[head].pi = 0;
1866 vars[head].si++;
1870 if (arrangeAll)
1871 wwin->flags.icon_moved = 0;
1872 /* we reversed the order, so we use next */
1873 wwin = wwin->next;
1876 wfree(vars);
1879 void wSelectWindow(WWindow *wwin, Bool flag)
1881 WScreen *scr = wwin->screen_ptr;
1883 if (flag) {
1884 wwin->flags.selected = 1;
1885 if (wwin->frame->selected_border_pixel)
1886 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1887 else
1888 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1890 if (!HAS_BORDER(wwin))
1891 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1893 if (!scr->selected_windows)
1894 scr->selected_windows = WMCreateArray(4);
1895 WMAddToArray(scr->selected_windows, wwin);
1896 } else {
1897 wwin->flags.selected = 0;
1898 if (wwin->flags.focused) {
1899 if (wwin->frame->focused_border_pixel)
1900 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->focused_border_pixel);
1901 else
1902 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_focused_border_pixel);
1903 } else {
1904 if (wwin->frame->border_pixel)
1905 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1906 else
1907 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1910 if (!HAS_BORDER(wwin))
1911 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1913 if (scr->selected_windows)
1914 WMRemoveFromArray(scr->selected_windows, wwin);
1918 void wMakeWindowVisible(WWindow *wwin)
1920 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1921 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1923 if (wwin->flags.shaded)
1924 wUnshadeWindow(wwin);
1926 if (wwin->flags.hidden) {
1927 WApplication *app;
1929 app = wApplicationOf(wwin->main_window);
1930 if (app) {
1931 /* trick to get focus to this window */
1932 app->last_focused = wwin;
1933 wUnhideApplication(app, False, False);
1936 if (wwin->flags.miniaturized) {
1937 wDeiconifyWindow(wwin);
1938 } else {
1939 if (!WFLAGP(wwin, no_focusable))
1940 wSetFocusTo(wwin->screen_ptr, wwin);
1941 wRaiseFrame(wwin->frame->core);
1946 * Do the animation while shading (called with what = SHADE)
1947 * or unshading (what = UNSHADE).
1949 #ifdef ANIMATIONS
1950 static void shade_animate(WWindow *wwin, Bool what)
1952 int y, s, w, h;
1953 time_t time0 = time(NULL);
1955 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1956 return;
1958 switch (what) {
1959 case SHADE:
1960 if (!wwin->screen_ptr->flags.startup) {
1961 /* do the shading animation */
1962 h = wwin->frame->core->height;
1963 s = h / SHADE_STEPS;
1964 if (s < 1)
1965 s = 1;
1966 w = wwin->frame->core->width;
1967 y = wwin->frame->top_width;
1968 while (h > wwin->frame->top_width + 1) {
1969 XMoveWindow(dpy, wwin->client_win, 0, y);
1970 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1971 XFlush(dpy);
1973 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1974 break;
1976 if (SHADE_DELAY > 0)
1977 wusleep(SHADE_DELAY * 1000L);
1978 else
1979 wusleep(10);
1980 h -= s;
1981 y -= s;
1983 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1985 break;
1987 case UNSHADE:
1988 h = wwin->frame->top_width + wwin->frame->bottom_width;
1989 y = wwin->frame->top_width - wwin->client.height;
1990 s = abs(y) / SHADE_STEPS;
1991 if (s < 1)
1992 s = 1;
1993 w = wwin->frame->core->width;
1994 XMoveWindow(dpy, wwin->client_win, 0, y);
1995 if (s > 0) {
1996 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1997 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1998 XMoveWindow(dpy, wwin->client_win, 0, y);
1999 XFlush(dpy);
2000 if (SHADE_DELAY > 0)
2001 wusleep(SHADE_DELAY * 2000L / 3);
2002 else
2003 wusleep(10);
2004 h += s;
2005 y += s;
2007 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
2008 break;
2011 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2012 break;
2015 #endif