Prevent crash when switchpanel is not initialised.
[wmaker-crm.git] / src / superfluous.c
blob0c95ed4d1618e4197a158a73c4798db341ec1705
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
52 extern WPreferences wPreferences;
54 void DoKaboom(WScreen * scr, Window win, int x, int y)
56 #ifdef NORMAL_ICON_KABOOM
57 int i, j, k;
58 int sw = scr->scr_width, sh = scr->scr_height;
59 int px[PIECES];
60 short py[PIECES];
61 char pvx[PIECES], pvy[PIECES];
62 /* in MkLinux/PPC gcc seems to think that char is unsigned? */
63 signed char ax[PIECES], ay[PIECES];
64 Pixmap tmp;
66 XSetClipMask(dpy, scr->copy_gc, None);
67 tmp = XCreatePixmap(dpy, scr->root_win, wPreferences.icon_size, wPreferences.icon_size, scr->depth);
68 if (scr->w_visual == DefaultVisual(dpy, scr->screen))
69 XCopyArea(dpy, win, tmp, scr->copy_gc, 0, 0, wPreferences.icon_size, wPreferences.icon_size, 0, 0);
70 else {
71 XImage *image;
73 image = XGetImage(dpy, win, 0, 0, wPreferences.icon_size,
74 wPreferences.icon_size, AllPlanes, ZPixmap);
75 if (!image) {
76 XUnmapWindow(dpy, win);
77 return;
79 XPutImage(dpy, tmp, scr->copy_gc, image, 0, 0, 0, 0,
80 wPreferences.icon_size, wPreferences.icon_size);
81 XDestroyImage(image);
84 for (i = 0, k = 0; i < wPreferences.icon_size / ICON_KABOOM_PIECE_SIZE && k < PIECES; i++) {
85 for (j = 0; j < wPreferences.icon_size / ICON_KABOOM_PIECE_SIZE && k < PIECES; j++) {
86 if (rand() % 2) {
87 ax[k] = i;
88 ay[k] = j;
89 px[k] = (x + i * ICON_KABOOM_PIECE_SIZE) << KAB_PRECISION;
90 py[k] = y + j * ICON_KABOOM_PIECE_SIZE;
91 pvx[k] = rand() % (1 << (KAB_PRECISION + 3)) - (1 << (KAB_PRECISION + 3)) / 2;
92 pvy[k] = -15 - rand() % 7;
93 k++;
94 } else {
95 ax[k] = -1;
100 XUnmapWindow(dpy, win);
102 j = k;
103 while (k > 0) {
104 XEvent foo;
106 if (XCheckTypedEvent(dpy, ButtonPress, &foo)) {
107 XPutBackEvent(dpy, &foo);
108 XClearWindow(dpy, scr->root_win);
109 break;
112 for (i = 0; i < j; i++) {
113 if (ax[i] >= 0) {
114 int _px = px[i] >> KAB_PRECISION;
115 XClearArea(dpy, scr->root_win, _px, py[i],
116 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE, False);
117 px[i] += pvx[i];
118 py[i] += pvy[i];
119 _px = px[i] >> KAB_PRECISION;
120 pvy[i]++;
121 if (_px < -wPreferences.icon_size || _px > sw || py[i] >= sh) {
122 ax[i] = -1;
123 k--;
124 } else {
125 XCopyArea(dpy, tmp, scr->root_win, scr->copy_gc,
126 ax[i] * ICON_KABOOM_PIECE_SIZE, ay[i] * ICON_KABOOM_PIECE_SIZE,
127 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE, _px, py[i]);
132 XFlush(dpy);
133 wusleep(MINIATURIZE_ANIMATION_DELAY_Z * 2);
136 XFreePixmap(dpy, tmp);
137 #endif /* NORMAL_ICON_KABOOM */
140 Pixmap MakeGhostIcon(WScreen * scr, Drawable drawable)
142 RImage *back;
143 RColor color;
144 Pixmap pixmap;
146 if (!drawable)
147 return None;
149 back = RCreateImageFromDrawable(scr->rcontext, drawable, None);
150 if (!back)
151 return None;
153 color.red = 0xff;
154 color.green = 0xff;
155 color.blue = 0xff;
156 color.alpha = 200;
158 RClearImage(back, &color);
159 RConvertImage(scr->rcontext, back, &pixmap);
161 RReleaseImage(back);
163 return pixmap;
166 void DoWindowBirth(WWindow *wwin)
168 #ifdef WINDOW_BIRTH_ZOOM
169 int center_x, center_y;
170 int width = wwin->frame->core->width;
171 int height = wwin->frame->core->height;
172 int w = WMIN(width, 20);
173 int h = WMIN(height, 20);
174 WScreen *scr = wwin->screen_ptr;
176 center_x = wwin->frame_x + (width - w) / 2;
177 center_y = wwin->frame_y + (height - h) / 2;
179 animateResize(scr, center_x, center_y, 1, 1, wwin->frame_x, wwin->frame_y, width, height);
180 #endif
183 typedef struct AppBouncerData {
184 WApplication *wapp;
185 int count;
186 int pow;
187 int dir;
188 WMHandlerID *timer;
189 } AppBouncerData;
191 static void doAppBounce(void *arg)
193 AppBouncerData *data = (AppBouncerData*)arg;
194 WAppIcon *aicon = data->wapp->app_icon;
196 reinit:
197 if (aicon && data->wapp->refcount > 1) {
198 if (wPreferences.raise_appicons_when_bouncing)
199 XRaiseWindow(dpy, aicon->icon->core->window);
201 const double ticks = BOUNCE_HZ * BOUNCE_LENGTH;
202 const double s = sqrt(BOUNCE_HEIGHT)/(ticks/2);
203 double h = BOUNCE_HEIGHT*pow(BOUNCE_DAMP, data->pow);
204 double sqrt_h = sqrt(h);
205 if (h > 3) {
206 double offset, x = s * data->count - sqrt_h;
207 if (x > sqrt_h) {
208 ++data->pow;
209 data->count = 0;
210 goto reinit;
211 } else ++data->count;
212 offset = h - x*x;
214 switch (data->dir) {
215 case 0: /* left, bounce to right */
216 XMoveWindow(dpy, aicon->icon->core->window,
217 aicon->x_pos + (int)offset, aicon->y_pos);
218 break;
219 case 1: /* right, bounce to left */
220 XMoveWindow(dpy, aicon->icon->core->window,
221 aicon->x_pos - (int)offset, aicon->y_pos);
222 break;
223 case 2: /* top, bounce down */
224 XMoveWindow(dpy, aicon->icon->core->window,
225 aicon->x_pos, aicon->y_pos + (int)offset);
226 break;
227 case 3: /* bottom, bounce up */
228 XMoveWindow(dpy, aicon->icon->core->window,
229 aicon->x_pos, aicon->y_pos - (int)offset);
230 break;
232 return;
236 XMoveWindow(dpy, aicon->icon->core->window,
237 aicon->x_pos, aicon->y_pos);
238 CommitStackingForWindow(aicon->icon->core);
239 data->wapp->flags.bouncing = 0;
240 WMDeleteTimerHandler(data->timer);
241 wApplicationDestroy(data->wapp);
242 free(data);
245 static int bounceDirection(WAppIcon *aicon)
247 enum { left_e = 1, right_e = 2, top_e = 4, bottom_e = 8 };
249 WScreen *scr = aicon->icon->core->screen_ptr;
250 WMRect rr, sr;
251 int l, r, t, b, h, v;
252 int dir = 0;
254 rr.pos.x = aicon->x_pos;
255 rr.pos.y = aicon->y_pos;
256 rr.size.width = rr.size.height = 64;
258 sr = wGetRectForHead(scr, wGetHeadForRect(scr, rr));
260 l = rr.pos.x - sr.pos.x;
261 r = sr.pos.x + sr.size.width - rr.pos.x - rr.size.width;
262 t = rr.pos.y - sr.pos.y;
263 b = sr.pos.y + sr.size.height - rr.pos.y - rr.size.height;
265 if (l < r) {
266 dir |= left_e;
267 h = l;
268 } else {
269 dir |= right_e;
270 h = r;
273 if (t < b) {
274 dir |= top_e;
275 v = t;
276 } else {
277 dir |= bottom_e;
278 v = b;
281 if (aicon->dock && abs(aicon->xindex) != abs(aicon->yindex)) {
282 if (abs(aicon->xindex) < abs(aicon->yindex)) dir &= ~(top_e | bottom_e);
283 else dir &= ~(left_e | right_e);
284 } else {
285 if (h < v) dir &= ~(top_e | bottom_e);
286 else dir &= ~(left_e | right_e);
289 switch (dir) {
290 case left_e:
291 dir = 0;
292 break;
294 case right_e:
295 dir = 1;
296 break;
298 case top_e:
299 dir = 2;
300 break;
302 case bottom_e:
303 dir = 3;
304 break;
306 default:
307 wwarning(_("Impossible direction: %d"), dir);
308 dir = 3;
309 break;
312 return dir;
315 void wAppBounce(WApplication *wapp)
317 if (!wPreferences.no_animations && wapp->app_icon && !wapp->flags.bouncing
318 && !wPreferences.do_not_make_appicons_bounce) {
319 ++wapp->refcount;
320 wapp->flags.bouncing = 1;
322 AppBouncerData *data = (AppBouncerData *)malloc(sizeof(AppBouncerData));
323 data->wapp = wapp;
324 data->count = data->pow = 0;
325 data->dir = bounceDirection(wapp->app_icon);
326 data->timer = WMAddPersistentTimerHandler(BOUNCE_DELAY, doAppBounce, data);
330 static int appIsUrgent(WApplication *wapp)
332 WScreen *scr;
333 WWindow *wlist;
335 if (!wapp->main_window_desc) {
336 wwarning("group leader not found for window group");
337 return 0;
339 scr = wapp->main_window_desc->screen_ptr;
340 wlist = scr->focused_window;
341 if (!wlist)
342 return 0;
344 while (wlist) {
345 if (wlist->main_window == wapp->main_window) {
346 if (wlist->flags.urgent)
347 return 1;
349 wlist = wlist->prev;
352 return 0;
355 static void doAppUrgentBounce(void *arg)
357 WApplication *wapp = (WApplication *)arg;
359 if (appIsUrgent(wapp)) {
360 if(wPreferences.bounce_appicons_when_urgent) wAppBounce(wapp);
361 } else {
362 WMDeleteTimerHandler(wapp->urgent_bounce_timer);
363 wapp->urgent_bounce_timer = NULL;
367 void wAppBounceWhileUrgent(WApplication *wapp)
369 if (!wapp) return;
370 if (appIsUrgent(wapp)) {
371 if (!wapp->urgent_bounce_timer) {
372 wapp->urgent_bounce_timer = WMAddPersistentTimerHandler(URGENT_BOUNCE_DELAY, doAppUrgentBounce, wapp);
373 doAppUrgentBounce(wapp);
375 } else {
376 if (wapp->urgent_bounce_timer) {
377 WMDeleteTimerHandler(wapp->urgent_bounce_timer);
378 wapp->urgent_bounce_timer = NULL;