Maximus: Avoid a window list order issue
[wmaker-crm.git] / src / actions.c
blobc9c8070e0f11b55a7e95dd86eb11764395e0761a
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 * 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 "wcore.h"
36 #include "framewin.h"
37 #include "window.h"
38 #include "client.h"
39 #include "icon.h"
40 #include "funcs.h"
41 #include "application.h"
42 #include "actions.h"
43 #include "stacking.h"
44 #include "appicon.h"
45 #include "dock.h"
46 #include "appmenu.h"
47 #include "winspector.h"
48 #include "workspace.h"
49 #include "wsound.h"
50 #include "xinerama.h"
52 /****** Global Variables ******/
53 extern Time LastTimestamp;
54 extern Time LastFocusChange;
56 extern Cursor wCursor[WCUR_LAST];
58 extern WPreferences wPreferences;
60 extern Atom _XA_WM_TAKE_FOCUS;
62 extern void ProcessPendingEvents();
63 extern int calcIntersectionLength(int p1, int l1, int p2, int l2);
65 /******* Local Variables *******/
66 static struct {
67 int steps;
68 int delay;
69 } shadePars[5] = {
71 SHADE_STEPS_UF, SHADE_DELAY_UF}, {
72 SHADE_STEPS_F, SHADE_DELAY_F}, {
73 SHADE_STEPS_M, SHADE_DELAY_M}, {
74 SHADE_STEPS_S, SHADE_DELAY_S}, {
75 SHADE_STEPS_US, SHADE_DELAY_US}};
77 #define UNSHADE 0
78 #define SHADE 1
79 #define SHADE_STEPS shadePars[(int)wPreferences.shade_speed].steps
80 #define SHADE_DELAY shadePars[(int)wPreferences.shade_speed].delay
82 static int compareTimes(Time t1, Time t2)
84 Time diff;
85 if (t1 == t2)
86 return 0;
87 diff = t1 - t2;
88 return (diff < 60000) ? 1 : -1;
92 *----------------------------------------------------------------------
93 * wSetFocusTo--
94 * Changes the window focus to the one passed as argument.
95 * If the window to focus is not already focused, it will be brought
96 * to the head of the list of windows. Previously focused window is
97 * unfocused.
99 * Side effects:
100 * Window list may be reordered and the window focus is changed.
102 *----------------------------------------------------------------------
104 void wSetFocusTo(WScreen * scr, WWindow * wwin)
106 static WScreen *old_scr = NULL;
108 WWindow *old_focused;
109 WWindow *focused = scr->focused_window;
110 Time timestamp = LastTimestamp;
111 WApplication *oapp = NULL, *napp = NULL;
112 int wasfocused;
114 if (scr->flags.ignore_focus_events || compareTimes(LastFocusChange, timestamp) > 0)
115 return;
117 if (!old_scr)
118 old_scr = scr;
119 old_focused = old_scr->focused_window;
121 LastFocusChange = timestamp;
123 if (old_focused)
124 oapp = wApplicationOf(old_focused->main_window);
126 if (wwin == NULL) {
127 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
128 if (old_focused) {
129 wWindowUnfocus(old_focused);
131 if (oapp) {
132 wAppMenuUnmap(oapp->menu);
133 #ifdef NEWAPPICON
134 wApplicationDeactivate(oapp);
135 #endif
138 WMPostNotificationName(WMNChangedFocus, NULL, (void *)True);
139 return;
140 } else if (old_scr != scr && old_focused) {
141 wWindowUnfocus(old_focused);
144 wasfocused = wwin->flags.focused;
145 napp = wApplicationOf(wwin->main_window);
147 /* remember last workspace where the app has been */
148 if (napp) {
149 /*napp->last_workspace = wwin->screen_ptr->current_workspace; */
150 napp->last_workspace = wwin->frame->workspace;
153 if (wwin->flags.mapped && !WFLAGP(wwin, no_focusable)) {
154 /* install colormap if colormap mode is lock mode */
155 if (wPreferences.colormap_mode == WCM_CLICK)
156 wColormapInstallForWindow(scr, wwin);
158 /* set input focus */
159 switch (wwin->focus_mode) {
160 case WFM_NO_INPUT:
161 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
162 break;
164 case WFM_PASSIVE:
165 case WFM_LOCALLY_ACTIVE:
166 XSetInputFocus(dpy, wwin->client_win, RevertToParent, CurrentTime);
167 break;
169 case WFM_GLOBALLY_ACTIVE:
170 break;
172 XFlush(dpy);
173 if (wwin->protocols.TAKE_FOCUS) {
174 wClientSendProtocol(wwin, _XA_WM_TAKE_FOCUS, timestamp);
176 XSync(dpy, False);
177 } else {
178 XSetInputFocus(dpy, scr->no_focus_win, RevertToParent, CurrentTime);
180 if (WFLAGP(wwin, no_focusable))
181 return;
183 /* if this is not the focused window focus it */
184 if (focused != wwin) {
185 /* change the focus window list order */
186 if (wwin->prev)
187 wwin->prev->next = wwin->next;
189 if (wwin->next)
190 wwin->next->prev = wwin->prev;
192 wwin->prev = focused;
193 focused->next = wwin;
194 wwin->next = NULL;
195 scr->focused_window = wwin;
197 if (oapp && oapp != napp) {
198 wAppMenuUnmap(oapp->menu);
199 #ifdef NEWAPPICON
200 wApplicationDeactivate(oapp);
201 #endif
205 wWindowFocus(wwin, focused);
207 if (napp && !wasfocused) {
208 #ifdef USER_MENU
209 wUserMenuRefreshInstances(napp->menu, wwin);
210 #endif /* USER_MENU */
212 if (wwin->flags.mapped)
213 wAppMenuMap(napp->menu, wwin);
214 #ifdef NEWAPPICON
215 wApplicationActivate(napp);
216 #endif
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 wSoundPlay(WSOUND_SHADE);
231 shade_animate(wwin, SHADE);
233 wwin->flags.skip_next_animation = 0;
234 wwin->flags.shaded = 1;
235 wwin->flags.mapped = 0;
236 /* prevent window withdrawal when getting UnmapNotify */
237 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
238 XUnmapWindow(dpy, wwin->client_win);
239 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
241 /* for the client it's just like iconification */
242 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
244 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
245 wWindowSynthConfigureNotify(wwin);
248 wClientSetState(wwin, IconicState, None);
251 WMPostNotificationName(WMNChangedState, wwin, "shade");
253 #ifdef ANIMATIONS
254 if (!wwin->screen_ptr->flags.startup) {
255 /* Catch up with events not processed while animation was running */
256 ProcessPendingEvents();
258 #endif
261 void wUnshadeWindow(WWindow *wwin)
264 if (!wwin->flags.shaded)
265 return;
267 wwin->flags.shaded = 0;
268 wwin->flags.mapped = 1;
269 XMapWindow(dpy, wwin->client_win);
271 wSoundPlay(WSOUND_UNSHADE);
272 shade_animate(wwin, UNSHADE);
274 wwin->flags.skip_next_animation = 0;
275 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
276 wwin->frame->top_width + wwin->client.height + wwin->frame->bottom_width);
278 wwin->client.y = wwin->frame_y + wwin->frame->top_width;
279 wWindowSynthConfigureNotify(wwin);
282 wClientSetState(wwin, NormalState, None);
284 /* if the window is focused, set the focus again as it was disabled during
285 * shading */
286 if (wwin->flags.focused)
287 wSetFocusTo(wwin->screen_ptr, wwin);
289 WMPostNotificationName(WMNChangedState, wwin, "shade");
292 /* Set the old coordinates using the current values */
293 static void save_old_geometry(WWindow *wwin)
295 wwin->old_geometry.width = wwin->client.width;
296 wwin->old_geometry.height = wwin->client.height;
297 wwin->old_geometry.x = wwin->frame_x;
298 wwin->old_geometry.y = wwin->frame_y;
301 void wMaximizeWindow(WWindow * wwin, int directions)
303 int new_x, new_y;
304 unsigned int new_width, new_height, half_scr_width;
305 WArea usableArea, totalArea;
306 Bool has_border = 1;
307 int adj_size;
309 if (!IS_RESIZABLE(wwin))
310 return;
312 if (!HAS_BORDER(wwin))
313 has_border = 0;
315 /* the size to adjust the geometry */
316 adj_size = FRAME_BORDER_WIDTH * 2 * has_border;
318 /* save old coordinates before we change the current values */
319 save_old_geometry(wwin);
321 totalArea.x1 = 0;
322 totalArea.y1 = 0;
323 totalArea.x2 = wwin->screen_ptr->scr_width;
324 totalArea.y2 = wwin->screen_ptr->scr_height;
325 usableArea = totalArea;
327 if (!(directions & MAX_IGNORE_XINERAMA)) {
328 WScreen *scr = wwin->screen_ptr;
329 int head;
331 if (directions & MAX_KEYBOARD)
332 head = wGetHeadForWindow(wwin);
333 else
334 head = wGetHeadForPointerLocation(scr);
336 usableArea = wGetUsableAreaForHead(scr, head, &totalArea, True);
339 /* Only save directions, not kbd or xinerama hints */
340 directions &= (MAX_HORIZONTAL | MAX_VERTICAL | MAX_LEFTHALF | MAX_RIGHTHALF | MAX_MAXIMUS);
342 if (WFLAGP(wwin, full_maximize)) {
343 usableArea = totalArea;
345 half_scr_width = (usableArea.x2 - usableArea.x1)/2;
347 if (wwin->flags.shaded) {
348 wwin->flags.skip_next_animation = 1;
349 wUnshadeWindow(wwin);
352 if (directions & MAX_HORIZONTAL) {
353 new_width = usableArea.x2 - usableArea.x1 - adj_size;
354 new_x = usableArea.x1;
355 } else if (directions & MAX_LEFTHALF) {
356 new_width = half_scr_width - adj_size;
357 new_x = usableArea.x1;
358 } else if (directions & MAX_RIGHTHALF) {
359 new_width = half_scr_width - adj_size;
360 new_x = usableArea.x1 + half_scr_width;
361 } else {
362 new_x = wwin->frame_x;
363 new_width = wwin->frame->core->width;
366 if (directions & MAX_VERTICAL) {
367 new_height = usableArea.y2 - usableArea.y1 - adj_size;
368 new_y = usableArea.y1;
369 if (WFLAGP(wwin, full_maximize)) {
370 new_y -= wwin->frame->top_width;
371 new_height += wwin->frame->bottom_width - 1;
373 } else {
374 new_y = wwin->frame_y;
375 new_height = wwin->frame->core->height;
378 if (!WFLAGP(wwin, full_maximize)) {
379 new_height -= wwin->frame->top_width + wwin->frame->bottom_width;
382 if (directions & MAX_MAXIMUS) {
383 find_Maximus_geometry(wwin, usableArea, &new_x, &new_y, &new_width, &new_height);
384 if (WFLAGP(wwin, full_maximize) && new_y == 0) {
385 new_y -= wwin->frame->top_width;
386 new_height += wwin->frame->top_width - 1;
390 wWindowConstrainSize(wwin, &new_width, &new_height);
392 wWindowCropSize(wwin, usableArea.x2 - usableArea.x1,
393 usableArea.y2 - usableArea.y1, &new_width, &new_height);
395 wWindowConfigure(wwin, new_x, new_y, new_width, new_height);
397 WMPostNotificationName(WMNChangedState, wwin, "maximize");
399 wSoundPlay(WSOUND_MAXIMIZE);
400 /* set maximization state */
401 wwin->flags.maximized = directions;
405 * Maximus: tiled maximization (maximize without overlapping other windows)
407 * The window to be maximized will be denoted by w_0 (sub-index zero)
408 * while the windows which will stop the maximization of w_0 are denoted by w_j.
410 static void find_Maximus_geometry(WWindow *wwin, WArea usableArea, int *new_x, int *new_y,
411 unsigned int *new_width, unsigned int *new_height)
413 WWindow *tmp;
414 int x_0 = wwin->frame_x;
415 int y_0 = wwin->frame_y;
416 int width_0 = wwin->frame->core->width;
417 int height_0 = wwin->frame->core->height;
418 int botton_0 = y_0 + height_0;
419 int right_border_0 = x_0 + width_0;
420 int new_x_0, new_y_0, new_botton_0, new_right_border_0, new_height_0;
421 int x_j, y_j, width_j, height_j, botton_j, top_j, right_border_j;
422 int x_intsect, y_intsect;
423 /* Assume that the window w_0 has titlebar etc */
424 int has_titlebar = 1, has_resizebar = 1, has_border = 1;
425 int adjust_height, adjust_width;
427 /* Try to fully maximize first, then readjust later */
428 new_x_0 = usableArea.x1;
429 new_y_0 = usableArea.y1;
430 new_botton_0 = usableArea.y2;
431 new_right_border_0 = usableArea.x2;
433 if (!HAS_TITLEBAR(wwin))
434 has_titlebar = 0;
435 if (!HAS_RESIZEBAR(wwin))
436 has_resizebar = 0;
437 if (!HAS_BORDER(wwin))
438 has_border = 0;
440 /* the lengths to be subtracted if w_0 has titlebar, etc */
441 adjust_height = TITLEBAR_HEIGHT * has_titlebar
442 + 2 * FRAME_BORDER_WIDTH * has_border + RESIZEBAR_HEIGHT * has_resizebar;
443 adjust_width = 2 * FRAME_BORDER_WIDTH * has_border;
445 tmp = wwin;
446 /* TODO: Is the focused window always the last in the list? */
447 while (tmp->prev) {
448 /* ignore windows in other workspaces or minimized */
449 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
450 || tmp->prev->flags.miniaturized) {
451 tmp = tmp->prev;
452 continue;
454 tmp = tmp->prev;
456 /* set the w_j window coordinates */
457 x_j = tmp->frame_x;
458 y_j = tmp->frame_y;
459 width_j = tmp->frame->core->width;
460 height_j = tmp->frame->core->height;
461 botton_j = y_j + height_j;
462 top_j = y_j;
463 right_border_j = x_j + width_j;
465 /* Try to maximize in the y direction first */
466 x_intsect = calcIntersectionLength(x_0, width_0, x_j, width_j);
467 if (x_intsect != 0) {
468 if (botton_j < y_0 && botton_j > new_y_0) {
469 /* w_0 is below the botton of w_j */
470 new_y_0 = botton_j;
472 if (botton_0 < top_j && top_j < new_botton_0) {
473 /* The botton of w_0 is above the top of w_j */
474 new_botton_0 = top_j;
479 tmp = wwin;
480 while (tmp->prev) {
481 /* ignore windows in other workspaces or minimized */
482 if (tmp->prev->frame->workspace != wwin->screen_ptr->current_workspace
483 || tmp->prev->flags.miniaturized) {
484 tmp = tmp->prev;
485 continue;
487 tmp = tmp->prev;
489 /* set the w_j window coordinates */
490 x_j = tmp->frame_x;
491 y_j = tmp->frame_y;
492 width_j = tmp->frame->core->width;
493 height_j = tmp->frame->core->height;
494 botton_j = y_j + height_j;
495 top_j = y_j;
496 right_border_j = x_j + width_j;
499 * Use the updated y coordinates from the above step to account
500 * the possibility that the new value of y_0 will have different
501 * intersections with w_j
503 new_height_0 = new_botton_0 - new_y_0 - adjust_height;
504 y_intsect = calcIntersectionLength(new_y_0, new_height_0, y_j, height_j);
505 if (y_intsect != 0) {
506 if (right_border_j < x_0 && right_border_j > new_x_0) {
507 /* w_0 is completely to the right of w_j */
508 new_x_0 = right_border_j;
510 if (right_border_0 < x_j && x_j < new_right_border_0) {
511 /* w_0 is completely to the left of w_j */
512 new_right_border_0 = x_j;
517 new_height_0 = new_botton_0 - new_y_0 - adjust_height;
518 *new_x = new_x_0;
519 *new_y = new_y_0;
520 *new_height = new_height_0;
521 *new_width = new_right_border_0 - new_x_0 - adjust_width;
524 void wUnmaximizeWindow(WWindow * wwin)
526 int x, y, w, h;
527 WMRect old_geom_rect;
528 int old_head;
529 Bool same_head;
531 if (!wwin->flags.maximized)
532 return;
534 if (wwin->flags.shaded) {
535 wwin->flags.skip_next_animation = 1;
536 wUnshadeWindow(wwin);
538 /* Use old coordinates if they are set, current values otherwise */
539 old_geom_rect = wmkrect(wwin->old_geometry.x, wwin->old_geometry.y, wwin->old_geometry.width, wwin->old_geometry.height);
540 old_head = wGetHeadForRect(wwin->screen_ptr, old_geom_rect);
541 same_head = (wGetHeadForWindow(wwin) == old_head);
542 x = (wwin->old_geometry.x && same_head) ? wwin->old_geometry.x : wwin->frame_x;
543 y = (wwin->old_geometry.y && same_head) ? wwin->old_geometry.y : wwin->frame_y;
544 w = wwin->old_geometry.width ? wwin->old_geometry.width : wwin->client.width;
545 h = wwin->old_geometry.height ? wwin->old_geometry.height : wwin->client.height;
547 wwin->flags.maximized = 0;
548 wWindowConfigure(wwin, x, y, w, h);
550 WMPostNotificationName(WMNChangedState, wwin, "maximize");
552 wSoundPlay(WSOUND_UNMAXIMIZE);
555 void wFullscreenWindow(WWindow * wwin)
557 int head;
558 WMRect rect;
560 if (wwin->flags.fullscreen)
561 return;
563 wwin->flags.fullscreen = True;
565 wWindowConfigureBorders(wwin);
567 ChangeStackingLevel(wwin->frame->core, WMFullscreenLevel);
569 wwin->bfs_geometry.x = wwin->frame_x;
570 wwin->bfs_geometry.y = wwin->frame_y;
571 wwin->bfs_geometry.width = wwin->frame->core->width;
572 wwin->bfs_geometry.height = wwin->frame->core->height;
574 head = wGetHeadForWindow(wwin);
575 rect = wGetRectForHead(wwin->screen_ptr, head);
576 wWindowConfigure(wwin, rect.pos.x, rect.pos.y, rect.size.width, rect.size.height);
578 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
581 void wUnfullscreenWindow(WWindow * wwin)
583 if (!wwin->flags.fullscreen)
584 return;
586 wwin->flags.fullscreen = False;
588 if (wwin->frame->core->stacking->window_level == WMFullscreenLevel) {
589 if (WFLAGP(wwin, sunken)) {
590 ChangeStackingLevel(wwin->frame->core, WMSunkenLevel);
591 } else if (WFLAGP(wwin, floating)) {
592 ChangeStackingLevel(wwin->frame->core, WMFloatingLevel);
593 } else {
594 ChangeStackingLevel(wwin->frame->core, WMNormalLevel);
598 wWindowConfigure(wwin, wwin->bfs_geometry.x, wwin->bfs_geometry.y,
599 wwin->bfs_geometry.width, wwin->bfs_geometry.height);
601 wWindowConfigureBorders(wwin);
603 // seems unnecessary, but also harmless (doesn't generate flicker) -Dan
604 wFrameWindowPaint(wwin->frame);
607 WMPostNotificationName(WMNChangedState, wwin, "fullscreen");
610 #ifdef ANIMATIONS
611 static void animateResizeFlip(WScreen * scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
613 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_F)
614 float cx, cy, cw, ch;
615 float xstep, ystep, wstep, hstep;
616 XPoint points[5];
617 float dx, dch, midy;
618 float angle, final_angle, delta;
620 xstep = (float)(fx - x) / steps;
621 ystep = (float)(fy - y) / steps;
622 wstep = (float)(fw - w) / steps;
623 hstep = (float)(fh - h) / steps;
625 cx = (float)x;
626 cy = (float)y;
627 cw = (float)w;
628 ch = (float)h;
630 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_F;
631 delta = (float)(final_angle / FRAMES);
632 for (angle = 0;; angle += delta) {
633 if (angle > final_angle)
634 angle = final_angle;
636 dx = (cw / 10) - ((cw / 5) * sin(angle));
637 dch = (ch / 2) * cos(angle);
638 midy = cy + (ch / 2);
640 points[0].x = cx + dx;
641 points[0].y = midy - dch;
642 points[1].x = cx + cw - dx;
643 points[1].y = points[0].y;
644 points[2].x = cx + cw + dx;
645 points[2].y = midy + dch;
646 points[3].x = cx - dx;
647 points[3].y = points[2].y;
648 points[4].x = points[0].x;
649 points[4].y = points[0].y;
651 XGrabServer(dpy);
652 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
653 XFlush(dpy);
654 #if (MINIATURIZE_ANIMATION_DELAY_F > 0)
655 wusleep(MINIATURIZE_ANIMATION_DELAY_F);
656 #else
657 wusleep(10);
658 #endif
660 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
661 XUngrabServer(dpy);
662 cx += xstep;
663 cy += ystep;
664 cw += wstep;
665 ch += hstep;
666 if (angle >= final_angle)
667 break;
670 XFlush(dpy);
673 #undef FRAMES
675 static void
676 animateResizeTwist(WScreen * scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
678 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_T)
679 float cx, cy, cw, ch;
680 float xstep, ystep, wstep, hstep;
681 XPoint points[5];
682 float angle, final_angle, a, d, delta;
684 x += w / 2;
685 y += h / 2;
686 fx += fw / 2;
687 fy += fh / 2;
689 xstep = (float)(fx - x) / steps;
690 ystep = (float)(fy - y) / steps;
691 wstep = (float)(fw - w) / steps;
692 hstep = (float)(fh - h) / steps;
694 cx = (float)x;
695 cy = (float)y;
696 cw = (float)w;
697 ch = (float)h;
699 final_angle = 2 * WM_PI * MINIATURIZE_ANIMATION_TWIST_T;
700 delta = (float)(final_angle / FRAMES);
701 for (angle = 0;; angle += delta) {
702 if (angle > final_angle)
703 angle = final_angle;
705 a = atan(ch / cw);
706 d = sqrt((cw / 2) * (cw / 2) + (ch / 2) * (ch / 2));
708 points[0].x = cx + cos(angle - a) * d;
709 points[0].y = cy + sin(angle - a) * d;
710 points[1].x = cx + cos(angle + a) * d;
711 points[1].y = cy + sin(angle + a) * d;
712 points[2].x = cx + cos(angle - a + WM_PI) * d;
713 points[2].y = cy + sin(angle - a + WM_PI) * d;
714 points[3].x = cx + cos(angle + a + WM_PI) * d;
715 points[3].y = cy + sin(angle + a + WM_PI) * d;
716 points[4].x = cx + cos(angle - a) * d;
717 points[4].y = cy + sin(angle - a) * d;
718 XGrabServer(dpy);
719 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
720 XFlush(dpy);
721 #if (MINIATURIZE_ANIMATION_DELAY_T > 0)
722 wusleep(MINIATURIZE_ANIMATION_DELAY_T);
723 #else
724 wusleep(10);
725 #endif
727 XDrawLines(dpy, scr->root_win, scr->frame_gc, points, 5, CoordModeOrigin);
728 XUngrabServer(dpy);
729 cx += xstep;
730 cy += ystep;
731 cw += wstep;
732 ch += hstep;
733 if (angle >= final_angle)
734 break;
737 XFlush(dpy);
740 #undef FRAMES
742 static void animateResizeZoom(WScreen * scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int steps)
744 #define FRAMES (MINIATURIZE_ANIMATION_FRAMES_Z)
745 float cx[FRAMES], cy[FRAMES], cw[FRAMES], ch[FRAMES];
746 float xstep, ystep, wstep, hstep;
747 int i, j;
749 xstep = (float)(fx - x) / steps;
750 ystep = (float)(fy - y) / steps;
751 wstep = (float)(fw - w) / steps;
752 hstep = (float)(fh - h) / steps;
754 for (j = 0; j < FRAMES; j++) {
755 cx[j] = (float)x;
756 cy[j] = (float)y;
757 cw[j] = (float)w;
758 ch[j] = (float)h;
760 XGrabServer(dpy);
761 for (i = 0; i < steps; i++) {
762 for (j = 0; j < FRAMES; j++) {
763 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
764 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
766 XFlush(dpy);
767 #if (MINIATURIZE_ANIMATION_DELAY_Z > 0)
768 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
769 #else
770 wusleep(10);
771 #endif
772 for (j = 0; j < FRAMES; j++) {
773 XDrawRectangle(dpy, scr->root_win, scr->frame_gc,
774 (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
775 if (j < FRAMES - 1) {
776 cx[j] = cx[j + 1];
777 cy[j] = cy[j + 1];
778 cw[j] = cw[j + 1];
779 ch[j] = ch[j + 1];
780 } else {
781 cx[j] += xstep;
782 cy[j] += ystep;
783 cw[j] += wstep;
784 ch[j] += hstep;
789 for (j = 0; j < FRAMES; j++) {
790 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
792 XFlush(dpy);
793 #if (MINIATURIZE_ANIMATION_DELAY_Z > 0)
794 wusleep(MINIATURIZE_ANIMATION_DELAY_Z);
795 #else
796 wusleep(10);
797 #endif
798 for (j = 0; j < FRAMES; j++) {
799 XDrawRectangle(dpy, scr->root_win, scr->frame_gc, (int)cx[j], (int)cy[j], (int)cw[j], (int)ch[j]);
802 XUngrabServer(dpy);
805 #undef FRAMES
807 void animateResize(WScreen * scr, int x, int y, int w, int h, int fx, int fy, int fw, int fh, int hiding)
809 int style = wPreferences.iconification_style; /* Catch the value */
810 int steps, k;
812 if (style == WIS_NONE)
813 return;
815 if (style == WIS_RANDOM) {
816 style = rand() % 3;
819 k = (hiding ? 2 : 3);
821 switch (style) {
822 case WIS_TWIST:
823 steps = (MINIATURIZE_ANIMATION_STEPS_T * k) / 3;
824 if (steps > 0)
825 animateResizeTwist(scr, x, y, w, h, fx, fy, fw, fh, steps);
826 break;
827 case WIS_FLIP:
828 steps = (MINIATURIZE_ANIMATION_STEPS_F * k) / 3;
829 if (steps > 0)
830 animateResizeFlip(scr, x, y, w, h, fx, fy, fw, fh, steps);
831 break;
832 case WIS_ZOOM:
833 default:
834 steps = (MINIATURIZE_ANIMATION_STEPS_Z * k) / 3;
835 if (steps > 0)
836 animateResizeZoom(scr, x, y, w, h, fx, fy, fw, fh, steps);
837 break;
840 #endif /* ANIMATIONS */
842 static void flushExpose()
844 XEvent tmpev;
846 while (XCheckTypedEvent(dpy, Expose, &tmpev))
847 WMHandleEvent(&tmpev);
848 XSync(dpy, 0);
851 static void unmapTransientsFor(WWindow * wwin)
853 WWindow *tmp;
855 tmp = wwin->screen_ptr->focused_window;
856 while (tmp) {
857 /* unmap the transients for this transient */
858 if (tmp != wwin && tmp->transient_for == wwin->client_win
859 && (tmp->flags.mapped || wwin->screen_ptr->flags.startup || tmp->flags.shaded)) {
860 unmapTransientsFor(tmp);
861 tmp->flags.miniaturized = 1;
862 if (!tmp->flags.shaded) {
863 wWindowUnmap(tmp);
864 } else {
865 XUnmapWindow(dpy, tmp->frame->core->window);
868 if (!tmp->flags.shaded)
870 wClientSetState(tmp, IconicState, None);
872 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
874 tmp = tmp->prev;
878 static void mapTransientsFor(WWindow * wwin)
880 WWindow *tmp;
882 tmp = wwin->screen_ptr->focused_window;
883 while (tmp) {
884 /* recursively map the transients for this transient */
885 if (tmp != wwin && tmp->transient_for == wwin->client_win && /*!tmp->flags.mapped */ tmp->flags.miniaturized
886 && tmp->icon == NULL) {
887 mapTransientsFor(tmp);
888 tmp->flags.miniaturized = 0;
889 if (!tmp->flags.shaded) {
890 wWindowMap(tmp);
891 } else {
892 XMapWindow(dpy, tmp->frame->core->window);
894 tmp->flags.semi_focused = 0;
896 if (!tmp->flags.shaded)
898 wClientSetState(tmp, NormalState, None);
900 WMPostNotificationName(WMNChangedState, tmp, "iconify-transient");
902 tmp = tmp->prev;
906 #if 0
907 static void setupIconGrabs(WIcon * icon)
909 /* setup passive grabs on the icon */
910 XGrabButton(dpy, Button1, AnyModifier, icon->core->window, True,
911 ButtonPressMask, GrabModeSync, GrabModeAsync, None, None);
912 XGrabButton(dpy, Button2, AnyModifier, icon->core->window, True,
913 ButtonPressMask, GrabModeSync, GrabModeAsync, None, None);
914 XGrabButton(dpy, Button3, AnyModifier, icon->core->window, True,
915 ButtonPressMask, GrabModeSync, GrabModeAsync, None, None);
916 XSync(dpy, 0);
918 #endif
920 static WWindow *recursiveTransientFor(WWindow * wwin)
922 int i;
924 if (!wwin)
925 return None;
927 /* hackish way to detect transient_for cycle */
928 i = wwin->screen_ptr->window_count + 1;
930 while (wwin && wwin->transient_for != None && i > 0) {
931 wwin = wWindowFor(wwin->transient_for);
932 i--;
934 if (i == 0 && wwin) {
935 wwarning("%s has a severely broken WM_TRANSIENT_FOR hint.", wwin->frame->title);
936 return NULL;
939 return wwin;
942 #if 0
943 static void removeIconGrabs(WIcon * icon)
945 /* remove passive grabs on the icon */
946 XUngrabButton(dpy, Button1, AnyModifier, icon->core->window);
947 XUngrabButton(dpy, Button2, AnyModifier, icon->core->window);
948 XUngrabButton(dpy, Button3, AnyModifier, icon->core->window);
949 XSync(dpy, 0);
951 #endif
953 void wIconifyWindow(WWindow * wwin)
955 XWindowAttributes attribs;
956 int present;
958 if (!XGetWindowAttributes(dpy, wwin->client_win, &attribs)) {
959 /* the window doesn't exist anymore */
960 return;
963 if (wwin->flags.miniaturized) {
964 return;
967 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
968 WWindow *owner = wWindowFor(wwin->transient_for);
970 if (owner && owner->flags.miniaturized)
971 return;
974 present = wwin->frame->workspace == wwin->screen_ptr->current_workspace;
976 /* if the window is in another workspace, simplify process */
977 if (present) {
978 /* icon creation may take a while */
979 XGrabPointer(dpy, wwin->screen_ptr->root_win, False,
980 ButtonMotionMask | ButtonReleaseMask, GrabModeAsync,
981 GrabModeAsync, None, None, CurrentTime);
984 if (!wPreferences.disable_miniwindows
985 #ifdef NETWM_HINTS
986 && !wwin->flags.net_handle_icon
987 #endif
989 if (!wwin->flags.icon_moved) {
990 PlaceIcon(wwin->screen_ptr, &wwin->icon_x, &wwin->icon_y, wGetHeadForWindow(wwin));
992 wwin->icon = wIconCreate(wwin);
994 wwin->icon->mapped = 1;
997 wwin->flags.miniaturized = 1;
998 wwin->flags.mapped = 0;
1000 /* unmap transients */
1002 unmapTransientsFor(wwin);
1004 if (present) {
1005 wSoundPlay(WSOUND_ICONIFY);
1007 XUngrabPointer(dpy, CurrentTime);
1008 wWindowUnmap(wwin);
1009 /* let all Expose events arrive so that we can repaint
1010 * something before the animation starts (and the server is grabbed) */
1011 XSync(dpy, 0);
1013 if (wPreferences.disable_miniwindows
1014 #ifdef NETWM_HINTS
1015 || wwin->flags.net_handle_icon
1016 #endif
1018 wClientSetState(wwin, IconicState, None);
1019 else
1020 wClientSetState(wwin, IconicState, wwin->icon->icon_win);
1022 flushExpose();
1023 #ifdef ANIMATIONS
1024 if (!wwin->screen_ptr->flags.startup && !wwin->flags.skip_next_animation
1025 && !wPreferences.no_animations) {
1026 int ix, iy, iw, ih;
1028 if (!wPreferences.disable_miniwindows
1029 #ifdef NETWM_HINTS
1030 && !wwin->flags.net_handle_icon
1031 #endif
1033 ix = wwin->icon_x;
1034 iy = wwin->icon_y;
1035 iw = wwin->icon->core->width;
1036 ih = wwin->icon->core->height;
1037 } else {
1038 #ifdef NETWM_HINTS
1039 if (wwin->flags.net_handle_icon) {
1040 ix = wwin->icon_x;
1041 iy = wwin->icon_y;
1042 iw = wwin->icon_w;
1043 ih = wwin->icon_h;
1044 } else
1045 #endif
1047 ix = 0;
1048 iy = 0;
1049 iw = wwin->screen_ptr->scr_width;
1050 ih = wwin->screen_ptr->scr_height;
1053 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1054 wwin->frame->core->width, wwin->frame->core->height, ix, iy, iw, ih, False);
1056 #endif
1059 wwin->flags.skip_next_animation = 0;
1061 if (!wPreferences.disable_miniwindows
1062 #ifdef NETWM_HINTS
1063 && !wwin->flags.net_handle_icon
1064 #endif
1067 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace ||
1068 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)
1070 XMapWindow(dpy, wwin->icon->core->window);
1072 AddToStackList(wwin->icon->core);
1074 wLowerFrame(wwin->icon->core);
1077 if (present) {
1078 WWindow *owner = recursiveTransientFor(wwin->screen_ptr->focused_window);
1081 * It doesn't seem to be working and causes button event hangup
1082 * when deiconifying a transient window.
1083 setupIconGrabs(wwin->icon);
1085 if ((wwin->flags.focused || (owner && wwin->client_win == owner->client_win))
1086 && wPreferences.focus_mode == WKF_CLICK) {
1087 WWindow *tmp;
1089 tmp = wwin->prev;
1090 while (tmp) {
1091 if (!WFLAGP(tmp, no_focusable)
1092 && !(tmp->flags.hidden || tmp->flags.miniaturized)
1093 && (wwin->frame->workspace == tmp->frame->workspace))
1094 break;
1095 tmp = tmp->prev;
1097 wSetFocusTo(wwin->screen_ptr, tmp);
1098 } else if (wPreferences.focus_mode != WKF_CLICK) {
1099 wSetFocusTo(wwin->screen_ptr, NULL);
1101 #ifdef ANIMATIONS
1102 if (!wwin->screen_ptr->flags.startup) {
1103 /* Catch up with events not processed while animation was running */
1104 Window clientwin = wwin->client_win;
1106 ProcessPendingEvents();
1108 /* the window can disappear while ProcessPendingEvents() runs */
1109 if (!wWindowFor(clientwin)) {
1110 return;
1113 #endif
1116 /* maybe we want to do this regardless of net_handle_icon
1117 * it seems to me we might break behaviour this way.
1119 if (wwin->flags.selected && !wPreferences.disable_miniwindows
1120 #ifdef NETWM_HINTS
1121 && !wwin->flags.net_handle_icon
1122 #endif
1124 wIconSelect(wwin->icon);
1126 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1128 if (wPreferences.auto_arrange_icons)
1129 wArrangeIcons(wwin->screen_ptr, True);
1132 void wDeiconifyWindow(WWindow * wwin)
1134 #ifdef NETWM_HINTS
1135 /* we're hiding for show_desktop */
1136 int netwm_hidden = wwin->flags.net_show_desktop &&
1137 wwin->frame->workspace != wwin->screen_ptr->current_workspace;
1138 #else
1139 int netwm_hidden = False;
1140 #endif
1142 if (!netwm_hidden)
1143 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1145 if (!wwin->flags.miniaturized)
1146 return;
1148 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1149 WWindow *owner = recursiveTransientFor(wwin);
1151 if (owner && owner->flags.miniaturized) {
1152 wDeiconifyWindow(owner);
1153 wSetFocusTo(wwin->screen_ptr, wwin);
1154 wRaiseFrame(wwin->frame->core);
1155 return;
1159 wwin->flags.miniaturized = 0;
1161 if (!netwm_hidden && !wwin->flags.shaded) {
1162 wwin->flags.mapped = 1;
1165 if (!netwm_hidden || wPreferences.sticky_icons) {
1166 /* maybe we want to do this regardless of net_handle_icon
1167 * it seems to me we might break behaviour this way.
1169 if (!wPreferences.disable_miniwindows
1170 #ifdef NETWM_HINTS
1171 && !wwin->flags.net_handle_icon
1172 #endif
1173 && wwin->icon != NULL) {
1174 if (wwin->icon->selected)
1175 wIconSelect(wwin->icon);
1177 XUnmapWindow(dpy, wwin->icon->core->window);
1181 if (!netwm_hidden)
1182 wSoundPlay(WSOUND_DEICONIFY);
1184 /* if the window is in another workspace, do it silently */
1185 if (!netwm_hidden) {
1186 #ifdef ANIMATIONS
1187 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations
1188 && !wwin->flags.skip_next_animation && wwin->icon != NULL) {
1189 int ix, iy, iw, ih;
1191 if (!wPreferences.disable_miniwindows
1192 #ifdef NETWM_HINTS
1193 && !wwin->flags.net_handle_icon
1194 #endif
1196 ix = wwin->icon_x;
1197 iy = wwin->icon_y;
1198 iw = wwin->icon->core->width;
1199 ih = wwin->icon->core->height;
1200 } else {
1201 #ifdef NETWM_HINTS
1202 if (wwin->flags.net_handle_icon) {
1203 ix = wwin->icon_x;
1204 iy = wwin->icon_y;
1205 iw = wwin->icon_w;
1206 ih = wwin->icon_h;
1207 } else
1208 #endif
1210 ix = 0;
1211 iy = 0;
1212 iw = wwin->screen_ptr->scr_width;
1213 ih = wwin->screen_ptr->scr_height;
1216 animateResize(wwin->screen_ptr, ix, iy, iw, ih,
1217 wwin->frame_x, wwin->frame_y,
1218 wwin->frame->core->width, wwin->frame->core->height, False);
1220 #endif /* ANIMATIONS */
1221 wwin->flags.skip_next_animation = 0;
1222 XGrabServer(dpy);
1223 if (!wwin->flags.shaded) {
1224 XMapWindow(dpy, wwin->client_win);
1226 XMapWindow(dpy, wwin->frame->core->window);
1227 wRaiseFrame(wwin->frame->core);
1228 if (!wwin->flags.shaded) {
1229 wClientSetState(wwin, NormalState, None);
1231 mapTransientsFor(wwin);
1234 if (!wPreferences.disable_miniwindows && wwin->icon != NULL
1235 #ifdef NETWM_HINTS
1236 && !wwin->flags.net_handle_icon
1237 #endif
1239 RemoveFromStackList(wwin->icon->core);
1240 /* removeIconGrabs(wwin->icon); */
1241 wIconDestroy(wwin->icon);
1242 wwin->icon = NULL;
1245 if (!netwm_hidden) {
1246 XUngrabServer(dpy);
1248 wSetFocusTo(wwin->screen_ptr, wwin);
1250 #ifdef ANIMATIONS
1251 if (!wwin->screen_ptr->flags.startup) {
1252 /* Catch up with events not processed while animation was running */
1253 Window clientwin = wwin->client_win;
1255 ProcessPendingEvents();
1257 /* the window can disappear while ProcessPendingEvents() runs */
1258 if (!wWindowFor(clientwin)) {
1259 return;
1262 #endif
1265 if (wPreferences.auto_arrange_icons) {
1266 wArrangeIcons(wwin->screen_ptr, True);
1269 WMPostNotificationName(WMNChangedState, wwin, "iconify");
1271 /* In case we were shaded and iconified, also unshade */
1272 if (!netwm_hidden)
1273 wUnshadeWindow(wwin);
1276 static void hideWindow(WIcon * icon, int icon_x, int icon_y, WWindow * wwin, int animate)
1278 if (wwin->flags.miniaturized) {
1279 if (wwin->icon) {
1280 XUnmapWindow(dpy, wwin->icon->core->window);
1281 wwin->icon->mapped = 0;
1283 wwin->flags.hidden = 1;
1285 WMPostNotificationName(WMNChangedState, wwin, "hide");
1286 return;
1289 if (wwin->flags.inspector_open) {
1290 wHideInspectorForWindow(wwin);
1293 wwin->flags.hidden = 1;
1294 wWindowUnmap(wwin);
1296 wClientSetState(wwin, IconicState, icon->icon_win);
1297 flushExpose();
1298 wSoundPlay(WSOUND_HIDE);
1299 #ifdef ANIMATIONS
1300 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations &&
1301 !wwin->flags.skip_next_animation && animate) {
1302 animateResize(wwin->screen_ptr, wwin->frame_x, wwin->frame_y,
1303 wwin->frame->core->width, wwin->frame->core->height,
1304 icon_x, icon_y, icon->core->width, icon->core->height, True);
1306 #endif
1307 wwin->flags.skip_next_animation = 0;
1309 WMPostNotificationName(WMNChangedState, wwin, "hide");
1312 void wHideOtherApplications(WWindow * awin)
1314 WWindow *wwin;
1315 WApplication *tapp;
1317 if (!awin)
1318 return;
1319 wwin = awin->screen_ptr->focused_window;
1321 while (wwin) {
1322 if (wwin != awin
1323 && wwin->frame->workspace == awin->screen_ptr->current_workspace
1324 && !(wwin->flags.miniaturized || wwin->flags.hidden)
1325 && !wwin->flags.internal_window
1326 && wGetWindowOfInspectorForWindow(wwin) != awin && !WFLAGP(wwin, no_hide_others)) {
1328 if (wwin->main_window == None || WFLAGP(wwin, no_appicon)) {
1329 if (!WFLAGP(wwin, no_miniaturizable)) {
1330 wwin->flags.skip_next_animation = 1;
1331 wIconifyWindow(wwin);
1333 } else if (wwin->main_window != None && awin->main_window != wwin->main_window) {
1334 tapp = wApplicationOf(wwin->main_window);
1335 if (tapp) {
1336 tapp->flags.skip_next_animation = 1;
1337 wHideApplication(tapp);
1338 } else {
1339 if (!WFLAGP(wwin, no_miniaturizable)) {
1340 wwin->flags.skip_next_animation = 1;
1341 wIconifyWindow(wwin);
1346 wwin = wwin->prev;
1349 wSetFocusTo(awin->screen_ptr, awin);
1353 void wHideApplication(WApplication * wapp)
1355 WScreen *scr;
1356 WWindow *wlist;
1357 int hadfocus;
1358 int animate;
1360 if (!wapp) {
1361 wwarning("trying to hide a non grouped window");
1362 return;
1364 if (!wapp->main_window_desc) {
1365 wwarning("group leader not found for window group");
1366 return;
1368 scr = wapp->main_window_desc->screen_ptr;
1369 hadfocus = 0;
1370 wlist = scr->focused_window;
1371 if (!wlist)
1372 return;
1374 if (wlist->main_window == wapp->main_window)
1375 wapp->last_focused = wlist;
1376 else
1377 wapp->last_focused = NULL;
1379 animate = !wapp->flags.skip_next_animation;
1381 while (wlist) {
1382 if (wlist->main_window == wapp->main_window) {
1383 if (wlist->flags.focused) {
1384 hadfocus = 1;
1386 if (wapp->app_icon) {
1387 hideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1388 wapp->app_icon->y_pos, wlist, animate);
1389 animate = False;
1392 wlist = wlist->prev;
1395 wapp->flags.skip_next_animation = 0;
1397 if (hadfocus) {
1398 if (wPreferences.focus_mode == WKF_CLICK) {
1399 wlist = scr->focused_window;
1400 while (wlist) {
1401 if (!WFLAGP(wlist, no_focusable) && !wlist->flags.hidden
1402 && (wlist->flags.mapped || wlist->flags.shaded))
1403 break;
1404 wlist = wlist->prev;
1406 wSetFocusTo(scr, wlist);
1407 } else {
1408 wSetFocusTo(scr, NULL);
1412 wapp->flags.hidden = 1;
1414 if (wPreferences.auto_arrange_icons) {
1415 wArrangeIcons(scr, True);
1417 #ifdef HIDDENDOT
1418 if (wapp->app_icon)
1419 wAppIconPaint(wapp->app_icon);
1420 #endif
1423 static void unhideWindow(WIcon * icon, int icon_x, int icon_y, WWindow * wwin, int animate, int bringToCurrentWS)
1425 if (bringToCurrentWS)
1426 wWindowChangeWorkspace(wwin, wwin->screen_ptr->current_workspace);
1428 wwin->flags.hidden = 0;
1430 wSoundPlay(WSOUND_UNHIDE);
1431 #ifdef ANIMATIONS
1432 if (!wwin->screen_ptr->flags.startup && !wPreferences.no_animations && animate) {
1433 animateResize(wwin->screen_ptr, icon_x, icon_y,
1434 icon->core->width, icon->core->height,
1435 wwin->frame_x, wwin->frame_y,
1436 wwin->frame->core->width, wwin->frame->core->height, True);
1438 #endif
1439 wwin->flags.skip_next_animation = 0;
1440 if (wwin->screen_ptr->current_workspace == wwin->frame->workspace) {
1441 XMapWindow(dpy, wwin->client_win);
1442 XMapWindow(dpy, wwin->frame->core->window);
1443 wClientSetState(wwin, NormalState, None);
1444 wwin->flags.mapped = 1;
1445 wRaiseFrame(wwin->frame->core);
1447 if (wwin->flags.inspector_open) {
1448 wUnhideInspectorForWindow(wwin);
1451 WMPostNotificationName(WMNChangedState, wwin, "hide");
1454 void wUnhideApplication(WApplication * wapp, Bool miniwindows, Bool bringToCurrentWS)
1456 WScreen *scr;
1457 WWindow *wlist, *next;
1458 WWindow *focused = NULL;
1459 int animate;
1461 if (!wapp)
1462 return;
1464 scr = wapp->main_window_desc->screen_ptr;
1465 wlist = scr->focused_window;
1466 if (!wlist)
1467 return;
1469 /* goto beginning of list */
1470 while (wlist->prev)
1471 wlist = wlist->prev;
1473 animate = !wapp->flags.skip_next_animation;
1475 while (wlist) {
1476 next = wlist->next;
1478 if (wlist->main_window == wapp->main_window) {
1479 if (wlist->flags.focused)
1480 focused = wlist;
1481 else if (!focused || !focused->flags.focused)
1482 focused = wlist;
1484 if (wlist->flags.miniaturized) {
1485 if ((bringToCurrentWS || wPreferences.sticky_icons ||
1486 wlist->frame->workspace == scr->current_workspace) && wlist->icon) {
1487 if (!wlist->icon->mapped) {
1488 int x, y;
1490 PlaceIcon(scr, &x, &y, wGetHeadForWindow(wlist));
1491 if (wlist->icon_x != x || wlist->icon_y != y) {
1492 XMoveWindow(dpy, wlist->icon->core->window, x, y);
1494 wlist->icon_x = x;
1495 wlist->icon_y = y;
1496 XMapWindow(dpy, wlist->icon->core->window);
1497 wlist->icon->mapped = 1;
1499 wRaiseFrame(wlist->icon->core);
1501 if (bringToCurrentWS)
1502 wWindowChangeWorkspace(wlist, scr->current_workspace);
1503 wlist->flags.hidden = 0;
1504 if (miniwindows && wlist->frame->workspace == scr->current_workspace) {
1505 wDeiconifyWindow(wlist);
1507 WMPostNotificationName(WMNChangedState, wlist, "hide");
1508 } else if (wlist->flags.shaded) {
1509 if (bringToCurrentWS)
1510 wWindowChangeWorkspace(wlist, scr->current_workspace);
1511 wlist->flags.hidden = 0;
1512 if (wlist->frame->workspace == scr->current_workspace) {
1513 XMapWindow(dpy, wlist->frame->core->window);
1514 if (miniwindows) {
1515 wUnshadeWindow(wlist);
1516 wRaiseFrame(wlist->frame->core);
1519 WMPostNotificationName(WMNChangedState, wlist, "hide");
1520 } else if (wlist->flags.hidden) {
1521 unhideWindow(wapp->app_icon->icon, wapp->app_icon->x_pos,
1522 wapp->app_icon->y_pos, wlist, animate, bringToCurrentWS);
1523 animate = False;
1524 } else {
1525 if (bringToCurrentWS && wlist->frame->workspace != scr->current_workspace) {
1526 wWindowChangeWorkspace(wlist, scr->current_workspace);
1528 wRaiseFrame(wlist->frame->core);
1531 wlist = next;
1534 wapp->flags.skip_next_animation = 0;
1535 wapp->flags.hidden = 0;
1537 if (wapp->last_focused && wapp->last_focused->flags.mapped) {
1538 wRaiseFrame(wapp->last_focused->frame->core);
1539 wSetFocusTo(scr, wapp->last_focused);
1540 } else if (focused) {
1541 wSetFocusTo(scr, focused);
1543 wapp->last_focused = NULL;
1544 if (wPreferences.auto_arrange_icons) {
1545 wArrangeIcons(scr, True);
1547 #ifdef HIDDENDOT
1548 wAppIconPaint(wapp->app_icon);
1549 #endif
1552 void wShowAllWindows(WScreen * scr)
1554 WWindow *wwin, *old_foc;
1555 WApplication *wapp;
1557 old_foc = wwin = scr->focused_window;
1558 while (wwin) {
1559 if (!wwin->flags.internal_window &&
1560 (scr->current_workspace == wwin->frame->workspace || IS_OMNIPRESENT(wwin))) {
1561 if (wwin->flags.miniaturized) {
1562 wwin->flags.skip_next_animation = 1;
1563 wDeiconifyWindow(wwin);
1564 } else if (wwin->flags.hidden) {
1565 wapp = wApplicationOf(wwin->main_window);
1566 if (wapp) {
1567 wUnhideApplication(wapp, False, False);
1568 } else {
1569 wwin->flags.skip_next_animation = 1;
1570 wDeiconifyWindow(wwin);
1574 wwin = wwin->prev;
1576 wSetFocusTo(scr, old_foc);
1577 /*wRaiseFrame(old_foc->frame->core); */
1580 void wRefreshDesktop(WScreen * scr)
1582 Window win;
1583 XSetWindowAttributes attr;
1585 attr.backing_store = NotUseful;
1586 attr.save_under = False;
1587 win = XCreateWindow(dpy, scr->root_win, 0, 0, scr->scr_width,
1588 scr->scr_height, 0, CopyFromParent, CopyFromParent,
1589 (Visual *) CopyFromParent, CWBackingStore | CWSaveUnder, &attr);
1590 XMapRaised(dpy, win);
1591 XDestroyWindow(dpy, win);
1592 XFlush(dpy);
1595 void wArrangeIcons(WScreen * scr, Bool arrangeAll)
1597 WWindow *wwin;
1598 WAppIcon *aicon;
1600 int head;
1601 const int heads = wXineramaHeads(scr);
1603 struct HeadVars {
1604 int pf; /* primary axis */
1605 int sf; /* secondary axis */
1606 int fullW;
1607 int fullH;
1608 int pi, si;
1609 int sx1, sx2, sy1, sy2; /* screen boundary */
1610 int sw, sh;
1611 int xo, yo;
1612 int xs, ys;
1613 } *vars;
1615 int isize = wPreferences.icon_size;
1617 vars = (struct HeadVars *)wmalloc(sizeof(struct HeadVars) * heads);
1619 for (head = 0; head < heads; ++head) {
1620 #if 0
1621 WMRect rect = wGetRectForHead(scr, head);
1622 #else
1623 WArea area = wGetUsableAreaForHead(scr, head, NULL, False);
1624 WMRect rect = wmkrect(area.x1, area.y1, area.x2 - area.x1, area.y2 - area.y1);
1625 #endif
1627 vars[head].pi = vars[head].si = 0;
1628 vars[head].sx1 = rect.pos.x;
1629 vars[head].sy1 = rect.pos.y;
1630 vars[head].sw = rect.size.width;
1631 vars[head].sh = rect.size.height;
1632 vars[head].sx2 = vars[head].sx1 + vars[head].sw;
1633 vars[head].sy2 = vars[head].sy1 + vars[head].sh;
1635 #if 0
1636 if (scr->dock) {
1637 if (scr->dock->on_right_side)
1638 vars[head].sx2 -= isize + DOCK_EXTRA_SPACE;
1639 else
1640 vars[head].sx1 += isize + DOCK_EXTRA_SPACE;
1642 #endif
1644 vars[head].sw = isize * (vars[head].sw / isize);
1645 vars[head].sh = isize * (vars[head].sh / isize);
1646 vars[head].fullW = (vars[head].sx2 - vars[head].sx1) / isize;
1647 vars[head].fullH = (vars[head].sy2 - vars[head].sy1) / isize;
1649 /* icon yard boundaries */
1650 if (wPreferences.icon_yard & IY_VERT) {
1651 vars[head].pf = vars[head].fullH;
1652 vars[head].sf = vars[head].fullW;
1653 } else {
1654 vars[head].pf = vars[head].fullW;
1655 vars[head].sf = vars[head].fullH;
1657 if (wPreferences.icon_yard & IY_RIGHT) {
1658 vars[head].xo = vars[head].sx2 - isize;
1659 vars[head].xs = -1;
1660 } else {
1661 vars[head].xo = vars[head].sx1;
1662 vars[head].xs = 1;
1664 if (wPreferences.icon_yard & IY_TOP) {
1665 vars[head].yo = vars[head].sy1;
1666 vars[head].ys = 1;
1667 } else {
1668 vars[head].yo = vars[head].sy2 - isize;
1669 vars[head].ys = -1;
1673 #define X ((wPreferences.icon_yard & IY_VERT) \
1674 ? vars[head].xo + vars[head].xs*(vars[head].si*isize) \
1675 : vars[head].xo + vars[head].xs*(vars[head].pi*isize))
1677 #define Y ((wPreferences.icon_yard & IY_VERT) \
1678 ? vars[head].yo + vars[head].ys*(vars[head].pi*isize) \
1679 : vars[head].yo + vars[head].ys*(vars[head].si*isize))
1681 /* arrange application icons */
1682 aicon = scr->app_icon_list;
1683 /* reverse them to avoid unnecessarily sliding of icons */
1684 while (aicon && aicon->next)
1685 aicon = aicon->next;
1687 while (aicon) {
1688 if (!aicon->docked) {
1689 /* CHECK: can icon be NULL here ? */
1690 /* The intention here is to place the AppIcon on the head that
1691 * contains most of the applications _main_ window. */
1692 head = wGetHeadForWindow(aicon->icon->owner);
1694 if (aicon->x_pos != X || aicon->y_pos != Y) {
1695 #ifdef ANIMATIONS
1696 if (!wPreferences.no_animations) {
1697 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1699 #endif /* ANIMATIONS */
1701 wAppIconMove(aicon, X, Y);
1702 vars[head].pi++;
1703 if (vars[head].pi >= vars[head].pf) {
1704 vars[head].pi = 0;
1705 vars[head].si++;
1708 aicon = aicon->prev;
1711 /* arrange miniwindows */
1712 wwin = scr->focused_window;
1713 /* reverse them to avoid unnecessarily shuffling */
1714 while (wwin && wwin->prev)
1715 wwin = wwin->prev;
1717 while (wwin) {
1718 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1719 (wwin->frame->workspace == scr->current_workspace ||
1720 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1722 head = wGetHeadForWindow(wwin);
1724 if (arrangeAll || !wwin->flags.icon_moved) {
1725 if (wwin->icon_x != X || wwin->icon_y != Y) {
1726 #ifdef ANIMATIONS
1727 if (wPreferences.no_animations) {
1728 XMoveWindow(dpy, wwin->icon->core->window, X, Y);
1729 } else {
1730 SlideWindow(wwin->icon->core->window, wwin->icon_x,
1731 wwin->icon_y, X, Y);
1733 #else
1734 XMoveWindow(dpy, wwin->icon->core->window, X, Y);
1735 #endif /* ANIMATIONS */
1737 wwin->icon_x = X;
1738 wwin->icon_y = Y;
1740 vars[head].pi++;
1741 if (vars[head].pi >= vars[head].pf) {
1742 vars[head].pi = 0;
1743 vars[head].si++;
1747 if (arrangeAll) {
1748 wwin->flags.icon_moved = 0;
1750 /* we reversed the order, so we use next */
1751 wwin = wwin->next;
1754 wfree(vars);
1757 #if 0
1758 void wArrangeIcons(WScreen * scr, Bool arrangeAll)
1760 WWindow *wwin;
1761 WAppIcon *aicon;
1762 int pf; /* primary axis */
1763 int sf; /* secondary axis */
1764 int fullW;
1765 int fullH;
1766 int pi, si;
1767 int sx1, sx2, sy1, sy2; /* screen boundary */
1768 int sw, sh;
1769 int xo, yo;
1770 int xs, ys;
1771 int isize = wPreferences.icon_size;
1774 * Find out screen boundaries.
1778 * Allows each head to have miniwindows
1780 WMRect rect = wGetRectForHead(scr, wGetHeadForPointerLocation(scr));
1782 sx1 = rect.pos.x;
1783 sy1 = rect.pos.y;
1784 sw = rect.size.width;
1785 sh = rect.size.height;
1786 sx2 = sx1 + sw;
1787 sy2 = sy1 + sh;
1788 if (scr->dock) {
1789 if (scr->dock->on_right_side)
1790 sx2 -= isize + DOCK_EXTRA_SPACE;
1791 else
1792 sx1 += isize + DOCK_EXTRA_SPACE;
1794 #if 0
1795 sw = isize * (scr->scr_width / isize);
1796 sh = isize * (scr->scr_height / isize);
1797 #else
1798 sw = isize * (sw / isize);
1799 sh = isize * (sh / isize);
1800 #endif
1801 fullW = (sx2 - sx1) / isize;
1802 fullH = (sy2 - sy1) / isize;
1804 /* icon yard boundaries */
1805 if (wPreferences.icon_yard & IY_VERT) {
1806 pf = fullH;
1807 sf = fullW;
1808 } else {
1809 pf = fullW;
1810 sf = fullH;
1812 if (wPreferences.icon_yard & IY_RIGHT) {
1813 xo = sx2 - isize;
1814 xs = -1;
1815 } else {
1816 xo = sx1;
1817 xs = 1;
1819 if (wPreferences.icon_yard & IY_TOP) {
1820 yo = sy1;
1821 ys = 1;
1822 } else {
1823 yo = sy2 - isize;
1824 ys = -1;
1827 /* arrange icons putting the most recently focused window
1828 * as the last icon */
1829 #define X ((wPreferences.icon_yard & IY_VERT) ? xo + xs*(si*isize)\
1830 : xo + xs*(pi*isize))
1831 #define Y ((wPreferences.icon_yard & IY_VERT) ? yo + ys*(pi*isize)\
1832 : yo + ys*(si*isize))
1834 /* arrange application icons */
1835 aicon = scr->app_icon_list;
1836 /* reverse them to avoid unnecessarily sliding of icons */
1837 while (aicon && aicon->next)
1838 aicon = aicon->next;
1840 pi = 0;
1841 si = 0;
1842 while (aicon) {
1843 if (!aicon->docked) {
1844 if (aicon->x_pos != X || aicon->y_pos != Y) {
1845 #ifdef ANIMATIONS
1846 if (!wPreferences.no_animations) {
1847 SlideWindow(aicon->icon->core->window, aicon->x_pos, aicon->y_pos, X, Y);
1849 #endif /* ANIMATIONS */
1851 wAppIconMove(aicon, X, Y);
1852 pi++;
1854 /* we reversed the order so we use prev */
1855 aicon = aicon->prev;
1856 if (pi >= pf) {
1857 pi = 0;
1858 si++;
1862 /* arrange miniwindows */
1864 wwin = scr->focused_window;
1865 /* reverse them to avoid unnecessarily shuffling */
1866 while (wwin && wwin->prev)
1867 wwin = wwin->prev;
1869 while (wwin) {
1870 if (wwin->icon && wwin->flags.miniaturized && !wwin->flags.hidden &&
1871 (wwin->frame->workspace == scr->current_workspace ||
1872 IS_OMNIPRESENT(wwin) || wPreferences.sticky_icons)) {
1874 if (arrangeAll || !wwin->flags.icon_moved) {
1875 if (wwin->icon_x != X || wwin->icon_y != Y) {
1876 #ifdef ANIMATIONS
1877 if (wPreferences.no_animations) {
1878 XMoveWindow(dpy, wwin->icon->core->window, X, Y);
1879 } else {
1880 SlideWindow(wwin->icon->core->window, wwin->icon_x,
1881 wwin->icon_y, X, Y);
1883 #else
1884 XMoveWindow(dpy, wwin->icon->core->window, X, Y);
1885 #endif /* ANIMATIONS */
1887 wwin->icon_x = X;
1888 wwin->icon_y = Y;
1889 pi++;
1892 if (arrangeAll) {
1893 wwin->flags.icon_moved = 0;
1895 /* we reversed the order, so we use next */
1896 wwin = wwin->next;
1897 if (pi >= pf) {
1898 pi = 0;
1899 si++;
1903 #endif
1905 void wSelectWindow(WWindow * wwin, Bool flag)
1907 WScreen *scr = wwin->screen_ptr;
1909 if (flag) {
1910 wwin->flags.selected = 1;
1911 XSetWindowBorder(dpy, wwin->frame->core->window, scr->white_pixel);
1913 if (!HAS_BORDER(wwin)) {
1914 XSetWindowBorderWidth(dpy, wwin->frame->core->window, FRAME_BORDER_WIDTH);
1917 if (!scr->selected_windows)
1918 scr->selected_windows = WMCreateArray(4);
1919 WMAddToArray(scr->selected_windows, wwin);
1920 } else {
1921 wwin->flags.selected = 0;
1922 XSetWindowBorder(dpy, wwin->frame->core->window, scr->frame_border_pixel);
1924 if (!HAS_BORDER(wwin)) {
1925 XSetWindowBorderWidth(dpy, wwin->frame->core->window, 0);
1928 if (scr->selected_windows) {
1929 WMRemoveFromArray(scr->selected_windows, wwin);
1934 void wMakeWindowVisible(WWindow * wwin)
1936 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
1937 wWorkspaceChange(wwin->screen_ptr, wwin->frame->workspace);
1939 if (wwin->flags.shaded) {
1940 wUnshadeWindow(wwin);
1942 if (wwin->flags.hidden) {
1943 WApplication *app;
1945 app = wApplicationOf(wwin->main_window);
1946 if (app) {
1947 /* trick to get focus to this window */
1948 app->last_focused = wwin;
1949 wUnhideApplication(app, False, False);
1952 if (wwin->flags.miniaturized) {
1953 wDeiconifyWindow(wwin);
1954 } else {
1955 if (!WFLAGP(wwin, no_focusable))
1956 wSetFocusTo(wwin->screen_ptr, wwin);
1957 wRaiseFrame(wwin->frame->core);
1962 * Do the animation while shading (called with what = SHADE)
1963 * or unshading (what = UNSHADE).
1965 #ifdef ANIMATIONS
1966 static void shade_animate(WWindow *wwin, Bool what)
1968 int y, s, w, h;
1969 time_t time0 = time(NULL);
1971 if (wwin->flags.skip_next_animation && wPreferences.no_animations)
1972 return;
1974 switch(what) {
1975 case SHADE:
1976 if (!wwin->screen_ptr->flags.startup) {
1977 /* do the shading animation */
1978 h = wwin->frame->core->height;
1979 s = h / SHADE_STEPS;
1980 if (s < 1)
1981 s = 1;
1982 w = wwin->frame->core->width;
1983 y = wwin->frame->top_width;
1984 while (h > wwin->frame->top_width + 1) {
1985 XMoveWindow(dpy, wwin->client_win, 0, y);
1986 XResizeWindow(dpy, wwin->frame->core->window, w, h);
1987 XFlush(dpy);
1989 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
1990 break;
1992 if (SHADE_DELAY > 0) {
1993 wusleep(SHADE_DELAY * 1000L);
1994 } else {
1995 wusleep(10);
1997 h -= s;
1998 y -= s;
2000 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2002 break;
2004 case UNSHADE:
2005 h = wwin->frame->top_width + wwin->frame->bottom_width;
2006 y = wwin->frame->top_width - wwin->client.height;
2007 s = abs(y) / SHADE_STEPS;
2008 if (s < 1)
2009 s = 1;
2010 w = wwin->frame->core->width;
2011 XMoveWindow(dpy, wwin->client_win, 0, y);
2012 if (s > 0) {
2013 while (h < wwin->client.height + wwin->frame->top_width + wwin->frame->bottom_width) {
2014 XResizeWindow(dpy, wwin->frame->core->window, w, h);
2015 XMoveWindow(dpy, wwin->client_win, 0, y);
2016 XFlush(dpy);
2017 if (SHADE_DELAY > 0) {
2018 wusleep(SHADE_DELAY * 2000L / 3);
2019 } else {
2020 wusleep(10);
2022 h += s;
2023 y += s;
2025 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
2026 break;
2029 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2030 break;
2033 #else
2034 static void shade_animate(WWindow *wwin, Bool what) { return; }
2035 #endif /* ANIMATIONS */