Option to raise bouncing appicons
[wmaker-crm.git] / src / superfluous.c
blobb2415024929f06e5060e0b4341850b61fa82fdb3
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
28 #include <stdlib.h>
29 #include <unistd.h>
30 #include <math.h>
31 #include <time.h>
33 #include <wraster.h>
35 #include "WindowMaker.h"
36 #include "screen.h"
37 #include "dock.h"
38 #include "superfluous.h"
39 #include "framewin.h"
40 #include "window.h"
41 #include "actions.h"
42 #include "xinerama.h"
44 extern WPreferences wPreferences;
46 #ifdef NORMAL_ICON_KABOOM
47 void DoKaboom(WScreen * scr, Window win, int x, int y)
49 int i, j, k;
50 int sw = scr->scr_width, sh = scr->scr_height;
51 #define KAB_PRECISION 4
52 int px[PIECES];
53 short py[PIECES];
54 char pvx[PIECES], pvy[PIECES];
55 /* in MkLinux/PPC gcc seems to think that char is unsigned? */
56 signed char ax[PIECES], ay[PIECES];
57 Pixmap tmp;
59 XSetClipMask(dpy, scr->copy_gc, None);
60 tmp = XCreatePixmap(dpy, scr->root_win, wPreferences.icon_size, wPreferences.icon_size, scr->depth);
61 if (scr->w_visual == DefaultVisual(dpy, scr->screen))
62 XCopyArea(dpy, win, tmp, scr->copy_gc, 0, 0, wPreferences.icon_size, wPreferences.icon_size, 0, 0);
63 else {
64 XImage *image;
66 image = XGetImage(dpy, win, 0, 0, wPreferences.icon_size,
67 wPreferences.icon_size, AllPlanes, ZPixmap);
68 if (!image) {
69 XUnmapWindow(dpy, win);
70 return;
72 XPutImage(dpy, tmp, scr->copy_gc, image, 0, 0, 0, 0,
73 wPreferences.icon_size, wPreferences.icon_size);
74 XDestroyImage(image);
77 for (i = 0, k = 0; i < wPreferences.icon_size / ICON_KABOOM_PIECE_SIZE && k < PIECES; i++) {
78 for (j = 0; j < wPreferences.icon_size / ICON_KABOOM_PIECE_SIZE && k < PIECES; j++) {
79 if (rand() % 2) {
80 ax[k] = i;
81 ay[k] = j;
82 px[k] = (x + i * ICON_KABOOM_PIECE_SIZE) << KAB_PRECISION;
83 py[k] = y + j * ICON_KABOOM_PIECE_SIZE;
84 pvx[k] = rand() % (1 << (KAB_PRECISION + 3)) - (1 << (KAB_PRECISION + 3)) / 2;
85 pvy[k] = -15 - rand() % 7;
86 k++;
87 } else {
88 ax[k] = -1;
93 XUnmapWindow(dpy, win);
95 j = k;
96 while (k > 0) {
97 XEvent foo;
99 if (XCheckTypedEvent(dpy, ButtonPress, &foo)) {
100 XPutBackEvent(dpy, &foo);
101 XClearWindow(dpy, scr->root_win);
102 break;
105 for (i = 0; i < j; i++) {
106 if (ax[i] >= 0) {
107 int _px = px[i] >> KAB_PRECISION;
108 XClearArea(dpy, scr->root_win, _px, py[i],
109 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE, False);
110 px[i] += pvx[i];
111 py[i] += pvy[i];
112 _px = px[i] >> KAB_PRECISION;
113 pvy[i]++;
114 if (_px < -wPreferences.icon_size || _px > sw || py[i] >= sh) {
115 ax[i] = -1;
116 k--;
117 } else {
118 XCopyArea(dpy, tmp, scr->root_win, scr->copy_gc,
119 ax[i] * ICON_KABOOM_PIECE_SIZE, ay[i] * ICON_KABOOM_PIECE_SIZE,
120 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE, _px, py[i]);
125 XFlush(dpy);
126 wusleep(MINIATURIZE_ANIMATION_DELAY_Z * 2);
129 XFreePixmap(dpy, tmp);
131 #endif /* NORMAL_ICON_KABOOM */
133 Pixmap MakeGhostDock(WDock * dock, int sx, int dx, int y)
135 WScreen *scr = dock->screen_ptr;
136 XImage *img;
137 RImage *back, *dock_image;
138 Pixmap pixmap;
139 int i, virtual_tiles, h, j, n;
140 unsigned long red_mask, green_mask, blue_mask;
142 virtual_tiles = 0;
143 for (i = 0; i < dock->max_icons; i++) {
144 if (dock->icon_array[i] != NULL && dock->icon_array[i]->yindex > virtual_tiles)
145 virtual_tiles = dock->icon_array[i]->yindex;
147 virtual_tiles++;
148 h = virtual_tiles * wPreferences.icon_size;
149 h = (y + h > scr->scr_height) ? scr->scr_height - y : h;
150 virtual_tiles = h / wPreferences.icon_size; /* The visible ones */
151 if (h % wPreferences.icon_size)
152 virtual_tiles++; /* There is one partially visible tile at end */
154 img = XGetImage(dpy, scr->root_win, dx, y, wPreferences.icon_size, h, AllPlanes, ZPixmap);
155 if (!img)
156 return None;
158 red_mask = img->red_mask;
159 green_mask = img->green_mask;
160 blue_mask = img->blue_mask;
162 back = RCreateImageFromXImage(scr->rcontext, img, NULL);
163 XDestroyImage(img);
164 if (!back) {
165 return None;
168 for (i = 0; i < dock->max_icons; i++) {
169 if (dock->icon_array[i] != NULL && dock->icon_array[i]->yindex < virtual_tiles) {
170 Pixmap which;
171 j = dock->icon_array[i]->yindex * wPreferences.icon_size;
172 n = (h - j < wPreferences.icon_size) ? h - j : wPreferences.icon_size;
173 if (dock->icon_array[i]->icon->pixmap)
174 which = dock->icon_array[i]->icon->pixmap;
175 else
176 which = dock->icon_array[i]->icon->core->window;
178 img = XGetImage(dpy, which, 0, 0, wPreferences.icon_size, n, AllPlanes, ZPixmap);
180 if (!img) {
181 RReleaseImage(back);
182 return None;
184 img->red_mask = red_mask;
185 img->green_mask = green_mask;
186 img->blue_mask = blue_mask;
188 dock_image = RCreateImageFromXImage(scr->rcontext, img, NULL);
189 XDestroyImage(img);
190 if (!dock_image) {
191 RReleaseImage(back);
192 return None;
194 RCombineAreaWithOpaqueness(back, dock_image, 0, 0,
195 wPreferences.icon_size, n, 0, j, 30 * 256 / 100);
196 RReleaseImage(dock_image);
200 RConvertImage(scr->rcontext, back, &pixmap);
202 RReleaseImage(back);
204 return pixmap;
207 Pixmap MakeGhostIcon(WScreen * scr, Drawable drawable)
209 RImage *back;
210 RColor color;
211 Pixmap pixmap;
213 if (!drawable)
214 return None;
216 back = RCreateImageFromDrawable(scr->rcontext, drawable, None);
217 if (!back)
218 return None;
220 color.red = 0xff;
221 color.green = 0xff;
222 color.blue = 0xff;
223 color.alpha = 200;
225 RClearImage(back, &color);
226 RConvertImage(scr->rcontext, back, &pixmap);
228 RReleaseImage(back);
230 return pixmap;
233 #ifdef WINDOW_BIRTH_ZOOM
235 void DoWindowBirth(WWindow *wwin)
237 int center_x, center_y;
238 int width = wwin->frame->core->width;
239 int height = wwin->frame->core->height;
240 int w = WMIN(width, 20);
241 int h = WMIN(height, 20);
242 WScreen *scr = wwin->screen_ptr;
244 center_x = wwin->frame_x + (width - w) / 2;
245 center_y = wwin->frame_y + (height - h) / 2;
247 animateResize(scr, center_x, center_y, 1, 1, wwin->frame_x, wwin->frame_y, width, height);
249 #else
250 void DoWindowBirth(WWindow *wwin)
252 /* dummy stub */
254 #endif
256 #define BOUNCE_HZ 25
257 #define BOUNCE_DELAY (1000/BOUNCE_HZ)
258 #define BOUNCE_HEIGHT 24
259 #define BOUNCE_LENGTH 0.3
260 #define BOUNCE_DAMP 0.6
261 #define URGENT_BOUNCE_DELAY 3000
263 typedef struct AppBouncerData {
264 WApplication *wapp;
265 int count;
266 int pow;
267 int dir;
268 WMHandlerID *timer;
269 } AppBouncerData;
271 static void doAppBounce(void *arg)
273 AppBouncerData *data = (AppBouncerData*)arg;
274 WAppIcon *aicon = data->wapp->app_icon;
276 reinit:
277 if (aicon && data->wapp->refcount > 1) {
278 if (wPreferences.raise_appicons_when_bouncing)
279 XRaiseWindow(dpy, aicon->icon->core->window);
281 const double ticks = BOUNCE_HZ * BOUNCE_LENGTH;
282 const double s = sqrt(BOUNCE_HEIGHT)/(ticks/2);
283 double h = BOUNCE_HEIGHT*pow(BOUNCE_DAMP, data->pow);
284 double sqrt_h = sqrt(h);
285 if (h > 3) {
286 double offset, x = s * data->count - sqrt_h;
287 if (x > sqrt_h) {
288 ++data->pow;
289 data->count = 0;
290 goto reinit;
291 } else ++data->count;
292 offset = h - x*x;
294 switch (data->dir) {
295 case 0: /* left, bounce to right */
296 XMoveWindow(dpy, aicon->icon->core->window,
297 aicon->x_pos + (int)offset, aicon->y_pos);
298 break;
299 case 1: /* right, bounce to left */
300 XMoveWindow(dpy, aicon->icon->core->window,
301 aicon->x_pos - (int)offset, aicon->y_pos);
302 break;
303 case 2: /* top, bounce down */
304 XMoveWindow(dpy, aicon->icon->core->window,
305 aicon->x_pos, aicon->y_pos + (int)offset);
306 break;
307 case 3: /* bottom, bounce up */
308 XMoveWindow(dpy, aicon->icon->core->window,
309 aicon->x_pos, aicon->y_pos - (int)offset);
310 break;
312 return;
314 XMoveWindow(dpy, aicon->icon->core->window,
315 aicon->x_pos, aicon->y_pos);
318 CommitStackingForWindow(aicon->icon->core);
319 data->wapp->flags.bouncing = 0;
320 WMDeleteTimerHandler(data->timer);
321 wApplicationDestroy(data->wapp);
322 free(data);
325 static int bounceDirection(WAppIcon *aicon)
327 enum { left_e = 1, right_e = 2, top_e = 4, bottom_e = 8 };
329 WScreen *scr = aicon->icon->core->screen_ptr;
330 WMRect rr, sr;
331 int l, r, t, b, h, v;
332 int dir = 0;
334 rr.pos.x = aicon->x_pos;
335 rr.pos.y = aicon->y_pos;
336 rr.size.width = rr.size.height = 64;
338 sr = wGetRectForHead(scr, wGetHeadForRect(scr, rr));
340 l = rr.pos.x - sr.pos.x;
341 r = sr.pos.x + sr.size.width - rr.pos.x - rr.size.width;
342 t = rr.pos.y - sr.pos.y;
343 b = sr.pos.y + sr.size.height - rr.pos.y - rr.size.height;
345 if (l < r) {
346 dir |= left_e;
347 h = l;
348 } else {
349 dir |= right_e;
350 h = r;
353 if (t < b) {
354 dir |= top_e;
355 v = t;
356 } else {
357 dir |= bottom_e;
358 v = b;
361 if (aicon->dock && abs(aicon->xindex) != abs(aicon->yindex)) {
362 if (abs(aicon->xindex) < abs(aicon->yindex)) dir &= ~(top_e | bottom_e);
363 else dir &= ~(left_e | right_e);
364 } else {
365 if (h < v) dir &= ~(top_e | bottom_e);
366 else dir &= ~(left_e | right_e);
369 switch (dir) {
370 case left_e:
371 dir = 0;
372 break;
374 case right_e:
375 dir = 1;
376 break;
378 case top_e:
379 dir = 2;
380 break;
382 case bottom_e:
383 dir = 3;
384 break;
386 default:
387 wwarning(_("Impossible direction: %d\n"), dir);
388 dir = 3;
389 break;
392 return dir;
395 void wAppBounce(WApplication *wapp)
397 if (!wPreferences.no_animations && wapp->app_icon && !wapp->flags.bouncing) {
398 ++wapp->refcount;
399 wapp->flags.bouncing = 1;
401 AppBouncerData *data = (AppBouncerData *)malloc(sizeof(AppBouncerData));
402 data->wapp = wapp;
403 data->count = data->pow = 0;
404 data->dir = bounceDirection(wapp->app_icon);
405 data->timer = WMAddPersistentTimerHandler(BOUNCE_DELAY, doAppBounce, data);
409 static int appIsUrgent(WApplication *wapp)
411 WScreen *scr;
412 WWindow *wlist;
414 if (!wapp->main_window_desc) {
415 wwarning("group leader not found for window group");
416 return 0;
418 scr = wapp->main_window_desc->screen_ptr;
419 wlist = scr->focused_window;
420 if (!wlist)
421 return 0;
423 while (wlist) {
424 if (wlist->main_window == wapp->main_window) {
425 if (wlist->flags.urgent)
426 return 1;
428 wlist = wlist->prev;
431 return 0;
434 static void doAppUrgentBounce(void *arg)
436 WApplication *wapp = (WApplication *)arg;
438 if (appIsUrgent(wapp)) {
439 if(wPreferences.bounce_appicons_when_urgent) wAppBounce(wapp);
440 } else {
441 WMDeleteTimerHandler(wapp->urgent_bounce_timer);
442 wapp->urgent_bounce_timer = NULL;
446 void wAppBounceWhileUrgent(WApplication *wapp)
448 if (!wapp) return;
449 if (appIsUrgent(wapp)) {
450 if (!wapp->urgent_bounce_timer) {
451 wapp->urgent_bounce_timer = WMAddPersistentTimerHandler(URGENT_BOUNCE_DELAY, doAppUrgentBounce, wapp);
452 doAppUrgentBounce(wapp);
454 } else {
455 if (wapp->urgent_bounce_timer) {
456 WMDeleteTimerHandler(wapp->urgent_bounce_timer);
457 wapp->urgent_bounce_timer = NULL;