-
-
Notifications
You must be signed in to change notification settings - Fork 611
/
conky.cc
2352 lines (2041 loc) · 70.6 KB
/
conky.cc
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
/*
*
* Conky, a system monitor, based on torsmo
*
* Any original torsmo code is licensed under the BSD license
*
* All code written since the fork of torsmo is licensed under the GPL
*
* Please see COPYING for details
*
* Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
* Copyright (c) 2005-2024 Brenden Matthews, Philip Kovacs, et. al.
* (see AUTHORS)
* All rights reserved.
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include "conky.h"
#include "config.h"
#include <algorithm>
#include <cerrno>
#include <climits>
#include <clocale>
#include <cmath>
#include <cstdarg>
#include <ctime>
#include <filesystem>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#include <fcntl.h>
#include <getopt.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#ifdef HAVE_SYS_INOTIFY_H
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc99-extensions"
#include <sys/inotify.h>
#pragma clang diagnostic pop
#endif /* HAVE_SYS_INOTIFY_H */
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif /* HAVE_DIRENT_H */
#include "common.h"
#include "text_object.h"
#ifdef BUILD_WAYLAND
#include "wl.h"
#endif /* BUILD_WAYLAND */
#ifdef BUILD_X11
#include "x11-settings.h"
#include "x11.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wvariadic-macros"
#include <X11/Xutil.h>
#pragma GCC diagnostic pop
#ifdef BUILD_XDAMAGE
#include <X11/extensions/Xdamage.h>
#endif
#ifdef BUILD_IMLIB2
#include "conky-imlib2.h"
#endif /* BUILD_IMLIB2 */
#endif /* BUILD_X11 */
#ifdef BUILD_NCURSES
#include <ncurses.h>
#endif /* BUILD_NCURSES */
#ifdef BUILD_CURL
#include <curl/curl.h>
#endif /* BUILD_CURL */
#ifdef BUILD_RSS
#include <libxml/parser.h>
#endif /* BUILD_RSS */
/* local headers */
#include "colours.h"
#include "core.h"
#include "diskio.h"
#include "exec.h"
#ifdef BUILD_GUI
#include "fonts.h"
#include "gui.h"
#endif /* BUILD_GUI */
#include "fs.h"
#ifdef BUILD_ICONV
#include "iconv_tools.h"
#endif /* BUILD_ICONV */
#include "llua.h"
#include "logging.h"
#include "mail.h"
#include "nc.h"
#include "net_stat.h"
#include "specials.h"
#include "temphelper.h"
#include "template.h"
#include "timeinfo.h"
#include "top.h"
#ifdef BUILD_MYSQL
#include "mysql.h"
#endif /* BUILD_MYSQL */
#ifdef BUILD_NVIDIA
#include "nvidia.h"
#endif /* BUILD_NVIDIA */
#ifdef BUILD_CURL
#include "ccurl_thread.h"
#endif /* BUILD_CURL */
#include "display-output.hh"
#include "lua-config.hh"
#include "setting.hh"
/* check for OS and include appropriate headers */
#if defined(__linux__)
#include "linux.h"
#elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#include "freebsd.h"
#elif defined(__DragonFly__)
#include "dragonfly.h"
#elif defined(__OpenBSD__)
#include "openbsd.h"
#endif /* __OpenBSD__ */
#include "gradient.h"
#ifdef BUILD_OLD_CONFIG
#include "convertconf.h"
#endif /* BUILD_OLD_CONFIG */
#ifdef BUILD_BUILTIN_CONFIG
#include "defconfig.h"
namespace {
const char builtin_config_magic[] = "==builtin==";
} // namespace
#endif /* BUILD_BUILTIN_CONFIG */
#ifndef S_ISSOCK
#define S_ISSOCK(x) ((x & S_IFMT) == S_IFSOCK)
#endif
#define MAX_IF_BLOCK_DEPTH 5
// #define SIGNAL_BLOCKING
#undef SIGNAL_BLOCKING
/* debugging level, used by logging.h */
int global_debug_level = 0;
/* disable inotify auto reload feature if desired */
static conky::simple_config_setting<bool> disable_auto_reload(
"disable_auto_reload", false, false);
/* two strings for internal use */
static char *tmpstring1, *tmpstring2;
enum spacer_state { NO_SPACER = 0, LEFT_SPACER, RIGHT_SPACER };
template <>
conky::lua_traits<spacer_state>::Map conky::lua_traits<spacer_state>::map = {
{"none", NO_SPACER}, {"left", LEFT_SPACER}, {"right", RIGHT_SPACER}};
static conky::simple_config_setting<spacer_state> use_spacer("use_spacer",
NO_SPACER, false);
/* variables holding various config settings */
static conky::simple_config_setting<bool> short_units("short_units", false,
true);
static conky::simple_config_setting<bool> format_human_readable(
"format_human_readable", true, true);
conky::simple_config_setting<std::string> units_spacer("units_spacer", "",
false);
conky::simple_config_setting<bool> out_to_stdout("out_to_console",
// Default value is false, unless we are building without X
#ifdef BUILD_GUI
false,
#else
true,
#endif
false);
conky::simple_config_setting<bool> out_to_stderr("out_to_stderr", false, false);
int top_cpu, top_mem, top_time;
#ifdef BUILD_IOSTATS
int top_io;
#endif
int top_running;
/* Update interval */
conky::range_config_setting<double> update_interval(
"update_interval", 0.0, std::numeric_limits<double>::infinity(), 3.0, true);
conky::range_config_setting<double> update_interval_on_battery(
"update_interval_on_battery", 0.0, std::numeric_limits<double>::infinity(),
NOBATTERY, true);
conky::simple_config_setting<std::string> detect_battery("detect_battery",
std::string("BAT0"),
false);
static bool on_battery = false;
double active_update_interval() {
return (on_battery ? update_interval_on_battery : update_interval)
.get(*state);
}
void music_player_interval_setting::lua_setter(lua::state &l, bool init) {
lua::stack_sentry s(l, -2);
if (l.isnil(-2)) {
l.checkstack(1);
l.pushnumber(update_interval.get(l));
l.replace(-3);
}
Base::lua_setter(l, init);
++s;
}
music_player_interval_setting music_player_interval;
void *global_cpu = nullptr;
static conky::range_config_setting<unsigned int> max_text_width(
"max_text_width", 0, std::numeric_limits<unsigned int>::max(), 0, true);
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
extern kvm_t *kd;
#endif
/* prototypes for internally used functions */
static void signal_handler(int /*sig*/);
static void reload_config();
static const char *suffixes[] = {_nop("B"), _nop("KiB"), _nop("MiB"),
_nop("GiB"), _nop("TiB"), _nop("PiB"),
""};
#ifdef BUILD_GUI
/* text size */
conky::vec2i text_start; /* text start position in window */
conky::vec2i text_offset; /* offset for start position */
conky::vec2i text_size =
conky::vec2i::One(); /* initially 1 so no zero-sized window is created */
#endif /* BUILD_GUI */
/* struct that has all info to be shared between
* instances of the same text object */
struct information info;
/* path to config file */
std::filesystem::path current_config;
/* set to 1 if you want all text to be in uppercase */
static conky::simple_config_setting<bool> stuff_in_uppercase("uppercase", false,
true);
static conky::simple_config_setting<bool> stuff_in_lowercase("lowercase", false,
true);
/* Run how many times? */
static conky::range_config_setting<unsigned long> total_run_times(
"total_run_times", 0, std::numeric_limits<unsigned long>::max(), 0, true);
/* fork? */
static conky::simple_config_setting<bool> fork_to_background("background",
false, false);
/* set to 0 after the first time conky is run, so we don't fork again after the
* first forking */
int first_pass = 1;
int argc_copy;
char **argv_copy;
conky::range_config_setting<int> cpu_avg_samples("cpu_avg_samples", 1, 14, 2,
true);
conky::range_config_setting<int> net_avg_samples("net_avg_samples", 1, 14, 2,
true);
conky::range_config_setting<int> diskio_avg_samples("diskio_avg_samples", 1, 14,
2, true);
#ifdef BUILD_GUI
/* graph */
conky::simple_config_setting<bool> show_graph_scale("show_graph_scale", false,
false);
conky::simple_config_setting<bool> show_graph_range("show_graph_range", false,
false);
enum gradient_state { RGB_GRADIENT = 0, HSV_GRADIENT, HCL_GRADIENT };
template <>
conky::lua_traits<gradient_state>::Map conky::lua_traits<gradient_state>::map =
{{"rgb", RGB_GRADIENT}, {"hsv", HSV_GRADIENT}, {"hcl", HCL_GRADIENT}};
static conky::simple_config_setting<gradient_state> graph_gradient_mode(
"graph_gradient_mode", RGB_GRADIENT, false);
/* Position on the screen */
conky::simple_config_setting<int> gap_x("gap_x", 5, true);
conky::simple_config_setting<int> gap_y("gap_y", 60, true);
/* border */
conky::simple_config_setting<bool> draw_borders("draw_borders", false, false);
conky::simple_config_setting<bool> draw_graph_borders("draw_graph_borders",
true, false);
conky::range_config_setting<char> stippled_borders(
"stippled_borders", 0, std::numeric_limits<char>::max(), 0, true);
conky::simple_config_setting<bool> draw_shades("draw_shades", true, false);
conky::simple_config_setting<bool> draw_outline("draw_outline", false, false);
#ifdef OWN_WINDOW
/* fixed size/pos is set if wm/user changes them */
int fixed_size = 0, fixed_pos = 0;
#endif
conky::range_config_setting<int> minimum_height("minimum_height", 0,
std::numeric_limits<int>::max(),
5, true);
conky::range_config_setting<int> minimum_width("minimum_width", 0,
std::numeric_limits<int>::max(),
5, true);
conky::range_config_setting<int> maximum_width("maximum_width", 0,
std::numeric_limits<int>::max(),
0, true);
static bool isutf8(const char *envvar) {
char *s = getenv(envvar);
if (s != nullptr) {
std::string temp = s;
std::transform(temp.begin(), temp.end(), temp.begin(), ::tolower);
if ((temp.find("utf-8") != std::string::npos) ||
(temp.find("utf8") != std::string::npos)) {
return true;
}
}
return false;
}
/* UTF-8 */
conky::simple_config_setting<bool> utf8_mode("override_utf8_locale",
isutf8("LC_ALL") ||
isutf8("LC_CTYPE") ||
isutf8("LANG"),
false);
#endif /* BUILD_GUI */
/* maximum size of config TEXT buffer, i.e. below TEXT line. */
conky::range_config_setting<unsigned int> max_user_text(
"max_user_text", 47, std::numeric_limits<unsigned int>::max(),
MAX_USER_TEXT_DEFAULT, false);
/* maximum size of individual text buffers, ie $exec buffer size */
conky::range_config_setting<unsigned int> text_buffer_size(
"text_buffer_size", DEFAULT_TEXT_BUFFER_SIZE,
std::numeric_limits<unsigned int>::max(), DEFAULT_TEXT_BUFFER_SIZE, false);
/* pad percentages to decimals? */
static conky::simple_config_setting<int> pad_percents("pad_percents", 0, false);
static char *global_text = nullptr;
char *get_global_text() { return global_text; }
long global_text_lines;
static int total_updates;
static int updatereset;
std::unique_ptr<lua::state> state;
void set_updatereset(int i) { updatereset = i; }
int get_updatereset() { return updatereset; }
int get_total_updates() { return total_updates; }
int calc_text_width(const char *s) {
if (display_output()) return display_output()->calc_text_width(s);
size_t slen = strlen(s);
return slen;
}
#ifdef BUILD_GUI
conky::gradient_factory *create_gradient_factory(int width, Colour first_colour,
Colour last_colour) {
switch (graph_gradient_mode.get(*state)) {
case RGB_GRADIENT:
return new conky::rgb_gradient_factory(width, first_colour, last_colour);
case HSV_GRADIENT:
return new conky::hsv_gradient_factory(width, first_colour, last_colour);
case HCL_GRADIENT:
return new conky::hcl_gradient_factory(width, first_colour, last_colour);
}
return nullptr;
}
#endif /* BUILD_GUI */
/* formatted text to render on screen, generated in generate_text(),
* drawn in draw_stuff() */
static char *text_buffer;
/* quite boring functions */
static inline void for_each_line(char *b, int f(char *, int)) {
char *ps, *pe;
int special_index = 0; /* specials index */
if (b == nullptr) { return; }
for (ps = b, pe = b; *pe != 0; pe++) {
if (*pe == '\n') {
*pe = '\0';
special_index = f(ps, special_index);
*pe = '\n';
ps = pe + 1;
}
}
if (ps < pe) { f(ps, special_index); }
}
static void convert_escapes(char *buf) {
char *p = buf, *s = buf;
while (*s != 0) {
if (*s == '\\') {
s++;
if (*s == 'n') {
*p++ = '\n';
} else if (*s == '\\') {
*p++ = '\\';
}
s++;
} else {
*p++ = *s++;
}
}
*p = '\0';
}
/* Prints anything normally printed with snprintf according to the current value
* of use_spacer. Actually slightly more flexible than snprintf, as you can
* safely specify the destination buffer as one of your inputs. */
int spaced_print(char *buf, int size, const char *format, int width, ...) {
int len = 0;
va_list argp;
char *tempbuf;
if (size < 1) { return 0; }
tempbuf = new char[size];
// Passes the varargs along to vsnprintf
va_start(argp, width);
vsnprintf(tempbuf, size, format, argp);
va_end(argp);
switch (use_spacer.get(*state)) {
case NO_SPACER:
len = snprintf(buf, size, "%s", tempbuf);
break;
case LEFT_SPACER:
len = snprintf(buf, size, "%*s", width, tempbuf);
break;
case RIGHT_SPACER:
len = snprintf(buf, size, "%-*s", width, tempbuf);
break;
}
delete[] tempbuf;
return len;
}
/* print percentage values
*
* - i.e., unsigned values between 0 and 100
* - respect the value of pad_percents */
int percent_print(char *buf, int size, unsigned value) {
return spaced_print(buf, size, "%u", pad_percents.get(*state), value);
}
/* converts from bytes to human readable format (K, M, G, T)
*
* The algorithm always divides by 1024, as unit-conversion of byte
* counts suggests. But for output length determination we need to
* compare with 1000 here, as we print in decimal form. */
void human_readable(long long num, char *buf, int size) {
const char **suffix = suffixes;
float fnum;
int precision;
int width;
const char *format;
/* Possibly just output as usual, for example for stdout usage */
if (!format_human_readable.get(*state)) {
spaced_print(buf, size, "%lld", 6, num);
return;
}
if (short_units.get(*state)) {
width = 5;
format = "%.*f%s%.1s";
} else {
width = 7;
format = "%.*f%s%-.3s";
}
width += strlen(units_spacer.get(*state).c_str());
if (llabs(num) < 1000LL) {
spaced_print(buf, size, format, width, 0, static_cast<float>(num),
units_spacer.get(*state).c_str(), _(*suffix));
return;
}
while (llabs(num / 1024) >= 1000LL && (**(suffix + 2) != 0)) {
num /= 1024;
suffix++;
}
suffix++;
fnum = num / 1024.0;
/* fnum should now be < 1000, so looks like 'AAA.BBBBB'
*
* The goal is to always have a significance of 3, by
* adjusting the decimal part of the number. Sample output:
* 123MiB
* 23.4GiB
* 5.12B
* so the point of alignment resides between number and unit. The
* upside of this is that there is minimal padding necessary, though
* there should be a way to make alignment take place at the decimal
* dot (then with fixed width decimal part).
*
* Note the repdigits below: when given a precision value, printf()
* rounds the float to it, not just cuts off the remaining digits. So
* e.g. 99.95 with a precision of 1 gets 100.0, which again should be
* printed with a precision of 0. Yay. */
precision = 0; /* print 100-999 without decimal part */
if (fnum < 99.95) { precision = 1; /* print 10-99 with one decimal place */ }
if (fnum < 9.995) { precision = 2; /* print 0-9 with two decimal places */ }
spaced_print(buf, size, format, width, precision, fnum,
units_spacer.get(*state).c_str(), _(*suffix));
}
/* global object list root element */
static struct text_object global_root_object;
static Colour current_text_color;
void set_current_text_color(Colour colour) { current_text_color = colour; }
Colour get_current_text_color() { return current_text_color; }
static void extract_variable_text(const char *p) {
free_text_objects(&global_root_object);
delete_block_and_zero(tmpstring1);
delete_block_and_zero(tmpstring2);
delete_block_and_zero(text_buffer);
extract_variable_text_internal(&global_root_object, p);
}
void parse_conky_vars(struct text_object *root, const char *txt, char *p,
int p_max_size) {
extract_variable_text_internal(root, txt);
generate_text_internal(p, p_max_size, *root);
}
/* IFBLOCK jumping algorithm
*
* This is easier as it looks like:
* - each IF checks it's condition
* - on FALSE: jump
* - on TRUE: don't care
* - each ELSE jumps unconditionally
* - each ENDIF is silently being ignored
*
* Why this works (or: how jumping works):
* Jumping means to overwrite the "obj" variable of the loop and set it to the
* target (i.e. the corresponding ELSE or ENDIF). After that, the for-loop does
* the rest: as regularly, "obj" is being updated to point to obj->next, so
* object parsing continues right after the corresponding ELSE or ENDIF. This
* means that if we find an ELSE, it's corresponding IF must not have jumped,
* so we need to jump always. If we encounter an ENDIF, it's corresponding IF
* or ELSE has not jumped, and there is nothing to do.
*/
void generate_text_internal(char *p, int p_max_size, struct text_object root) {
struct text_object *obj;
size_t a;
if (p == nullptr) { return; }
#ifdef BUILD_ICONV
char *buff_in;
buff_in = new char[p_max_size];
memset(buff_in, 0, p_max_size);
#endif /* BUILD_ICONV */
p[0] = 0;
obj = root.next;
while ((obj != nullptr) && p_max_size > 0) {
/* check callbacks for existence and act accordingly */
if (obj->callbacks.print != nullptr) {
(*obj->callbacks.print)(obj, p, p_max_size);
} else if (obj->callbacks.iftest != nullptr) {
if ((*obj->callbacks.iftest)(obj) == 0) {
DBGP2("jumping");
if (obj->ifblock_next != nullptr) { obj = obj->ifblock_next; }
}
} else if (obj->callbacks.barval != nullptr) {
new_bar(obj, p, p_max_size, (*obj->callbacks.barval)(obj));
} else if (obj->callbacks.gaugeval != nullptr) {
new_gauge(obj, p, p_max_size, (*obj->callbacks.gaugeval)(obj));
#ifdef BUILD_GUI
} else if (obj->callbacks.graphval != nullptr) {
new_graph(obj, p, p_max_size, (*obj->callbacks.graphval)(obj));
#endif /* BUILD_GUI */
} else if (obj->callbacks.percentage != nullptr) {
percent_print(p, p_max_size, (*obj->callbacks.percentage)(obj));
}
a = strlen(p);
#ifdef BUILD_ICONV
iconv_convert(&a, buff_in, p, p_max_size);
#endif /* BUILD_ICONV */
p += a;
p_max_size -= a;
(*p) = 0;
obj = obj->next;
}
#ifdef BUILD_GUI
/* load any new fonts we may have had */
load_fonts(utf8_mode.get(*state));
#endif /* BUILD_GUI */
#ifdef BUILD_ICONV
delete[] buff_in;
#endif /* BUILD_ICONV */
}
void evaluate(const char *text, char *p, int p_max_size) {
struct text_object subroot {};
/**
* Consider expressions like: ${execp echo '${execp echo hi}'}
* These would require run extract_variable_text_internal() before
* callbacks and generate_text_internal() after callbacks.
*/
extract_variable_text_internal(&subroot, text);
generate_text_internal(p, p_max_size, subroot);
DBGP2("evaluated '%s' to '%s'", text, p);
free_text_objects(&subroot);
}
double current_update_time, next_update_time, last_update_time;
static void generate_text() {
char *p;
unsigned int i, j, k;
special_count = 0;
current_update_time = get_time();
/* clears netstats info, calls conky::run_all_callbacks(), and changes
* some info.mem entries */
update_stuff();
/* populate the text buffer; generate_text_internal() iterates through
* global_root_object (an instance of the text_object struct) and calls
* any callbacks that were set on startup by construct_text_object(). */
p = text_buffer;
generate_text_internal(p, max_user_text.get(*state), global_root_object);
unsigned int mw = max_text_width.get(*state);
unsigned int tbs = text_buffer_size.get(*state);
if (mw > 0) {
for (i = 0, j = 0; p[i] != 0; i++) {
if (p[i] == '\n') {
j = 0;
} else if (j == mw) {
k = i + strlen(p + i) + 1;
if (k < tbs) {
while (k != i) {
p[k] = p[k - 1];
k--;
}
p[k] = '\n';
j = 0;
} else {
NORM_ERR(
"The end of the text_buffer is reached, increase "
"\"text_buffer_size\"");
}
} else {
j++;
}
}
}
if (stuff_in_uppercase.get(*state)) {
char *tmp_p;
tmp_p = text_buffer;
while (*tmp_p != 0) {
*tmp_p = toupper(static_cast<unsigned char>(*tmp_p));
tmp_p++;
}
} else if (stuff_in_lowercase.get(*state)) {
char *tmp_p;
tmp_p = text_buffer;
while (*tmp_p != 0) {
*tmp_p = tolower(static_cast<unsigned char>(*tmp_p));
tmp_p++;
}
}
double ui = active_update_interval();
double time = get_time();
next_update_time += ui;
if (next_update_time < time || next_update_time > time + ui) {
next_update_time = time - fmod(time, ui) + ui;
}
last_update_time = current_update_time;
total_updates++;
}
int get_string_width(const char *s) { return *s != 0 ? calc_text_width(s) : 0; }
#ifdef BUILD_GUI
int get_border_total() {
return dpi_scale(border_inner_margin.get(*state)) +
dpi_scale(border_outer_margin.get(*state)) +
dpi_scale(border_width.get(*state));
}
void remove_first_char(char *s) { memmove(s, s + 1, strlen(s)); }
static int get_string_width_special(char *s, int special_index) {
char *p, *final;
special_node *current = specials;
int width = 0;
long i;
if (s == nullptr) { return 0; }
if (display_output() == nullptr || !display_output()->graphical()) {
return strlen(s);
}
p = strndup(s, text_buffer_size.get(*state));
final = p;
for (i = 0; i <= special_index; i++) { current = current->next; }
while (*p != 0) {
if (*p == SPECIAL_CHAR) {
/* shift everything over by 1 so that the special char
* doesn't mess up the size calculation */
remove_first_char(p);
/*for (i = 0; i < static_cast<long>(strlen(p)); i++) {
*(p + i) = *(p + i + 1);
}*/
if (current->type == text_node_t::GRAPH ||
current->type == text_node_t::GAUGE ||
current->type == text_node_t::BAR) {
width += current->width;
}
if (current->type == text_node_t::FONT) {
// put all following text until the next fontchange/stringend in
// influenced_by_font but do not include specials
char *influenced_by_font = strdup(p);
special_node *current_after_font = current;
// influenced_by_font gets special chars removed, so after this loop i
// counts the number of letters (not special chars) influenced by font
for (i = 0; influenced_by_font[i] != 0; i++) {
if (influenced_by_font[i] == SPECIAL_CHAR) {
// remove specials and stop at fontchange
current_after_font = current_after_font->next;
if (current_after_font->type == text_node_t::FONT) {
influenced_by_font[i] = 0;
break;
}
remove_first_char(&influenced_by_font[i]);
}
}
// add the length of influenced_by_font in the new font to width
int orig_font = selected_font;
selected_font = current->font_added;
width += calc_text_width(influenced_by_font);
selected_font = orig_font;
free(influenced_by_font);
// make sure the chars counted in the new font are not again counted
// in the old font
int specials_skipped = 0;
while (i > 0) {
if (p[specials_skipped] != SPECIAL_CHAR) {
remove_first_char(&p[specials_skipped]);
// i only counts non-special chars, so only decrement it for those
i--;
} else {
specials_skipped++;
}
}
}
current = current->next;
} else {
p++;
}
}
if (strlen(final) > 1) { width += calc_text_width(final); }
free(final);
return width;
}
static int text_size_updater(char *s, int special_index);
int last_font_height;
void update_text_area() {
conky::vec2i xy;
if (display_output() == nullptr || !display_output()->graphical()) { return; }
/* update text size if it isn't fixed */
#ifdef OWN_WINDOW
if (fixed_size == 0)
#endif
{
text_size = conky::vec2i(dpi_scale(minimum_width.get(*state)), 0);
last_font_height = font_height();
for_each_line(text_buffer, text_size_updater);
text_size = text_size.max(conky::vec2i(text_size.x() + 1, minimum_height.get(*state)));
int mw = dpi_scale(maximum_width.get(*state));
if (mw > 0) text_size = text_size.min(conky::vec2i(mw, text_size.y()));
}
alignment align = text_alignment.get(*state);
/* get text position on workarea */
switch (vertical_alignment(align)) {
case axis_align::START:
xy.set_y(workarea.y() + dpi_scale(gap_y.get(*state)));
break;
case axis_align::END:
default:
xy.set_y(workarea.end_y() - text_size.y() - dpi_scale(gap_y.get(*state)));
break;
case axis_align::MIDDLE:
xy.set_y(workarea.y() + workarea.height() / 2 - text_size.y() / 2 -
dpi_scale(gap_y.get(*state)));
break;
}
switch (horizontal_alignment(align)) {
case axis_align::START:
default:
xy.set_x(workarea.x() + dpi_scale(gap_x.get(*state)));
break;
case axis_align::END:
xy.set_x(workarea.end_x() - text_size.x() - dpi_scale(gap_x.get(*state)));
break;
case axis_align::MIDDLE:
xy.set_x(workarea.x() + workarea.width() / 2 - text_size.x() / 2 -
dpi_scale(gap_x.get(*state)));
break;
}
#ifdef OWN_WINDOW
if (align == alignment::NONE) { // Let the WM manage the window
xy = window.geometry.pos();
fixed_pos = 1;
fixed_size = 1;
}
#endif /* OWN_WINDOW */
#ifdef OWN_WINDOW
if (own_window.get(*state) && (fixed_pos == 0)) {
int border_total = get_border_total();
text_start = conky::vec2i::uniform(border_total);
window.geometry.set_pos(xy - text_start);
} else
#endif
{
text_start = xy;
}
/* update lua window globals */
llua_update_window_table(conky::rect<int>(text_start, text_size));
}
/* drawing stuff */
static int cur_x, cur_y; /* current x and y for drawing */
#endif
// draw_mode also without BUILD_GUI because we only need to print to stdout with
// FG
static draw_mode_t draw_mode; /* FG, BG or OUTLINE */
#ifdef BUILD_GUI
/*static*/ Colour current_color;
static int text_size_updater(char *s, int special_index) {
int w = 0;
char *p;
special_node *current = specials;
for (int i = 0; i < special_index; i++) { current = current->next; }
if (display_output() == nullptr || !display_output()->graphical()) {
return 0;
}
/* get string widths and skip specials */
p = s;
while (*p != 0) {
if (*p == SPECIAL_CHAR) {
*p = '\0';
w += get_string_width(s);
*p = SPECIAL_CHAR;
if (current->type == text_node_t::BAR ||
current->type == text_node_t::GAUGE ||
current->type == text_node_t::GRAPH) {
w += current->width;
if (current->height > last_font_height) {
last_font_height = current->height;
last_font_height += font_height();
}
} else if (current->type == text_node_t::OFFSET) {
if (current->arg > 0) { w += current->arg; }
} else if (current->type == text_node_t::VOFFSET) {
last_font_height += current->arg;
} else if (current->type == text_node_t::GOTO) {
if (current->arg > cur_x) { w = static_cast<int>(current->arg); }
} else if (current->type == text_node_t::TAB) {
int start = current->arg;
int step = current->width;
if ((step == 0) || step < 0) { step = 10; }
w += step - (cur_x - text_start.x() - start) % step;
} else if (current->type == text_node_t::FONT) {
selected_font = current->font_added;
if (font_height() > last_font_height) {
last_font_height = font_height();
}
}
special_index++;
current = current->next;
s = p + 1;
}
p++;
}
w += get_string_width(s);
if (w > text_size.x()) { text_size.set_x(w); }
int mw = dpi_scale(maximum_width.get(*state));
if (mw > 0) { text_size.set_x(std::min(mw, text_size.x())); }
text_size += conky::vec2i(0, last_font_height);
last_font_height = font_height();
return special_index;
}
#endif /* BUILD_GUI */
static inline void set_foreground_color(Colour c) {
for (auto output : display_outputs()) output->set_foreground_color(c);
}
static void draw_string(const char *s) {
int i;
int i2;