kde 1.1 update
[wmaker-crm.git] / src / superfluous.c
blob40c0b8f6d2a92a0dcce7d38008df9f3f1a56d65b
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 Alfredo K. Kojima
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
27 #include <stdlib.h>
28 #include <unistd.h>
30 #include <time.h>
32 #include <wraster.h>
34 #include "WindowMaker.h"
35 #include "screen.h"
36 #include "superfluous.h"
37 #include "dock.h"
38 #include "wcore.h"
39 #include "framewin.h"
40 #include "window.h"
41 #include "icon.h"
42 #include "appicon.h"
45 extern WPreferences wPreferences;
48 #ifdef SPEAKER_SOUND
49 static void
50 play(Display *dpy, int pitch, int delay)
52 XKeyboardControl kc;
54 kc.bell_pitch = pitch;
55 kc.bell_percent = 50;
56 kc.bell_duration = delay;
57 XChangeKeyboardControl(dpy, KBBellPitch|KBBellDuration|KBBellPercent,&kc);
58 XBell(dpy, 50);
59 XFlush(dpy);
60 wusleep(delay);
62 #endif
66 #ifdef DEMATERIALIZE_ICON
67 void
68 DoKaboom(WScreen *scr, Window win, int x, int y)
70 RImage *icon;
71 RImage *back;
72 RImage *image;
73 Pixmap pixmap;
74 XImage *ximage;
75 int i;
76 int w, h;
78 h = w = wPreferences.icon_size;
79 if (x < 0 || x + w > scr->scr_width || y < 0 || y + h > scr->scr_height)
80 return;
82 icon = RCreateImageFromDrawable(scr->rcontext, win, None);
83 if (!icon)
84 return;
86 XUnmapWindow(dpy, win);
87 XSync(dpy, False);
89 ximage = XGetImage(dpy, scr->root_win, x, y, w, h, AllPlanes, ZPixmap);
90 back = RCreateImageFromXImage(scr->rcontext, ximage, NULL);
91 XDestroyImage(ximage);
92 if (!back) {
93 RDestroyImage(icon);
94 return;
97 XSetClipMask(dpy, scr->copy_gc, None);
99 for (i=0; i<DEMATERIALIZE_STEPS; i++) {
100 XEvent foo;
101 if (XCheckTypedEvent(dpy, ButtonPress, &foo)) {
102 XPutBackEvent(dpy, &foo);
103 XClearWindow(dpy, scr->root_win);
104 break;
106 image = RCloneImage(back);
107 RCombineImagesWithOpaqueness(image, icon,
108 (DEMATERIALIZE_STEPS-1-i)*256/(DEMATERIALIZE_STEPS+2));
109 RConvertImage(scr->rcontext, image, &pixmap);
110 XCopyArea(dpy, pixmap, scr->root_win, scr->copy_gc, 0, 0, w, h, x, y);
111 XFreePixmap(dpy, pixmap);
112 XFlush(dpy);
113 wusleep(1000);
115 XClearArea(dpy, scr->root_win, x, y, w, h, False);
116 XFlush(dpy);
118 RDestroyImage(icon);
119 RDestroyImage(back);
121 #else /* !DEMATERIALIZE_ICON */
122 void
123 DoKaboom(WScreen *scr, Window win, int x, int y)
125 int i, j, k;
126 int sw=scr->scr_width, sh=scr->scr_height;
127 #define KAB_PRECISION 4
128 int px[PIECES];
129 short py[PIECES];
130 #ifdef ICON_KABOOM_EXTRA
131 short ptx[2][PIECES], pty[2][PIECES];
132 int ll;
133 #endif
134 char pvx[PIECES], pvy[PIECES];
135 /* in MkLinux/PPC gcc seems to think that char is unsigned? */
136 signed char ax[PIECES], ay[PIECES];
137 Pixmap tmp;
139 XSetClipMask(dpy, scr->copy_gc, None);
140 tmp = XCreatePixmap(dpy, scr->root_win, wPreferences.icon_size,
141 wPreferences.icon_size, scr->depth);
142 if (scr->w_visual == DefaultVisual(dpy, scr->screen))
143 XCopyArea(dpy, win, tmp, scr->copy_gc, 0, 0, wPreferences.icon_size,
144 wPreferences.icon_size, 0, 0);
145 else {
146 XImage *image;
148 image = XGetImage(dpy, win, 0, 0, wPreferences.icon_size,
149 wPreferences.icon_size, AllPlanes, ZPixmap);
150 if (!image) {
151 XUnmapWindow(dpy, win);
152 return;
154 XPutImage(dpy, tmp, scr->copy_gc, image, 0, 0, 0, 0,
155 wPreferences.icon_size, wPreferences.icon_size);
156 XDestroyImage(image);
159 for (i=0,k=0; i<wPreferences.icon_size/ICON_KABOOM_PIECE_SIZE && k<PIECES;
160 i++) {
161 for (j=0; j<wPreferences.icon_size/ICON_KABOOM_PIECE_SIZE && k<PIECES;
162 j++) {
163 if (rand()%2) {
164 ax[k]=i;
165 ay[k]=j;
166 px[k]=(x+i*ICON_KABOOM_PIECE_SIZE)<<KAB_PRECISION;
167 py[k]=y+j*ICON_KABOOM_PIECE_SIZE;
168 pvx[k]=rand()%(1<<(KAB_PRECISION+3))-(1<<(KAB_PRECISION+3))/2;
169 pvy[k]=-15-rand()%7;
170 #ifdef ICON_KABOOM_EXTRA
171 for (ll=0; ll<2; ll++) {
172 ptx[ll][k] = px[k];
173 pty[ll][k] = py[k];
175 #endif
176 k++;
177 } else {
178 ax[k]=-1;
183 XUnmapWindow(dpy, win);
185 j=k;
186 while (k>0) {
187 XEvent foo;
189 if (XCheckTypedEvent(dpy, ButtonPress, &foo)) {
190 XPutBackEvent(dpy, &foo);
191 XClearWindow(dpy, scr->root_win);
192 break;
195 for (i=0; i<j ; i++) {
196 if (ax[i]>=0) {
197 int _px = px[i]>>KAB_PRECISION;
198 #ifdef ICON_KABOOM_EXTRA
199 XClearArea(dpy, scr->root_win, ptx[1][i], pty[1][i],
200 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE,
201 False);
203 ptx[1][i] = ptx[0][i];
204 pty[1][i] = pty[0][i];
205 ptx[0][i] = _px;
206 pty[0][i] = py[i];
207 #else
208 XClearArea(dpy, scr->root_win, _px, py[i],
209 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE,
210 False);
211 #endif
212 px[i]+=pvx[i];
213 py[i]+=pvy[i];
214 _px = px[i]>>KAB_PRECISION;
215 pvy[i]++;
216 if (_px<-wPreferences.icon_size || _px>sw || py[i]>=sh) {
217 #ifdef ICON_KABOOM_EXTRA
218 if (py[i]>sh && _px<sw && _px>0) {
219 pvy[i] = -(pvy[i]/2);
220 if (abs(pvy[i]) > 3) {
221 py[i] = sh-ICON_KABOOM_PIECE_SIZE;
222 XCopyArea(dpy, tmp, scr->root_win, scr->copy_gc,
223 ax[i]*ICON_KABOOM_PIECE_SIZE,
224 ay[i]*ICON_KABOOM_PIECE_SIZE,
225 ICON_KABOOM_PIECE_SIZE,
226 ICON_KABOOM_PIECE_SIZE,
227 _px, py[i]);
228 } else {
229 ax[i] = -1;
231 } else {
232 ax[i] = -1;
234 if (ax[i]<0) {
235 for (ll=0; ll<2; ll++)
236 XClearArea(dpy, scr->root_win, ptx[ll][i], pty[ll][i],
237 ICON_KABOOM_PIECE_SIZE,
238 ICON_KABOOM_PIECE_SIZE, False);
239 k--;
241 #else /* !ICON_KABOOM_EXTRA */
242 ax[i]=-1;
243 k--;
244 #endif /* !ICON_KABOOM_EXTRA */
245 } else {
246 XCopyArea(dpy, tmp, scr->root_win, scr->copy_gc,
247 ax[i]*ICON_KABOOM_PIECE_SIZE, ay[i]*ICON_KABOOM_PIECE_SIZE,
248 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE,
249 _px, py[i]);
254 XFlush(dpy);
255 #ifdef SPEAKER_SOUND
256 play(dpy, 100+rand()%250, 12);
257 #else
258 # if (MINIATURIZE_ANIMATION_DELAY_Z > 0)
259 wusleep(MINIATURIZE_ANIMATION_DELAY_Z*2);
260 # endif
261 #endif
264 XFreePixmap(dpy, tmp);
266 #endif /* !DEMATERIALIZE_ICON */
269 Pixmap
270 MakeGhostDock(WDock *dock, int sx, int dx, int y)
272 WScreen *scr = dock->screen_ptr;
273 XImage *img;
274 RImage *back, *dock_image;
275 Pixmap pixmap;
276 int i, virtual_tiles, h, j, n;
277 unsigned long red_mask, green_mask, blue_mask;
279 virtual_tiles = 0;
280 for (i=0; i<dock->max_icons; i++) {
281 if (dock->icon_array[i]!=NULL &&
282 dock->icon_array[i]->yindex > virtual_tiles)
283 virtual_tiles = dock->icon_array[i]->yindex;
285 virtual_tiles++;
286 h = virtual_tiles * wPreferences.icon_size;
287 h = (y + h > scr->scr_height) ? scr->scr_height-y : h;
288 virtual_tiles = h / wPreferences.icon_size; /* The visible ones */
289 if (h % wPreferences.icon_size)
290 virtual_tiles++; /* There is one partially visible tile at end */
292 img=XGetImage(dpy, scr->root_win, dx, y, wPreferences.icon_size, h,
293 AllPlanes, ZPixmap);
294 if (!img)
295 return None;
297 red_mask = img->red_mask;
298 green_mask = img->green_mask;
299 blue_mask = img->blue_mask;
301 back = RCreateImageFromXImage(scr->rcontext, img, NULL);
302 XDestroyImage(img);
303 if (!back) {
304 return None;
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 Pixmap which;
311 j = dock->icon_array[i]->yindex * wPreferences.icon_size;
312 n = (h - j < wPreferences.icon_size) ? h - j :
313 wPreferences.icon_size;
314 if (dock->icon_array[i]->icon->pixmap)
315 which = dock->icon_array[i]->icon->pixmap;
316 else
317 which = dock->icon_array[i]->icon->core->window;
319 img=XGetImage(dpy, which, 0, 0, wPreferences.icon_size, n,
320 AllPlanes, ZPixmap);
322 if (!img){
323 RDestroyImage(back);
324 return None;
326 img->red_mask = red_mask;
327 img->green_mask = green_mask;
328 img->blue_mask = blue_mask;
330 dock_image = RCreateImageFromXImage(scr->rcontext, img, NULL);
331 XDestroyImage(img);
332 if (!dock_image) {
333 RDestroyImage(back);
334 return None;
336 RCombineAreaWithOpaqueness(back, dock_image, 0, 0,
337 wPreferences.icon_size, n,
338 0, j, 30 * 256 / 100);
339 RDestroyImage(dock_image);
344 RConvertImage(scr->rcontext, back, &pixmap);
346 RDestroyImage(back);
348 return pixmap;
352 Pixmap
353 MakeGhostIcon(WScreen *scr, Drawable drawable)
355 RImage *back;
356 RColor color;
357 Pixmap pixmap;
360 if (!drawable)
361 return None;
363 back = RCreateImageFromDrawable(scr->rcontext, drawable, None);
364 if (!back)
365 return None;
367 color.red = 0xff;
368 color.green = 0xff;
369 color.blue = 0xff;
370 color.alpha = 200;
372 RClearImage(back, &color);
373 RConvertImage(scr->rcontext, back, &pixmap);
375 RDestroyImage(back);
377 return pixmap;
382 #ifdef WINDOW_BIRTH_ZOOM
383 void
384 DoWindowBirth(WWindow *wwin)
386 int width = wwin->frame->core->width;
387 int height = wwin->frame->core->height;
388 int w = WMIN(width, 20);
389 int h = WMIN(height, 20);
390 int x, y;
391 int dw, dh;
392 int i;
393 time_t time0 = time(NULL);
395 dw = (width-w)/WINDOW_BIRTH_STEPS;
396 dh = (height-h)/WINDOW_BIRTH_STEPS;
398 x = wwin->frame_x + (width-w)/2;
399 y = wwin->frame_y + (height-h)/2;
401 XMoveResizeWindow(dpy, wwin->frame->core->window, x, y, w, h);
403 XMapWindow(dpy, wwin->frame->core->window);
405 XFlush(dpy);
406 for (i=0; i<WINDOW_BIRTH_STEPS; i++) {
407 x -= dw/2 + dw%2;
408 y -= dh/2 + dh%2;
409 w += dw;
410 h += dh;
411 XMoveResizeWindow(dpy, wwin->frame->core->window, x, y, w, h);
412 XFlush(dpy);
413 /* a timeout */
414 if (time(NULL)-time0 > MAX_ANIMATION_TIME)
415 break;
418 XMoveResizeWindow(dpy, wwin->frame->core->window,
419 wwin->frame_x, wwin->frame_y, width, height);
420 XFlush(dpy);
422 #else
423 void
424 DoWindowBirth(WWindow *wwin)
426 /* dummy stub */
428 #endif
432 #ifdef SILLYNESS
433 static WMPixmap *data[12];
436 static Bool
437 loadData(WScreen *scr)
439 FILE *f;
440 int i;
441 RImage *image;
442 Pixmap d[12];
444 f = fopen(PKGDATADIR"/xtree.dat", "r");
445 if (!f)
446 return False;
448 image = RCreateImage(50, 50, False);
449 if (!image) {
450 fclose(f);
451 return False;
454 for (i = 0; i < 12; i++) {
455 if (fread(image->data[0], 50*50, 1, f)!=1) {
456 goto error;
458 if (fread(image->data[1], 50*50, 1, f)!=1) {
459 goto error;
461 if (fread(image->data[2], 50*50, 1, f)!=1) {
462 goto error;
464 if (!RConvertImage(scr->rcontext, image, &(d[i]))) {
465 goto error;
468 RDestroyImage(image);
470 fclose(f);
472 for (i=0; i<12; i++) {
473 data[i] = WMCreatePixmapFromXPixmaps(scr->wmscreen, d[i], None, 50, 50,
474 scr->w_depth);
477 return True;
479 error:
480 RDestroyImage(image);
482 fclose(f);
484 while (--i > 0) {
485 XFreePixmap(dpy, d[i]);
488 return False;
492 WMPixmap*
493 DoXThing(WWindow *wwin)
495 static int order = 0;
497 order++;
499 return data[order % 12];
503 Bool
504 InitXThing(WScreen *scr)
506 time_t t;
507 struct tm *l;
508 static int i = 0;
510 if (i)
511 return True;
513 t = time(NULL);
514 l = localtime(&t);
515 if ((l->tm_mon!=12||l->tm_mday<24||l->tm_mday>26)) {
516 return False;
519 if (!loadData(scr))
520 return False;
522 i = 1;
524 return True;
527 #endif /* SILLYNESS */
530 #ifdef GHOST_WINDOW_MOVE
531 RImage*
532 InitGhostWindowMove(WScreen *scr)
534 RXImage *ximg;
535 RImage *img;
537 ximg = RGetXImage(scr->rcontext, scr->root_win, 0, 0,
538 scr->scr_width, scr->scr_height);
539 img = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
540 RDestroyXImage(dpy, ximg);
542 return img;
545 #endif /* GHOST_WINDOW_MOVE */