bug fixes
[wmaker-crm.git] / src / superfluous.c
blobc4033e4d465c5c77c38cf641b51a54c88f74b0e4
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>
30 #include <math.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;
81 XEvent event;
83 h = w = wPreferences.icon_size;
84 if (x < 0 || x + w > scr->scr_width || y < 0 || y + h > scr->scr_height)
85 return;
87 icon = RCreateImageFromDrawable(scr->rcontext, win, None);
88 if (!icon)
89 return;
91 gcv.foreground = scr->white_pixel;
92 gcv.background = scr->black_pixel;
93 gcv.graphics_exposures = False;
94 gcv.subwindow_mode = IncludeInferiors;
95 gc = XCreateGC(dpy, scr->w_win, GCForeground|GCBackground|GCSubwindowMode
96 |GCGraphicsExposures, &gcv);
99 XGrabServer(dpy);
100 RConvertImage(scr->rcontext, icon, &pixmap);
101 XUnmapWindow(dpy, win);
103 ximage = XGetImage(dpy, scr->root_win, x, y, w, h, AllPlanes, ZPixmap);
104 XCopyArea(dpy, pixmap, scr->root_win, gc, 0, 0, w, h, x, y);
105 XFreePixmap(dpy,pixmap);
107 back = RCreateImageFromXImage(scr->rcontext, ximage, NULL);
108 XDestroyImage(ximage);
109 if (!back) {
110 RDestroyImage(icon);
111 return;
115 for (i=0,run=0; i<DEMATERIALIZE_STEPS; i++) {
116 XEvent foo;
117 if (!run && XCheckTypedEvent(dpy, ButtonPress, &foo)) {
118 run=1;
119 XPutBackEvent(dpy, &foo);
121 image = RCloneImage(back);
122 RCombineImagesWithOpaqueness(image, icon,
123 (DEMATERIALIZE_STEPS-1-i)*256/(DEMATERIALIZE_STEPS+2));
124 RConvertImage(scr->rcontext, image, &pixmap);
125 XCopyArea(dpy, pixmap, scr->root_win, gc, 0, 0, w, h, x, y);
126 XFreePixmap(dpy, pixmap);
127 XFlush(dpy);
128 if(!run) wusleep(1000);
131 while (XCheckTypedEvent(dpy, MotionNotify, &event)) {
133 XPutBackEvent(dpy, &event);
134 XFlush(dpy);
136 XUngrabServer(dpy);
137 XFreeGC(dpy, gc);
138 RDestroyImage(icon);
139 RDestroyImage(back);
141 #endif /* DEMATERIALIZE_ICON */
148 #ifdef NORMAL_ICON_KABOOM
149 void
150 DoKaboom(WScreen *scr, Window win, int x, int y)
152 int i, j, k;
153 int sw=scr->scr_width, sh=scr->scr_height;
154 #define KAB_PRECISION 4
155 int px[PIECES];
156 short py[PIECES];
157 #ifdef ICON_KABOOM_EXTRA
158 short ptx[2][PIECES], pty[2][PIECES];
159 int ll;
160 #endif
161 char pvx[PIECES], pvy[PIECES];
162 /* in MkLinux/PPC gcc seems to think that char is unsigned? */
163 signed char ax[PIECES], ay[PIECES];
164 Pixmap tmp;
166 XSetClipMask(dpy, scr->copy_gc, None);
167 tmp = XCreatePixmap(dpy, scr->root_win, wPreferences.icon_size,
168 wPreferences.icon_size, scr->depth);
169 if (scr->w_visual == DefaultVisual(dpy, scr->screen))
170 XCopyArea(dpy, win, tmp, scr->copy_gc, 0, 0, wPreferences.icon_size,
171 wPreferences.icon_size, 0, 0);
172 else {
173 XImage *image;
175 image = XGetImage(dpy, win, 0, 0, wPreferences.icon_size,
176 wPreferences.icon_size, AllPlanes, ZPixmap);
177 if (!image) {
178 XUnmapWindow(dpy, win);
179 return;
181 XPutImage(dpy, tmp, scr->copy_gc, image, 0, 0, 0, 0,
182 wPreferences.icon_size, wPreferences.icon_size);
183 XDestroyImage(image);
186 for (i=0,k=0; i<wPreferences.icon_size/ICON_KABOOM_PIECE_SIZE && k<PIECES;
187 i++) {
188 for (j=0; j<wPreferences.icon_size/ICON_KABOOM_PIECE_SIZE && k<PIECES;
189 j++) {
190 if (rand()%2) {
191 ax[k]=i;
192 ay[k]=j;
193 px[k]=(x+i*ICON_KABOOM_PIECE_SIZE)<<KAB_PRECISION;
194 py[k]=y+j*ICON_KABOOM_PIECE_SIZE;
195 pvx[k]=rand()%(1<<(KAB_PRECISION+3))-(1<<(KAB_PRECISION+3))/2;
196 pvy[k]=-15-rand()%7;
197 #ifdef ICON_KABOOM_EXTRA
198 for (ll=0; ll<2; ll++) {
199 ptx[ll][k] = px[k];
200 pty[ll][k] = py[k];
202 #endif
203 k++;
204 } else {
205 ax[k]=-1;
210 XUnmapWindow(dpy, win);
212 j=k;
213 while (k>0) {
214 XEvent foo;
216 if (XCheckTypedEvent(dpy, ButtonPress, &foo)) {
217 XPutBackEvent(dpy, &foo);
218 XClearWindow(dpy, scr->root_win);
219 break;
222 for (i=0; i<j ; i++) {
223 if (ax[i]>=0) {
224 int _px = px[i]>>KAB_PRECISION;
225 #ifdef ICON_KABOOM_EXTRA
226 XClearArea(dpy, scr->root_win, ptx[1][i], pty[1][i],
227 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE,
228 False);
230 ptx[1][i] = ptx[0][i];
231 pty[1][i] = pty[0][i];
232 ptx[0][i] = _px;
233 pty[0][i] = py[i];
234 #else
235 XClearArea(dpy, scr->root_win, _px, py[i],
236 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE,
237 False);
238 #endif
239 px[i]+=pvx[i];
240 py[i]+=pvy[i];
241 _px = px[i]>>KAB_PRECISION;
242 pvy[i]++;
243 if (_px<-wPreferences.icon_size || _px>sw || py[i]>=sh) {
244 #ifdef ICON_KABOOM_EXTRA
245 if (py[i]>sh && _px<sw && _px>0) {
246 pvy[i] = -(pvy[i]/2);
247 if (abs(pvy[i]) > 3) {
248 py[i] = sh-ICON_KABOOM_PIECE_SIZE;
249 XCopyArea(dpy, tmp, scr->root_win, scr->copy_gc,
250 ax[i]*ICON_KABOOM_PIECE_SIZE,
251 ay[i]*ICON_KABOOM_PIECE_SIZE,
252 ICON_KABOOM_PIECE_SIZE,
253 ICON_KABOOM_PIECE_SIZE,
254 _px, py[i]);
255 } else {
256 ax[i] = -1;
258 } else {
259 ax[i] = -1;
261 if (ax[i]<0) {
262 for (ll=0; ll<2; ll++)
263 XClearArea(dpy, scr->root_win, ptx[ll][i], pty[ll][i],
264 ICON_KABOOM_PIECE_SIZE,
265 ICON_KABOOM_PIECE_SIZE, False);
266 k--;
268 #else /* !ICON_KABOOM_EXTRA */
269 ax[i]=-1;
270 k--;
271 #endif /* !ICON_KABOOM_EXTRA */
272 } else {
273 XCopyArea(dpy, tmp, scr->root_win, scr->copy_gc,
274 ax[i]*ICON_KABOOM_PIECE_SIZE, ay[i]*ICON_KABOOM_PIECE_SIZE,
275 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE,
276 _px, py[i]);
281 XFlush(dpy);
282 #ifdef SPEAKER_SOUND
283 play(dpy, 100+rand()%250, 12);
284 #else
285 # if (MINIATURIZE_ANIMATION_DELAY_Z > 0)
286 wusleep(MINIATURIZE_ANIMATION_DELAY_Z*2);
287 # endif
288 #endif
291 XFreePixmap(dpy, tmp);
293 #endif /* NORMAL_ICON_KABOOM */
296 Pixmap
297 MakeGhostDock(WDock *dock, int sx, int dx, int y)
299 WScreen *scr = dock->screen_ptr;
300 XImage *img;
301 RImage *back, *dock_image;
302 Pixmap pixmap;
303 int i, virtual_tiles, h, j, n;
304 unsigned long red_mask, green_mask, blue_mask;
306 virtual_tiles = 0;
307 for (i=0; i<dock->max_icons; i++) {
308 if (dock->icon_array[i]!=NULL &&
309 dock->icon_array[i]->yindex > virtual_tiles)
310 virtual_tiles = dock->icon_array[i]->yindex;
312 virtual_tiles++;
313 h = virtual_tiles * wPreferences.icon_size;
314 h = (y + h > scr->scr_height) ? scr->scr_height-y : h;
315 virtual_tiles = h / wPreferences.icon_size; /* The visible ones */
316 if (h % wPreferences.icon_size)
317 virtual_tiles++; /* There is one partially visible tile at end */
319 img=XGetImage(dpy, scr->root_win, dx, y, wPreferences.icon_size, h,
320 AllPlanes, ZPixmap);
321 if (!img)
322 return None;
324 red_mask = img->red_mask;
325 green_mask = img->green_mask;
326 blue_mask = img->blue_mask;
328 back = RCreateImageFromXImage(scr->rcontext, img, NULL);
329 XDestroyImage(img);
330 if (!back) {
331 return None;
334 for (i=0;i<dock->max_icons;i++) {
335 if (dock->icon_array[i]!=NULL &&
336 dock->icon_array[i]->yindex < virtual_tiles) {
337 Pixmap which;
338 j = dock->icon_array[i]->yindex * wPreferences.icon_size;
339 n = (h - j < wPreferences.icon_size) ? h - j :
340 wPreferences.icon_size;
341 if (dock->icon_array[i]->icon->pixmap)
342 which = dock->icon_array[i]->icon->pixmap;
343 else
344 which = dock->icon_array[i]->icon->core->window;
346 img=XGetImage(dpy, which, 0, 0, wPreferences.icon_size, n,
347 AllPlanes, ZPixmap);
349 if (!img){
350 RDestroyImage(back);
351 return None;
353 img->red_mask = red_mask;
354 img->green_mask = green_mask;
355 img->blue_mask = blue_mask;
357 dock_image = RCreateImageFromXImage(scr->rcontext, img, NULL);
358 XDestroyImage(img);
359 if (!dock_image) {
360 RDestroyImage(back);
361 return None;
363 RCombineAreaWithOpaqueness(back, dock_image, 0, 0,
364 wPreferences.icon_size, n,
365 0, j, 30 * 256 / 100);
366 RDestroyImage(dock_image);
371 RConvertImage(scr->rcontext, back, &pixmap);
373 RDestroyImage(back);
375 return pixmap;
379 Pixmap
380 MakeGhostIcon(WScreen *scr, Drawable drawable)
382 RImage *back;
383 RColor color;
384 Pixmap pixmap;
387 if (!drawable)
388 return None;
390 back = RCreateImageFromDrawable(scr->rcontext, drawable, None);
391 if (!back)
392 return None;
394 color.red = 0xff;
395 color.green = 0xff;
396 color.blue = 0xff;
397 color.alpha = 200;
399 RClearImage(back, &color);
400 RConvertImage(scr->rcontext, back, &pixmap);
402 RDestroyImage(back);
404 return pixmap;
409 #ifdef WINDOW_BIRTH_ZOOM
410 void
411 DoWindowBirth(WWindow *wwin)
413 int width = wwin->frame->core->width;
414 int height = wwin->frame->core->height;
415 int w = WMIN(width, 20);
416 int h = WMIN(height, 20);
417 int x, y;
418 int dw, dh;
419 int i;
420 time_t time0 = time(NULL);
422 dw = (width-w)/WINDOW_BIRTH_STEPS;
423 dh = (height-h)/WINDOW_BIRTH_STEPS;
425 x = wwin->frame_x + (width-w)/2;
426 y = wwin->frame_y + (height-h)/2;
428 XMoveResizeWindow(dpy, wwin->frame->core->window, x, y, w, h);
430 XMapWindow(dpy, wwin->frame->core->window);
432 XFlush(dpy);
433 for (i=0; i<WINDOW_BIRTH_STEPS; i++) {
434 x -= dw/2 + dw%2;
435 y -= dh/2 + dh%2;
436 w += dw;
437 h += dh;
438 XMoveResizeWindow(dpy, wwin->frame->core->window, x, y, w, h);
439 XFlush(dpy);
440 /* a timeout */
441 if (time(NULL)-time0 > MAX_ANIMATION_TIME)
442 break;
445 XMoveResizeWindow(dpy, wwin->frame->core->window,
446 wwin->frame_x, wwin->frame_y, width, height);
447 XFlush(dpy);
449 #else
450 void
451 DoWindowBirth(WWindow *wwin)
453 /* dummy stub */
455 #endif
459 #ifdef SILLYNESS
460 static WMPixmap *data[12];
463 static Bool
464 loadData(WScreen *scr)
466 FILE *f;
467 int i;
468 RImage *image;
469 Pixmap d[12];
471 f = fopen(PKGDATADIR"/xtree.dat", "r");
472 if (!f)
473 return False;
475 image = RCreateImage(50, 50, False);
476 if (!image) {
477 fclose(f);
478 return False;
481 for (i = 0; i < 12; i++) {
482 if (fread(image->data[0], 50*50, 1, f)!=1) {
483 goto error;
485 if (fread(image->data[1], 50*50, 1, f)!=1) {
486 goto error;
488 if (fread(image->data[2], 50*50, 1, f)!=1) {
489 goto error;
491 if (!RConvertImage(scr->rcontext, image, &(d[i]))) {
492 goto error;
495 RDestroyImage(image);
497 fclose(f);
499 for (i=0; i<12; i++) {
500 data[i] = WMCreatePixmapFromXPixmaps(scr->wmscreen, d[i], None, 50, 50,
501 scr->w_depth);
504 return True;
506 error:
507 RDestroyImage(image);
509 fclose(f);
511 while (--i > 0) {
512 XFreePixmap(dpy, d[i]);
515 return False;
519 WMPixmap*
520 DoXThing(WWindow *wwin)
522 static int order = 0;
524 order++;
526 return data[order % 12];
530 Bool
531 InitXThing(WScreen *scr)
533 time_t t;
534 struct tm *l;
535 static int i = 0;
537 if (i)
538 return True;
540 t = time(NULL);
541 l = localtime(&t);
542 if ((l->tm_mon!=12||l->tm_mday<24||l->tm_mday>26)) {
543 return False;
546 if (!loadData(scr))
547 return False;
549 i = 1;
551 return True;
554 #endif /* SILLYNESS */
557 #ifdef GHOST_WINDOW_MOVE
558 RImage*
559 InitGhostWindowMove(WScreen *scr)
561 RXImage *ximg;
562 RImage *img;
564 ximg = RGetXImage(scr->rcontext, scr->root_win, 0, 0,
565 scr->scr_width, scr->scr_height);
566 img = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
567 RDestroyXImage(dpy, ximg);
569 return img;
572 #endif /* GHOST_WINDOW_MOVE */