-
Notifications
You must be signed in to change notification settings - Fork 0
/
WPGenerator.c
580 lines (485 loc) · 17.5 KB
/
WPGenerator.c
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
#include <assert.h>
#include <cairo.h>
#include <fcntl.h>
#include <getopt.h>
#include <librsvg/rsvg.h>
#ifndef RSVG_CAIRO_H
#include <librsvg/rsvg-cairo.h>
#endif
#include <limits.h>
#include <math.h>
#include <regex.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define PROGRAM_NAME "WPGenerator"
#ifndef M_PI
#define M_PI 3.141592653589793
#endif
#define PHI 1.6180339887
#define LOGO_FILENAME "archlinux-logo-light-scalable.svg"
#define FILENAME_MAXLENGTH 1000
#define NUM_CIRCLES_DEFAULT 0
#define NUM_WAVES_DEFAULT 0
#define MAX_RANDOM_CIRCLES 5000
#define MAX_RANDOM_WAVES 5000
#define STANDARD_OUTFILENAME "wallpaper.png"
// http://stackoverflow.com/questions/195975/how-to-make-a-char-string-from-a-c-macros-value
#define xstr(s) blub(s)
#define blub(s) #s
enum LogoAlignment {LEFT_ALIGNED, CENTER_ALIGNED, RIGHT_ALIGNED};
struct ProgramArguments {
int screenWidth;
int screenHeight;
int numCircles;
int numWaves;
char * outFile;
bool quadsBackground;
bool dotPattern;
bool noLogo;
bool help;
bool stripedBackground;
enum LogoAlignment alignment;
};
struct ColorRGBA {
double red;
double green;
double blue;
double alpha;
};
static struct ColorRGBA colorDarkBlue = { 60.0/255.0, 76.0/255.0, 85.0/255.0, 1.0 };
static struct ColorRGBA colorArchBlue = { 23.0/255.0, 147.0/255.0, 209.0/255.0, 1.0 };
static struct ColorRGBA colorBgGrey = { 38.0/255.0, 39.0/255.0, 33.0/255.0, 0.9 };
unsigned int urandomRng(void) {
int urandomFD = open("/dev/urandom", O_RDONLY);
unsigned int randomInt;
ssize_t ret = read(urandomFD, &randomInt, sizeof(randomInt));
assert(ret == sizeof(randomInt));
close(urandomFD);
return randomInt;
}
char* strdup(const char *str) {
int n = strlen(str) + 1;
char *dup = malloc(n);
if(dup)
{
strcpy(dup, str);
}
return dup;
}
// xorshift, see http://en.wikipedia.org/wiki/Xorshift
double rng(void) {
static uint32_t x = 123456789;
static uint32_t y = 362436069;
static uint32_t z = 521288629;
static uint32_t w = 88675123;
uint32_t t = x ^ (x << 11);
x = y; y = z; z = w;
w ^= (w >> 19) ^ t ^ (t >> 8);
return (double) w / UINT_MAX;
}
int getSurfaceWidth(cairo_t* cr) {
assert(cr);
cairo_surface_t* surface = cairo_get_group_target(cr);
return cairo_image_surface_get_width(surface);
}
int getSurfaceHeight(cairo_t* cr) {
assert(cr);
cairo_surface_t* surface = cairo_get_group_target(cr);
return cairo_image_surface_get_height(surface);
}
void drawRandomCircles(cairo_t* cr) {
assert(cr);
int surfaceHeight = getSurfaceHeight(cr);
int surfaceWidth = getSurfaceWidth(cr);
cairo_set_line_width(cr, 2.0 * rng() + 3.0);
cairo_set_source_rgba(cr, colorDarkBlue.red, colorDarkBlue.green, colorDarkBlue.blue, 0.3 * rng());
cairo_arc(cr, rng() * surfaceWidth, rng() * surfaceHeight, rng() * 20, 0, 2.0 * M_PI);
cairo_fill_preserve(cr);
cairo_set_source_rgba(cr, 60.0/255.0, 76.0/255.0, 85.0/255.0, 0.3 * rng());
cairo_stroke(cr);
}
void drawRandomSineWaves(cairo_t* cr) {
assert(cr);
cairo_new_path(cr);
cairo_set_line_width(cr, 2.0);
int surfaceHeight = getSurfaceHeight(cr);
int surfaceWidth = getSurfaceWidth(cr);
double minPeriod = 5;
double period1 = (minPeriod + rng()) * surfaceWidth;
double period2 = 2 * period1;
double startX = 0.0;
double startY = surfaceHeight * 1.1111 * (rng() - 0.1);
double phase1 = rng() * surfaceWidth;
double phase2 = rng() * surfaceWidth;
double minAmplitude = 5.0;
double amplitude1 = minAmplitude + rng() * surfaceHeight / 5.0;
double amplitude2 = minAmplitude + rng() * surfaceHeight / 5.0;
cairo_move_to(cr, startX, startY + amplitude1 * sin(2 * M_PI * (phase1)/ period1));
for (double x = 1; x < surfaceWidth; ++x) {
cairo_line_to(cr, x, startY + amplitude1 * sin(2 * M_PI * ((double) x + phase1 )/ period1));
}
cairo_stroke(cr);
// second wave in around golden ratio of height to create a more dense
// look around the logo if someone sets a big number of sinewaves (eg. 10000)
startX = 0;
startY = 1.0 / PHI * surfaceHeight + (rng() - 0.5) * surfaceHeight / 4.0;
cairo_move_to(cr, startX, startY + amplitude2 * sin(2 * M_PI * ((double) surfaceWidth - 1 + phase2)/ period2));
cairo_new_path(cr);
for (double x = 0; x < surfaceWidth; ++x) {
cairo_line_to(cr, x, startY + amplitude2 * sin(2 * M_PI * ((double) x + phase2 )/ period2));
}
// arch blue
cairo_set_source_rgba(cr, colorArchBlue.red, colorArchBlue.green, colorArchBlue.blue, 0.1 * rng());
cairo_stroke(cr);
}
void drawArchLogo(cairo_t* cr, RsvgHandle* archLogoSVG, enum LogoAlignment alignment) {
assert(cr);
assert(archLogoSVG);
RsvgDimensionData* logoDimensions = (RsvgDimensionData*) malloc(sizeof(RsvgDimensionData));
rsvg_handle_get_dimensions(archLogoSVG, logoDimensions);
double rightBorderWidth = 10.0;
double surfaceHeight = getSurfaceHeight(cr);
double surfaceWidth = getSurfaceWidth(cr);
double logoWidth = logoDimensions->width;
double logoHeight = logoDimensions->height;
double scale = (0.17 * surfaceHeight) / logoHeight;
logoWidth *= scale;
logoHeight *= scale;
double rectY = 1.0 / PHI * surfaceHeight - logoHeight / 2.0;
double logoOriginX;
double logoOriginY = 1.0 / PHI * surfaceHeight - logoHeight / 2.0;
switch (alignment) {
case LEFT_ALIGNED:
logoOriginX = 0.0;
break;
case CENTER_ALIGNED:
logoOriginX = (surfaceWidth - logoWidth) / 2.0;
break;
case RIGHT_ALIGNED:
logoOriginX = surfaceWidth - logoWidth - rightBorderWidth;
break;
default: // should never come hear
exit(EXIT_FAILURE);
}
// grey
cairo_set_source_rgba(cr, colorBgGrey.red, colorBgGrey.green, colorBgGrey.blue, colorBgGrey.alpha);
cairo_rectangle(cr, 0, rectY, surfaceWidth, logoHeight);
cairo_fill(cr);
cairo_set_line_width(cr, 2.0);
cairo_set_source_rgba(cr, 23.0/255.0, 147.0/255.0, 209.0/255.0, 0.8);
cairo_move_to(cr, 0, rectY);
cairo_rel_line_to(cr, surfaceWidth, 0);
cairo_stroke(cr);
cairo_move_to(cr, 0, rectY + logoHeight);
cairo_rel_line_to(cr, surfaceWidth, 0);
cairo_stroke(cr);
cairo_surface_t* surface = cairo_get_group_target(cr);
cairo_surface_t* logoSurface = cairo_surface_create_for_rectangle(surface, logoOriginX, logoOriginY, logoWidth, logoHeight);
cairo_t* logoContext = cairo_create(logoSurface);
cairo_save(logoContext);
cairo_scale(logoContext, scale, scale);
rsvg_handle_render_cairo(archLogoSVG, logoContext);
cairo_restore(logoContext);
cairo_destroy(logoContext);
cairo_surface_destroy(logoSurface);
free(logoDimensions);
}
void usage(int status) {
printf("WPGenerator is a tool which can create simple, nice looking wallpapers through argument variation.\n\n");
printf("Usage:\n"
"./WPGenerator --width WIDTH --height HEIGHT [--circles NUM_CIRCLES --waves NUM_WAVES --random --dots --quads --outFile FILENAME --nologo --logopos ALIGNMENT]\n\n");
printf(
"Optionname\tOptional Argument\tExplanation\n"
"--circles\tNUM_CIRCLES\t\tControls the number of circles drawn\n"
"--dots\t\t\t\t\ttoggles wheter a dot raster is drawn\n"
"--height\tHEIGHT\t\t\tscreen resolution height [px]\n"
"--nologo\t\t\t\tommit Arch Linux logo\n"
"--help\t\t\t\t\tshow this help message\n"
"--logopos\tleft,center,right\tsets the alignment of the Arch Linux Logo\n"
"--outFile\tFILENAME\t\tpath of the generated wallpaper\n"
"--quads\t\t\t\t\tdraw quads in the background\n"
"--random\t\t\t\tdraw a random number of circles and waves\n"
"\t\t\t\t\t(max. %i, max. %i)\n"
"--stripes\t\t\t\tdraw a striped background\n"
"--waves\t\tNUM_WAVES\t\tcontrol the number of waves drawn\n"
"--width\t\tWIDTH\t\t\tscreen resolution width [px]\n",
MAX_RANDOM_CIRCLES, MAX_RANDOM_WAVES);
exit(status);
}
void checkNumericalArgument(char* arg) {
assert(arg);
regex_t regex;
regcomp(®ex, "^[[:digit:]]+$", REG_EXTENDED);
if(regexec(®ex, arg, 0, NULL, 0) == REG_NOMATCH) {
printf("No match %s", arg);
usage(EXIT_FAILURE);
}
regfree(®ex);
}
void getArguments(int argc, char ** argv, struct ProgramArguments* progArgs) {
assert(argv);
assert(progArgs);
bool widthOptionSet = false;
bool heightOptionSet = false;
bool circlesOptionSet = false;
bool wavesOptionSet = false;
bool randomOptionSet = false;
bool quadsOptionSet = false;
bool dotsOptionSet = false;
bool showLogoOptionSet = false;
bool helpOptionSet = false;
bool alignmentOptionSet = false;
bool stripedBackgroundOptionSet = false;
bool outFileOptionSet = false;
static struct option long_options[] = {
{ "logopos", required_argument, 0, 'a' },
{ "help", no_argument, 0, 'x' },
{ "nologo", no_argument, 0, 'l' },
{ "dots", no_argument, 0, 'd' },
{ "quads", no_argument, 0, 'q' },
{ "random", no_argument, 0, 'r' },
{ "width", required_argument, 0, 'w' },
{ "height", required_argument, 0, 'h' },
{ "circles", required_argument, 0, 'c' },
{ "waves", required_argument, 0, 's' },
{ "stripes", no_argument, 0, 't' },
{ "outFile", required_argument, 0, 'o' },
{ 0, 0, 0, 0 }
};
int shortOptionValue;
while (true) {
shortOptionValue = getopt_long(argc, argv, "axldqrwhcs", long_options, NULL);
/* Detect the end of the options. */
if (shortOptionValue == -1)
break;
switch (shortOptionValue) {
case 'a':
alignmentOptionSet = true;
if (strncmp(optarg, "left", strlen("left")) == 0)
progArgs->alignment = LEFT_ALIGNED;
else if (strncmp(optarg, "center", strlen("center")) == 0)
progArgs->alignment = CENTER_ALIGNED;
else if (strncmp(optarg, "right", strlen("right")) == 0)
progArgs->alignment = RIGHT_ALIGNED;
else
usage(EXIT_FAILURE);
break;
case 'x':
helpOptionSet = true;
progArgs->help = true;
break;
case 'l':
showLogoOptionSet = true;
progArgs->noLogo = true;
break;
case 'd':
dotsOptionSet = true;
progArgs->dotPattern = true;
break;
case 'q':
quadsOptionSet = true;
progArgs->quadsBackground = true;
break;
case 'r':
randomOptionSet = true;
break;
case 'w':
checkNumericalArgument(optarg);
widthOptionSet = true;
progArgs->screenWidth = atoi(optarg);
break;
case 'h':
checkNumericalArgument(optarg);
heightOptionSet = true;
progArgs->screenHeight = atoi(optarg);
break;
case 'c':
checkNumericalArgument(optarg);
circlesOptionSet = true;
progArgs->numCircles = atoi(optarg);
break;
case 'o':
outFileOptionSet = true;
progArgs->outFile = strdup(optarg);
break;
case 's':
checkNumericalArgument(optarg);
wavesOptionSet = true;
progArgs->numWaves = atoi(optarg);
break;
case 't':
stripedBackgroundOptionSet = true;
progArgs->stripedBackground = true;
break;
case '?':
usage(EXIT_FAILURE);
break;
default:
abort();
}
}
// set standard values
if (!alignmentOptionSet)
progArgs->alignment = CENTER_ALIGNED;
if (!helpOptionSet && !(widthOptionSet && heightOptionSet)) {
usage(EXIT_FAILURE);
}
if (!circlesOptionSet)
progArgs->numCircles = NUM_CIRCLES_DEFAULT;
if (!wavesOptionSet)
progArgs->numWaves = NUM_WAVES_DEFAULT;
if (randomOptionSet) {
const size_t BUFSIZE = 50;
char* tmpString = (char*) malloc(BUFSIZE * sizeof(char));
printf("Random Mode\n");
progArgs->numCircles = urandomRng() % MAX_RANDOM_CIRCLES;
sprintf(tmpString, "%i", progArgs->numCircles);
printf("Number circles: %s\n", tmpString);
progArgs->numWaves = urandomRng() % MAX_RANDOM_WAVES;
sprintf(tmpString, "%i", progArgs->numWaves);
printf("Number waves: %s\n", tmpString);
free(tmpString);
}
if(!helpOptionSet)
progArgs->help = false;
if (!dotsOptionSet)
progArgs->dotPattern = false;
if (!quadsOptionSet)
progArgs->quadsBackground = false;
if (!stripedBackgroundOptionSet)
progArgs->stripedBackground = false;
if (!showLogoOptionSet)
progArgs->noLogo = false;
if (!outFileOptionSet)
progArgs->outFile = strdup(STANDARD_OUTFILENAME);
}
void drawSimpleFilledBackground(cairo_t* cr) {
assert(cr);
int surfaceHeight = getSurfaceHeight(cr);
int surfaceWidth = getSurfaceWidth(cr);
cairo_set_source_rgba(cr, colorBgGrey.red, colorBgGrey.green, colorBgGrey.blue, 1.0);
cairo_rectangle(cr, 0, 0, surfaceWidth, surfaceHeight);
cairo_fill(cr);
}
void drawStripedBackground(cairo_t* cr) {
assert(cr);
int surfaceHeight = getSurfaceHeight(cr);
int surfaceWidth = getSurfaceWidth(cr);
int width = 0;
int stripeWidth = 0;
while (width < surfaceWidth) {
stripeWidth = 3;
double colorFactor = rng();
cairo_set_source_rgba(cr,
colorFactor * colorArchBlue.red,
colorFactor * colorArchBlue.green,
colorFactor * colorArchBlue.blue,
0.2);
cairo_rectangle(cr, width, 0, width + stripeWidth, surfaceHeight);
cairo_fill(cr);
width += stripeWidth;
}
}
void drawQuadsBackground(cairo_t* cr) {
assert(cr);
int surfaceHeight = getSurfaceHeight(cr);
int surfaceWidth = getSurfaceWidth(cr);
cairo_set_line_width(cr, 0.5);
int rectDim = surfaceWidth/200;
int x = 0.0;
int y = 0.0;
while (y < surfaceHeight) {
while (x < surfaceWidth) {
cairo_set_source_rgba(cr, 0.4, 0.4, 0.4, 0.6 * rng());
cairo_rectangle(cr, x, y, rectDim, rectDim);
cairo_fill(cr);
x += rectDim + 1;
}
x = 0.0;
y += rectDim + 1;
}
}
void drawDotPattern(cairo_t* cr) {
assert(cr);
int surfaceHeight = getSurfaceHeight(cr);
int surfaceWidth = getSurfaceWidth(cr);
cairo_set_source_rgba(cr, 1,1,1,0.2);
cairo_set_line_width(cr, 2.0);
// draw points
double dx = surfaceWidth / 30.0;
double x = dx;
double y = dx;
while (y < surfaceHeight) {
while (x < surfaceWidth) {
cairo_arc(cr, x, y, 2, 0, 2.0 * M_PI);
cairo_fill (cr);
x += dx;
}
x = dx;
y += dx;
}
}
int main(int argc, char ** argv) {
struct ProgramArguments * progArgs = (struct ProgramArguments *) malloc(sizeof(struct ProgramArguments));
getArguments(argc, argv, progArgs);
if (progArgs->help)
usage(EXIT_SUCCESS);
RsvgHandle* archLogoSVG = NULL;
// assemble logo path
const char* datarootdir = xstr(DATAROOTDIR);
size_t datarootdirLen = strlen(datarootdir);
size_t filenameLen = strlen(LOGO_FILENAME);
char* logoPath = (char*) calloc(datarootdirLen + filenameLen + 2, sizeof(char));
// printf("DATAROOTDIR %s\n", datarootdir);
strcat(logoPath, datarootdir);
strcat(logoPath, "/");
strcat(logoPath, LOGO_FILENAME);
printf("Reading Arch Linux logo from %s\n", logoPath);
GError* error = NULL;
archLogoSVG = rsvg_handle_new_from_file(logoPath, &error);
if (archLogoSVG == NULL) {
printf("error opening svg, exiting!\n");
exit(EXIT_FAILURE);
}
printf("Reading logo done.\n");
cairo_surface_t* surface = NULL;
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
progArgs->screenWidth,
progArgs->screenHeight);
cairo_t* cr = cairo_create(surface);
drawSimpleFilledBackground(cr);
if (progArgs->quadsBackground)
drawQuadsBackground(cr);
if (progArgs->stripedBackground)
drawStripedBackground(cr);
printf("Background filled.\n");
for (int circle = 0; circle < progArgs->numCircles; ++circle)
drawRandomCircles(cr);
printf("Circles drawn.\n");
for (int wave = 0 ; wave < progArgs->numWaves; ++wave)
drawRandomSineWaves(cr);
printf("Waves drawn.\n");
if (progArgs->dotPattern) {
drawDotPattern(cr);
printf("Dot Pattern drawn.\n");
}
if (!progArgs->noLogo) {
drawArchLogo(cr, archLogoSVG, progArgs->alignment);
printf("Logo drawn.\n");
}
rsvg_handle_close(archLogoSVG, NULL);
g_object_unref(archLogoSVG);
cairo_surface_write_to_png(surface, progArgs->outFile);
printf("Wallpaper written to file.\n");
cairo_destroy(cr);
cairo_surface_destroy(surface);
free(progArgs->outFile);
free(progArgs);
free(logoPath);
printf("Exiting.\n");
return EXIT_SUCCESS;
}