File: deco.c

package info (click to toggle)
xlockmore 4.12-4
  • links: PTS
  • area: main
  • in suites: slink
  • size: 6,636 kB
  • ctags: 9,184
  • sloc: ansic: 71,502; makefile: 1,950; sh: 1,609; tcl: 1,403; cpp: 724; java: 277; perl: 179
file content (182 lines) | stat: -rw-r--r-- 5,124 bytes parent folder | download
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
/* -*- Mode: C; tab-width: 4 -*- */
/* deco --- art as ugly as sin */

#if !defined( lint ) && !defined( SABER )
static const char sccsid[] = "@(#)deco.c 4.07 97/11/24 xlockmore";

#endif
/* 
 * Copyright (c) 1997 by Jamie Zawinski <jwz@jwz.org>
 *
 * 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:
 * 29-Oct-97: xlock version (David Bagley <bagleyd@bigfoot.com>)
 * 1997: xscreensaver version Jamie Zawinski <jwz@jwz.org>
 */

/*-
 * original copyright
 * xscreensaver, Copyright (c) 1997 Jamie Zawinski <jwz@jwz.org>
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 *
 * Concept snarfed from Michael D. Bayne in
 * http://www.go2net.com/internet/deep/1997/04/16/body.html
 */

#ifdef STANDALONE
#define PROGCLASS "Deco"
#define HACK_INIT init_deco
#define HACK_DRAW draw_deco
#define deco_opts xlockmore_opts
#define DEFAULTS "*delay: 1000000 \n" \
 "*count: -30 \n" \
 "*cycles: 2 \n" \
 "*size: -10 \n" \
 "*ncolors: 200 \n"
#include "xlockmore.h"		/* in xscreensaver distribution */
#else /* STANDALONE */
#include "xlock.h"		/* in xlockmore distribution */
#endif /* STANDALONE */

ModeSpecOpt deco_opts =
{0, NULL, 0, NULL, NULL};

#ifdef USE_MODULES
ModStruct   deco_description =
{"deco", "init_deco", "draw_deco", "release_deco",
 "init_deco", "init_deco", NULL, &deco_opts,
 1000000, -30, 2, -10, 64, 0.6, "",
 "Shows art as ugly as sin", 0, NULL};

#endif

#define MINSIZE 2
#define MINDEPTH 1

typedef struct {
	int         max_depth;
	int         min_height;
	int         min_width;
	int         time;
	int         colorindex;
	unsigned long bordercolor;
} decostruct;

static decostruct *decos = NULL;

static void
deco(ModeInfo * mi, int x, int y, int w, int h, int depth)
{
	Display    *display = MI_DISPLAY(mi);
	Window      window = MI_WINDOW(mi);
	GC          gc = MI_GC(mi);
	decostruct *dp = &decos[MI_SCREEN(mi)];

	if ((NRAND(dp->max_depth) + 1 < depth) ||
	    (w < dp->min_width) || (h < dp->min_height)) {
		if (w > 2 && h > 2) {
			if (MI_NPIXELS(mi) > 2) {
				XSetForeground(display, gc, MI_PIXEL(mi, dp->colorindex));
				if (++dp->colorindex >= MI_NPIXELS(mi))
					dp->colorindex = 0;
			} else
				XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
			XFillRectangle(display, window, gc, x + 1, y + 1, w - 2, h - 2);
		}
	} else {
		if (LRAND() & 1) {
			deco(mi, x, y, w / 2, h, depth + 1);
			deco(mi, x + w / 2, y, w - w / 2, h, depth + 1);
		} else {
			deco(mi, x, y, w, h / 2, depth + 1);
			deco(mi, x, y + h / 2, w, h - h / 2, depth + 1);
		}
	}
}

void
init_deco(ModeInfo * mi)
{
	decostruct *dp;
	int         depth = MI_COUNT(mi);
	int         size = MI_SIZE(mi);

	if (decos == NULL) {
		if ((decos = (decostruct *) calloc(MI_NUM_SCREENS(mi),
					       sizeof (decostruct))) == NULL)
			return;
	}
	dp = &decos[MI_SCREEN(mi)];

	if (MI_NPIXELS(mi) > 2) {
		dp->bordercolor = MI_PIXEL(mi, NRAND(MI_NPIXELS(mi)));
		dp->colorindex = NRAND(MI_NPIXELS(mi));
	} else
		dp->bordercolor = MI_BLACK_PIXEL(mi);
	if (depth < -MINDEPTH)
		dp->max_depth = NRAND(-depth - MINDEPTH + 1) + MINDEPTH;
	else if (depth < MINDEPTH)
		dp->max_depth = MINDEPTH;
	else
		dp->max_depth = depth;
	if (size < -MINSIZE) {
		dp->min_width = NRAND(-size - MINSIZE + 1) + MINSIZE;
		dp->min_height = NRAND(-size - MINSIZE + 1) + MINSIZE;
	} else if (size < MINDEPTH)
		dp->min_width = dp->min_height = MINSIZE;
	else
		dp->min_width = dp->min_height = size;
	dp->time = 0;
}

void
draw_deco(ModeInfo * mi)
{
	decostruct *dp = &decos[MI_SCREEN(mi)];

	if (dp->time == 0) {
		/* This fills up holes */
		MI_CLEARWINDOWCOLOR(mi, dp->bordercolor);
#ifdef SOLARIS2
		/*
		 * if this is not done the first rectangle is sometimes messed up on
		 * Solaris2 with 24 bit TrueColor (Ultra2)
		 */
		XDrawRectangle(MI_DISPLAY(mi), MI_WINDOW(mi), MI_GC(mi),
			  0, 0, MI_WIDTH(mi) / 2 + 1, MI_HEIGHT(mi) / 2 + 1);
#endif
		deco(mi, 0, 0, MI_WIDTH(mi), MI_HEIGHT(mi), 0);
	}
	if (++dp->time > MI_CYCLES(mi))
		init_deco(mi);

	MI_IS_DRAWN(mi) = True;

}

void
release_deco(ModeInfo * mi)
{
	if (decos != NULL) {
		(void) free((void *) decos);
		decos = NULL;
	}
}