WPrefs: Code formatting in TexturePanel.c; minimizes checkpatch.pl warnings.
[wmaker-crm.git] / src / actions.c
blobf46e58685de9749d4e5fc2fd549e574a50f33e79
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 #ifndef HAVE_FLOAT_MATHFUNC
56 #define sinf(x) ((float)sin((double)(x)))
57 #define cosf(x) ((float)cos((double)(x)))
58 #define sqrtf(x) ((float)sqrt((double)(x)))
59 #define atan2f(y, x) ((float)atan((double)(y) / (double)(x)))
60 #endif
62 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
63 unsigned int *new_width, unsigned int *new_height);
64 static void save_old_geometry(WWindow *wwin, int directions);
66 /******* Local Variables *******/
67 #ifdef USE_ANIMATIONS
68 static struct {
69 int steps;
70 int delay;
71 } shadePars[5] = {
72 { SHADE_STEPS_UF, SHADE_DELAY_UF },
73 { SHADE_STEPS_F, SHADE_DELAY_F },
74 { SHADE_STEPS_M, SHADE_DELAY_M },
75 { SHADE_STEPS_S, SHADE_DELAY_S },
76 { SHADE_STEPS_US, SHADE_DELAY_US }
79 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
80 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
81 #endif
83 #define UNSHADE 0
84 #define SHADE 1
86 static int compareTimes(Time t1, Time t2)
88 Time diff;
89 if (t1 == t2)
90 return 0;
91 diff = t1 - t2;
92 return (diff < 60000) ? 1 : -1;
95 #ifdef USE_ANIMATIONS
96 static void shade_animate(WWindow *wwin, Bool what);
97 #else
98 static inline void shade_animate(WWindow *wwin, Bool what)
101 * This function is empty on purpose, so tell the compiler
102 * to not warn about parameters being not used
104 (void) wwin;
105 (void) what;
107 #endif
110 *----------------------------------------------------------------------
111 * wSetFocusTo--
112 * Changes the window focus to the one passed as argument.
113 * If the window to focus is not already focused, it will be brought
114 * to the head of the list of windows. Previously focused window is
115 * unfocused.
117 * Side effects:
118 * Window list may be reordered and the window focus is changed.
120 *----------------------------------------------------------------------
122 void wSetFocusTo(WScreen *scr, WWindow *wwin)
124 static WScreen *old_scr = NULL;
126 WWindow *old_focused;
127 WWindow *focused = scr->focused_window;
128 Time timestamp = w_global.timestamp.last_event;
129 WApplication *oapp = NULL, *napp = NULL;
130 int wasfocused;
132 if (scr->flags.ignore_focus_events || compareTimes(w_global.timestamp.focus_change, timestamp) > 0)
133 return;
135 if (!old_scr)
136 old_scr = scr;
138 old_focused = old_scr->focused_window;
140 w_global.timestamp.focus_change = timestamp;
142 if (old_focused)
143 oapp = wApplicationOf(old_focused->main_window);
145 if (wwin == NULL) {
146 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
147 if (old_focused)
148 wWindowUnfocus(old_focused);
150 if (oapp) {
151 wAppMenuUnmap(oapp->menu);
152 if (wPreferences.highlight_active_app)
153 wApplicationDeactivate(oapp);
156 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
157 return;
160 if (old_scr != scr && old_focused)
161 wWindowUnfocus(old_focused);
163 wasfocused = wwin->flags.focused;
164 napp = wApplicationOf(wwin->main_window);
166 /* remember last workspace where the app has been */
167 if (napp)
168 napp->last_workspace = wwin->frame->workspace;
170 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
171 /* install colormap if colormap mode is lock mode */
172 if (wPreferences.colormap_mode == WCM_CLICK)
173 wColormapInstallForWindow(scr, wwin);
175 /* set input focus */
176 switch (wwin->focus_mode) {
177 case WFM_NO_INPUT:
178 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
179 break;
180 case WFM_PASSIVE:
181 case WFM_LOCALLY_ACTIVE:
182 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
183 break;
184 case WFM_GLOBALLY_ACTIVE:
185 break;
188 XFlush(dpy);
189 if (wwin->protocols.TAKE_FOCUS)
190 wClientSendProtocol(wwin, w_global.atom.wm.take_focus, timestamp);
192 XSync(dpy, False);
193 } else {
194 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
197 if (WFLAGP(wwin, no_focusable))
198 return;
200 /* if this is not the focused window focus it */
201 if (focused != wwin) {
202 /* change the focus window list order */
203 if (wwin->prev)
204 wwin->prev->next = wwin->next;
206 if (wwin->next)
207 wwin->next->prev = wwin->prev;
209 wwin->prev = focused;
210 focused->next = wwin;
211 wwin->next = NULL;
212 scr->focused_window = wwin;
214 if (oapp && oapp != napp) {
215 wAppMenuUnmap(oapp->menu);
216 if (wPreferences.highlight_active_app)
217 wApplicationDeactivate(oapp);
221 wWindowFocus(wwin, focused);
223 if (napp && !wasfocused) {
224 #ifdef USER_MENU
225 wUserMenuRefreshInstances(napp->menu, wwin);
226 #endif /* USER_MENU */
228 /* kix: Only menu map with mouse, not alt+tab! */
229 if (wwin->flags.mapped)
230 wAppMenuMap(napp->menu, wwin);
232 if (napp && wPreferences.highlight_active_app)
233 wApplicationActivate(napp);
235 XFlush(dpy);
236 old_scr = scr;
239 void wShadeWindow(WWindow *wwin)
242 if (wwin->flags.shaded)
243 return;
245 XLowerWindow(dpy, wwin->client_win);
246 shade_animate(wwin, SHADE);
248 wwin->flags.skip_next_animation = 0;
249 wwin->flags.shaded = 1;
250 wwin->flags.mapped = 0;
251 /* prevent window withdrawal when getting UnmapNotify */
252 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
253 XUnmapWindow(dpy, wwin->client_win);
254 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
256 /* for the client it's just like iconification */
257 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
259 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
260 wWindowSynthConfigureNotify(wwin);
263 wClientSetState(wwin, IconicState, None);
266 WMPostNotificationName(WMNChangedState, wwin, "shade");
268 #ifdef USE_ANIMATIONS
269 if (!wwin->screen_ptr->flags.startup) {
270 /* Catch up with events not processed while animation was running */
271 ProcessPendingEvents();
273 #endif
276 void wUnshadeWindow(WWindow *wwin)
279 if (!wwin->flags.shaded)
280 return;
282 wwin->flags.shaded = 0;
283 wwin->flags.mapped = 1;
284 XMapWindow(dpy, wwin->client_win);
286 shade_animate(wwin, UNSHADE);
288 wwin->flags.skip_next_animation = 0;
289 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
290 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
292 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
293 wWindowSynthConfigureNotify(wwin);
295 /* if the window is focused, set the focus again as it was disabled during
296 * shading */
297 if (wwin->flags.focused)
298 wSetFocusTo(wwin->screen_ptr, wwin);
300 WMPostNotificationName(WMNChangedState, wwin, "shade");
303 /* Set the old coordinates using the current values */
304 static void save_old_geometry(WWindow *wwin, int directions)
306 /* never been saved? */
307 if (!wwin->old_geometry.width)
308 directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
309 if (!wwin->old_geometry.height)
310 directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
312 if (directions & SAVE_GEOMETRY_X)
313 wwin->old_geometry.x = wwin->frame_x;
314 if (directions & SAVE_GEOMETRY_Y)
315 wwin->old_geometry.y = wwin->frame_y;
316 if (directions & SAVE_GEOMETRY_WIDTH)
317 wwin->old_geometry.width = wwin->client.width;
318 if (directions & SAVE_GEOMETRY_HEIGHT)
319 wwin->old_geometry.height = wwin->client.height;
322 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
324 WMRect old_geom_rect;
325 int old_head;
326 Bool same_head;
328 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
329 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
330 same_head = (wGetHeadForWindow(wwin) == old_head);
331 *x = ((wwin->old_geometry.x || wwin->old_geometry.width) && same_head) ? wwin->old_geometry.x : wwin->frame_x;
332 *y = ((wwin->old_geometry.y || wwin->old_geometry.height) && same_head) ? wwin->old_geometry.y : wwin->frame_y;
333 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
334 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
337 /* Remember geometry for unmaximizing */
338 void update_saved_geometry(WWindow *wwin)
340 /* NOT if we aren't already maximized
341 * we'll save geometry when maximizing */
342 if (!wwin->flags.maximized)
343 return;
345 /* NOT if we are fully maximized */
346 if ((wwin->flags.maximized & MAX_MAXIMUS) ||
347 ((wwin->flags.maximized & MAX_HORIZONTAL) &&
348 (wwin->flags.maximized & MAX_VERTICAL)))
349 return;
351 /* save the co-ordinate in the axis in which we AREN'T maximized */
352 if (wwin->flags.maximized & MAX_HORIZONTAL)
353 save_old_geometry(wwin, SAVE_GEOMETRY_Y);
354 if (wwin->flags.maximized & MAX_VERTICAL)
355 save_old_geometry(wwin, SAVE_GEOMETRY_X);
358 void wMaximizeWindow(WWindow *wwin, int directions)
360 unsigned int new_width, new_height, half_scr_width, half_scr_height;
361 int new_x = 0;
362 int new_y = 0;
363 int maximus_x = 0;
364 int maximus_y = 0;
365 unsigned int maximus_width = 0;
366 unsigned int maximus_height = 0;
367 WArea usableArea, totalArea;
368 Bool has_border = 1;
369 int adj_size;
370 WScreen *scr = wwin->screen_ptr;
372 if (!IS_RESIZABLE(wwin))
373 return;
375 if (!HAS_BORDER(wwin))
376 has_border = 0;
378 if (wPreferences.drag_maximized_window == DRAGMAX_NOMOVE)
379 wwin->client_flags.no_movable = 1;
381 /* the size to adjust the geometry */
382 adj_size = scr->frame_border_width * 2 * has_border;
384 /* save old coordinates before we change the current values */
385 if (!wwin->flags.maximized)
386 save_old_geometry(wwin, SAVE_GEOMETRY_ALL);
388 totalArea.x2 = scr->scr_width;
389 totalArea.y2 = scr->scr_height;
390 totalArea.x1 = 0;
391 totalArea.y1 = 0;
392 usableArea = totalArea;
394 if (!(directions & MAX_IGNORE_XINERAMA)) {
395 WScreen *scr = wwin->screen_ptr;
396 int head;
398 if (directions & MAX_KEYBOARD)
399 head = wGetHeadForWindow(wwin);
400 else
401 head = wGetHeadForPointerLocation(scr);
403 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
407 /* Only save directions, not kbd or xinerama hints */
408 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
410 if (WFLAGP(wwin, full_maximize))
411 usableArea = totalArea;
412 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
413 half_scr_height = (usableArea.y2 - usableArea.y1)/2;
415 if (wwin->flags.shaded) {
416 wwin->flags.skip_next_animation = 1;
417 wUnshadeWindow(wwin);
420 if (directions & MAX_MAXIMUS) {
421 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
422 new_width = maximus_width - adj_size;
423 new_height = maximus_height - adj_size;
424 new_x = maximus_x;
425 new_y = maximus_y;
426 if (WFLAGP(wwin, full_maximize) && (new_y == 0)) {
427 new_height += wwin->frame->bottom_width - 1;
428 new_y -= wwin->frame->top_width;
431 wwin->maximus_x = new_x;
432 wwin->maximus_y = new_y;
433 wwin->flags.old_maximized |= MAX_MAXIMUS;
434 } else {
435 /* set default values if no option set then */
436 if (!(directions & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS))) {
437 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
438 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
440 if (!(directions & (MAX_VERTICAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS))) {
441 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
442 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
445 /* left|right position */
446 if (directions & MAX_LEFTHALF) {
447 new_width = half_scr_width - adj_size;
448 new_x = usableArea.x1;
449 } else if (directions & MAX_RIGHTHALF) {
450 new_width = half_scr_width - adj_size;
451 new_x = usableArea.x1 + half_scr_width;
453 /* top|bottom position */
454 if (directions & MAX_TOPHALF) {
455 new_height = half_scr_height - adj_size;
456 new_y = usableArea.y1;
457 } else if (directions & MAX_BOTTOMHALF) {
458 new_height = half_scr_height - adj_size;
459 new_y = usableArea.y1 + half_scr_height;
462 /* vertical|horizontal position */
463 if (directions & MAX_HORIZONTAL) {
464 new_width = usableArea.x2 - usableArea.x1 - adj_size;
465 new_x = usableArea.x1;
467 if (directions & MAX_VERTICAL) {
468 new_height = usableArea.y2 - usableArea.y1 - adj_size;
469 new_y = usableArea.y1;
470 if (WFLAGP(wwin, full_maximize) && (new_y == 0))
471 new_y -= wwin->frame->top_width;
475 if (!WFLAGP(wwin, full_maximize) && !(directions == MAX_MAXIMUS || directions == MAX_HORIZONTAL))
476 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
478 /* set maximization state */
479 wwin->flags.maximized = directions;
480 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
481 wwin->flags.maximized = MAX_MAXIMUS;
483 wWindowConstrainSize(wwin, &new_width, &new_height);
485 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
486 usableArea.y2 - usableArea.y1, &new_width, &new_height);
488 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
489 wWindowSynthConfigureNotify(wwin);
491 WMPostNotificationName(WMNChangedState, wwin, "maximize");
494 /* generic (un)maximizer */
495 void handleMaximize(WWindow *wwin, int directions)
497 int current = wwin->flags.maximized;
498 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
499 int effective = requested ^ current;
500 int flags = directions & ~requested;
502 if (!effective) {
503 /* allow wMaximizeWindow to restore the Maximusized size */
504 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
505 !(requested & MAX_MAXIMUS))
506 wMaximizeWindow(wwin, MAX_MAXIMUS | flags);
507 else
508 wUnmaximizeWindow(wwin);
509 /* these alone mean vertical|horizontal toggle */
510 } else if ((effective == MAX_LEFTHALF) ||
511 (effective == MAX_RIGHTHALF) ||
512 (effective == MAX_TOPHALF) ||
513 (effective == MAX_BOTTOMHALF))
514 wUnmaximizeWindow(wwin);
515 else {
516 if ((requested == (MAX_HORIZONTAL | MAX_VERTICAL)) ||
517 (requested == MAX_MAXIMUS))
518 effective = requested;
519 else {
520 if (requested & MAX_LEFTHALF) {
521 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
522 effective |= MAX_VERTICAL;
523 else
524 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
525 effective |= MAX_LEFTHALF;
526 effective &= ~(MAX_HORIZONTAL | MAX_RIGHTHALF);
527 } else if (requested & MAX_RIGHTHALF) {
528 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
529 effective |= MAX_VERTICAL;
530 else
531 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
532 effective |= MAX_RIGHTHALF;
533 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF);
535 if (requested & MAX_TOPHALF) {
536 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
537 effective |= MAX_HORIZONTAL;
538 else
539 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
540 effective |= MAX_TOPHALF;
541 effective &= ~(MAX_VERTICAL | MAX_BOTTOMHALF);
542 } else if (requested & MAX_BOTTOMHALF) {
543 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
544 effective |= MAX_HORIZONTAL;
545 else
546 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
547 effective |= MAX_BOTTOMHALF;
548 effective &= ~(MAX_VERTICAL | MAX_TOPHALF);
550 if (requested & MAX_HORIZONTAL)
551 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
552 if (requested & MAX_VERTICAL)
553 effective &= ~(MAX_TOPHALF | MAX_BOTTOMHALF);
554 effective &= ~MAX_MAXIMUS;
556 wMaximizeWindow(wwin, effective | flags);
560 /* the window boundary coordinates */
561 typedef struct {
562 int left;
563 int right;
564 int bottom;
565 int top;
566 int width;
567 int height;
568 } win_coords;
570 static void set_window_coords(WWindow *wwin, win_coords *obs)
572 obs->left = wwin->frame_x;
573 obs->top = wwin->frame_y;
574 obs->width = wwin->frame->core->width;
575 obs->height = wwin->frame->core->height;
576 obs->bottom = obs->top + obs->height;
577 obs->right = obs->left + obs->width;
581 * Maximus: tiled maximization (maximize without overlapping other windows)
583 * The original window 'orig' will be maximized to new coordinates 'new'.
584 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
586 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
587 unsigned int *new_width, unsigned int *new_height)
589 WWindow *tmp;
590 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
591 short int adjust_height;
592 int x_intsect, y_intsect;
593 /* the obstructing, original and new windows */
594 win_coords obs, orig, new;
596 /* set the original coordinate positions of the window to be Maximumized */
597 if (wwin->flags.maximized) {
598 /* window is already maximized; consider original geometry */
599 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
600 orig.bottom = orig.top + orig.height;
601 orig.right = orig.left + orig.width;
602 } else
603 set_window_coords(wwin, &orig);
605 /* Try to fully maximize first, then readjust later */
606 new.left = usableArea.x1;
607 new.right = usableArea.x2;
608 new.top = usableArea.y1;
609 new.bottom = usableArea.y2;
611 if (HAS_TITLEBAR(wwin))
612 tbar_height_0 = TITLEBAR_HEIGHT;
613 if (HAS_RESIZEBAR(wwin))
614 rbar_height_0 = RESIZEBAR_HEIGHT;
615 if (HAS_BORDER(wwin))
616 bd_width_0 = wwin->screen_ptr->frame_border_width;
618 /* the length to be subtracted if the window has titlebar, etc */
619 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
621 tmp = wwin;
622 /* The focused window is always the last in the list */
623 while (tmp->prev) {
624 /* ignore windows in other workspaces etc */
625 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
626 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
627 tmp = tmp->prev;
628 continue;
630 tmp = tmp->prev;
632 /* Set the coordinates of obstructing window */
633 set_window_coords(tmp, &obs);
635 /* Try to maximize in the y direction first */
636 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
637 if (x_intsect != 0) {
638 /* TODO: Consider the case when coords are equal */
639 if (obs.bottom < orig.top && obs.bottom > new.top) {
640 /* w_0 is below the bottom of w_j */
641 new.top = obs.bottom + 1;
643 if (orig.bottom < obs.top && obs.top < new.bottom) {
644 /* The bottom of w_0 is above the top of w_j */
645 new.bottom = obs.top - 1;
650 tmp = wwin;
651 while (tmp->prev) {
652 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
653 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
654 tmp = tmp->prev;
655 continue;
657 tmp = tmp->prev;
659 set_window_coords(tmp, &obs);
662 * Use the new.top and new.height instead of original values
663 * as they may have different intersections with the obstructing windows
665 new.height = new.bottom - new.top - adjust_height;
666 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
667 if (y_intsect != 0) {
668 if (obs.right < orig.left && obs.right > new.left) {
669 /* w_0 is completely to the right of w_j */
670 new.left = obs.right + 1;
672 if (orig.right < obs.left && obs.left < new.right) {
673 /* w_0 is completely to the left of w_j */
674 new.right = obs.left - 1;
679 *new_x = new.left;
680 *new_y = new.top;
681 /* xcalc needs -7 here, but other apps don't */
682 *new_height = new.bottom - new.top - adjust_height - 1;
683 *new_width = new.right - new.left;
686 void wUnmaximizeWindow(WWindow *wwin)
688 int x, y, w, h;
690 if (!wwin->flags.maximized)
691 return;
693 if (wwin->flags.shaded) {
694 wwin->flags.skip_next_animation = 1;
695 wUnshadeWindow(wwin);
698 if (wPreferences.drag_maximized_window == DRAGMAX_NOMOVE)
699 wwin->client_flags.no_movable = 0;
701 /* Use old coordinates if they are set, current values otherwise */
702 remember_geometry(wwin, &x, &y, &w, &h);
704 /* unMaximusize relative to original position */
705 if (wwin->flags.maximized & MAX_MAXIMUS) {
706 x += wwin->frame_x - wwin->maximus_x;
707 y += wwin->frame_y - wwin->maximus_y;
710 wwin->flags.maximized = 0;
711 wwin->flags.old_maximized = 0;
712 wWindowConfigure(wwin, x, y, w, h);
713 wWindowSynthConfigureNotify(wwin);
715 WMPostNotificationName(WMNChangedState, wwin, "maximize");
718 void wFullscreenWindow(WWindow *wwin)
720 int head;
721 WMRect rect;
723 if (wwin->flags.fullscreen)
724 return;
726 wwin->flags.fullscreen = True;
728 wWindowConfigureBorders(wwin);
730 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
731 wRaiseFrame(wwin->frame->core);
733 wwin->bfs_geometry.x = wwin->frame_x;
734 wwin->bfs_geometry.y = wwin->frame_y;
735 wwin->bfs_geometry.width = wwin->frame->core->width;
736 wwin->bfs_geometry.height = wwin->frame->core->height;
738 head = wGetHeadForWindow(wwin);
739 rect = wGetRectForHead(wwin->screen_ptr, head);
740 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
742 wwin->screen_ptr->bfs_focused_window = wwin->screen_ptr->focused_window;
743 wSetFocusTo(wwin->screen_ptr, wwin);
745 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
748 void wUnfullscreenWindow(WWindow *wwin)
750 if (!wwin->flags.fullscreen)
751 return;
753 wwin->flags.fullscreen = False;
755 if (WFLAGP(wwin, sunken))
756 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
757 else if (WFLAGP(wwin, floating))
758 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
760 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
761 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
763 wWindowConfigureBorders(wwin);
765 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
766 wFrameWindowPaint(wwin->frame);
769 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
771 if (wwin->screen_ptr->bfs_focused_window) {
772 wSetFocusTo(wwin->screen_ptr, wwin->screen_ptr->bfs_focused_window);
773 wwin->screen_ptr->bfs_focused_window = NULL;
777 #ifdef USE_ANIMATIONS
778 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
780 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
781 float cx, cy, cw, ch;
782 float xstep, ystep, wstep, hstep;
783 XPoint points[5];
784 float dx, dch, midy;
785 float angle, final_angle, delta;
787 xstep = (float)(fx - x) / steps;
788 ystep = (float)(fy - y) / steps;
789 wstep = (float)(fw - w) / steps;
790 hstep = (float)(fh - h) / steps;
792 cx = (float)x;
793 cy = (float)y;
794 cw = (float)w;
795 ch = (float)h;
797 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
798 delta = (float)(final_angle / FRAMES);
799 for (angle = 0;; angle += delta) {
800 if (angle > final_angle)
801 angle = final_angle;
803 dx = (cw / 10) - ((cw / 5) * sinf(angle));
804 dch = (ch / 2) * cosf(angle);
805 midy = cy + (ch / 2);
807 points[0].x = cx + dx;
808 points[0].y = midy - dch;
809 points[1].x = cx + cw - dx;
810 points[1].y = points[0].y;
811 points[2].x = cx + cw + dx;
812 points[2].y = midy + dch;
813 points[3].x = cx - dx;
814 points[3].y = points[2].y;
815 points[4].x = points[0].x;
816 points[4].y = points[0].y;
818 XGrabServer(dpy);
819 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
820 XFlush(dpy);
821 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
823 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
824 XUngrabServer(dpy);
825 cx += xstep;
826 cy += ystep;
827 cw += wstep;
828 ch += hstep;
829 if (angle >= final_angle)
830 break;
833 XFlush(dpy);
836 #undef FRAMES
838 static void
839 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
841 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
842 float cx, cy, cw, ch;
843 float xstep, ystep, wstep, hstep;
844 XPoint points[5];
845 float angle, final_angle, a, d, delta;
847 x += w / 2;
848 y += h / 2;
849 fx += fw / 2;
850 fy += fh / 2;
852 xstep = (float)(fx - x) / steps;
853 ystep = (float)(fy - y) / steps;
854 wstep = (float)(fw - w) / steps;
855 hstep = (float)(fh - h) / steps;
857 cx = (float)x;
858 cy = (float)y;
859 cw = (float)w;
860 ch = (float)h;
862 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
863 delta = (float)(final_angle / FRAMES);
864 for (angle = 0;; angle += delta) {
865 if (angle > final_angle)
866 angle = final_angle;
868 a = atan2f(ch, cw);
869 d = sqrtf((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
871 points[0].x = cx + cosf(angle - a) * d;
872 points[0].y = cy + sinf(angle - a) * d;
873 points[1].x = cx + cosf(angle + a) * d;
874 points[1].y = cy + sinf(angle + a) * d;
875 points[2].x = cx + cosf(angle - a + (float)WM_PI) * d;
876 points[2].y = cy + sinf(angle - a + (float)WM_PI) * d;
877 points[3].x = cx + cosf(angle + a + (float)WM_PI) * d;
878 points[3].y = cy + sinf(angle + a + (float)WM_PI) * d;
879 points[4].x = cx + cosf(angle - a) * d;
880 points[4].y = cy + sinf(angle - a) * d;
881 XGrabServer(dpy);
882 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
883 XFlush(dpy);
884 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
886 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
887 XUngrabServer(dpy);
888 cx += xstep;
889 cy += ystep;
890 cw += wstep;
891 ch += hstep;
892 if (angle >= final_angle)
893 break;
896 XFlush(dpy);
899 #undef FRAMES
901 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
903 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
904 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
905 float xstep, ystep, wstep, hstep;
906 int i, j;
908 xstep = (float)(fx - x) / steps;
909 ystep = (float)(fy - y) / steps;
910 wstep = (float)(fw - w) / steps;
911 hstep = (float)(fh - h) / steps;
913 for (j = 0; j < FRAMES; j++) {
914 cx[j] = (float)x;
915 cy[j] = (float)y;
916 cw[j] = (float)w;
917 ch[j] = (float)h;
919 XGrabServer(dpy);
920 for (i = 0; i < steps; i++) {
921 for (j = 0; j < FRAMES; j++) {
922 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
923 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
925 XFlush(dpy);
926 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
928 for (j = 0; j < FRAMES; j++) {
929 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
930 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
931 if (j < FRAMES - 1) {
932 cx[j] = cx[j + 1];
933 cy[j] = cy[j + 1];
934 cw[j] = cw[j + 1];
935 ch[j] = ch[j + 1];
936 } else {
937 cx[j] += xstep;
938 cy[j] += ystep;
939 cw[j] += wstep;
940 ch[j] += hstep;
945 for (j = 0; j < FRAMES; j++)
946 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
947 XFlush(dpy);
948 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
950 for (j = 0; j < FRAMES; j++)
951 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
953 XUngrabServer(dpy);
956 #undef FRAMES
958 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
960 int style = wPreferences.iconification_style; /* Catch the value */
961 int steps;
963 if (style == WIS_NONE)
964 return;
966 if (style == WIS_RANDOM)
967 style = rand() % 3;
969 switch (style) {
970 case WIS_TWIST:
971 steps = MINIATURIZE_ANIMATION_STEPS_T;
972 if (steps > 0)
973 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
974 break;
975 case WIS_FLIP:
976 steps = MINIATURIZE_ANIMATION_STEPS_F;
977 if (steps > 0)
978 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
979 break;
980 case WIS_ZOOM:
981 default:
982 steps = MINIATURIZE_ANIMATION_STEPS_Z;
983 if (steps > 0)
984 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
985 break;
988 #endif /* USE_ANIMATIONS */
990 static void flushExpose(void)
992 XEvent tmpev;
994 while (XCheckTypedEvent(dpy, Expose, &tmpev))
995 WMHandleEvent(&tmpev);
996 XSync(dpy, 0);
999 static void unmapTransientsFor(WWindow *wwin)
1001 WWindow *tmp;
1003 tmp = wwin->screen_ptr->focused_window;
1004 while (tmp) {
1005 /* unmap the transients for this transient */
1006 if (tmp != wwin && tmp->transient_for == wwin->client_win
1007 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
1008 unmapTransientsFor(tmp);
1009 tmp->flags.miniaturized = 1;
1010 if (!tmp->flags.shaded)
1011 wWindowUnmap(tmp);
1012 else
1013 XUnmapWindow(dpy, tmp->frame->core->window);
1015 if (!tmp->flags.shaded)
1017 wClientSetState(tmp, IconicState, None);
1019 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1021 tmp = tmp->prev;
1025 static void mapTransientsFor(WWindow *wwin)
1027 WWindow *tmp;
1029 tmp = wwin->screen_ptr->focused_window;
1030 while (tmp) {
1031 /* recursively map the transients for this transient */
1032 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1033 && tmp->icon == NULL) {
1034 mapTransientsFor(tmp);
1035 tmp->flags.miniaturized = 0;
1036 if (!tmp->flags.shaded)
1037 wWindowMap(tmp);
1038 else
1039 XMapWindow(dpy, tmp->frame->core->window);
1040 tmp->flags.semi_focused = 0;
1042 if (!tmp->flags.shaded)
1044 wClientSetState(tmp, NormalState, None);
1046 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1048 tmp = tmp->prev;
1052 static WWindow *recursiveTransientFor(WWindow *wwin)
1054 int i;
1056 if (!wwin)
1057 return None;
1059 /* hackish way to detect transient_for cycle */
1060 i = wwin->screen_ptr->window_count + 1;
1062 while (wwin && wwin->transient_for != None && i > 0) {
1063 wwin = wWindowFor(wwin->transient_for);
1064 i--;
1066 if (i == 0 && wwin) {
1067 wwarning(_("window \"%s\" has a severely broken WM_TRANSIENT_FOR hint"),
1068 wwin->frame->title);
1069 return NULL;
1072 return wwin;
1075 #ifdef USE_ANIMATIONS
1076 static int getAnimationGeometry(WWindow *wwin, int *ix, int *iy, int *iw, int *ih)
1078 if (wwin->screen_ptr->flags.startup || wPreferences.no_animations
1079 || wwin->flags.skip_next_animation || wwin->icon == NULL)
1080 return 0;
1082 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1083 *ix = wwin->icon_x;
1084 *iy = wwin->icon_y;
1085 *iw = wwin->icon->core->width;
1086 *ih = wwin->icon->core->height;
1087 } else {
1088 if (wwin->flags.net_handle_icon) {
1089 *ix = wwin->icon_x;
1090 *iy = wwin->icon_y;
1091 *iw = wwin->icon_w;
1092 *ih = wwin->icon_h;
1093 } else {
1094 *ix = 0;
1095 *iy = 0;
1096 *iw = wwin->screen_ptr->scr_width;
1097 *ih = wwin->screen_ptr->scr_height;
1100 return 1;
1102 #endif /* USE_ANIMATIONS */
1104 void wIconifyWindow(WWindow *wwin)
1106 XWindowAttributes attribs;
1107 int present;
1109 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1110 return; /* the window doesn't exist anymore */
1112 if (wwin->flags.miniaturized)
1113 return; /* already miniaturized */
1115 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1116 WWindow *owner = wWindowFor(wwin->transient_for);
1118 if (owner && owner->flags.miniaturized)
1119 return;
1122 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1124 /* if the window is in another workspace, simplify process */
1125 if (present) {
1126 /* icon creation may take a while */
1127 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1128 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1129 GrabModeAsync, None, None, CurrentTime);
1132 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1133 if (!wwin->flags.icon_moved)
1134 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1136 wwin->icon = icon_create_for_wwindow(wwin);
1137 wwin->icon->mapped = 1;
1139 /* extract the window screenshot every time, as the option can be enable anytime */
1140 if (wwin->client_win && wwin->flags.mapped) {
1141 RImage *mini_preview;
1142 XImage *pimg;
1143 unsigned int w, h;
1144 int x, y;
1145 Window baz;
1147 XRaiseWindow(dpy, wwin->frame->core->window);
1148 XTranslateCoordinates(dpy, wwin->client_win, wwin->screen_ptr->root_win, 0, 0, &x, &y, &baz);
1150 w = attribs.width;
1151 h = attribs.height;
1153 if (x - attribs.x + attribs.width > wwin->screen_ptr->scr_width)
1154 w = wwin->screen_ptr->scr_width - x + attribs.x;
1156 if (y - attribs.y + attribs.height > wwin->screen_ptr->scr_height)
1157 h = wwin->screen_ptr->scr_height - y + attribs.y;
1159 pimg = XGetImage(dpy, wwin->client_win, 0, 0, w, h, AllPlanes, ZPixmap);
1160 if (pimg) {
1161 mini_preview = RCreateImageFromXImage(wwin->screen_ptr->rcontext, pimg, NULL);
1162 XDestroyImage(pimg);
1164 if (mini_preview) {
1165 set_icon_minipreview(wwin->icon, mini_preview);
1166 RReleaseImage(mini_preview);
1167 } else {
1168 const char *title;
1169 char title_buf[32];
1171 if (wwin->frame->title) {
1172 title = wwin->frame->title;
1173 } else {
1174 snprintf(title_buf, sizeof(title_buf), "(id=0x%lx)", wwin->client_win);
1175 title = title_buf;
1177 wwarning(_("creation of mini-preview failed for window \"%s\""), title);
1183 wwin->flags.miniaturized = 1;
1184 wwin->flags.mapped = 0;
1186 /* unmap transients */
1187 unmapTransientsFor(wwin);
1189 if (present) {
1190 #ifdef USE_ANIMATIONS
1191 int ix, iy, iw, ih;
1192 #endif
1193 XUngrabPointer(dpy, CurrentTime);
1194 wWindowUnmap(wwin);
1195 /* let all Expose events arrive so that we can repaint
1196 * something before the animation starts (and the server is grabbed) */
1197 XSync(dpy, 0);
1199 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1200 wClientSetState(wwin, IconicState, None);
1201 else
1202 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1204 flushExpose();
1205 #ifdef USE_ANIMATIONS
1206 if (getAnimationGeometry(wwin, &ix, &iy, &iw, &ih))
1207 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1208 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1209 #endif
1212 wwin->flags.skip_next_animation = 0;
1214 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1215 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1216 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1217 XMapWindow(dpy, wwin->icon->core->window);
1219 AddToStackList(wwin->icon->core);
1220 wLowerFrame(wwin->icon->core);
1223 if (present) {
1224 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1227 * It doesn't seem to be working and causes button event hangup
1228 * when deiconifying a transient window.
1229 setupIconGrabs(wwin->icon);
1231 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1232 && wPreferences.focus_mode == WKF_CLICK) {
1233 WWindow *tmp;
1235 tmp = wwin->prev;
1236 while (tmp) {
1237 if (!WFLAGP(tmp, no_focusable)
1238 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1239 && (wwin->frame->workspace == tmp->frame->workspace))
1240 break;
1241 tmp = tmp->prev;
1243 wSetFocusTo(wwin->screen_ptr, tmp);
1244 } else if (wPreferences.focus_mode != WKF_CLICK) {
1245 wSetFocusTo(wwin->screen_ptr, NULL);
1247 #ifdef USE_ANIMATIONS
1248 if (!wwin->screen_ptr->flags.startup) {
1249 /* Catch up with events not processed while animation was running */
1250 Window clientwin = wwin->client_win;
1252 ProcessPendingEvents();
1254 /* the window can disappear while ProcessPendingEvents() runs */
1255 if (!wWindowFor(clientwin))
1256 return;
1258 #endif
1261 /* maybe we want to do this regardless of net_handle_icon
1262 * it seems to me we might break behaviour this way.
1264 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1265 && !wwin->flags.net_handle_icon)
1266 wIconSelect(wwin->icon);
1268 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1270 if (wPreferences.auto_arrange_icons)
1271 wArrangeIcons(wwin->screen_ptr, True);
1274 void wDeiconifyWindow(WWindow *wwin)
1276 /* Let's avoid changing workspace while deiconifying */
1277 w_global.ignore_workspace_change = True;
1279 /* we're hiding for show_desktop */
1280 int netwm_hidden = wwin->flags.net_show_desktop &&
1281 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1283 if (!netwm_hidden)
1284 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1286 if (!wwin->flags.miniaturized) {
1287 w_global.ignore_workspace_change = False;
1288 return;
1291 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1292 WWindow *owner = recursiveTransientFor(wwin);
1294 if (owner && owner->flags.miniaturized) {
1295 wDeiconifyWindow(owner);
1296 wSetFocusTo(wwin->screen_ptr, wwin);
1297 wRaiseFrame(wwin->frame->core);
1298 w_global.ignore_workspace_change = False;
1299 return;
1303 wwin->flags.miniaturized = 0;
1305 if (!netwm_hidden && !wwin->flags.shaded)
1306 wwin->flags.mapped = 1;
1308 if (!netwm_hidden || wPreferences.sticky_icons) {
1309 /* maybe we want to do this regardless of net_handle_icon
1310 * it seems to me we might break behaviour this way.
1312 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1313 && wwin->icon != NULL) {
1314 if (wwin->icon->selected)
1315 wIconSelect(wwin->icon);
1317 XUnmapWindow(dpy, wwin->icon->core->window);
1321 /* if the window is in another workspace, do it silently */
1322 if (!netwm_hidden) {
1323 #ifdef USE_ANIMATIONS
1324 int ix, iy, iw, ih;
1325 if (getAnimationGeometry(wwin, &ix, &iy, &iw, &ih))
1326 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1327 wwin->frame_x, wwin->frame_y,
1328 wwin->frame->core->width, wwin->frame->core->height);
1329 #endif
1330 wwin->flags.skip_next_animation = 0;
1331 XGrabServer(dpy);
1332 if (!wwin->flags.shaded)
1333 XMapWindow(dpy, wwin->client_win);
1335 XMapWindow(dpy, wwin->frame->core->window);
1336 wRaiseFrame(wwin->frame->core);
1337 if (!wwin->flags.shaded)
1338 wClientSetState(wwin, NormalState, None);
1340 mapTransientsFor(wwin);
1343 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1344 && !wwin->flags.net_handle_icon) {
1345 RemoveFromStackList(wwin->icon->core);
1346 wSetFocusTo(wwin->screen_ptr, wwin);
1347 wIconDestroy(wwin->icon);
1348 wwin->icon = NULL;
1351 if (!netwm_hidden) {
1352 XUngrabServer(dpy);
1354 wSetFocusTo(wwin->screen_ptr, wwin);
1356 #ifdef USE_ANIMATIONS
1357 if (!wwin->screen_ptr->flags.startup) {
1358 /* Catch up with events not processed while animation was running */
1359 Window clientwin = wwin->client_win;
1361 ProcessPendingEvents();
1363 /* the window can disappear while ProcessPendingEvents() runs */
1364 if (!wWindowFor(clientwin)) {
1365 w_global.ignore_workspace_change = False;
1366 return;
1369 #endif
1372 if (wPreferences.auto_arrange_icons)
1373 wArrangeIcons(wwin->screen_ptr, True);
1375 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1377 /* In case we were shaded and iconified, also unshade */
1378 if (!netwm_hidden)
1379 wUnshadeWindow(wwin);
1381 w_global.ignore_workspace_change = False;
1384 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1386 if (wwin->flags.miniaturized) {
1387 if (wwin->icon) {
1388 XUnmapWindow(dpy, wwin->icon->core->window);
1389 wwin->icon->mapped = 0;
1391 wwin->flags.hidden = 1;
1393 WMPostNotificationName(WMNChangedState, wwin, "hide");
1394 return;
1397 if (wwin->flags.inspector_open)
1398 wHideInspectorForWindow(wwin);
1400 wwin->flags.hidden = 1;
1401 wWindowUnmap(wwin);
1403 wClientSetState(wwin, IconicState, icon->icon_win);
1404 flushExpose();
1406 #ifdef USE_ANIMATIONS
1407 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1408 !wwin->flags.skip_next_animation && animate) {
1409 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1410 wwin->frame->core->width, wwin->frame->core->height,
1411 icon_x, icon_y, icon->core->width, icon->core->height);
1413 #else
1414 /* Tell the compiler it is normal that those parameters are not used in this case */
1415 (void) icon_x;
1416 (void) icon_y;
1417 (void) animate;
1418 #endif
1419 wwin->flags.skip_next_animation = 0;
1421 WMPostNotificationName(WMNChangedState, wwin, "hide");
1424 void wHideAll(WScreen *scr)
1426 WWindow *wwin;
1427 WWindow **windows;
1428 WMenu *menu;
1429 unsigned int wcount = 0;
1430 int i;
1432 if (!scr)
1433 return;
1435 menu = scr->switch_menu;
1437 windows = wmalloc(sizeof(WWindow *));
1439 if (menu != NULL) {
1440 for (i = 0; i < menu->entry_no; i++) {
1441 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1442 wcount++;
1443 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1445 } else {
1446 wwin = scr->focused_window;
1448 while (wwin) {
1449 windows[wcount] = wwin;
1450 wcount++;
1451 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1452 wwin = wwin->prev;
1457 for (i = 0; i < wcount; i++) {
1458 wwin = windows[i];
1459 if (wwin->frame->workspace == scr->current_workspace
1460 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1461 && !wwin->flags.internal_window
1462 && !WFLAGP(wwin, no_miniaturizable)
1464 wwin->flags.skip_next_animation = 1;
1465 wIconifyWindow(wwin);
1469 wfree(windows);
1472 void wHideOtherApplications(WWindow *awin)
1474 WWindow *wwin;
1475 WApplication *tapp;
1477 if (!awin)
1478 return;
1479 wwin = awin->screen_ptr->focused_window;
1481 while (wwin) {
1482 if (wwin != awin
1483 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1484 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1485 && !wwin->flags.internal_window
1486 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1488 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1489 if (!WFLAGP(wwin, no_miniaturizable)) {
1490 wwin->flags.skip_next_animation = 1;
1491 wIconifyWindow(wwin);
1493 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1494 tapp = wApplicationOf(wwin->main_window);
1495 if (tapp) {
1496 tapp->flags.skip_next_animation = 1;
1497 wHideApplication(tapp);
1498 } else {
1499 if (!WFLAGP(wwin, no_miniaturizable)) {
1500 wwin->flags.skip_next_animation = 1;
1501 wIconifyWindow(wwin);
1506 wwin = wwin->prev;
1509 wSetFocusTo(awin->screen_ptr, awin);
1513 void wHideApplication(WApplication *wapp)
1515 WScreen *scr;
1516 WWindow *wlist;
1517 int hadfocus;
1518 int animate;
1520 if (!wapp) {
1521 wwarning("trying to hide a non grouped window");
1522 return;
1524 if (!wapp->main_window_desc) {
1525 wwarning("group leader not found for window group");
1526 return;
1528 scr = wapp->main_window_desc->screen_ptr;
1529 hadfocus = 0;
1530 wlist = scr->focused_window;
1531 if (!wlist)
1532 return;
1534 if (wlist->main_window == wapp->main_window)
1535 wapp->last_focused = wlist;
1536 else
1537 wapp->last_focused = NULL;
1539 animate = !wapp->flags.skip_next_animation;
1541 while (wlist) {
1542 if (wlist->main_window == wapp->main_window) {
1543 if (wlist->flags.focused)
1544 hadfocus = 1;
1545 if (wapp->app_icon) {
1546 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1547 wapp->app_icon->y_pos, wlist, animate);
1548 animate = False;
1551 wlist = wlist->prev;
1554 wapp->flags.skip_next_animation = 0;
1556 if (hadfocus) {
1557 if (wPreferences.focus_mode == WKF_CLICK) {
1558 wlist = scr->focused_window;
1559 while (wlist) {
1560 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1561 && (wlist->flags.mapped || wlist->flags.shaded))
1562 break;
1563 wlist = wlist->prev;
1565 wSetFocusTo(scr, wlist);
1566 } else {
1567 wSetFocusTo(scr, NULL);
1571 wapp->flags.hidden = 1;
1573 if (wPreferences.auto_arrange_icons)
1574 wArrangeIcons(scr, True);
1576 #ifdef HIDDENDOT
1577 if (wapp->app_icon)
1578 wAppIconPaint(wapp->app_icon);
1579 #endif
1582 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1584 if (bringToCurrentWS)
1585 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1587 wwin->flags.hidden = 0;
1589 #ifdef USE_ANIMATIONS
1590 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1591 animateResize(wwin->screen_ptr, icon_x, icon_y,
1592 icon->core->width, icon->core->height,
1593 wwin->frame_x, wwin->frame_y,
1594 wwin->frame->core->width, wwin->frame->core->height);
1596 #else
1597 /* Tell the compiler it is normal that those parameters are not used in this case */
1598 (void) icon;
1599 (void) icon_x;
1600 (void) icon_y;
1601 (void) animate;
1602 #endif
1603 wwin->flags.skip_next_animation = 0;
1604 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1605 XMapWindow(dpy, wwin->client_win);
1606 XMapWindow(dpy, wwin->frame->core->window);
1607 wClientSetState(wwin, NormalState, None);
1608 wwin->flags.mapped = 1;
1609 wRaiseFrame(wwin->frame->core);
1611 if (wwin->flags.inspector_open)
1612 wUnhideInspectorForWindow(wwin);
1614 WMPostNotificationName(WMNChangedState, wwin, "hide");
1617 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1619 WScreen *scr;
1620 WWindow *wlist, *next;
1621 WWindow *focused = NULL;
1622 int animate;
1624 if (!wapp)
1625 return;
1627 scr = wapp->main_window_desc->screen_ptr;
1628 wlist = scr->focused_window;
1629 if (!wlist)
1630 return;
1632 /* goto beginning of list */
1633 while (wlist->prev)
1634 wlist = wlist->prev;
1636 animate = !wapp->flags.skip_next_animation;
1638 while (wlist) {
1639 next = wlist->next;
1641 if (wlist->main_window == wapp->main_window) {
1642 if (wlist->flags.focused)
1643 focused = wlist;
1644 else if (!focused || !focused->flags.focused)
1645 focused = wlist;
1647 if (wlist->flags.miniaturized) {
1648 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1649 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1650 if (!wlist->icon->mapped) {
1651 int x, y;
1653 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1654 if (wlist->icon_x != x || wlist->icon_y != y)
1655 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1656 wlist->icon_x = x;
1657 wlist->icon_y = y;
1658 XMapWindow(dpy, wlist->icon->core->window);
1659 wlist->icon->mapped = 1;
1661 wRaiseFrame(wlist->icon->core);
1663 if (bringToCurrentWS)
1664 wWindowChangeWorkspace(wlist, scr->current_workspace);
1665 wlist->flags.hidden = 0;
1666 if (miniwindows && wlist->frame->workspace == scr->current_workspace)
1667 wDeiconifyWindow(wlist);
1668 WMPostNotificationName(WMNChangedState, wlist, "hide");
1669 } else if (wlist->flags.shaded) {
1670 if (bringToCurrentWS)
1671 wWindowChangeWorkspace(wlist, scr->current_workspace);
1672 wlist->flags.hidden = 0;
1673 wRaiseFrame(wlist->frame->core);
1674 if (wlist->frame->workspace == scr->current_workspace) {
1675 XMapWindow(dpy, wlist->frame->core->window);
1676 if (miniwindows)
1677 wUnshadeWindow(wlist);
1679 WMPostNotificationName(WMNChangedState, wlist, "hide");
1680 } else if (wlist->flags.hidden) {
1681 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1682 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1683 animate = False;
1684 } else {
1685 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace)
1686 wWindowChangeWorkspace(wlist, scr->current_workspace);
1688 wRaiseFrame(wlist->frame->core);
1691 wlist = next;
1694 wapp->flags.skip_next_animation = 0;
1695 wapp->flags.hidden = 0;
1697 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1698 wRaiseFrame(wapp->last_focused->frame->core);
1699 wSetFocusTo(scr, wapp->last_focused);
1700 } else if (focused) {
1701 wSetFocusTo(scr, focused);
1703 wapp->last_focused = NULL;
1704 if (wPreferences.auto_arrange_icons)
1705 wArrangeIcons(scr, True);
1707 #ifdef HIDDENDOT
1708 wAppIconPaint(wapp->app_icon);
1709 #endif
1712 void wShowAllWindows(WScreen *scr)
1714 WWindow *wwin, *old_foc;
1715 WApplication *wapp;
1717 old_foc = wwin = scr->focused_window;
1718 while (wwin) {
1719 if (!wwin->flags.internal_window &&
1720 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1721 if (wwin->flags.miniaturized) {
1722 wwin->flags.skip_next_animation = 1;
1723 wDeiconifyWindow(wwin);
1724 } else if (wwin->flags.hidden) {
1725 wapp = wApplicationOf(wwin->main_window);
1726 if (wapp) {
1727 wUnhideApplication(wapp, False, False);
1728 } else {
1729 wwin->flags.skip_next_animation = 1;
1730 wDeiconifyWindow(wwin);
1734 wwin = wwin->prev;
1736 wSetFocusTo(scr, old_foc);
1737 /*wRaiseFrame(old_foc->frame->core); */
1740 void wRefreshDesktop(WScreen *scr)
1742 Window win;
1743 XSetWindowAttributes attr;
1745 attr.backing_store = NotUseful;
1746 attr.save_under = False;
1747 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1748 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1749 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1750 XMapRaised(dpy, win);
1751 XDestroyWindow(dpy, win);
1752 XFlush(dpy);
1755 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1757 WWindow *wwin;
1758 WAppIcon *aicon;
1760 int head;
1761 const int heads = wXineramaHeads(scr);
1763 struct HeadVars {
1764 int pf; /* primary axis */
1765 int sf; /* secondary axis */
1766 int fullW;
1767 int fullH;
1768 int pi, si;
1769 int sx1, sx2, sy1, sy2; /* screen boundary */
1770 int sw, sh;
1771 int xo, yo;
1772 int xs, ys;
1773 } *vars;
1775 int isize = wPreferences.icon_size;
1777 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1779 for (head = 0; head < heads; ++head) {
1780 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1781 WMRect rect;
1783 if (scr->dock) {
1784 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
1786 if (scr->dock->on_right_side)
1787 area.x2 -= offset;
1788 else
1789 area.x1 += offset;
1792 rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1794 vars[head].pi = vars[head].si = 0;
1795 vars[head].sx1 = rect.pos.x;
1796 vars[head].sy1 = rect.pos.y;
1797 vars[head].sw = rect.size.width;
1798 vars[head].sh = rect.size.height;
1799 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1800 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1801 vars[head].sw = isize * (vars[head].sw / isize);
1802 vars[head].sh = isize * (vars[head].sh / isize);
1803 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1804 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1806 /* icon yard boundaries */
1807 if (wPreferences.icon_yard & IY_VERT) {
1808 vars[head].pf = vars[head].fullH;
1809 vars[head].sf = vars[head].fullW;
1810 } else {
1811 vars[head].pf = vars[head].fullW;
1812 vars[head].sf = vars[head].fullH;
1814 if (wPreferences.icon_yard & IY_RIGHT) {
1815 vars[head].xo = vars[head].sx2 - isize;
1816 vars[head].xs = -1;
1817 } else {
1818 vars[head].xo = vars[head].sx1;
1819 vars[head].xs = 1;
1821 if (wPreferences.icon_yard & IY_TOP) {
1822 vars[head].yo = vars[head].sy1;
1823 vars[head].ys = 1;
1824 } else {
1825 vars[head].yo = vars[head].sy2 - isize;
1826 vars[head].ys = -1;
1830 #define X ((wPreferences.icon_yard & IY_VERT) \
1831 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1832 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1834 #define Y ((wPreferences.icon_yard & IY_VERT) \
1835 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1836 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1838 /* arrange application icons */
1839 aicon = scr->app_icon_list;
1840 /* reverse them to avoid unnecessarily sliding of icons */
1841 while (aicon && aicon->next)
1842 aicon = aicon->next;
1844 while (aicon) {
1845 if (!aicon->docked) {
1846 /* CHECK: can icon be NULL here ? */
1847 /* The intention here is to place the AppIcon on the head that
1848 * contains most of the applications _main_ window. */
1849 head = wGetHeadForWindow(aicon->icon->owner);
1851 if (aicon->x_pos != X || aicon->y_pos != Y) {
1852 #ifdef USE_ANIMATIONS
1853 if (!wPreferences.no_animations)
1854 slide_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1855 #endif /* USE_ANIMATIONS */
1857 wAppIconMove(aicon, X, Y);
1858 vars[head].pi++;
1859 if (vars[head].pi >= vars[head].pf) {
1860 vars[head].pi = 0;
1861 vars[head].si++;
1864 aicon = aicon->prev;
1867 /* arrange miniwindows */
1868 wwin = scr->focused_window;
1869 /* reverse them to avoid unnecessarily shuffling */
1870 while (wwin && wwin->prev)
1871 wwin = wwin->prev;
1873 while (wwin) {
1874 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1875 (wwin->frame->workspace == scr->current_workspace ||
1876 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1878 head = wGetHeadForWindow(wwin);
1880 if (arrangeAll || !wwin->flags.icon_moved) {
1881 if (wwin->icon_x != X || wwin->icon_y != Y)
1882 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1884 wwin->icon_x = X;
1885 wwin->icon_y = Y;
1887 vars[head].pi++;
1888 if (vars[head].pi >= vars[head].pf) {
1889 vars[head].pi = 0;
1890 vars[head].si++;
1894 if (arrangeAll)
1895 wwin->flags.icon_moved = 0;
1896 /* we reversed the order, so we use next */
1897 wwin = wwin->next;
1900 wfree(vars);
1903 void wSelectWindow(WWindow *wwin, Bool flag)
1905 WScreen *scr = wwin->screen_ptr;
1907 if (flag) {
1908 wwin->flags.selected = 1;
1909 if (wwin->frame->selected_border_pixel)
1910 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1911 else
1912 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1914 if (!HAS_BORDER(wwin))
1915 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1917 if (!scr->selected_windows)
1918 scr->selected_windows = WMCreateArray(4);
1919 WMAddToArray(scr->selected_windows, wwin);
1920 } else {
1921 wwin->flags.selected = 0;
1922 if (wwin->flags.focused) {
1923 if (wwin->frame->focused_border_pixel)
1924 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->focused_border_pixel);
1925 else
1926 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_focused_border_pixel);
1927 } else {
1928 if (wwin->frame->border_pixel)
1929 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1930 else
1931 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1934 if (!HAS_BORDER(wwin))
1935 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1937 if (scr->selected_windows)
1938 WMRemoveFromArray(scr->selected_windows, wwin);
1942 void wMakeWindowVisible(WWindow *wwin)
1944 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1945 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1947 if (wwin->flags.shaded)
1948 wUnshadeWindow(wwin);
1950 if (wwin->flags.hidden) {
1951 WApplication *app;
1953 app = wApplicationOf(wwin->main_window);
1954 if (app) {
1955 /* trick to get focus to this window */
1956 app->last_focused = wwin;
1957 wUnhideApplication(app, False, False);
1960 if (wwin->flags.miniaturized) {
1961 wDeiconifyWindow(wwin);
1962 } else {
1963 if (!WFLAGP(wwin, no_focusable))
1964 wSetFocusTo(wwin->screen_ptr, wwin);
1965 wRaiseFrame(wwin->frame->core);
1970 * Do the animation while shading (called with what = SHADE)
1971 * or unshading (what = UNSHADE).
1973 #ifdef USE_ANIMATIONS
1974 static void shade_animate(WWindow *wwin, Bool what)
1976 int y, s, w, h;
1977 time_t time0 = time(NULL);
1979 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1980 return;
1982 switch (what) {
1983 case SHADE:
1984 if (!wwin->screen_ptr->flags.startup) {
1985 /* do the shading animation */
1986 h = wwin->frame->core->height;
1987 s = h / SHADE_STEPS;
1988 if (s < 1)
1989 s = 1;
1990 w = wwin->frame->core->width;
1991 y = wwin->frame->top_width;
1992 while (h > wwin->frame->top_width + 1) {
1993 XMoveWindow(dpy, wwin->client_win, 0, y);
1994 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1995 XFlush(dpy);
1997 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1998 break;
2000 if (SHADE_DELAY > 0)
2001 wusleep(SHADE_DELAY * 1000L);
2002 else
2003 wusleep(10);
2004 h -= s;
2005 y -= s;
2007 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2009 break;
2011 case UNSHADE:
2012 h = wwin->frame->top_width + wwin->frame->bottom_width;
2013 y = wwin->frame->top_width - wwin->client.height;
2014 s = abs(y) / SHADE_STEPS;
2015 if (s < 1)
2016 s = 1;
2017 w = wwin->frame->core->width;
2018 XMoveWindow(dpy, wwin->client_win, 0, y);
2019 if (s > 0) {
2020 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
2021 XResizeWindow(dpy, wwin->frame->core->window, w, h);
2022 XMoveWindow(dpy, wwin->client_win, 0, y);
2023 XFlush(dpy);
2024 if (SHADE_DELAY > 0)
2025 wusleep(SHADE_DELAY * 2000L / 3);
2026 else
2027 wusleep(10);
2028 h += s;
2029 y += s;
2031 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
2032 break;
2035 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2036 break;
2039 #endif