1 /*
2  *
3  * Conky, a system monitor, based on torsmo
4  *
5  * Any original torsmo code is licensed under the BSD license
6  *
7  * All code written since the fork of torsmo is licensed under the GPL
8  *
9  * Please see COPYING for details
10  *
11  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
12  * Copyright (c) 2005-2021 Brenden Matthews, Philip Kovacs, et. al.
13  *	(see AUTHORS)
14  * All rights reserved.
15  *
16  * This program is free software: you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation, either version 3 of the License, or
19  * (at your option) any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  * You should have received a copy of the GNU General Public License
26  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27  *
28  */
29 #include "conky.h"
30 #ifdef BUILD_X11
31 #include "fonts.h"
32 #endif /* BUILD_X11 */
33 #include <cmath>
34 #include "logging.h"
35 #include "nc.h"
36 #include "specials.h"
37 #ifdef HAVE_SYS_PARAM_H
38 #include <sys/param.h>
39 #endif /* HAVE_SYS_PARAM_H */
40 #include <algorithm>
41 #include <map>
42 #include <sstream>
43 #include "colours.h"
44 #include "common.h"
45 #include "conky.h"
46 
47 struct special_t *specials = nullptr;
48 
49 int special_count;
50 int graph_count = 0;
51 
52 std::map<int, double *> graphs;
53 
54 namespace {
55 conky::range_config_setting<int> default_bar_width(
56     "default_bar_width", 0, std::numeric_limits<int>::max(), 0, false);
57 conky::range_config_setting<int> default_bar_height(
58     "default_bar_height", 0, std::numeric_limits<int>::max(), 6, false);
59 
60 #ifdef BUILD_X11
61 conky::range_config_setting<int> default_graph_width(
62     "default_graph_width", 0, std::numeric_limits<int>::max(), 0, false);
63 conky::range_config_setting<int> default_graph_height(
64     "default_graph_height", 0, std::numeric_limits<int>::max(), 25, false);
65 
66 conky::range_config_setting<int> default_gauge_width(
67     "default_gauge_width", 0, std::numeric_limits<int>::max(), 40, false);
68 conky::range_config_setting<int> default_gauge_height(
69     "default_gauge_height", 0, std::numeric_limits<int>::max(), 25, false);
70 #endif /* BUILD_X11 */
71 
72 conky::simple_config_setting<std::string> console_graph_ticks(
73     "console_graph_ticks", " ,_,=,#", false);
74 }  // namespace
75 
76 /* special data types flags */
77 #define SF_SCALED (1 << 0)
78 #define SF_SHOWLOG (1 << 1)
79 
80 /*
81  * Special data typedefs
82  */
83 
84 struct bar {
85   char flags;
86   int width, height;
87   double scale;
88 };
89 
90 struct gauge {
91   char flags;
92   int width, height;
93   double scale;
94 };
95 
96 struct graph {
97   int id;
98   char flags;
99   int width, height;
100   unsigned int first_colour, last_colour;
101   double scale;
102   char tempgrad;
103 };
104 
105 struct stippled_hr {
106   int height, arg;
107 };
108 
109 struct tab {
110   int width, arg;
111 };
112 
113 /*
114  * Scanning arguments to various special text objects
115  */
116 
117 #ifdef BUILD_X11
scan_gauge(struct text_object * obj,const char * args,double scale)118 const char *scan_gauge(struct text_object *obj, const char *args,
119                        double scale) {
120   struct gauge *g;
121 
122   g = static_cast<struct gauge *>(malloc(sizeof(struct gauge)));
123   memset(g, 0, sizeof(struct gauge));
124 
125   /*width and height*/
126   g->width = default_gauge_width.get(*state);
127   g->height = default_gauge_height.get(*state);
128 
129   if (scale != 0.0) {
130     g->scale = scale;
131   } else {
132     g->flags |= SF_SCALED;
133   }
134 
135   /* gauge's argument is either height or height,width */
136   if (args != nullptr) {
137     int n = 0;
138 
139     if (sscanf(args, "%d,%d %n", &g->height, &g->width, &n) <= 1) {
140       if (sscanf(args, "%d %n", &g->height, &n) == 2) {
141         g->width = g->height; /*square gauge*/
142       }
143     }
144     args += n;
145   }
146 
147   obj->special_data = g;
148   return args;
149 }
150 #endif
151 
scan_bar(struct text_object * obj,const char * args,double scale)152 const char *scan_bar(struct text_object *obj, const char *args, double scale) {
153   struct bar *b;
154 
155   b = static_cast<struct bar *>(malloc(sizeof(struct bar)));
156   memset(b, 0, sizeof(struct bar));
157 
158   /* zero width means all space that is available */
159   b->width = default_bar_width.get(*state);
160   b->height = default_bar_height.get(*state);
161 
162   if (scale != 0.0) {
163     b->scale = scale;
164   } else {
165     b->flags |= SF_SCALED;
166   }
167 
168   /* bar's argument is either height or height,width */
169   if (args != nullptr) {
170     int n = 0;
171 
172     if (sscanf(args, "%d,%d %n", &b->height, &b->width, &n) <= 1) {
173       sscanf(args, "%d %n", &b->height, &n);
174     }
175     args += n;
176   }
177 
178   obj->special_data = b;
179   return args;
180 }
181 
182 #ifdef BUILD_X11
scan_font(struct text_object * obj,const char * args)183 void scan_font(struct text_object *obj, const char *args) {
184   if ((args != nullptr) && (*args != 0)) {
185     obj->data.s = strndup(args, DEFAULT_TEXT_BUFFER_SIZE);
186   }
187 }
188 
189 /**
190  * parses for [height,width] [color1 color2] [scale] [-t] [-l]
191  *
192  * -l will set the showlog flag, enabling logarithmic graph scales
193  * -t will set the tempgrad member to true, enabling temperature gradient colors
194  *
195  * @param[out] obj  struct in which to save width, height and other options
196  * @param[in]  args argument string to parse
197  * @param[in]  defscale default scale if no scale argument given
198  * @return string to the command argument, nullptr if argument didn't start with
199  *         a string, but a number or if invalid argument string
200  **/
scan_graph(struct text_object * obj,const char * args,double defscale)201 char *scan_graph(struct text_object *obj, const char *args, double defscale) {
202   char quoted_cmd[1024] = {'\0'}; /* double-quoted execgraph command */
203   char argstr[1024] = {'\0'};     /* args minus quoted_cmd */
204   char buf[1024] = {'\0'};        /* first unquoted string argument in argstr */
205 
206   auto *g = static_cast<struct graph *>(malloc(sizeof(struct graph)));
207   memset(g, 0, sizeof(struct graph));
208   obj->special_data = g;
209 
210   /* zero width means all space that is available */
211   g->id = ++graph_count;
212   g->width = default_graph_width.get(*state);
213   g->height = default_graph_height.get(*state);
214   g->first_colour = 0;
215   g->last_colour = 0;
216   g->scale = defscale;
217   g->tempgrad = FALSE;
218   if (args != nullptr) {
219     /* extract double-quoted command in case of execgraph */
220     if (*args == '"') {
221       char *_ptr;
222       size_t _size;
223       if (((_ptr = const_cast<char *>(strrchr(args, '"'))) != nullptr) &&
224           _ptr != args) {
225         _size = _ptr - args - 1;
226       } else {
227         NORM_ERR("mismatched double-quote in execgraph object");
228         return nullptr;
229       }
230 
231       _size = _size < 1024 ? _size : 1023;
232       strncpy(quoted_cmd, args + 1, _size);
233       quoted_cmd[_size] = '\0';
234 
235       /* copy everything after the last quote into argstr */
236       if (_size + 2 < strlen(args)) { strncpy(argstr, args + _size + 2, 1023); }
237     } else {
238       /* redundant, but simplifies the code below */
239       strncpy(argstr, args, 1023);
240     }
241 
242     /* set tempgrad to true, if '-t' specified.
243      * It doesn#t matter where the argument is exactly. */
244     if ((strstr(argstr, " " TEMPGRAD) != nullptr) ||
245         strncmp(argstr, TEMPGRAD, strlen(TEMPGRAD)) == 0) {
246       g->tempgrad = TRUE;
247     }
248     /* set showlog-flag, if '-l' specified
249      * It doesn#t matter where the argument is exactly. */
250     if ((strstr(argstr, " " LOGGRAPH) != nullptr) ||
251         strncmp(argstr, LOGGRAPH, strlen(LOGGRAPH)) == 0) {
252       g->flags |= SF_SHOWLOG;
253     }
254 
255     /* all the following functions try to interpret the beginning of a
256      * a string with different formaters. If successfully the return from
257      * this whole function */
258 
259     /* interpret the beginning(!) of the argument string as:
260      * '[height],[width] [color1] [color2] [scale]'
261      * This means parameters like -t and -l may not be in the beginning */
262     if (sscanf(argstr, "%d,%d %x %x %lf", &g->height, &g->width,
263                &g->first_colour, &g->last_colour, &g->scale) == 5) {
264       return *quoted_cmd != 0
265                  ? strndup(quoted_cmd, text_buffer_size.get(*state))
266                  : nullptr;
267     }
268     /* [height],[width] [color1] [color2] */
269     g->scale = defscale;
270     if (sscanf(argstr, "%d,%d %x %x", &g->height, &g->width, &g->first_colour,
271                &g->last_colour) == 4) {
272       return *quoted_cmd != 0
273                  ? strndup(quoted_cmd, text_buffer_size.get(*state))
274                  : nullptr;
275     }
276     /* [command] [height],[width] [color1] [color2] [scale] */
277     if (sscanf(argstr, "%1023s %d,%d %x %x %lf", buf, &g->height, &g->width,
278                &g->first_colour, &g->last_colour, &g->scale) == 6) {
279       return strndup(buf, text_buffer_size.get(*state));
280     }
281     g->scale = defscale;
282     if (sscanf(argstr, "%1023s %d,%d %x %x", buf, &g->height, &g->width,
283                &g->first_colour, &g->last_colour) == 5) {
284       return strndup(buf, text_buffer_size.get(*state));
285     }
286 
287     buf[0] = '\0';
288     g->height = default_graph_height.get(*state);
289     g->width = default_graph_width.get(*state);
290     if (sscanf(argstr, "%x %x %lf", &g->first_colour, &g->last_colour,
291                &g->scale) == 3) {
292       return *quoted_cmd != 0
293                  ? strndup(quoted_cmd, text_buffer_size.get(*state))
294                  : nullptr;
295     }
296     g->scale = defscale;
297     if (sscanf(argstr, "%x %x", &g->first_colour, &g->last_colour) == 2) {
298       return *quoted_cmd != 0
299                  ? strndup(quoted_cmd, text_buffer_size.get(*state))
300                  : nullptr;
301     }
302     if (sscanf(argstr, "%1023s %x %x %lf", buf, &g->first_colour,
303                &g->last_colour, &g->scale) == 4) {
304       return strndup(buf, text_buffer_size.get(*state));
305     }
306     g->scale = defscale;
307     if (sscanf(argstr, "%1023s %x %x", buf, &g->first_colour,
308                &g->last_colour) == 3) {
309       return strndup(buf, text_buffer_size.get(*state));
310     }
311 
312     buf[0] = '\0';
313     g->first_colour = 0;
314     g->last_colour = 0;
315     if (sscanf(argstr, "%d,%d %lf", &g->height, &g->width, &g->scale) == 3) {
316       return *quoted_cmd != 0
317                  ? strndup(quoted_cmd, text_buffer_size.get(*state))
318                  : nullptr;
319     }
320     g->scale = defscale;
321     if (sscanf(argstr, "%d,%d", &g->height, &g->width) == 2) {
322       return *quoted_cmd != 0
323                  ? strndup(quoted_cmd, text_buffer_size.get(*state))
324                  : nullptr;
325     }
326     if (sscanf(argstr, "%1023s %d,%d %lf", buf, &g->height, &g->width,
327                &g->scale) < 4) {
328       g->scale = defscale;
329       // TODO(brenden): check the return value and throw an error?
330       sscanf(argstr, "%1023s %d,%d", buf, &g->height, &g->width);
331     }
332 
333     if ((*quoted_cmd == 0) && (*buf == 0)) { return nullptr; }
334     return strndup(*quoted_cmd != 0 ? quoted_cmd : buf,
335                    text_buffer_size.get(*state));
336   }
337 
338   return nullptr;
339 }
340 #endif /* BUILD_X11 */
341 
342 /*
343  * Printing various special text objects
344  */
345 
new_special_t_node()346 struct special_t *new_special_t_node() {
347   auto *newnode = new special_t;
348 
349   memset(newnode, 0, sizeof *newnode);
350   return newnode;
351 }
352 
353 /**
354  * expands the current global linked list specials to special_count elements
355  *
356  * increases special_count
357  * @param[out] buf is set to "\x01\x00" not sure why ???
358  * @param[in]  t   special type enum, e.g. alignc, alignr, fg, bg, ...
359  * @return pointer to the newly inserted special of type t
360  **/
new_special(char * buf,enum special_types t)361 struct special_t *new_special(char *buf, enum special_types t) {
362   special_t *current;
363 
364   buf[0] = SPECIAL_CHAR;
365   buf[1] = '\0';
366   if (specials == nullptr) { specials = new_special_t_node(); }
367   current = specials;
368   /* allocate special_count linked list elements */
369   for (int i = 0; i < special_count; i++) {
370     if (current->next == nullptr) { current->next = new_special_t_node(); }
371     current = current->next;
372   }
373   current->type = t;
374   special_count++;
375   return current;
376 }
377 
new_gauge_in_shell(struct text_object * obj,char * p,unsigned int p_max_size,double usage)378 void new_gauge_in_shell(struct text_object *obj, char *p,
379                         unsigned int p_max_size, double usage) {
380   static const char *gaugevals[] = {"_. ", "\\. ", " | ", " ./", " ._"};
381   auto *g = static_cast<struct gauge *>(obj->special_data);
382 
383   snprintf(p, p_max_size, "%s",
384            gaugevals[round_to_positive_int(usage * 4 / g->scale)]);
385 }
386 
387 #ifdef BUILD_X11
new_gauge_in_x11(struct text_object * obj,char * buf,double usage)388 void new_gauge_in_x11(struct text_object *obj, char *buf, double usage) {
389   struct special_t *s = nullptr;
390   auto *g = static_cast<struct gauge *>(obj->special_data);
391 
392   if (!out_to_x.get(*state)) { return; }
393 
394   if (g == nullptr) { return; }
395 
396   s = new_special(buf, GAUGE);
397 
398   s->arg = usage;
399   s->width = xft_dpi_scale(g->width);
400   s->height = xft_dpi_scale(g->height);
401   s->scale = g->scale;
402 }
403 #endif /* BUILD_X11 */
404 
new_gauge(struct text_object * obj,char * p,unsigned int p_max_size,double usage)405 void new_gauge(struct text_object *obj, char *p, unsigned int p_max_size,
406                double usage) {
407   auto *g = static_cast<struct gauge *>(obj->special_data);
408 
409   if ((p_max_size == 0) || (g == nullptr)) { return; }
410 
411   if ((g->flags & SF_SCALED) != 0) {
412     g->scale = std::max(g->scale, usage);
413   } else {
414     usage = std::min(g->scale, usage);
415   }
416 
417 #ifdef BUILD_X11
418   if (out_to_x.get(*state)) { new_gauge_in_x11(obj, p, usage); }
419   if (out_to_stdout.get(*state)) {
420     new_gauge_in_shell(obj, p, p_max_size, usage);
421   }
422 #else  /* BUILD_X11 */
423   new_gauge_in_shell(obj, p, p_max_size, usage);
424 #endif /* BUILD_X11 */
425 }
426 
427 #ifdef BUILD_X11
new_font(struct text_object * obj,char * p,unsigned int p_max_size)428 void new_font(struct text_object *obj, char *p, unsigned int p_max_size) {
429   struct special_t *s;
430   unsigned int tmp = selected_font;
431 
432   if (!out_to_x.get(*state)) { return; }
433 
434   if (p_max_size == 0) { return; }
435 
436   s = new_special(p, FONT);
437 
438   if (obj->data.s != nullptr) {
439     if (s->font_added >= static_cast<int>(fonts.size()) ||
440         (s->font_added == 0) || obj->data.s != fonts[s->font_added].name) {
441       selected_font = s->font_added = add_font(obj->data.s);
442       selected_font = tmp;
443     }
444   } else {
445     selected_font = s->font_added = 0;
446     selected_font = tmp;
447   }
448 }
449 
450 /**
451  * Adds value f to graph possibly truncating and scaling the graph
452  **/
graph_append(struct special_t * graph,double f,char showaslog)453 static void graph_append(struct special_t *graph, double f, char showaslog) {
454   int i;
455 
456   /* do nothing if we don't even have a graph yet */
457   if (graph->graph == nullptr) { return; }
458 
459   if (showaslog != 0) {
460 #ifdef BUILD_MATH
461     f = log10(f + 1);
462 #endif
463   }
464 
465   if ((graph->scaled == 0) && f > graph->scale) { f = graph->scale; }
466 
467   /* shift all the data by 1 */
468   for (i = graph->graph_allocated - 1; i > 0; i--) {
469     graph->graph[i] = graph->graph[i - 1];
470   }
471   graph->graph[0] = f; /* add new data */
472 
473   if (graph->scaled != 0) {
474     graph->scale =
475         *std::max_element(graph->graph + 0, graph->graph + graph->graph_width);
476     if (graph->scale < 1e-47) {
477       /* avoid NaN's when the graph is all-zero (e.g. before the first update)
478        * there is nothing magical about 1e-47 here */
479       graph->scale = 1e-47;
480     }
481   }
482 }
483 
new_graph_in_shell(struct special_t * s,char * buf,int buf_max_size)484 void new_graph_in_shell(struct special_t *s, char *buf, int buf_max_size) {
485   // Split config string on comma to avoid the hassle of dealing with the
486   // idiosyncrasies of multi-byte unicode on different platforms.
487   // TODO(brenden): Parse config string once and cache result.
488   const std::string ticks = console_graph_ticks.get(*state);
489   std::stringstream ss(ticks);
490   std::string tickitem;
491   std::vector<std::string> tickitems;
492   while (std::getline(ss, tickitem, ',')) { tickitems.push_back(tickitem); }
493 
494   char *p = buf;
495   char *buf_max = buf + (sizeof(char) * buf_max_size);
496   double scale = (tickitems.size() - 1) / s->scale;
497   for (int i = s->graph_allocated - 1; i >= 0; i--) {
498     const unsigned int v = round_to_positive_int(s->graph[i] * scale);
499     const char *tick = tickitems[v].c_str();
500     size_t itemlen = tickitems[v].size();
501     for (unsigned int j = 0; j < itemlen; j++) {
502       *p++ = tick[j];
503       if (p == buf_max) { goto graph_buf_end; }
504     }
505   }
506 graph_buf_end:
507   *p = '\0';
508 }
509 
copy_graph(double * original_graph,int graph_width)510 double *copy_graph(double *original_graph, int graph_width) {
511   double *new_graph =
512       static_cast<double *>(malloc(graph_width * sizeof(double)));
513 
514   memcpy(new_graph, original_graph, graph_width * sizeof(double));
515 
516   return new_graph;
517 }
518 
retrieve_graph(int graph_id,int graph_width)519 double *retrieve_graph(int graph_id, int graph_width) {
520   if (graphs.find(graph_id) == graphs.end()) {
521     return static_cast<double *>(calloc(1, graph_width * sizeof(double)));
522   } else {
523     return copy_graph(graphs[graph_id], graph_width);
524   }
525 }
526 
store_graph(int graph_id,struct special_t * s)527 void store_graph(int graph_id, struct special_t *s) {
528   if (s->graph == nullptr) {
529     graphs[graph_id] = nullptr;
530   } else {
531     if (graphs.find(graph_id) != graphs.end()) { free(graphs[graph_id]); }
532     graphs[graph_id] = s->graph;
533   }
534 }
535 
536 /**
537  * Creates a visual graph and/or appends val to the graph / plot
538  *
539  * @param[in] obj struct containing all relevant flags like width, height, ...
540  * @param[in] buf buffer for ascii art graph in console
541  * @param[in] buf_max_size maximum length of buf
542  * @param[in] val value to plot i.e. to add to plot
543  **/
new_graph(struct text_object * obj,char * buf,int buf_max_size,double val)544 void new_graph(struct text_object *obj, char *buf, int buf_max_size,
545                double val) {
546   struct special_t *s = nullptr;
547   auto *g = static_cast<struct graph *>(obj->special_data);
548 
549   if ((g == nullptr) || (buf_max_size == 0)) { return; }
550 
551   s = new_special(buf, GRAPH);
552 
553   /* set graph (special) width to width in obj */
554   s->width = xft_dpi_scale(g->width);
555   if (s->width != 0) { s->graph_width = s->width; }
556 
557   if (s->graph_width != s->graph_allocated) {
558     auto *graph = static_cast<double *>(
559         realloc(s->graph, s->graph_width * sizeof(double)));
560     DBGP("reallocing graph from %d to %d", s->graph_allocated, s->graph_width);
561     if (s->graph == nullptr) {
562       /* initialize */
563       std::fill_n(graph, s->graph_width, 0.0);
564       s->scale = 100;
565     } else if (graph != nullptr) {
566       if (s->graph_width > s->graph_allocated) {
567         /* initialize the new region */
568         std::fill(graph + s->graph_allocated, graph + s->graph_width, 0.0);
569       }
570     } else {
571       DBGP("reallocing FAILED");
572       graph = s->graph;
573       s->graph_width = s->graph_allocated;
574     }
575     s->graph = graph;
576     s->graph_allocated = s->graph_width;
577     graphs[g->id] = graph;
578   }
579   s->height = xft_dpi_scale(g->height);
580   s->first_colour = adjust_colours(g->first_colour);
581   s->last_colour = adjust_colours(g->last_colour);
582   if (g->scale != 0) {
583     s->scaled = 0;
584     s->scale = g->scale;
585     s->show_scale = 0;
586   } else {
587     s->scaled = 1;
588     s->scale = 1;
589     s->show_scale = 1;
590   }
591   s->tempgrad = g->tempgrad;
592 #ifdef BUILD_MATH
593   if ((g->flags & SF_SHOWLOG) != 0) {
594     s->scale_log = 1;
595     s->scale = log10(s->scale + 1);
596   }
597 #endif
598 
599   int graph_id = ((struct graph *)obj->special_data)->id;
600   s->graph = retrieve_graph(graph_id, s->graph_width);
601 
602   graph_append(s, val, g->flags);
603 
604   store_graph(graph_id, s);
605 
606   if (out_to_stdout.get(*state)) { new_graph_in_shell(s, buf, buf_max_size); }
607 }
608 
new_hr(struct text_object * obj,char * p,unsigned int p_max_size)609 void new_hr(struct text_object *obj, char *p, unsigned int p_max_size) {
610   if (!out_to_x.get(*state)) { return; }
611 
612   if (p_max_size == 0) { return; }
613 
614   new_special(p, HORIZONTAL_LINE)->height = xft_dpi_scale(obj->data.l);
615 }
616 
scan_stippled_hr(struct text_object * obj,const char * arg)617 void scan_stippled_hr(struct text_object *obj, const char *arg) {
618   struct stippled_hr *sh;
619 
620   sh = static_cast<struct stippled_hr *>(malloc(sizeof(struct stippled_hr)));
621   memset(sh, 0, sizeof(struct stippled_hr));
622 
623   sh->arg = stippled_borders.get(*state);
624   sh->height = 1;
625 
626   if (arg != nullptr) {
627     if (sscanf(arg, "%d %d", &sh->arg, &sh->height) != 2) {
628       sscanf(arg, "%d", &sh->height);
629     }
630   }
631   if (sh->arg <= 0) { sh->arg = 1; }
632   obj->special_data = sh;
633 }
634 
new_stippled_hr(struct text_object * obj,char * p,unsigned int p_max_size)635 void new_stippled_hr(struct text_object *obj, char *p,
636                      unsigned int p_max_size) {
637   struct special_t *s = nullptr;
638   auto *sh = static_cast<struct stippled_hr *>(obj->special_data);
639 
640   if (!out_to_x.get(*state)) { return; }
641 
642   if ((sh == nullptr) || (p_max_size == 0)) { return; }
643 
644   s = new_special(p, STIPPLED_HR);
645 
646   s->height = xft_dpi_scale(sh->height);
647   s->arg = xft_dpi_scale(sh->arg);
648 }
649 #endif /* BUILD_X11 */
650 
new_fg(struct text_object * obj,char * p,unsigned int p_max_size)651 void new_fg(struct text_object *obj, char *p, unsigned int p_max_size) {
652 #ifdef BUILD_X11
653   if (out_to_x.get(*state)) { new_special(p, FG)->arg = obj->data.l; }
654 #endif /* BUILD_X11 */
655 #ifdef BUILD_NCURSES
656   if (out_to_ncurses.get(*state)) { new_special(p, FG)->arg = obj->data.l; }
657 #endif /* BUILD_NCURSES */
658   UNUSED(obj);
659   UNUSED(p);
660   UNUSED(p_max_size);
661 }
662 
663 #ifdef BUILD_X11
new_bg(struct text_object * obj,char * p,unsigned int p_max_size)664 void new_bg(struct text_object *obj, char *p, unsigned int p_max_size) {
665   if (!out_to_x.get(*state)) { return; }
666 
667   if (p_max_size == 0) { return; }
668 
669   new_special(p, BG)->arg = obj->data.l;
670 }
671 #endif /* BUILD_X11 */
672 
new_bar_in_shell(struct text_object * obj,char * buffer,unsigned int buf_max_size,double usage)673 static void new_bar_in_shell(struct text_object *obj, char *buffer,
674                              unsigned int buf_max_size, double usage) {
675   auto *b = static_cast<struct bar *>(obj->special_data);
676   unsigned int width, i, scaledusage;
677 
678   if (b == nullptr) { return; }
679 
680   width = b->width;
681   if (width == 0) { width = DEFAULT_BAR_WIDTH_NO_X; }
682 
683   if (width > buf_max_size) { width = buf_max_size; }
684 
685   scaledusage = round_to_positive_int(usage * width / b->scale);
686 
687   for (i = 0; i < scaledusage; i++) {
688     buffer[i] = *(bar_fill.get(*state).c_str());
689   }
690 
691   for (; i < width; i++) { buffer[i] = *(bar_unfill.get(*state).c_str()); }
692 
693   buffer[i] = 0;
694 }
695 
696 #ifdef BUILD_X11
new_bar_in_x11(struct text_object * obj,char * buf,double usage)697 static void new_bar_in_x11(struct text_object *obj, char *buf, double usage) {
698   struct special_t *s = nullptr;
699   auto *b = static_cast<struct bar *>(obj->special_data);
700 
701   if (!out_to_x.get(*state)) { return; }
702 
703   if (b == nullptr) { return; }
704 
705   s = new_special(buf, BAR);
706 
707   s->arg = usage;
708   s->width = xft_dpi_scale(b->width);
709   s->height = xft_dpi_scale(b->height);
710   s->scale = b->scale;
711 }
712 #endif /* BUILD_X11 */
713 
714 /* usage is in range [0,255] */
new_bar(struct text_object * obj,char * p,unsigned int p_max_size,double usage)715 void new_bar(struct text_object *obj, char *p, unsigned int p_max_size,
716              double usage) {
717   auto *b = static_cast<struct bar *>(obj->special_data);
718 
719   if ((p_max_size == 0) || (b == nullptr)) { return; }
720 
721   if ((b->flags & SF_SCALED) != 0) {
722     b->scale = std::max(b->scale, usage);
723   } else {
724     usage = std::min(b->scale, usage);
725   }
726 
727 #ifdef BUILD_X11
728   if (out_to_x.get(*state)) { new_bar_in_x11(obj, p, usage); }
729   if (out_to_stdout.get(*state)) {
730     new_bar_in_shell(obj, p, p_max_size, usage);
731   }
732 #else  /* BUILD_X11 */
733   new_bar_in_shell(obj, p, p_max_size, usage);
734 #endif /* BUILD_X11 */
735 }
736 
new_outline(struct text_object * obj,char * p,unsigned int p_max_size)737 void new_outline(struct text_object *obj, char *p, unsigned int p_max_size) {
738   if (p_max_size == 0) { return; }
739   new_special(p, OUTLINE)->arg = obj->data.l;
740 }
741 
new_offset(struct text_object * obj,char * p,unsigned int p_max_size)742 void new_offset(struct text_object *obj, char *p, unsigned int p_max_size) {
743   if (p_max_size == 0) { return; }
744   new_special(p, OFFSET)->arg = xft_dpi_scale(obj->data.l);
745 }
746 
new_voffset(struct text_object * obj,char * p,unsigned int p_max_size)747 void new_voffset(struct text_object *obj, char *p, unsigned int p_max_size) {
748   if (p_max_size == 0) { return; }
749   new_special(p, VOFFSET)->arg = xft_dpi_scale(obj->data.l);
750 }
751 
new_save_coordinates(struct text_object * obj,char * p,unsigned int p_max_size)752 void new_save_coordinates(struct text_object *obj, char *p,
753                           unsigned int p_max_size) {
754   if (p_max_size == 0) { return; }
755   new_special(p, SAVE_COORDINATES)->arg = obj->data.l;
756 }
757 
new_alignr(struct text_object * obj,char * p,unsigned int p_max_size)758 void new_alignr(struct text_object *obj, char *p, unsigned int p_max_size) {
759   if (p_max_size == 0) { return; }
760   new_special(p, ALIGNR)->arg = xft_dpi_scale(obj->data.l);
761 }
762 
763 // A positive offset pushes the text further left
new_alignc(struct text_object * obj,char * p,unsigned int p_max_size)764 void new_alignc(struct text_object *obj, char *p, unsigned int p_max_size) {
765   if (p_max_size == 0) { return; }
766   new_special(p, ALIGNC)->arg = xft_dpi_scale(obj->data.l);
767 }
768 
new_goto(struct text_object * obj,char * p,unsigned int p_max_size)769 void new_goto(struct text_object *obj, char *p, unsigned int p_max_size) {
770   if (p_max_size == 0) { return; }
771   new_special(p, GOTO)->arg = xft_dpi_scale(obj->data.l);
772 }
773 
scan_tab(struct text_object * obj,const char * arg)774 void scan_tab(struct text_object *obj, const char *arg) {
775   struct tab *t;
776 
777   t = static_cast<struct tab *>(malloc(sizeof(struct tab)));
778   memset(t, 0, sizeof(struct tab));
779 
780   t->width = 10;
781   t->arg = 0;
782 
783   if (arg != nullptr) {
784     if (sscanf(arg, "%d %d", &t->width, &t->arg) != 2) {
785       sscanf(arg, "%d", &t->arg);
786     }
787   }
788   if (t->width <= 0) { t->width = 1; }
789   obj->special_data = t;
790 }
791 
new_tab(struct text_object * obj,char * p,unsigned int p_max_size)792 void new_tab(struct text_object *obj, char *p, unsigned int p_max_size) {
793   struct special_t *s = nullptr;
794   auto *t = static_cast<struct tab *>(obj->special_data);
795 
796   if ((t == nullptr) || (p_max_size == 0)) { return; }
797 
798   s = new_special(p, TAB);
799   s->width = xft_dpi_scale(t->width);
800   s->arg = xft_dpi_scale(t->arg);
801 }
802 
clear_stored_graphs()803 void clear_stored_graphs() {
804   graph_count = 0;
805   graphs.clear();
806 }
807