Christmas does not last forever
[wmaker-crm.git] / src / superfluous.c
blob20a00a394bec7280881c4ba6f22669bb88febeb0
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
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"
45 extern WPreferences wPreferences;
47 #ifdef SPEAKER_SOUND
48 static void play(Display * dpy, int pitch, int delay)
50 XKeyboardControl kc;
52 kc.bell_pitch = pitch;
53 kc.bell_percent = 50;
54 kc.bell_duration = delay;
55 XChangeKeyboardControl(dpy, KBBellPitch | KBBellDuration | KBBellPercent, &kc);
56 XBell(dpy, 50);
57 XFlush(dpy);
58 wusleep(delay);
60 #endif
62 #ifdef DEMATERIALIZE_ICON
63 void DoKaboom(WScreen * scr, Window win, int x, int y)
65 RImage *icon;
66 RImage *back;
67 RImage *image;
68 Pixmap pixmap;
69 XImage *ximage;
70 GC gc;
71 XGCValues gcv;
72 int i;
73 int w, h;
74 int run;
75 XEvent event;
77 h = w = wPreferences.icon_size;
78 if (x < 0 || x + w > scr->scr_width || y < 0 || y + h > scr->scr_height)
79 return;
81 icon = RCreateImageFromDrawable(scr->rcontext, win, None);
82 if (!icon)
83 return;
85 gcv.foreground = scr->white_pixel;
86 gcv.background = scr->black_pixel;
87 gcv.graphics_exposures = False;
88 gcv.subwindow_mode = IncludeInferiors;
89 gc = XCreateGC(dpy, scr->w_win, GCForeground | GCBackground | GCSubwindowMode | GCGraphicsExposures, &gcv);
91 XGrabServer(dpy);
92 RConvertImage(scr->rcontext, icon, &pixmap);
93 XUnmapWindow(dpy, win);
95 ximage = XGetImage(dpy, scr->root_win, x, y, w, h, AllPlanes, ZPixmap);
96 XCopyArea(dpy, pixmap, scr->root_win, gc, 0, 0, w, h, x, y);
97 XFreePixmap(dpy, pixmap);
99 back = RCreateImageFromXImage(scr->rcontext, ximage, NULL);
100 XDestroyImage(ximage);
101 if (!back) {
102 RReleaseImage(icon);
103 return;
106 for (i = 0, run = 0; i < DEMATERIALIZE_STEPS; i++) {
107 XEvent foo;
108 if (!run && XCheckTypedEvent(dpy, ButtonPress, &foo)) {
109 run = 1;
110 XPutBackEvent(dpy, &foo);
112 image = RCloneImage(back);
113 RCombineImagesWithOpaqueness(image, icon,
114 (DEMATERIALIZE_STEPS - 1 - i) * 256 / (DEMATERIALIZE_STEPS + 2));
115 RConvertImage(scr->rcontext, image, &pixmap);
116 XCopyArea(dpy, pixmap, scr->root_win, gc, 0, 0, w, h, x, y);
117 XFreePixmap(dpy, pixmap);
118 XFlush(dpy);
119 if (!run)
120 wusleep(1000);
123 while (XCheckTypedEvent(dpy, MotionNotify, &event)) {
125 XFlush(dpy);
127 XUngrabServer(dpy);
128 XFreeGC(dpy, gc);
129 RReleaseImage(icon);
130 RReleaseImage(back);
132 #endif /* DEMATERIALIZE_ICON */
134 #ifdef NORMAL_ICON_KABOOM
135 void DoKaboom(WScreen * scr, Window win, int x, int y)
137 int i, j, k;
138 int sw = scr->scr_width, sh = scr->scr_height;
139 #define KAB_PRECISION 4
140 int px[PIECES];
141 short py[PIECES];
142 #ifdef ICON_KABOOM_EXTRA
143 short ptx[2][PIECES], pty[2][PIECES];
144 int ll;
145 #endif
146 char pvx[PIECES], pvy[PIECES];
147 /* in MkLinux/PPC gcc seems to think that char is unsigned? */
148 signed char ax[PIECES], ay[PIECES];
149 Pixmap tmp;
151 XSetClipMask(dpy, scr->copy_gc, None);
152 tmp = XCreatePixmap(dpy, scr->root_win, wPreferences.icon_size, wPreferences.icon_size, scr->depth);
153 if (scr->w_visual == DefaultVisual(dpy, scr->screen))
154 XCopyArea(dpy, win, tmp, scr->copy_gc, 0, 0, wPreferences.icon_size, wPreferences.icon_size, 0, 0);
155 else {
156 XImage *image;
158 image = XGetImage(dpy, win, 0, 0, wPreferences.icon_size,
159 wPreferences.icon_size, AllPlanes, ZPixmap);
160 if (!image) {
161 XUnmapWindow(dpy, win);
162 return;
164 XPutImage(dpy, tmp, scr->copy_gc, image, 0, 0, 0, 0,
165 wPreferences.icon_size, wPreferences.icon_size);
166 XDestroyImage(image);
169 for (i = 0, k = 0; i < wPreferences.icon_size / ICON_KABOOM_PIECE_SIZE && k < PIECES; i++) {
170 for (j = 0; j < wPreferences.icon_size / ICON_KABOOM_PIECE_SIZE && k < PIECES; j++) {
171 if (rand() % 2) {
172 ax[k] = i;
173 ay[k] = j;
174 px[k] = (x + i * ICON_KABOOM_PIECE_SIZE) << KAB_PRECISION;
175 py[k] = y + j * ICON_KABOOM_PIECE_SIZE;
176 pvx[k] = rand() % (1 << (KAB_PRECISION + 3)) - (1 << (KAB_PRECISION + 3)) / 2;
177 pvy[k] = -15 - rand() % 7;
178 #ifdef ICON_KABOOM_EXTRA
179 for (ll = 0; ll < 2; ll++) {
180 ptx[ll][k] = px[k];
181 pty[ll][k] = py[k];
183 #endif
184 k++;
185 } else {
186 ax[k] = -1;
191 XUnmapWindow(dpy, win);
193 j = k;
194 while (k > 0) {
195 XEvent foo;
197 if (XCheckTypedEvent(dpy, ButtonPress, &foo)) {
198 XPutBackEvent(dpy, &foo);
199 XClearWindow(dpy, scr->root_win);
200 break;
203 for (i = 0; i < j; i++) {
204 if (ax[i] >= 0) {
205 int _px = px[i] >> KAB_PRECISION;
206 #ifdef ICON_KABOOM_EXTRA
207 XClearArea(dpy, scr->root_win, ptx[1][i], pty[1][i],
208 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE, False);
210 ptx[1][i] = ptx[0][i];
211 pty[1][i] = pty[0][i];
212 ptx[0][i] = _px;
213 pty[0][i] = py[i];
214 #else
215 XClearArea(dpy, scr->root_win, _px, py[i],
216 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE, False);
217 #endif
218 px[i] += pvx[i];
219 py[i] += pvy[i];
220 _px = px[i] >> KAB_PRECISION;
221 pvy[i]++;
222 if (_px < -wPreferences.icon_size || _px > sw || py[i] >= sh) {
223 #ifdef ICON_KABOOM_EXTRA
224 if (py[i] > sh && _px < sw && _px > 0) {
225 pvy[i] = -(pvy[i] / 2);
226 if (abs(pvy[i]) > 3) {
227 py[i] = sh - ICON_KABOOM_PIECE_SIZE;
228 XCopyArea(dpy, tmp, scr->root_win, scr->copy_gc,
229 ax[i] * ICON_KABOOM_PIECE_SIZE,
230 ay[i] * ICON_KABOOM_PIECE_SIZE,
231 ICON_KABOOM_PIECE_SIZE,
232 ICON_KABOOM_PIECE_SIZE, _px, py[i]);
233 } else {
234 ax[i] = -1;
236 } else {
237 ax[i] = -1;
239 if (ax[i] < 0) {
240 for (ll = 0; ll < 2; ll++)
241 XClearArea(dpy, scr->root_win, ptx[ll][i], pty[ll][i],
242 ICON_KABOOM_PIECE_SIZE,
243 ICON_KABOOM_PIECE_SIZE, False);
244 k--;
246 #else /* !ICON_KABOOM_EXTRA */
247 ax[i] = -1;
248 k--;
249 #endif /* !ICON_KABOOM_EXTRA */
250 } else {
251 XCopyArea(dpy, tmp, scr->root_win, scr->copy_gc,
252 ax[i] * ICON_KABOOM_PIECE_SIZE, ay[i] * ICON_KABOOM_PIECE_SIZE,
253 ICON_KABOOM_PIECE_SIZE, ICON_KABOOM_PIECE_SIZE, _px, py[i]);
258 XFlush(dpy);
259 #ifdef SPEAKER_SOUND
260 play(dpy, 100 + rand() % 250, 12);
261 #else
262 # if (MINIATURIZE_ANIMATION_DELAY_Z > 0)
263 wusleep(MINIATURIZE_ANIMATION_DELAY_Z * 2);
264 # endif
265 #endif
268 XFreePixmap(dpy, tmp);
270 #endif /* NORMAL_ICON_KABOOM */
272 Pixmap MakeGhostDock(WDock * dock, int sx, int dx, int y)
274 WScreen *scr = dock->screen_ptr;
275 XImage *img;
276 RImage *back, *dock_image;
277 Pixmap pixmap;
278 int i, virtual_tiles, h, j, n;
279 unsigned long red_mask, green_mask, blue_mask;
281 virtual_tiles = 0;
282 for (i = 0; i < dock->max_icons; i++) {
283 if (dock->icon_array[i] != NULL && dock->icon_array[i]->yindex > virtual_tiles)
284 virtual_tiles = dock->icon_array[i]->yindex;
286 virtual_tiles++;
287 h = virtual_tiles * wPreferences.icon_size;
288 h = (y + h > scr->scr_height) ? scr->scr_height - y : h;
289 virtual_tiles = h / wPreferences.icon_size; /* The visible ones */
290 if (h % wPreferences.icon_size)
291 virtual_tiles++; /* There is one partially visible tile at end */
293 img = XGetImage(dpy, scr->root_win, dx, y, wPreferences.icon_size, h, 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 && dock->icon_array[i]->yindex < virtual_tiles) {
309 Pixmap which;
310 j = dock->icon_array[i]->yindex * wPreferences.icon_size;
311 n = (h - j < wPreferences.icon_size) ? h - j : wPreferences.icon_size;
312 if (dock->icon_array[i]->icon->pixmap)
313 which = dock->icon_array[i]->icon->pixmap;
314 else
315 which = dock->icon_array[i]->icon->core->window;
317 img = XGetImage(dpy, which, 0, 0, wPreferences.icon_size, n, AllPlanes, ZPixmap);
319 if (!img) {
320 RReleaseImage(back);
321 return None;
323 img->red_mask = red_mask;
324 img->green_mask = green_mask;
325 img->blue_mask = blue_mask;
327 dock_image = RCreateImageFromXImage(scr->rcontext, img, NULL);
328 XDestroyImage(img);
329 if (!dock_image) {
330 RReleaseImage(back);
331 return None;
333 RCombineAreaWithOpaqueness(back, dock_image, 0, 0,
334 wPreferences.icon_size, n, 0, j, 30 * 256 / 100);
335 RReleaseImage(dock_image);
339 RConvertImage(scr->rcontext, back, &pixmap);
341 RReleaseImage(back);
343 return pixmap;
346 Pixmap MakeGhostIcon(WScreen * scr, Drawable drawable)
348 RImage *back;
349 RColor color;
350 Pixmap pixmap;
352 if (!drawable)
353 return None;
355 back = RCreateImageFromDrawable(scr->rcontext, drawable, None);
356 if (!back)
357 return None;
359 color.red = 0xff;
360 color.green = 0xff;
361 color.blue = 0xff;
362 color.alpha = 200;
364 RClearImage(back, &color);
365 RConvertImage(scr->rcontext, back, &pixmap);
367 RReleaseImage(back);
369 return pixmap;
372 #ifdef WINDOW_BIRTH_ZOOM
373 void DoWindowBirth(WWindow * wwin)
375 int width = wwin->frame->core->width;
376 int height = wwin->frame->core->height;
377 int w = WMIN(width, 20);
378 int h = WMIN(height, 20);
379 int x, y;
380 int dw, dh;
381 int i;
382 time_t time0 = time(NULL);
384 dw = (width - w) / WINDOW_BIRTH_STEPS;
385 dh = (height - h) / WINDOW_BIRTH_STEPS;
387 x = wwin->frame_x + (width - w) / 2;
388 y = wwin->frame_y + (height - h) / 2;
390 XMoveResizeWindow(dpy, wwin->frame->core->window, x, y, w, h);
392 XMapWindow(dpy, wwin->frame->core->window);
394 XFlush(dpy);
395 for (i = 0; i < WINDOW_BIRTH_STEPS; i++) {
396 x -= dw / 2 + dw % 2;
397 y -= dh / 2 + dh % 2;
398 w += dw;
399 h += dh;
400 XMoveResizeWindow(dpy, wwin->frame->core->window, x, y, w, h);
401 XFlush(dpy);
402 /* a timeout */
403 if (time(NULL) - time0 > MAX_ANIMATION_TIME)
404 break;
407 XMoveResizeWindow(dpy, wwin->frame->core->window, wwin->frame_x, wwin->frame_y, width, height);
408 XFlush(dpy);
410 #else
411 #ifdef WINDOW_BIRTH_ZOOM2
412 extern void animateResize();
414 void DoWindowBirth(WWindow * wwin)
416 /* dummy stub */
418 int center_x, center_y;
419 int width = wwin->frame->core->width;
420 int height = wwin->frame->core->height;
421 int w = WMIN(width, 20);
422 int h = WMIN(height, 20);
423 WScreen *scr = wwin->screen_ptr;
425 center_x = wwin->frame_x + (width - w) / 2;
426 center_y = wwin->frame_y + (height - h) / 2;
428 animateResize(scr, center_x, center_y, 1, 1, wwin->frame_x, wwin->frame_y, width, height, 0);
430 #else
431 void DoWindowBirth(WWindow * wwin)
433 /* dummy stub */
435 #endif
436 #endif
438 #ifdef SILLYNESS
439 static WMPixmap *data[12];
441 static Bool loadData(WScreen * scr)
443 FILE *f;
444 int i;
445 RImage *image;
446 Pixmap d[12];
448 f = fopen(PKGDATADIR "/xtree.dat", "rb");
449 if (!f)
450 return False;
452 image = RCreateImage(50, 50, False);
453 if (!image) {
454 fclose(f);
455 return False;
458 for (i = 0; i < 12; i++) {
459 if (fread(image->data, 50 * 50 * 3, 1, f) != 1) {
460 goto error;
462 if (!RConvertImage(scr->rcontext, image, &(d[i]))) {
463 goto error;
466 RReleaseImage(image);
468 fclose(f);
470 for (i = 0; i < 12; i++) {
471 data[i] = WMCreatePixmapFromXPixmaps(scr->wmscreen, d[i], None, 50, 50, scr->w_depth);
474 return True;
476 error:
477 RReleaseImage(image);
479 fclose(f);
481 while (--i > 0) {
482 XFreePixmap(dpy, d[i]);
485 return False;
488 WMPixmap *DoXThing(WWindow * wwin)
490 static int order = 0;
492 order++;
494 return data[order % 12];
497 Bool InitXThing(WScreen * scr)
499 time_t t;
500 struct tm *l;
501 static int i = 0;
503 t = time(NULL);
504 l = localtime(&t);
505 if ((l->tm_mon != 11 || l->tm_mday < 24 || l->tm_mday > 26))
506 return False;
508 if (i)
509 return True;
511 if (!loadData(scr))
512 return False;
514 i = 1;
516 return True;
519 #endif /* SILLYNESS */
521 #ifdef GHOST_WINDOW_MOVE
523 typedef struct {
524 WScreen *scr;
526 int width, height;
528 int iniX, iniY;
529 int boxX, boxY;
531 Window window;
533 RXImage *winImage;
535 RXImage *backImage;
537 /* the combined image */
538 RXImage *image;
539 Pixmap pixmap;
541 } _GhostWindowData;
543 _GhostWindowData *InitGhostWindowMove(WWindow * wwin)
545 _GhostWindowData *gdata;
546 WScreen *scr = wwin->screen_ptr;
547 unsigned short *ptr;
548 unsigned short mask;
549 int i;
551 gdata = wmalloc(sizeof(_GhostWindowData));
553 gdata->width = wwin->frame->core->width;
554 gdata->height = wwin->frame->core->height;
556 gdata->iniX = wwin->frame_x;
557 gdata->iniY = wwin->frame_y;
559 gdata->boxX = wwin->frame_x;
560 gdata->boxY = wwin->frame_y;
562 gdata->window =
563 XCreateSimpleWindow(dpy, scr->root_win, wwin->frame_x, wwin->frame_y,
564 gdata->width, gdata->height, 0, 0, 0);
566 gdata->winImage = RGetXImage(scr->rcontext, wwin->frame->core->window, 0, 0, gdata->width, gdata->height);
568 gdata->backImage = RCreateXImage(scr->rcontext, scr->w_depth, gdata->width, gdata->height);
570 memcpy(gdata->backImage->image->data, gdata->winImage->image->data,
571 gdata->winImage->image->bytes_per_line * gdata->height);
573 gdata->image = RCreateXImage(scr->rcontext, scr->w_depth, gdata->width, gdata->height);
575 ptr = (unsigned short *)gdata->winImage->image->data;
577 mask = 0x7b00 | 0x3d0 | 0x1e;
579 for (i = 0; i < gdata->winImage->image->bytes_per_line * gdata->height; i++, ptr++) {
581 *ptr &= mask;
584 return gdata;
587 static void mergeGhostWindow(_GhostWindowData * gdata)
589 register unsigned short *ptrw, *ptrb, *ptr;
590 int count;
591 int i;
593 ptr = (unsigned short *)gdata->image->image->data;
594 ptrw = (unsigned short *)gdata->winImage->image->data;
595 ptrb = (unsigned short *)gdata->backImage->image->data;
597 count = gdata->winImage->image->bytes_per_line * gdata->height;
599 while (count--) {
600 *ptr = (*ptrw + *ptrb) >> 1;
602 ptr++;
603 ptrw++;
604 ptrb++;
608 void UpdateGhostWindowMove(void *data, int x, int y)
610 _GhostWindowData *gdata = (_GhostWindowData *) data;
611 WScreen *scr = gdata->scr;
613 /* no intersection of new background with current */
614 if (x + gdata->width <= gdata->boxX
615 || x >= gdata->boxX + gdata->width
616 || y + gdata->height <= gdata->boxY || y >= gdata->boxY + gdata->height) {
617 int i;
619 RDestroyXImage(gdata->backImage);
621 gdata->backImage = RGetXImage(scr->rcontext, scr->root_win, x, y, gdata->width, gdata->height);
623 ptr = (unsigned short *)gdata->backImage->image->data;
625 mask = 0x7b00 | 0x3d0 | 0x1e;
627 for (i = 0; i < gdata->winImage->image->bytes_per_line * gdata->height; i++, ptr++) {
629 *ptr &= mask;
631 } else {
632 int hx, hw, hy, hh;
633 int vx, vw, vy, vh;
634 int i, j;
635 unsigned char *backP = gdata->backImage->image->data;
636 unsigned char *winP = gdata->winImage->image->data;
637 int backLineLen = gdata->backImage->image->bytes_per_line;
638 int winLineLen = gdata->winImage->image->bytes_per_line;
640 /* 1st move the area of the current backImage that overlaps
641 * the new backImage to it's new position */
643 if (x < gdata->boxX) {
644 vx = x + gdata->width;
645 vw = gdata->width - vx;
646 } else if (x > gdata->boxX) {
647 vw = gdata->boxX + gdata->width - x;
648 vx = gdata->boxX - vw;
649 } else {
650 vx = 0;
651 vw = gdata->width;
654 if (y < gdata->boxY) {
655 vy = y + gdata->height;
656 vh = gdata->height - vy;
657 } else if (y > gdata->boxY) {
658 vh = gdata->boxY + gdata->height - y;
659 vy = gdata->boxY - vh;
660 } else {
661 vy = 0;
662 vh = gdata->height;
665 if (y < gdata->boxY) {
666 int dy = vy - gdata->boxY;
668 if (x < gdata->boxX) {
669 for (i = vh - 1; i >= 0; i--) {
670 memmove(&backP[(i + dy) * backLineLen + 2 * vx],
671 &backP[i * backLineLen], 2 * vw);
673 } else { /* if (x > gdata->boxX) */
675 for (i = vh - 1; i >= 0; i--) {
676 memmove(&backP[(i + dy) * backLineLen],
677 &backP[i * backLineLen + 2 * vx], 2 * vw);
680 } else { /*if (y > gdata->boxY) */
682 int dy = gdata->boxY - vy;
684 if (x < gdata->boxX) {
685 for (i = 0; i < vh - 1; i++) {
686 memmove(&backP[i * backLineLen + 2 * vx],
687 &backP[(i + dy) * backLineLen], 2 * vw);
689 } else { /*if (x > gdata->boxX) */
691 for (i = 0; i < vh - 1; i++) {
692 memmove(&backP[i * backLineLen],
693 &backP[(i + dy) * backLineLen + 2 * vx], 2 * vw);
698 /* 2nd grab the background image from the screen and copy to the
699 * buffer. also maskout the lsb of rgb in each pixel of grabbed data */
701 if (y < gdata->boxY) {
702 vy = y;
703 vh = gdata->boxY - vy;
705 hy = y + vh;
706 hh = gdata->height - vh;
707 } else if (y > gdata->boxY) {
708 vy = gdata->boxY + gdata->height;
709 vh = vy - (y + gdata->height);
711 hy = y;
712 hh = y - gdata->boxY;
713 } else {
714 vy = vh = 0;
716 hy = y;
717 hh = gdata->height;
720 if (x < gdata->boxX) {
721 hx = x;
722 hw = gdata->boxX - hx;
723 } else if (x > gdata->boxX) {
724 hx = gdata->boxX + gdata->width;
725 hw = hx - (x + gdata->width);
726 } else {
727 hx = hw = 0;
729 vx = x;
730 vw = gdata->width;
733 /* 1st the top/bottom part */
735 /* 2nd the left/right part */
738 mergeGhostWindow(gdata);
741 #endif /* GHOST_WINDOW_MOVE */