Added the crash dialog panel
[wmaker-crm.git] / src / superfluous.c
blob3596650b9528d83466a82fbd0b856c9f81a83356
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 * Copyright (c) 1997, 1998 Dan Pascu
6 *
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>
31 #include <time.h>
33 #include <wraster.h>
35 #include "WindowMaker.h"
36 #include "screen.h"
37 #include "superfluous.h"
38 #include "dock.h"
39 #include "wcore.h"
40 #include "framewin.h"
41 #include "window.h"
42 #include "icon.h"
43 #include "appicon.h"
46 extern WPreferences wPreferences;
49 #ifdef SPEAKER_SOUND
50 static void
51 play(Display *dpy, int pitch, int delay)
53 XKeyboardControl kc;
55 kc.bell_pitch = pitch;
56 kc.bell_percent = 50;
57 kc.bell_duration = delay;
58 XChangeKeyboardControl(dpy, KBBellPitch|KBBellDuration|KBBellPercent,&kc);
59 XBell(dpy, 50);
60 XFlush(dpy);
61 wusleep(delay);
63 #endif
67 #ifdef DEMATERIALIZE_ICON
68 void
69 DoKaboom(WScreen *scr, Window win, int x, int y)
71 RImage *icon;
72 RImage *back;
73 RImage *image;
74 Pixmap pixmap;
75 XImage *ximage;
76 GC gc;
77 XGCValues gcv;
78 int i;
79 int w, h;
80 int run;
82 h = w = wPreferences.icon_size;
83 if (x < 0 || x + w > scr->scr_width || y < 0 || y + h > scr->scr_height)
84 return;
86 icon = RCreateImageFromDrawable(scr->rcontext, win, None);
87 if (!icon)
88 return;
90 XGrabServer(dpy);
91 XUnmapWindow(dpy, win);
92 XSync(dpy, False);
94 ximage = XGetImage(dpy, scr->root_win, x, y, w, h, AllPlanes, ZPixmap);
95 back = RCreateImageFromXImage(scr->rcontext, ximage, NULL);
96 XDestroyImage(ximage);
97 if (!back) {
98 RDestroyImage(icon);
99 return;
102 gcv.foreground = scr->white_pixel;
103 gcv.background = scr->black_pixel;
104 gcv.graphics_exposures = False;
105 gcv.subwindow_mode = IncludeInferiors;
106 gc = XCreateGC(dpy, scr->w_win, GCForeground|GCBackground|GCSubwindowMode
107 |GCGraphicsExposures, &gcv);
110 XSetClipMask(dpy, scr->copy_gc, None);
113 for (i=0,run=0; i<DEMATERIALIZE_STEPS; i++) {
114 XEvent foo;
115 if (!run && XCheckTypedEvent(dpy, ButtonPress, &foo)) {
116 run=1;
117 XPutBackEvent(dpy, &foo);
119 XClearWindow(dpy, scr->root_win);
120 break;
123 image = RCloneImage(back);
124 RCombineImagesWithOpaqueness(image, icon,
125 (DEMATERIALIZE_STEPS-1-i)*256/(DEMATERIALIZE_STEPS+2));
126 RConvertImage(scr->rcontext, image, &pixmap);
127 XCopyArea(dpy, pixmap, scr->root_win, gc, 0, 0, w, h, x, y);
128 XFreePixmap(dpy, pixmap);
129 XFlush(dpy);
130 if(!run) wusleep(1000);
133 XClearArea(dpy, scr->root_win, x, y, w, h, False);
135 XFlush(dpy);
137 XUngrabServer(dpy);
138 XFreeGC(dpy, gc);
139 RDestroyImage(icon);
140 RDestroyImage(back);
142 #else /* !DEMATERIALIZE_ICON */
143 void
144 DoKaboom(WScreen *scr, Window win, int x, int y)
146 int i, j, k;
147 int sw=scr->scr_width, sh=scr->scr_height;
148 #define KAB_PRECISION 4
149 int px[PIECES];
150 short py[PIECES];
151 #ifdef ICON_KABOOM_EXTRA
152 short ptx[2][PIECES], pty[2][PIECES];
153 int ll;
154 #endif
155 char pvx[PIECES], pvy[PIECES];
156 /* in MkLinux/PPC gcc seems to think that char is unsigned? */
157 signed char ax[PIECES], ay[PIECES];
158 Pixmap tmp;
160 XSetClipMask(dpy, scr->copy_gc, None);
161 tmp = XCreatePixmap(dpy, scr->root_win, wPreferences.icon_size,
162 wPreferences.icon_size, scr->depth);
163 if (scr->w_visual == DefaultVisual(dpy, scr->screen))
164 XCopyArea(dpy, win, tmp, scr->copy_gc, 0, 0, wPreferences.icon_size,
165 wPreferences.icon_size, 0, 0);
166 else {
167 XImage *image;
169 image = XGetImage(dpy, win, 0, 0, wPreferences.icon_size,
170 wPreferences.icon_size, AllPlanes, ZPixmap);
171 if (!image) {
172 XUnmapWindow(dpy, win);
173 return;
175 XPutImage(dpy, tmp, scr->copy_gc, image, 0, 0, 0, 0,
176 wPreferences.icon_size, wPreferences.icon_size);
177 XDestroyImage(image);
180 for (i=0,k=0; i<wPreferences.icon_size/ICON_KABOOM_PIECE_SIZE && k<PIECES;
181 i++) {
182 for (j=0; j<wPreferences.icon_size/ICON_KABOOM_PIECE_SIZE && k<PIECES;
183 j++) {
184 if (rand()%2) {
185 ax[k]=i;
186 ay[k]=j;
187 px[k]=(x+i*ICON_KABOOM_PIECE_SIZE)<<KAB_PRECISION;
188 py[k]=y+j*ICON_KABOOM_PIECE_SIZE;
189 pvx[k]=rand()%(1<<(KAB_PRECISION+3))-(1<<(KAB_PRECISION+3))/2;
190 pvy[k]=-15-rand()%7;
191 #ifdef ICON_KABOOM_EXTRA
192 for (ll=0; ll<2; ll++) {
193 ptx[ll][k] = px[k];
194 pty[ll][k] = py[k];
196 #endif
197 k++;
198 } else {
199 ax[k]=-1;
204 XUnmapWindow(dpy, win);
206 j=k;
207 while (k>0) {
208 XEvent foo;
210 if (XCheckTypedEvent(dpy, ButtonPress, &foo)) {
211 XPutBackEvent(dpy, &foo);
212 XClearWindow(dpy, scr->root_win);
213 break;
216 for (i=0; i<j ; i++) {
217 if (ax[i]>=0) {
218 int _px = px[i]>>KAB_PRECISION;
219 #ifdef ICON_KABOOM_EXTRA
220 XClearArea(dpy, scr->root_win, ptx[1][i], pty[1][i],
221 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE,
222 False);
224 ptx[1][i] = ptx[0][i];
225 pty[1][i] = pty[0][i];
226 ptx[0][i] = _px;
227 pty[0][i] = py[i];
228 #else
229 XClearArea(dpy, scr->root_win, _px, py[i],
230 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE,
231 False);
232 #endif
233 px[i]+=pvx[i];
234 py[i]+=pvy[i];
235 _px = px[i]>>KAB_PRECISION;
236 pvy[i]++;
237 if (_px<-wPreferences.icon_size || _px>sw || py[i]>=sh) {
238 #ifdef ICON_KABOOM_EXTRA
239 if (py[i]>sh && _px<sw && _px>0) {
240 pvy[i] = -(pvy[i]/2);
241 if (abs(pvy[i]) > 3) {
242 py[i] = sh-ICON_KABOOM_PIECE_SIZE;
243 XCopyArea(dpy, tmp, scr->root_win, scr->copy_gc,
244 ax[i]*ICON_KABOOM_PIECE_SIZE,
245 ay[i]*ICON_KABOOM_PIECE_SIZE,
246 ICON_KABOOM_PIECE_SIZE,
247 ICON_KABOOM_PIECE_SIZE,
248 _px, py[i]);
249 } else {
250 ax[i] = -1;
252 } else {
253 ax[i] = -1;
255 if (ax[i]<0) {
256 for (ll=0; ll<2; ll++)
257 XClearArea(dpy, scr->root_win, ptx[ll][i], pty[ll][i],
258 ICON_KABOOM_PIECE_SIZE,
259 ICON_KABOOM_PIECE_SIZE, False);
260 k--;
262 #else /* !ICON_KABOOM_EXTRA */
263 ax[i]=-1;
264 k--;
265 #endif /* !ICON_KABOOM_EXTRA */
266 } else {
267 XCopyArea(dpy, tmp, scr->root_win, scr->copy_gc,
268 ax[i]*ICON_KABOOM_PIECE_SIZE, ay[i]*ICON_KABOOM_PIECE_SIZE,
269 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE,
270 _px, py[i]);
275 XFlush(dpy);
276 #ifdef SPEAKER_SOUND
277 play(dpy, 100+rand()%250, 12);
278 #else
279 # if (MINIATURIZE_ANIMATION_DELAY_Z > 0)
280 wusleep(MINIATURIZE_ANIMATION_DELAY_Z*2);
281 # endif
282 #endif
285 XFreePixmap(dpy, tmp);
287 #endif /* !DEMATERIALIZE_ICON */
290 Pixmap
291 MakeGhostDock(WDock *dock, int sx, int dx, int y)
293 WScreen *scr = dock->screen_ptr;
294 XImage *img;
295 RImage *back, *dock_image;
296 Pixmap pixmap;
297 int i, virtual_tiles, h, j, n;
298 unsigned long red_mask, green_mask, blue_mask;
300 virtual_tiles = 0;
301 for (i=0; i<dock->max_icons; i++) {
302 if (dock->icon_array[i]!=NULL &&
303 dock->icon_array[i]->yindex > virtual_tiles)
304 virtual_tiles = dock->icon_array[i]->yindex;
306 virtual_tiles++;
307 h = virtual_tiles * wPreferences.icon_size;
308 h = (y + h > scr->scr_height) ? scr->scr_height-y : h;
309 virtual_tiles = h / wPreferences.icon_size; /* The visible ones */
310 if (h % wPreferences.icon_size)
311 virtual_tiles++; /* There is one partially visible tile at end */
313 img=XGetImage(dpy, scr->root_win, dx, y, wPreferences.icon_size, h,
314 AllPlanes, ZPixmap);
315 if (!img)
316 return None;
318 red_mask = img->red_mask;
319 green_mask = img->green_mask;
320 blue_mask = img->blue_mask;
322 back = RCreateImageFromXImage(scr->rcontext, img, NULL);
323 XDestroyImage(img);
324 if (!back) {
325 return None;
328 for (i=0;i<dock->max_icons;i++) {
329 if (dock->icon_array[i]!=NULL &&
330 dock->icon_array[i]->yindex < virtual_tiles) {
331 Pixmap which;
332 j = dock->icon_array[i]->yindex * wPreferences.icon_size;
333 n = (h - j < wPreferences.icon_size) ? h - j :
334 wPreferences.icon_size;
335 if (dock->icon_array[i]->icon->pixmap)
336 which = dock->icon_array[i]->icon->pixmap;
337 else
338 which = dock->icon_array[i]->icon->core->window;
340 img=XGetImage(dpy, which, 0, 0, wPreferences.icon_size, n,
341 AllPlanes, ZPixmap);
343 if (!img){
344 RDestroyImage(back);
345 return None;
347 img->red_mask = red_mask;
348 img->green_mask = green_mask;
349 img->blue_mask = blue_mask;
351 dock_image = RCreateImageFromXImage(scr->rcontext, img, NULL);
352 XDestroyImage(img);
353 if (!dock_image) {
354 RDestroyImage(back);
355 return None;
357 RCombineAreaWithOpaqueness(back, dock_image, 0, 0,
358 wPreferences.icon_size, n,
359 0, j, 30 * 256 / 100);
360 RDestroyImage(dock_image);
365 RConvertImage(scr->rcontext, back, &pixmap);
367 RDestroyImage(back);
369 return pixmap;
373 Pixmap
374 MakeGhostIcon(WScreen *scr, Drawable drawable)
376 RImage *back;
377 RColor color;
378 Pixmap pixmap;
381 if (!drawable)
382 return None;
384 back = RCreateImageFromDrawable(scr->rcontext, drawable, None);
385 if (!back)
386 return None;
388 color.red = 0xff;
389 color.green = 0xff;
390 color.blue = 0xff;
391 color.alpha = 200;
393 RClearImage(back, &color);
394 RConvertImage(scr->rcontext, back, &pixmap);
396 RDestroyImage(back);
398 return pixmap;
403 #ifdef WINDOW_BIRTH_ZOOM
404 void
405 DoWindowBirth(WWindow *wwin)
407 int width = wwin->frame->core->width;
408 int height = wwin->frame->core->height;
409 int w = WMIN(width, 20);
410 int h = WMIN(height, 20);
411 int x, y;
412 int dw, dh;
413 int i;
414 time_t time0 = time(NULL);
416 dw = (width-w)/WINDOW_BIRTH_STEPS;
417 dh = (height-h)/WINDOW_BIRTH_STEPS;
419 x = wwin->frame_x + (width-w)/2;
420 y = wwin->frame_y + (height-h)/2;
422 XMoveResizeWindow(dpy, wwin->frame->core->window, x, y, w, h);
424 XMapWindow(dpy, wwin->frame->core->window);
426 XFlush(dpy);
427 for (i=0; i<WINDOW_BIRTH_STEPS; i++) {
428 x -= dw/2 + dw%2;
429 y -= dh/2 + dh%2;
430 w += dw;
431 h += dh;
432 XMoveResizeWindow(dpy, wwin->frame->core->window, x, y, w, h);
433 XFlush(dpy);
434 /* a timeout */
435 if (time(NULL)-time0 > MAX_ANIMATION_TIME)
436 break;
439 XMoveResizeWindow(dpy, wwin->frame->core->window,
440 wwin->frame_x, wwin->frame_y, width, height);
441 XFlush(dpy);
443 #else
444 void
445 DoWindowBirth(WWindow *wwin)
447 /* dummy stub */
449 #endif
453 #ifdef SILLYNESS
454 static WMPixmap *data[12];
457 static Bool
458 loadData(WScreen *scr)
460 FILE *f;
461 int i;
462 RImage *image;
463 Pixmap d[12];
465 f = fopen(PKGDATADIR"/xtree.dat", "r");
466 if (!f)
467 return False;
469 image = RCreateImage(50, 50, False);
470 if (!image) {
471 fclose(f);
472 return False;
475 for (i = 0; i < 12; i++) {
476 if (fread(image->data[0], 50*50, 1, f)!=1) {
477 goto error;
479 if (fread(image->data[1], 50*50, 1, f)!=1) {
480 goto error;
482 if (fread(image->data[2], 50*50, 1, f)!=1) {
483 goto error;
485 if (!RConvertImage(scr->rcontext, image, &(d[i]))) {
486 goto error;
489 RDestroyImage(image);
491 fclose(f);
493 for (i=0; i<12; i++) {
494 data[i] = WMCreatePixmapFromXPixmaps(scr->wmscreen, d[i], None, 50, 50,
495 scr->w_depth);
498 return True;
500 error:
501 RDestroyImage(image);
503 fclose(f);
505 while (--i > 0) {
506 XFreePixmap(dpy, d[i]);
509 return False;
513 WMPixmap*
514 DoXThing(WWindow *wwin)
516 static int order = 0;
518 order++;
520 return data[order % 12];
524 Bool
525 InitXThing(WScreen *scr)
527 time_t t;
528 struct tm *l;
529 static int i = 0;
531 if (i)
532 return True;
534 t = time(NULL);
535 l = localtime(&t);
536 if ((l->tm_mon!=12||l->tm_mday<24||l->tm_mday>26)) {
537 return False;
540 if (!loadData(scr))
541 return False;
543 i = 1;
545 return True;
548 #endif /* SILLYNESS */
551 #ifdef GHOST_WINDOW_MOVE
552 RImage*
553 InitGhostWindowMove(WScreen *scr)
555 RXImage *ximg;
556 RImage *img;
558 ximg = RGetXImage(scr->rcontext, scr->root_win, 0, 0,
559 scr->scr_width, scr->scr_height);
560 img = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
561 RDestroyXImage(dpy, ximg);
563 return img;
566 #endif /* GHOST_WINDOW_MOVE */