File: meltdown.c.cln

package info (click to toggle)
xflip 1.01-28
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 184 kB
  • sloc: ansic: 895; makefile: 11
file content (247 lines) | stat: -rw-r--r-- 5,949 bytes parent folder | download | duplicates (8)
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
/*
 * meltdown -- mess up the screen
 */
/*
 * Copyright 1990 David Lemke and Network Computing Devices
 *
 * 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, and that the name of Network Computing Devices not be 
 * used in advertising or publicity pertaining to distribution of the 
 * software without specific, written prior permission.  Network Computing 
 * Devices makes no representations about the suitability of this software 
 * for any purpose.  It is provided "as is" without express or implied 
 * warranty.
 *
 * NETWORK COMPUTING DEVICES DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS 
 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS,
 * IN NO EVENT SHALL NETWORK COMPUTING DEVICES BE LIABLE FOR ANY SPECIAL,
 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 
 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 
 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE 
 * OR PERFORMANCE OF THIS SOFTWARE.
 *
 * Author:  	
 *		Dave Lemke
 *		lemke@ncd.com
 *
 *		Network Computing Devices, Inc
 *		350 North Bernardo Ave
 *		Mountain View, CA 94043
 *
 *		@(#)meltdown.c	1.2	90/02/22
 *
 */

#include	<X11/Xlib.h>
#include	<stdio.h>

#define	MIN_SIZE	10
#define	MAX_SIZE	100

#define	MIN_DIST	10

#define	MIN_WIDTH	30
#define	WIDTH_ADD	20

#define	FINISHED	50

#define	rnd(x)	(random() % (x))

#define	min(a, b)	((a) < (b) ? (a) : (b))
#define	max(a, b)	((a) > (b) ? (a) : (b))

Display	*dpy;
Window	win;
GC	copygc, fillgc;
int	screen;
int	depth;

short	**heights;

usage()
{
	fprintf(stderr, "Usage:  meltdown [-planes] [-display <displayname>]\n");
	exit(1);
}

main(argc, argv)
int	argc;
char	**argv;
{
char	*display = NULL;
unsigned long	vmask;
XSetWindowAttributes	xswat;
XGCValues	gcvals;
Bool	use_planes = False;
int	i;

	srandom(getpid());

	for (i = 1; i < argc; i++)	{
		if (strncmp(argv[i], "-dis", 4) == 0)	{
			if (argv[i+1])
				display = argv[++i];
			else
				usage();
		} else if (strncmp(argv[i], "-p", 2) == 0)	{
			use_planes = 1;
		}	else
			usage();
	}

	if ((dpy = XOpenDisplay(display)) == NULL)	{
		fprintf(stderr, "can't open display\n");
		exit(0);
	}

	screen = DefaultScreen(dpy);

	xswat.override_redirect = True;
	xswat.do_not_propagate_mask = KeyPressMask | KeyReleaseMask |
		ButtonPressMask | ButtonReleaseMask;
	vmask = CWOverrideRedirect | CWDontPropagate;
	win = XCreateWindow(dpy, RootWindow(dpy, screen), 0, 0, 
		DisplayWidth(dpy, screen), DisplayHeight(dpy, screen),
		0, CopyFromParent, CopyFromParent, CopyFromParent, vmask,
		&xswat);
	XMapWindow(dpy, win);

	gcvals.graphics_exposures = False;
	/* copyplane gc wants to leave the data alone */
	gcvals.foreground = 1;
	gcvals.background = 0;
	copygc = XCreateGC(dpy, win, 
		GCForeground | GCBackground | GCGraphicsExposures, &gcvals);

	gcvals.foreground = BlackPixel(dpy, screen);
	fillgc = XCreateGC(dpy, win, GCForeground, &gcvals);


	XSync(dpy, 0);
	sleep(1);

	if (use_planes)	{
		do_planes();
	} else	{
		do_all();
	}

	/* NOTREACHED */
	exit(0);
}

do_planes()
{
int	finished = 0;
int	width, xloc, yloc, dist, size, i;
short	**heights;

	heights = (short **) malloc(DisplayPlanes(dpy, screen));
	for (i = 0; i < DisplayPlanes(dpy, screen); i++)
		heights[i] = (short *) calloc(sizeof(short),
			DisplayWidth(dpy, screen));
	while (1)	{
		depth = rnd(DisplayPlanes(dpy, screen));
		width = rnd(MIN_WIDTH) + WIDTH_ADD;

		xloc = calc_xloc(width);

		yloc = DisplayHeight(dpy, screen);
		for (i = xloc; i < (xloc + width); i++)	{
			yloc = min(yloc, heights[depth][i]);
		}
		if (yloc == DisplayHeight(dpy, screen))
			continue;

		dist = rnd(yloc/10 + MIN_DIST);
		size = rnd(max(yloc + MIN_SIZE, MAX_SIZE));

		XSetPlaneMask(dpy, copygc, 1 << depth);
		XSetPlaneMask(dpy, fillgc, 1 << depth);
		XCopyArea(dpy, win, win, copygc, 
			xloc, yloc, 
			width, size,
			xloc, yloc + dist);
		XFillRectangle(dpy, win, fillgc, 
			xloc, yloc, 
			width, dist);
		yloc += dist;
		for (i = xloc; i < (xloc + width); i++)	{
		    if ((heights[depth][i] < (DisplayHeight(dpy, screen) - MIN_SIZE))
		        && (yloc >= (DisplayHeight(dpy, screen) - MIN_SIZE)))
				finished++;

		    heights[depth][i] = max(heights[depth][i], yloc);
		}
		if (finished >= (DisplayWidth(dpy, screen) - FINISHED)) {
			XSync(dpy, 0);
			exit(0);
		}
	}
}

do_all()
{
int	finished = 0;
int	width, xloc, yloc, dist, size, i;
short	*heights;

	heights = (short *) calloc(sizeof(short), DisplayWidth(dpy, screen));

	while (1)	{
		depth = rnd(DisplayPlanes(dpy, screen));
		width = rnd(MIN_WIDTH) + WIDTH_ADD;

		xloc = calc_xloc(width);

		yloc = DisplayHeight(dpy, screen);
		for (i = xloc; i < (xloc + width); i++)	{
			yloc = min(yloc, heights[i]);
		}
		if (yloc == DisplayHeight(dpy, screen))
			continue;

		dist = rnd(yloc/10 + MIN_DIST);
		size = rnd(max(yloc + MIN_SIZE, MAX_SIZE));

		XCopyArea(dpy, win, win, copygc, 
			xloc, yloc, 
			width, size,
			xloc, yloc + dist);
		XFillRectangle(dpy, win, fillgc, 
			xloc, yloc, 
			width, dist);
		yloc += dist;
		for (i = xloc; i < (xloc + width); i++)	{
		    if ((heights[i] < (DisplayHeight(dpy, screen) - MIN_SIZE))
		        && (yloc >= (DisplayHeight(dpy, screen) - MIN_SIZE)))
				finished++;

		    heights[i] = max(heights[i], yloc);
		}
		if (finished >= (DisplayWidth(dpy, screen) - FINISHED))	{
			XSync(dpy, 0);
			exit(0);
		}
	}
}

int
calc_xloc(width)
int	width;
{
int	xloc;

	/* give values near edges a better chance */
	xloc = rnd(DisplayWidth(dpy, screen) + MIN_WIDTH) - MIN_WIDTH;

	if ((xloc + width) > DisplayWidth(dpy, screen))
		xloc = DisplayWidth(dpy, screen) - width;
	else if (xloc < 0)
		xloc = 0;

	return xloc;
}