dunno
[wmaker-crm.git] / src / superfluous.c
blob5fad30df1091b8b92d6231f91f5166ca7082084f
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;
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 #endif /* DEMATERIALIZE_ICON */
149 #ifdef NORMAL_ICON_KABOOM
150 void
151 DoKaboom(WScreen *scr, Window win, int x, int y)
153 int i, j, k;
154 int sw=scr->scr_width, sh=scr->scr_height;
155 #define KAB_PRECISION 4
156 int px[PIECES];
157 short py[PIECES];
158 #ifdef ICON_KABOOM_EXTRA
159 short ptx[2][PIECES], pty[2][PIECES];
160 int ll;
161 #endif
162 char pvx[PIECES], pvy[PIECES];
163 /* in MkLinux/PPC gcc seems to think that char is unsigned? */
164 signed char ax[PIECES], ay[PIECES];
165 Pixmap tmp;
167 XSetClipMask(dpy, scr->copy_gc, None);
168 tmp = XCreatePixmap(dpy, scr->root_win, wPreferences.icon_size,
169 wPreferences.icon_size, scr->depth);
170 if (scr->w_visual == DefaultVisual(dpy, scr->screen))
171 XCopyArea(dpy, win, tmp, scr->copy_gc, 0, 0, wPreferences.icon_size,
172 wPreferences.icon_size, 0, 0);
173 else {
174 XImage *image;
176 image = XGetImage(dpy, win, 0, 0, wPreferences.icon_size,
177 wPreferences.icon_size, AllPlanes, ZPixmap);
178 if (!image) {
179 XUnmapWindow(dpy, win);
180 return;
182 XPutImage(dpy, tmp, scr->copy_gc, image, 0, 0, 0, 0,
183 wPreferences.icon_size, wPreferences.icon_size);
184 XDestroyImage(image);
187 for (i=0,k=0; i<wPreferences.icon_size/ICON_KABOOM_PIECE_SIZE && k<PIECES;
188 i++) {
189 for (j=0; j<wPreferences.icon_size/ICON_KABOOM_PIECE_SIZE && k<PIECES;
190 j++) {
191 if (rand()%2) {
192 ax[k]=i;
193 ay[k]=j;
194 px[k]=(x+i*ICON_KABOOM_PIECE_SIZE)<<KAB_PRECISION;
195 py[k]=y+j*ICON_KABOOM_PIECE_SIZE;
196 pvx[k]=rand()%(1<<(KAB_PRECISION+3))-(1<<(KAB_PRECISION+3))/2;
197 pvy[k]=-15-rand()%7;
198 #ifdef ICON_KABOOM_EXTRA
199 for (ll=0; ll<2; ll++) {
200 ptx[ll][k] = px[k];
201 pty[ll][k] = py[k];
203 #endif
204 k++;
205 } else {
206 ax[k]=-1;
211 XUnmapWindow(dpy, win);
213 j=k;
214 while (k>0) {
215 XEvent foo;
217 if (XCheckTypedEvent(dpy, ButtonPress, &foo)) {
218 XPutBackEvent(dpy, &foo);
219 XClearWindow(dpy, scr->root_win);
220 break;
223 for (i=0; i<j ; i++) {
224 if (ax[i]>=0) {
225 int _px = px[i]>>KAB_PRECISION;
226 #ifdef ICON_KABOOM_EXTRA
227 XClearArea(dpy, scr->root_win, ptx[1][i], pty[1][i],
228 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE,
229 False);
231 ptx[1][i] = ptx[0][i];
232 pty[1][i] = pty[0][i];
233 ptx[0][i] = _px;
234 pty[0][i] = py[i];
235 #else
236 XClearArea(dpy, scr->root_win, _px, py[i],
237 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE,
238 False);
239 #endif
240 px[i]+=pvx[i];
241 py[i]+=pvy[i];
242 _px = px[i]>>KAB_PRECISION;
243 pvy[i]++;
244 if (_px<-wPreferences.icon_size || _px>sw || py[i]>=sh) {
245 #ifdef ICON_KABOOM_EXTRA
246 if (py[i]>sh && _px<sw && _px>0) {
247 pvy[i] = -(pvy[i]/2);
248 if (abs(pvy[i]) > 3) {
249 py[i] = sh-ICON_KABOOM_PIECE_SIZE;
250 XCopyArea(dpy, tmp, scr->root_win, scr->copy_gc,
251 ax[i]*ICON_KABOOM_PIECE_SIZE,
252 ay[i]*ICON_KABOOM_PIECE_SIZE,
253 ICON_KABOOM_PIECE_SIZE,
254 ICON_KABOOM_PIECE_SIZE,
255 _px, py[i]);
256 } else {
257 ax[i] = -1;
259 } else {
260 ax[i] = -1;
262 if (ax[i]<0) {
263 for (ll=0; ll<2; ll++)
264 XClearArea(dpy, scr->root_win, ptx[ll][i], pty[ll][i],
265 ICON_KABOOM_PIECE_SIZE,
266 ICON_KABOOM_PIECE_SIZE, False);
267 k--;
269 #else /* !ICON_KABOOM_EXTRA */
270 ax[i]=-1;
271 k--;
272 #endif /* !ICON_KABOOM_EXTRA */
273 } else {
274 XCopyArea(dpy, tmp, scr->root_win, scr->copy_gc,
275 ax[i]*ICON_KABOOM_PIECE_SIZE, ay[i]*ICON_KABOOM_PIECE_SIZE,
276 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE,
277 _px, py[i]);
282 XFlush(dpy);
283 #ifdef SPEAKER_SOUND
284 play(dpy, 100+rand()%250, 12);
285 #else
286 # if (MINIATURIZE_ANIMATION_DELAY_Z > 0)
287 wusleep(MINIATURIZE_ANIMATION_DELAY_Z*2);
288 # endif
289 #endif
292 XFreePixmap(dpy, tmp);
294 #endif /* NORMAL_ICON_KABOOM */
297 Pixmap
298 MakeGhostDock(WDock *dock, int sx, int dx, int y)
300 WScreen *scr = dock->screen_ptr;
301 XImage *img;
302 RImage *back, *dock_image;
303 Pixmap pixmap;
304 int i, virtual_tiles, h, j, n;
305 unsigned long red_mask, green_mask, blue_mask;
307 virtual_tiles = 0;
308 for (i=0; i<dock->max_icons; i++) {
309 if (dock->icon_array[i]!=NULL &&
310 dock->icon_array[i]->yindex > virtual_tiles)
311 virtual_tiles = dock->icon_array[i]->yindex;
313 virtual_tiles++;
314 h = virtual_tiles * wPreferences.icon_size;
315 h = (y + h > scr->scr_height) ? scr->scr_height-y : h;
316 virtual_tiles = h / wPreferences.icon_size; /* The visible ones */
317 if (h % wPreferences.icon_size)
318 virtual_tiles++; /* There is one partially visible tile at end */
320 img=XGetImage(dpy, scr->root_win, dx, y, wPreferences.icon_size, h,
321 AllPlanes, ZPixmap);
322 if (!img)
323 return None;
325 red_mask = img->red_mask;
326 green_mask = img->green_mask;
327 blue_mask = img->blue_mask;
329 back = RCreateImageFromXImage(scr->rcontext, img, NULL);
330 XDestroyImage(img);
331 if (!back) {
332 return None;
335 for (i=0;i<dock->max_icons;i++) {
336 if (dock->icon_array[i]!=NULL &&
337 dock->icon_array[i]->yindex < virtual_tiles) {
338 Pixmap which;
339 j = dock->icon_array[i]->yindex * wPreferences.icon_size;
340 n = (h - j < wPreferences.icon_size) ? h - j :
341 wPreferences.icon_size;
342 if (dock->icon_array[i]->icon->pixmap)
343 which = dock->icon_array[i]->icon->pixmap;
344 else
345 which = dock->icon_array[i]->icon->core->window;
347 img=XGetImage(dpy, which, 0, 0, wPreferences.icon_size, n,
348 AllPlanes, ZPixmap);
350 if (!img){
351 RDestroyImage(back);
352 return None;
354 img->red_mask = red_mask;
355 img->green_mask = green_mask;
356 img->blue_mask = blue_mask;
358 dock_image = RCreateImageFromXImage(scr->rcontext, img, NULL);
359 XDestroyImage(img);
360 if (!dock_image) {
361 RDestroyImage(back);
362 return None;
364 RCombineAreaWithOpaqueness(back, dock_image, 0, 0,
365 wPreferences.icon_size, n,
366 0, j, 30 * 256 / 100);
367 RDestroyImage(dock_image);
372 RConvertImage(scr->rcontext, back, &pixmap);
374 RDestroyImage(back);
376 return pixmap;
380 Pixmap
381 MakeGhostIcon(WScreen *scr, Drawable drawable)
383 RImage *back;
384 RColor color;
385 Pixmap pixmap;
388 if (!drawable)
389 return None;
391 back = RCreateImageFromDrawable(scr->rcontext, drawable, None);
392 if (!back)
393 return None;
395 color.red = 0xff;
396 color.green = 0xff;
397 color.blue = 0xff;
398 color.alpha = 200;
400 RClearImage(back, &color);
401 RConvertImage(scr->rcontext, back, &pixmap);
403 RDestroyImage(back);
405 return pixmap;
410 #ifdef WINDOW_BIRTH_ZOOM
411 void
412 DoWindowBirth(WWindow *wwin)
414 int width = wwin->frame->core->width;
415 int height = wwin->frame->core->height;
416 int w = WMIN(width, 20);
417 int h = WMIN(height, 20);
418 int x, y;
419 int dw, dh;
420 int i;
421 time_t time0 = time(NULL);
423 dw = (width-w)/WINDOW_BIRTH_STEPS;
424 dh = (height-h)/WINDOW_BIRTH_STEPS;
426 x = wwin->frame_x + (width-w)/2;
427 y = wwin->frame_y + (height-h)/2;
429 XMoveResizeWindow(dpy, wwin->frame->core->window, x, y, w, h);
431 XMapWindow(dpy, wwin->frame->core->window);
433 XFlush(dpy);
434 for (i=0; i<WINDOW_BIRTH_STEPS; i++) {
435 x -= dw/2 + dw%2;
436 y -= dh/2 + dh%2;
437 w += dw;
438 h += dh;
439 XMoveResizeWindow(dpy, wwin->frame->core->window, x, y, w, h);
440 XFlush(dpy);
441 /* a timeout */
442 if (time(NULL)-time0 > MAX_ANIMATION_TIME)
443 break;
446 XMoveResizeWindow(dpy, wwin->frame->core->window,
447 wwin->frame_x, wwin->frame_y, width, height);
448 XFlush(dpy);
450 #else
451 void
452 DoWindowBirth(WWindow *wwin)
454 /* dummy stub */
456 #endif
460 #ifdef SILLYNESS
461 static WMPixmap *data[12];
464 static Bool
465 loadData(WScreen *scr)
467 FILE *f;
468 int i;
469 RImage *image;
470 Pixmap d[12];
472 f = fopen(PKGDATADIR"/xtree.dat", "r");
473 if (!f)
474 return False;
476 image = RCreateImage(50, 50, False);
477 if (!image) {
478 fclose(f);
479 return False;
482 for (i = 0; i < 12; i++) {
483 if (fread(image->data[0], 50*50, 1, f)!=1) {
484 goto error;
486 if (fread(image->data[1], 50*50, 1, f)!=1) {
487 goto error;
489 if (fread(image->data[2], 50*50, 1, f)!=1) {
490 goto error;
492 if (!RConvertImage(scr->rcontext, image, &(d[i]))) {
493 goto error;
496 RDestroyImage(image);
498 fclose(f);
500 for (i=0; i<12; i++) {
501 data[i] = WMCreatePixmapFromXPixmaps(scr->wmscreen, d[i], None, 50, 50,
502 scr->w_depth);
505 return True;
507 error:
508 RDestroyImage(image);
510 fclose(f);
512 while (--i > 0) {
513 XFreePixmap(dpy, d[i]);
516 return False;
520 WMPixmap*
521 DoXThing(WWindow *wwin)
523 static int order = 0;
525 order++;
527 return data[order % 12];
531 Bool
532 InitXThing(WScreen *scr)
534 time_t t;
535 struct tm *l;
536 static int i = 0;
538 if (i)
539 return True;
541 t = time(NULL);
542 l = localtime(&t);
543 if ((l->tm_mon!=12||l->tm_mday<24||l->tm_mday>26)) {
544 return False;
547 if (!loadData(scr))
548 return False;
550 i = 1;
552 return True;
555 #endif /* SILLYNESS */
558 #ifdef GHOST_WINDOW_MOVE
559 RImage*
560 InitGhostWindowMove(WScreen *scr)
562 RXImage *ximg;
563 RImage *img;
565 ximg = RGetXImage(scr->rcontext, scr->root_win, 0, 0,
566 scr->scr_width, scr->scr_height);
567 img = RCreateImageFromXImage(scr->rcontext, ximg->image, NULL);
568 RDestroyXImage(dpy, ximg);
570 return img;
573 #endif /* GHOST_WINDOW_MOVE */