Address 'may be used uninitialized' warnings
[wmaker-crm.git] / src / actions.c
blob9617fb5ca9a13bcedcfc46e6d8aa8ca1faee38d3
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 "funcs.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"
50 /****** Global Variables ******/
52 int ignore_wks_change = 0;
53 extern Time LastTimestamp;
54 extern Time LastFocusChange;
55 extern WPreferences wPreferences;
56 extern Atom _XA_WM_TAKE_FOCUS;
57 extern void ProcessPendingEvents();
58 extern int calcIntersectionLength(int p1, int l1, int p2, int l2);
60 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
61 unsigned int *new_width, unsigned int *new_height);
62 static void save_old_geometry(WWindow *wwin, int directions);
64 /******* Local Variables *******/
65 static struct {
66 int steps;
67 int delay;
68 } shadePars[5] = {
70 SHADE_STEPS_UF, SHADE_DELAY_UF}, {
71 SHADE_STEPS_F, SHADE_DELAY_F}, {
72 SHADE_STEPS_M, SHADE_DELAY_M}, {
73 SHADE_STEPS_S, SHADE_DELAY_S}, {
74 SHADE_STEPS_US, SHADE_DELAY_US}};
76 #define UNSHADE 0
77 #define SHADE 1
78 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
79 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
81 static int compareTimes(Time t1, Time t2)
83 Time diff;
84 if (t1 == t2)
85 return 0;
86 diff = t1 - t2;
87 return (diff < 60000) ? 1 : -1;
90 #ifdef ANIMATIONS
91 static void shade_animate(WWindow *wwin, Bool what);
92 #else
93 static void shade_animate(WWindow *wwin, Bool what) { }
94 #endif
97 *----------------------------------------------------------------------
98 * wSetFocusTo--
99 * Changes the window focus to the one passed as argument.
100 * If the window to focus is not already focused, it will be brought
101 * to the head of the list of windows. Previously focused window is
102 * unfocused.
104 * Side effects:
105 * Window list may be reordered and the window focus is changed.
107 *----------------------------------------------------------------------
109 void wSetFocusTo(WScreen *scr, WWindow *wwin)
111 static WScreen *old_scr = NULL;
113 WWindow *old_focused;
114 WWindow *focused = scr->focused_window;
115 Time timestamp = LastTimestamp;
116 WApplication *oapp = NULL, *napp = NULL;
117 int wasfocused;
119 if (scr->flags.ignore_focus_events || compareTimes(LastFocusChange, timestamp) > 0)
120 return;
122 if (!old_scr)
123 old_scr = scr;
125 old_focused = old_scr->focused_window;
127 LastFocusChange = timestamp;
129 if (old_focused)
130 oapp = wApplicationOf(old_focused->main_window);
132 if (wwin == NULL) {
133 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
134 if (old_focused)
135 wWindowUnfocus(old_focused);
137 if (oapp) {
138 wAppMenuUnmap(oapp->menu);
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 wApplicationDeactivate(oapp);
206 wWindowFocus(wwin, focused);
208 if (napp && !wasfocused) {
209 #ifdef USER_MENU
210 wUserMenuRefreshInstances(napp->menu, wwin);
211 #endif /* USER_MENU */
213 if (wwin->flags.mapped)
214 wAppMenuMap(napp->menu, wwin);
216 if (napp)
217 wApplicationActivate(napp);
219 XFlush(dpy);
220 old_scr = scr;
223 void wShadeWindow(WWindow *wwin)
226 if (wwin->flags.shaded)
227 return;
229 XLowerWindow(dpy, wwin->client_win);
230 shade_animate(wwin, SHADE);
232 wwin->flags.skip_next_animation = 0;
233 wwin->flags.shaded = 1;
234 wwin->flags.mapped = 0;
235 /* prevent window withdrawal when getting UnmapNotify */
236 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
237 XUnmapWindow(dpy, wwin->client_win);
238 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
240 /* for the client it's just like iconification */
241 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
243 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
244 wWindowSynthConfigureNotify(wwin);
247 wClientSetState(wwin, IconicState, None);
250 WMPostNotificationName(WMNChangedState, wwin, "shade");
252 #ifdef ANIMATIONS
253 if (!wwin->screen_ptr->flags.startup) {
254 /* Catch up with events not processed while animation was running */
255 ProcessPendingEvents();
257 #endif
260 void wUnshadeWindow(WWindow *wwin)
263 if (!wwin->flags.shaded)
264 return;
266 wwin->flags.shaded = 0;
267 wwin->flags.mapped = 1;
268 XMapWindow(dpy, wwin->client_win);
270 shade_animate(wwin, UNSHADE);
272 wwin->flags.skip_next_animation = 0;
273 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
274 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
276 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
277 wWindowSynthConfigureNotify(wwin);
279 /* if the window is focused, set the focus again as it was disabled during
280 * shading */
281 if (wwin->flags.focused)
282 wSetFocusTo(wwin->screen_ptr, wwin);
284 WMPostNotificationName(WMNChangedState, wwin, "shade");
287 /* Set the old coordinates using the current values */
288 static void save_old_geometry(WWindow *wwin, int directions)
290 if (directions & MAX_HORIZONTAL || ! wwin->old_geometry.width) {
291 wwin->old_geometry.width = wwin->client.width;
292 wwin->old_geometry.x = wwin->frame_x;
294 if (directions & MAX_VERTICAL || ! wwin->old_geometry.height) {
295 wwin->old_geometry.height = wwin->client.height;
296 wwin->old_geometry.y = wwin->frame_y;
300 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
302 WMRect old_geom_rect;
303 int old_head;
304 Bool same_head;
306 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
307 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
308 same_head = (wGetHeadForWindow(wwin) == old_head);
309 *x = (wwin->old_geometry.x && same_head) ? wwin->old_geometry.x : wwin->frame_x;
310 *y = (wwin->old_geometry.y && same_head) ? wwin->old_geometry.y : wwin->frame_y;
311 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
312 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
315 #define IS_MAX_HORIZONTALLY(directions) ((directions & MAX_HORIZONTAL) | (directions & MAX_LEFTHALF) | (directions & MAX_RIGHTHALF))
316 void wMaximizeWindow(WWindow *wwin, int directions)
318 int new_x, new_y;
319 unsigned int new_width, new_height, half_scr_width;
320 int maximus_x = 0;
321 int maximus_y = 0;
322 unsigned int maximus_width = 0;
323 unsigned int maximus_height = 0;
324 WArea usableArea, totalArea;
325 Bool has_border = 1;
326 int save_directions = 0;
327 int adj_size;
329 if (!IS_RESIZABLE(wwin))
330 return;
332 if (!HAS_BORDER(wwin))
333 has_border = 0;
335 /* the size to adjust the geometry */
336 adj_size = FRAME_BORDER_WIDTH * 2 * has_border;
338 /* save old coordinates before we change the current values
339 * but never if the window has been Maximusized */
340 if (!(wwin->flags.old_maximized & MAX_MAXIMUS)) {
341 if ((directions & MAX_VERTICAL) &&
342 !(wwin->flags.maximized & MAX_VERTICAL))
343 save_directions |= MAX_VERTICAL;
344 if (IS_MAX_HORIZONTALLY(directions) &&
345 !IS_MAX_HORIZONTALLY(wwin->flags.maximized))
346 save_directions |= MAX_HORIZONTAL;
348 if ((directions & MAX_MAXIMUS) && !wwin->flags.maximized)
349 save_directions |= MAX_VERTICAL | MAX_HORIZONTAL;
350 save_old_geometry(wwin, save_directions);
352 totalArea.x1 = 0;
353 totalArea.y1 = 0;
354 totalArea.x2 = wwin->screen_ptr->scr_width;
355 totalArea.y2 = wwin->screen_ptr->scr_height;
356 usableArea = totalArea;
358 if (!(directions & MAX_IGNORE_XINERAMA)) {
359 WScreen *scr = wwin->screen_ptr;
360 int head;
362 if (directions & MAX_KEYBOARD)
363 head = wGetHeadForWindow(wwin);
364 else
365 head = wGetHeadForPointerLocation(scr);
367 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
370 /* remember Maximus geometry if we'll need it later */
371 if ((wwin->flags.old_maximized & MAX_MAXIMUS) || (directions & MAX_MAXIMUS))
372 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
374 /* Only save directions, not kbd or xinerama hints */
375 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
377 if (WFLAGP(wwin, full_maximize)) {
378 usableArea = totalArea;
380 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
382 if (wwin->flags.shaded) {
383 wwin->flags.skip_next_animation = 1;
384 wUnshadeWindow(wwin);
387 if (directions & MAX_HORIZONTAL) {
388 new_width = usableArea.x2 - usableArea.x1 - adj_size;
389 new_x = usableArea.x1;
390 } else if (directions & MAX_LEFTHALF) {
391 new_width = half_scr_width - adj_size;
392 new_x = usableArea.x1;
393 wwin->flags.old_maximized |= MAX_LEFTHALF;
394 wwin->flags.old_maximized &= ~MAX_RIGHTHALF;
395 } else if (directions & MAX_RIGHTHALF) {
396 new_width = half_scr_width - adj_size;
397 new_x = usableArea.x1 + half_scr_width;
398 wwin->flags.old_maximized |= MAX_RIGHTHALF;
399 wwin->flags.old_maximized &= ~MAX_LEFTHALF;
400 } else if (wwin->flags.old_maximized & MAX_MAXIMUS) {
401 new_x = maximus_x;
402 new_width = maximus_width - adj_size;
403 } else if (IS_MAX_HORIZONTALLY(wwin->flags.maximized)) {
404 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
405 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
406 } else {
407 wwin->flags.old_maximized &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
408 new_x = wwin->frame_x;
409 new_width = wwin->frame->core->width;
412 if (directions & MAX_VERTICAL) {
413 new_height = usableArea.y2 - usableArea.y1 - adj_size;
414 new_y = usableArea.y1;
415 if (WFLAGP(wwin, full_maximize)) {
416 new_y -= wwin->frame->top_width;
417 new_height += wwin->frame->bottom_width - 1;
419 } else if (wwin->flags.old_maximized & MAX_MAXIMUS) {
420 new_y = maximus_y;
421 new_height = maximus_height - adj_size;
422 /* HACK: this will be subtracted again below */
423 new_height += wwin->frame->top_width + wwin->frame->bottom_width;
424 } else if (wwin->flags.maximized & MAX_VERTICAL) {
425 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
426 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
427 /* HACK: this will be subtracted again below */
428 new_height += wwin->frame->top_width + wwin->frame->bottom_width;
429 wwin->flags.old_maximized &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
430 } else {
431 new_y = wwin->frame_y;
432 new_height = wwin->frame->core->height;
435 if (!WFLAGP(wwin, full_maximize)) {
436 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
439 if (directions & MAX_MAXIMUS) {
440 new_x = maximus_x;
441 new_y = maximus_y;
442 new_width = maximus_width - adj_size;
443 new_height = maximus_height - adj_size;
444 if (WFLAGP(wwin, full_maximize) && new_y == 0) {
445 new_y -= wwin->frame->top_width;
446 new_height += wwin->frame->top_width - 1;
448 wwin->flags.old_maximized |= MAX_MAXIMUS;
451 wWindowConstrainSize(wwin, &new_width, &new_height);
453 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
454 usableArea.y2 - usableArea.y1, &new_width, &new_height);
456 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
457 wWindowSynthConfigureNotify(wwin);
459 WMPostNotificationName(WMNChangedState, wwin, "maximize");
461 /* set maximization state */
462 wwin->flags.maximized = directions;
463 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
464 wwin->flags.maximized = MAX_MAXIMUS;
467 /* generic (un)maximizer */
468 void handleMaximize(WWindow *wwin, int directions)
470 int current = wwin->flags.maximized;
471 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
472 int effective = requested ^ current;
473 int flags = directions & ~requested;
475 if (!effective) {
476 /* allow wMaximizeWindow to restore the Maximusized size */
477 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
478 !(requested & MAX_MAXIMUS))
479 wMaximizeWindow(wwin, flags);
480 else
481 wUnmaximizeWindow(wwin);
483 else {
484 /* MAX_MAXIMUS takes precedence */
485 effective &= ~MAX_MAXIMUS;
486 if (requested & MAX_MAXIMUS) {
487 /* window was previously Maximusized then maximized */
488 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !current) {
489 wUnmaximizeWindow(wwin);
490 return;
492 else
493 effective = MAX_MAXIMUS;
495 else if (requested == (MAX_HORIZONTAL | MAX_VERTICAL))
496 effective = requested;
497 else {
498 /* handle MAX_HORIZONTAL -> MAX_(LEFT|RIGHT)HALF */
499 if (IS_MAX_HORIZONTALLY(current)) {
500 if (IS_MAX_HORIZONTALLY(requested)) {
501 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF);
502 effective |= (requested & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF));
503 if (requested & MAX_HORIZONTAL) {
504 /* restore to half maximization */
505 if (wwin->flags.old_maximized & MAX_LEFTHALF)
506 effective |= MAX_LEFTHALF;
507 else if (wwin->flags.old_maximized & MAX_RIGHTHALF)
508 effective |= MAX_RIGHTHALF;
510 /* MAX_VERTICAL is implicit with MAX_(LEFT|RIGHT)HALF */
511 else
512 effective |= MAX_VERTICAL;
513 } else {
514 /* toggling MAX_VERTICAL */
515 if ((requested & MAX_VERTICAL) &&
516 (current & MAX_VERTICAL)) {
517 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF | MAX_VERTICAL);
521 /* handle MAX_VERTICAL -> MAX_(LEFT|RIGHT)HALF */
522 if (current & MAX_VERTICAL) {
523 if ((requested & MAX_LEFTHALF) ||
524 (requested & MAX_RIGHTHALF)) {
525 effective |= MAX_VERTICAL;
528 /* toggling MAX_HORIZONTAL */
529 if ((requested & MAX_HORIZONTAL) &&
530 (current & MAX_HORIZONTAL))
531 effective &= ~MAX_HORIZONTAL;
533 wMaximizeWindow(wwin, effective | flags);
535 return;
538 /* the window boundary coordinates */
539 typedef struct {
540 int left;
541 int right;
542 int bottom;
543 int top;
544 int width;
545 int height;
546 } win_coords;
548 static void set_window_coords(WWindow *wwin, win_coords *obs)
550 obs->left = wwin->frame_x;
551 obs->top = wwin->frame_y;
552 obs->width = wwin->frame->core->width;
553 obs->height = wwin->frame->core->height;
554 obs->bottom = obs->top + obs->height;
555 obs->right = obs->left + obs->width;
559 * Maximus: tiled maximization (maximize without overlapping other windows)
561 * The original window 'orig' will be maximized to new coordinates 'new'.
562 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
564 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
565 unsigned int *new_width, unsigned int *new_height)
567 WWindow *tmp;
568 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
569 short int adjust_height;
570 int x_intsect, y_intsect;
571 /* the obstructing, original and new windows */
572 win_coords obs, orig, new;
574 /* set the original coordinate positions of the window to be Maximumized */
575 if (wwin->flags.maximized) {
576 /* window is already maximized; consider original geometry */
577 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
578 orig.bottom = orig.top + orig.height;
579 orig.right = orig.left + orig.width;
581 else
582 set_window_coords(wwin, &orig);
584 /* Try to fully maximize first, then readjust later */
585 new.left = usableArea.x1;
586 new.right = usableArea.x2;
587 new.top = usableArea.y1;
588 new.bottom = usableArea.y2;
590 if (HAS_TITLEBAR(wwin))
591 tbar_height_0 = TITLEBAR_HEIGHT;
592 if (HAS_RESIZEBAR(wwin))
593 rbar_height_0 = RESIZEBAR_HEIGHT;
594 if (HAS_BORDER(wwin))
595 bd_width_0 = FRAME_BORDER_WIDTH;
597 /* the length to be subtracted if the window has titlebar, etc */
598 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
600 tmp = wwin;
601 /* The focused window is always the last in the list */
602 while (tmp->prev) {
603 /* ignore windows in other workspaces etc */
604 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
605 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
606 tmp = tmp->prev;
607 continue;
609 tmp = tmp->prev;
611 /* Set the coordinates of obstructing window */
612 set_window_coords(tmp, &obs);
614 /* Try to maximize in the y direction first */
615 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
616 if (x_intsect != 0) {
617 /* TODO: Consider the case when coords are equal */
618 if (obs.bottom < orig.top && obs.bottom > new.top) {
619 /* w_0 is below the bottom of w_j */
620 new.top = obs.bottom + 1;
622 if (orig.bottom < obs.top && obs.top < new.bottom) {
623 /* The bottom of w_0 is above the top of w_j */
624 new.bottom = obs.top - 1;
629 tmp = wwin;
630 while (tmp->prev) {
631 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
632 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
633 tmp = tmp->prev;
634 continue;
636 tmp = tmp->prev;
638 set_window_coords(tmp, &obs);
641 * Use the new.top and new.height instead of original values
642 * as they may have different intersections with the obstructing windows
644 new.height = new.bottom - new.top - adjust_height;
645 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
646 if (y_intsect != 0) {
647 if (obs.right < orig.left && obs.right > new.left) {
648 /* w_0 is completely to the right of w_j */
649 new.left = obs.right + 1;
651 if (orig.right < obs.left && obs.left < new.right) {
652 /* w_0 is completely to the left of w_j */
653 new.right = obs.left - 1;
658 *new_x = new.left;
659 *new_y = new.top;
660 /* xcalc needs -7 here, but other apps don't */
661 *new_height = new.bottom - new.top - adjust_height - 1;;
662 *new_width = new.right - new.left;
665 void wUnmaximizeWindow(WWindow *wwin)
667 int x, y, w, h;
669 if (!wwin->flags.maximized)
670 return;
672 if (wwin->flags.shaded) {
673 wwin->flags.skip_next_animation = 1;
674 wUnshadeWindow(wwin);
676 /* Use old coordinates if they are set, current values otherwise */
677 remember_geometry(wwin, &x, &y, &w, &h);
679 wwin->flags.maximized = 0;
680 wwin->flags.old_maximized = 0;
681 wWindowConfigure(wwin, x, y, w, h);
682 wWindowSynthConfigureNotify(wwin);
684 WMPostNotificationName(WMNChangedState, wwin, "maximize");
687 void wFullscreenWindow(WWindow *wwin)
689 int head;
690 WMRect rect;
692 if (wwin->flags.fullscreen)
693 return;
695 wwin->flags.fullscreen = True;
697 wWindowConfigureBorders(wwin);
699 ChangeStackingLevel(wwin->frame->core, WMFullscreenLevel);
701 wwin->bfs_geometry.x = wwin->frame_x;
702 wwin->bfs_geometry.y = wwin->frame_y;
703 wwin->bfs_geometry.width = wwin->frame->core->width;
704 wwin->bfs_geometry.height = wwin->frame->core->height;
706 head = wGetHeadForWindow(wwin);
707 rect = wGetRectForHead(wwin->screen_ptr, head);
708 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
710 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
713 void wUnfullscreenWindow(WWindow *wwin)
715 if (!wwin->flags.fullscreen)
716 return;
718 wwin->flags.fullscreen = False;
720 if (wwin->frame->core->stacking->window_level == WMFullscreenLevel) {
721 if (WFLAGP(wwin, sunken)) {
722 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
723 } else if (WFLAGP(wwin, floating)) {
724 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
725 } else {
726 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
730 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
731 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
733 wWindowConfigureBorders(wwin);
735 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
736 wFrameWindowPaint(wwin->frame);
739 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
742 #ifdef ANIMATIONS
743 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
745 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
746 float cx, cy, cw, ch;
747 float xstep, ystep, wstep, hstep;
748 XPoint points[5];
749 float dx, dch, midy;
750 float angle, final_angle, delta;
752 xstep = (float)(fx - x) / steps;
753 ystep = (float)(fy - y) / steps;
754 wstep = (float)(fw - w) / steps;
755 hstep = (float)(fh - h) / steps;
757 cx = (float)x;
758 cy = (float)y;
759 cw = (float)w;
760 ch = (float)h;
762 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
763 delta = (float)(final_angle / FRAMES);
764 for (angle = 0;; angle += delta) {
765 if (angle > final_angle)
766 angle = final_angle;
768 dx = (cw / 10) - ((cw / 5) * sin(angle));
769 dch = (ch / 2) * cos(angle);
770 midy = cy + (ch / 2);
772 points[0].x = cx + dx;
773 points[0].y = midy - dch;
774 points[1].x = cx + cw - dx;
775 points[1].y = points[0].y;
776 points[2].x = cx + cw + dx;
777 points[2].y = midy + dch;
778 points[3].x = cx - dx;
779 points[3].y = points[2].y;
780 points[4].x = points[0].x;
781 points[4].y = points[0].y;
783 XGrabServer(dpy);
784 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
785 XFlush(dpy);
786 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
788 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
789 XUngrabServer(dpy);
790 cx += xstep;
791 cy += ystep;
792 cw += wstep;
793 ch += hstep;
794 if (angle >= final_angle)
795 break;
798 XFlush(dpy);
801 #undef FRAMES
803 static void
804 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
806 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
807 float cx, cy, cw, ch;
808 float xstep, ystep, wstep, hstep;
809 XPoint points[5];
810 float angle, final_angle, a, d, delta;
812 x += w / 2;
813 y += h / 2;
814 fx += fw / 2;
815 fy += fh / 2;
817 xstep = (float)(fx - x) / steps;
818 ystep = (float)(fy - y) / steps;
819 wstep = (float)(fw - w) / steps;
820 hstep = (float)(fh - h) / steps;
822 cx = (float)x;
823 cy = (float)y;
824 cw = (float)w;
825 ch = (float)h;
827 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
828 delta = (float)(final_angle / FRAMES);
829 for (angle = 0;; angle += delta) {
830 if (angle > final_angle)
831 angle = final_angle;
833 a = atan(ch / cw);
834 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
836 points[0].x = cx + cos(angle - a) * d;
837 points[0].y = cy + sin(angle - a) * d;
838 points[1].x = cx + cos(angle + a) * d;
839 points[1].y = cy + sin(angle + a) * d;
840 points[2].x = cx + cos(angle - a + WM_PI) * d;
841 points[2].y = cy + sin(angle - a + WM_PI) * d;
842 points[3].x = cx + cos(angle + a + WM_PI) * d;
843 points[3].y = cy + sin(angle + a + WM_PI) * d;
844 points[4].x = cx + cos(angle - a) * d;
845 points[4].y = cy + sin(angle - a) * d;
846 XGrabServer(dpy);
847 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
848 XFlush(dpy);
849 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
851 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
852 XUngrabServer(dpy);
853 cx += xstep;
854 cy += ystep;
855 cw += wstep;
856 ch += hstep;
857 if (angle >= final_angle)
858 break;
861 XFlush(dpy);
864 #undef FRAMES
866 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
868 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
869 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
870 float xstep, ystep, wstep, hstep;
871 int i, j;
873 xstep = (float)(fx - x) / steps;
874 ystep = (float)(fy - y) / steps;
875 wstep = (float)(fw - w) / steps;
876 hstep = (float)(fh - h) / steps;
878 for (j = 0; j < FRAMES; j++) {
879 cx[j] = (float)x;
880 cy[j] = (float)y;
881 cw[j] = (float)w;
882 ch[j] = (float)h;
884 XGrabServer(dpy);
885 for (i = 0; i < steps; i++) {
886 for (j = 0; j < FRAMES; j++) {
887 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
888 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
890 XFlush(dpy);
891 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
893 for (j = 0; j < FRAMES; j++) {
894 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
895 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
896 if (j < FRAMES - 1) {
897 cx[j] = cx[j + 1];
898 cy[j] = cy[j + 1];
899 cw[j] = cw[j + 1];
900 ch[j] = ch[j + 1];
901 } else {
902 cx[j] += xstep;
903 cy[j] += ystep;
904 cw[j] += wstep;
905 ch[j] += hstep;
910 for (j = 0; j < FRAMES; j++) {
911 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
913 XFlush(dpy);
914 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
916 for (j = 0; j < FRAMES; j++) {
917 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
920 XUngrabServer(dpy);
923 #undef FRAMES
925 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
927 int style = wPreferences.iconification_style; /* Catch the value */
928 int steps;
930 if (style == WIS_NONE)
931 return;
933 if (style == WIS_RANDOM) {
934 style = rand() % 3;
937 switch (style) {
938 case WIS_TWIST:
939 steps = MINIATURIZE_ANIMATION_STEPS_T;
940 if (steps > 0)
941 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
942 break;
943 case WIS_FLIP:
944 steps = MINIATURIZE_ANIMATION_STEPS_F;
945 if (steps > 0)
946 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
947 break;
948 case WIS_ZOOM:
949 default:
950 steps = MINIATURIZE_ANIMATION_STEPS_Z;
951 if (steps > 0)
952 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
953 break;
956 #endif /* ANIMATIONS */
958 static void flushExpose(void)
960 XEvent tmpev;
962 while (XCheckTypedEvent(dpy, Expose, &tmpev))
963 WMHandleEvent(&tmpev);
964 XSync(dpy, 0);
967 static void unmapTransientsFor(WWindow *wwin)
969 WWindow *tmp;
971 tmp = wwin->screen_ptr->focused_window;
972 while (tmp) {
973 /* unmap the transients for this transient */
974 if (tmp != wwin && tmp->transient_for == wwin->client_win
975 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
976 unmapTransientsFor(tmp);
977 tmp->flags.miniaturized = 1;
978 if (!tmp->flags.shaded) {
979 wWindowUnmap(tmp);
980 } else {
981 XUnmapWindow(dpy, tmp->frame->core->window);
984 if (!tmp->flags.shaded)
986 wClientSetState(tmp, IconicState, None);
988 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
990 tmp = tmp->prev;
994 static void mapTransientsFor(WWindow *wwin)
996 WWindow *tmp;
998 tmp = wwin->screen_ptr->focused_window;
999 while (tmp) {
1000 /* recursively map the transients for this transient */
1001 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1002 && tmp->icon == NULL) {
1003 mapTransientsFor(tmp);
1004 tmp->flags.miniaturized = 0;
1005 if (!tmp->flags.shaded) {
1006 wWindowMap(tmp);
1007 } else {
1008 XMapWindow(dpy, tmp->frame->core->window);
1010 tmp->flags.semi_focused = 0;
1012 if (!tmp->flags.shaded)
1014 wClientSetState(tmp, NormalState, None);
1016 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1018 tmp = tmp->prev;
1022 static WWindow *recursiveTransientFor(WWindow * wwin)
1024 int i;
1026 if (!wwin)
1027 return None;
1029 /* hackish way to detect transient_for cycle */
1030 i = wwin->screen_ptr->window_count + 1;
1032 while (wwin && wwin->transient_for != None && i > 0) {
1033 wwin = wWindowFor(wwin->transient_for);
1034 i--;
1036 if (i == 0 && wwin) {
1037 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
1038 return NULL;
1041 return wwin;
1044 void wIconifyWindow(WWindow * wwin)
1046 XWindowAttributes attribs;
1047 int present;
1049 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1050 return; /* the window doesn't exist anymore */
1052 if (wwin->flags.miniaturized)
1053 return; /* already miniaturized */
1055 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1056 WWindow *owner = wWindowFor(wwin->transient_for);
1058 if (owner && owner->flags.miniaturized)
1059 return;
1062 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1064 /* if the window is in another workspace, simplify process */
1065 if (present) {
1066 /* icon creation may take a while */
1067 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1068 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1069 GrabModeAsync, None, None, CurrentTime);
1072 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1073 if (!wwin->flags.icon_moved)
1074 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1076 wwin->icon = wIconCreate(wwin);
1077 wwin->icon->mapped = 1;
1080 wwin->flags.miniaturized = 1;
1081 wwin->flags.mapped = 0;
1083 /* unmap transients */
1084 unmapTransientsFor(wwin);
1086 if (present) {
1087 XUngrabPointer(dpy, CurrentTime);
1088 wWindowUnmap(wwin);
1089 /* let all Expose events arrive so that we can repaint
1090 * something before the animation starts (and the server is grabbed) */
1091 XSync(dpy, 0);
1093 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1094 wClientSetState(wwin, IconicState, None);
1095 else
1096 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1098 flushExpose();
1099 #ifdef ANIMATIONS
1100 if (!wwin->screen_ptr->flags.startup && !wwin->flags.skip_next_animation
1101 && !wPreferences.no_animations) {
1102 int ix, iy, iw, ih;
1104 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1105 ix = wwin->icon_x;
1106 iy = wwin->icon_y;
1107 iw = wwin->icon->core->width;
1108 ih = wwin->icon->core->height;
1109 } else {
1110 if (wwin->flags.net_handle_icon) {
1111 ix = wwin->icon_x;
1112 iy = wwin->icon_y;
1113 iw = wwin->icon_w;
1114 ih = wwin->icon_h;
1115 } else {
1116 ix = 0;
1117 iy = 0;
1118 iw = wwin->screen_ptr->scr_width;
1119 ih = wwin->screen_ptr->scr_height;
1122 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1123 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1125 #endif
1128 wwin->flags.skip_next_animation = 0;
1130 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1131 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1132 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1133 XMapWindow(dpy, wwin->icon->core->window);
1135 AddToStackList(wwin->icon->core);
1136 wLowerFrame(wwin->icon->core);
1139 if (present) {
1140 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1143 * It doesn't seem to be working and causes button event hangup
1144 * when deiconifying a transient window.
1145 setupIconGrabs(wwin->icon);
1147 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1148 && wPreferences.focus_mode == WKF_CLICK) {
1149 WWindow *tmp;
1151 tmp = wwin->prev;
1152 while (tmp) {
1153 if (!WFLAGP(tmp, no_focusable)
1154 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1155 && (wwin->frame->workspace == tmp->frame->workspace))
1156 break;
1157 tmp = tmp->prev;
1159 wSetFocusTo(wwin->screen_ptr, tmp);
1160 } else if (wPreferences.focus_mode != WKF_CLICK) {
1161 wSetFocusTo(wwin->screen_ptr, NULL);
1163 #ifdef ANIMATIONS
1164 if (!wwin->screen_ptr->flags.startup) {
1165 /* Catch up with events not processed while animation was running */
1166 Window clientwin = wwin->client_win;
1168 ProcessPendingEvents();
1170 /* the window can disappear while ProcessPendingEvents() runs */
1171 if (!wWindowFor(clientwin)) {
1172 return;
1175 #endif
1178 /* maybe we want to do this regardless of net_handle_icon
1179 * it seems to me we might break behaviour this way.
1181 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1182 && !wwin->flags.net_handle_icon)
1183 wIconSelect(wwin->icon);
1185 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1187 if (wPreferences.auto_arrange_icons)
1188 wArrangeIcons(wwin->screen_ptr, True);
1191 void wDeiconifyWindow(WWindow *wwin)
1193 /* Let's avoid changing workspace while deiconifying */
1194 ignore_wks_change = 1;
1196 /* we're hiding for show_desktop */
1197 int netwm_hidden = wwin->flags.net_show_desktop &&
1198 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1200 if (!netwm_hidden)
1201 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1203 if (!wwin->flags.miniaturized)
1204 return;
1206 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1207 WWindow *owner = recursiveTransientFor(wwin);
1209 if (owner && owner->flags.miniaturized) {
1210 wDeiconifyWindow(owner);
1211 wSetFocusTo(wwin->screen_ptr, wwin);
1212 wRaiseFrame(wwin->frame->core);
1213 return;
1217 wwin->flags.miniaturized = 0;
1219 if (!netwm_hidden && !wwin->flags.shaded)
1220 wwin->flags.mapped = 1;
1222 if (!netwm_hidden || wPreferences.sticky_icons) {
1223 /* maybe we want to do this regardless of net_handle_icon
1224 * it seems to me we might break behaviour this way.
1226 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1227 && wwin->icon != NULL) {
1228 if (wwin->icon->selected)
1229 wIconSelect(wwin->icon);
1231 XUnmapWindow(dpy, wwin->icon->core->window);
1235 /* if the window is in another workspace, do it silently */
1236 if (!netwm_hidden) {
1237 #ifdef ANIMATIONS
1238 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations
1239 && !wwin->flags.skip_next_animation && wwin->icon != NULL) {
1240 int ix, iy, iw, ih;
1242 if (!wPreferences.disable_miniwindows
1243 && !wwin->flags.net_handle_icon) {
1244 ix = wwin->icon_x;
1245 iy = wwin->icon_y;
1246 iw = wwin->icon->core->width;
1247 ih = wwin->icon->core->height;
1248 } else {
1249 if (wwin->flags.net_handle_icon) {
1250 ix = wwin->icon_x;
1251 iy = wwin->icon_y;
1252 iw = wwin->icon_w;
1253 ih = wwin->icon_h;
1254 } else {
1255 ix = 0;
1256 iy = 0;
1257 iw = wwin->screen_ptr->scr_width;
1258 ih = wwin->screen_ptr->scr_height;
1261 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1262 wwin->frame_x, wwin->frame_y,
1263 wwin->frame->core->width, wwin->frame->core->height);
1265 #endif /* ANIMATIONS */
1266 wwin->flags.skip_next_animation = 0;
1267 XGrabServer(dpy);
1268 if (!wwin->flags.shaded)
1269 XMapWindow(dpy, wwin->client_win);
1271 XMapWindow(dpy, wwin->frame->core->window);
1272 wRaiseFrame(wwin->frame->core);
1273 if (!wwin->flags.shaded)
1274 wClientSetState(wwin, NormalState, None);
1276 mapTransientsFor(wwin);
1279 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1280 && !wwin->flags.net_handle_icon) {
1281 RemoveFromStackList(wwin->icon->core);
1282 /* removeIconGrabs(wwin->icon); */
1283 wIconDestroy(wwin->icon);
1284 wwin->icon = NULL;
1287 if (!netwm_hidden) {
1288 XUngrabServer(dpy);
1290 wSetFocusTo(wwin->screen_ptr, wwin);
1292 #ifdef ANIMATIONS
1293 if (!wwin->screen_ptr->flags.startup) {
1294 /* Catch up with events not processed while animation was running */
1295 Window clientwin = wwin->client_win;
1297 ProcessPendingEvents();
1299 /* the window can disappear while ProcessPendingEvents() runs */
1300 if (!wWindowFor(clientwin))
1301 return;
1303 #endif
1306 if (wPreferences.auto_arrange_icons)
1307 wArrangeIcons(wwin->screen_ptr, True);
1309 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1311 /* In case we were shaded and iconified, also unshade */
1312 if (!netwm_hidden)
1313 wUnshadeWindow(wwin);
1315 ignore_wks_change = 0;
1318 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1320 if (wwin->flags.miniaturized) {
1321 if (wwin->icon) {
1322 XUnmapWindow(dpy, wwin->icon->core->window);
1323 wwin->icon->mapped = 0;
1325 wwin->flags.hidden = 1;
1327 WMPostNotificationName(WMNChangedState, wwin, "hide");
1328 return;
1331 if (wwin->flags.inspector_open) {
1332 wHideInspectorForWindow(wwin);
1335 wwin->flags.hidden = 1;
1336 wWindowUnmap(wwin);
1338 wClientSetState(wwin, IconicState, icon->icon_win);
1339 flushExpose();
1341 #ifdef ANIMATIONS
1342 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1343 !wwin->flags.skip_next_animation && animate) {
1344 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1345 wwin->frame->core->width, wwin->frame->core->height,
1346 icon_x, icon_y, icon->core->width, icon->core->height);
1348 #endif
1349 wwin->flags.skip_next_animation = 0;
1351 WMPostNotificationName(WMNChangedState, wwin, "hide");
1354 void wHideAll(WScreen *scr)
1356 WWindow *wwin;
1357 WWindow **windows;
1358 WMenu *menu;
1359 unsigned int wcount = 0;
1360 int i;
1362 if (!scr)
1363 return;
1365 menu = scr->switch_menu;
1367 windows = wmalloc(sizeof(WWindow *));
1369 if (menu != NULL) {
1370 for (i = 0; i < menu->entry_no; i++) {
1371 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1372 wcount++;
1373 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1375 } else {
1376 wwin = scr->focused_window;
1378 while (wwin) {
1379 windows[wcount] = wwin;
1380 wcount++;
1381 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1382 wwin = wwin->prev;
1387 for (i = 0; i < wcount; i++) {
1388 wwin = windows[i];
1389 if (wwin->frame->workspace == scr->current_workspace
1390 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1391 && !wwin->flags.internal_window
1392 && !WFLAGP(wwin, no_miniaturizable)
1394 wwin->flags.skip_next_animation = 1;
1395 wIconifyWindow(wwin);
1399 wfree(windows);
1402 void wHideOtherApplications(WWindow *awin)
1404 WWindow *wwin;
1405 WApplication *tapp;
1407 if (!awin)
1408 return;
1409 wwin = awin->screen_ptr->focused_window;
1411 while (wwin) {
1412 if (wwin != awin
1413 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1414 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1415 && !wwin->flags.internal_window
1416 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1418 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1419 if (!WFLAGP(wwin, no_miniaturizable)) {
1420 wwin->flags.skip_next_animation = 1;
1421 wIconifyWindow(wwin);
1423 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1424 tapp = wApplicationOf(wwin->main_window);
1425 if (tapp) {
1426 tapp->flags.skip_next_animation = 1;
1427 wHideApplication(tapp);
1428 } else {
1429 if (!WFLAGP(wwin, no_miniaturizable)) {
1430 wwin->flags.skip_next_animation = 1;
1431 wIconifyWindow(wwin);
1436 wwin = wwin->prev;
1439 wSetFocusTo(awin->screen_ptr, awin);
1443 void wHideApplication(WApplication *wapp)
1445 WScreen *scr;
1446 WWindow *wlist;
1447 int hadfocus;
1448 int animate;
1450 if (!wapp) {
1451 wwarning("trying to hide a non grouped window");
1452 return;
1454 if (!wapp->main_window_desc) {
1455 wwarning("group leader not found for window group");
1456 return;
1458 scr = wapp->main_window_desc->screen_ptr;
1459 hadfocus = 0;
1460 wlist = scr->focused_window;
1461 if (!wlist)
1462 return;
1464 if (wlist->main_window == wapp->main_window)
1465 wapp->last_focused = wlist;
1466 else
1467 wapp->last_focused = NULL;
1469 animate = !wapp->flags.skip_next_animation;
1471 while (wlist) {
1472 if (wlist->main_window == wapp->main_window) {
1473 if (wlist->flags.focused) {
1474 hadfocus = 1;
1476 if (wapp->app_icon) {
1477 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1478 wapp->app_icon->y_pos, wlist, animate);
1479 animate = False;
1482 wlist = wlist->prev;
1485 wapp->flags.skip_next_animation = 0;
1487 if (hadfocus) {
1488 if (wPreferences.focus_mode == WKF_CLICK) {
1489 wlist = scr->focused_window;
1490 while (wlist) {
1491 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1492 && (wlist->flags.mapped || wlist->flags.shaded))
1493 break;
1494 wlist = wlist->prev;
1496 wSetFocusTo(scr, wlist);
1497 } else {
1498 wSetFocusTo(scr, NULL);
1502 wapp->flags.hidden = 1;
1504 if (wPreferences.auto_arrange_icons) {
1505 wArrangeIcons(scr, True);
1507 #ifdef HIDDENDOT
1508 if (wapp->app_icon)
1509 wAppIconPaint(wapp->app_icon);
1510 #endif
1513 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1515 if (bringToCurrentWS)
1516 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1518 wwin->flags.hidden = 0;
1520 #ifdef ANIMATIONS
1521 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1522 animateResize(wwin->screen_ptr, icon_x, icon_y,
1523 icon->core->width, icon->core->height,
1524 wwin->frame_x, wwin->frame_y,
1525 wwin->frame->core->width, wwin->frame->core->height);
1527 #endif
1528 wwin->flags.skip_next_animation = 0;
1529 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1530 XMapWindow(dpy, wwin->client_win);
1531 XMapWindow(dpy, wwin->frame->core->window);
1532 wClientSetState(wwin, NormalState, None);
1533 wwin->flags.mapped = 1;
1534 wRaiseFrame(wwin->frame->core);
1536 if (wwin->flags.inspector_open) {
1537 wUnhideInspectorForWindow(wwin);
1540 WMPostNotificationName(WMNChangedState, wwin, "hide");
1543 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1545 WScreen *scr;
1546 WWindow *wlist, *next;
1547 WWindow *focused = NULL;
1548 int animate;
1550 if (!wapp)
1551 return;
1553 scr = wapp->main_window_desc->screen_ptr;
1554 wlist = scr->focused_window;
1555 if (!wlist)
1556 return;
1558 /* goto beginning of list */
1559 while (wlist->prev)
1560 wlist = wlist->prev;
1562 animate = !wapp->flags.skip_next_animation;
1564 while (wlist) {
1565 next = wlist->next;
1567 if (wlist->main_window == wapp->main_window) {
1568 if (wlist->flags.focused)
1569 focused = wlist;
1570 else if (!focused || !focused->flags.focused)
1571 focused = wlist;
1573 if (wlist->flags.miniaturized) {
1574 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1575 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1576 if (!wlist->icon->mapped) {
1577 int x, y;
1579 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1580 if (wlist->icon_x != x || wlist->icon_y != y) {
1581 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1583 wlist->icon_x = x;
1584 wlist->icon_y = y;
1585 XMapWindow(dpy, wlist->icon->core->window);
1586 wlist->icon->mapped = 1;
1588 wRaiseFrame(wlist->icon->core);
1590 if (bringToCurrentWS)
1591 wWindowChangeWorkspace(wlist, scr->current_workspace);
1592 wlist->flags.hidden = 0;
1593 if (miniwindows && wlist->frame->workspace == scr->current_workspace) {
1594 wDeiconifyWindow(wlist);
1596 WMPostNotificationName(WMNChangedState, wlist, "hide");
1597 } else if (wlist->flags.shaded) {
1598 if (bringToCurrentWS)
1599 wWindowChangeWorkspace(wlist, scr->current_workspace);
1600 wlist->flags.hidden = 0;
1601 wRaiseFrame(wlist->frame->core);
1602 if (wlist->frame->workspace == scr->current_workspace) {
1603 XMapWindow(dpy, wlist->frame->core->window);
1604 if (miniwindows) {
1605 wUnshadeWindow(wlist);
1608 WMPostNotificationName(WMNChangedState, wlist, "hide");
1609 } else if (wlist->flags.hidden) {
1610 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1611 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1612 animate = False;
1613 } else {
1614 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace) {
1615 wWindowChangeWorkspace(wlist, scr->current_workspace);
1617 wRaiseFrame(wlist->frame->core);
1620 wlist = next;
1623 wapp->flags.skip_next_animation = 0;
1624 wapp->flags.hidden = 0;
1626 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1627 wRaiseFrame(wapp->last_focused->frame->core);
1628 wSetFocusTo(scr, wapp->last_focused);
1629 } else if (focused) {
1630 wSetFocusTo(scr, focused);
1632 wapp->last_focused = NULL;
1633 if (wPreferences.auto_arrange_icons) {
1634 wArrangeIcons(scr, True);
1636 #ifdef HIDDENDOT
1637 wAppIconPaint(wapp->app_icon);
1638 #endif
1641 void wShowAllWindows(WScreen *scr)
1643 WWindow *wwin, *old_foc;
1644 WApplication *wapp;
1646 old_foc = wwin = scr->focused_window;
1647 while (wwin) {
1648 if (!wwin->flags.internal_window &&
1649 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1650 if (wwin->flags.miniaturized) {
1651 wwin->flags.skip_next_animation = 1;
1652 wDeiconifyWindow(wwin);
1653 } else if (wwin->flags.hidden) {
1654 wapp = wApplicationOf(wwin->main_window);
1655 if (wapp) {
1656 wUnhideApplication(wapp, False, False);
1657 } else {
1658 wwin->flags.skip_next_animation = 1;
1659 wDeiconifyWindow(wwin);
1663 wwin = wwin->prev;
1665 wSetFocusTo(scr, old_foc);
1666 /*wRaiseFrame(old_foc->frame->core); */
1669 void wRefreshDesktop(WScreen *scr)
1671 Window win;
1672 XSetWindowAttributes attr;
1674 attr.backing_store = NotUseful;
1675 attr.save_under = False;
1676 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1677 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1678 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1679 XMapRaised(dpy, win);
1680 XDestroyWindow(dpy, win);
1681 XFlush(dpy);
1684 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1686 WWindow *wwin;
1687 WAppIcon *aicon;
1689 int head;
1690 const int heads = wXineramaHeads(scr);
1692 struct HeadVars {
1693 int pf; /* primary axis */
1694 int sf; /* secondary axis */
1695 int fullW;
1696 int fullH;
1697 int pi, si;
1698 int sx1, sx2, sy1, sy2; /* screen boundary */
1699 int sw, sh;
1700 int xo, yo;
1701 int xs, ys;
1702 } *vars;
1704 int isize = wPreferences.icon_size;
1706 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1708 for (head = 0; head < heads; ++head) {
1709 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1710 WMRect rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1711 vars[head].pi = vars[head].si = 0;
1712 vars[head].sx1 = rect.pos.x;
1713 vars[head].sy1 = rect.pos.y;
1714 vars[head].sw = rect.size.width;
1715 vars[head].sh = rect.size.height;
1716 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1717 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1718 vars[head].sw = isize * (vars[head].sw / isize);
1719 vars[head].sh = isize * (vars[head].sh / isize);
1720 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1721 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1723 /* icon yard boundaries */
1724 if (wPreferences.icon_yard & IY_VERT) {
1725 vars[head].pf = vars[head].fullH;
1726 vars[head].sf = vars[head].fullW;
1727 } else {
1728 vars[head].pf = vars[head].fullW;
1729 vars[head].sf = vars[head].fullH;
1731 if (wPreferences.icon_yard & IY_RIGHT) {
1732 vars[head].xo = vars[head].sx2 - isize;
1733 vars[head].xs = -1;
1734 } else {
1735 vars[head].xo = vars[head].sx1;
1736 vars[head].xs = 1;
1738 if (wPreferences.icon_yard & IY_TOP) {
1739 vars[head].yo = vars[head].sy1;
1740 vars[head].ys = 1;
1741 } else {
1742 vars[head].yo = vars[head].sy2 - isize;
1743 vars[head].ys = -1;
1747 #define X ((wPreferences.icon_yard & IY_VERT) \
1748 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1749 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1751 #define Y ((wPreferences.icon_yard & IY_VERT) \
1752 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1753 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1755 /* arrange application icons */
1756 aicon = scr->app_icon_list;
1757 /* reverse them to avoid unnecessarily sliding of icons */
1758 while (aicon && aicon->next)
1759 aicon = aicon->next;
1761 while (aicon) {
1762 if (!aicon->docked) {
1763 /* CHECK: can icon be NULL here ? */
1764 /* The intention here is to place the AppIcon on the head that
1765 * contains most of the applications _main_ window. */
1766 head = wGetHeadForWindow(aicon->icon->owner);
1768 if (aicon->x_pos != X || aicon->y_pos != Y) {
1769 #ifdef ANIMATIONS
1770 if (!wPreferences.no_animations) {
1771 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1773 #endif /* ANIMATIONS */
1775 wAppIconMove(aicon, X, Y);
1776 vars[head].pi++;
1777 if (vars[head].pi >= vars[head].pf) {
1778 vars[head].pi = 0;
1779 vars[head].si++;
1782 aicon = aicon->prev;
1785 /* arrange miniwindows */
1786 wwin = scr->focused_window;
1787 /* reverse them to avoid unnecessarily shuffling */
1788 while (wwin && wwin->prev)
1789 wwin = wwin->prev;
1791 while (wwin) {
1792 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1793 (wwin->frame->workspace == scr->current_workspace ||
1794 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1796 head = wGetHeadForWindow(wwin);
1798 if (arrangeAll || !wwin->flags.icon_moved) {
1799 if (wwin->icon_x != X || wwin->icon_y != Y) {
1800 #ifdef ANIMATIONS
1801 if (wPreferences.no_animations) {
1802 XMoveWindow(dpy, wwin->icon->core->window, X, Y);
1803 } else {
1804 SlideWindow(wwin->icon->core->window, wwin->icon_x,
1805 wwin->icon_y, X, Y);
1807 #else
1808 XMoveWindow(dpy, wwin->icon->core->window, X, Y);
1809 #endif /* ANIMATIONS */
1811 wwin->icon_x = X;
1812 wwin->icon_y = Y;
1814 vars[head].pi++;
1815 if (vars[head].pi >= vars[head].pf) {
1816 vars[head].pi = 0;
1817 vars[head].si++;
1821 if (arrangeAll) {
1822 wwin->flags.icon_moved = 0;
1824 /* we reversed the order, so we use next */
1825 wwin = wwin->next;
1828 wfree(vars);
1831 void wSelectWindow(WWindow *wwin, Bool flag)
1833 WScreen *scr = wwin->screen_ptr;
1835 if (flag) {
1836 wwin->flags.selected = 1;
1837 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1839 if (!HAS_BORDER(wwin)) {
1840 XSetWindowBorderWidth(dpy, wwin->frame->core->window, FRAME_BORDER_WIDTH);
1843 if (!scr->selected_windows)
1844 scr->selected_windows = WMCreateArray(4);
1845 WMAddToArray(scr->selected_windows, wwin);
1846 } else {
1847 wwin->flags.selected = 0;
1848 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1850 if (!HAS_BORDER(wwin)) {
1851 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1854 if (scr->selected_windows) {
1855 WMRemoveFromArray(scr->selected_windows, wwin);
1860 void wMakeWindowVisible(WWindow *wwin)
1862 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1863 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1865 if (wwin->flags.shaded) {
1866 wUnshadeWindow(wwin);
1868 if (wwin->flags.hidden) {
1869 WApplication *app;
1871 app = wApplicationOf(wwin->main_window);
1872 if (app) {
1873 /* trick to get focus to this window */
1874 app->last_focused = wwin;
1875 wUnhideApplication(app, False, False);
1878 if (wwin->flags.miniaturized) {
1879 wDeiconifyWindow(wwin);
1880 } else {
1881 if (!WFLAGP(wwin, no_focusable))
1882 wSetFocusTo(wwin->screen_ptr, wwin);
1883 wRaiseFrame(wwin->frame->core);
1888 * Do the animation while shading (called with what = SHADE)
1889 * or unshading (what = UNSHADE).
1891 #ifdef ANIMATIONS
1892 static void shade_animate(WWindow *wwin, Bool what)
1894 int y, s, w, h;
1895 time_t time0 = time(NULL);
1897 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1898 return;
1900 switch(what) {
1901 case SHADE:
1902 if (!wwin->screen_ptr->flags.startup) {
1903 /* do the shading animation */
1904 h = wwin->frame->core->height;
1905 s = h / SHADE_STEPS;
1906 if (s < 1)
1907 s = 1;
1908 w = wwin->frame->core->width;
1909 y = wwin->frame->top_width;
1910 while (h > wwin->frame->top_width + 1) {
1911 XMoveWindow(dpy, wwin->client_win, 0, y);
1912 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1913 XFlush(dpy);
1915 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1916 break;
1918 if (SHADE_DELAY > 0) {
1919 wusleep(SHADE_DELAY * 1000L);
1920 } else {
1921 wusleep(10);
1923 h -= s;
1924 y -= s;
1926 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1928 break;
1930 case UNSHADE:
1931 h = wwin->frame->top_width + wwin->frame->bottom_width;
1932 y = wwin->frame->top_width - wwin->client.height;
1933 s = abs(y) / SHADE_STEPS;
1934 if (s < 1)
1935 s = 1;
1936 w = wwin->frame->core->width;
1937 XMoveWindow(dpy, wwin->client_win, 0, y);
1938 if (s > 0) {
1939 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
1940 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1941 XMoveWindow(dpy, wwin->client_win, 0, y);
1942 XFlush(dpy);
1943 if (SHADE_DELAY > 0) {
1944 wusleep(SHADE_DELAY * 2000L / 3);
1945 } else {
1946 wusleep(10);
1948 h += s;
1949 y += s;
1951 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1952 break;
1955 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1956 break;
1959 #endif