1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
|
/* -*- Mode: C; tab-width: 4 -*- */
/* flag --- a waving flag of your operating system */
#if !defined( lint ) && !defined( SABER )
static const char sccsid[] = "@(#)flag.c 4.07 97/11/24 xlockmore";
#endif
/*-
* Copyright (c) 1996 by Charles Vidal <vidalc@club-internet.fr>
* http://www.chez.com/vidalc
*
* Thanks to Bas van Gaalen, Holland, PD, for his Pascal source
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation.
*
* This file is provided AS IS with no warranties of any kind. The author
* shall have no liability with respect to the infringement of copyrights,
* trade secrets or any patents by this file or any part thereof. In no
* event will the author be liable for any lost revenue or profits or
* other special, indirect and consequential damages.
*
* Revision History:
* 24-Oct-97: xpm and ras capability added.
* 13-May-97: jwz@netscape.com: turned into a standalone program.
* Made it able to animate arbitrary (runtime) text or bitmaps
* 15-May-96: written.
*/
#ifdef STANDALONE
#define PROGCLASS "Flag"
#define HACK_INIT init_flag
#define HACK_DRAW draw_flag
#define flag_opts xlockmore_opts
#define DEFAULTS "*delay: 50000 \n" \
"*cycles: 1000 \n" \
"*size: -7 \n" \
"*ncolors: 200 \n"
#define BRIGHT_COLORS
#define UNIFORM_COLORS
#define DEF_FONT "-*-helvetica-bold-r-*-240-*"
#define DEF_BITMAP ""
#define DEF_TEXT ""
#include "xlockmore.h" /* in xscreensaver distribution */
#else /* STANDALONE */
#include "xlock.h" /* in xlockmore distribution */
#endif /* STANDALONE */
#define DEF_INVERT "False"
static Bool invert;
static XrmOptionDescRec opts[] =
{
{"-invert", ".flag.invert", XrmoptionNoArg, (caddr_t) "on"},
{"+invert", ".flag.invert", XrmoptionNoArg, (caddr_t) "off"}
};
static argtype vars[] =
{
{(caddr_t *) & invert, "invert", "Invert", DEF_INVERT, t_Bool}
};
static OptionStruct desc[] =
{
{"-/+invert", "turn on/off inverting of flag"}
};
ModeSpecOpt flag_opts =
{sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
#ifdef USE_MODULES
ModStruct flag_description =
{"flag", "init_flag", "draw_flag", "release_flag",
"refresh_flag", "init_flag", NULL, &flag_opts,
50000, 1, 1000, -7, 64, 1.0, "",
"Shows a waving flag of your operating system", 0, NULL};
#endif
/* aliases for vars defined in the bitmap file */
#define FLAG_WIDTH image_width
#define FLAG_HEIGHT image_height
#define FLAG_BITS image_bits
#include "flag.xbm"
#if defined( USE_XPM ) || defined( USE_XPMINC )
#define FLAG_NAME image_name
#include "flag.xpm"
#define DEFAULT_XPM 0
#endif
#if !defined( VMS ) || ( __VMS_VER >= 70000000 )
#include <sys/utsname.h>
#else
#if USE_XVMSUTILS
#if 0
#include "../xvmsutils/utsname.h"
#else
#include <X11/utsname.h>
#endif
#endif /* USE_XVMSUTILS */
#endif
#define MINSIZE 1
#define MAXSCALE 8
#define MINSCALE 2
#define MAXINITSIZE 6
#define MININITSIZE 2
#define MINAMP 5
#define MAXAMP 20
#define MAXW(fp) (MAXSCALE * (fp)->image->width + 2 * MAXAMP + (fp)->pointsize + 2 * (fp)->sofs)
#define MAXH(fp) (MAXSCALE * (fp)->image->height+ 2 * MAXAMP + (fp)->pointsize + 2 * (fp)->sofs)
#define MINW(fp) (MINSCALE * (fp)->image->width + 2 * MINAMP + (fp)->pointsize)
#define MINH(fp) (MINSCALE * (fp)->image->height+ 2 * MINAMP + (fp)->pointsize)
#define ANGLES 360
#define IMAGE_FLAG 0
#define MESSAGE_FLAG 1
typedef struct {
int samp;
int sofs;
int sidx;
int x_flag, y_flag;
int timer;
int stab[ANGLES];
Pixmap cache;
int width, height;
int pointsize;
float size;
float inctaille;
int startcolor;
int choice;
XImage *image;
XImage *logo;
Colormap cmap;
unsigned long black;
int graphics_format;
GC backGC;
} flagstruct;
static XFontStruct *mode_font = None;
static flagstruct *flags = NULL;
extern XFontStruct *getFont(Display * display);
static int
random_num(int n)
{
return ((int) (((float) LRAND() / MAXRAND) * (n + 1.0)));
}
static void
initSintab(ModeInfo * mi)
{
flagstruct *fp = &flags[MI_SCREEN(mi)];
int i;
/*-
* change the periodicity of the sin formula : the maximum of the
* periocity seem to be 16 ( 2^4 ), after the drawing isn't good looking
*/
int periodicity = random_num(4);
int puissance = 1;
/* for (i=0;i<periodicity;i++) puissance*=2; */
puissance <<= periodicity;
for (i = 0; i < ANGLES; i++)
fp->stab[i] = (int) (SINF(i * puissance * M_PI / ANGLES) * fp->samp) +
fp->sofs;
}
static void
affiche(ModeInfo * mi)
{
Display *display = MI_DISPLAY(mi);
int x, y, xp, yp;
flagstruct *fp = &flags[MI_SCREEN(mi)];
for (x = 0; x < fp->image->width; x++)
for (y = fp->image->height - 1; y >= 0; y--) {
xp = (int) (fp->size * (float) x) +
fp->stab[(fp->sidx + x + y) % ANGLES];
yp = (int) (fp->size * (float) y) +
fp->stab[(fp->sidx + 4 * x + y + y) % ANGLES];
if (MI_NPIXELS(mi) <= 2 || fp->graphics_format < IS_XPM ||
fp->choice == MESSAGE_FLAG) {
if (((int) !invert) ^ XGetPixel(fp->image, x, y))
XSetForeground(display, fp->backGC, fp->black);
else if (MI_NPIXELS(mi) <= 2)
XSetForeground(display, fp->backGC, MI_WHITE_PIXEL(mi));
else
XSetForeground(display, fp->backGC,
MI_PIXEL(mi, (y + x + fp->sidx + fp->startcolor) %
MI_NPIXELS(mi)));
} else {
XSetForeground(display, fp->backGC, XGetPixel(fp->image, x, y));
}
if (fp->pointsize <= 1)
XDrawPoint(display, fp->cache, fp->backGC, xp, yp);
else
#ifndef NOTHREED_EFFECT
XFillRectangle(display, fp->cache, fp->backGC, xp, yp,
fp->pointsize, fp->pointsize);
#else
if (fp->pointsize < 6)
XFillRectangle(display, fp->cache, fp->backGC, xp, yp,
fp->pointsize, fp->pointsize);
else
XFillArc(display, fp->cache, fp->backGC, xp, yp,
fp->pointsize, fp->pointsize, 0, 360 * 64);
#endif
}
}
extern char *message, *imagefile;
static void
getText(ModeInfo * mi, XImage ** image)
{
Display *display = MI_DISPLAY(mi);
char *text1, *text2;
char *line, *token;
int width, height;
int lines;
int margin = 2;
XCharStruct overall;
XGCValues gcv;
GC gc;
Pixmap text_pixmap;
if (!message || !*message) {
#if !defined( VMS ) || ( __VMS_VER >= 70000000 ) || defined( USE_XVMSUTILS )
struct utsname uts;
if (uname(&uts) < 0) {
text1 = (char *) strdup("uname() failed");
} else {
char *s;
if ((s = strchr(uts.nodename, '.')))
*s = 0;
text1 = (char *) malloc(strlen(uts.nodename) +
strlen(uts.sysname) +
strlen(uts.release) + 10);
#ifdef AIXV3
(void) sprintf(text1, "%s\n%s %s",
uts.nodename, uts.sysname, "3");
#else
(void) sprintf(text1, "%s\n%s %s",
uts.nodename, uts.sysname, uts.release);
#endif
}
#else
text1 = (char *) strdup("OpenVMS"); /* It says release 0 in my utsname.h */
#endif
} else {
text1 = (char *) strdup(message);
}
while (*text1 && (text1[strlen(text1) - 1] == '\r' ||
text1[strlen(text1) - 1] == '\n'))
text1[strlen(text1) - 1] = 0;
text2 = (char *) strdup(text1);
if (mode_font == None)
mode_font = getFont(display);
(void) memset(&overall, 0, sizeof (overall));
token = text1;
lines = 0;
while ((line = strtok(token, "\r\n"))) {
XCharStruct o2;
int ascent, descent, direction;
token = 0;
(void) XTextExtents(mode_font, line, strlen(line),
&direction, &ascent, &descent, &o2);
overall.lbearing = MAX(overall.lbearing, o2.lbearing);
overall.rbearing = MAX(overall.rbearing, o2.rbearing);
lines++;
}
width = overall.lbearing + overall.rbearing + margin + margin + 1;
height = ((mode_font->ascent + mode_font->descent) * lines) +
margin + margin;
text_pixmap = XCreatePixmap(display, MI_WINDOW(mi), width, height, 1);
gcv.font = mode_font->fid;
gcv.foreground = 0;
gcv.background = 0;
gc = XCreateGC(display, text_pixmap,
GCFont | GCForeground | GCBackground, &gcv);
XFillRectangle(display, text_pixmap, gc, 0, 0, width, height);
XSetForeground(display, gc, 1);
token = text2;
lines = 0;
while ((line = strtok(token, "\r\n"))) {
XCharStruct o2;
int ascent, descent, direction, xoff;
token = 0;
(void) XTextExtents(mode_font, line, strlen(line),
&direction, &ascent, &descent, &o2);
xoff = ((overall.lbearing + overall.rbearing) -
(o2.lbearing + o2.rbearing)) / 2;
(void) XDrawString(display, text_pixmap, gc,
overall.lbearing + margin + xoff,
((mode_font->ascent * (lines + 1)) +
(mode_font->descent * lines) + margin),
line, strlen(line));
lines++;
}
(void) free((void *) text1);
(void) free((void *) text2);
/*XUnloadFont(display, mode_font->fid); */
XFreeGC(display, gc);
*image = XGetImage(display, text_pixmap, 0, 0, width, height,
1L, XYPixmap);
XFreePixmap(display, text_pixmap);
}
static void
init_stuff(ModeInfo * mi)
{
Display *display = MI_DISPLAY(mi);
Window window = MI_WINDOW(mi);
flagstruct *fp = &flags[MI_SCREEN(mi)];
if (!fp->logo)
getImage(mi, &fp->logo, FLAG_WIDTH, FLAG_HEIGHT, FLAG_BITS,
#if defined( USE_XPM ) || defined( USE_XPMINC )
DEFAULT_XPM, FLAG_NAME,
#endif
&fp->graphics_format, &fp->cmap, &fp->black);
if (fp->cmap != None) {
setColormap(display, window, fp->cmap, MI_WIN_IS_INWINDOW(mi));
if (fp->backGC == None) {
XGCValues xgcv;
xgcv.background = fp->black;
fp->backGC = XCreateGC(display, window, GCBackground, &xgcv);
}
} else {
fp->black = MI_BLACK_PIXEL(mi);
fp->backGC = MI_GC(mi);
}
}
static void
free_stuff(Display * display, flagstruct * fp)
{
if (fp->cmap != None) {
XFreeColormap(display, fp->cmap);
if (fp->backGC != None) {
XFreeGC(display, fp->backGC);
fp->backGC = None;
}
fp->cmap = None;
} else
fp->backGC = None;
destroyImage(&fp->logo, &fp->graphics_format);
}
void
init_flag(ModeInfo * mi)
{
Display *display = MI_DISPLAY(mi);
int size = MI_SIZE(mi);
flagstruct *fp;
if (flags == NULL) {
if ((flags = (flagstruct *) calloc(MI_NUM_SCREENS(mi),
sizeof (flagstruct))) == NULL)
return;
}
fp = &flags[MI_SCREEN(mi)];
init_stuff(mi);
fp->width = MI_WIN_WIDTH(mi);
fp->height = MI_WIN_HEIGHT(mi);
if (fp->image) {
if (fp->graphics_format == IS_XBM)
XFree(fp->image);
else
(void) XDestroyImage(fp->image);
fp->image = NULL;
}
if (MI_WIN_IS_FULLRANDOM(mi) ||
(imagefile && *imagefile && message && *message) ||
((!imagefile || !*imagefile) && (!message || !*message)))
fp->choice = LRAND() & 1;
else if (imagefile && *imagefile)
fp->choice = IMAGE_FLAG;
else
fp->choice = MESSAGE_FLAG;
if (fp->choice == MESSAGE_FLAG) {
getText(mi, &fp->image);
} else {
int depth = MI_DEPTH(mi);
if (depth >= 24 && depth < 32)
depth = 32;
if (fp->graphics_format < IS_XPM)
depth = 1;
fp->image = XCreateImage(display, MI_VISUAL(mi), depth,
(fp->graphics_format < IS_XPM) ? XYBitmap : ZPixmap,
0, fp->logo->data,
(fp->graphics_format == IS_RASTERFILE && (fp->logo->width & 1)) ?
fp->logo->width + 1 : fp->logo->width, fp->logo->height, 8, 0);
fp->image->byte_order = LSBFirst;
fp->image->bitmap_bit_order = LSBFirst;
}
fp->samp = MAXAMP; /* Amplitude */
fp->sofs = 20; /* ???????? */
fp->pointsize = size;
if (size < -MINSIZE)
fp->pointsize = NRAND(-size - MINSIZE + 1) + MINSIZE;
if (fp->pointsize < MINSIZE ||
fp->width <= MAXW(fp) || fp->height <= MAXH(fp))
fp->pointsize = MINSIZE;
fp->size = MAXINITSIZE; /* Initial distance between pts */
fp->inctaille = 0.05;
fp->timer = 0;
fp->sidx = fp->x_flag = fp->y_flag = 0;
if (fp->cache) {
XFreePixmap(display, fp->cache);
}
if (!(fp->cache = XCreatePixmap(display, MI_WINDOW(mi),
MAXW(fp), MAXH(fp), MI_DEPTH(mi)))) {
return;
}
XSetForeground(display, fp->backGC, fp->black);
XSetBackground(display, fp->backGC, fp->black);
XFillRectangle(display, fp->cache, fp->backGC,
0, 0, MAXW(fp), MAXH(fp));
/* don't want any exposure events from XCopyArea */
XSetGraphicsExposures(display, fp->backGC, False);
if (MI_NPIXELS(mi) > 2)
fp->startcolor = NRAND(MI_NPIXELS(mi));
if (fp->width <= MAXW(fp) || fp->height <= MAXH(fp)) {
fp->samp = MINAMP;
fp->sofs = 0;
fp->x_flag = random_num(fp->width - MINW(fp));
fp->y_flag = random_num(fp->height - MINH(fp));
} else {
fp->samp = MAXAMP;
fp->sofs = 20;
fp->x_flag = random_num(fp->width - MAXW(fp));
fp->y_flag = random_num(fp->height - MAXH(fp));
}
initSintab(mi);
MI_CLEARWINDOWCOLORMAP(mi, fp->backGC, fp->black);
}
void
draw_flag(ModeInfo * mi)
{
Display *display = MI_DISPLAY(mi);
Window window = MI_WINDOW(mi);
flagstruct *fp = &flags[MI_SCREEN(mi)];
if (fp->width <= MAXW(fp) || fp->height <= MAXH(fp)) {
fp->size = MININITSIZE;
/* fp->pointsize = MINPOINTSIZE; */
XCopyArea(display, fp->cache, window, fp->backGC,
0, 0, MINW(fp), MINH(fp), fp->x_flag, fp->y_flag);
} else {
if ((fp->size + fp->inctaille) > MAXSCALE)
fp->inctaille = -fp->inctaille;
if ((fp->size + fp->inctaille) < MINSCALE)
fp->inctaille = -fp->inctaille;
fp->size += fp->inctaille;
XCopyArea(display, fp->cache, window, fp->backGC,
0, 0, MAXW(fp), MAXH(fp), fp->x_flag, fp->y_flag);
}
XSetForeground(MI_DISPLAY(mi), fp->backGC, fp->black);
XFillRectangle(display, fp->cache, fp->backGC,
0, 0, MAXW(fp), MAXH(fp));
XFlush(display);
affiche(mi);
fp->sidx += 2;
fp->sidx %= (ANGLES * MI_NPIXELS(mi));
XFlush(display);
fp->timer++;
if ((MI_CYCLES(mi) > 0) && (fp->timer >= MI_CYCLES(mi)))
init_flag(mi);
}
void
release_flag(ModeInfo * mi)
{
if (flags != NULL) {
int screen;
for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
flagstruct *fp = &flags[screen];
Display *display = MI_DISPLAY(mi);
if (fp->cache)
XFreePixmap(display, fp->cache);
if (fp->image) {
if (fp->choice == IMAGE_FLAG && (fp->graphics_format == IS_XBM ||
fp->graphics_format == IS_XBMDONE))
XFree((caddr_t) fp->image); /* Do not destroy data */
else
(void) XDestroyImage(fp->image);
}
free_stuff(display, fp);
}
(void) free((void *) flags);
flags = NULL;
}
if (mode_font != None) {
XFreeFont(MI_DISPLAY(mi), mode_font);
mode_font = None;
}
}
void
refresh_flag(ModeInfo * mi)
{
/* Do nothing, it will refresh by itself */
}
|