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
|
/*-
* swarm.c - swarm of bees for xlock, the X Window System lockscreen.
*
* Copyright (c) 1991 by Patrick J. Naughton.
*
* Revision History:
* 31-Aug-90: Adapted from xswarm by Jeff Butterworth. (butterwo@ncsc.org)
*/
/* Ported to tdescreensaver:
July 1997, Emanuel Pirker <epirker@edu.uni-klu.ac.at>
Contact me in case of problems, not the original author!
Last revised: 10-Jul-97
*/
// layout management added 1998/04/19 by Mario Weilguni <mweilguni@kde.org>
#define MAXSPEED 100
#define MINSPEED 0
#define DEFSPEED 50
#define MAXBATCH 200
#define MINBATCH 0
#define DEFBATCH 20
#include <tqslider.h>
#include <kglobal.h>
#include <kconfig.h>
#include <krandomsequence.h>
#include "xlock.h"
#undef index
#define TIMES 4 /* number of time positions recorded */
#define BEEACC 2 /* acceleration of bees */
#define WASPACC 5 /* maximum acceleration of wasp */
#define BEEVEL 12 /* maximum bee velocity */
#define WASPVEL 10 /* maximum wasp velocity */
/* Macros */
#define X(t,b) (sp->x[(t)*sp->beecount+(b)])
#define Y(t,b) (sp->y[(t)*sp->beecount+(b)])
#define balance_rand(v) (rnd.getLong(v)-((v)/2)) /* random number around 0 */
//ModeSpecOpt swarm_opts = {0, NULL, NULL, NULL};
typedef struct {
int pix;
int width;
int height;
int border; /* wasp won't go closer than this to the edge */
int beecount; /* number of bees */
XSegment segs[MAXBATCH]; /* bee lines */
XSegment old_segs[MAXBATCH]; /* old bee lines */
short x[MAXBATCH*TIMES];
short y[MAXBATCH*TIMES]; /* bee positions x[time][bee#] */
short xv[MAXBATCH];
short yv[MAXBATCH]; /* bee velocities xv[bee#] */
short wx[3];
short wy[3];
short wxv;
short wyv;
} swarmstruct;
static swarmstruct swarms[MAXSCREENS];
void
initswarm(Window win, KRandomSequence &rnd)
{
swarmstruct *sp = &swarms[screen];
int b;
XWindowAttributes xwa;
sp->beecount = batchcount;
(void) XGetWindowAttributes(dsp, win, &xwa);
sp->width = xwa.width;
sp->height = xwa.height;
sp->border = (sp->width + sp->height) / 50;
/* Clear the background. */
XSetForeground(dsp, Scr[screen].gc, BlackPixel(dsp, screen));
XFillRectangle(dsp, win, Scr[screen].gc, 0, 0, sp->width, sp->height);
/* Now static data structures. epirker */
//if (!sp->segs) {
//sp->segs = (XSegment *) malloc(sizeof (XSegment) * sp->beecount);
//sp->old_segs = (XSegment *) malloc(sizeof (XSegment) * sp->beecount);
//sp->x = (short *) malloc(sizeof (short) * sp->beecount * TIMES);
//sp->y = (short *) malloc(sizeof (short) * sp->beecount * TIMES);
//sp->xv = (short *) malloc(sizeof (short) * sp->beecount);
//sp->yv = (short *) malloc(sizeof (short) * sp->beecount);
//}
/* Initialize point positions, velocities, etc. */
/* wasp */
sp->wx[0] = sp->border + rnd.getLong(sp->width - 2 * sp->border);
sp->wy[0] = sp->border + rnd.getLong(sp->height - 2 * sp->border);
sp->wx[1] = sp->wx[0];
sp->wy[1] = sp->wy[0];
sp->wxv = 0;
sp->wyv = 0;
/* bees */
for (b = 0; b < sp->beecount; b++) {
X(0, b) = rnd.getLong(sp->width);
X(1, b) = X(0, b);
Y(0, b) = rnd.getLong(sp->height);
Y(1, b) = Y(0, b);
sp->xv[b] = balance_rand(7);
sp->yv[b] = balance_rand(7);
}
}
void
drawswarm(Window win, KRandomSequence &rnd)
{
swarmstruct *sp = &swarms[screen];
int b;
/* <=- Wasp -=> */
/* Age the arrays. */
sp->wx[2] = sp->wx[1];
sp->wx[1] = sp->wx[0];
sp->wy[2] = sp->wy[1];
sp->wy[1] = sp->wy[0];
/* Accelerate */
sp->wxv += balance_rand(WASPACC);
sp->wyv += balance_rand(WASPACC);
/* Speed Limit Checks */
if (sp->wxv > WASPVEL)
sp->wxv = WASPVEL;
if (sp->wxv < -WASPVEL)
sp->wxv = -WASPVEL;
if (sp->wyv > WASPVEL)
sp->wyv = WASPVEL;
if (sp->wyv < -WASPVEL)
sp->wyv = -WASPVEL;
/* Move */
sp->wx[0] = sp->wx[1] + sp->wxv;
sp->wy[0] = sp->wy[1] + sp->wyv;
/* Bounce Checks */
if ((sp->wx[0] < sp->border) || (sp->wx[0] > sp->width - sp->border - 1)) {
sp->wxv = -sp->wxv;
sp->wx[0] += sp->wxv;
}
if ((sp->wy[0] < sp->border) || (sp->wy[0] > sp->height - sp->border - 1)) {
sp->wyv = -sp->wyv;
sp->wy[0] += sp->wyv;
}
/* Don't let things settle down. */
sp->xv[rnd.getLong(sp->beecount)] += balance_rand(3);
sp->yv[rnd.getLong(sp->beecount)] += balance_rand(3);
/* <=- Bees -=> */
for (b = 0; b < sp->beecount; b++) {
int distance, dx, dy;
/* Age the arrays. */
X(2, b) = X(1, b);
X(1, b) = X(0, b);
Y(2, b) = Y(1, b);
Y(1, b) = Y(0, b);
/* Accelerate */
dx = sp->wx[1] - X(1, b);
dy = sp->wy[1] - Y(1, b);
distance = abs(dx) + abs(dy); /* approximation */
if (distance == 0)
distance = 1;
sp->xv[b] += (dx * BEEACC) / distance;
sp->yv[b] += (dy * BEEACC) / distance;
/* Speed Limit Checks */
if (sp->xv[b] > BEEVEL)
sp->xv[b] = BEEVEL;
if (sp->xv[b] < -BEEVEL)
sp->xv[b] = -BEEVEL;
if (sp->yv[b] > BEEVEL)
sp->yv[b] = BEEVEL;
if (sp->yv[b] < -BEEVEL)
sp->yv[b] = -BEEVEL;
/* Move */
X(0, b) = X(1, b) + sp->xv[b];
Y(0, b) = Y(1, b) + sp->yv[b];
/* Fill the segment lists. */
sp->segs[b].x1 = X(0, b);
sp->segs[b].y1 = Y(0, b);
sp->segs[b].x2 = X(1, b);
sp->segs[b].y2 = Y(1, b);
sp->old_segs[b].x1 = X(1, b);
sp->old_segs[b].y1 = Y(1, b);
sp->old_segs[b].x2 = X(2, b);
sp->old_segs[b].y2 = Y(2, b);
}
XSetForeground(dsp, Scr[screen].gc, BlackPixel(dsp, screen));
XDrawLine(dsp, win, Scr[screen].gc,
sp->wx[1], sp->wy[1], sp->wx[2], sp->wy[2]);
XDrawSegments(dsp, win, Scr[screen].gc, sp->old_segs, sp->beecount);
XSetForeground(dsp, Scr[screen].gc, WhitePixel(dsp, screen));
XDrawLine(dsp, win, Scr[screen].gc,
sp->wx[0], sp->wy[0], sp->wx[1], sp->wy[1]);
if (!mono && Scr[screen].npixels > 2) {
XSetForeground(dsp, Scr[screen].gc, Scr[screen].pixels[sp->pix]);
if (++sp->pix >= Scr[screen].npixels)
sp->pix = 0;
}
XDrawSegments(dsp, win, Scr[screen].gc, sp->segs, sp->beecount);
}
//-----------------------------------------------------------------------------
#include <tqcheckbox.h>
#include <tqlabel.h>
#include <tqcolor.h>
#include <tqlayout.h>
#include <klocale.h>
#include <kmessagebox.h>
#include "swarm.h"
#include "swarm.moc"
#include "helpers.h"
#undef Below
static kSwarmSaver *saver = NULL;
void startScreenSaver( Drawable d )
{
if ( saver )
return;
saver = new kSwarmSaver( d );
}
void stopScreenSaver()
{
if ( saver )
delete saver;
saver = NULL;
}
int setupScreenSaver()
{
kSwarmSetup dlg;
return dlg.exec();
}
//-----------------------------------------------------------------------------
kSwarmSaver::kSwarmSaver( Drawable drawable ) : kScreenSaver( drawable )
{
readSettings();
// Clear to background colour when exposed
XSetWindowBackground(tqt_xdisplay(), mDrawable,
BlackPixel(tqt_xdisplay(), tqt_xscreen()));
colorContext = TQColor::enterAllocContext();
batchcount = maxLevels;
initXLock( mGc );
initswarm( mDrawable, rnd );
timer.start( speed );
connect( &timer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotTimeout() ) );
}
kSwarmSaver::~kSwarmSaver()
{
timer.stop();
TQColor::leaveAllocContext();
TQColor::destroyAllocContext( colorContext );
}
void kSwarmSaver::setSpeed( int spd )
{
timer.stop();
speed = MAXSPEED - spd;
timer.start( speed );
}
void kSwarmSaver::setLevels( int l )
{
batchcount = maxLevels = l;
initswarm( mDrawable, rnd );
}
void kSwarmSaver::readSettings()
{
TDEConfig *config = klock_config();
config->setGroup( "Settings" );
speed = MAXSPEED - config->readNumEntry( "Speed", MAXSPEED - DEFSPEED );
maxLevels = config->readNumEntry( "MaxLevels", DEFBATCH );
delete config;
}
void kSwarmSaver::slotTimeout()
{
drawswarm( mDrawable, rnd );
}
//-----------------------------------------------------------------------------
kSwarmSetup::kSwarmSetup( TQWidget *parent, const char *name )
: KDialogBase( parent, name, true, i18n( "Setup Swarm Screen Saver" ),
Ok|Cancel|Help, Ok, true )
{
readSettings();
setButtonText( Help, i18n( "A&bout" ) );
TQWidget *main = makeMainWidget();
TQHBoxLayout *top = new TQHBoxLayout( main, 0, spacingHint() );
TQVBoxLayout *left = new TQVBoxLayout(top, spacingHint());
TQLabel *label = new TQLabel( i18n("Speed:"), main );
min_size(label);
left->addWidget(label);
TQSlider *slider = new TQSlider(MINSPEED, MAXSPEED, 10, speed,
Qt::Horizontal, main );
slider->setMinimumSize( 120, 20 );
slider->setTickmarks(TQSlider::Below);
slider->setTickInterval(10);
connect( slider, TQT_SIGNAL( valueChanged( int ) ),
TQT_SLOT( slotSpeed( int ) ) );
left->addWidget(slider);
label = new TQLabel( i18n("Number of bees:"), main );
min_size(label);
left->addWidget(label);
slider = new TQSlider(MINBATCH, MAXBATCH, 20, maxLevels,Qt::Horizontal, main );
slider->setMinimumSize( 120, 20 );
slider->setTickmarks(TQSlider::Below);
slider->setTickInterval(20);
connect( slider, TQT_SIGNAL( valueChanged( int ) ),
TQT_SLOT( slotLevels( int ) ) );
left->addWidget(slider);
left->addStretch();
preview = new TQWidget( main );
preview->setFixedSize( 220, 170 );
preview->setBackgroundColor( black );
preview->show(); // otherwise saver does not get correct size
saver = new kSwarmSaver( preview->winId() );
top->addWidget(preview);
top->addStretch();
}
void kSwarmSetup::readSettings()
{
TDEConfig *config = klock_config();
config->setGroup( "Settings" );
speed = config->readNumEntry( "Speed", speed );
if ( speed > MAXSPEED )
speed = MAXSPEED;
else if ( speed < MINSPEED )
speed = MINSPEED;
maxLevels = config->readNumEntry( "MaxLevels", DEFBATCH );
delete config;
}
void kSwarmSetup::slotSpeed( int num )
{
speed = num;
if ( saver )
saver->setSpeed( speed );
}
void kSwarmSetup::slotLevels( int num )
{
maxLevels = num;
if ( saver )
saver->setLevels( maxLevels );
}
void kSwarmSetup::slotOk()
{
TDEConfig *config = klock_config();
config->setGroup( "Settings" );
TQString sspeed;
sspeed.setNum( speed );
config->writeEntry( "Speed", sspeed );
TQString slevels;
slevels.setNum( maxLevels );
config->writeEntry( "MaxLevels", slevels );
config->sync();
delete config;
accept();
}
void kSwarmSetup::slotHelp()
{
KMessageBox::information(this,
i18n("Swarm\n\nCopyright (c) 1991 by Patrick J. Naughton\n\nPorted to tdescreensaver by Emanuel Pirker."),
i18n("About Swarm"));
}
|