-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbubblemon.c
1616 lines (1405 loc) · 52.3 KB
/
bubblemon.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* WMBubble dockapp 1.54
*
* Todo: merge in wmfishtime/bubblefishymon, reduce number of
* compilation-time settings, make more things configurable via xresources.
*
* - dockapp for Window Maker/Blackbox/E/Afterstep/SawBabble
* - Code from Robert Jacobs <[email protected]>, 2010-2011
* - Code from the debian maintainers, 2005-2009
* - Code outside of bubblemon_update copyright 2000, 2001
* - oleg dashevskii <[email protected]> made changes to collect memory
* and cpu information on FreeBSD. Some major performance improvements
* and other cool hacks. Useful ideas - memscreen, load screen, etc.
* - Adrian B <[email protected]> came up with the idea of load
* meter.
* - [email protected] sent in cute duck gfx and suggestions, plus some
* code and duck motion fixes.
* - Phil Lu <[email protected]> Dan Price <[email protected]> - Solaris/SunOS
* port
* - Everything else copyright one of the guys below
* Bubbling Load Monitoring Applet
* - A GNOME panel applet that displays the CPU + memory load as a
* bubbling liquid.
* Copyright (C) 1999-2000 Johan Walles
* Copyright (C) 1999 Merlin Hughes
* - http://nitric.com/freeware/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
*
*/
#define _GNU_SOURCE
#define VERSION "1.54"
/* general includes */
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <ctype.h> /* I know tolower isn't i18n, I'm sorry */
#include <math.h>
#include <inttypes.h>
/* x11 includes */
#include "wmx11pixmap.h"
#include <X11/Xresource.h>
#include "include/bubblemon.h"
#include "include/sys_include.h"
#include "include/numbers-2.h"
#include "include/ducks.h"
#include "include/digits.h"
#include "misc/numbers.xpm"
#include "misc/ofmspct.xpm"
#include "misc/datefont.xpm"
/* #define DEBUG_DUCK 1 */
#define NAME "wmbubble"
/* Want a better way to work with these. But we use them in two places now, so... */
#define GET_RED(x) (((x)>>16)&255)
#define GET_GRN(x) (((x)>> 8)&255)
#define GET_BLU(x) (((x) )&255)
enum bubblebuf_values { watercolor, antialiascolor, aircolor };
/* local prototypes *INDENT-OFF* */
void bubblemon_allocate_buffers(void);
void do_water_sim(int cpu);
void draw_watertank(void);
void bubblebuf_colorspace(void);
void build_graphs(void);
void make_new_bubblemon_dockapp(void);
void get_memory_load_percentage(void);
void bubblemon_session_defaults(XrmDatabase x_resource_database);
int get_screen_selection(void);
/* draw functions for load average / memory screens */
void draw_from_xpm(char **xpm, unsigned char *whither, unsigned int targetw,
unsigned int xpmx, unsigned int xpmy, unsigned int xpmw,
unsigned int xpmh, unsigned int color);
void draw_history(int num, int size, unsigned int *history,
unsigned char *buf);
void draw_digit(unsigned char * from, unsigned char * whither);
void draw_string(char *string, int x, int y, int color);
void draw_cpudigit(int what, unsigned char *whither);
void draw_cpugauge(int cpu);
void draw_rgba_pixel(unsigned char * whither, int color, float opacity);
void draw_aa_line(float x1,float y1, float x2,float y2, int color);
void draw_clockhands(void);
void render_secondary(void);
void calculate_transparencies(int proximity);
void alpha_cpu(void);
void alpha_graph(void);
void alpha_digitalclock(struct tm * mytime);
void alpha_date(struct tm * mytime);
void roll_history(void);
void draw_dtchr(const char letter, unsigned char *where);
int animate_correctly(void);
void draw_duck(int x, int y, int frame_no, int flipx, int flipy);
void duck_swimmer(void);
/* local prototypes end *INDENT-ON* */
extern char * optarg;
/* global variables */
BubbleMonData bm;
int duck_enabled = 1;
int upside_down_duck_enabled = 1;
int cpu_enabled = 1;
int memscreen_enabled = 1;
int memscreen_megabytes = 0;
int graph_digit_color = 0x308cf0;
int graph_warning_digit_color = 0xed1717;
int pale = 0;
int do_analog_clock = 0;
int hourcolor = 0xEEEEEE;
int mincolor = 0xBF0000;
int seccolor = 0xC79F2B;
int shifttime = 0;
int do_digital_clock = 0;
int do_date = 0;
int do_help = 0;
int delay_time = 15000;
int gauge_alpha = CPUMAXBLEND;
int graph_alpha = GRAPHMAXBLEND;
/* duck_colors[0] is always transparent */
int duck_colors[4] = {0,0xF8FC00,0xF8B040,0};
/* 1, 5, 15 on load average graph; m, s on memory utilization graph */
int graph_labels = 0xC1C400;
int graph_field = 0x202020;
int graph_grid = 0x062A00;
int graph_max = 0x20B6AE;
int graph_bar = 0x007D71;
int graph_hundreds = 0x71E371;
unsigned char * empty_loadgraph, * empty_memgraph;
unsigned char * graph_numbers_n_rgb, * graph_numbers_b_rgb;
unsigned char cpu_gauge[25*9*3];
int datefont_widths[256];
char datefont_transparent;
unsigned int datefont_offset;
int duck_blink = 0;
int blinkdelay = 1;
const struct XrmUnified {
char * const option;
char * const specifier;
const char * const valueifnoarg;
const enum { Is_Int, Is_Color, Is_Float, Is_Bool } parse_as;
void * write_to;
const char * const description;
} x_resource_unified[] = {
{"-maxbubbles", "*maxbubbles", NULL, Is_Int, &bm.maxbubbles, "Maximum number of simultaneous bubbles in the dockapp" },
{"-air_noswap", "*air_noswap", NULL, Is_Color, &bm.air_noswap, "Color of air and bubbles when swap is at 0%" },
{"-air_maxswap", "*air_maxswap", NULL, Is_Color, &bm.air_maxswap, "Color of air and bubbles when swap is at 100%" },
{"-liquid_noswap", "*liquid_noswap", NULL, Is_Color, &bm.liquid_noswap, "Color of water when swap is at 0%" },
{"-liquid_maxswap","*liquid_maxswap", NULL, Is_Color, &bm.liquid_maxswap, "Color of water when swap is at 100%" },
{"-duckbody", "*duckbody", NULL, Is_Color, &duck_colors[1], "Color of duck's body" },
{"-duckbill", "*duckbill", NULL, Is_Color, &duck_colors[2], "Color of duck's bill" },
{"-duckeye", "*duckeye", NULL, Is_Color, &duck_colors[3], "Color of duck's eye" },
{"-delay", "*delay", NULL, Is_Int, &delay_time, "delay this number of microseconds between redraws" },
{"-ripples", "*ripples", NULL, Is_Float, &bm.ripples, "Pixels to disturb the surface when a bubble is formed/pops" },
{"-gravity", "*gravity", NULL, Is_Float, &bm.gravity, "Pixels/refresh/refresh to accelerate bubbles upwards" },
{"-volatility", "*volatility", NULL, Is_Float, &bm.volatility, "Restorative force on water surface in proportion/refresh"},
{"-viscosity", "*viscosity", NULL, Is_Float, &bm.viscosity, "Attenuation of surface velocity in proportion/refresh"},
{"-speed_limit", "*speed_limit", NULL, Is_Float, &bm.speed_limit, "Maximum water surface velocity in pixels/refresh" },
{"-help", ".help", "1" , Is_Bool, &do_help, "Displays this help" },
{"-duck", "*duck", NULL, Is_Bool, &duck_enabled, "Draw the duck?"},
{"-d", "*duck", "no", Is_Bool, &duck_enabled, "Just don't draw the duck" },
{"-upsidedown", "*upsidedown", NULL, Is_Bool, &upside_down_duck_enabled, "Can the duck flip when the tank is overfull?" },
{"-u", "*upsidedown", "no", Is_Bool, &upside_down_duck_enabled, "The duck can never flip" },
{"-cpumeter", "*cpumeter", NULL, Is_Bool, &cpu_enabled, "Show the current load at the bottom"},
{"-c", "*cpumeter", "no", Is_Bool, &cpu_enabled, "Don't show the current load"},
{"-graphdigit", "*graphdigit", NULL, Is_Color, &graph_digit_color, "Color of the digits on the graphs"},
{"-graphwarn", "*graphwarn", NULL, Is_Color, &graph_warning_digit_color, "Color of the digits on the memory graph when above 90%" },
{"-graphlabel", "*graphlabel", NULL, Is_Color, &graph_labels, "Color of the 1 5 and 15 on load graph and m and s on mem graph" },
{"-graphfield", "*graphfield", NULL, Is_Color, &graph_field, "Color of the background of the graphs" },
{"-graphgrid", "*graphgrid", NULL, Is_Color, &graph_grid, "Color of the grid lines in the graphs" },
{"-graphmax", "*graphmax", NULL, Is_Color, &graph_max, "Color of the top two pixels of the bar graph" },
{"-graphbar", "*graphbar", NULL, Is_Color, &graph_bar, "Color of the rest of the bar graph" },
{"-graphmarkers", "*graphmarkers", NULL, Is_Color, &graph_hundreds, "Color of the horizontal lines on the graph that indicate each integer load average" },
{"-p", ".graphdigitpale", "1" , Is_Bool, &pale, "Adjust the digit colors to pale blue and cyan"},
{"-graphs", "*graphs", NULL, Is_Bool, &memscreen_enabled, "Does hovering show the graphs" },
{"-m", "*graphs", "no", Is_Bool, &memscreen_enabled, "Graphs are never shown"},
{"-units", "*units", NULL, Is_Bool, &memscreen_megabytes, "Units for memory in KB or MB"},
{"-k", "*units", "m" , Is_Bool, &memscreen_megabytes, "Memory graphs use MB" },
{"-shifttime", "*shifttime", NULL, Is_Int, &shifttime, "Number of hours after midnight that are drawn as part of the previous day on digital clock and date" },
{"-digital", "*digital", NULL, Is_Bool, &do_digital_clock, "Draw 24h digital clock" },
{"-showdate", "*showdate", NULL, Is_Bool, &do_date, "Draw day-of-week month day-of-month "},
{"-analog", "*analog" , NULL, Is_Bool, &do_analog_clock, "Draw analog clock face" },
{"-hourcolor", "*hourcolor", NULL, Is_Color, &hourcolor, "Color of hour hand on analog clock "},
{"-mincolor", "*mincolor", NULL, Is_Color, &mincolor, "Color of minute hand on analog clock "},
{"-seccolor", "*seccolor", NULL, Is_Color, &seccolor, "Color of second hand on analog clock "}};
void bubblemon_session_defaults(XrmDatabase x_resource_database)
{
/* XResource stuff */
char name[BUFSIZ] = "";
XrmValue val;
XColor colorparsing;
char *type;
int i;
/* number of CPU load samples */
bm.samples = 16;
/* default colors. changeable from Xresources */
bm.air_noswap = 0x2299ff;
bm.liquid_noswap = 0x0055ff;
bm.air_maxswap = 0xff0000;
bm.liquid_maxswap = 0xaa0000;
/* default bubble engine parameters. Changeable from Xresources */
bm.maxbubbles = 100;
bm.ripples = .2;
bm.gravity = 0.01;
bm.volatility = 1;
bm.viscosity = .98;
bm.speed_limit = 1.0;
for (i = 0; i < (sizeof(x_resource_unified) / sizeof(x_resource_unified[0])); i++) {
strncpy(name,NAME,BUFSIZ), strncat(name,x_resource_unified[i].specifier,BUFSIZ-strlen(name));
if (XrmGetResource(x_resource_database, name, name, &type, &val)) {
/* Type returned by XrmGetResource is useless, it seems to always return "String" */
if (val.size > 0) /* prevent empty strings */
switch (x_resource_unified[i].parse_as) {
case Is_Int:
*(int *) x_resource_unified[i].write_to = strtol(val.addr,NULL,0);
break;
case Is_Float:
*(double *) x_resource_unified[i].write_to = strtod(val.addr,NULL);
break;
case Is_Color:
if (XParseColor(wmxp_display,
DefaultColormap(wmxp_display,
DefaultScreen(wmxp_display)),
val.addr, &colorparsing) == 0) {
fprintf(stderr,"Couldn't parse color %s for control %s\n",
val.addr,x_resource_unified[i].option);
exit(-3);
}
*(int *) x_resource_unified[i].write_to =
((colorparsing.red & 0xFF00) << 8) |
((colorparsing.green & 0xFF00)) |
((colorparsing.blue & 0xFF00) >> 8);
break;
case Is_Bool:
/* yes, on, 1, megabytes vs no, off, 0, kilobytes */
if (tolower(val.addr[0]) == 'y' ||
tolower(val.addr[0]) == 'm' ||
val.addr[0] == '1' ||
(tolower(val.addr[0]) == 'o' && tolower(val.addr[1]) == 'n'))
*(int *) x_resource_unified[i].write_to = 1; /* bools are stored in ints, sorry */
else if (tolower(val.addr[0]) == 'n' ||
tolower(val.addr[0]) == 'k' ||
val.addr[0] == '0' ||
(tolower(val.addr[0]) == 'o' && tolower(val.addr[1]) == 'f'))
*(int *) x_resource_unified[i].write_to = 0;
else {
fprintf(stderr,"Couldn't parse %s as a boolean for resource %s\n",val.addr,name);
exit(-2);
}
break;
default:
fprintf(stderr, "Compilation time error: element #%d (%s) has not-understood parse type %d\n",
i, x_resource_unified[i].option, x_resource_unified[i].parse_as);
abort();
break;
}
}
}
if (pale) {
graph_digit_color = 0x9ec4ed;
graph_warning_digit_color = 0x00ffe9;
}
/* convert doubles into integer representation */
bm.ripples_int = MAKE_INTEGER(bm.ripples);
bm.gravity_int = MAKE_INTEGER(bm.gravity);
bm.volatility_int = MAKE_INTEGER(bm.volatility);
bm.viscosity_int = MAKE_INTEGER(bm.viscosity);
bm.speed_limit_int = MAKE_INTEGER(bm.speed_limit);
}
void print_usage(void) {
char preformat[33];
int i;
printf("WMBubble version "VERSION"\n"
"Usage: "NAME" [switches] [program1] [program2] [...] [program(# of mouse buttons)]\n\n"
"Permitted options are:\n");
for (i=0; i < sizeof(x_resource_unified) / sizeof(x_resource_unified[0]); i++) {
strncpy(preformat,x_resource_unified[i].option,32);
switch(x_resource_unified[i].parse_as) {
case Is_Int:
strncat(preformat," [num]",32-strlen(preformat));
break;
case Is_Color:
strncat(preformat," [color]",32-strlen(preformat));
break;
case Is_Float:
strncat(preformat," [float]",32-strlen(preformat));
break;
case Is_Bool:
if (x_resource_unified[i].valueifnoarg == NULL)
strncat(preformat," [y/n]",32-strlen(preformat));
break;
}
printf("%-24s %s\n",preformat,x_resource_unified[i].description);
}
}
int main(int argc, char **argv) {
char execute[256];
char * x_resources_as_string;
unsigned int loadPercentage;
int gaugedelay, gaugedivisor, graphdelay, graphdivisor;
int proximity = 0;
time_t mytt;
int ii;
int i_am_visible = 1;
int iconwin_visible = 1;
int win_visible = 1;
struct tm * mytime;
int mday=0, hours=0;
#ifdef FPS
int frames_count;
time_t last_time;
#endif
#if defined(PRO) && PRO > 0
struct timeval start, end;
int cnt = PRO;
#endif
XEvent event;
XrmDatabase x_resource_db;
XrmOptionDescRec * x_resource_options;
#ifdef FPS
frames_count = last_time = 0;
#endif
/* VERY first thing: zero data structure */
memset(&bm, 0, sizeof(bm));
/* Support localized date strings */
setlocale(LC_ALL,"");
/* initialize Ximage */
bm.xim = initwmX11pixmap(argc,argv);
XrmInitialize();
x_resources_as_string = XResourceManagerString(wmxp_display);
if (x_resources_as_string == NULL)
x_resources_as_string = "";
x_resource_db = XrmGetStringDatabase(x_resources_as_string);
x_resource_options = (XrmOptionDescRec *)calloc(sizeof(XrmOptionDescRec),sizeof(x_resource_unified)/sizeof(x_resource_unified[0]));
for (ii = 0; ii < sizeof(x_resource_unified)/sizeof(x_resource_unified[0]); ii ++) {
x_resource_options[ii].option = x_resource_unified[ii].option;
x_resource_options[ii].specifier = x_resource_unified[ii].specifier;
x_resource_options[ii].value = (XPointer) x_resource_unified[ii].valueifnoarg;
x_resource_options[ii].argKind = (x_resource_unified[ii].valueifnoarg == NULL) ? XrmoptionSepArg : XrmoptionNoArg;
}
XrmParseCommand(&x_resource_db, x_resource_options,
sizeof(x_resource_unified)/sizeof(x_resource_unified[0]),
NAME, &argc, argv);
free(x_resource_options);
/* set default things, from Xresources or compiled-in defaults. Must come after initwmX11pixmap and we have a DISPLAY */
bubblemon_session_defaults(x_resource_db);
if (do_help || (argv[1] && argv[1][0] == '-')) { /* That's gotta be wrong. */
print_usage();
exit(0);
}
argv++; argc--; /* Otherwise we'll make more of ourselves on a left click */
make_new_bubblemon_dockapp();
/* the math below makes the cpu gauge try to update at 5 Hz.
Originally it was 15ms*10 meaning 7Hz */
gaugedelay = gaugedivisor = 200000 / delay_time;
if (gaugedivisor == 0) gaugedivisor = 1;
graphdelay = graphdivisor = 1000000 / delay_time;
if (graphdivisor == 0) graphdivisor = 1;
blinkdelay = 150000 / delay_time;
if (blinkdelay == 0) blinkdelay++;
loadPercentage = 0;
#ifdef PRO
gettimeofday(&start,NULL);
#endif
while (
#ifdef PRO
cnt--
#else
1
#endif
) {
/* XPending: 1184ns/frame */
while (XPending(wmxp_display)) {
XNextEvent(wmxp_display,&event);
switch (event.type) {
case ButtonPress:
if (memscreen_enabled && event.xbutton.button == 3) {
bm.picture_lock = !bm.picture_lock;
break;
}
if (event.xbutton.button <= argc) {
snprintf(execute, 250, "%s &",
argv[event.xbutton.button - 1]);
if (system(execute) == -1)
duck_blink += 6 * blinkdelay;
}
break;
case EnterNotify:
/* mouse in: make it darker, and eventually bring up
* meminfo */
proximity = 1;
if (!bm.picture_lock)
bm.screen_type = get_screen_selection();
break;
case VisibilityNotify:
if (event.xvisibility.window == wmxp_iconwin) {
iconwin_visible = !(event.xvisibility.state == VisibilityFullyObscured);
}
if (event.xvisibility.window == wmxp_win) {
win_visible = !(event.xvisibility.state == VisibilityFullyObscured);
}
i_am_visible = iconwin_visible || win_visible;
break;
case LeaveNotify:
/* mouse out: back to light */
proximity = 0;
break;
default:
break;
}
}
#ifndef PRO
usleep(delay_time);
#endif /*PRO*/
/* gmlp: 72.53us/frame */
get_memory_load_percentage();
if (++gaugedelay >= gaugedivisor) {
/* on linux, apparently opening /proc/stat is expensive, whodathunk? */
/* system_cpu: 494.0us/frame */
loadPercentage = system_cpu();
gaugedelay = 0;
}
if (memscreen_enabled && ++graphdelay >= graphdivisor) {
/* update graph histories: ? */
roll_history();
graphdelay = 0;
}
if (i_am_visible) {
/* bubblemon_update: 2.207us/frame */
do_water_sim(loadPercentage);
draw_watertank();
/* 18.68us/frame */
bubblebuf_colorspace();
}
/* 1.785us/frame */
if (duck_enabled && i_am_visible) {
duck_swimmer();
}
if (i_am_visible && cpu_enabled && gaugedelay == 0)
/* we don't want to redraw changing digits every update because that
* doesn't look so good. we throttle it above because system_cpu is
* expensive on linux. */
draw_cpugauge(loadPercentage);
/* ? */
calculate_transparencies(proximity);
/* ? */
/* originally, numbers above are updated every (30/66.7)=0.45 s and
graphs are rolled every 500/66.7=7.5 s.
For now we'll just update everything at the same rate */
if (i_am_visible && memscreen_enabled && graph_alpha < GRAPHMAXBLEND && graphdelay == 0)
render_secondary();
if (i_am_visible && cpu_enabled)
alpha_cpu();
if (i_am_visible && do_analog_clock)
draw_clockhands();
time(&mytt);
mytime = localtime(&mytt);
mday = mytime->tm_mday;
hours = 0;
if (mytime->tm_hour<shifttime) {
while (mday == mytime->tm_mday) {
mytt -= 3600; hours++;
mytime = localtime(&mytt);
}
mytime->tm_hour += hours;
}
if (i_am_visible && do_digital_clock)
alpha_digitalclock(mytime);
if (i_am_visible && do_date)
alpha_date(mytime);
if (i_am_visible && memscreen_enabled && graph_alpha < GRAPHMAXBLEND)
alpha_graph();
#ifdef FPS
/* 157ns/frame */
frames_count++;
if(time(NULL)!=last_time) {
fprintf(stderr,"%03dfps\n",frames_count);
frames_count=0;
last_time=time(NULL);
}
#endif /*FPS*/
/* drawing borders: 1.136us/frame */
if (i_am_visible) {
int xx,yy;
unsigned char * from;
for (from=bm.rgb_buf,xx=0;xx<BOX_SIZE*3-3;from++,xx++) {
from[0]/=4;
from[BOX_SIZE*(BOX_SIZE-1)*3+3]=
(255+from[BOX_SIZE*(BOX_SIZE-1)*3+3])/2;
}
for (from=bm.rgb_buf,yy=0;yy<BOX_SIZE-1;yy++,
from+=BOX_SIZE*3) {
from[0]/=4; from[1]/=4; from[2]/=4;
from[(2*BOX_SIZE-1)*3 ]=
(255+from[(2*BOX_SIZE-1)*3 ])/2;
from[(2*BOX_SIZE-1)*3+1]=
(255+from[(2*BOX_SIZE-1)*3+1])/2;
from[(2*BOX_SIZE-1)*3+2]=
(255+from[(2*BOX_SIZE-1)*3+2])/2;
}
}
/* Our colorspace conversion: 18.17us/frame */
if (i_am_visible) RGBtoXIm(bm.rgb_buf,bm.xim);
/* X11 XImage->Pixmap->display: 148.6us/frame */
if (i_am_visible) RedrawWindow(bm.xim);
}
#ifdef PRO
gettimeofday(&end,NULL);
end.tv_sec -= start.tv_sec;
end.tv_usec -= start.tv_usec;
fprintf(stderr,"%d redraws in %f seconds = %f fps, %f us/f\n",PRO,
end.tv_sec+end.tv_usec/1000000.0,
(float)PRO/(end.tv_sec+end.tv_usec/1000000.0),
(end.tv_sec*1000000.0+end.tv_usec)/(float)PRO);
#endif
return 0;
} /* main */
/*
* This determines if the left or right shift keys are depressed.
*/
int get_screen_selection(void) {
static KeyCode lshift_code, rshift_code;
static int first_time = 1;
char keys[32];
if (first_time) {
first_time = 0;
lshift_code = XKeysymToKeycode(wmxp_display,
XStringToKeysym("Shift_L"));
rshift_code = XKeysymToKeycode(wmxp_display,
XStringToKeysym("Shift_R"));
}
XQueryKeymap(wmxp_display, keys);
if ((keys[lshift_code >> 3] & (1 << (lshift_code % 8))) ||
(keys[rshift_code >> 3] & (1 << (rshift_code % 8)))) {
return 0;
} else {
return 1;
}
}
void make_new_bubblemon_dockapp(void) {
unsigned int cc, yy, maxwidth;
int xx;
/* We begin with zero bubbles */
bm.n_bubbles = 0;
/* Allocate memory for calculations */
bubblemon_allocate_buffers();
build_graphs();
sscanf(datefont_xpm[0],"%u %u %u %u",&maxwidth,&yy,&datefont_offset,&cc);
if (cc != 1) abort(); /* wat */
datefont_offset++; /* include header line */
for (yy = 1; yy < datefont_offset; yy++) {
if (strcasestr(datefont_xpm[yy],"none")) {
datefont_transparent = datefont_xpm[yy][0];
yy = datefont_offset;
}
}
/* calculate proportional spacing widths of font used for writing date */
for (cc = 33; cc < 128; cc++)
for (xx = maxwidth-1; xx >= 0; xx--)
for (yy = 0; yy < 8; yy++)
if (datefont_xpm[(cc-32)*8+yy+datefont_offset][xx] != datefont_transparent) {
datefont_widths[cc] = xx+2;
xx = -1; yy = 9;
}
datefont_widths[' ']=2;
/* force non-ascii strings to display as MONTH_IN_ROMAN_NUMERALS - DAY_OF_MONTH */
for (cc = 0; cc < 32; cc++)
datefont_widths[cc] = BOX_SIZE;
for (cc = 128; cc < 256; cc++)
datefont_widths[cc] = BOX_SIZE;
} /* make_new_bubblemon_dockapp */
/*
* This function, bubblemon_update, gets the CPU usage and updates
* the bubble array and main rgb buffer.
*/
void do_water_sim(int loadPercentage) {
unsigned int i, x;
unsigned int waterlevels_goal;
/*
The bubblebuf is made up of int8s (0..2), corresponding to the enum. A
pixel in the bubblebuf is accessed using the formula bubblebuf[row * w
+ column].
*/
/* y coordinates are counted from here multiplied by MULTIPLIER
to get actual screen coordinate, use REALY */
/* Move the water level with the current memory usage. */
waterlevels_goal = MAKEY(BOX_SIZE) - ((bm.mem_percent * MAKEY(BOX_SIZE)) / 100);
/* Guard against boundary errors */
waterlevels_goal -= (1 << (POWER2 - 1));
bm.waterlevels[0] = waterlevels_goal;
bm.waterlevels[BOX_SIZE-1] = waterlevels_goal;
for (x = 1; x < BOX_SIZE-1; x++) {
/* Accelerate the current waterlevel towards its correct value */
bm.waterlevels_dy[x] +=
(((bm.waterlevels[x - 1] + bm.waterlevels[x + 1] - 2 * bm.waterlevels[x])
* bm.volatility_int) >> (POWER2 + 1));
bm.waterlevels_dy[x] *= bm.viscosity_int;
bm.waterlevels_dy[x] >>= POWER2;
if (bm.waterlevels_dy[x] > bm.speed_limit_int)
bm.waterlevels_dy[x] = bm.speed_limit_int;
else if (bm.waterlevels_dy[x] < -bm.speed_limit_int)
bm.waterlevels_dy[x] = -bm.speed_limit_int;
}
for (x = 1; x < BOX_SIZE-1; x++) {
/* Move the current water level */
bm.waterlevels[x] = bm.waterlevels[x] + bm.waterlevels_dy[x];
if (bm.waterlevels[x] > MAKEY(BOX_SIZE)) {
/* Stop the wave if it hits the floor... */
bm.waterlevels[x] = MAKEY(BOX_SIZE);
bm.waterlevels_dy[x] = 0;
} else if (bm.waterlevels[x] < 0) {
/* ... or the ceiling. */
bm.waterlevels[x] = 0;
bm.waterlevels_dy[x] = 0;
}
}
/* Create a new bubble if the planets are correctly aligned... */
if ((bm.n_bubbles < bm.maxbubbles)
&& ((rand() % 101) <= loadPercentage)) {
/* We don't allow bubbles on the edges 'cause we'd have to clip them */
bm.bubbles[bm.n_bubbles].x = (rand() % (BOX_SIZE-2)) + 1;
bm.bubbles[bm.n_bubbles].y = MAKEY(BOX_SIZE-1);
bm.bubbles[bm.n_bubbles].dy = 0;
#ifdef DEBUG_DUCK
fprintf (stderr, "new bubble: bm.bubbles[bm.n_bubbles].x = %i\n",
bm.bubbles[bm.n_bubbles].x);
#endif
/* Raise the water level above where the bubble is created */
if (bm.bubbles[bm.n_bubbles].x > 2)
bm.waterlevels[bm.bubbles[bm.n_bubbles].x - 2] -= bm.ripples_int;
bm.waterlevels[bm.bubbles[bm.n_bubbles].x - 1] -= bm.ripples_int;
bm.waterlevels[bm.bubbles[bm.n_bubbles].x] -= bm.ripples_int;
bm.waterlevels[bm.bubbles[bm.n_bubbles].x + 1] -= bm.ripples_int;
if (bm.bubbles[bm.n_bubbles].x < (BOX_SIZE-3))
bm.waterlevels[bm.bubbles[bm.n_bubbles].x + 2] -= bm.ripples_int;
/* Count the new bubble */
bm.n_bubbles++;
}
/* Update the bubbles */
for (i = 0; i < bm.n_bubbles; i++) {
/* Accelerate the bubble */
bm.bubbles[i].dy -= bm.gravity_int;
/* Move the bubble vertically */
bm.bubbles[i].y += bm.bubbles[i].dy;
/* is the bubble grossly out of bounds? */
if (bm.bubbles[i].x < 1 || bm.bubbles[i].x > (BOX_SIZE-2) ||
bm.bubbles[i].y > MAKEY(BOX_SIZE)) {
#ifdef DEBUG_DUCK
fprintf (stderr, "bubble out of bounds "
"bm.bubbles[%i].x=%i, bm.bubbles[%i].y=%i\n",
i, bm.bubbles[i].x, i, bm.bubbles[i].y);
#endif
/* Yes; nuke it by replacing its properties with those
of the last one and deallocate the last one. */
bm.n_bubbles--;
bm.bubbles[i].x = bm.bubbles[bm.n_bubbles].x;
bm.bubbles[i].y = bm.bubbles[bm.n_bubbles].y;
bm.bubbles[i].dy = bm.bubbles[bm.n_bubbles].dy;
/*
We must still check what was the next bubble which is
now the current bubble.
*/
i--;
continue;
}
/* Did we lose it? */
if (bm.bubbles[i].y < bm.waterlevels[bm.bubbles[i].x]) {
/* Lower the water level around where the bubble is about to vanish */
bm.waterlevels[bm.bubbles[i].x - 1] += bm.ripples_int;
bm.waterlevels[bm.bubbles[i].x] += 3 * bm.ripples_int;
bm.waterlevels[bm.bubbles[i].x + 1] += bm.ripples_int;
bm.n_bubbles--;
bm.bubbles[i].x = bm.bubbles[bm.n_bubbles].x;
bm.bubbles[i].y = bm.bubbles[bm.n_bubbles].y;
bm.bubbles[i].dy = bm.bubbles[bm.n_bubbles].dy;
i--;
continue;
}
}
}
void draw_watertank(void) {
int x, y, i;
unsigned char *bubblebuf_ptr;
/* Draw the air-and-water background */
for (x = 0; x < BOX_SIZE; x++) {
/* Air... */
for (y = 0;
y < REALY(bm.waterlevels[x]); y++)
bm.bubblebuf[y * BOX_SIZE + x] = aircolor;
/* ... and water */
for (; y < BOX_SIZE; y++)
bm.bubblebuf[y * BOX_SIZE + x] = watercolor;
}
/* Draw the bubbles */
for (i = 0; i < bm.n_bubbles; i++) {
/*
Clipping is not necessary for x, but it *is* for y.
To prevent ugliness, we draw antialiascolor only on top of
watercolor, and aircolor on top of antialiascolor.
*/
/* Top row */
bubblebuf_ptr = &(bm.bubblebuf[(((REALY(bm.bubbles[i].y) - 1) * BOX_SIZE) + BOX_SIZE) + bm.bubbles[i].x - 1]);
if (bubblebuf_ptr[0] < aircolor)
bubblebuf_ptr[0]++; /* water becomes antialias; antialias becomes air for outside corners */
bubblebuf_ptr[1] = aircolor;
if (bubblebuf_ptr[2] < aircolor)
bubblebuf_ptr[2]++;
bubblebuf_ptr += BOX_SIZE;
/* Middle row - no color clipping necessary */
bubblebuf_ptr[0] = aircolor;
bubblebuf_ptr[1] = aircolor;
bubblebuf_ptr[2] = aircolor;
bubblebuf_ptr += BOX_SIZE;
/* Bottom row */
if (bm.bubbles[i].y < MAKEY(BOX_SIZE-1)) {
if (bubblebuf_ptr[0] < aircolor)
bubblebuf_ptr[0]++;
bubblebuf_ptr[1] = aircolor;
if (bubblebuf_ptr[2] < aircolor)
bubblebuf_ptr[2]++;
}
}
} /* bubblemon_update */
void bubblebuf_colorspace(void) {
unsigned char reds[3], grns[3], blus[3];
unsigned char * bubblebuf_ptr, * rgbbuf_ptr;
int count, bubblebuf_val;
/*
Vary the colors of air and water with how many percent of the available
swap space that is in use.
*/
reds[watercolor] =
(GET_RED(bm.liquid_maxswap) * bm.swap_percent +
GET_RED(bm.liquid_noswap) * (100 - bm.swap_percent)) / 100;
reds[aircolor] =
(GET_RED(bm.air_maxswap) * bm.swap_percent +
GET_RED(bm.air_noswap) * (100 - bm.swap_percent)) / 100;
reds[antialiascolor] = ((int)reds[watercolor] + reds[aircolor])/2;
grns[watercolor] =
(GET_GRN(bm.liquid_maxswap) * bm.swap_percent +
GET_GRN(bm.liquid_noswap) * (100 - bm.swap_percent)) / 100;
grns[aircolor] =
(GET_GRN(bm.air_maxswap) * bm.swap_percent +
GET_GRN(bm.air_noswap) * (100 - bm.swap_percent)) / 100;
grns[antialiascolor] = ((int)grns[watercolor] + grns[aircolor])/2;
blus[watercolor] =
(GET_BLU(bm.liquid_maxswap) * bm.swap_percent +
GET_BLU(bm.liquid_noswap) * (100 - bm.swap_percent)) / 100;
blus[aircolor] =
(GET_BLU(bm.air_maxswap) * bm.swap_percent +
GET_BLU(bm.air_noswap) * (100 - bm.swap_percent)) / 100;
blus[antialiascolor] = ((int)blus[watercolor] + blus[aircolor])/2;
for (count = BOX_SIZE*BOX_SIZE, rgbbuf_ptr = bm.rgb_buf, bubblebuf_ptr = bm.bubblebuf;
count; count--) {
bubblebuf_val = *bubblebuf_ptr++; /* -O3 did not optimize away the 3x load of *bubblebuf_ptr */
*rgbbuf_ptr++ = reds[bubblebuf_val];
*rgbbuf_ptr++ = grns[bubblebuf_val];
*rgbbuf_ptr++ = blus[bubblebuf_val];
}
} /* bubblebuf_colorspace */
void draw_from_xpm(char **xpm, unsigned char *whither, unsigned int targetw,
unsigned int xpmx, unsigned int xpmy, unsigned int xpmw,
unsigned int xpmh, unsigned int color) {
unsigned char r=GET_RED(color),g=GET_GRN(color),b=GET_BLU(color);
unsigned int yy,xx,ncolors,cpp;
unsigned char * to;
char * from;
char transparent=0;
sscanf(xpm[0],"%u %u %u %u",&xx,&yy,&ncolors,&cpp);
if (cpp != 1) abort(); /* wat */
if (xpmx+xpmw > xx || xpmy+xpmh > yy) return;
for (yy=1;yy<=ncolors;yy++) {
if (strcasestr(xpm[yy],"none")) {
transparent = xpm[yy][0];
yy=255;
}
}
for (yy=0;yy<xpmh;yy++) {
to = whither + targetw*3*yy;
from = &xpm[1+ncolors+xpmy+yy][xpmx];
for (xx=0;xx<xpmw;xx++,from++,to+=3) {
if (*from != transparent) {
to[0]=r; to[1]=g; to[2]=b;
}
}
}
}
/* draws 3x8 (4x9 padding) digits for the memory/swap panel */
void draw_digit(unsigned char * from, unsigned char * whither) {
int yy;
/* assumes layout of from is 3x9x3bpp */
for (yy = 0; yy < 8; yy++) {
memcpy(whither, from, 12);
from += 12;
whither += 3*BOX_SIZE;
}
}
/* draws a string using previous function. non-digits and non-K/M = space */
void draw_string(char *string, int x, int y, int color) {
unsigned char c;
unsigned char * graph_numbers = graph_numbers_n_rgb;
if (color) graph_numbers = graph_numbers_b_rgb;
/* bluish rgb:48,140,240 pale rgb:158,196,237
reddish rgb:237,23,23 pale(cyan) rgb:0,255,233 */
while ((c = *string++)) {
if (c == 'K') c = 10;
else if (c == 'M') c = 11;
else if (c >= '0' && c <= '9') c -= '0';
if (c <= 11)
draw_digit(&graph_numbers[3*4*9*c],
&bm.mem_buf[3*(y*BOX_SIZE+x)]);
x += 4;
}
}
/* draw graph num x size, data taken from history (num long), into rgb
buffer buf (width BOX_SIZE, height size). */
void draw_history(int num, int size, unsigned int *history, unsigned char *buf) {
int pixels_per_byte;
int yy, xx;
int height;
unsigned char mr,mg,mb, br,bg,bb;
unsigned char * graphptr;
pixels_per_byte = 100;
for (xx = 0; xx < num; xx++) {
while (history[xx] > pixels_per_byte) /* autoscaling */
pixels_per_byte += 100;
}
mr = GET_RED(graph_max);
mg = GET_GRN(graph_max);
mb = GET_BLU(graph_max);
br = GET_RED(graph_bar);
bg = GET_GRN(graph_bar);
bb = GET_BLU(graph_bar);
for (xx = 0; xx < num; xx++) {
height = size - size * history[xx] / pixels_per_byte;
for (yy = height, graphptr = &buf[(height*BOX_SIZE+xx+2)*3];
yy < height+2 && yy < size;
yy++, graphptr += 3*BOX_SIZE) {
graphptr[0] = mr; graphptr[1] = mg; graphptr[2] = mb;
}
for (;yy < size; yy++, graphptr += 3*BOX_SIZE) {
graphptr[0] = br; graphptr[1] = bg; graphptr[2] = bb;
}
}
br = GET_RED(graph_hundreds);
bg = GET_GRN(graph_hundreds);
bb = GET_BLU(graph_hundreds);
for (yy = pixels_per_byte - 100; yy > 0; yy -= 100) { /* draw lines for each 100s */
height = size - size * yy / pixels_per_byte;
graphptr = &buf[(height*BOX_SIZE+2)*3];