Avoid create again a docked application cache icon
[wmaker-crm.git] / src / actions.c
blob5adfab49d6eb21b463154040508ef4426f06aeb2
1 /* action.c- misc. window commands (miniaturize, hide etc.)
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
6 * Copyright (c) 1998-2003 Dan Pascu
7 * Copyright (c) 2014 Window Maker Team
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "wconfig.h"
26 #include <X11/Xlib.h>
27 #include <X11/Xutil.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <math.h>
32 #include <time.h>
34 #include "WindowMaker.h"
35 #include "framewin.h"
36 #include "window.h"
37 #include "client.h"
38 #include "icon.h"
39 #include "colormap.h"
40 #include "application.h"
41 #include "actions.h"
42 #include "stacking.h"
43 #include "appicon.h"
44 #include "dock.h"
45 #include "appmenu.h"
46 #include "winspector.h"
47 #include "workspace.h"
48 #include "xinerama.h"
49 #include "usermenu.h"
50 #include "placement.h"
51 #include "misc.h"
52 #include "event.h"
55 #ifndef HAVE_FLOAT_MATHFUNC
56 #define sinf(x) ((float)sin((double)(x)))
57 #define cosf(x) ((float)cos((double)(x)))
58 #define sqrtf(x) ((float)sqrt((double)(x)))
59 #define atan2f(y, x) ((float)atan((double)(y) / (double)(x)))
60 #endif
62 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
63 unsigned int *new_width, unsigned int *new_height);
64 static void save_old_geometry(WWindow *wwin, int directions);
66 /******* Local Variables *******/
67 #ifdef USE_ANIMATIONS
68 static struct {
69 int steps;
70 int delay;
71 } shadePars[5] = {
72 { SHADE_STEPS_UF, SHADE_DELAY_UF },
73 { SHADE_STEPS_F, SHADE_DELAY_F },
74 { SHADE_STEPS_M, SHADE_DELAY_M },
75 { SHADE_STEPS_S, SHADE_DELAY_S },
76 { SHADE_STEPS_US, SHADE_DELAY_US }
79 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
80 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
81 #endif
83 #define UNSHADE 0
84 #define SHADE 1
86 static int compareTimes(Time t1, Time t2)
88 Time diff;
89 if (t1 == t2)
90 return 0;
91 diff = t1 - t2;
92 return (diff < 60000) ? 1 : -1;
95 #ifdef USE_ANIMATIONS
96 static void shade_animate(WWindow *wwin, Bool what);
97 #else
98 static inline void shade_animate(WWindow *wwin, Bool what)
101 * This function is empty on purpose, so tell the compiler
102 * to not warn about parameters being not used
104 (void) wwin;
105 (void) what;
107 #endif
110 *----------------------------------------------------------------------
111 * wSetFocusTo--
112 * Changes the window focus to the one passed as argument.
113 * If the window to focus is not already focused, it will be brought
114 * to the head of the list of windows. Previously focused window is
115 * unfocused.
117 * Side effects:
118 * Window list may be reordered and the window focus is changed.
120 *----------------------------------------------------------------------
122 void wSetFocusTo(WScreen *scr, WWindow *wwin)
124 static WScreen *old_scr = NULL;
126 WWindow *old_focused;
127 WWindow *focused = scr->focused_window;
128 Time timestamp = w_global.timestamp.last_event;
129 WApplication *oapp = NULL, *napp = NULL;
130 int wasfocused;
132 if (scr->flags.ignore_focus_events || compareTimes(w_global.timestamp.focus_change, timestamp) > 0)
133 return;
135 if (!old_scr)
136 old_scr = scr;
138 old_focused = old_scr->focused_window;
140 w_global.timestamp.focus_change = timestamp;
142 if (old_focused)
143 oapp = wApplicationOf(old_focused->main_window);
145 if (wwin == NULL) {
146 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
147 if (old_focused)
148 wWindowUnfocus(old_focused);
150 if (oapp) {
151 wAppMenuUnmap(oapp->menu);
152 if (wPreferences.highlight_active_app)
153 wApplicationDeactivate(oapp);
156 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
157 return;
160 if (old_scr != scr && old_focused)
161 wWindowUnfocus(old_focused);
163 wasfocused = wwin->flags.focused;
164 napp = wApplicationOf(wwin->main_window);
166 /* remember last workspace where the app has been */
167 if (napp)
168 napp->last_workspace = wwin->frame->workspace;
170 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
171 /* install colormap if colormap mode is lock mode */
172 if (wPreferences.colormap_mode == WCM_CLICK)
173 wColormapInstallForWindow(scr, wwin);
175 /* set input focus */
176 switch (wwin->focus_mode) {
177 case WFM_NO_INPUT:
178 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
179 break;
180 case WFM_PASSIVE:
181 case WFM_LOCALLY_ACTIVE:
182 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
183 break;
184 case WFM_GLOBALLY_ACTIVE:
185 break;
188 XFlush(dpy);
189 if (wwin->protocols.TAKE_FOCUS)
190 wClientSendProtocol(wwin, w_global.atom.wm.take_focus, timestamp);
192 XSync(dpy, False);
193 } else {
194 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
197 if (WFLAGP(wwin, no_focusable))
198 return;
200 /* if this is not the focused window focus it */
201 if (focused != wwin) {
202 /* change the focus window list order */
203 if (wwin->prev)
204 wwin->prev->next = wwin->next;
206 if (wwin->next)
207 wwin->next->prev = wwin->prev;
209 wwin->prev = focused;
210 focused->next = wwin;
211 wwin->next = NULL;
212 scr->focused_window = wwin;
214 if (oapp && oapp != napp) {
215 wAppMenuUnmap(oapp->menu);
216 if (wPreferences.highlight_active_app)
217 wApplicationDeactivate(oapp);
221 wWindowFocus(wwin, focused);
223 if (napp && !wasfocused) {
224 #ifdef USER_MENU
225 wUserMenuRefreshInstances(napp->menu, wwin);
226 #endif /* USER_MENU */
228 if (wwin->flags.mapped)
229 wAppMenuMap(napp->menu, wwin);
231 if (napp && wPreferences.highlight_active_app)
232 wApplicationActivate(napp);
234 XFlush(dpy);
235 old_scr = scr;
238 void wShadeWindow(WWindow *wwin)
241 if (wwin->flags.shaded)
242 return;
244 XLowerWindow(dpy, wwin->client_win);
245 shade_animate(wwin, SHADE);
247 wwin->flags.skip_next_animation = 0;
248 wwin->flags.shaded = 1;
249 wwin->flags.mapped = 0;
250 /* prevent window withdrawal when getting UnmapNotify */
251 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
252 XUnmapWindow(dpy, wwin->client_win);
253 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
255 /* for the client it's just like iconification */
256 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
258 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
259 wWindowSynthConfigureNotify(wwin);
262 wClientSetState(wwin, IconicState, None);
265 WMPostNotificationName(WMNChangedState, wwin, "shade");
267 #ifdef USE_ANIMATIONS
268 if (!wwin->screen_ptr->flags.startup) {
269 /* Catch up with events not processed while animation was running */
270 ProcessPendingEvents();
272 #endif
275 void wUnshadeWindow(WWindow *wwin)
278 if (!wwin->flags.shaded)
279 return;
281 wwin->flags.shaded = 0;
282 wwin->flags.mapped = 1;
283 XMapWindow(dpy, wwin->client_win);
285 shade_animate(wwin, UNSHADE);
287 wwin->flags.skip_next_animation = 0;
288 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
289 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
291 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
292 wWindowSynthConfigureNotify(wwin);
294 /* if the window is focused, set the focus again as it was disabled during
295 * shading */
296 if (wwin->flags.focused)
297 wSetFocusTo(wwin->screen_ptr, wwin);
299 WMPostNotificationName(WMNChangedState, wwin, "shade");
302 /* Set the old coordinates using the current values */
303 static void save_old_geometry(WWindow *wwin, int directions)
305 /* never been saved? */
306 if (!wwin->old_geometry.width)
307 directions |= SAVE_GEOMETRY_X | SAVE_GEOMETRY_WIDTH;
308 if (!wwin->old_geometry.height)
309 directions |= SAVE_GEOMETRY_Y | SAVE_GEOMETRY_HEIGHT;
311 if (directions & SAVE_GEOMETRY_X)
312 wwin->old_geometry.x = wwin->frame_x;
313 if (directions & SAVE_GEOMETRY_Y)
314 wwin->old_geometry.y = wwin->frame_y;
315 if (directions & SAVE_GEOMETRY_WIDTH)
316 wwin->old_geometry.width = wwin->client.width;
317 if (directions & SAVE_GEOMETRY_HEIGHT)
318 wwin->old_geometry.height = wwin->client.height;
321 static void remember_geometry(WWindow *wwin, int *x, int *y, int *w, int *h)
323 WMRect old_geom_rect;
324 int old_head;
325 Bool same_head;
327 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
328 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
329 same_head = (wGetHeadForWindow(wwin) == old_head);
330 *x = ((wwin->old_geometry.x || wwin->old_geometry.width) && same_head) ? wwin->old_geometry.x : wwin->frame_x;
331 *y = ((wwin->old_geometry.y || wwin->old_geometry.height) && same_head) ? wwin->old_geometry.y : wwin->frame_y;
332 *w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
333 *h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
336 /* Remember geometry for unmaximizing */
337 void update_saved_geometry(WWindow *wwin)
339 /* NOT if we aren't already maximized
340 * we'll save geometry when maximizing */
341 if (!wwin->flags.maximized)
342 return;
344 /* NOT if we are fully maximized */
345 if ((wwin->flags.maximized & MAX_MAXIMUS) ||
346 ((wwin->flags.maximized & MAX_HORIZONTAL) &&
347 (wwin->flags.maximized & MAX_VERTICAL)))
348 return;
350 /* save the co-ordinate in the axis in which we AREN'T maximized */
351 if (wwin->flags.maximized & MAX_HORIZONTAL)
352 save_old_geometry(wwin, SAVE_GEOMETRY_Y);
353 if (wwin->flags.maximized & MAX_VERTICAL)
354 save_old_geometry(wwin, SAVE_GEOMETRY_X);
357 void wMaximizeWindow(WWindow *wwin, int directions)
359 unsigned int new_width, new_height, half_scr_width, half_scr_height;
360 int new_x = 0;
361 int new_y = 0;
362 int maximus_x = 0;
363 int maximus_y = 0;
364 unsigned int maximus_width = 0;
365 unsigned int maximus_height = 0;
366 WArea usableArea, totalArea;
367 Bool has_border = 1;
368 int adj_size;
369 WScreen *scr = wwin->screen_ptr;
371 if (!IS_RESIZABLE(wwin))
372 return;
374 if (!HAS_BORDER(wwin))
375 has_border = 0;
377 if (wPreferences.drag_maximized_window == DRAGMAX_NOMOVE)
378 wwin->client_flags.no_movable = 1;
380 /* the size to adjust the geometry */
381 adj_size = scr->frame_border_width * 2 * has_border;
383 /* save old coordinates before we change the current values */
384 if (!wwin->flags.maximized)
385 save_old_geometry(wwin, SAVE_GEOMETRY_ALL);
387 totalArea.x2 = scr->scr_width;
388 totalArea.y2 = scr->scr_height;
389 totalArea.x1 = 0;
390 totalArea.y1 = 0;
391 usableArea = totalArea;
393 if (!(directions & MAX_IGNORE_XINERAMA)) {
394 WScreen *scr = wwin->screen_ptr;
395 int head;
397 if (directions & MAX_KEYBOARD)
398 head = wGetHeadForWindow(wwin);
399 else
400 head = wGetHeadForPointerLocation(scr);
402 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
406 /* Only save directions, not kbd or xinerama hints */
407 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
409 if (WFLAGP(wwin, full_maximize))
410 usableArea = totalArea;
411 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
412 half_scr_height = (usableArea.y2 - usableArea.y1)/2;
414 if (wwin->flags.shaded) {
415 wwin->flags.skip_next_animation = 1;
416 wUnshadeWindow(wwin);
419 if (directions & MAX_MAXIMUS) {
420 find_Maximus_geometry(wwin, usableArea, &maximus_x, &maximus_y, &maximus_width, &maximus_height);
421 new_width = maximus_width - adj_size;
422 new_height = maximus_height - adj_size;
423 new_x = maximus_x;
424 new_y = maximus_y;
425 if (WFLAGP(wwin, full_maximize) && (new_y == 0)) {
426 new_height += wwin->frame->bottom_width - 1;
427 new_y -= wwin->frame->top_width;
430 wwin->maximus_x = new_x;
431 wwin->maximus_y = new_y;
432 wwin->flags.old_maximized |= MAX_MAXIMUS;
433 } else {
434 /* set default values if no option set then */
435 if (!(directions & (MAX_HORIZONTAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS))) {
436 new_width = (wwin->old_geometry.width) ? wwin->old_geometry.width : wwin->frame->core->width;
437 new_x = (wwin->old_geometry.x) ? wwin->old_geometry.x : wwin->frame_x;
439 if (!(directions & (MAX_VERTICAL | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS))) {
440 new_height = (wwin->old_geometry.height) ? wwin->old_geometry.height : wwin->frame->core->height;
441 new_y = (wwin->old_geometry.y) ? wwin->old_geometry.y : wwin->frame_y;
444 /* left|right position */
445 if (directions & MAX_LEFTHALF) {
446 new_width = half_scr_width - adj_size;
447 new_x = usableArea.x1;
448 } else if (directions & MAX_RIGHTHALF) {
449 new_width = half_scr_width - adj_size;
450 new_x = usableArea.x1 + half_scr_width;
452 /* top|bottom position */
453 if (directions & MAX_TOPHALF) {
454 new_height = half_scr_height - adj_size;
455 new_y = usableArea.y1;
456 } else if (directions & MAX_BOTTOMHALF) {
457 new_height = half_scr_height - adj_size;
458 new_y = usableArea.y1 + half_scr_height;
461 /* vertical|horizontal position */
462 if (directions & MAX_HORIZONTAL) {
463 new_width = usableArea.x2 - usableArea.x1 - adj_size;
464 new_x = usableArea.x1;
466 if (directions & MAX_VERTICAL) {
467 new_height = usableArea.y2 - usableArea.y1 - adj_size;
468 new_y = usableArea.y1;
469 if (WFLAGP(wwin, full_maximize) && (new_y == 0))
470 new_y -= wwin->frame->top_width;
474 if (!WFLAGP(wwin, full_maximize) && !(directions == MAX_MAXIMUS || directions == MAX_HORIZONTAL))
475 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
477 /* set maximization state */
478 wwin->flags.maximized = directions;
479 if ((wwin->flags.old_maximized & MAX_MAXIMUS) && !wwin->flags.maximized)
480 wwin->flags.maximized = MAX_MAXIMUS;
482 wWindowConstrainSize(wwin, &new_width, &new_height);
484 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
485 usableArea.y2 - usableArea.y1, &new_width, &new_height);
487 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
488 wWindowSynthConfigureNotify(wwin);
490 WMPostNotificationName(WMNChangedState, wwin, "maximize");
493 /* generic (un)maximizer */
494 void handleMaximize(WWindow *wwin, int directions)
496 int current = wwin->flags.maximized;
497 int requested = directions & (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_TOPHALF | MAX_BOTTOMHALF | MAX_MAXIMUS);
498 int effective = requested ^ current;
499 int flags = directions & ~requested;
501 if (!effective) {
502 /* allow wMaximizeWindow to restore the Maximusized size */
503 if ((wwin->flags.old_maximized & MAX_MAXIMUS) &&
504 !(requested & MAX_MAXIMUS))
505 wMaximizeWindow(wwin, MAX_MAXIMUS | flags);
506 else
507 wUnmaximizeWindow(wwin);
508 /* these alone mean vertical|horizontal toggle */
509 } else if ((effective == MAX_LEFTHALF) ||
510 (effective == MAX_RIGHTHALF) ||
511 (effective == MAX_TOPHALF) ||
512 (effective == MAX_BOTTOMHALF))
513 wUnmaximizeWindow(wwin);
514 else {
515 if ((requested == (MAX_HORIZONTAL | MAX_VERTICAL)) ||
516 (requested == MAX_MAXIMUS))
517 effective = requested;
518 else {
519 if (requested & MAX_LEFTHALF) {
520 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
521 effective |= MAX_VERTICAL;
522 else
523 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
524 effective |= MAX_LEFTHALF;
525 effective &= ~(MAX_HORIZONTAL | MAX_RIGHTHALF);
526 } else if (requested & MAX_RIGHTHALF) {
527 if (!(requested & (MAX_TOPHALF | MAX_BOTTOMHALF)))
528 effective |= MAX_VERTICAL;
529 else
530 effective |= requested & (MAX_TOPHALF | MAX_BOTTOMHALF);
531 effective |= MAX_RIGHTHALF;
532 effective &= ~(MAX_HORIZONTAL | MAX_LEFTHALF);
534 if (requested & MAX_TOPHALF) {
535 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
536 effective |= MAX_HORIZONTAL;
537 else
538 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
539 effective |= MAX_TOPHALF;
540 effective &= ~(MAX_VERTICAL | MAX_BOTTOMHALF);
541 } else if (requested & MAX_BOTTOMHALF) {
542 if (!(requested & (MAX_LEFTHALF | MAX_RIGHTHALF)))
543 effective |= MAX_HORIZONTAL;
544 else
545 effective |= requested & (MAX_LEFTHALF | MAX_RIGHTHALF);
546 effective |= MAX_BOTTOMHALF;
547 effective &= ~(MAX_VERTICAL | MAX_TOPHALF);
549 if (requested & MAX_HORIZONTAL)
550 effective &= ~(MAX_LEFTHALF | MAX_RIGHTHALF);
551 if (requested & MAX_VERTICAL)
552 effective &= ~(MAX_TOPHALF | MAX_BOTTOMHALF);
553 effective &= ~MAX_MAXIMUS;
555 wMaximizeWindow(wwin, effective | flags);
559 /* the window boundary coordinates */
560 typedef struct {
561 int left;
562 int right;
563 int bottom;
564 int top;
565 int width;
566 int height;
567 } win_coords;
569 static void set_window_coords(WWindow *wwin, win_coords *obs)
571 obs->left = wwin->frame_x;
572 obs->top = wwin->frame_y;
573 obs->width = wwin->frame->core->width;
574 obs->height = wwin->frame->core->height;
575 obs->bottom = obs->top + obs->height;
576 obs->right = obs->left + obs->width;
580 * Maximus: tiled maximization (maximize without overlapping other windows)
582 * The original window 'orig' will be maximized to new coordinates 'new'.
583 * The windows obstructing the maximization of 'orig' are denoted 'obs'.
585 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
586 unsigned int *new_width, unsigned int *new_height)
588 WWindow *tmp;
589 short int tbar_height_0 = 0, rbar_height_0 = 0, bd_width_0 = 0;
590 short int adjust_height;
591 int x_intsect, y_intsect;
592 /* the obstructing, original and new windows */
593 win_coords obs, orig, new;
595 /* set the original coordinate positions of the window to be Maximumized */
596 if (wwin->flags.maximized) {
597 /* window is already maximized; consider original geometry */
598 remember_geometry(wwin, &orig.left, &orig.top, &orig.width, &orig.height);
599 orig.bottom = orig.top + orig.height;
600 orig.right = orig.left + orig.width;
601 } else
602 set_window_coords(wwin, &orig);
604 /* Try to fully maximize first, then readjust later */
605 new.left = usableArea.x1;
606 new.right = usableArea.x2;
607 new.top = usableArea.y1;
608 new.bottom = usableArea.y2;
610 if (HAS_TITLEBAR(wwin))
611 tbar_height_0 = TITLEBAR_HEIGHT;
612 if (HAS_RESIZEBAR(wwin))
613 rbar_height_0 = RESIZEBAR_HEIGHT;
614 if (HAS_BORDER(wwin))
615 bd_width_0 = wwin->screen_ptr->frame_border_width;
617 /* the length to be subtracted if the window has titlebar, etc */
618 adjust_height = tbar_height_0 + 2 * bd_width_0 + rbar_height_0;
620 tmp = wwin;
621 /* The focused window is always the last in the list */
622 while (tmp->prev) {
623 /* ignore windows in other workspaces etc */
624 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
625 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
626 tmp = tmp->prev;
627 continue;
629 tmp = tmp->prev;
631 /* Set the coordinates of obstructing window */
632 set_window_coords(tmp, &obs);
634 /* Try to maximize in the y direction first */
635 x_intsect = calcIntersectionLength(orig.left, orig.width, obs.left, obs.width);
636 if (x_intsect != 0) {
637 /* TODO: Consider the case when coords are equal */
638 if (obs.bottom < orig.top && obs.bottom > new.top) {
639 /* w_0 is below the bottom of w_j */
640 new.top = obs.bottom + 1;
642 if (orig.bottom < obs.top && obs.top < new.bottom) {
643 /* The bottom of w_0 is above the top of w_j */
644 new.bottom = obs.top - 1;
649 tmp = wwin;
650 while (tmp->prev) {
651 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
652 || tmp->prev->flags.miniaturized || tmp->prev->flags.hidden) {
653 tmp = tmp->prev;
654 continue;
656 tmp = tmp->prev;
658 set_window_coords(tmp, &obs);
661 * Use the new.top and new.height instead of original values
662 * as they may have different intersections with the obstructing windows
664 new.height = new.bottom - new.top - adjust_height;
665 y_intsect = calcIntersectionLength(new.top, new.height, obs.top, obs.height);
666 if (y_intsect != 0) {
667 if (obs.right < orig.left && obs.right > new.left) {
668 /* w_0 is completely to the right of w_j */
669 new.left = obs.right + 1;
671 if (orig.right < obs.left && obs.left < new.right) {
672 /* w_0 is completely to the left of w_j */
673 new.right = obs.left - 1;
678 *new_x = new.left;
679 *new_y = new.top;
680 /* xcalc needs -7 here, but other apps don't */
681 *new_height = new.bottom - new.top - adjust_height - 1;
682 *new_width = new.right - new.left;
685 void wUnmaximizeWindow(WWindow *wwin)
687 int x, y, w, h;
689 if (!wwin->flags.maximized)
690 return;
692 if (wwin->flags.shaded) {
693 wwin->flags.skip_next_animation = 1;
694 wUnshadeWindow(wwin);
697 if (wPreferences.drag_maximized_window == DRAGMAX_NOMOVE)
698 wwin->client_flags.no_movable = 0;
700 /* Use old coordinates if they are set, current values otherwise */
701 remember_geometry(wwin, &x, &y, &w, &h);
703 /* unMaximusize relative to original position */
704 if (wwin->flags.maximized & MAX_MAXIMUS) {
705 x += wwin->frame_x - wwin->maximus_x;
706 y += wwin->frame_y - wwin->maximus_y;
709 wwin->flags.maximized = 0;
710 wwin->flags.old_maximized = 0;
711 wWindowConfigure(wwin, x, y, w, h);
712 wWindowSynthConfigureNotify(wwin);
714 WMPostNotificationName(WMNChangedState, wwin, "maximize");
717 void wFullscreenWindow(WWindow *wwin)
719 int head;
720 WMRect rect;
722 if (wwin->flags.fullscreen)
723 return;
725 wwin->flags.fullscreen = True;
727 wWindowConfigureBorders(wwin);
729 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
730 wRaiseFrame(wwin->frame->core);
732 wwin->bfs_geometry.x = wwin->frame_x;
733 wwin->bfs_geometry.y = wwin->frame_y;
734 wwin->bfs_geometry.width = wwin->frame->core->width;
735 wwin->bfs_geometry.height = wwin->frame->core->height;
737 head = wGetHeadForWindow(wwin);
738 rect = wGetRectForHead(wwin->screen_ptr, head);
739 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
741 wwin->screen_ptr->bfs_focused_window = wwin->screen_ptr->focused_window;
742 wSetFocusTo(wwin->screen_ptr, wwin);
744 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
747 void wUnfullscreenWindow(WWindow *wwin)
749 if (!wwin->flags.fullscreen)
750 return;
752 wwin->flags.fullscreen = False;
754 if (WFLAGP(wwin, sunken))
755 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
756 else if (WFLAGP(wwin, floating))
757 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
759 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
760 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
762 wWindowConfigureBorders(wwin);
764 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
765 wFrameWindowPaint(wwin->frame);
768 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
770 if (wwin->screen_ptr->bfs_focused_window) {
771 wSetFocusTo(wwin->screen_ptr, wwin->screen_ptr->bfs_focused_window);
772 wwin->screen_ptr->bfs_focused_window = NULL;
776 #ifdef USE_ANIMATIONS
777 static void animateResizeFlip(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
779 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
780 float cx, cy, cw, ch;
781 float xstep, ystep, wstep, hstep;
782 XPoint points[5];
783 float dx, dch, midy;
784 float angle, final_angle, delta;
786 xstep = (float)(fx - x) / steps;
787 ystep = (float)(fy - y) / steps;
788 wstep = (float)(fw - w) / steps;
789 hstep = (float)(fh - h) / steps;
791 cx = (float)x;
792 cy = (float)y;
793 cw = (float)w;
794 ch = (float)h;
796 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
797 delta = (float)(final_angle / FRAMES);
798 for (angle = 0;; angle += delta) {
799 if (angle > final_angle)
800 angle = final_angle;
802 dx = (cw / 10) - ((cw / 5) * sinf(angle));
803 dch = (ch / 2) * cosf(angle);
804 midy = cy + (ch / 2);
806 points[0].x = cx + dx;
807 points[0].y = midy - dch;
808 points[1].x = cx + cw - dx;
809 points[1].y = points[0].y;
810 points[2].x = cx + cw + dx;
811 points[2].y = midy + dch;
812 points[3].x = cx - dx;
813 points[3].y = points[2].y;
814 points[4].x = points[0].x;
815 points[4].y = points[0].y;
817 XGrabServer(dpy);
818 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
819 XFlush(dpy);
820 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
822 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
823 XUngrabServer(dpy);
824 cx += xstep;
825 cy += ystep;
826 cw += wstep;
827 ch += hstep;
828 if (angle >= final_angle)
829 break;
832 XFlush(dpy);
835 #undef FRAMES
837 static void
838 animateResizeTwist(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
840 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
841 float cx, cy, cw, ch;
842 float xstep, ystep, wstep, hstep;
843 XPoint points[5];
844 float angle, final_angle, a, d, delta;
846 x += w / 2;
847 y += h / 2;
848 fx += fw / 2;
849 fy += fh / 2;
851 xstep = (float)(fx - x) / steps;
852 ystep = (float)(fy - y) / steps;
853 wstep = (float)(fw - w) / steps;
854 hstep = (float)(fh - h) / steps;
856 cx = (float)x;
857 cy = (float)y;
858 cw = (float)w;
859 ch = (float)h;
861 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
862 delta = (float)(final_angle / FRAMES);
863 for (angle = 0;; angle += delta) {
864 if (angle > final_angle)
865 angle = final_angle;
867 a = atan2f(ch, cw);
868 d = sqrtf((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
870 points[0].x = cx + cosf(angle - a) * d;
871 points[0].y = cy + sinf(angle - a) * d;
872 points[1].x = cx + cosf(angle + a) * d;
873 points[1].y = cy + sinf(angle + a) * d;
874 points[2].x = cx + cosf(angle - a + (float)WM_PI) * d;
875 points[2].y = cy + sinf(angle - a + (float)WM_PI) * d;
876 points[3].x = cx + cosf(angle + a + (float)WM_PI) * d;
877 points[3].y = cy + sinf(angle + a + (float)WM_PI) * d;
878 points[4].x = cx + cosf(angle - a) * d;
879 points[4].y = cy + sinf(angle - a) * d;
880 XGrabServer(dpy);
881 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
882 XFlush(dpy);
883 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
885 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
886 XUngrabServer(dpy);
887 cx += xstep;
888 cy += ystep;
889 cw += wstep;
890 ch += hstep;
891 if (angle >= final_angle)
892 break;
895 XFlush(dpy);
898 #undef FRAMES
900 static void animateResizeZoom(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
902 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
903 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
904 float xstep, ystep, wstep, hstep;
905 int i, j;
907 xstep = (float)(fx - x) / steps;
908 ystep = (float)(fy - y) / steps;
909 wstep = (float)(fw - w) / steps;
910 hstep = (float)(fh - h) / steps;
912 for (j = 0; j < FRAMES; j++) {
913 cx[j] = (float)x;
914 cy[j] = (float)y;
915 cw[j] = (float)w;
916 ch[j] = (float)h;
918 XGrabServer(dpy);
919 for (i = 0; i < steps; i++) {
920 for (j = 0; j < FRAMES; j++) {
921 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
922 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
924 XFlush(dpy);
925 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
927 for (j = 0; j < FRAMES; j++) {
928 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
929 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
930 if (j < FRAMES - 1) {
931 cx[j] = cx[j + 1];
932 cy[j] = cy[j + 1];
933 cw[j] = cw[j + 1];
934 ch[j] = ch[j + 1];
935 } else {
936 cx[j] += xstep;
937 cy[j] += ystep;
938 cw[j] += wstep;
939 ch[j] += hstep;
944 for (j = 0; j < FRAMES; j++)
945 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
946 XFlush(dpy);
947 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
949 for (j = 0; j < FRAMES; j++)
950 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
952 XUngrabServer(dpy);
955 #undef FRAMES
957 void animateResize(WScreen *scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh)
959 int style = wPreferences.iconification_style; /* Catch the value */
960 int steps;
962 if (style == WIS_NONE)
963 return;
965 if (style == WIS_RANDOM)
966 style = rand() % 3;
968 switch (style) {
969 case WIS_TWIST:
970 steps = MINIATURIZE_ANIMATION_STEPS_T;
971 if (steps > 0)
972 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
973 break;
974 case WIS_FLIP:
975 steps = MINIATURIZE_ANIMATION_STEPS_F;
976 if (steps > 0)
977 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
978 break;
979 case WIS_ZOOM:
980 default:
981 steps = MINIATURIZE_ANIMATION_STEPS_Z;
982 if (steps > 0)
983 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
984 break;
987 #endif /* USE_ANIMATIONS */
989 static void flushExpose(void)
991 XEvent tmpev;
993 while (XCheckTypedEvent(dpy, Expose, &tmpev))
994 WMHandleEvent(&tmpev);
995 XSync(dpy, 0);
998 static void unmapTransientsFor(WWindow *wwin)
1000 WWindow *tmp;
1002 tmp = wwin->screen_ptr->focused_window;
1003 while (tmp) {
1004 /* unmap the transients for this transient */
1005 if (tmp != wwin && tmp->transient_for == wwin->client_win
1006 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
1007 unmapTransientsFor(tmp);
1008 tmp->flags.miniaturized = 1;
1009 if (!tmp->flags.shaded)
1010 wWindowUnmap(tmp);
1011 else
1012 XUnmapWindow(dpy, tmp->frame->core->window);
1014 if (!tmp->flags.shaded)
1016 wClientSetState(tmp, IconicState, None);
1018 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1020 tmp = tmp->prev;
1024 static void mapTransientsFor(WWindow *wwin)
1026 WWindow *tmp;
1028 tmp = wwin->screen_ptr->focused_window;
1029 while (tmp) {
1030 /* recursively map the transients for this transient */
1031 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
1032 && tmp->icon == NULL) {
1033 mapTransientsFor(tmp);
1034 tmp->flags.miniaturized = 0;
1035 if (!tmp->flags.shaded)
1036 wWindowMap(tmp);
1037 else
1038 XMapWindow(dpy, tmp->frame->core->window);
1039 tmp->flags.semi_focused = 0;
1041 if (!tmp->flags.shaded)
1043 wClientSetState(tmp, NormalState, None);
1045 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
1047 tmp = tmp->prev;
1051 static WWindow *recursiveTransientFor(WWindow *wwin)
1053 int i;
1055 if (!wwin)
1056 return None;
1058 /* hackish way to detect transient_for cycle */
1059 i = wwin->screen_ptr->window_count + 1;
1061 while (wwin && wwin->transient_for != None && i > 0) {
1062 wwin = wWindowFor(wwin->transient_for);
1063 i--;
1065 if (i == 0 && wwin) {
1066 wwarning(_("window \"%s\" has a severely broken WM_TRANSIENT_FOR hint"),
1067 wwin->frame->title);
1068 return NULL;
1071 return wwin;
1074 #ifdef USE_ANIMATIONS
1075 static int getAnimationGeometry(WWindow *wwin, int *ix, int *iy, int *iw, int *ih)
1077 if (wwin->screen_ptr->flags.startup || wPreferences.no_animations
1078 || wwin->flags.skip_next_animation || wwin->icon == NULL)
1079 return 0;
1081 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1082 *ix = wwin->icon_x;
1083 *iy = wwin->icon_y;
1084 *iw = wwin->icon->core->width;
1085 *ih = wwin->icon->core->height;
1086 } else {
1087 if (wwin->flags.net_handle_icon) {
1088 *ix = wwin->icon_x;
1089 *iy = wwin->icon_y;
1090 *iw = wwin->icon_w;
1091 *ih = wwin->icon_h;
1092 } else {
1093 *ix = 0;
1094 *iy = 0;
1095 *iw = wwin->screen_ptr->scr_width;
1096 *ih = wwin->screen_ptr->scr_height;
1099 return 1;
1101 #endif /* USE_ANIMATIONS */
1103 void wIconifyWindow(WWindow *wwin)
1105 XWindowAttributes attribs;
1106 int present;
1108 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs))
1109 return; /* the window doesn't exist anymore */
1111 if (wwin->flags.miniaturized)
1112 return; /* already miniaturized */
1114 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1115 WWindow *owner = wWindowFor(wwin->transient_for);
1117 if (owner && owner->flags.miniaturized)
1118 return;
1121 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
1123 /* if the window is in another workspace, simplify process */
1124 if (present) {
1125 /* icon creation may take a while */
1126 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
1127 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
1128 GrabModeAsync, None, None, CurrentTime);
1131 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1132 if (!wwin->flags.icon_moved)
1133 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
1135 wwin->icon = icon_create_for_wwindow(wwin);
1136 wwin->icon->mapped = 1;
1138 /* extract the window screenshot everytime, as the option can be enable anytime */
1139 if (wwin->client_win && wwin->flags.mapped) {
1140 RImage *mini_preview;
1141 XImage *pimg;
1142 unsigned int w, h;
1143 int x, y;
1144 Window baz;
1146 XRaiseWindow(dpy, wwin->frame->core->window);
1147 XTranslateCoordinates(dpy, wwin->client_win, wwin->screen_ptr->root_win, 0, 0, &x, &y, &baz);
1149 w = attribs.width;
1150 h = attribs.height;
1152 if (x - attribs.x + attribs.width > wwin->screen_ptr->scr_width)
1153 w = wwin->screen_ptr->scr_width - x + attribs.x;
1155 if (y - attribs.y + attribs.height > wwin->screen_ptr->scr_height)
1156 h = wwin->screen_ptr->scr_height - y + attribs.y;
1158 pimg = XGetImage(dpy, wwin->client_win, 0, 0, w, h, AllPlanes, ZPixmap);
1159 if (pimg) {
1160 mini_preview = RCreateImageFromXImage(wwin->screen_ptr->rcontext, pimg, NULL);
1161 XDestroyImage(pimg);
1163 if (mini_preview) {
1164 set_icon_minipreview(wwin->icon, mini_preview);
1165 RReleaseImage(mini_preview);
1166 } else {
1167 const char *title;
1168 char title_buf[32];
1170 if (wwin->frame->title) {
1171 title = wwin->frame->title;
1172 } else {
1173 snprintf(title_buf, sizeof(title_buf), "(id=0x%lx)", wwin->client_win);
1174 title = title_buf;
1176 wwarning(_("creation of mini-preview failed for window \"%s\""), title);
1182 wwin->flags.miniaturized = 1;
1183 wwin->flags.mapped = 0;
1185 /* unmap transients */
1186 unmapTransientsFor(wwin);
1188 if (present) {
1189 #ifdef USE_ANIMATIONS
1190 int ix, iy, iw, ih;
1191 #endif
1192 XUngrabPointer(dpy, CurrentTime);
1193 wWindowUnmap(wwin);
1194 /* let all Expose events arrive so that we can repaint
1195 * something before the animation starts (and the server is grabbed) */
1196 XSync(dpy, 0);
1198 if (wPreferences.disable_miniwindows || wwin->flags.net_handle_icon)
1199 wClientSetState(wwin, IconicState, None);
1200 else
1201 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1203 flushExpose();
1204 #ifdef USE_ANIMATIONS
1205 if (getAnimationGeometry(wwin, &ix, &iy, &iw, &ih))
1206 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1207 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih);
1208 #endif
1211 wwin->flags.skip_next_animation = 0;
1213 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon) {
1214 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1215 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1216 XMapWindow(dpy, wwin->icon->core->window);
1218 AddToStackList(wwin->icon->core);
1219 wLowerFrame(wwin->icon->core);
1222 if (present) {
1223 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1226 * It doesn't seem to be working and causes button event hangup
1227 * when deiconifying a transient window.
1228 setupIconGrabs(wwin->icon);
1230 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1231 && wPreferences.focus_mode == WKF_CLICK) {
1232 WWindow *tmp;
1234 tmp = wwin->prev;
1235 while (tmp) {
1236 if (!WFLAGP(tmp, no_focusable)
1237 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1238 && (wwin->frame->workspace == tmp->frame->workspace))
1239 break;
1240 tmp = tmp->prev;
1242 wSetFocusTo(wwin->screen_ptr, tmp);
1243 } else if (wPreferences.focus_mode != WKF_CLICK) {
1244 wSetFocusTo(wwin->screen_ptr, NULL);
1246 #ifdef USE_ANIMATIONS
1247 if (!wwin->screen_ptr->flags.startup) {
1248 /* Catch up with events not processed while animation was running */
1249 Window clientwin = wwin->client_win;
1251 ProcessPendingEvents();
1253 /* the window can disappear while ProcessPendingEvents() runs */
1254 if (!wWindowFor(clientwin))
1255 return;
1257 #endif
1260 /* maybe we want to do this regardless of net_handle_icon
1261 * it seems to me we might break behaviour this way.
1263 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1264 && !wwin->flags.net_handle_icon)
1265 wIconSelect(wwin->icon);
1267 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1269 if (wPreferences.auto_arrange_icons)
1270 wArrangeIcons(wwin->screen_ptr, True);
1273 void wDeiconifyWindow(WWindow *wwin)
1275 /* Let's avoid changing workspace while deiconifying */
1276 w_global.ignore_workspace_change = True;
1278 /* we're hiding for show_desktop */
1279 int netwm_hidden = wwin->flags.net_show_desktop &&
1280 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1282 if (!netwm_hidden)
1283 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1285 if (!wwin->flags.miniaturized) {
1286 w_global.ignore_workspace_change = False;
1287 return;
1290 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1291 WWindow *owner = recursiveTransientFor(wwin);
1293 if (owner && owner->flags.miniaturized) {
1294 wDeiconifyWindow(owner);
1295 wSetFocusTo(wwin->screen_ptr, wwin);
1296 wRaiseFrame(wwin->frame->core);
1297 w_global.ignore_workspace_change = False;
1298 return;
1302 wwin->flags.miniaturized = 0;
1304 if (!netwm_hidden && !wwin->flags.shaded)
1305 wwin->flags.mapped = 1;
1307 if (!netwm_hidden || wPreferences.sticky_icons) {
1308 /* maybe we want to do this regardless of net_handle_icon
1309 * it seems to me we might break behaviour this way.
1311 if (!wPreferences.disable_miniwindows && !wwin->flags.net_handle_icon
1312 && wwin->icon != NULL) {
1313 if (wwin->icon->selected)
1314 wIconSelect(wwin->icon);
1316 XUnmapWindow(dpy, wwin->icon->core->window);
1320 /* if the window is in another workspace, do it silently */
1321 if (!netwm_hidden) {
1322 #ifdef USE_ANIMATIONS
1323 int ix, iy, iw, ih;
1324 if (getAnimationGeometry(wwin, &ix, &iy, &iw, &ih))
1325 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1326 wwin->frame_x, wwin->frame_y,
1327 wwin->frame->core->width, wwin->frame->core->height);
1328 #endif
1329 wwin->flags.skip_next_animation = 0;
1330 XGrabServer(dpy);
1331 if (!wwin->flags.shaded)
1332 XMapWindow(dpy, wwin->client_win);
1334 XMapWindow(dpy, wwin->frame->core->window);
1335 wRaiseFrame(wwin->frame->core);
1336 if (!wwin->flags.shaded)
1337 wClientSetState(wwin, NormalState, None);
1339 mapTransientsFor(wwin);
1342 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1343 && !wwin->flags.net_handle_icon) {
1344 RemoveFromStackList(wwin->icon->core);
1345 wSetFocusTo(wwin->screen_ptr, wwin);
1346 wIconDestroy(wwin->icon);
1347 wwin->icon = NULL;
1350 if (!netwm_hidden) {
1351 XUngrabServer(dpy);
1353 wSetFocusTo(wwin->screen_ptr, wwin);
1355 #ifdef USE_ANIMATIONS
1356 if (!wwin->screen_ptr->flags.startup) {
1357 /* Catch up with events not processed while animation was running */
1358 Window clientwin = wwin->client_win;
1360 ProcessPendingEvents();
1362 /* the window can disappear while ProcessPendingEvents() runs */
1363 if (!wWindowFor(clientwin)) {
1364 w_global.ignore_workspace_change = False;
1365 return;
1368 #endif
1371 if (wPreferences.auto_arrange_icons)
1372 wArrangeIcons(wwin->screen_ptr, True);
1374 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1376 /* In case we were shaded and iconified, also unshade */
1377 if (!netwm_hidden)
1378 wUnshadeWindow(wwin);
1380 w_global.ignore_workspace_change = False;
1383 static void hideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate)
1385 if (wwin->flags.miniaturized) {
1386 if (wwin->icon) {
1387 XUnmapWindow(dpy, wwin->icon->core->window);
1388 wwin->icon->mapped = 0;
1390 wwin->flags.hidden = 1;
1392 WMPostNotificationName(WMNChangedState, wwin, "hide");
1393 return;
1396 if (wwin->flags.inspector_open)
1397 wHideInspectorForWindow(wwin);
1399 wwin->flags.hidden = 1;
1400 wWindowUnmap(wwin);
1402 wClientSetState(wwin, IconicState, icon->icon_win);
1403 flushExpose();
1405 #ifdef USE_ANIMATIONS
1406 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1407 !wwin->flags.skip_next_animation && animate) {
1408 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1409 wwin->frame->core->width, wwin->frame->core->height,
1410 icon_x, icon_y, icon->core->width, icon->core->height);
1412 #else
1413 /* Tell the compiler it is normal that those parameters are not used in this case */
1414 (void) icon_x;
1415 (void) icon_y;
1416 (void) animate;
1417 #endif
1418 wwin->flags.skip_next_animation = 0;
1420 WMPostNotificationName(WMNChangedState, wwin, "hide");
1423 void wHideAll(WScreen *scr)
1425 WWindow *wwin;
1426 WWindow **windows;
1427 WMenu *menu;
1428 unsigned int wcount = 0;
1429 int i;
1431 if (!scr)
1432 return;
1434 menu = scr->switch_menu;
1436 windows = wmalloc(sizeof(WWindow *));
1438 if (menu != NULL) {
1439 for (i = 0; i < menu->entry_no; i++) {
1440 windows[wcount] = (WWindow *) menu->entries[i]->clientdata;
1441 wcount++;
1442 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1444 } else {
1445 wwin = scr->focused_window;
1447 while (wwin) {
1448 windows[wcount] = wwin;
1449 wcount++;
1450 windows = wrealloc(windows, sizeof(WWindow *) * (wcount + 1));
1451 wwin = wwin->prev;
1456 for (i = 0; i < wcount; i++) {
1457 wwin = windows[i];
1458 if (wwin->frame->workspace == scr->current_workspace
1459 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1460 && !wwin->flags.internal_window
1461 && !WFLAGP(wwin, no_miniaturizable)
1463 wwin->flags.skip_next_animation = 1;
1464 wIconifyWindow(wwin);
1468 wfree(windows);
1471 void wHideOtherApplications(WWindow *awin)
1473 WWindow *wwin;
1474 WApplication *tapp;
1476 if (!awin)
1477 return;
1478 wwin = awin->screen_ptr->focused_window;
1480 while (wwin) {
1481 if (wwin != awin
1482 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1483 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1484 && !wwin->flags.internal_window
1485 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1487 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1488 if (!WFLAGP(wwin, no_miniaturizable)) {
1489 wwin->flags.skip_next_animation = 1;
1490 wIconifyWindow(wwin);
1492 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1493 tapp = wApplicationOf(wwin->main_window);
1494 if (tapp) {
1495 tapp->flags.skip_next_animation = 1;
1496 wHideApplication(tapp);
1497 } else {
1498 if (!WFLAGP(wwin, no_miniaturizable)) {
1499 wwin->flags.skip_next_animation = 1;
1500 wIconifyWindow(wwin);
1505 wwin = wwin->prev;
1508 wSetFocusTo(awin->screen_ptr, awin);
1512 void wHideApplication(WApplication *wapp)
1514 WScreen *scr;
1515 WWindow *wlist;
1516 int hadfocus;
1517 int animate;
1519 if (!wapp) {
1520 wwarning("trying to hide a non grouped window");
1521 return;
1523 if (!wapp->main_window_desc) {
1524 wwarning("group leader not found for window group");
1525 return;
1527 scr = wapp->main_window_desc->screen_ptr;
1528 hadfocus = 0;
1529 wlist = scr->focused_window;
1530 if (!wlist)
1531 return;
1533 if (wlist->main_window == wapp->main_window)
1534 wapp->last_focused = wlist;
1535 else
1536 wapp->last_focused = NULL;
1538 animate = !wapp->flags.skip_next_animation;
1540 while (wlist) {
1541 if (wlist->main_window == wapp->main_window) {
1542 if (wlist->flags.focused)
1543 hadfocus = 1;
1544 if (wapp->app_icon) {
1545 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1546 wapp->app_icon->y_pos, wlist, animate);
1547 animate = False;
1550 wlist = wlist->prev;
1553 wapp->flags.skip_next_animation = 0;
1555 if (hadfocus) {
1556 if (wPreferences.focus_mode == WKF_CLICK) {
1557 wlist = scr->focused_window;
1558 while (wlist) {
1559 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1560 && (wlist->flags.mapped || wlist->flags.shaded))
1561 break;
1562 wlist = wlist->prev;
1564 wSetFocusTo(scr, wlist);
1565 } else {
1566 wSetFocusTo(scr, NULL);
1570 wapp->flags.hidden = 1;
1572 if (wPreferences.auto_arrange_icons)
1573 wArrangeIcons(scr, True);
1575 #ifdef HIDDENDOT
1576 if (wapp->app_icon)
1577 wAppIconPaint(wapp->app_icon);
1578 #endif
1581 static void unhideWindow(WIcon *icon, int icon_x, int icon_y, WWindow *wwin, int animate, int bringToCurrentWS)
1583 if (bringToCurrentWS)
1584 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1586 wwin->flags.hidden = 0;
1588 #ifdef USE_ANIMATIONS
1589 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1590 animateResize(wwin->screen_ptr, icon_x, icon_y,
1591 icon->core->width, icon->core->height,
1592 wwin->frame_x, wwin->frame_y,
1593 wwin->frame->core->width, wwin->frame->core->height);
1595 #else
1596 /* Tell the compiler it is normal that those parameters are not used in this case */
1597 (void) icon;
1598 (void) icon_x;
1599 (void) icon_y;
1600 (void) animate;
1601 #endif
1602 wwin->flags.skip_next_animation = 0;
1603 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1604 XMapWindow(dpy, wwin->client_win);
1605 XMapWindow(dpy, wwin->frame->core->window);
1606 wClientSetState(wwin, NormalState, None);
1607 wwin->flags.mapped = 1;
1608 wRaiseFrame(wwin->frame->core);
1610 if (wwin->flags.inspector_open)
1611 wUnhideInspectorForWindow(wwin);
1613 WMPostNotificationName(WMNChangedState, wwin, "hide");
1616 void wUnhideApplication(WApplication *wapp, Bool miniwindows, Bool bringToCurrentWS)
1618 WScreen *scr;
1619 WWindow *wlist, *next;
1620 WWindow *focused = NULL;
1621 int animate;
1623 if (!wapp)
1624 return;
1626 scr = wapp->main_window_desc->screen_ptr;
1627 wlist = scr->focused_window;
1628 if (!wlist)
1629 return;
1631 /* goto beginning of list */
1632 while (wlist->prev)
1633 wlist = wlist->prev;
1635 animate = !wapp->flags.skip_next_animation;
1637 while (wlist) {
1638 next = wlist->next;
1640 if (wlist->main_window == wapp->main_window) {
1641 if (wlist->flags.focused)
1642 focused = wlist;
1643 else if (!focused || !focused->flags.focused)
1644 focused = wlist;
1646 if (wlist->flags.miniaturized) {
1647 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1648 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1649 if (!wlist->icon->mapped) {
1650 int x, y;
1652 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1653 if (wlist->icon_x != x || wlist->icon_y != y)
1654 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1655 wlist->icon_x = x;
1656 wlist->icon_y = y;
1657 XMapWindow(dpy, wlist->icon->core->window);
1658 wlist->icon->mapped = 1;
1660 wRaiseFrame(wlist->icon->core);
1662 if (bringToCurrentWS)
1663 wWindowChangeWorkspace(wlist, scr->current_workspace);
1664 wlist->flags.hidden = 0;
1665 if (miniwindows && wlist->frame->workspace == scr->current_workspace)
1666 wDeiconifyWindow(wlist);
1667 WMPostNotificationName(WMNChangedState, wlist, "hide");
1668 } else if (wlist->flags.shaded) {
1669 if (bringToCurrentWS)
1670 wWindowChangeWorkspace(wlist, scr->current_workspace);
1671 wlist->flags.hidden = 0;
1672 wRaiseFrame(wlist->frame->core);
1673 if (wlist->frame->workspace == scr->current_workspace) {
1674 XMapWindow(dpy, wlist->frame->core->window);
1675 if (miniwindows)
1676 wUnshadeWindow(wlist);
1678 WMPostNotificationName(WMNChangedState, wlist, "hide");
1679 } else if (wlist->flags.hidden) {
1680 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1681 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1682 animate = False;
1683 } else {
1684 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace)
1685 wWindowChangeWorkspace(wlist, scr->current_workspace);
1687 wRaiseFrame(wlist->frame->core);
1690 wlist = next;
1693 wapp->flags.skip_next_animation = 0;
1694 wapp->flags.hidden = 0;
1696 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1697 wRaiseFrame(wapp->last_focused->frame->core);
1698 wSetFocusTo(scr, wapp->last_focused);
1699 } else if (focused) {
1700 wSetFocusTo(scr, focused);
1702 wapp->last_focused = NULL;
1703 if (wPreferences.auto_arrange_icons)
1704 wArrangeIcons(scr, True);
1706 #ifdef HIDDENDOT
1707 wAppIconPaint(wapp->app_icon);
1708 #endif
1711 void wShowAllWindows(WScreen *scr)
1713 WWindow *wwin, *old_foc;
1714 WApplication *wapp;
1716 old_foc = wwin = scr->focused_window;
1717 while (wwin) {
1718 if (!wwin->flags.internal_window &&
1719 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1720 if (wwin->flags.miniaturized) {
1721 wwin->flags.skip_next_animation = 1;
1722 wDeiconifyWindow(wwin);
1723 } else if (wwin->flags.hidden) {
1724 wapp = wApplicationOf(wwin->main_window);
1725 if (wapp) {
1726 wUnhideApplication(wapp, False, False);
1727 } else {
1728 wwin->flags.skip_next_animation = 1;
1729 wDeiconifyWindow(wwin);
1733 wwin = wwin->prev;
1735 wSetFocusTo(scr, old_foc);
1736 /*wRaiseFrame(old_foc->frame->core); */
1739 void wRefreshDesktop(WScreen *scr)
1741 Window win;
1742 XSetWindowAttributes attr;
1744 attr.backing_store = NotUseful;
1745 attr.save_under = False;
1746 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1747 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1748 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1749 XMapRaised(dpy, win);
1750 XDestroyWindow(dpy, win);
1751 XFlush(dpy);
1754 void wArrangeIcons(WScreen *scr, Bool arrangeAll)
1756 WWindow *wwin;
1757 WAppIcon *aicon;
1759 int head;
1760 const int heads = wXineramaHeads(scr);
1762 struct HeadVars {
1763 int pf; /* primary axis */
1764 int sf; /* secondary axis */
1765 int fullW;
1766 int fullH;
1767 int pi, si;
1768 int sx1, sx2, sy1, sy2; /* screen boundary */
1769 int sw, sh;
1770 int xo, yo;
1771 int xs, ys;
1772 } *vars;
1774 int isize = wPreferences.icon_size;
1776 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1778 for (head = 0; head < heads; ++head) {
1779 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1780 WMRect rect;
1782 if (scr->dock) {
1783 int offset = wPreferences.icon_size + DOCK_EXTRA_SPACE;
1785 if (scr->dock->on_right_side)
1786 area.x2 -= offset;
1787 else
1788 area.x1 += offset;
1791 rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1793 vars[head].pi = vars[head].si = 0;
1794 vars[head].sx1 = rect.pos.x;
1795 vars[head].sy1 = rect.pos.y;
1796 vars[head].sw = rect.size.width;
1797 vars[head].sh = rect.size.height;
1798 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1799 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1800 vars[head].sw = isize * (vars[head].sw / isize);
1801 vars[head].sh = isize * (vars[head].sh / isize);
1802 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1803 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1805 /* icon yard boundaries */
1806 if (wPreferences.icon_yard & IY_VERT) {
1807 vars[head].pf = vars[head].fullH;
1808 vars[head].sf = vars[head].fullW;
1809 } else {
1810 vars[head].pf = vars[head].fullW;
1811 vars[head].sf = vars[head].fullH;
1813 if (wPreferences.icon_yard & IY_RIGHT) {
1814 vars[head].xo = vars[head].sx2 - isize;
1815 vars[head].xs = -1;
1816 } else {
1817 vars[head].xo = vars[head].sx1;
1818 vars[head].xs = 1;
1820 if (wPreferences.icon_yard & IY_TOP) {
1821 vars[head].yo = vars[head].sy1;
1822 vars[head].ys = 1;
1823 } else {
1824 vars[head].yo = vars[head].sy2 - isize;
1825 vars[head].ys = -1;
1829 #define X ((wPreferences.icon_yard & IY_VERT) \
1830 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1831 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1833 #define Y ((wPreferences.icon_yard & IY_VERT) \
1834 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1835 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1837 /* arrange application icons */
1838 aicon = scr->app_icon_list;
1839 /* reverse them to avoid unnecessarily sliding of icons */
1840 while (aicon && aicon->next)
1841 aicon = aicon->next;
1843 while (aicon) {
1844 if (!aicon->docked) {
1845 /* CHECK: can icon be NULL here ? */
1846 /* The intention here is to place the AppIcon on the head that
1847 * contains most of the applications _main_ window. */
1848 head = wGetHeadForWindow(aicon->icon->owner);
1850 if (aicon->x_pos != X || aicon->y_pos != Y) {
1851 #ifdef USE_ANIMATIONS
1852 if (!wPreferences.no_animations)
1853 slide_window(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1854 #endif /* USE_ANIMATIONS */
1856 wAppIconMove(aicon, X, Y);
1857 vars[head].pi++;
1858 if (vars[head].pi >= vars[head].pf) {
1859 vars[head].pi = 0;
1860 vars[head].si++;
1863 aicon = aicon->prev;
1866 /* arrange miniwindows */
1867 wwin = scr->focused_window;
1868 /* reverse them to avoid unnecessarily shuffling */
1869 while (wwin && wwin->prev)
1870 wwin = wwin->prev;
1872 while (wwin) {
1873 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1874 (wwin->frame->workspace == scr->current_workspace ||
1875 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1877 head = wGetHeadForWindow(wwin);
1879 if (arrangeAll || !wwin->flags.icon_moved) {
1880 if (wwin->icon_x != X || wwin->icon_y != Y)
1881 move_window(wwin->icon->core->window, wwin->icon_x, wwin->icon_y, X, Y);
1883 wwin->icon_x = X;
1884 wwin->icon_y = Y;
1886 vars[head].pi++;
1887 if (vars[head].pi >= vars[head].pf) {
1888 vars[head].pi = 0;
1889 vars[head].si++;
1893 if (arrangeAll)
1894 wwin->flags.icon_moved = 0;
1895 /* we reversed the order, so we use next */
1896 wwin = wwin->next;
1899 wfree(vars);
1902 void wSelectWindow(WWindow *wwin, Bool flag)
1904 WScreen *scr = wwin->screen_ptr;
1906 if (flag) {
1907 wwin->flags.selected = 1;
1908 if (wwin->frame->selected_border_pixel)
1909 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->selected_border_pixel);
1910 else
1911 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1913 if (!HAS_BORDER(wwin))
1914 XSetWindowBorderWidth(dpy, wwin->frame->core->window, wwin->screen_ptr->frame_border_width);
1916 if (!scr->selected_windows)
1917 scr->selected_windows = WMCreateArray(4);
1918 WMAddToArray(scr->selected_windows, wwin);
1919 } else {
1920 wwin->flags.selected = 0;
1921 if (wwin->flags.focused) {
1922 if (wwin->frame->focused_border_pixel)
1923 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->focused_border_pixel);
1924 else
1925 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_focused_border_pixel);
1926 } else {
1927 if (wwin->frame->border_pixel)
1928 XSetWindowBorder(dpy, wwin->frame->core->window, *wwin->frame->border_pixel);
1929 else
1930 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1933 if (!HAS_BORDER(wwin))
1934 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1936 if (scr->selected_windows)
1937 WMRemoveFromArray(scr->selected_windows, wwin);
1941 void wMakeWindowVisible(WWindow *wwin)
1943 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1944 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1946 if (wwin->flags.shaded)
1947 wUnshadeWindow(wwin);
1949 if (wwin->flags.hidden) {
1950 WApplication *app;
1952 app = wApplicationOf(wwin->main_window);
1953 if (app) {
1954 /* trick to get focus to this window */
1955 app->last_focused = wwin;
1956 wUnhideApplication(app, False, False);
1959 if (wwin->flags.miniaturized) {
1960 wDeiconifyWindow(wwin);
1961 } else {
1962 if (!WFLAGP(wwin, no_focusable))
1963 wSetFocusTo(wwin->screen_ptr, wwin);
1964 wRaiseFrame(wwin->frame->core);
1969 * Do the animation while shading (called with what = SHADE)
1970 * or unshading (what = UNSHADE).
1972 #ifdef USE_ANIMATIONS
1973 static void shade_animate(WWindow *wwin, Bool what)
1975 int y, s, w, h;
1976 time_t time0 = time(NULL);
1978 if (wwin->flags.skip_next_animation || wPreferences.no_animations)
1979 return;
1981 switch (what) {
1982 case SHADE:
1983 if (!wwin->screen_ptr->flags.startup) {
1984 /* do the shading animation */
1985 h = wwin->frame->core->height;
1986 s = h / SHADE_STEPS;
1987 if (s < 1)
1988 s = 1;
1989 w = wwin->frame->core->width;
1990 y = wwin->frame->top_width;
1991 while (h > wwin->frame->top_width + 1) {
1992 XMoveWindow(dpy, wwin->client_win, 0, y);
1993 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1994 XFlush(dpy);
1996 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1997 break;
1999 if (SHADE_DELAY > 0)
2000 wusleep(SHADE_DELAY * 1000L);
2001 else
2002 wusleep(10);
2003 h -= s;
2004 y -= s;
2006 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2008 break;
2010 case UNSHADE:
2011 h = wwin->frame->top_width + wwin->frame->bottom_width;
2012 y = wwin->frame->top_width - wwin->client.height;
2013 s = abs(y) / SHADE_STEPS;
2014 if (s < 1)
2015 s = 1;
2016 w = wwin->frame->core->width;
2017 XMoveWindow(dpy, wwin->client_win, 0, y);
2018 if (s > 0) {
2019 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
2020 XResizeWindow(dpy, wwin->frame->core->window, w, h);
2021 XMoveWindow(dpy, wwin->client_win, 0, y);
2022 XFlush(dpy);
2023 if (SHADE_DELAY > 0)
2024 wusleep(SHADE_DELAY * 2000L / 3);
2025 else
2026 wusleep(10);
2027 h += s;
2028 y += s;
2030 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
2031 break;
2034 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2035 break;
2038 #endif