wmaker: replace and be replaced (ICCCM protocol)
[wmaker-crm.git] / src / actions.c
blob299bc78d59386213fb0f08cd90b8b4dd51d63a8a
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 #ifdef USE_ANIMATIONS
61 static struct {
62 int steps;
63 int delay;
64 } shadePars[5] = {
65 { SHADE_STEPS_UF, SHADE_DELAY_UF },
66 { SHADE_STEPS_F, SHADE_DELAY_F },
67 { SHADE_STEPS_M, SHADE_DELAY_M },
68 { SHADE_STEPS_S, SHADE_DELAY_S },
69 { SHADE_STEPS_US, SHADE_DELAY_US }
72 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
73 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
74 #endif
76 #define UNSHADE 0
77 #define SHADE 1
79 static int compareTimes(Time t1, Time t2)
81 Time diff;
82 if (t1 == t2)
83 return 0;
84 diff = t1 - t2;
85 return (diff < 60000) ? 1 : -1;
88 #ifdef USE_ANIMATIONS
89 static void shade_animate(WWindow *wwin, Bool what);
90 #else
91 static inline void shade_animate(WWindow *wwin, Bool what)
94 * This function is empty on purpose, so tell the compiler
95 * to not warn about parameters being not used
97 (void) wwin;
98 (void) what;
100 #endif
103 *----------------------------------------------------------------------
104 * wSetFocusTo--
105 * Changes the window focus to the one passed as argument.
106 * If the window to focus is not already focused, it will be brought
107 * to the head of the list of windows. Previously focused window is
108 * unfocused.
110 * Side effects:
111 * Window list may be reordered and the window focus is changed.
113 *----------------------------------------------------------------------
115 void wSetFocusTo(WScreen *scr, WWindow *wwin)
117 static WScreen *old_scr = NULL;
119 WWindow *old_focused;
120 WWindow *focused = scr->focused_window;
121 Time timestamp = w_global.timestamp.last_event;
122 WApplication *oapp = NULL, *napp = NULL;
123 int wasfocused;
125 if (scr->flags.ignore_focus_events || compareTimes(w_global.timestamp.focus_change, timestamp) > 0)
126 return;
128 if (!old_scr)
129 old_scr = scr;
131 old_focused = old_scr->focused_window;
133 w_global.timestamp.focus_change = timestamp;
135 if (old_focused)
136 oapp = wApplicationOf(old_focused->main_window);
138 if (wwin == NULL) {
139 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
140 if (old_focused)
141 wWindowUnfocus(old_focused);
143 if (oapp) {
144 wAppMenuUnmap(oapp->menu);
145 if (wPreferences.highlight_active_app)
146 wApplicationDeactivate(oapp);
149 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
150 return;
153 if (old_scr != scr && old_focused)
154 wWindowUnfocus(old_focused);
156 wasfocused = wwin->flags.focused;
157 napp = wApplicationOf(wwin->main_window);
159 /* remember last workspace where the app has been */
160 if (napp)
161 napp->last_workspace = wwin->frame->workspace;
163 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
164 /* install colormap if colormap mode is lock mode */
165 if (wPreferences.colormap_mode == WCM_CLICK)
166 wColormapInstallForWindow(scr, wwin);
168 /* set input focus */
169 switch (wwin->focus_mode) {
170 case WFM_NO_INPUT:
171 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
172 break;
173 case WFM_PASSIVE:
174 case WFM_LOCALLY_ACTIVE:
175 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
176 break;
177 case WFM_GLOBALLY_ACTIVE:
178 break;
181 XFlush(dpy);
182 if (wwin->protocols.TAKE_FOCUS)
183 wClientSendProtocol(wwin, w_global.atom.wm.take_focus, timestamp);
185 XSync(dpy, False);
186 } else {
187 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
190 if (WFLAGP(wwin, no_focusable))
191 return;
193 /* if this is not the focused window focus it */
194 if (focused != wwin) {
195 /* change the focus window list order */
196 if (wwin->prev)
197 wwin->prev->next = wwin->next;
199 if (wwin->next)
200 wwin->next->prev = wwin->prev;
202 wwin->prev = focused;
203 focused->next = wwin;
204 wwin->next = NULL;
205 scr->focused_window = wwin;
207 if (oapp && oapp != napp) {
208 wAppMenuUnmap(oapp->menu);
209 if (wPreferences.highlight_active_app)
210 wApplicationDeactivate(oapp);
214 wWindowFocus(wwin, focused);
216 if (napp && !wasfocused) {
217 #ifdef USER_MENU
218 wUserMenuRefreshInstances(napp->menu, wwin);
219 #endif /* USER_MENU */
221 if (wwin->flags.mapped)
222 wAppMenuMap(napp->menu, wwin);
224 if (napp && wPreferences.highlight_active_app)
225 wApplicationActivate(napp);
227 XFlush(dpy);
228 old_scr = scr;
231 void wShadeWindow(WWindow *wwin)
234 if (wwin->flags.shaded)
235 return;
237 XLowerWindow(dpy, wwin->client_win);
238 shade_animate(wwin, SHADE);
240 wwin->flags.skip_next_animation = 0;
241 wwin->flags.shaded = 1;
242 wwin->flags.mapped = 0;
243 /* prevent window withdrawal when getting UnmapNotify */
244 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
245 XUnmapWindow(dpy, wwin->client_win);
246 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
248 /* for the client it's just like iconification */
249 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
251 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
252 wWindowSynthConfigureNotify(wwin);
255 wClientSetState(wwin, IconicState, None);
258 WMPostNotificationName(WMNChangedState, wwin, "shade");
260 #ifdef USE_ANIMATIONS
261 if (!wwin->screen_ptr->flags.startup) {
262 /* Catch up with events not processed while animation was running */
263 ProcessPendingEvents();
265 #endif
268 void wUnshadeWindow(WWindow *wwin)
271 if (!wwin->flags.shaded)
272 return;
274 wwin->flags.shaded = 0;
275 wwin->flags.mapped = 1;
276 XMapWindow(dpy, wwin->client_win);
278 shade_animate(wwin, UNSHADE);
280 wwin->flags.skip_next_animation = 0;
281 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
282 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
284 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
285 wWindowSynthConfigureNotify(wwin);
287 /* if the window is focused, set the focus again as it was disabled during
288 * shading */
289 if (wwin->flags.focused)
290 wSetFocusTo(wwin->screen_ptr, wwin);
292 WMPostNotificationName(WMNChangedState, wwin, "shade");
295 /* Set the old coordinates using the current values */
296 static void save_old_geometry(WWindow *wwin, int directions)
298 /* never been saved? */
299 if (!wwin->old_geometry.width)
300 directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
301 if (!wwin->old_geometry.height)
302 directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
304 if (directions & SAVE_GEOMETRY_X)
305 wwin->old_geometry.x = wwin->frame_x;
306 if (directions & SAVE_GEOMETRY_Y)
307 wwin->old_geometry.y = wwin->frame_y;
308 if (directions & SAVE_GEOMETRY_WIDTH)
309 wwin->old_geometry.width = wwin->client.width;
310 if (directions & SAVE_GEOMETRY_HEIGHT)
311 wwin->old_geometry.height = wwin->client.height;
314 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
316 WMRect old_geom_rect;
317 int old_head;
318 Bool same_head;
320 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
321 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
322 same_head = (wGetHeadForWindow(wwin) == old_head);
323 *x = ((wwin->old_geometry.x || wwin->old_geometry.width) && same_head) ? wwin->old_geometry.x : wwin->frame_x;
324 *y = ((wwin->old_geometry.y || wwin->old_geometry.height) && same_head) ? wwin->old_geometry.y : wwin->frame_y;
325 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
326 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
329 /* Remember geometry for unmaximizing */
330 void update_saved_geometry(WWindow *wwin)
332 /* NOT if we aren't already maximized
333 * we'll save geometry when maximizing */
334 if (!wwin->flags.maximized)
335 return;
337 /* NOT if we are fully maximized */
338 if ((wwin->flags.maximized & MAX_MAXIMUS) ||
339 ((wwin->flags.maximized & MAX_HORIZONTAL) &&
340 (wwin->flags.maximized & MAX_VERTICAL)))
341 return;
343 /* save the co-ordinate in the axis in which we AREN'T maximized */
344 if (wwin->flags.maximized & MAX_HORIZONTAL)
345 save_old_geometry(wwin, SAVE_GEOMETRY_Y);
346 if (wwin->flags.maximized & MAX_VERTICAL)
347 save_old_geometry(wwin, SAVE_GEOMETRY_X);
350 void wMaximizeWindow(WWindow *wwin, int directions)
352 unsigned int new_width, new_height, half_scr_width, half_scr_height;
353 int new_x = 0;
354 int new_y = 0;
355 int maximus_x = 0;
356 int maximus_y = 0;
357 unsigned int maximus_width = 0;
358 unsigned int maximus_height = 0;
359 WArea usableArea, totalArea;
360 Bool has_border = 1;
361 int adj_size;
362 WScreen *scr = wwin->screen_ptr;
364 if (!IS_RESIZABLE(wwin))
365 return;
367 if (!HAS_BORDER(wwin))
368 has_border = 0;
370 if (wPreferences.drag_maximized_window == DRAGMAX_NOMOVE)
371 wwin->client_flags.no_movable = 1;
373 /* the size to adjust the geometry */
374 adj_size = scr->frame_border_width * 2 * has_border;
376 /* save old coordinates before we change the current values */
377 if (!wwin->flags.maximized)
378 save_old_geometry(wwin, SAVE_GEOMETRY_ALL);
380 totalArea.x2 = scr->scr_width;
381 totalArea.y2 = scr->scr_height;
382 totalArea.x1 = 0;
383 totalArea.y1 = 0;
384 usableArea = totalArea;
386 if (!(directions & MAX_IGNORE_XINERAMA)) {
387 WScreen *scr = wwin->screen_ptr;
388 int head;
390 if (directions & MAX_KEYBOARD)
391 head = wGetHeadForWindow(wwin);
392 else
393 head = wGetHeadForPointerLocation(scr);
395 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
399 /* Only save directions, not kbd or xinerama hints */
400 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
402 if (WFLAGP(wwin, full_maximize))
403 usableArea = totalArea;
404 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
405 half_scr_height = (usableArea.y2 - usableArea.y1)/2;
407 if (wwin->flags.shaded) {
408 wwin->flags.skip_next_animation = 1;
409 wUnshadeWindow(wwin);
412 if (directions & MAX_MAXIMUS) {
413 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
414 new_width = maximus_width - adj_size;
415 new_height = maximus_height - adj_size;
416 new_x = maximus_x;
417 new_y = maximus_y;
418 if (WFLAGP(wwin, full_maximize) && (new_y == 0)) {
419 new_height += wwin->frame->bottom_width - 1;
420 new_y -= wwin->frame->top_width;
423 wwin->maximus_x = new_x;
424 wwin->maximus_y = new_y;
425 wwin->flags.old_maximized |= MAX_MAXIMUS;
426 } else {
427 /* set default values if no option set then */
428 if (!(directions & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS))) {
429 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
430 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
432 if (!(directions & (MAX_VERTICAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS))) {
433 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
434 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
437 /* left|right position */
438 if (directions & MAX_LEFTHALF) {
439 new_width = half_scr_width - adj_size;
440 new_x = usableArea.x1;
441 } else if (directions & MAX_RIGHTHALF) {
442 new_width = half_scr_width - adj_size;
443 new_x = usableArea.x1 + half_scr_width;
445 /* top|bottom position */
446 if (directions & MAX_TOPHALF) {
447 new_height = half_scr_height - adj_size;
448 new_y = usableArea.y1;
449 } else if (directions & MAX_BOTTOMHALF) {
450 new_height = half_scr_height - adj_size;
451 new_y = usableArea.y1 + half_scr_height;
454 /* vertical|horizontal position */
455 if (directions & MAX_HORIZONTAL) {
456 new_width = usableArea.x2 - usableArea.x1 - adj_size;
457 new_x = usableArea.x1;
459 if (directions & MAX_VERTICAL) {
460 new_height = usableArea.y2 - usableArea.y1 - adj_size;
461 new_y = usableArea.y1;
462 if (WFLAGP(wwin, full_maximize) && (new_y == 0))
463 new_y -= wwin->frame->top_width;
467 if (!WFLAGP(wwin, full_maximize) && !(directions == MAX_MAXIMUS || directions == MAX_HORIZONTAL))
468 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
470 /* set maximization state */
471 wwin->flags.maximized = directions;
472 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
473 wwin->flags.maximized = MAX_MAXIMUS;
475 wWindowConstrainSize(wwin, &new_width, &new_height);
477 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
478 usableArea.y2 - usableArea.y1, &new_width, &new_height);
480 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
481 wWindowSynthConfigureNotify(wwin);
483 WMPostNotificationName(WMNChangedState, wwin, "maximize");
486 /* generic (un)maximizer */
487 void handleMaximize(WWindow *wwin, int directions)
489 int current = wwin->flags.maximized;
490 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
491 int effective = requested ^ current;
492 int flags = directions & ~requested;
494 if (!effective) {
495 /* allow wMaximizeWindow to restore the Maximusized size */
496 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
497 !(requested & MAX_MAXIMUS))
498 wMaximizeWindow(wwin, MAX_MAXIMUS | flags);
499 else
500 wUnmaximizeWindow(wwin);
501 /* these alone mean vertical|horizontal toggle */
502 } else if ((effective == MAX_LEFTHALF) ||
503 (effective == MAX_RIGHTHALF) ||
504 (effective == MAX_TOPHALF) ||
505 (effective == MAX_BOTTOMHALF))
506 wUnmaximizeWindow(wwin);
507 else {
508 if ((requested == (MAX_HORIZONTAL | MAX_VERTICAL)) ||
509 (requested == MAX_MAXIMUS))
510 effective = requested;
511 else {
512 if (requested & MAX_LEFTHALF) {
513 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
514 effective |= MAX_VERTICAL;
515 else
516 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
517 effective |= MAX_LEFTHALF;
518 effective &= ~(MAX_HORIZONTAL | MAX_RIGHTHALF);
519 } else if (requested & MAX_RIGHTHALF) {
520 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
521 effective |= MAX_VERTICAL;
522 else
523 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
524 effective |= MAX_RIGHTHALF;
525 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF);
527 if (requested & MAX_TOPHALF) {
528 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
529 effective |= MAX_HORIZONTAL;
530 else
531 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
532 effective |= MAX_TOPHALF;
533 effective &= ~(MAX_VERTICAL | MAX_BOTTOMHALF);
534 } else if (requested & MAX_BOTTOMHALF) {
535 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
536 effective |= MAX_HORIZONTAL;
537 else
538 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
539 effective |= MAX_BOTTOMHALF;
540 effective &= ~(MAX_VERTICAL | MAX_TOPHALF);
542 if (requested & MAX_HORIZONTAL)
543 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
544 if (requested & MAX_VERTICAL)
545 effective &= ~(MAX_TOPHALF | MAX_BOTTOMHALF);
546 effective &= ~MAX_MAXIMUS;
548 wMaximizeWindow(wwin, effective | flags);
552 /* the window boundary coordinates */
553 typedef struct {
554 int left;
555 int right;
556 int bottom;
557 int top;
558 int width;
559 int height;
560 } win_coords;
562 static void set_window_coords(WWindow *wwin, win_coords *obs)
564 obs->left = wwin->frame_x;
565 obs->top = wwin->frame_y;
566 obs->width = wwin->frame->core->width;
567 obs->height = wwin->frame->core->height;
568 obs->bottom = obs->top + obs->height;
569 obs->right = obs->left + obs->width;
573 * Maximus: tiled maximization (maximize without overlapping other windows)
575 * The original window 'orig' will be maximized to new coordinates 'new'.
576 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
578 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
579 unsigned int *new_width, unsigned int *new_height)
581 WWindow *tmp;
582 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
583 short int adjust_height;
584 int x_intsect, y_intsect;
585 /* the obstructing, original and new windows */
586 win_coords obs, orig, new;
588 /* set the original coordinate positions of the window to be Maximumized */
589 if (wwin->flags.maximized) {
590 /* window is already maximized; consider original geometry */
591 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
592 orig.bottom = orig.top + orig.height;
593 orig.right = orig.left + orig.width;
594 } else
595 set_window_coords(wwin, &orig);
597 /* Try to fully maximize first, then readjust later */
598 new.left = usableArea.x1;
599 new.right = usableArea.x2;
600 new.top = usableArea.y1;
601 new.bottom = usableArea.y2;
603 if (HAS_TITLEBAR(wwin))
604 tbar_height_0 = TITLEBAR_HEIGHT;
605 if (HAS_RESIZEBAR(wwin))
606 rbar_height_0 = RESIZEBAR_HEIGHT;
607 if (HAS_BORDER(wwin))
608 bd_width_0 = wwin->screen_ptr->frame_border_width;
610 /* the length to be subtracted if the window has titlebar, etc */
611 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
613 tmp = wwin;
614 /* The focused window is always the last in the list */
615 while (tmp->prev) {
616 /* ignore windows in other workspaces etc */
617 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
618 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
619 tmp = tmp->prev;
620 continue;
622 tmp = tmp->prev;
624 /* Set the coordinates of obstructing window */
625 set_window_coords(tmp, &obs);
627 /* Try to maximize in the y direction first */
628 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
629 if (x_intsect != 0) {
630 /* TODO: Consider the case when coords are equal */
631 if (obs.bottom < orig.top && obs.bottom > new.top) {
632 /* w_0 is below the bottom of w_j */
633 new.top = obs.bottom + 1;
635 if (orig.bottom < obs.top && obs.top < new.bottom) {
636 /* The bottom of w_0 is above the top of w_j */
637 new.bottom = obs.top - 1;
642 tmp = wwin;
643 while (tmp->prev) {
644 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
645 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
646 tmp = tmp->prev;
647 continue;
649 tmp = tmp->prev;
651 set_window_coords(tmp, &obs);
654 * Use the new.top and new.height instead of original values
655 * as they may have different intersections with the obstructing windows
657 new.height = new.bottom - new.top - adjust_height;
658 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
659 if (y_intsect != 0) {
660 if (obs.right < orig.left && obs.right > new.left) {
661 /* w_0 is completely to the right of w_j */
662 new.left = obs.right + 1;
664 if (orig.right < obs.left && obs.left < new.right) {
665 /* w_0 is completely to the left of w_j */
666 new.right = obs.left - 1;
671 *new_x = new.left;
672 *new_y = new.top;
673 /* xcalc needs -7 here, but other apps don't */
674 *new_height = new.bottom - new.top - adjust_height - 1;
675 *new_width = new.right - new.left;
678 void wUnmaximizeWindow(WWindow *wwin)
680 int x, y, w, h;
682 if (!wwin->flags.maximized)
683 return;
685 if (wwin->flags.shaded) {
686 wwin->flags.skip_next_animation = 1;
687 wUnshadeWindow(wwin);
690 if (wPreferences.drag_maximized_window == DRAGMAX_NOMOVE)
691 wwin->client_flags.no_movable = 0;
693 /* Use old coordinates if they are set, current values otherwise */
694 remember_geometry(wwin, &x, &y, &w, &h);
696 /* unMaximusize relative to original position */
697 if (wwin->flags.maximized & MAX_MAXIMUS) {
698 x += wwin->frame_x - wwin->maximus_x;
699 y += wwin->frame_y - wwin->maximus_y;
702 wwin->flags.maximized = 0;
703 wwin->flags.old_maximized = 0;
704 wWindowConfigure(wwin, x, y, w, h);
705 wWindowSynthConfigureNotify(wwin);
707 WMPostNotificationName(WMNChangedState, wwin, "maximize");
710 void wFullscreenWindow(WWindow *wwin)
712 int head;
713 WMRect rect;
715 if (wwin->flags.fullscreen)
716 return;
718 wwin->flags.fullscreen = True;
720 wWindowConfigureBorders(wwin);
722 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
723 wRaiseFrame(wwin->frame->core);
725 wwin->bfs_geometry.x = wwin->frame_x;
726 wwin->bfs_geometry.y = wwin->frame_y;
727 wwin->bfs_geometry.width = wwin->frame->core->width;
728 wwin->bfs_geometry.height = wwin->frame->core->height;
730 head = wGetHeadForWindow(wwin);
731 rect = wGetRectForHead(wwin->screen_ptr, head);
732 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
734 wwin->screen_ptr->bfs_focused_window = wwin->screen_ptr->focused_window;
735 wSetFocusTo(wwin->screen_ptr, wwin);
737 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
740 void wUnfullscreenWindow(WWindow *wwin)
742 if (!wwin->flags.fullscreen)
743 return;
745 wwin->flags.fullscreen = False;
747 if (WFLAGP(wwin, sunken))
748 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
749 else if (WFLAGP(wwin, floating))
750 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
752 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
753 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
755 wWindowConfigureBorders(wwin);
757 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
758 wFrameWindowPaint(wwin->frame);
761 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
763 if (wwin->screen_ptr->bfs_focused_window) {
764 wSetFocusTo(wwin->screen_ptr, wwin->screen_ptr->bfs_focused_window);
765 wwin->screen_ptr->bfs_focused_window = NULL;
769 #ifdef USE_ANIMATIONS
770 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
772 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
773 float cx, cy, cw, ch;
774 float xstep, ystep, wstep, hstep;
775 XPoint points[5];
776 float dx, dch, midy;
777 float angle, final_angle, delta;
779 xstep = (float)(fx - x) / steps;
780 ystep = (float)(fy - y) / steps;
781 wstep = (float)(fw - w) / steps;
782 hstep = (float)(fh - h) / steps;
784 cx = (float)x;
785 cy = (float)y;
786 cw = (float)w;
787 ch = (float)h;
789 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
790 delta = (float)(final_angle / FRAMES);
791 for (angle = 0;; angle += delta) {
792 if (angle > final_angle)
793 angle = final_angle;
795 dx = (cw / 10) - ((cw / 5) * sin(angle));
796 dch = (ch / 2) * cos(angle);
797 midy = cy + (ch / 2);
799 points[0].x = cx + dx;
800 points[0].y = midy - dch;
801 points[1].x = cx + cw - dx;
802 points[1].y = points[0].y;
803 points[2].x = cx + cw + dx;
804 points[2].y = midy + dch;
805 points[3].x = cx - dx;
806 points[3].y = points[2].y;
807 points[4].x = points[0].x;
808 points[4].y = points[0].y;
810 XGrabServer(dpy);
811 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
812 XFlush(dpy);
813 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
815 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
816 XUngrabServer(dpy);
817 cx += xstep;
818 cy += ystep;
819 cw += wstep;
820 ch += hstep;
821 if (angle >= final_angle)
822 break;
825 XFlush(dpy);
828 #undef FRAMES
830 static void
831 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
833 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
834 float cx, cy, cw, ch;
835 float xstep, ystep, wstep, hstep;
836 XPoint points[5];
837 float angle, final_angle, a, d, delta;
839 x += w / 2;
840 y += h / 2;
841 fx += fw / 2;
842 fy += fh / 2;
844 xstep = (float)(fx - x) / steps;
845 ystep = (float)(fy - y) / steps;
846 wstep = (float)(fw - w) / steps;
847 hstep = (float)(fh - h) / steps;
849 cx = (float)x;
850 cy = (float)y;
851 cw = (float)w;
852 ch = (float)h;
854 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
855 delta = (float)(final_angle / FRAMES);
856 for (angle = 0;; angle += delta) {
857 if (angle > final_angle)
858 angle = final_angle;
860 a = atan(ch / cw);
861 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
863 points[0].x = cx + cos(angle - a) * d;
864 points[0].y = cy + sin(angle - a) * d;
865 points[1].x = cx + cos(angle + a) * d;
866 points[1].y = cy + sin(angle + a) * d;
867 points[2].x = cx + cos(angle - a + WM_PI) * d;
868 points[2].y = cy + sin(angle - a + WM_PI) * d;
869 points[3].x = cx + cos(angle + a + WM_PI) * d;
870 points[3].y = cy + sin(angle + a + WM_PI) * d;
871 points[4].x = cx + cos(angle - a) * d;
872 points[4].y = cy + sin(angle - a) * d;
873 XGrabServer(dpy);
874 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
875 XFlush(dpy);
876 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
878 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
879 XUngrabServer(dpy);
880 cx += xstep;
881 cy += ystep;
882 cw += wstep;
883 ch += hstep;
884 if (angle >= final_angle)
885 break;
888 XFlush(dpy);
891 #undef FRAMES
893 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
895 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
896 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
897 float xstep, ystep, wstep, hstep;
898 int i, j;
900 xstep = (float)(fx - x) / steps;
901 ystep = (float)(fy - y) / steps;
902 wstep = (float)(fw - w) / steps;
903 hstep = (float)(fh - h) / steps;
905 for (j = 0; j < FRAMES; j++) {
906 cx[j] = (float)x;
907 cy[j] = (float)y;
908 cw[j] = (float)w;
909 ch[j] = (float)h;
911 XGrabServer(dpy);
912 for (i = 0; i < steps; i++) {
913 for (j = 0; j < FRAMES; j++) {
914 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
915 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
917 XFlush(dpy);
918 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
920 for (j = 0; j < FRAMES; j++) {
921 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
922 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
923 if (j < FRAMES - 1) {
924 cx[j] = cx[j + 1];
925 cy[j] = cy[j + 1];
926 cw[j] = cw[j + 1];
927 ch[j] = ch[j + 1];
928 } else {
929 cx[j] += xstep;
930 cy[j] += ystep;
931 cw[j] += wstep;
932 ch[j] += hstep;
937 for (j = 0; j < FRAMES; j++)
938 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
939 XFlush(dpy);
940 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
942 for (j = 0; j < FRAMES; j++)
943 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
945 XUngrabServer(dpy);
948 #undef FRAMES
950 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
952 int style = wPreferences.iconification_style; /* Catch the value */
953 int steps;
955 if (style == WIS_NONE)
956 return;
958 if (style == WIS_RANDOM)
959 style = rand() % 3;
961 switch (style) {
962 case WIS_TWIST:
963 steps = MINIATURIZE_ANIMATION_STEPS_T;
964 if (steps > 0)
965 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
966 break;
967 case WIS_FLIP:
968 steps = MINIATURIZE_ANIMATION_STEPS_F;
969 if (steps > 0)
970 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
971 break;
972 case WIS_ZOOM:
973 default:
974 steps = MINIATURIZE_ANIMATION_STEPS_Z;
975 if (steps > 0)
976 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
977 break;
980 #endif /* USE_ANIMATIONS */
982 static void flushExpose(void)
984 XEvent tmpev;
986 while (XCheckTypedEvent(dpy, Expose, &tmpev))
987 WMHandleEvent(&tmpev);
988 XSync(dpy, 0);
991 static void unmapTransientsFor(WWindow *wwin)
993 WWindow *tmp;
995 tmp = wwin->screen_ptr->focused_window;
996 while (tmp) {
997 /* unmap the transients for this transient */
998 if (tmp != wwin && tmp->transient_for == wwin->client_win
999 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
1000 unmapTransientsFor(tmp);
1001 tmp->flags.miniaturized = 1;
1002 if (!tmp->flags.shaded)
1003 wWindowUnmap(tmp);
1004 else
1005 XUnmapWindow(dpy, tmp->frame->core->window);
1007 if (!tmp->flags.shaded)
1009 wClientSetState(tmp, IconicState, None);
1011 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1013 tmp = tmp->prev;
1017 static void mapTransientsFor(WWindow *wwin)
1019 WWindow *tmp;
1021 tmp = wwin->screen_ptr->focused_window;
1022 while (tmp) {
1023 /* recursively map the transients for this transient */
1024 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1025 && tmp->icon == NULL) {
1026 mapTransientsFor(tmp);
1027 tmp->flags.miniaturized = 0;
1028 if (!tmp->flags.shaded)
1029 wWindowMap(tmp);
1030 else
1031 XMapWindow(dpy, tmp->frame->core->window);
1032 tmp->flags.semi_focused = 0;
1034 if (!tmp->flags.shaded)
1036 wClientSetState(tmp, NormalState, None);
1038 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1040 tmp = tmp->prev;
1044 static WWindow *recursiveTransientFor(WWindow *wwin)
1046 int i;
1048 if (!wwin)
1049 return None;
1051 /* hackish way to detect transient_for cycle */
1052 i = wwin->screen_ptr->window_count + 1;
1054 while (wwin && wwin->transient_for != None && i > 0) {
1055 wwin = wWindowFor(wwin->transient_for);
1056 i--;
1058 if (i == 0 && wwin) {
1059 wwarning(_("window \"%s\" has a severely broken WM_TRANSIENT_FOR hint"),
1060 wwin->frame->title);
1061 return NULL;
1064 return wwin;
1067 #ifdef USE_ANIMATIONS
1068 static int getAnimationGeometry(WWindow *wwin, int *ix, int *iy, int *iw, int *ih)
1070 if (wwin->screen_ptr->flags.startup || wPreferences.no_animations
1071 || wwin->flags.skip_next_animation || wwin->icon == NULL)
1072 return 0;
1074 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1075 *ix = wwin->icon_x;
1076 *iy = wwin->icon_y;
1077 *iw = wwin->icon->core->width;
1078 *ih = wwin->icon->core->height;
1079 } else {
1080 if (wwin->flags.net_handle_icon) {
1081 *ix = wwin->icon_x;
1082 *iy = wwin->icon_y;
1083 *iw = wwin->icon_w;
1084 *ih = wwin->icon_h;
1085 } else {
1086 *ix = 0;
1087 *iy = 0;
1088 *iw = wwin->screen_ptr->scr_width;
1089 *ih = wwin->screen_ptr->scr_height;
1092 return 1;
1094 #endif /* USE_ANIMATIONS */
1096 void wIconifyWindow(WWindow *wwin)
1098 XWindowAttributes attribs;
1099 int present;
1101 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1102 return; /* the window doesn't exist anymore */
1104 if (wwin->flags.miniaturized)
1105 return; /* already miniaturized */
1107 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1108 WWindow *owner = wWindowFor(wwin->transient_for);
1110 if (owner && owner->flags.miniaturized)
1111 return;
1114 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1116 /* if the window is in another workspace, simplify process */
1117 if (present) {
1118 /* icon creation may take a while */
1119 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1120 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1121 GrabModeAsync, None, None, CurrentTime);
1124 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1125 if (!wwin->flags.icon_moved)
1126 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1128 wwin->icon = icon_create_for_wwindow(wwin);
1129 wwin->icon->mapped = 1;
1131 /* extract the window screenshot everytime, as the option can be enable anytime */
1132 if (wwin->client_win && wwin->flags.mapped) {
1133 RImage *mini_preview;
1134 XImage *pimg;
1135 unsigned int w, h;
1136 int x, y;
1137 Window baz;
1139 XRaiseWindow(dpy, wwin->frame->core->window);
1140 XTranslateCoordinates(dpy, wwin->client_win, wwin->screen_ptr->root_win, 0, 0, &x, &y, &baz);
1142 w = attribs.width;
1143 h = attribs.height;
1145 if (x - attribs.x + attribs.width > wwin->screen_ptr->scr_width)
1146 w = wwin->screen_ptr->scr_width - x + attribs.x;
1148 if (y - attribs.y + attribs.height > wwin->screen_ptr->scr_height)
1149 h = wwin->screen_ptr->scr_height - y + attribs.y;
1151 pimg = XGetImage(dpy, wwin->client_win, 0, 0, w, h, AllPlanes, ZPixmap);
1152 if (pimg) {
1153 mini_preview = RCreateImageFromXImage(wwin->screen_ptr->rcontext, pimg, NULL);
1154 XDestroyImage(pimg);
1156 if (mini_preview) {
1157 set_icon_minipreview(wwin->icon, mini_preview);
1158 RReleaseImage(mini_preview);
1159 } else {
1160 const char *title;
1161 char title_buf[32];
1163 if (wwin->frame->title) {
1164 title = wwin->frame->title;
1165 } else {
1166 snprintf(title_buf, sizeof(title_buf), "(id=0x%lx)", wwin->client_win);
1167 title = title_buf;
1169 wwarning(_("creation of mini-preview failed for window \"%s\""), title);
1175 wwin->flags.miniaturized = 1;
1176 wwin->flags.mapped = 0;
1178 /* unmap transients */
1179 unmapTransientsFor(wwin);
1181 if (present) {
1182 #ifdef USE_ANIMATIONS
1183 int ix, iy, iw, ih;
1184 #endif
1185 XUngrabPointer(dpy, CurrentTime);
1186 wWindowUnmap(wwin);
1187 /* let all Expose events arrive so that we can repaint
1188 * something before the animation starts (and the server is grabbed) */
1189 XSync(dpy, 0);
1191 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1192 wClientSetState(wwin, IconicState, None);
1193 else
1194 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1196 flushExpose();
1197 #ifdef USE_ANIMATIONS
1198 if (getAnimationGeometry(wwin, &ix, &iy, &iw, &ih))
1199 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1200 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1201 #endif
1204 wwin->flags.skip_next_animation = 0;
1206 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1207 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1208 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1209 XMapWindow(dpy, wwin->icon->core->window);
1211 AddToStackList(wwin->icon->core);
1212 wLowerFrame(wwin->icon->core);
1215 if (present) {
1216 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1219 * It doesn't seem to be working and causes button event hangup
1220 * when deiconifying a transient window.
1221 setupIconGrabs(wwin->icon);
1223 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1224 && wPreferences.focus_mode == WKF_CLICK) {
1225 WWindow *tmp;
1227 tmp = wwin->prev;
1228 while (tmp) {
1229 if (!WFLAGP(tmp, no_focusable)
1230 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1231 && (wwin->frame->workspace == tmp->frame->workspace))
1232 break;
1233 tmp = tmp->prev;
1235 wSetFocusTo(wwin->screen_ptr, tmp);
1236 } else if (wPreferences.focus_mode != WKF_CLICK) {
1237 wSetFocusTo(wwin->screen_ptr, NULL);
1239 #ifdef USE_ANIMATIONS
1240 if (!wwin->screen_ptr->flags.startup) {
1241 /* Catch up with events not processed while animation was running */
1242 Window clientwin = wwin->client_win;
1244 ProcessPendingEvents();
1246 /* the window can disappear while ProcessPendingEvents() runs */
1247 if (!wWindowFor(clientwin))
1248 return;
1250 #endif
1253 /* maybe we want to do this regardless of net_handle_icon
1254 * it seems to me we might break behaviour this way.
1256 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1257 && !wwin->flags.net_handle_icon)
1258 wIconSelect(wwin->icon);
1260 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1262 if (wPreferences.auto_arrange_icons)
1263 wArrangeIcons(wwin->screen_ptr, True);
1266 void wDeiconifyWindow(WWindow *wwin)
1268 /* Let's avoid changing workspace while deiconifying */
1269 w_global.ignore_workspace_change = True;
1271 /* we're hiding for show_desktop */
1272 int netwm_hidden = wwin->flags.net_show_desktop &&
1273 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1275 if (!netwm_hidden)
1276 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1278 if (!wwin->flags.miniaturized) {
1279 w_global.ignore_workspace_change = False;
1280 return;
1283 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1284 WWindow *owner = recursiveTransientFor(wwin);
1286 if (owner && owner->flags.miniaturized) {
1287 wDeiconifyWindow(owner);
1288 wSetFocusTo(wwin->screen_ptr, wwin);
1289 wRaiseFrame(wwin->frame->core);
1290 w_global.ignore_workspace_change = False;
1291 return;
1295 wwin->flags.miniaturized = 0;
1297 if (!netwm_hidden && !wwin->flags.shaded)
1298 wwin->flags.mapped = 1;
1300 if (!netwm_hidden || wPreferences.sticky_icons) {
1301 /* maybe we want to do this regardless of net_handle_icon
1302 * it seems to me we might break behaviour this way.
1304 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1305 && wwin->icon != NULL) {
1306 if (wwin->icon->selected)
1307 wIconSelect(wwin->icon);
1309 XUnmapWindow(dpy, wwin->icon->core->window);
1313 /* if the window is in another workspace, do it silently */
1314 if (!netwm_hidden) {
1315 #ifdef USE_ANIMATIONS
1316 int ix, iy, iw, ih;
1317 if (getAnimationGeometry(wwin, &ix, &iy, &iw, &ih))
1318 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1319 wwin->frame_x, wwin->frame_y,
1320 wwin->frame->core->width, wwin->frame->core->height);
1321 #endif
1322 wwin->flags.skip_next_animation = 0;
1323 XGrabServer(dpy);
1324 if (!wwin->flags.shaded)
1325 XMapWindow(dpy, wwin->client_win);
1327 XMapWindow(dpy, wwin->frame->core->window);
1328 wRaiseFrame(wwin->frame->core);
1329 if (!wwin->flags.shaded)
1330 wClientSetState(wwin, NormalState, None);
1332 mapTransientsFor(wwin);
1335 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1336 && !wwin->flags.net_handle_icon) {
1337 RemoveFromStackList(wwin->icon->core);
1338 wSetFocusTo(wwin->screen_ptr, wwin);
1339 wIconDestroy(wwin->icon);
1340 wwin->icon = NULL;
1343 if (!netwm_hidden) {
1344 XUngrabServer(dpy);
1346 wSetFocusTo(wwin->screen_ptr, wwin);
1348 #ifdef USE_ANIMATIONS
1349 if (!wwin->screen_ptr->flags.startup) {
1350 /* Catch up with events not processed while animation was running */
1351 Window clientwin = wwin->client_win;
1353 ProcessPendingEvents();
1355 /* the window can disappear while ProcessPendingEvents() runs */
1356 if (!wWindowFor(clientwin)) {
1357 w_global.ignore_workspace_change = False;
1358 return;
1361 #endif
1364 if (wPreferences.auto_arrange_icons)
1365 wArrangeIcons(wwin->screen_ptr, True);
1367 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1369 /* In case we were shaded and iconified, also unshade */
1370 if (!netwm_hidden)
1371 wUnshadeWindow(wwin);
1373 w_global.ignore_workspace_change = False;
1376 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1378 if (wwin->flags.miniaturized) {
1379 if (wwin->icon) {
1380 XUnmapWindow(dpy, wwin->icon->core->window);
1381 wwin->icon->mapped = 0;
1383 wwin->flags.hidden = 1;
1385 WMPostNotificationName(WMNChangedState, wwin, "hide");
1386 return;
1389 if (wwin->flags.inspector_open)
1390 wHideInspectorForWindow(wwin);
1392 wwin->flags.hidden = 1;
1393 wWindowUnmap(wwin);
1395 wClientSetState(wwin, IconicState, icon->icon_win);
1396 flushExpose();
1398 #ifdef USE_ANIMATIONS
1399 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1400 !wwin->flags.skip_next_animation && animate) {
1401 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1402 wwin->frame->core->width, wwin->frame->core->height,
1403 icon_x, icon_y, icon->core->width, icon->core->height);
1405 #else
1406 /* Tell the compiler it is normal that those parameters are not used in this case */
1407 (void) icon_x;
1408 (void) icon_y;
1409 (void) animate;
1410 #endif
1411 wwin->flags.skip_next_animation = 0;
1413 WMPostNotificationName(WMNChangedState, wwin, "hide");
1416 void wHideAll(WScreen *scr)
1418 WWindow *wwin;
1419 WWindow **windows;
1420 WMenu *menu;
1421 unsigned int wcount = 0;
1422 int i;
1424 if (!scr)
1425 return;
1427 menu = scr->switch_menu;
1429 windows = wmalloc(sizeof(WWindow *));
1431 if (menu != NULL) {
1432 for (i = 0; i < menu->entry_no; i++) {
1433 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1434 wcount++;
1435 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1437 } else {
1438 wwin = scr->focused_window;
1440 while (wwin) {
1441 windows[wcount] = wwin;
1442 wcount++;
1443 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1444 wwin = wwin->prev;
1449 for (i = 0; i < wcount; i++) {
1450 wwin = windows[i];
1451 if (wwin->frame->workspace == scr->current_workspace
1452 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1453 && !wwin->flags.internal_window
1454 && !WFLAGP(wwin, no_miniaturizable)
1456 wwin->flags.skip_next_animation = 1;
1457 wIconifyWindow(wwin);
1461 wfree(windows);
1464 void wHideOtherApplications(WWindow *awin)
1466 WWindow *wwin;
1467 WApplication *tapp;
1469 if (!awin)
1470 return;
1471 wwin = awin->screen_ptr->focused_window;
1473 while (wwin) {
1474 if (wwin != awin
1475 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1476 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1477 && !wwin->flags.internal_window
1478 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1480 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1481 if (!WFLAGP(wwin, no_miniaturizable)) {
1482 wwin->flags.skip_next_animation = 1;
1483 wIconifyWindow(wwin);
1485 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1486 tapp = wApplicationOf(wwin->main_window);
1487 if (tapp) {
1488 tapp->flags.skip_next_animation = 1;
1489 wHideApplication(tapp);
1490 } else {
1491 if (!WFLAGP(wwin, no_miniaturizable)) {
1492 wwin->flags.skip_next_animation = 1;
1493 wIconifyWindow(wwin);
1498 wwin = wwin->prev;
1501 wSetFocusTo(awin->screen_ptr, awin);
1505 void wHideApplication(WApplication *wapp)
1507 WScreen *scr;
1508 WWindow *wlist;
1509 int hadfocus;
1510 int animate;
1512 if (!wapp) {
1513 wwarning("trying to hide a non grouped window");
1514 return;
1516 if (!wapp->main_window_desc) {
1517 wwarning("group leader not found for window group");
1518 return;
1520 scr = wapp->main_window_desc->screen_ptr;
1521 hadfocus = 0;
1522 wlist = scr->focused_window;
1523 if (!wlist)
1524 return;
1526 if (wlist->main_window == wapp->main_window)
1527 wapp->last_focused = wlist;
1528 else
1529 wapp->last_focused = NULL;
1531 animate = !wapp->flags.skip_next_animation;
1533 while (wlist) {
1534 if (wlist->main_window == wapp->main_window) {
1535 if (wlist->flags.focused)
1536 hadfocus = 1;
1537 if (wapp->app_icon) {
1538 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1539 wapp->app_icon->y_pos, wlist, animate);
1540 animate = False;
1543 wlist = wlist->prev;
1546 wapp->flags.skip_next_animation = 0;
1548 if (hadfocus) {
1549 if (wPreferences.focus_mode == WKF_CLICK) {
1550 wlist = scr->focused_window;
1551 while (wlist) {
1552 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1553 && (wlist->flags.mapped || wlist->flags.shaded))
1554 break;
1555 wlist = wlist->prev;
1557 wSetFocusTo(scr, wlist);
1558 } else {
1559 wSetFocusTo(scr, NULL);
1563 wapp->flags.hidden = 1;
1565 if (wPreferences.auto_arrange_icons)
1566 wArrangeIcons(scr, True);
1568 #ifdef HIDDENDOT
1569 if (wapp->app_icon)
1570 wAppIconPaint(wapp->app_icon);
1571 #endif
1574 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1576 if (bringToCurrentWS)
1577 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1579 wwin->flags.hidden = 0;
1581 #ifdef USE_ANIMATIONS
1582 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1583 animateResize(wwin->screen_ptr, icon_x, icon_y,
1584 icon->core->width, icon->core->height,
1585 wwin->frame_x, wwin->frame_y,
1586 wwin->frame->core->width, wwin->frame->core->height);
1588 #else
1589 /* Tell the compiler it is normal that those parameters are not used in this case */
1590 (void) icon;
1591 (void) icon_x;
1592 (void) icon_y;
1593 (void) animate;
1594 #endif
1595 wwin->flags.skip_next_animation = 0;
1596 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1597 XMapWindow(dpy, wwin->client_win);
1598 XMapWindow(dpy, wwin->frame->core->window);
1599 wClientSetState(wwin, NormalState, None);
1600 wwin->flags.mapped = 1;
1601 wRaiseFrame(wwin->frame->core);
1603 if (wwin->flags.inspector_open)
1604 wUnhideInspectorForWindow(wwin);
1606 WMPostNotificationName(WMNChangedState, wwin, "hide");
1609 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1611 WScreen *scr;
1612 WWindow *wlist, *next;
1613 WWindow *focused = NULL;
1614 int animate;
1616 if (!wapp)
1617 return;
1619 scr = wapp->main_window_desc->screen_ptr;
1620 wlist = scr->focused_window;
1621 if (!wlist)
1622 return;
1624 /* goto beginning of list */
1625 while (wlist->prev)
1626 wlist = wlist->prev;
1628 animate = !wapp->flags.skip_next_animation;
1630 while (wlist) {
1631 next = wlist->next;
1633 if (wlist->main_window == wapp->main_window) {
1634 if (wlist->flags.focused)
1635 focused = wlist;
1636 else if (!focused || !focused->flags.focused)
1637 focused = wlist;
1639 if (wlist->flags.miniaturized) {
1640 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1641 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1642 if (!wlist->icon->mapped) {
1643 int x, y;
1645 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1646 if (wlist->icon_x != x || wlist->icon_y != y)
1647 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1648 wlist->icon_x = x;
1649 wlist->icon_y = y;
1650 XMapWindow(dpy, wlist->icon->core->window);
1651 wlist->icon->mapped = 1;
1653 wRaiseFrame(wlist->icon->core);
1655 if (bringToCurrentWS)
1656 wWindowChangeWorkspace(wlist, scr->current_workspace);
1657 wlist->flags.hidden = 0;
1658 if (miniwindows && wlist->frame->workspace == scr->current_workspace)
1659 wDeiconifyWindow(wlist);
1660 WMPostNotificationName(WMNChangedState, wlist, "hide");
1661 } else if (wlist->flags.shaded) {
1662 if (bringToCurrentWS)
1663 wWindowChangeWorkspace(wlist, scr->current_workspace);
1664 wlist->flags.hidden = 0;
1665 wRaiseFrame(wlist->frame->core);
1666 if (wlist->frame->workspace == scr->current_workspace) {
1667 XMapWindow(dpy, wlist->frame->core->window);
1668 if (miniwindows)
1669 wUnshadeWindow(wlist);
1671 WMPostNotificationName(WMNChangedState, wlist, "hide");
1672 } else if (wlist->flags.hidden) {
1673 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1674 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1675 animate = False;
1676 } else {
1677 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace)
1678 wWindowChangeWorkspace(wlist, scr->current_workspace);
1680 wRaiseFrame(wlist->frame->core);
1683 wlist = next;
1686 wapp->flags.skip_next_animation = 0;
1687 wapp->flags.hidden = 0;
1689 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1690 wRaiseFrame(wapp->last_focused->frame->core);
1691 wSetFocusTo(scr, wapp->last_focused);
1692 } else if (focused) {
1693 wSetFocusTo(scr, focused);
1695 wapp->last_focused = NULL;
1696 if (wPreferences.auto_arrange_icons)
1697 wArrangeIcons(scr, True);
1699 #ifdef HIDDENDOT
1700 wAppIconPaint(wapp->app_icon);
1701 #endif
1704 void wShowAllWindows(WScreen *scr)
1706 WWindow *wwin, *old_foc;
1707 WApplication *wapp;
1709 old_foc = wwin = scr->focused_window;
1710 while (wwin) {
1711 if (!wwin->flags.internal_window &&
1712 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1713 if (wwin->flags.miniaturized) {
1714 wwin->flags.skip_next_animation = 1;
1715 wDeiconifyWindow(wwin);
1716 } else if (wwin->flags.hidden) {
1717 wapp = wApplicationOf(wwin->main_window);
1718 if (wapp) {
1719 wUnhideApplication(wapp, False, False);
1720 } else {
1721 wwin->flags.skip_next_animation = 1;
1722 wDeiconifyWindow(wwin);
1726 wwin = wwin->prev;
1728 wSetFocusTo(scr, old_foc);
1729 /*wRaiseFrame(old_foc->frame->core); */
1732 void wRefreshDesktop(WScreen *scr)
1734 Window win;
1735 XSetWindowAttributes attr;
1737 attr.backing_store = NotUseful;
1738 attr.save_under = False;
1739 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1740 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1741 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1742 XMapRaised(dpy, win);
1743 XDestroyWindow(dpy, win);
1744 XFlush(dpy);
1747 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1749 WWindow *wwin;
1750 WAppIcon *aicon;
1752 int head;
1753 const int heads = wXineramaHeads(scr);
1755 struct HeadVars {
1756 int pf; /* primary axis */
1757 int sf; /* secondary axis */
1758 int fullW;
1759 int fullH;
1760 int pi, si;
1761 int sx1, sx2, sy1, sy2; /* screen boundary */
1762 int sw, sh;
1763 int xo, yo;
1764 int xs, ys;
1765 } *vars;
1767 int isize = wPreferences.icon_size;
1769 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1771 for (head = 0; head < heads; ++head) {
1772 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1773 WMRect rect;
1775 if (scr->dock) {
1776 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
1778 if (scr->dock->on_right_side)
1779 area.x2 -= offset;
1780 else
1781 area.x1 += offset;
1784 rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1786 vars[head].pi = vars[head].si = 0;
1787 vars[head].sx1 = rect.pos.x;
1788 vars[head].sy1 = rect.pos.y;
1789 vars[head].sw = rect.size.width;
1790 vars[head].sh = rect.size.height;
1791 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1792 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1793 vars[head].sw = isize * (vars[head].sw / isize);
1794 vars[head].sh = isize * (vars[head].sh / isize);
1795 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1796 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1798 /* icon yard boundaries */
1799 if (wPreferences.icon_yard & IY_VERT) {
1800 vars[head].pf = vars[head].fullH;
1801 vars[head].sf = vars[head].fullW;
1802 } else {
1803 vars[head].pf = vars[head].fullW;
1804 vars[head].sf = vars[head].fullH;
1806 if (wPreferences.icon_yard & IY_RIGHT) {
1807 vars[head].xo = vars[head].sx2 - isize;
1808 vars[head].xs = -1;
1809 } else {
1810 vars[head].xo = vars[head].sx1;
1811 vars[head].xs = 1;
1813 if (wPreferences.icon_yard & IY_TOP) {
1814 vars[head].yo = vars[head].sy1;
1815 vars[head].ys = 1;
1816 } else {
1817 vars[head].yo = vars[head].sy2 - isize;
1818 vars[head].ys = -1;
1822 #define X ((wPreferences.icon_yard & IY_VERT) \
1823 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1824 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1826 #define Y ((wPreferences.icon_yard & IY_VERT) \
1827 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1828 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1830 /* arrange application icons */
1831 aicon = scr->app_icon_list;
1832 /* reverse them to avoid unnecessarily sliding of icons */
1833 while (aicon && aicon->next)
1834 aicon = aicon->next;
1836 while (aicon) {
1837 if (!aicon->docked) {
1838 /* CHECK: can icon be NULL here ? */
1839 /* The intention here is to place the AppIcon on the head that
1840 * contains most of the applications _main_ window. */
1841 head = wGetHeadForWindow(aicon->icon->owner);
1843 if (aicon->x_pos != X || aicon->y_pos != Y) {
1844 #ifdef USE_ANIMATIONS
1845 if (!wPreferences.no_animations)
1846 slide_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1847 #endif /* USE_ANIMATIONS */
1849 wAppIconMove(aicon, X, Y);
1850 vars[head].pi++;
1851 if (vars[head].pi >= vars[head].pf) {
1852 vars[head].pi = 0;
1853 vars[head].si++;
1856 aicon = aicon->prev;
1859 /* arrange miniwindows */
1860 wwin = scr->focused_window;
1861 /* reverse them to avoid unnecessarily shuffling */
1862 while (wwin && wwin->prev)
1863 wwin = wwin->prev;
1865 while (wwin) {
1866 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1867 (wwin->frame->workspace == scr->current_workspace ||
1868 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1870 head = wGetHeadForWindow(wwin);
1872 if (arrangeAll || !wwin->flags.icon_moved) {
1873 if (wwin->icon_x != X || wwin->icon_y != Y)
1874 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1876 wwin->icon_x = X;
1877 wwin->icon_y = Y;
1879 vars[head].pi++;
1880 if (vars[head].pi >= vars[head].pf) {
1881 vars[head].pi = 0;
1882 vars[head].si++;
1886 if (arrangeAll)
1887 wwin->flags.icon_moved = 0;
1888 /* we reversed the order, so we use next */
1889 wwin = wwin->next;
1892 wfree(vars);
1895 void wSelectWindow(WWindow *wwin, Bool flag)
1897 WScreen *scr = wwin->screen_ptr;
1899 if (flag) {
1900 wwin->flags.selected = 1;
1901 if (wwin->frame->selected_border_pixel)
1902 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1903 else
1904 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1906 if (!HAS_BORDER(wwin))
1907 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1909 if (!scr->selected_windows)
1910 scr->selected_windows = WMCreateArray(4);
1911 WMAddToArray(scr->selected_windows, wwin);
1912 } else {
1913 wwin->flags.selected = 0;
1914 if (wwin->flags.focused) {
1915 if (wwin->frame->focused_border_pixel)
1916 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->focused_border_pixel);
1917 else
1918 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_focused_border_pixel);
1919 } else {
1920 if (wwin->frame->border_pixel)
1921 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1922 else
1923 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1926 if (!HAS_BORDER(wwin))
1927 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1929 if (scr->selected_windows)
1930 WMRemoveFromArray(scr->selected_windows, wwin);
1934 void wMakeWindowVisible(WWindow *wwin)
1936 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1937 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1939 if (wwin->flags.shaded)
1940 wUnshadeWindow(wwin);
1942 if (wwin->flags.hidden) {
1943 WApplication *app;
1945 app = wApplicationOf(wwin->main_window);
1946 if (app) {
1947 /* trick to get focus to this window */
1948 app->last_focused = wwin;
1949 wUnhideApplication(app, False, False);
1952 if (wwin->flags.miniaturized) {
1953 wDeiconifyWindow(wwin);
1954 } else {
1955 if (!WFLAGP(wwin, no_focusable))
1956 wSetFocusTo(wwin->screen_ptr, wwin);
1957 wRaiseFrame(wwin->frame->core);
1962 * Do the animation while shading (called with what = SHADE)
1963 * or unshading (what = UNSHADE).
1965 #ifdef USE_ANIMATIONS
1966 static void shade_animate(WWindow *wwin, Bool what)
1968 int y, s, w, h;
1969 time_t time0 = time(NULL);
1971 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1972 return;
1974 switch (what) {
1975 case SHADE:
1976 if (!wwin->screen_ptr->flags.startup) {
1977 /* do the shading animation */
1978 h = wwin->frame->core->height;
1979 s = h / SHADE_STEPS;
1980 if (s < 1)
1981 s = 1;
1982 w = wwin->frame->core->width;
1983 y = wwin->frame->top_width;
1984 while (h > wwin->frame->top_width + 1) {
1985 XMoveWindow(dpy, wwin->client_win, 0, y);
1986 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1987 XFlush(dpy);
1989 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1990 break;
1992 if (SHADE_DELAY > 0)
1993 wusleep(SHADE_DELAY * 1000L);
1994 else
1995 wusleep(10);
1996 h -= s;
1997 y -= s;
1999 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2001 break;
2003 case UNSHADE:
2004 h = wwin->frame->top_width + wwin->frame->bottom_width;
2005 y = wwin->frame->top_width - wwin->client.height;
2006 s = abs(y) / SHADE_STEPS;
2007 if (s < 1)
2008 s = 1;
2009 w = wwin->frame->core->width;
2010 XMoveWindow(dpy, wwin->client_win, 0, y);
2011 if (s > 0) {
2012 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
2013 XResizeWindow(dpy, wwin->frame->core->window, w, h);
2014 XMoveWindow(dpy, wwin->client_win, 0, y);
2015 XFlush(dpy);
2016 if (SHADE_DELAY > 0)
2017 wusleep(SHADE_DELAY * 2000L / 3);
2018 else
2019 wusleep(10);
2020 h += s;
2021 y += s;
2023 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
2024 break;
2027 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2028 break;
2031 #endif