wmaker: Add "unmaximize on move" feature.
[wmaker-crm.git] / src / actions.c
blobb8e4fc4ed912994b68e91ba9f0e4f11397adb8d5
1 /* action.c- misc. window commands (miniaturize, hide etc.)
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
7 * Copyright (c) 2014 Window Maker Team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <time.h>
34 #include "WindowMaker.h"
35 #include "framewin.h"
36 #include "window.h"
37 #include "client.h"
38 #include "icon.h"
39 #include "colormap.h"
40 #include "application.h"
41 #include "actions.h"
42 #include "stacking.h"
43 #include "appicon.h"
44 #include "dock.h"
45 #include "appmenu.h"
46 #include "winspector.h"
47 #include "workspace.h"
48 #include "xinerama.h"
49 #include "usermenu.h"
50 #include "placement.h"
51 #include "misc.h"
52 #include "event.h"
55 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
56 unsigned int *new_width, unsigned int *new_height);
57 static void save_old_geometry(WWindow *wwin, int directions);
59 /******* Local Variables *******/
60 static struct {
61 int steps;
62 int delay;
63 } shadePars[5] = {
64 { SHADE_STEPS_UF, SHADE_DELAY_UF },
65 { SHADE_STEPS_F, SHADE_DELAY_F },
66 { SHADE_STEPS_M, SHADE_DELAY_M },
67 { SHADE_STEPS_S, SHADE_DELAY_S },
68 { SHADE_STEPS_US, SHADE_DELAY_US }
71 #define UNSHADE 0
72 #define SHADE 1
73 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
74 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
76 static int compareTimes(Time t1, Time t2)
78 Time diff;
79 if (t1 == t2)
80 return 0;
81 diff = t1 - t2;
82 return (diff < 60000) ? 1 : -1;
85 #ifdef ANIMATIONS
86 static void shade_animate(WWindow *wwin, Bool what);
87 #else
88 static inline void shade_animate(WWindow *wwin, Bool what)
91 * This function is empty on purpose, so tell the compiler
92 * to not warn about parameters being not used
94 (void) wwin;
95 (void) what;
97 #endif
100 *----------------------------------------------------------------------
101 * wSetFocusTo--
102 * Changes the window focus to the one passed as argument.
103 * If the window to focus is not already focused, it will be brought
104 * to the head of the list of windows. Previously focused window is
105 * unfocused.
107 * Side effects:
108 * Window list may be reordered and the window focus is changed.
110 *----------------------------------------------------------------------
112 void wSetFocusTo(WScreen *scr, WWindow *wwin)
114 static WScreen *old_scr = NULL;
116 WWindow *old_focused;
117 WWindow *focused = scr->focused_window;
118 Time timestamp = w_global.timestamp.last_event;
119 WApplication *oapp = NULL, *napp = NULL;
120 int wasfocused;
122 if (scr->flags.ignore_focus_events || compareTimes(w_global.timestamp.focus_change, timestamp) > 0)
123 return;
125 if (!old_scr)
126 old_scr = scr;
128 old_focused = old_scr->focused_window;
130 w_global.timestamp.focus_change = timestamp;
132 if (old_focused)
133 oapp = wApplicationOf(old_focused->main_window);
135 if (wwin == NULL) {
136 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
137 if (old_focused)
138 wWindowUnfocus(old_focused);
140 if (oapp) {
141 wAppMenuUnmap(oapp->menu);
142 if (wPreferences.highlight_active_app)
143 wApplicationDeactivate(oapp);
146 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
147 return;
150 if (old_scr != scr && old_focused)
151 wWindowUnfocus(old_focused);
153 wasfocused = wwin->flags.focused;
154 napp = wApplicationOf(wwin->main_window);
156 /* remember last workspace where the app has been */
157 if (napp)
158 napp->last_workspace = wwin->frame->workspace;
160 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
161 /* install colormap if colormap mode is lock mode */
162 if (wPreferences.colormap_mode == WCM_CLICK)
163 wColormapInstallForWindow(scr, wwin);
165 /* set input focus */
166 switch (wwin->focus_mode) {
167 case WFM_NO_INPUT:
168 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
169 break;
170 case WFM_PASSIVE:
171 case WFM_LOCALLY_ACTIVE:
172 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
173 break;
174 case WFM_GLOBALLY_ACTIVE:
175 break;
178 XFlush(dpy);
179 if (wwin->protocols.TAKE_FOCUS)
180 wClientSendProtocol(wwin, w_global.atom.wm.take_focus, timestamp);
182 XSync(dpy, False);
183 } else {
184 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
187 if (WFLAGP(wwin, no_focusable))
188 return;
190 /* if this is not the focused window focus it */
191 if (focused != wwin) {
192 /* change the focus window list order */
193 if (wwin->prev)
194 wwin->prev->next = wwin->next;
196 if (wwin->next)
197 wwin->next->prev = wwin->prev;
199 wwin->prev = focused;
200 focused->next = wwin;
201 wwin->next = NULL;
202 scr->focused_window = wwin;
204 if (oapp && oapp != napp) {
205 wAppMenuUnmap(oapp->menu);
206 if (wPreferences.highlight_active_app)
207 wApplicationDeactivate(oapp);
211 wWindowFocus(wwin, focused);
213 if (napp && !wasfocused) {
214 #ifdef USER_MENU
215 wUserMenuRefreshInstances(napp->menu, wwin);
216 #endif /* USER_MENU */
218 if (wwin->flags.mapped)
219 wAppMenuMap(napp->menu, wwin);
221 if (napp && wPreferences.highlight_active_app)
222 wApplicationActivate(napp);
224 XFlush(dpy);
225 old_scr = scr;
228 void wShadeWindow(WWindow *wwin)
231 if (wwin->flags.shaded)
232 return;
234 XLowerWindow(dpy, wwin->client_win);
235 shade_animate(wwin, SHADE);
237 wwin->flags.skip_next_animation = 0;
238 wwin->flags.shaded = 1;
239 wwin->flags.mapped = 0;
240 /* prevent window withdrawal when getting UnmapNotify */
241 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
242 XUnmapWindow(dpy, wwin->client_win);
243 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
245 /* for the client it's just like iconification */
246 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
248 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
249 wWindowSynthConfigureNotify(wwin);
252 wClientSetState(wwin, IconicState, None);
255 WMPostNotificationName(WMNChangedState, wwin, "shade");
257 #ifdef ANIMATIONS
258 if (!wwin->screen_ptr->flags.startup) {
259 /* Catch up with events not processed while animation was running */
260 ProcessPendingEvents();
262 #endif
265 void wUnshadeWindow(WWindow *wwin)
268 if (!wwin->flags.shaded)
269 return;
271 wwin->flags.shaded = 0;
272 wwin->flags.mapped = 1;
273 XMapWindow(dpy, wwin->client_win);
275 shade_animate(wwin, UNSHADE);
277 wwin->flags.skip_next_animation = 0;
278 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
279 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
281 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
282 wWindowSynthConfigureNotify(wwin);
284 /* if the window is focused, set the focus again as it was disabled during
285 * shading */
286 if (wwin->flags.focused)
287 wSetFocusTo(wwin->screen_ptr, wwin);
289 WMPostNotificationName(WMNChangedState, wwin, "shade");
292 /* Set the old coordinates using the current values */
293 static void save_old_geometry(WWindow *wwin, int directions)
295 /* never been saved? */
296 if (!wwin->old_geometry.width)
297 directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
298 if (!wwin->old_geometry.height)
299 directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
301 if (directions & SAVE_GEOMETRY_X)
302 wwin->old_geometry.x = wwin->frame_x;
303 if (directions & SAVE_GEOMETRY_Y)
304 wwin->old_geometry.y = wwin->frame_y;
305 if (directions & SAVE_GEOMETRY_WIDTH)
306 wwin->old_geometry.width = wwin->client.width;
307 if (directions & SAVE_GEOMETRY_HEIGHT)
308 wwin->old_geometry.height = wwin->client.height;
311 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
313 WMRect old_geom_rect;
314 int old_head;
315 Bool same_head;
317 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
318 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
319 same_head = (wGetHeadForWindow(wwin) == old_head);
320 *x = ((wwin->old_geometry.x || wwin->old_geometry.width) && same_head) ? wwin->old_geometry.x : wwin->frame_x;
321 *y = ((wwin->old_geometry.y || wwin->old_geometry.height) && same_head) ? wwin->old_geometry.y : wwin->frame_y;
322 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
323 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
326 /* Remember geometry for unmaximizing */
327 void update_saved_geometry(WWindow *wwin)
329 /* NOT if we aren't already maximized
330 * we'll save geometry when maximizing */
331 if (!wwin->flags.maximized)
332 return;
334 /* NOT if we are fully maximized */
335 if ((wwin->flags.maximized & MAX_MAXIMUS) ||
336 ((wwin->flags.maximized & MAX_HORIZONTAL) &&
337 (wwin->flags.maximized & MAX_VERTICAL)))
338 return;
340 /* save the co-ordinate in the axis in which we AREN'T maximized */
341 if (wwin->flags.maximized & MAX_HORIZONTAL)
342 save_old_geometry(wwin, SAVE_GEOMETRY_Y);
343 if (wwin->flags.maximized & MAX_VERTICAL)
344 save_old_geometry(wwin, SAVE_GEOMETRY_X);
347 void wMaximizeWindow(WWindow *wwin, int directions)
349 unsigned int new_width, new_height, half_scr_width, half_scr_height;
350 int new_x = 0;
351 int new_y = 0;
352 int maximus_x = 0;
353 int maximus_y = 0;
354 unsigned int maximus_width = 0;
355 unsigned int maximus_height = 0;
356 WArea usableArea, totalArea;
357 Bool has_border = 1;
358 int adj_size;
359 WScreen *scr = wwin->screen_ptr;
361 if (!IS_RESIZABLE(wwin))
362 return;
364 if (!HAS_BORDER(wwin))
365 has_border = 0;
367 /* the size to adjust the geometry */
368 adj_size = scr->frame_border_width * 2 * has_border;
370 /* save old coordinates before we change the current values */
371 if (!wwin->flags.maximized)
372 save_old_geometry(wwin, SAVE_GEOMETRY_ALL);
374 totalArea.x2 = scr->scr_width;
375 totalArea.y2 = scr->scr_height;
376 totalArea.x1 = 0;
377 totalArea.y1 = 0;
378 usableArea = totalArea;
380 if (!(directions & MAX_IGNORE_XINERAMA)) {
381 WScreen *scr = wwin->screen_ptr;
382 int head;
384 if (directions & MAX_KEYBOARD)
385 head = wGetHeadForWindow(wwin);
386 else
387 head = wGetHeadForPointerLocation(scr);
389 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
393 /* Only save directions, not kbd or xinerama hints */
394 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
396 if (WFLAGP(wwin, full_maximize))
397 usableArea = totalArea;
398 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
399 half_scr_height = (usableArea.y2 - usableArea.y1)/2;
401 if (wwin->flags.shaded) {
402 wwin->flags.skip_next_animation = 1;
403 wUnshadeWindow(wwin);
406 if (directions & MAX_MAXIMUS) {
407 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
408 new_width = maximus_width - adj_size;
409 new_height = maximus_height - adj_size;
410 new_x = maximus_x;
411 new_y = maximus_y;
412 if (WFLAGP(wwin, full_maximize) && (new_y == 0)) {
413 new_height += wwin->frame->bottom_width - 1;
414 new_y -= wwin->frame->top_width;
417 wwin->maximus_x = new_x;
418 wwin->maximus_y = new_y;
419 wwin->flags.old_maximized |= MAX_MAXIMUS;
420 } else {
421 /* set default values if no option set then */
422 if (!(directions & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS))) {
423 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
424 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
426 if (!(directions & (MAX_VERTICAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS))) {
427 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
428 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
431 /* left|right position */
432 if (directions & MAX_LEFTHALF) {
433 new_width = half_scr_width - adj_size;
434 new_x = usableArea.x1;
435 } else if (directions & MAX_RIGHTHALF) {
436 new_width = half_scr_width - adj_size;
437 new_x = usableArea.x1 + half_scr_width;
439 /* top|bottom position */
440 if (directions & MAX_TOPHALF) {
441 new_height = half_scr_height - adj_size;
442 new_y = usableArea.y1;
443 } else if (directions & MAX_BOTTOMHALF) {
444 new_height = half_scr_height - adj_size;
445 new_y = usableArea.y1 + half_scr_height;
448 /* vertical|horizontal position */
449 if (directions & MAX_HORIZONTAL) {
450 new_width = usableArea.x2 - usableArea.x1 - adj_size;
451 new_x = usableArea.x1;
453 if (directions & MAX_VERTICAL) {
454 new_height = usableArea.y2 - usableArea.y1 - adj_size;
455 new_y = usableArea.y1;
456 if (WFLAGP(wwin, full_maximize) && (new_y == 0))
457 new_y -= wwin->frame->top_width;
461 if (!WFLAGP(wwin, full_maximize) && !(directions == MAX_MAXIMUS || directions == MAX_HORIZONTAL))
462 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
464 /* set maximization state */
465 wwin->flags.maximized = directions;
466 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
467 wwin->flags.maximized = MAX_MAXIMUS;
469 wWindowConstrainSize(wwin, &new_width, &new_height);
471 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
472 usableArea.y2 - usableArea.y1, &new_width, &new_height);
474 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
475 wWindowSynthConfigureNotify(wwin);
477 WMPostNotificationName(WMNChangedState, wwin, "maximize");
480 /* generic (un)maximizer */
481 void handleMaximize(WWindow *wwin, int directions)
483 int current = wwin->flags.maximized;
484 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
485 int effective = requested ^ current;
486 int flags = directions & ~requested;
488 if (!effective) {
489 /* allow wMaximizeWindow to restore the Maximusized size */
490 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
491 !(requested & MAX_MAXIMUS))
492 wMaximizeWindow(wwin, MAX_MAXIMUS | flags);
493 else
494 wUnmaximizeWindow(wwin);
495 /* these alone mean vertical|horizontal toggle */
496 } else if ((effective == MAX_LEFTHALF) ||
497 (effective == MAX_RIGHTHALF) ||
498 (effective == MAX_TOPHALF) ||
499 (effective == MAX_BOTTOMHALF))
500 wUnmaximizeWindow(wwin);
501 else {
502 if ((requested == (MAX_HORIZONTAL | MAX_VERTICAL)) ||
503 (requested == MAX_MAXIMUS))
504 effective = requested;
505 else {
506 if (requested & MAX_LEFTHALF) {
507 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
508 effective |= MAX_VERTICAL;
509 else
510 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
511 effective |= MAX_LEFTHALF;
512 effective &= ~(MAX_HORIZONTAL | MAX_RIGHTHALF);
513 } else if (requested & MAX_RIGHTHALF) {
514 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
515 effective |= MAX_VERTICAL;
516 else
517 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
518 effective |= MAX_RIGHTHALF;
519 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF);
521 if (requested & MAX_TOPHALF) {
522 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
523 effective |= MAX_HORIZONTAL;
524 else
525 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
526 effective |= MAX_TOPHALF;
527 effective &= ~(MAX_VERTICAL | MAX_BOTTOMHALF);
528 } else if (requested & MAX_BOTTOMHALF) {
529 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
530 effective |= MAX_HORIZONTAL;
531 else
532 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
533 effective |= MAX_BOTTOMHALF;
534 effective &= ~(MAX_VERTICAL | MAX_TOPHALF);
536 if (requested & MAX_HORIZONTAL)
537 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
538 if (requested & MAX_VERTICAL)
539 effective &= ~(MAX_TOPHALF | MAX_BOTTOMHALF);
540 effective &= ~MAX_MAXIMUS;
542 wMaximizeWindow(wwin, effective | flags);
546 /* the window boundary coordinates */
547 typedef struct {
548 int left;
549 int right;
550 int bottom;
551 int top;
552 int width;
553 int height;
554 } win_coords;
556 static void set_window_coords(WWindow *wwin, win_coords *obs)
558 obs->left = wwin->frame_x;
559 obs->top = wwin->frame_y;
560 obs->width = wwin->frame->core->width;
561 obs->height = wwin->frame->core->height;
562 obs->bottom = obs->top + obs->height;
563 obs->right = obs->left + obs->width;
567 * Maximus: tiled maximization (maximize without overlapping other windows)
569 * The original window 'orig' will be maximized to new coordinates 'new'.
570 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
572 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
573 unsigned int *new_width, unsigned int *new_height)
575 WWindow *tmp;
576 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
577 short int adjust_height;
578 int x_intsect, y_intsect;
579 /* the obstructing, original and new windows */
580 win_coords obs, orig, new;
582 /* set the original coordinate positions of the window to be Maximumized */
583 if (wwin->flags.maximized) {
584 /* window is already maximized; consider original geometry */
585 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
586 orig.bottom = orig.top + orig.height;
587 orig.right = orig.left + orig.width;
588 } else
589 set_window_coords(wwin, &orig);
591 /* Try to fully maximize first, then readjust later */
592 new.left = usableArea.x1;
593 new.right = usableArea.x2;
594 new.top = usableArea.y1;
595 new.bottom = usableArea.y2;
597 if (HAS_TITLEBAR(wwin))
598 tbar_height_0 = TITLEBAR_HEIGHT;
599 if (HAS_RESIZEBAR(wwin))
600 rbar_height_0 = RESIZEBAR_HEIGHT;
601 if (HAS_BORDER(wwin))
602 bd_width_0 = wwin->screen_ptr->frame_border_width;
604 /* the length to be subtracted if the window has titlebar, etc */
605 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
607 tmp = wwin;
608 /* The focused window is always the last in the list */
609 while (tmp->prev) {
610 /* ignore windows in other workspaces etc */
611 if (tmp->prev->frame->workspace != w_global.workspace.current ||
612 tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
613 tmp = tmp->prev;
614 continue;
616 tmp = tmp->prev;
618 /* Set the coordinates of obstructing window */
619 set_window_coords(tmp, &obs);
621 /* Try to maximize in the y direction first */
622 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
623 if (x_intsect != 0) {
624 /* TODO: Consider the case when coords are equal */
625 if (obs.bottom < orig.top && obs.bottom > new.top) {
626 /* w_0 is below the bottom of w_j */
627 new.top = obs.bottom + 1;
629 if (orig.bottom < obs.top && obs.top < new.bottom) {
630 /* The bottom of w_0 is above the top of w_j */
631 new.bottom = obs.top - 1;
636 tmp = wwin;
637 while (tmp->prev) {
638 if (tmp->prev->frame->workspace != w_global.workspace.current ||
639 tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
640 tmp = tmp->prev;
641 continue;
643 tmp = tmp->prev;
645 set_window_coords(tmp, &obs);
648 * Use the new.top and new.height instead of original values
649 * as they may have different intersections with the obstructing windows
651 new.height = new.bottom - new.top - adjust_height;
652 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
653 if (y_intsect != 0) {
654 if (obs.right < orig.left && obs.right > new.left) {
655 /* w_0 is completely to the right of w_j */
656 new.left = obs.right + 1;
658 if (orig.right < obs.left && obs.left < new.right) {
659 /* w_0 is completely to the left of w_j */
660 new.right = obs.left - 1;
665 *new_x = new.left;
666 *new_y = new.top;
667 /* xcalc needs -7 here, but other apps don't */
668 *new_height = new.bottom - new.top - adjust_height - 1;
669 *new_width = new.right - new.left;
672 void wUnmaximizeWindow(WWindow *wwin)
674 int x, y, w, h;
676 if (!wwin->flags.maximized)
677 return;
679 if (wwin->flags.shaded) {
680 wwin->flags.skip_next_animation = 1;
681 wUnshadeWindow(wwin);
684 /* Use old coordinates if they are set, current values otherwise */
685 remember_geometry(wwin, &x, &y, &w, &h);
687 /* unMaximusize relative to original position */
688 if (wwin->flags.maximized & MAX_MAXIMUS) {
689 x += wwin->frame_x - wwin->maximus_x;
690 y += wwin->frame_y - wwin->maximus_y;
693 wwin->flags.maximized = 0;
694 wwin->flags.old_maximized = 0;
695 wWindowConfigure(wwin, x, y, w, h);
696 wWindowSynthConfigureNotify(wwin);
698 WMPostNotificationName(WMNChangedState, wwin, "maximize");
701 void wFullscreenWindow(WWindow *wwin)
703 int head;
704 WMRect rect;
706 if (wwin->flags.fullscreen)
707 return;
709 wwin->flags.fullscreen = True;
711 wWindowConfigureBorders(wwin);
713 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
714 wRaiseFrame(wwin->frame->core);
716 wwin->bfs_geometry.x = wwin->frame_x;
717 wwin->bfs_geometry.y = wwin->frame_y;
718 wwin->bfs_geometry.width = wwin->frame->core->width;
719 wwin->bfs_geometry.height = wwin->frame->core->height;
721 head = wGetHeadForWindow(wwin);
722 rect = wGetRectForHead(wwin->screen_ptr, head);
723 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
725 wwin->screen_ptr->bfs_focused_window = wwin->screen_ptr->focused_window;
726 wSetFocusTo(wwin->screen_ptr, wwin);
728 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
731 void wUnfullscreenWindow(WWindow *wwin)
733 if (!wwin->flags.fullscreen)
734 return;
736 wwin->flags.fullscreen = False;
738 if (WFLAGP(wwin, sunken))
739 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
740 else if (WFLAGP(wwin, floating))
741 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
743 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
744 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
746 wWindowConfigureBorders(wwin);
748 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
749 wFrameWindowPaint(wwin->frame);
752 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
754 if (wwin->screen_ptr->bfs_focused_window) {
755 wSetFocusTo(wwin->screen_ptr, wwin->screen_ptr->bfs_focused_window);
756 wwin->screen_ptr->bfs_focused_window = NULL;
760 #ifdef ANIMATIONS
761 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
763 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
764 float cx, cy, cw, ch;
765 float xstep, ystep, wstep, hstep;
766 XPoint points[5];
767 float dx, dch, midy;
768 float angle, final_angle, delta;
770 xstep = (float)(fx - x) / steps;
771 ystep = (float)(fy - y) / steps;
772 wstep = (float)(fw - w) / steps;
773 hstep = (float)(fh - h) / steps;
775 cx = (float)x;
776 cy = (float)y;
777 cw = (float)w;
778 ch = (float)h;
780 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
781 delta = (float)(final_angle / FRAMES);
782 for (angle = 0;; angle += delta) {
783 if (angle > final_angle)
784 angle = final_angle;
786 dx = (cw / 10) - ((cw / 5) * sin(angle));
787 dch = (ch / 2) * cos(angle);
788 midy = cy + (ch / 2);
790 points[0].x = cx + dx;
791 points[0].y = midy - dch;
792 points[1].x = cx + cw - dx;
793 points[1].y = points[0].y;
794 points[2].x = cx + cw + dx;
795 points[2].y = midy + dch;
796 points[3].x = cx - dx;
797 points[3].y = points[2].y;
798 points[4].x = points[0].x;
799 points[4].y = points[0].y;
801 XGrabServer(dpy);
802 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
803 XFlush(dpy);
804 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
806 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
807 XUngrabServer(dpy);
808 cx += xstep;
809 cy += ystep;
810 cw += wstep;
811 ch += hstep;
812 if (angle >= final_angle)
813 break;
816 XFlush(dpy);
819 #undef FRAMES
821 static void
822 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
824 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
825 float cx, cy, cw, ch;
826 float xstep, ystep, wstep, hstep;
827 XPoint points[5];
828 float angle, final_angle, a, d, delta;
830 x += w / 2;
831 y += h / 2;
832 fx += fw / 2;
833 fy += fh / 2;
835 xstep = (float)(fx - x) / steps;
836 ystep = (float)(fy - y) / steps;
837 wstep = (float)(fw - w) / steps;
838 hstep = (float)(fh - h) / steps;
840 cx = (float)x;
841 cy = (float)y;
842 cw = (float)w;
843 ch = (float)h;
845 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
846 delta = (float)(final_angle / FRAMES);
847 for (angle = 0;; angle += delta) {
848 if (angle > final_angle)
849 angle = final_angle;
851 a = atan(ch / cw);
852 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
854 points[0].x = cx + cos(angle - a) * d;
855 points[0].y = cy + sin(angle - a) * d;
856 points[1].x = cx + cos(angle + a) * d;
857 points[1].y = cy + sin(angle + a) * d;
858 points[2].x = cx + cos(angle - a + WM_PI) * d;
859 points[2].y = cy + sin(angle - a + WM_PI) * d;
860 points[3].x = cx + cos(angle + a + WM_PI) * d;
861 points[3].y = cy + sin(angle + a + WM_PI) * d;
862 points[4].x = cx + cos(angle - a) * d;
863 points[4].y = cy + sin(angle - a) * d;
864 XGrabServer(dpy);
865 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
866 XFlush(dpy);
867 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
869 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
870 XUngrabServer(dpy);
871 cx += xstep;
872 cy += ystep;
873 cw += wstep;
874 ch += hstep;
875 if (angle >= final_angle)
876 break;
879 XFlush(dpy);
882 #undef FRAMES
884 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
886 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
887 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
888 float xstep, ystep, wstep, hstep;
889 int i, j;
891 xstep = (float)(fx - x) / steps;
892 ystep = (float)(fy - y) / steps;
893 wstep = (float)(fw - w) / steps;
894 hstep = (float)(fh - h) / steps;
896 for (j = 0; j < FRAMES; j++) {
897 cx[j] = (float)x;
898 cy[j] = (float)y;
899 cw[j] = (float)w;
900 ch[j] = (float)h;
902 XGrabServer(dpy);
903 for (i = 0; i < steps; i++) {
904 for (j = 0; j < FRAMES; j++) {
905 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
906 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
908 XFlush(dpy);
909 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
911 for (j = 0; j < FRAMES; j++) {
912 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
913 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
914 if (j < FRAMES - 1) {
915 cx[j] = cx[j + 1];
916 cy[j] = cy[j + 1];
917 cw[j] = cw[j + 1];
918 ch[j] = ch[j + 1];
919 } else {
920 cx[j] += xstep;
921 cy[j] += ystep;
922 cw[j] += wstep;
923 ch[j] += hstep;
928 for (j = 0; j < FRAMES; j++)
929 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
930 XFlush(dpy);
931 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
933 for (j = 0; j < FRAMES; j++)
934 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
936 XUngrabServer(dpy);
939 #undef FRAMES
941 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
943 int style = wPreferences.iconification_style; /* Catch the value */
944 int steps;
946 if (style == WIS_NONE)
947 return;
949 if (style == WIS_RANDOM)
950 style = rand() % 3;
952 switch (style) {
953 case WIS_TWIST:
954 steps = MINIATURIZE_ANIMATION_STEPS_T;
955 if (steps > 0)
956 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
957 break;
958 case WIS_FLIP:
959 steps = MINIATURIZE_ANIMATION_STEPS_F;
960 if (steps > 0)
961 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
962 break;
963 case WIS_ZOOM:
964 default:
965 steps = MINIATURIZE_ANIMATION_STEPS_Z;
966 if (steps > 0)
967 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
968 break;
971 #endif /* ANIMATIONS */
973 static void flushExpose(void)
975 XEvent tmpev;
977 while (XCheckTypedEvent(dpy, Expose, &tmpev))
978 WMHandleEvent(&tmpev);
979 XSync(dpy, 0);
982 static void unmapTransientsFor(WWindow *wwin)
984 WWindow *tmp;
986 tmp = wwin->screen_ptr->focused_window;
987 while (tmp) {
988 /* unmap the transients for this transient */
989 if (tmp != wwin && tmp->transient_for == wwin->client_win
990 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
991 unmapTransientsFor(tmp);
992 tmp->flags.miniaturized = 1;
993 if (!tmp->flags.shaded)
994 wWindowUnmap(tmp);
995 else
996 XUnmapWindow(dpy, tmp->frame->core->window);
998 if (!tmp->flags.shaded)
1000 wClientSetState(tmp, IconicState, None);
1002 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1004 tmp = tmp->prev;
1008 static void mapTransientsFor(WWindow *wwin)
1010 WWindow *tmp;
1012 tmp = wwin->screen_ptr->focused_window;
1013 while (tmp) {
1014 /* recursively map the transients for this transient */
1015 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1016 && tmp->icon == NULL) {
1017 mapTransientsFor(tmp);
1018 tmp->flags.miniaturized = 0;
1019 if (!tmp->flags.shaded)
1020 wWindowMap(tmp);
1021 else
1022 XMapWindow(dpy, tmp->frame->core->window);
1023 tmp->flags.semi_focused = 0;
1025 if (!tmp->flags.shaded)
1027 wClientSetState(tmp, NormalState, None);
1029 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1031 tmp = tmp->prev;
1035 static WWindow *recursiveTransientFor(WWindow *wwin)
1037 int i;
1039 if (!wwin)
1040 return None;
1042 /* hackish way to detect transient_for cycle */
1043 i = wwin->screen_ptr->window_count + 1;
1045 while (wwin && wwin->transient_for != None && i > 0) {
1046 wwin = wWindowFor(wwin->transient_for);
1047 i--;
1049 if (i == 0 && wwin) {
1050 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
1051 return NULL;
1054 return wwin;
1057 static int getAnimationGeometry(WWindow *wwin, int *ix, int *iy, int *iw, int *ih)
1059 if (wwin->screen_ptr->flags.startup || wPreferences.no_animations
1060 || wwin->flags.skip_next_animation || wwin->icon == NULL)
1061 return 0;
1063 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1064 *ix = wwin->icon_x;
1065 *iy = wwin->icon_y;
1066 *iw = wwin->icon->core->width;
1067 *ih = wwin->icon->core->height;
1068 } else {
1069 if (wwin->flags.net_handle_icon) {
1070 *ix = wwin->icon_x;
1071 *iy = wwin->icon_y;
1072 *iw = wwin->icon_w;
1073 *ih = wwin->icon_h;
1074 } else {
1075 *ix = 0;
1076 *iy = 0;
1077 *iw = wwin->screen_ptr->scr_width;
1078 *ih = wwin->screen_ptr->scr_height;
1081 return 1;
1084 void wIconifyWindow(WWindow *wwin)
1086 XWindowAttributes attribs;
1087 int present;
1089 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1090 return; /* the window doesn't exist anymore */
1092 if (wwin->flags.miniaturized)
1093 return; /* already miniaturized */
1095 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1096 WWindow *owner = wWindowFor(wwin->transient_for);
1098 if (owner && owner->flags.miniaturized)
1099 return;
1102 present = wwin->frame->workspace == w_global.workspace.current;
1104 /* if the window is in another workspace, simplify process */
1105 if (present) {
1106 /* icon creation may take a while */
1107 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1108 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1109 GrabModeAsync, None, None, CurrentTime);
1112 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1113 if (!wwin->flags.icon_moved)
1114 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1116 wwin->icon = icon_create_for_wwindow(wwin);
1117 wwin->icon->mapped = 1;
1119 /* extract the window screenshot everytime, as the option can be enable anytime */
1120 if (wwin->client_win && wwin->flags.mapped) {
1121 RImage *apercu;
1122 XImage *pimg;
1123 unsigned int w, h;
1124 int x, y;
1125 Window baz;
1127 XRaiseWindow(dpy, wwin->frame->core->window);
1128 XTranslateCoordinates(dpy, wwin->client_win, wwin->screen_ptr->root_win, 0, 0, &x, &y, &baz);
1130 w = attribs.width;
1131 h = attribs.height;
1133 if (x - attribs.x + attribs.width > wwin->screen_ptr->scr_width)
1134 w = wwin->screen_ptr->scr_width - x + attribs.x;
1136 if (y - attribs.y + attribs.height > wwin->screen_ptr->scr_height)
1137 h = wwin->screen_ptr->scr_height - y + attribs.y;
1139 pimg = XGetImage(dpy, wwin->client_win, 0, 0, w, h, AllPlanes, ZPixmap);
1140 if (pimg) {
1141 apercu = RCreateImageFromXImage(wwin->screen_ptr->rcontext, pimg, NULL);
1142 XDestroyImage(pimg);
1144 if (apercu) {
1145 set_icon_apercu(wwin->icon, apercu);
1146 RReleaseImage(apercu);
1147 } else {
1148 wwarning("window apercu creation failed");
1154 wwin->flags.miniaturized = 1;
1155 wwin->flags.mapped = 0;
1157 /* unmap transients */
1158 unmapTransientsFor(wwin);
1160 if (present) {
1161 #ifdef ANIMATIONS
1162 int ix, iy, iw, ih;
1163 #endif
1164 XUngrabPointer(dpy, CurrentTime);
1165 wWindowUnmap(wwin);
1166 /* let all Expose events arrive so that we can repaint
1167 * something before the animation starts (and the server is grabbed) */
1168 XSync(dpy, 0);
1170 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1171 wClientSetState(wwin, IconicState, None);
1172 else
1173 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1175 flushExpose();
1176 #ifdef ANIMATIONS
1177 if (getAnimationGeometry(wwin, &ix, &iy, &iw, &ih))
1178 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1179 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1180 #endif
1183 wwin->flags.skip_next_animation = 0;
1185 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1186 if (w_global.workspace.current == wwin->frame->workspace ||
1187 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1188 XMapWindow(dpy, wwin->icon->core->window);
1190 AddToStackList(wwin->icon->core);
1191 wLowerFrame(wwin->icon->core);
1194 if (present) {
1195 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1198 * It doesn't seem to be working and causes button event hangup
1199 * when deiconifying a transient window.
1200 setupIconGrabs(wwin->icon);
1202 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1203 && wPreferences.focus_mode == WKF_CLICK) {
1204 WWindow *tmp;
1206 tmp = wwin->prev;
1207 while (tmp) {
1208 if (!WFLAGP(tmp, no_focusable)
1209 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1210 && (wwin->frame->workspace == tmp->frame->workspace))
1211 break;
1212 tmp = tmp->prev;
1214 wSetFocusTo(wwin->screen_ptr, tmp);
1215 } else if (wPreferences.focus_mode != WKF_CLICK) {
1216 wSetFocusTo(wwin->screen_ptr, NULL);
1218 #ifdef ANIMATIONS
1219 if (!wwin->screen_ptr->flags.startup) {
1220 /* Catch up with events not processed while animation was running */
1221 Window clientwin = wwin->client_win;
1223 ProcessPendingEvents();
1225 /* the window can disappear while ProcessPendingEvents() runs */
1226 if (!wWindowFor(clientwin))
1227 return;
1229 #endif
1232 /* maybe we want to do this regardless of net_handle_icon
1233 * it seems to me we might break behaviour this way.
1235 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1236 && !wwin->flags.net_handle_icon)
1237 wIconSelect(wwin->icon);
1239 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1241 if (wPreferences.auto_arrange_icons)
1242 wArrangeIcons(wwin->screen_ptr, True);
1245 void wDeiconifyWindow(WWindow *wwin)
1247 /* Let's avoid changing workspace while deiconifying */
1248 w_global.workspace.ignore_change = True;
1250 /* we're hiding for show_desktop */
1251 int netwm_hidden = wwin->flags.net_show_desktop &&
1252 wwin->frame->workspace != w_global.workspace.current;
1254 if (!netwm_hidden)
1255 wWindowChangeWorkspace(wwin, w_global.workspace.current);
1257 if (!wwin->flags.miniaturized) {
1258 w_global.workspace.ignore_change = False;
1259 return;
1262 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1263 WWindow *owner = recursiveTransientFor(wwin);
1265 if (owner && owner->flags.miniaturized) {
1266 wDeiconifyWindow(owner);
1267 wSetFocusTo(wwin->screen_ptr, wwin);
1268 wRaiseFrame(wwin->frame->core);
1269 w_global.workspace.ignore_change = False;
1270 return;
1274 wwin->flags.miniaturized = 0;
1276 if (!netwm_hidden && !wwin->flags.shaded)
1277 wwin->flags.mapped = 1;
1279 if (!netwm_hidden || wPreferences.sticky_icons) {
1280 /* maybe we want to do this regardless of net_handle_icon
1281 * it seems to me we might break behaviour this way.
1283 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1284 && wwin->icon != NULL) {
1285 if (wwin->icon->selected)
1286 wIconSelect(wwin->icon);
1288 XUnmapWindow(dpy, wwin->icon->core->window);
1292 /* if the window is in another workspace, do it silently */
1293 if (!netwm_hidden) {
1294 #ifdef ANIMATIONS
1295 int ix, iy, iw, ih;
1296 if (getAnimationGeometry(wwin, &ix, &iy, &iw, &ih))
1297 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1298 wwin->frame_x, wwin->frame_y,
1299 wwin->frame->core->width, wwin->frame->core->height);
1300 #endif
1301 wwin->flags.skip_next_animation = 0;
1302 XGrabServer(dpy);
1303 if (!wwin->flags.shaded)
1304 XMapWindow(dpy, wwin->client_win);
1306 XMapWindow(dpy, wwin->frame->core->window);
1307 wRaiseFrame(wwin->frame->core);
1308 if (!wwin->flags.shaded)
1309 wClientSetState(wwin, NormalState, None);
1311 mapTransientsFor(wwin);
1314 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1315 && !wwin->flags.net_handle_icon) {
1316 RemoveFromStackList(wwin->icon->core);
1317 wSetFocusTo(wwin->screen_ptr, wwin);
1318 wIconDestroy(wwin->icon);
1319 wwin->icon = NULL;
1322 if (!netwm_hidden) {
1323 XUngrabServer(dpy);
1325 wSetFocusTo(wwin->screen_ptr, wwin);
1327 #ifdef ANIMATIONS
1328 if (!wwin->screen_ptr->flags.startup) {
1329 /* Catch up with events not processed while animation was running */
1330 Window clientwin = wwin->client_win;
1332 ProcessPendingEvents();
1334 /* the window can disappear while ProcessPendingEvents() runs */
1335 if (!wWindowFor(clientwin)) {
1336 w_global.workspace.ignore_change = False;
1337 return;
1340 #endif
1343 if (wPreferences.auto_arrange_icons)
1344 wArrangeIcons(wwin->screen_ptr, True);
1346 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1348 /* In case we were shaded and iconified, also unshade */
1349 if (!netwm_hidden)
1350 wUnshadeWindow(wwin);
1352 w_global.workspace.ignore_change = False;
1355 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1357 if (wwin->flags.miniaturized) {
1358 if (wwin->icon) {
1359 XUnmapWindow(dpy, wwin->icon->core->window);
1360 wwin->icon->mapped = 0;
1362 wwin->flags.hidden = 1;
1364 WMPostNotificationName(WMNChangedState, wwin, "hide");
1365 return;
1368 if (wwin->flags.inspector_open)
1369 wHideInspectorForWindow(wwin);
1371 wwin->flags.hidden = 1;
1372 wWindowUnmap(wwin);
1374 wClientSetState(wwin, IconicState, icon->icon_win);
1375 flushExpose();
1377 #ifdef ANIMATIONS
1378 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1379 !wwin->flags.skip_next_animation && animate) {
1380 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1381 wwin->frame->core->width, wwin->frame->core->height,
1382 icon_x, icon_y, icon->core->width, icon->core->height);
1384 #endif
1385 wwin->flags.skip_next_animation = 0;
1387 WMPostNotificationName(WMNChangedState, wwin, "hide");
1390 void wHideAll(WScreen *scr)
1392 WWindow *wwin;
1393 WWindow **windows;
1394 WMenu *menu;
1395 unsigned int wcount = 0;
1396 int i;
1398 if (!scr)
1399 return;
1401 menu = scr->switch_menu;
1403 windows = wmalloc(sizeof(WWindow *));
1405 if (menu != NULL) {
1406 for (i = 0; i < menu->entry_no; i++) {
1407 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1408 wcount++;
1409 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1411 } else {
1412 wwin = scr->focused_window;
1414 while (wwin) {
1415 windows[wcount] = wwin;
1416 wcount++;
1417 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1418 wwin = wwin->prev;
1423 for (i = 0; i < wcount; i++) {
1424 wwin = windows[i];
1425 if (wwin->frame->workspace == w_global.workspace.current
1426 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1427 && !wwin->flags.internal_window
1428 && !WFLAGP(wwin, no_miniaturizable)
1430 wwin->flags.skip_next_animation = 1;
1431 wIconifyWindow(wwin);
1435 wfree(windows);
1438 void wHideOtherApplications(WWindow *awin)
1440 WWindow *wwin;
1441 WApplication *tapp;
1443 if (!awin)
1444 return;
1445 wwin = awin->screen_ptr->focused_window;
1447 while (wwin) {
1448 if (wwin != awin
1449 && wwin->frame->workspace == w_global.workspace.current
1450 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1451 && !wwin->flags.internal_window
1452 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1454 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1455 if (!WFLAGP(wwin, no_miniaturizable)) {
1456 wwin->flags.skip_next_animation = 1;
1457 wIconifyWindow(wwin);
1459 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1460 tapp = wApplicationOf(wwin->main_window);
1461 if (tapp) {
1462 tapp->flags.skip_next_animation = 1;
1463 wHideApplication(tapp);
1464 } else {
1465 if (!WFLAGP(wwin, no_miniaturizable)) {
1466 wwin->flags.skip_next_animation = 1;
1467 wIconifyWindow(wwin);
1472 wwin = wwin->prev;
1475 wSetFocusTo(awin->screen_ptr, awin);
1479 void wHideApplication(WApplication *wapp)
1481 WScreen *scr;
1482 WWindow *wlist;
1483 int hadfocus;
1484 int animate;
1486 if (!wapp) {
1487 wwarning("trying to hide a non grouped window");
1488 return;
1490 if (!wapp->main_window_desc) {
1491 wwarning("group leader not found for window group");
1492 return;
1494 scr = wapp->main_window_desc->screen_ptr;
1495 hadfocus = 0;
1496 wlist = scr->focused_window;
1497 if (!wlist)
1498 return;
1500 if (wlist->main_window == wapp->main_window)
1501 wapp->last_focused = wlist;
1502 else
1503 wapp->last_focused = NULL;
1505 animate = !wapp->flags.skip_next_animation;
1507 while (wlist) {
1508 if (wlist->main_window == wapp->main_window) {
1509 if (wlist->flags.focused)
1510 hadfocus = 1;
1511 if (wapp->app_icon) {
1512 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1513 wapp->app_icon->y_pos, wlist, animate);
1514 animate = False;
1517 wlist = wlist->prev;
1520 wapp->flags.skip_next_animation = 0;
1522 if (hadfocus) {
1523 if (wPreferences.focus_mode == WKF_CLICK) {
1524 wlist = scr->focused_window;
1525 while (wlist) {
1526 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1527 && (wlist->flags.mapped || wlist->flags.shaded))
1528 break;
1529 wlist = wlist->prev;
1531 wSetFocusTo(scr, wlist);
1532 } else {
1533 wSetFocusTo(scr, NULL);
1537 wapp->flags.hidden = 1;
1539 if (wPreferences.auto_arrange_icons)
1540 wArrangeIcons(scr, True);
1542 #ifdef HIDDENDOT
1543 if (wapp->app_icon)
1544 wAppIconPaint(wapp->app_icon);
1545 #endif
1548 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1550 if (bringToCurrentWS)
1551 wWindowChangeWorkspace(wwin, w_global.workspace.current);
1553 wwin->flags.hidden = 0;
1555 #ifdef ANIMATIONS
1556 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1557 animateResize(wwin->screen_ptr, icon_x, icon_y,
1558 icon->core->width, icon->core->height,
1559 wwin->frame_x, wwin->frame_y,
1560 wwin->frame->core->width, wwin->frame->core->height);
1562 #endif
1563 wwin->flags.skip_next_animation = 0;
1564 if (w_global.workspace.current == wwin->frame->workspace) {
1565 XMapWindow(dpy, wwin->client_win);
1566 XMapWindow(dpy, wwin->frame->core->window);
1567 wClientSetState(wwin, NormalState, None);
1568 wwin->flags.mapped = 1;
1569 wRaiseFrame(wwin->frame->core);
1571 if (wwin->flags.inspector_open)
1572 wUnhideInspectorForWindow(wwin);
1574 WMPostNotificationName(WMNChangedState, wwin, "hide");
1577 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1579 WScreen *scr;
1580 WWindow *wlist, *next;
1581 WWindow *focused = NULL;
1582 int animate;
1584 if (!wapp)
1585 return;
1587 scr = wapp->main_window_desc->screen_ptr;
1588 wlist = scr->focused_window;
1589 if (!wlist)
1590 return;
1592 /* goto beginning of list */
1593 while (wlist->prev)
1594 wlist = wlist->prev;
1596 animate = !wapp->flags.skip_next_animation;
1598 while (wlist) {
1599 next = wlist->next;
1601 if (wlist->main_window == wapp->main_window) {
1602 if (wlist->flags.focused)
1603 focused = wlist;
1604 else if (!focused || !focused->flags.focused)
1605 focused = wlist;
1607 if (wlist->flags.miniaturized) {
1608 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1609 wlist->frame->workspace == w_global.workspace.current) && wlist->icon) {
1610 if (!wlist->icon->mapped) {
1611 int x, y;
1613 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1614 if (wlist->icon_x != x || wlist->icon_y != y)
1615 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1616 wlist->icon_x = x;
1617 wlist->icon_y = y;
1618 XMapWindow(dpy, wlist->icon->core->window);
1619 wlist->icon->mapped = 1;
1621 wRaiseFrame(wlist->icon->core);
1623 if (bringToCurrentWS)
1624 wWindowChangeWorkspace(wlist, w_global.workspace.current);
1625 wlist->flags.hidden = 0;
1626 if (miniwindows && wlist->frame->workspace == w_global.workspace.current)
1627 wDeiconifyWindow(wlist);
1628 WMPostNotificationName(WMNChangedState, wlist, "hide");
1629 } else if (wlist->flags.shaded) {
1630 if (bringToCurrentWS)
1631 wWindowChangeWorkspace(wlist, w_global.workspace.current);
1632 wlist->flags.hidden = 0;
1633 wRaiseFrame(wlist->frame->core);
1634 if (wlist->frame->workspace == w_global.workspace.current) {
1635 XMapWindow(dpy, wlist->frame->core->window);
1636 if (miniwindows)
1637 wUnshadeWindow(wlist);
1639 WMPostNotificationName(WMNChangedState, wlist, "hide");
1640 } else if (wlist->flags.hidden) {
1641 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1642 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1643 animate = False;
1644 } else {
1645 if (bringToCurrentWS && wlist->frame->workspace != w_global.workspace.current)
1646 wWindowChangeWorkspace(wlist, w_global.workspace.current);
1648 wRaiseFrame(wlist->frame->core);
1651 wlist = next;
1654 wapp->flags.skip_next_animation = 0;
1655 wapp->flags.hidden = 0;
1657 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1658 wRaiseFrame(wapp->last_focused->frame->core);
1659 wSetFocusTo(scr, wapp->last_focused);
1660 } else if (focused) {
1661 wSetFocusTo(scr, focused);
1663 wapp->last_focused = NULL;
1664 if (wPreferences.auto_arrange_icons)
1665 wArrangeIcons(scr, True);
1667 #ifdef HIDDENDOT
1668 wAppIconPaint(wapp->app_icon);
1669 #endif
1672 void wShowAllWindows(WScreen *scr)
1674 WWindow *wwin, *old_foc;
1675 WApplication *wapp;
1677 old_foc = wwin = scr->focused_window;
1678 while (wwin) {
1679 if (!wwin->flags.internal_window &&
1680 (w_global.workspace.current == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1681 if (wwin->flags.miniaturized) {
1682 wwin->flags.skip_next_animation = 1;
1683 wDeiconifyWindow(wwin);
1684 } else if (wwin->flags.hidden) {
1685 wapp = wApplicationOf(wwin->main_window);
1686 if (wapp) {
1687 wUnhideApplication(wapp, False, False);
1688 } else {
1689 wwin->flags.skip_next_animation = 1;
1690 wDeiconifyWindow(wwin);
1694 wwin = wwin->prev;
1696 wSetFocusTo(scr, old_foc);
1697 /*wRaiseFrame(old_foc->frame->core); */
1700 void wRefreshDesktop(WScreen *scr)
1702 Window win;
1703 XSetWindowAttributes attr;
1705 attr.backing_store = NotUseful;
1706 attr.save_under = False;
1707 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1708 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1709 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1710 XMapRaised(dpy, win);
1711 XDestroyWindow(dpy, win);
1712 XFlush(dpy);
1715 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1717 WWindow *wwin;
1718 WAppIcon *aicon;
1720 int head;
1721 const int heads = wXineramaHeads(scr);
1723 struct HeadVars {
1724 int pf; /* primary axis */
1725 int sf; /* secondary axis */
1726 int fullW;
1727 int fullH;
1728 int pi, si;
1729 int sx1, sx2, sy1, sy2; /* screen boundary */
1730 int sw, sh;
1731 int xo, yo;
1732 int xs, ys;
1733 } *vars;
1735 int isize = wPreferences.icon_size;
1737 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1739 for (head = 0; head < heads; ++head) {
1740 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1741 WMRect rect;
1743 if (scr->dock) {
1744 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
1746 if (scr->dock->on_right_side)
1747 area.x2 -= offset;
1748 else
1749 area.x1 += offset;
1752 rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1754 vars[head].pi = vars[head].si = 0;
1755 vars[head].sx1 = rect.pos.x;
1756 vars[head].sy1 = rect.pos.y;
1757 vars[head].sw = rect.size.width;
1758 vars[head].sh = rect.size.height;
1759 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1760 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1761 vars[head].sw = isize * (vars[head].sw / isize);
1762 vars[head].sh = isize * (vars[head].sh / isize);
1763 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1764 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1766 /* icon yard boundaries */
1767 if (wPreferences.icon_yard & IY_VERT) {
1768 vars[head].pf = vars[head].fullH;
1769 vars[head].sf = vars[head].fullW;
1770 } else {
1771 vars[head].pf = vars[head].fullW;
1772 vars[head].sf = vars[head].fullH;
1774 if (wPreferences.icon_yard & IY_RIGHT) {
1775 vars[head].xo = vars[head].sx2 - isize;
1776 vars[head].xs = -1;
1777 } else {
1778 vars[head].xo = vars[head].sx1;
1779 vars[head].xs = 1;
1781 if (wPreferences.icon_yard & IY_TOP) {
1782 vars[head].yo = vars[head].sy1;
1783 vars[head].ys = 1;
1784 } else {
1785 vars[head].yo = vars[head].sy2 - isize;
1786 vars[head].ys = -1;
1790 #define X ((wPreferences.icon_yard & IY_VERT) \
1791 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1792 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1794 #define Y ((wPreferences.icon_yard & IY_VERT) \
1795 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1796 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1798 /* arrange application icons */
1799 aicon = w_global.app_icon_list;
1800 /* reverse them to avoid unnecessarily sliding of icons */
1801 while (aicon && aicon->next)
1802 aicon = aicon->next;
1804 while (aicon) {
1805 if (!aicon->docked) {
1806 /* CHECK: can icon be NULL here ? */
1807 /* The intention here is to place the AppIcon on the head that
1808 * contains most of the applications _main_ window. */
1809 head = wGetHeadForWindow(aicon->icon->owner);
1811 if (aicon->x_pos != X || aicon->y_pos != Y) {
1812 #ifdef ANIMATIONS
1813 if (!wPreferences.no_animations)
1814 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1815 #endif /* ANIMATIONS */
1817 wAppIconMove(aicon, X, Y);
1818 vars[head].pi++;
1819 if (vars[head].pi >= vars[head].pf) {
1820 vars[head].pi = 0;
1821 vars[head].si++;
1824 aicon = aicon->prev;
1827 /* arrange miniwindows */
1828 wwin = scr->focused_window;
1829 /* reverse them to avoid unnecessarily shuffling */
1830 while (wwin && wwin->prev)
1831 wwin = wwin->prev;
1833 while (wwin) {
1834 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1835 (wwin->frame->workspace == w_global.workspace.current ||
1836 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1838 head = wGetHeadForWindow(wwin);
1840 if (arrangeAll || !wwin->flags.icon_moved) {
1841 if (wwin->icon_x != X || wwin->icon_y != Y)
1842 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1844 wwin->icon_x = X;
1845 wwin->icon_y = Y;
1847 vars[head].pi++;
1848 if (vars[head].pi >= vars[head].pf) {
1849 vars[head].pi = 0;
1850 vars[head].si++;
1854 if (arrangeAll)
1855 wwin->flags.icon_moved = 0;
1856 /* we reversed the order, so we use next */
1857 wwin = wwin->next;
1860 wfree(vars);
1863 void wSelectWindow(WWindow *wwin, Bool flag)
1865 WScreen *scr = wwin->screen_ptr;
1867 if (flag) {
1868 wwin->flags.selected = 1;
1869 if (wwin->frame->selected_border_pixel)
1870 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1871 else
1872 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1874 if (!HAS_BORDER(wwin))
1875 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1877 if (!scr->selected_windows)
1878 scr->selected_windows = WMCreateArray(4);
1879 WMAddToArray(scr->selected_windows, wwin);
1880 } else {
1881 wwin->flags.selected = 0;
1882 if (wwin->flags.focused) {
1883 if (wwin->frame->focused_border_pixel)
1884 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->focused_border_pixel);
1885 else
1886 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_focused_border_pixel);
1887 } else {
1888 if (wwin->frame->border_pixel)
1889 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1890 else
1891 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1894 if (!HAS_BORDER(wwin))
1895 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1897 if (scr->selected_windows)
1898 WMRemoveFromArray(scr->selected_windows, wwin);
1902 void wMakeWindowVisible(WWindow *wwin)
1904 if (wwin->frame->workspace != w_global.workspace.current)
1905 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1907 if (wwin->flags.shaded)
1908 wUnshadeWindow(wwin);
1910 if (wwin->flags.hidden) {
1911 WApplication *app;
1913 app = wApplicationOf(wwin->main_window);
1914 if (app) {
1915 /* trick to get focus to this window */
1916 app->last_focused = wwin;
1917 wUnhideApplication(app, False, False);
1920 if (wwin->flags.miniaturized) {
1921 wDeiconifyWindow(wwin);
1922 } else {
1923 if (!WFLAGP(wwin, no_focusable))
1924 wSetFocusTo(wwin->screen_ptr, wwin);
1925 wRaiseFrame(wwin->frame->core);
1930 * Do the animation while shading (called with what = SHADE)
1931 * or unshading (what = UNSHADE).
1933 #ifdef ANIMATIONS
1934 static void shade_animate(WWindow *wwin, Bool what)
1936 int y, s, w, h;
1937 time_t time0 = time(NULL);
1939 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1940 return;
1942 switch (what) {
1943 case SHADE:
1944 if (!wwin->screen_ptr->flags.startup) {
1945 /* do the shading animation */
1946 h = wwin->frame->core->height;
1947 s = h / SHADE_STEPS;
1948 if (s < 1)
1949 s = 1;
1950 w = wwin->frame->core->width;
1951 y = wwin->frame->top_width;
1952 while (h > wwin->frame->top_width + 1) {
1953 XMoveWindow(dpy, wwin->client_win, 0, y);
1954 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1955 XFlush(dpy);
1957 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1958 break;
1960 if (SHADE_DELAY > 0)
1961 wusleep(SHADE_DELAY * 1000L);
1962 else
1963 wusleep(10);
1964 h -= s;
1965 y -= s;
1967 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1969 break;
1971 case UNSHADE:
1972 h = wwin->frame->top_width + wwin->frame->bottom_width;
1973 y = wwin->frame->top_width - wwin->client.height;
1974 s = abs(y) / SHADE_STEPS;
1975 if (s < 1)
1976 s = 1;
1977 w = wwin->frame->core->width;
1978 XMoveWindow(dpy, wwin->client_win, 0, y);
1979 if (s > 0) {
1980 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1981 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1982 XMoveWindow(dpy, wwin->client_win, 0, y);
1983 XFlush(dpy);
1984 if (SHADE_DELAY > 0)
1985 wusleep(SHADE_DELAY * 2000L / 3);
1986 else
1987 wusleep(10);
1988 h += s;
1989 y += s;
1991 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1992 break;
1995 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1996 break;
1999 #endif