shortcutWindows moved to w_global
[wmaker-crm.git] / src / superfluous.c
blobe33e6eb7a0e27a7216546e5f2c849dcaec269dcb
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 * Copyright (c) 1998-2003 Dan Pascu
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <math.h>
30 #include <time.h>
32 #include <wraster.h>
34 #include "WindowMaker.h"
35 #include "screen.h"
36 #include "superfluous.h"
37 #include "framewin.h"
38 #include "window.h"
39 #include "actions.h"
40 #include "xinerama.h"
41 #include "stacking.h"
43 #define PIECES ((64/ICON_KABOOM_PIECE_SIZE)*(64/ICON_KABOOM_PIECE_SIZE))
44 #define KAB_PRECISION 4
45 #define BOUNCE_HZ 25
46 #define BOUNCE_DELAY (1000/BOUNCE_HZ)
47 #define BOUNCE_HEIGHT 24
48 #define BOUNCE_LENGTH 0.3
49 #define BOUNCE_DAMP 0.6
50 #define URGENT_BOUNCE_DELAY 3000
53 void DoKaboom(WScreen * scr, Window win, int x, int y)
55 #ifdef NORMAL_ICON_KABOOM
56 int i, j, k;
57 int sw = scr->scr_width, sh = scr->scr_height;
58 int px[PIECES];
59 short py[PIECES];
60 char pvx[PIECES], pvy[PIECES];
61 /* in MkLinux/PPC gcc seems to think that char is unsigned? */
62 signed char ax[PIECES], ay[PIECES];
63 Pixmap tmp;
65 XSetClipMask(dpy, scr->copy_gc, None);
66 tmp = XCreatePixmap(dpy, scr->root_win, wPreferences.icon_size, wPreferences.icon_size, scr->depth);
67 if (scr->w_visual == DefaultVisual(dpy, scr->screen))
68 XCopyArea(dpy, win, tmp, scr->copy_gc, 0, 0, wPreferences.icon_size, wPreferences.icon_size, 0, 0);
69 else {
70 XImage *image;
72 image = XGetImage(dpy, win, 0, 0, wPreferences.icon_size,
73 wPreferences.icon_size, AllPlanes, ZPixmap);
74 if (!image) {
75 XUnmapWindow(dpy, win);
76 return;
78 XPutImage(dpy, tmp, scr->copy_gc, image, 0, 0, 0, 0,
79 wPreferences.icon_size, wPreferences.icon_size);
80 XDestroyImage(image);
83 for (i = 0, k = 0; i < wPreferences.icon_size / ICON_KABOOM_PIECE_SIZE && k < PIECES; i++) {
84 for (j = 0; j < wPreferences.icon_size / ICON_KABOOM_PIECE_SIZE && k < PIECES; j++) {
85 if (rand() % 2) {
86 ax[k] = i;
87 ay[k] = j;
88 px[k] = (x + i * ICON_KABOOM_PIECE_SIZE) << KAB_PRECISION;
89 py[k] = y + j * ICON_KABOOM_PIECE_SIZE;
90 pvx[k] = rand() % (1 << (KAB_PRECISION + 3)) - (1 << (KAB_PRECISION + 3)) / 2;
91 pvy[k] = -15 - rand() % 7;
92 k++;
93 } else {
94 ax[k] = -1;
99 XUnmapWindow(dpy, win);
101 j = k;
102 while (k > 0) {
103 XEvent foo;
105 if (XCheckTypedEvent(dpy, ButtonPress, &foo)) {
106 XPutBackEvent(dpy, &foo);
107 XClearWindow(dpy, scr->root_win);
108 break;
111 for (i = 0; i < j; i++) {
112 if (ax[i] >= 0) {
113 int _px = px[i] >> KAB_PRECISION;
114 XClearArea(dpy, scr->root_win, _px, py[i],
115 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE, False);
116 px[i] += pvx[i];
117 py[i] += pvy[i];
118 _px = px[i] >> KAB_PRECISION;
119 pvy[i]++;
120 if (_px < -wPreferences.icon_size || _px > sw || py[i] >= sh) {
121 ax[i] = -1;
122 k--;
123 } else {
124 XCopyArea(dpy, tmp, scr->root_win, scr->copy_gc,
125 ax[i] * ICON_KABOOM_PIECE_SIZE, ay[i] * ICON_KABOOM_PIECE_SIZE,
126 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE, _px, py[i]);
131 XFlush(dpy);
132 wusleep(MINIATURIZE_ANIMATION_DELAY_Z * 2);
135 XFreePixmap(dpy, tmp);
136 #endif /* NORMAL_ICON_KABOOM */
139 Pixmap MakeGhostIcon(WScreen * scr, Drawable drawable)
141 RImage *back;
142 RColor color;
143 Pixmap pixmap;
145 if (!drawable)
146 return None;
148 back = RCreateImageFromDrawable(scr->rcontext, drawable, None);
149 if (!back)
150 return None;
152 color.red = 0xff;
153 color.green = 0xff;
154 color.blue = 0xff;
155 color.alpha = 200;
157 RClearImage(back, &color);
158 RConvertImage(scr->rcontext, back, &pixmap);
160 RReleaseImage(back);
162 return pixmap;
165 void DoWindowBirth(WWindow *wwin)
167 #ifdef WINDOW_BIRTH_ZOOM
168 int center_x, center_y;
169 int width = wwin->frame->core->width;
170 int height = wwin->frame->core->height;
171 int w = WMIN(width, 20);
172 int h = WMIN(height, 20);
173 WScreen *scr = wwin->screen_ptr;
175 center_x = wwin->frame_x + (width - w) / 2;
176 center_y = wwin->frame_y + (height - h) / 2;
178 animateResize(scr, center_x, center_y, 1, 1, wwin->frame_x, wwin->frame_y, width, height);
179 #endif
182 typedef struct AppBouncerData {
183 WApplication *wapp;
184 int count;
185 int pow;
186 int dir;
187 WMHandlerID *timer;
188 } AppBouncerData;
190 static void doAppBounce(void *arg)
192 AppBouncerData *data = (AppBouncerData*)arg;
193 WAppIcon *aicon = data->wapp->app_icon;
195 reinit:
196 if (aicon && data->wapp->refcount > 1) {
197 if (wPreferences.raise_appicons_when_bouncing)
198 XRaiseWindow(dpy, aicon->icon->core->window);
200 const double ticks = BOUNCE_HZ * BOUNCE_LENGTH;
201 const double s = sqrt(BOUNCE_HEIGHT)/(ticks/2);
202 double h = BOUNCE_HEIGHT*pow(BOUNCE_DAMP, data->pow);
203 double sqrt_h = sqrt(h);
204 if (h > 3) {
205 double offset, x = s * data->count - sqrt_h;
206 if (x > sqrt_h) {
207 ++data->pow;
208 data->count = 0;
209 goto reinit;
210 } else ++data->count;
211 offset = h - x*x;
213 switch (data->dir) {
214 case 0: /* left, bounce to right */
215 XMoveWindow(dpy, aicon->icon->core->window,
216 aicon->x_pos + (int)offset, aicon->y_pos);
217 break;
218 case 1: /* right, bounce to left */
219 XMoveWindow(dpy, aicon->icon->core->window,
220 aicon->x_pos - (int)offset, aicon->y_pos);
221 break;
222 case 2: /* top, bounce down */
223 XMoveWindow(dpy, aicon->icon->core->window,
224 aicon->x_pos, aicon->y_pos + (int)offset);
225 break;
226 case 3: /* bottom, bounce up */
227 XMoveWindow(dpy, aicon->icon->core->window,
228 aicon->x_pos, aicon->y_pos - (int)offset);
229 break;
231 return;
235 XMoveWindow(dpy, aicon->icon->core->window,
236 aicon->x_pos, aicon->y_pos);
237 CommitStackingForWindow(aicon->icon->core);
238 data->wapp->flags.bouncing = 0;
239 WMDeleteTimerHandler(data->timer);
240 wApplicationDestroy(data->wapp);
241 free(data);
244 static int bounceDirection(WAppIcon *aicon)
246 enum { left_e = 1, right_e = 2, top_e = 4, bottom_e = 8 };
248 WScreen *scr = aicon->icon->core->screen_ptr;
249 WMRect rr, sr;
250 int l, r, t, b, h, v;
251 int dir = 0;
253 rr.pos.x = aicon->x_pos;
254 rr.pos.y = aicon->y_pos;
255 rr.size.width = rr.size.height = 64;
257 sr = wGetRectForHead(scr, wGetHeadForRect(scr, rr));
259 l = rr.pos.x - sr.pos.x;
260 r = sr.pos.x + sr.size.width - rr.pos.x - rr.size.width;
261 t = rr.pos.y - sr.pos.y;
262 b = sr.pos.y + sr.size.height - rr.pos.y - rr.size.height;
264 if (l < r) {
265 dir |= left_e;
266 h = l;
267 } else {
268 dir |= right_e;
269 h = r;
272 if (t < b) {
273 dir |= top_e;
274 v = t;
275 } else {
276 dir |= bottom_e;
277 v = b;
280 if (aicon->dock && abs(aicon->xindex) != abs(aicon->yindex)) {
281 if (abs(aicon->xindex) < abs(aicon->yindex)) dir &= ~(top_e | bottom_e);
282 else dir &= ~(left_e | right_e);
283 } else {
284 if (h < v) dir &= ~(top_e | bottom_e);
285 else dir &= ~(left_e | right_e);
288 switch (dir) {
289 case left_e:
290 dir = 0;
291 break;
293 case right_e:
294 dir = 1;
295 break;
297 case top_e:
298 dir = 2;
299 break;
301 case bottom_e:
302 dir = 3;
303 break;
305 default:
306 wwarning(_("Impossible direction: %d"), dir);
307 dir = 3;
308 break;
311 return dir;
314 void wAppBounce(WApplication *wapp)
316 if (!wPreferences.no_animations && wapp->app_icon && !wapp->flags.bouncing
317 && !wPreferences.do_not_make_appicons_bounce) {
318 ++wapp->refcount;
319 wapp->flags.bouncing = 1;
321 AppBouncerData *data = (AppBouncerData *)malloc(sizeof(AppBouncerData));
322 data->wapp = wapp;
323 data->count = data->pow = 0;
324 data->dir = bounceDirection(wapp->app_icon);
325 data->timer = WMAddPersistentTimerHandler(BOUNCE_DELAY, doAppBounce, data);
329 static int appIsUrgent(WApplication *wapp)
331 WScreen *scr;
332 WWindow *wlist;
334 if (!wapp->main_window_desc) {
335 wwarning("group leader not found for window group");
336 return 0;
338 scr = wapp->main_window_desc->screen_ptr;
339 wlist = scr->focused_window;
340 if (!wlist)
341 return 0;
343 while (wlist) {
344 if (wlist->main_window == wapp->main_window) {
345 if (wlist->flags.urgent)
346 return 1;
348 wlist = wlist->prev;
351 return 0;
354 static void doAppUrgentBounce(void *arg)
356 WApplication *wapp = (WApplication *)arg;
358 if (appIsUrgent(wapp)) {
359 if(wPreferences.bounce_appicons_when_urgent) wAppBounce(wapp);
360 } else {
361 WMDeleteTimerHandler(wapp->urgent_bounce_timer);
362 wapp->urgent_bounce_timer = NULL;
366 void wAppBounceWhileUrgent(WApplication *wapp)
368 if (!wapp) return;
369 if (appIsUrgent(wapp)) {
370 if (!wapp->urgent_bounce_timer) {
371 wapp->urgent_bounce_timer = WMAddPersistentTimerHandler(URGENT_BOUNCE_DELAY, doAppUrgentBounce, wapp);
372 doAppUrgentBounce(wapp);
374 } else {
375 if (wapp->urgent_bounce_timer) {
376 WMDeleteTimerHandler(wapp->urgent_bounce_timer);
377 wapp->urgent_bounce_timer = NULL;