wmaker: Moved timestamp variable (#1) to the global variable structure
[wmaker-crm.git] / src / actions.c
blob0b4f450345452e95be78264e50260bb9a1fca9be
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
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <math.h>
31 #include <time.h>
33 #include "WindowMaker.h"
34 #include "framewin.h"
35 #include "window.h"
36 #include "client.h"
37 #include "icon.h"
38 #include "colormap.h"
39 #include "application.h"
40 #include "actions.h"
41 #include "stacking.h"
42 #include "appicon.h"
43 #include "dock.h"
44 #include "appmenu.h"
45 #include "winspector.h"
46 #include "workspace.h"
47 #include "xinerama.h"
48 #include "usermenu.h"
49 #include "placement.h"
50 #include "misc.h"
51 #include "event.h"
53 /****** Global Variables ******/
55 int ignore_wks_change = 0;
56 extern Time LastFocusChange;
57 extern Atom _XA_WM_TAKE_FOCUS;
59 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
60 unsigned int *new_width, unsigned int *new_height);
61 static void save_old_geometry(WWindow *wwin, int directions);
63 /******* Local Variables *******/
64 static struct {
65 int steps;
66 int delay;
67 } shadePars[5] = {
69 SHADE_STEPS_UF, SHADE_DELAY_UF}, {
70 SHADE_STEPS_F, SHADE_DELAY_F}, {
71 SHADE_STEPS_M, SHADE_DELAY_M}, {
72 SHADE_STEPS_S, SHADE_DELAY_S}, {
73 SHADE_STEPS_US, SHADE_DELAY_US}};
75 #define UNSHADE 0
76 #define SHADE 1
77 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
78 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
80 static int compareTimes(Time t1, Time t2)
82 Time diff;
83 if (t1 == t2)
84 return 0;
85 diff = t1 - t2;
86 return (diff < 60000) ? 1 : -1;
89 #ifdef ANIMATIONS
90 static void shade_animate(WWindow *wwin, Bool what);
91 #else
92 static void shade_animate(WWindow *wwin, Bool what) { }
93 #endif
96 *----------------------------------------------------------------------
97 * wSetFocusTo--
98 * Changes the window focus to the one passed as argument.
99 * If the window to focus is not already focused, it will be brought
100 * to the head of the list of windows. Previously focused window is
101 * unfocused.
103 * Side effects:
104 * Window list may be reordered and the window focus is changed.
106 *----------------------------------------------------------------------
108 void wSetFocusTo(WScreen *scr, WWindow *wwin)
110 static WScreen *old_scr = NULL;
112 WWindow *old_focused;
113 WWindow *focused = scr->focused_window;
114 Time timestamp = w_global.timestamp.last_event;
115 WApplication *oapp = NULL, *napp = NULL;
116 int wasfocused;
118 if (scr->flags.ignore_focus_events || compareTimes(LastFocusChange, timestamp) > 0)
119 return;
121 if (!old_scr)
122 old_scr = scr;
124 old_focused = old_scr->focused_window;
126 LastFocusChange = timestamp;
128 if (old_focused)
129 oapp = wApplicationOf(old_focused->main_window);
131 if (wwin == NULL) {
132 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
133 if (old_focused)
134 wWindowUnfocus(old_focused);
136 if (oapp) {
137 wAppMenuUnmap(oapp->menu);
138 if (wPreferences.highlight_active_app)
139 wApplicationDeactivate(oapp);
142 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
143 return;
146 if (old_scr != scr && old_focused)
147 wWindowUnfocus(old_focused);
149 wasfocused = wwin->flags.focused;
150 napp = wApplicationOf(wwin->main_window);
152 /* remember last workspace where the app has been */
153 if (napp)
154 napp->last_workspace = wwin->frame->workspace;
156 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
157 /* install colormap if colormap mode is lock mode */
158 if (wPreferences.colormap_mode == WCM_CLICK)
159 wColormapInstallForWindow(scr, wwin);
161 /* set input focus */
162 switch (wwin->focus_mode) {
163 case WFM_NO_INPUT:
164 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
165 break;
166 case WFM_PASSIVE:
167 case WFM_LOCALLY_ACTIVE:
168 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
169 break;
170 case WFM_GLOBALLY_ACTIVE:
171 break;
174 XFlush(dpy);
175 if (wwin->protocols.TAKE_FOCUS)
176 wClientSendProtocol(wwin, _XA_WM_TAKE_FOCUS, timestamp);
178 XSync(dpy, False);
179 } else {
180 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
183 if (WFLAGP(wwin, no_focusable))
184 return;
186 /* if this is not the focused window focus it */
187 if (focused != wwin) {
188 /* change the focus window list order */
189 if (wwin->prev)
190 wwin->prev->next = wwin->next;
192 if (wwin->next)
193 wwin->next->prev = wwin->prev;
195 wwin->prev = focused;
196 focused->next = wwin;
197 wwin->next = NULL;
198 scr->focused_window = wwin;
200 if (oapp && oapp != napp) {
201 wAppMenuUnmap(oapp->menu);
202 if (wPreferences.highlight_active_app)
203 wApplicationDeactivate(oapp);
207 wWindowFocus(wwin, focused);
209 if (napp && !wasfocused) {
210 #ifdef USER_MENU
211 wUserMenuRefreshInstances(napp->menu, wwin);
212 #endif /* USER_MENU */
214 if (wwin->flags.mapped)
215 wAppMenuMap(napp->menu, wwin);
217 if (napp && wPreferences.highlight_active_app)
218 wApplicationActivate(napp);
220 XFlush(dpy);
221 old_scr = scr;
224 void wShadeWindow(WWindow *wwin)
227 if (wwin->flags.shaded)
228 return;
230 XLowerWindow(dpy, wwin->client_win);
231 shade_animate(wwin, SHADE);
233 wwin->flags.skip_next_animation = 0;
234 wwin->flags.shaded = 1;
235 wwin->flags.mapped = 0;
236 /* prevent window withdrawal when getting UnmapNotify */
237 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
238 XUnmapWindow(dpy, wwin->client_win);
239 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
241 /* for the client it's just like iconification */
242 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
244 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
245 wWindowSynthConfigureNotify(wwin);
248 wClientSetState(wwin, IconicState, None);
251 WMPostNotificationName(WMNChangedState, wwin, "shade");
253 #ifdef ANIMATIONS
254 if (!wwin->screen_ptr->flags.startup) {
255 /* Catch up with events not processed while animation was running */
256 ProcessPendingEvents();
258 #endif
261 void wUnshadeWindow(WWindow *wwin)
264 if (!wwin->flags.shaded)
265 return;
267 wwin->flags.shaded = 0;
268 wwin->flags.mapped = 1;
269 XMapWindow(dpy, wwin->client_win);
271 shade_animate(wwin, UNSHADE);
273 wwin->flags.skip_next_animation = 0;
274 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
275 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
277 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
278 wWindowSynthConfigureNotify(wwin);
280 /* if the window is focused, set the focus again as it was disabled during
281 * shading */
282 if (wwin->flags.focused)
283 wSetFocusTo(wwin->screen_ptr, wwin);
285 WMPostNotificationName(WMNChangedState, wwin, "shade");
288 /* Set the old coordinates using the current values */
289 static void save_old_geometry(WWindow *wwin, int directions)
291 /* never been saved? */
292 if (! wwin->old_geometry.width)
293 directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
294 if (! wwin->old_geometry.height)
295 directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
297 if (directions & SAVE_GEOMETRY_X)
298 wwin->old_geometry.x = wwin->frame_x;
299 if (directions & SAVE_GEOMETRY_Y)
300 wwin->old_geometry.y = wwin->frame_y;
301 if (directions & SAVE_GEOMETRY_WIDTH)
302 wwin->old_geometry.width = wwin->client.width;
303 if (directions & SAVE_GEOMETRY_HEIGHT)
304 wwin->old_geometry.height = wwin->client.height;
307 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
309 WMRect old_geom_rect;
310 int old_head;
311 Bool same_head;
313 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
314 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
315 same_head = (wGetHeadForWindow(wwin) == old_head);
316 *x = ((wwin->old_geometry.x || wwin->old_geometry.width) && same_head) ? wwin->old_geometry.x : wwin->frame_x;
317 *y = ((wwin->old_geometry.y || wwin->old_geometry.height) && same_head) ? wwin->old_geometry.y : wwin->frame_y;
318 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
319 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
322 /* Remember geometry for unmaximizing */
323 void update_saved_geometry(WWindow *wwin)
325 /* NOT if we aren't already maximized
326 * we'll save geometry when maximizing */
327 if (!wwin->flags.maximized)
328 return;
330 /* NOT if we are fully maximized */
331 if ((wwin->flags.maximized & MAX_MAXIMUS) ||
332 ((wwin->flags.maximized & MAX_HORIZONTAL) &&
333 (wwin->flags.maximized & MAX_VERTICAL)))
334 return;
336 /* save the co-ordinate in the axis in which we AREN'T maximized */
337 if (wwin->flags.maximized & MAX_HORIZONTAL)
338 save_old_geometry(wwin, SAVE_GEOMETRY_Y);
339 if (wwin->flags.maximized & MAX_VERTICAL)
340 save_old_geometry(wwin, SAVE_GEOMETRY_X);
343 void wMaximizeWindow(WWindow *wwin, int directions)
345 unsigned int new_width, new_height, half_scr_width, half_scr_height;
346 int new_x = 0;
347 int new_y = 0;
348 int maximus_x = 0;
349 int maximus_y = 0;
350 unsigned int maximus_width = 0;
351 unsigned int maximus_height = 0;
352 WArea usableArea, totalArea;
353 Bool has_border = 1;
354 int adj_size;
356 if (!IS_RESIZABLE(wwin))
357 return;
359 if (!HAS_BORDER(wwin))
360 has_border = 0;
362 /* the size to adjust the geometry */
363 adj_size = wwin->screen_ptr->frame_border_width * 2 * has_border;
365 /* save old coordinates before we change the current values */
366 if (!wwin->flags.maximized)
367 save_old_geometry(wwin, SAVE_GEOMETRY_ALL);
369 totalArea.x2 = wwin->screen_ptr->scr_width;
370 totalArea.y2 = wwin->screen_ptr->scr_height;
371 totalArea.x1 = 0;
372 totalArea.y1 = 0;
373 usableArea = totalArea;
375 if (!(directions & MAX_IGNORE_XINERAMA)) {
376 WScreen *scr = wwin->screen_ptr;
377 int head;
379 if (directions & MAX_KEYBOARD)
380 head = wGetHeadForWindow(wwin);
381 else
382 head = wGetHeadForPointerLocation(scr);
384 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
387 /* Only save directions, not kbd or xinerama hints */
388 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
390 if (WFLAGP(wwin, full_maximize)) {
391 usableArea = totalArea;
393 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
394 half_scr_height = (usableArea.y2 - usableArea.y1)/2;
396 if (wwin->flags.shaded) {
397 wwin->flags.skip_next_animation = 1;
398 wUnshadeWindow(wwin);
401 if (directions & MAX_MAXIMUS) {
402 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
403 new_width = maximus_width - adj_size;
404 new_height = maximus_height - adj_size;
405 new_x = maximus_x;
406 new_y = maximus_y;
407 if (WFLAGP(wwin, full_maximize) && (new_y == 0)) {
408 new_height += wwin->frame->bottom_width - 1;
409 new_y -= wwin->frame->top_width;
412 wwin->maximus_x = new_x;
413 wwin->maximus_y = new_y;
414 wwin->flags.old_maximized |= MAX_MAXIMUS;
415 } else {
416 /* set default values if no option set then */
417 if (!(directions & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS))) {
418 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
419 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
421 if (!(directions & (MAX_VERTICAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS))) {
422 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
423 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
426 /* left|right position */
427 if (directions & MAX_LEFTHALF) {
428 new_width = half_scr_width - adj_size;
429 new_x = usableArea.x1;
430 } else if (directions & MAX_RIGHTHALF) {
431 new_width = half_scr_width - adj_size;
432 new_x = usableArea.x1 + half_scr_width;
434 /* top|bottom position */
435 if (directions & MAX_TOPHALF) {
436 new_height = half_scr_height - adj_size;
437 new_y = usableArea.y1;
438 } else if (directions & MAX_BOTTOMHALF) {
439 new_height = half_scr_height - adj_size;
440 new_y = usableArea.y1 + half_scr_height;
443 /* vertical|horizontal position */
444 if (directions & MAX_HORIZONTAL) {
445 new_width = usableArea.x2 - usableArea.x1 - adj_size;
446 new_x = usableArea.x1;
448 if (directions & MAX_VERTICAL) {
449 new_height = usableArea.y2 - usableArea.y1 - adj_size;
450 new_y = usableArea.y1;
451 if (WFLAGP(wwin, full_maximize) && (new_y == 0))
452 new_y -= wwin->frame->top_width;
456 if (!WFLAGP(wwin, full_maximize) && !(directions == MAX_MAXIMUS || directions == MAX_HORIZONTAL))
457 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
459 /* set maximization state */
460 wwin->flags.maximized = directions;
461 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
462 wwin->flags.maximized = MAX_MAXIMUS;
464 wWindowConstrainSize(wwin, &new_width, &new_height);
466 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
467 usableArea.y2 - usableArea.y1, &new_width, &new_height);
469 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
470 wWindowSynthConfigureNotify(wwin);
472 WMPostNotificationName(WMNChangedState, wwin, "maximize");
475 /* generic (un)maximizer */
476 void handleMaximize(WWindow *wwin, int directions)
478 int current = wwin->flags.maximized;
479 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
480 int effective = requested ^ current;
481 int flags = directions & ~requested;
483 if (!effective) {
484 /* allow wMaximizeWindow to restore the Maximusized size */
485 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
486 !(requested & MAX_MAXIMUS))
487 wMaximizeWindow(wwin, MAX_MAXIMUS | flags);
488 else
489 wUnmaximizeWindow(wwin);
490 /* these alone mean vertical|horizontal toggle */
491 } else if ((effective == MAX_LEFTHALF) ||
492 (effective == MAX_RIGHTHALF) ||
493 (effective == MAX_TOPHALF) ||
494 (effective == MAX_BOTTOMHALF))
495 wUnmaximizeWindow(wwin);
496 else {
497 if ((requested == (MAX_HORIZONTAL | MAX_VERTICAL)) ||
498 (requested == MAX_MAXIMUS))
499 effective = requested;
500 else {
501 if (requested & MAX_LEFTHALF) {
502 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
503 effective |= MAX_VERTICAL;
504 else
505 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
506 effective |= MAX_LEFTHALF;
507 effective &= ~(MAX_HORIZONTAL | MAX_RIGHTHALF);
508 } else if (requested & MAX_RIGHTHALF) {
509 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
510 effective |= MAX_VERTICAL;
511 else
512 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
513 effective |= MAX_RIGHTHALF;
514 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF);
516 if (requested & MAX_TOPHALF) {
517 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
518 effective |= MAX_HORIZONTAL;
519 else
520 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
521 effective |= MAX_TOPHALF;
522 effective &= ~(MAX_VERTICAL | MAX_BOTTOMHALF);
523 } else if (requested & MAX_BOTTOMHALF) {
524 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
525 effective |= MAX_HORIZONTAL;
526 else
527 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
528 effective |= MAX_BOTTOMHALF;
529 effective &= ~(MAX_VERTICAL | MAX_TOPHALF);
531 if (requested & MAX_HORIZONTAL)
532 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
533 if (requested & MAX_VERTICAL)
534 effective &= ~(MAX_TOPHALF | MAX_BOTTOMHALF);
535 effective &= ~MAX_MAXIMUS;
537 wMaximizeWindow(wwin, effective | flags);
541 /* the window boundary coordinates */
542 typedef struct {
543 int left;
544 int right;
545 int bottom;
546 int top;
547 int width;
548 int height;
549 } win_coords;
551 static void set_window_coords(WWindow *wwin, win_coords *obs)
553 obs->left = wwin->frame_x;
554 obs->top = wwin->frame_y;
555 obs->width = wwin->frame->core->width;
556 obs->height = wwin->frame->core->height;
557 obs->bottom = obs->top + obs->height;
558 obs->right = obs->left + obs->width;
562 * Maximus: tiled maximization (maximize without overlapping other windows)
564 * The original window 'orig' will be maximized to new coordinates 'new'.
565 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
567 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
568 unsigned int *new_width, unsigned int *new_height)
570 WWindow *tmp;
571 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
572 short int adjust_height;
573 int x_intsect, y_intsect;
574 /* the obstructing, original and new windows */
575 win_coords obs, orig, new;
577 /* set the original coordinate positions of the window to be Maximumized */
578 if (wwin->flags.maximized) {
579 /* window is already maximized; consider original geometry */
580 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
581 orig.bottom = orig.top + orig.height;
582 orig.right = orig.left + orig.width;
584 else
585 set_window_coords(wwin, &orig);
587 /* Try to fully maximize first, then readjust later */
588 new.left = usableArea.x1;
589 new.right = usableArea.x2;
590 new.top = usableArea.y1;
591 new.bottom = usableArea.y2;
593 if (HAS_TITLEBAR(wwin))
594 tbar_height_0 = TITLEBAR_HEIGHT;
595 if (HAS_RESIZEBAR(wwin))
596 rbar_height_0 = RESIZEBAR_HEIGHT;
597 if (HAS_BORDER(wwin))
598 bd_width_0 = wwin->screen_ptr->frame_border_width;
600 /* the length to be subtracted if the window has titlebar, etc */
601 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
603 tmp = wwin;
604 /* The focused window is always the last in the list */
605 while (tmp->prev) {
606 /* ignore windows in other workspaces etc */
607 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
608 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
609 tmp = tmp->prev;
610 continue;
612 tmp = tmp->prev;
614 /* Set the coordinates of obstructing window */
615 set_window_coords(tmp, &obs);
617 /* Try to maximize in the y direction first */
618 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
619 if (x_intsect != 0) {
620 /* TODO: Consider the case when coords are equal */
621 if (obs.bottom < orig.top && obs.bottom > new.top) {
622 /* w_0 is below the bottom of w_j */
623 new.top = obs.bottom + 1;
625 if (orig.bottom < obs.top && obs.top < new.bottom) {
626 /* The bottom of w_0 is above the top of w_j */
627 new.bottom = obs.top - 1;
632 tmp = wwin;
633 while (tmp->prev) {
634 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
635 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
636 tmp = tmp->prev;
637 continue;
639 tmp = tmp->prev;
641 set_window_coords(tmp, &obs);
644 * Use the new.top and new.height instead of original values
645 * as they may have different intersections with the obstructing windows
647 new.height = new.bottom - new.top - adjust_height;
648 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
649 if (y_intsect != 0) {
650 if (obs.right < orig.left && obs.right > new.left) {
651 /* w_0 is completely to the right of w_j */
652 new.left = obs.right + 1;
654 if (orig.right < obs.left && obs.left < new.right) {
655 /* w_0 is completely to the left of w_j */
656 new.right = obs.left - 1;
661 *new_x = new.left;
662 *new_y = new.top;
663 /* xcalc needs -7 here, but other apps don't */
664 *new_height = new.bottom - new.top - adjust_height - 1;;
665 *new_width = new.right - new.left;
668 void wUnmaximizeWindow(WWindow *wwin)
670 int x, y, w, h;
672 if (!wwin->flags.maximized)
673 return;
675 if (wwin->flags.shaded) {
676 wwin->flags.skip_next_animation = 1;
677 wUnshadeWindow(wwin);
680 /* Use old coordinates if they are set, current values otherwise */
681 remember_geometry(wwin, &x, &y, &w, &h);
683 /* unMaximusize relative to original position */
684 if (wwin->flags.maximized & MAX_MAXIMUS) {
685 x += wwin->frame_x - wwin->maximus_x;
686 y += wwin->frame_y - wwin->maximus_y;
689 wwin->flags.maximized = 0;
690 wwin->flags.old_maximized = 0;
691 wWindowConfigure(wwin, x, y, w, h);
692 wWindowSynthConfigureNotify(wwin);
694 WMPostNotificationName(WMNChangedState, wwin, "maximize");
697 void wFullscreenWindow(WWindow *wwin)
699 int head;
700 WMRect rect;
702 if (wwin->flags.fullscreen)
703 return;
705 wwin->flags.fullscreen = True;
707 wWindowConfigureBorders(wwin);
709 ChangeStackingLevel(wwin->frame->core, WMFullscreenLevel);
711 wwin->bfs_geometry.x = wwin->frame_x;
712 wwin->bfs_geometry.y = wwin->frame_y;
713 wwin->bfs_geometry.width = wwin->frame->core->width;
714 wwin->bfs_geometry.height = wwin->frame->core->height;
716 head = wGetHeadForWindow(wwin);
717 rect = wGetRectForHead(wwin->screen_ptr, head);
718 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
720 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
723 void wUnfullscreenWindow(WWindow *wwin)
725 if (!wwin->flags.fullscreen)
726 return;
728 wwin->flags.fullscreen = False;
730 if (wwin->frame->core->stacking->window_level == WMFullscreenLevel) {
731 if (WFLAGP(wwin, sunken)) {
732 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
733 } else if (WFLAGP(wwin, floating)) {
734 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
735 } else {
736 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
740 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
741 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
743 wWindowConfigureBorders(wwin);
745 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
746 wFrameWindowPaint(wwin->frame);
749 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
752 #ifdef ANIMATIONS
753 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
755 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
756 float cx, cy, cw, ch;
757 float xstep, ystep, wstep, hstep;
758 XPoint points[5];
759 float dx, dch, midy;
760 float angle, final_angle, delta;
762 xstep = (float)(fx - x) / steps;
763 ystep = (float)(fy - y) / steps;
764 wstep = (float)(fw - w) / steps;
765 hstep = (float)(fh - h) / steps;
767 cx = (float)x;
768 cy = (float)y;
769 cw = (float)w;
770 ch = (float)h;
772 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
773 delta = (float)(final_angle / FRAMES);
774 for (angle = 0;; angle += delta) {
775 if (angle > final_angle)
776 angle = final_angle;
778 dx = (cw / 10) - ((cw / 5) * sin(angle));
779 dch = (ch / 2) * cos(angle);
780 midy = cy + (ch / 2);
782 points[0].x = cx + dx;
783 points[0].y = midy - dch;
784 points[1].x = cx + cw - dx;
785 points[1].y = points[0].y;
786 points[2].x = cx + cw + dx;
787 points[2].y = midy + dch;
788 points[3].x = cx - dx;
789 points[3].y = points[2].y;
790 points[4].x = points[0].x;
791 points[4].y = points[0].y;
793 XGrabServer(dpy);
794 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
795 XFlush(dpy);
796 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
798 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
799 XUngrabServer(dpy);
800 cx += xstep;
801 cy += ystep;
802 cw += wstep;
803 ch += hstep;
804 if (angle >= final_angle)
805 break;
808 XFlush(dpy);
811 #undef FRAMES
813 static void
814 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
816 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
817 float cx, cy, cw, ch;
818 float xstep, ystep, wstep, hstep;
819 XPoint points[5];
820 float angle, final_angle, a, d, delta;
822 x += w / 2;
823 y += h / 2;
824 fx += fw / 2;
825 fy += fh / 2;
827 xstep = (float)(fx - x) / steps;
828 ystep = (float)(fy - y) / steps;
829 wstep = (float)(fw - w) / steps;
830 hstep = (float)(fh - h) / steps;
832 cx = (float)x;
833 cy = (float)y;
834 cw = (float)w;
835 ch = (float)h;
837 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
838 delta = (float)(final_angle / FRAMES);
839 for (angle = 0;; angle += delta) {
840 if (angle > final_angle)
841 angle = final_angle;
843 a = atan(ch / cw);
844 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
846 points[0].x = cx + cos(angle - a) * d;
847 points[0].y = cy + sin(angle - a) * d;
848 points[1].x = cx + cos(angle + a) * d;
849 points[1].y = cy + sin(angle + a) * d;
850 points[2].x = cx + cos(angle - a + WM_PI) * d;
851 points[2].y = cy + sin(angle - a + WM_PI) * d;
852 points[3].x = cx + cos(angle + a + WM_PI) * d;
853 points[3].y = cy + sin(angle + a + WM_PI) * d;
854 points[4].x = cx + cos(angle - a) * d;
855 points[4].y = cy + sin(angle - a) * d;
856 XGrabServer(dpy);
857 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
858 XFlush(dpy);
859 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
861 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
862 XUngrabServer(dpy);
863 cx += xstep;
864 cy += ystep;
865 cw += wstep;
866 ch += hstep;
867 if (angle >= final_angle)
868 break;
871 XFlush(dpy);
874 #undef FRAMES
876 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
878 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
879 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
880 float xstep, ystep, wstep, hstep;
881 int i, j;
883 xstep = (float)(fx - x) / steps;
884 ystep = (float)(fy - y) / steps;
885 wstep = (float)(fw - w) / steps;
886 hstep = (float)(fh - h) / steps;
888 for (j = 0; j < FRAMES; j++) {
889 cx[j] = (float)x;
890 cy[j] = (float)y;
891 cw[j] = (float)w;
892 ch[j] = (float)h;
894 XGrabServer(dpy);
895 for (i = 0; i < steps; i++) {
896 for (j = 0; j < FRAMES; j++) {
897 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
898 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
900 XFlush(dpy);
901 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
903 for (j = 0; j < FRAMES; j++) {
904 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
905 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
906 if (j < FRAMES - 1) {
907 cx[j] = cx[j + 1];
908 cy[j] = cy[j + 1];
909 cw[j] = cw[j + 1];
910 ch[j] = ch[j + 1];
911 } else {
912 cx[j] += xstep;
913 cy[j] += ystep;
914 cw[j] += wstep;
915 ch[j] += hstep;
920 for (j = 0; j < FRAMES; j++) {
921 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
923 XFlush(dpy);
924 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
926 for (j = 0; j < FRAMES; j++) {
927 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
930 XUngrabServer(dpy);
933 #undef FRAMES
935 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
937 int style = wPreferences.iconification_style; /* Catch the value */
938 int steps;
940 if (style == WIS_NONE)
941 return;
943 if (style == WIS_RANDOM) {
944 style = rand() % 3;
947 switch (style) {
948 case WIS_TWIST:
949 steps = MINIATURIZE_ANIMATION_STEPS_T;
950 if (steps > 0)
951 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
952 break;
953 case WIS_FLIP:
954 steps = MINIATURIZE_ANIMATION_STEPS_F;
955 if (steps > 0)
956 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
957 break;
958 case WIS_ZOOM:
959 default:
960 steps = MINIATURIZE_ANIMATION_STEPS_Z;
961 if (steps > 0)
962 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
963 break;
966 #endif /* ANIMATIONS */
968 static void flushExpose(void)
970 XEvent tmpev;
972 while (XCheckTypedEvent(dpy, Expose, &tmpev))
973 WMHandleEvent(&tmpev);
974 XSync(dpy, 0);
977 static void unmapTransientsFor(WWindow *wwin)
979 WWindow *tmp;
981 tmp = wwin->screen_ptr->focused_window;
982 while (tmp) {
983 /* unmap the transients for this transient */
984 if (tmp != wwin && tmp->transient_for == wwin->client_win
985 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
986 unmapTransientsFor(tmp);
987 tmp->flags.miniaturized = 1;
988 if (!tmp->flags.shaded) {
989 wWindowUnmap(tmp);
990 } else {
991 XUnmapWindow(dpy, tmp->frame->core->window);
994 if (!tmp->flags.shaded)
996 wClientSetState(tmp, IconicState, None);
998 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1000 tmp = tmp->prev;
1004 static void mapTransientsFor(WWindow *wwin)
1006 WWindow *tmp;
1008 tmp = wwin->screen_ptr->focused_window;
1009 while (tmp) {
1010 /* recursively map the transients for this transient */
1011 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1012 && tmp->icon == NULL) {
1013 mapTransientsFor(tmp);
1014 tmp->flags.miniaturized = 0;
1015 if (!tmp->flags.shaded) {
1016 wWindowMap(tmp);
1017 } else {
1018 XMapWindow(dpy, tmp->frame->core->window);
1020 tmp->flags.semi_focused = 0;
1022 if (!tmp->flags.shaded)
1024 wClientSetState(tmp, NormalState, None);
1026 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1028 tmp = tmp->prev;
1032 static WWindow *recursiveTransientFor(WWindow * wwin)
1034 int i;
1036 if (!wwin)
1037 return None;
1039 /* hackish way to detect transient_for cycle */
1040 i = wwin->screen_ptr->window_count + 1;
1042 while (wwin && wwin->transient_for != None && i > 0) {
1043 wwin = wWindowFor(wwin->transient_for);
1044 i--;
1046 if (i == 0 && wwin) {
1047 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
1048 return NULL;
1051 return wwin;
1054 void wIconifyWindow(WWindow * wwin)
1056 XWindowAttributes attribs;
1057 int present;
1059 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1060 return; /* the window doesn't exist anymore */
1062 if (wwin->flags.miniaturized)
1063 return; /* already miniaturized */
1065 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1066 WWindow *owner = wWindowFor(wwin->transient_for);
1068 if (owner && owner->flags.miniaturized)
1069 return;
1072 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1074 /* if the window is in another workspace, simplify process */
1075 if (present) {
1076 /* icon creation may take a while */
1077 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1078 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1079 GrabModeAsync, None, None, CurrentTime);
1082 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1083 if (!wwin->flags.icon_moved)
1084 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1086 wwin->icon = icon_create_for_wwindow(wwin);
1087 wwin->icon->mapped = 1;
1090 wwin->flags.miniaturized = 1;
1091 wwin->flags.mapped = 0;
1093 /* unmap transients */
1094 unmapTransientsFor(wwin);
1096 if (present) {
1097 XUngrabPointer(dpy, CurrentTime);
1098 wWindowUnmap(wwin);
1099 /* let all Expose events arrive so that we can repaint
1100 * something before the animation starts (and the server is grabbed) */
1101 XSync(dpy, 0);
1103 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1104 wClientSetState(wwin, IconicState, None);
1105 else
1106 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1108 flushExpose();
1109 #ifdef ANIMATIONS
1110 if (!wwin->screen_ptr->flags.startup && !wwin->flags.skip_next_animation
1111 && !wPreferences.no_animations) {
1112 int ix, iy, iw, ih;
1114 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1115 ix = wwin->icon_x;
1116 iy = wwin->icon_y;
1117 iw = wwin->icon->core->width;
1118 ih = wwin->icon->core->height;
1119 } else {
1120 if (wwin->flags.net_handle_icon) {
1121 ix = wwin->icon_x;
1122 iy = wwin->icon_y;
1123 iw = wwin->icon_w;
1124 ih = wwin->icon_h;
1125 } else {
1126 ix = 0;
1127 iy = 0;
1128 iw = wwin->screen_ptr->scr_width;
1129 ih = wwin->screen_ptr->scr_height;
1132 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1133 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1135 #endif
1138 wwin->flags.skip_next_animation = 0;
1140 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1141 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1142 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1143 XMapWindow(dpy, wwin->icon->core->window);
1145 AddToStackList(wwin->icon->core);
1146 wLowerFrame(wwin->icon->core);
1149 if (present) {
1150 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1153 * It doesn't seem to be working and causes button event hangup
1154 * when deiconifying a transient window.
1155 setupIconGrabs(wwin->icon);
1157 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1158 && wPreferences.focus_mode == WKF_CLICK) {
1159 WWindow *tmp;
1161 tmp = wwin->prev;
1162 while (tmp) {
1163 if (!WFLAGP(tmp, no_focusable)
1164 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1165 && (wwin->frame->workspace == tmp->frame->workspace))
1166 break;
1167 tmp = tmp->prev;
1169 wSetFocusTo(wwin->screen_ptr, tmp);
1170 } else if (wPreferences.focus_mode != WKF_CLICK) {
1171 wSetFocusTo(wwin->screen_ptr, NULL);
1173 #ifdef ANIMATIONS
1174 if (!wwin->screen_ptr->flags.startup) {
1175 /* Catch up with events not processed while animation was running */
1176 Window clientwin = wwin->client_win;
1178 ProcessPendingEvents();
1180 /* the window can disappear while ProcessPendingEvents() runs */
1181 if (!wWindowFor(clientwin)) {
1182 return;
1185 #endif
1188 /* maybe we want to do this regardless of net_handle_icon
1189 * it seems to me we might break behaviour this way.
1191 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1192 && !wwin->flags.net_handle_icon)
1193 wIconSelect(wwin->icon);
1195 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1197 if (wPreferences.auto_arrange_icons)
1198 wArrangeIcons(wwin->screen_ptr, True);
1201 void wDeiconifyWindow(WWindow *wwin)
1203 /* Let's avoid changing workspace while deiconifying */
1204 ignore_wks_change = 1;
1206 /* we're hiding for show_desktop */
1207 int netwm_hidden = wwin->flags.net_show_desktop &&
1208 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1210 if (!netwm_hidden)
1211 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1213 if (!wwin->flags.miniaturized) {
1214 ignore_wks_change = 0;
1215 return;
1218 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1219 WWindow *owner = recursiveTransientFor(wwin);
1221 if (owner && owner->flags.miniaturized) {
1222 wDeiconifyWindow(owner);
1223 wSetFocusTo(wwin->screen_ptr, wwin);
1224 wRaiseFrame(wwin->frame->core);
1225 ignore_wks_change = 0;
1226 return;
1230 wwin->flags.miniaturized = 0;
1232 if (!netwm_hidden && !wwin->flags.shaded)
1233 wwin->flags.mapped = 1;
1235 if (!netwm_hidden || wPreferences.sticky_icons) {
1236 /* maybe we want to do this regardless of net_handle_icon
1237 * it seems to me we might break behaviour this way.
1239 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1240 && wwin->icon != NULL) {
1241 if (wwin->icon->selected)
1242 wIconSelect(wwin->icon);
1244 XUnmapWindow(dpy, wwin->icon->core->window);
1248 /* if the window is in another workspace, do it silently */
1249 if (!netwm_hidden) {
1250 #ifdef ANIMATIONS
1251 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations
1252 && !wwin->flags.skip_next_animation && wwin->icon != NULL) {
1253 int ix, iy, iw, ih;
1255 if (!wPreferences.disable_miniwindows
1256 && !wwin->flags.net_handle_icon) {
1257 ix = wwin->icon_x;
1258 iy = wwin->icon_y;
1259 iw = wwin->icon->core->width;
1260 ih = wwin->icon->core->height;
1261 } else {
1262 if (wwin->flags.net_handle_icon) {
1263 ix = wwin->icon_x;
1264 iy = wwin->icon_y;
1265 iw = wwin->icon_w;
1266 ih = wwin->icon_h;
1267 } else {
1268 ix = 0;
1269 iy = 0;
1270 iw = wwin->screen_ptr->scr_width;
1271 ih = wwin->screen_ptr->scr_height;
1274 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1275 wwin->frame_x, wwin->frame_y,
1276 wwin->frame->core->width, wwin->frame->core->height);
1278 #endif /* ANIMATIONS */
1279 wwin->flags.skip_next_animation = 0;
1280 XGrabServer(dpy);
1281 if (!wwin->flags.shaded)
1282 XMapWindow(dpy, wwin->client_win);
1284 XMapWindow(dpy, wwin->frame->core->window);
1285 wRaiseFrame(wwin->frame->core);
1286 if (!wwin->flags.shaded)
1287 wClientSetState(wwin, NormalState, None);
1289 mapTransientsFor(wwin);
1292 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1293 && !wwin->flags.net_handle_icon) {
1294 RemoveFromStackList(wwin->icon->core);
1295 /* removeIconGrabs(wwin->icon); */
1296 wIconDestroy(wwin->icon);
1297 wwin->icon = NULL;
1300 if (!netwm_hidden) {
1301 XUngrabServer(dpy);
1303 wSetFocusTo(wwin->screen_ptr, wwin);
1305 #ifdef ANIMATIONS
1306 if (!wwin->screen_ptr->flags.startup) {
1307 /* Catch up with events not processed while animation was running */
1308 Window clientwin = wwin->client_win;
1310 ProcessPendingEvents();
1312 /* the window can disappear while ProcessPendingEvents() runs */
1313 if (!wWindowFor(clientwin)) {
1314 ignore_wks_change = 0;
1315 return;
1318 #endif
1321 if (wPreferences.auto_arrange_icons)
1322 wArrangeIcons(wwin->screen_ptr, True);
1324 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1326 /* In case we were shaded and iconified, also unshade */
1327 if (!netwm_hidden)
1328 wUnshadeWindow(wwin);
1330 ignore_wks_change = 0;
1333 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1335 if (wwin->flags.miniaturized) {
1336 if (wwin->icon) {
1337 XUnmapWindow(dpy, wwin->icon->core->window);
1338 wwin->icon->mapped = 0;
1340 wwin->flags.hidden = 1;
1342 WMPostNotificationName(WMNChangedState, wwin, "hide");
1343 return;
1346 if (wwin->flags.inspector_open) {
1347 wHideInspectorForWindow(wwin);
1350 wwin->flags.hidden = 1;
1351 wWindowUnmap(wwin);
1353 wClientSetState(wwin, IconicState, icon->icon_win);
1354 flushExpose();
1356 #ifdef ANIMATIONS
1357 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1358 !wwin->flags.skip_next_animation && animate) {
1359 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1360 wwin->frame->core->width, wwin->frame->core->height,
1361 icon_x, icon_y, icon->core->width, icon->core->height);
1363 #endif
1364 wwin->flags.skip_next_animation = 0;
1366 WMPostNotificationName(WMNChangedState, wwin, "hide");
1369 void wHideAll(WScreen *scr)
1371 WWindow *wwin;
1372 WWindow **windows;
1373 WMenu *menu;
1374 unsigned int wcount = 0;
1375 int i;
1377 if (!scr)
1378 return;
1380 menu = scr->switch_menu;
1382 windows = wmalloc(sizeof(WWindow *));
1384 if (menu != NULL) {
1385 for (i = 0; i < menu->entry_no; i++) {
1386 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1387 wcount++;
1388 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1390 } else {
1391 wwin = scr->focused_window;
1393 while (wwin) {
1394 windows[wcount] = wwin;
1395 wcount++;
1396 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1397 wwin = wwin->prev;
1402 for (i = 0; i < wcount; i++) {
1403 wwin = windows[i];
1404 if (wwin->frame->workspace == scr->current_workspace
1405 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1406 && !wwin->flags.internal_window
1407 && !WFLAGP(wwin, no_miniaturizable)
1409 wwin->flags.skip_next_animation = 1;
1410 wIconifyWindow(wwin);
1414 wfree(windows);
1417 void wHideOtherApplications(WWindow *awin)
1419 WWindow *wwin;
1420 WApplication *tapp;
1422 if (!awin)
1423 return;
1424 wwin = awin->screen_ptr->focused_window;
1426 while (wwin) {
1427 if (wwin != awin
1428 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1429 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1430 && !wwin->flags.internal_window
1431 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1433 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1434 if (!WFLAGP(wwin, no_miniaturizable)) {
1435 wwin->flags.skip_next_animation = 1;
1436 wIconifyWindow(wwin);
1438 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1439 tapp = wApplicationOf(wwin->main_window);
1440 if (tapp) {
1441 tapp->flags.skip_next_animation = 1;
1442 wHideApplication(tapp);
1443 } else {
1444 if (!WFLAGP(wwin, no_miniaturizable)) {
1445 wwin->flags.skip_next_animation = 1;
1446 wIconifyWindow(wwin);
1451 wwin = wwin->prev;
1454 wSetFocusTo(awin->screen_ptr, awin);
1458 void wHideApplication(WApplication *wapp)
1460 WScreen *scr;
1461 WWindow *wlist;
1462 int hadfocus;
1463 int animate;
1465 if (!wapp) {
1466 wwarning("trying to hide a non grouped window");
1467 return;
1469 if (!wapp->main_window_desc) {
1470 wwarning("group leader not found for window group");
1471 return;
1473 scr = wapp->main_window_desc->screen_ptr;
1474 hadfocus = 0;
1475 wlist = scr->focused_window;
1476 if (!wlist)
1477 return;
1479 if (wlist->main_window == wapp->main_window)
1480 wapp->last_focused = wlist;
1481 else
1482 wapp->last_focused = NULL;
1484 animate = !wapp->flags.skip_next_animation;
1486 while (wlist) {
1487 if (wlist->main_window == wapp->main_window) {
1488 if (wlist->flags.focused) {
1489 hadfocus = 1;
1491 if (wapp->app_icon) {
1492 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1493 wapp->app_icon->y_pos, wlist, animate);
1494 animate = False;
1497 wlist = wlist->prev;
1500 wapp->flags.skip_next_animation = 0;
1502 if (hadfocus) {
1503 if (wPreferences.focus_mode == WKF_CLICK) {
1504 wlist = scr->focused_window;
1505 while (wlist) {
1506 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1507 && (wlist->flags.mapped || wlist->flags.shaded))
1508 break;
1509 wlist = wlist->prev;
1511 wSetFocusTo(scr, wlist);
1512 } else {
1513 wSetFocusTo(scr, NULL);
1517 wapp->flags.hidden = 1;
1519 if (wPreferences.auto_arrange_icons)
1520 wArrangeIcons(scr, True);
1522 #ifdef HIDDENDOT
1523 if (wapp->app_icon)
1524 wAppIconPaint(wapp->app_icon);
1525 #endif
1528 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1530 if (bringToCurrentWS)
1531 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1533 wwin->flags.hidden = 0;
1535 #ifdef ANIMATIONS
1536 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1537 animateResize(wwin->screen_ptr, icon_x, icon_y,
1538 icon->core->width, icon->core->height,
1539 wwin->frame_x, wwin->frame_y,
1540 wwin->frame->core->width, wwin->frame->core->height);
1542 #endif
1543 wwin->flags.skip_next_animation = 0;
1544 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1545 XMapWindow(dpy, wwin->client_win);
1546 XMapWindow(dpy, wwin->frame->core->window);
1547 wClientSetState(wwin, NormalState, None);
1548 wwin->flags.mapped = 1;
1549 wRaiseFrame(wwin->frame->core);
1551 if (wwin->flags.inspector_open) {
1552 wUnhideInspectorForWindow(wwin);
1555 WMPostNotificationName(WMNChangedState, wwin, "hide");
1558 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1560 WScreen *scr;
1561 WWindow *wlist, *next;
1562 WWindow *focused = NULL;
1563 int animate;
1565 if (!wapp)
1566 return;
1568 scr = wapp->main_window_desc->screen_ptr;
1569 wlist = scr->focused_window;
1570 if (!wlist)
1571 return;
1573 /* goto beginning of list */
1574 while (wlist->prev)
1575 wlist = wlist->prev;
1577 animate = !wapp->flags.skip_next_animation;
1579 while (wlist) {
1580 next = wlist->next;
1582 if (wlist->main_window == wapp->main_window) {
1583 if (wlist->flags.focused)
1584 focused = wlist;
1585 else if (!focused || !focused->flags.focused)
1586 focused = wlist;
1588 if (wlist->flags.miniaturized) {
1589 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1590 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1591 if (!wlist->icon->mapped) {
1592 int x, y;
1594 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1595 if (wlist->icon_x != x || wlist->icon_y != y) {
1596 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1598 wlist->icon_x = x;
1599 wlist->icon_y = y;
1600 XMapWindow(dpy, wlist->icon->core->window);
1601 wlist->icon->mapped = 1;
1603 wRaiseFrame(wlist->icon->core);
1605 if (bringToCurrentWS)
1606 wWindowChangeWorkspace(wlist, scr->current_workspace);
1607 wlist->flags.hidden = 0;
1608 if (miniwindows && wlist->frame->workspace == scr->current_workspace) {
1609 wDeiconifyWindow(wlist);
1611 WMPostNotificationName(WMNChangedState, wlist, "hide");
1612 } else if (wlist->flags.shaded) {
1613 if (bringToCurrentWS)
1614 wWindowChangeWorkspace(wlist, scr->current_workspace);
1615 wlist->flags.hidden = 0;
1616 wRaiseFrame(wlist->frame->core);
1617 if (wlist->frame->workspace == scr->current_workspace) {
1618 XMapWindow(dpy, wlist->frame->core->window);
1619 if (miniwindows) {
1620 wUnshadeWindow(wlist);
1623 WMPostNotificationName(WMNChangedState, wlist, "hide");
1624 } else if (wlist->flags.hidden) {
1625 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1626 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1627 animate = False;
1628 } else {
1629 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace) {
1630 wWindowChangeWorkspace(wlist, scr->current_workspace);
1632 wRaiseFrame(wlist->frame->core);
1635 wlist = next;
1638 wapp->flags.skip_next_animation = 0;
1639 wapp->flags.hidden = 0;
1641 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1642 wRaiseFrame(wapp->last_focused->frame->core);
1643 wSetFocusTo(scr, wapp->last_focused);
1644 } else if (focused) {
1645 wSetFocusTo(scr, focused);
1647 wapp->last_focused = NULL;
1648 if (wPreferences.auto_arrange_icons)
1649 wArrangeIcons(scr, True);
1651 #ifdef HIDDENDOT
1652 wAppIconPaint(wapp->app_icon);
1653 #endif
1656 void wShowAllWindows(WScreen *scr)
1658 WWindow *wwin, *old_foc;
1659 WApplication *wapp;
1661 old_foc = wwin = scr->focused_window;
1662 while (wwin) {
1663 if (!wwin->flags.internal_window &&
1664 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1665 if (wwin->flags.miniaturized) {
1666 wwin->flags.skip_next_animation = 1;
1667 wDeiconifyWindow(wwin);
1668 } else if (wwin->flags.hidden) {
1669 wapp = wApplicationOf(wwin->main_window);
1670 if (wapp) {
1671 wUnhideApplication(wapp, False, False);
1672 } else {
1673 wwin->flags.skip_next_animation = 1;
1674 wDeiconifyWindow(wwin);
1678 wwin = wwin->prev;
1680 wSetFocusTo(scr, old_foc);
1681 /*wRaiseFrame(old_foc->frame->core); */
1684 void wRefreshDesktop(WScreen *scr)
1686 Window win;
1687 XSetWindowAttributes attr;
1689 attr.backing_store = NotUseful;
1690 attr.save_under = False;
1691 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1692 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1693 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1694 XMapRaised(dpy, win);
1695 XDestroyWindow(dpy, win);
1696 XFlush(dpy);
1699 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1701 WWindow *wwin;
1702 WAppIcon *aicon;
1704 int head;
1705 const int heads = wXineramaHeads(scr);
1707 struct HeadVars {
1708 int pf; /* primary axis */
1709 int sf; /* secondary axis */
1710 int fullW;
1711 int fullH;
1712 int pi, si;
1713 int sx1, sx2, sy1, sy2; /* screen boundary */
1714 int sw, sh;
1715 int xo, yo;
1716 int xs, ys;
1717 } *vars;
1719 int isize = wPreferences.icon_size;
1721 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1723 for (head = 0; head < heads; ++head) {
1724 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1725 WMRect rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1726 vars[head].pi = vars[head].si = 0;
1727 vars[head].sx1 = rect.pos.x;
1728 vars[head].sy1 = rect.pos.y;
1729 vars[head].sw = rect.size.width;
1730 vars[head].sh = rect.size.height;
1731 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1732 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1733 vars[head].sw = isize * (vars[head].sw / isize);
1734 vars[head].sh = isize * (vars[head].sh / isize);
1735 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1736 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1738 /* icon yard boundaries */
1739 if (wPreferences.icon_yard & IY_VERT) {
1740 vars[head].pf = vars[head].fullH;
1741 vars[head].sf = vars[head].fullW;
1742 } else {
1743 vars[head].pf = vars[head].fullW;
1744 vars[head].sf = vars[head].fullH;
1746 if (wPreferences.icon_yard & IY_RIGHT) {
1747 vars[head].xo = vars[head].sx2 - isize;
1748 vars[head].xs = -1;
1749 } else {
1750 vars[head].xo = vars[head].sx1;
1751 vars[head].xs = 1;
1753 if (wPreferences.icon_yard & IY_TOP) {
1754 vars[head].yo = vars[head].sy1;
1755 vars[head].ys = 1;
1756 } else {
1757 vars[head].yo = vars[head].sy2 - isize;
1758 vars[head].ys = -1;
1762 #define X ((wPreferences.icon_yard & IY_VERT) \
1763 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1764 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1766 #define Y ((wPreferences.icon_yard & IY_VERT) \
1767 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1768 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1770 /* arrange application icons */
1771 aicon = scr->app_icon_list;
1772 /* reverse them to avoid unnecessarily sliding of icons */
1773 while (aicon && aicon->next)
1774 aicon = aicon->next;
1776 while (aicon) {
1777 if (!aicon->docked) {
1778 /* CHECK: can icon be NULL here ? */
1779 /* The intention here is to place the AppIcon on the head that
1780 * contains most of the applications _main_ window. */
1781 head = wGetHeadForWindow(aicon->icon->owner);
1783 if (aicon->x_pos != X || aicon->y_pos != Y) {
1784 #ifdef ANIMATIONS
1785 if (!wPreferences.no_animations)
1786 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1787 #endif /* ANIMATIONS */
1789 wAppIconMove(aicon, X, Y);
1790 vars[head].pi++;
1791 if (vars[head].pi >= vars[head].pf) {
1792 vars[head].pi = 0;
1793 vars[head].si++;
1796 aicon = aicon->prev;
1799 /* arrange miniwindows */
1800 wwin = scr->focused_window;
1801 /* reverse them to avoid unnecessarily shuffling */
1802 while (wwin && wwin->prev)
1803 wwin = wwin->prev;
1805 while (wwin) {
1806 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1807 (wwin->frame->workspace == scr->current_workspace ||
1808 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1810 head = wGetHeadForWindow(wwin);
1812 if (arrangeAll || !wwin->flags.icon_moved) {
1813 if (wwin->icon_x != X || wwin->icon_y != Y)
1814 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1816 wwin->icon_x = X;
1817 wwin->icon_y = Y;
1819 vars[head].pi++;
1820 if (vars[head].pi >= vars[head].pf) {
1821 vars[head].pi = 0;
1822 vars[head].si++;
1826 if (arrangeAll) {
1827 wwin->flags.icon_moved = 0;
1829 /* we reversed the order, so we use next */
1830 wwin = wwin->next;
1833 wfree(vars);
1836 void wSelectWindow(WWindow *wwin, Bool flag)
1838 WScreen *scr = wwin->screen_ptr;
1840 if (flag) {
1841 wwin->flags.selected = 1;
1842 if (wwin->frame->selected_border_pixel)
1843 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1844 else
1845 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1847 if (!HAS_BORDER(wwin)) {
1848 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1851 if (!scr->selected_windows)
1852 scr->selected_windows = WMCreateArray(4);
1853 WMAddToArray(scr->selected_windows, wwin);
1854 } else {
1855 wwin->flags.selected = 0;
1856 if (wwin->frame->border_pixel)
1857 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1858 else
1859 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1861 if (!HAS_BORDER(wwin)) {
1862 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1865 if (scr->selected_windows) {
1866 WMRemoveFromArray(scr->selected_windows, wwin);
1871 void wMakeWindowVisible(WWindow *wwin)
1873 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1874 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1876 if (wwin->flags.shaded) {
1877 wUnshadeWindow(wwin);
1879 if (wwin->flags.hidden) {
1880 WApplication *app;
1882 app = wApplicationOf(wwin->main_window);
1883 if (app) {
1884 /* trick to get focus to this window */
1885 app->last_focused = wwin;
1886 wUnhideApplication(app, False, False);
1889 if (wwin->flags.miniaturized) {
1890 wDeiconifyWindow(wwin);
1891 } else {
1892 if (!WFLAGP(wwin, no_focusable))
1893 wSetFocusTo(wwin->screen_ptr, wwin);
1894 wRaiseFrame(wwin->frame->core);
1899 * Do the animation while shading (called with what = SHADE)
1900 * or unshading (what = UNSHADE).
1902 #ifdef ANIMATIONS
1903 static void shade_animate(WWindow *wwin, Bool what)
1905 int y, s, w, h;
1906 time_t time0 = time(NULL);
1908 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1909 return;
1911 switch(what) {
1912 case SHADE:
1913 if (!wwin->screen_ptr->flags.startup) {
1914 /* do the shading animation */
1915 h = wwin->frame->core->height;
1916 s = h / SHADE_STEPS;
1917 if (s < 1)
1918 s = 1;
1919 w = wwin->frame->core->width;
1920 y = wwin->frame->top_width;
1921 while (h > wwin->frame->top_width + 1) {
1922 XMoveWindow(dpy, wwin->client_win, 0, y);
1923 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1924 XFlush(dpy);
1926 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1927 break;
1929 if (SHADE_DELAY > 0) {
1930 wusleep(SHADE_DELAY * 1000L);
1931 } else {
1932 wusleep(10);
1934 h -= s;
1935 y -= s;
1937 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1939 break;
1941 case UNSHADE:
1942 h = wwin->frame->top_width + wwin->frame->bottom_width;
1943 y = wwin->frame->top_width - wwin->client.height;
1944 s = abs(y) / SHADE_STEPS;
1945 if (s < 1)
1946 s = 1;
1947 w = wwin->frame->core->width;
1948 XMoveWindow(dpy, wwin->client_win, 0, y);
1949 if (s > 0) {
1950 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1951 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1952 XMoveWindow(dpy, wwin->client_win, 0, y);
1953 XFlush(dpy);
1954 if (SHADE_DELAY > 0) {
1955 wusleep(SHADE_DELAY * 2000L / 3);
1956 } else {
1957 wusleep(10);
1959 h += s;
1960 y += s;
1962 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1963 break;
1966 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1967 break;
1970 #endif