Make AddMouseRegion's index unsigned
[dockapps.git] / wmgtemp / src / wmgtemp.c
blob379917c7a7e13f319fc4235d066295ae0c981f71
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <fcntl.h>
5 #include <unistd.h>
6 #include <sys/param.h>
7 #include <sys/types.h>
8 #include <sys/poll.h>
9 #include <time.h>
10 #include <X11/Xlib.h>
11 #include <X11/xpm.h>
12 #include <X11/extensions/shape.h>
13 #include <sensors/sensors.h>
14 #include <sensors/error.h>
15 #include <libdockapp/wmgeneral.h>
16 #include <libdockapp/misc.h>
17 #include "wmgtemp-interface.xpm"
18 #include "wmgtemp-interface-mask.xbm"
19 #include <math.h>
20 #include <signal.h>
21 #include <getopt.h>
23 /* Defines */
24 #define BitOff(a,x) ((void)((a) &= ~(1 << (x))))
25 #define BitOn(a,x) ((void)((a) |= (1 << (x))))
26 #define BitFlip(a,x) ((void)((a) ^= (1 << (x))))
27 #define IsOn(a,x) ((a) & (1 << (x)))
29 // Display flags.
30 #define CPU 0
31 #define SYS 1
32 #define WARN_NONE 2
33 #define WARN_WARN 3
34 #define WARN_HIGH 4
35 #define TSCALE_CELCIUS 5
36 #define TSCALE_FAHRENHEIT 6
37 #define TSCALE_KELVIN 7
38 #define GRAPH_LINE 8
39 #define GRAPH_BLOCK 9
40 #define HIGH_CPU 10
41 #define HIGH_SYS 11
43 #define D_MIN 0
44 #define D_MAX 1
46 #define CPU_YPOS 3
47 #define SYS_YPOS 53
49 #define BLOCK 0
50 #define LINE 1
52 #define DEBUG 0 /* 0 disable 1 enable */
54 #define OPT_STRING "g:sS:hH:w:m:M:a:e:u:1:2:c:tq"
56 #define TEMPTOFAHRENHEIT(t) ((int)((t * (1.8) + 32)))
57 #define TEMPTOKELVIN(t) ((int)(t + 273))
58 #define TEMPTOCELCIUS(t) (t)
59 #define TEMPTODISPLAYSCALE(temp, display_flags) (IsOn((display_flags), TSCALE_CELCIUS) ? TEMPTOCELCIUS((temp)) : (IsOn((display_flags), TSCALE_KELVIN) ? TEMPTOKELVIN((temp)) : TEMPTOFAHRENHEIT((temp))))
61 /* Prototypes */
62 int init_sensors();
63 int process_config(int argc, char **argv);
64 int recompute_range(double cpu_high, double cpu_low, double sys_high, double sys_low);
65 void display_usage();
66 void process_xevents();
67 void draw_scale_indicator();
68 void add_to_graph(double temp, int type, short blank, double range, int pos);
69 void draw_range_line(double temp, double range, short type);
70 void update_display();
71 void update_sensor_data();
72 void do_sensors(int val);
73 double highest_temp(double *temp_array);
74 double lowest_temp(double *temp_array);
75 void draw_temp(short value, int type);
76 void draw_warning_lights(double current_temp);
77 void draw_max(int type);
78 void blank_max(int type);
79 void draw_type(int type);
80 void blank_type(int type);
81 void cycle_temptype();
83 /* Globals */
84 int delay = 1;
85 const sensors_chip_name *name;
86 char *exec_app = NULL;
87 char *rc_config = NULL;
89 short SENSOR_DISP = 0;
90 int SUBFEAT_NUM_CPU = 0;
91 int SUBFEAT_NUM_SYS = 0;
93 double cpu_history[59];
94 double sys_history[59];
96 double display_min = 20;
97 double display_max = 35;
99 double range_upper = 35;
100 double range_lower = 20;
101 double range_step = 5.0;
103 double warn_temp = 45;
104 double high_temp = 50;
106 double run_cpu_high = 0;
107 double run_sys_high = 0;
109 double execat = 0;
110 short execed = 0;
111 short swap_types = 0;
113 char *sensor_feature1 = "temp1";
114 char *sensor_feature2 = "temp2";
115 char *sensor_chip = NULL;
117 short quiet = 0;
119 int main(int argc, char **argv) {
120 int chip_nr = 0;
121 int i = 0;
122 int tmp_swap;
124 const sensors_feature* feature = NULL;
125 const sensors_subfeature* subfeature_cpu = NULL;
126 const sensors_subfeature* subfeature_sys = NULL;
127 short chip_found = -1;
129 BitOn(SENSOR_DISP, WARN_NONE);
130 BitOn(SENSOR_DISP, TSCALE_CELCIUS);
131 BitOn(SENSOR_DISP, GRAPH_LINE);
133 /* *conffname = "/etc/sensors3.conf"; */
135 if(!process_config(argc, argv)) {
136 exit(-1);
139 if(!init_sensors()) {
140 exit(-1);
143 /* Get the chip name */
144 name = sensors_get_detected_chips(NULL, &chip_nr);
145 while(name != NULL && chip_found == -1) {
146 if (!sensor_chip || strcmp(name->prefix, (const char *)sensor_chip) == 0) {
147 i = 0;
148 while ((feature = sensors_get_features(name, &i))) {
149 if(strcmp(feature->name, (const char *)sensor_feature1) == 0) {
150 subfeature_cpu = sensors_get_subfeature(name, feature, SENSORS_SUBFEATURE_TEMP_INPUT);
151 SUBFEAT_NUM_CPU = subfeature_cpu->number;
152 BitOn(SENSOR_DISP, CPU);
153 chip_found = 1;
155 if(strcmp(feature->name, (const char *)sensor_feature2) == 0) {
156 subfeature_sys = sensors_get_subfeature(name, feature, SENSORS_SUBFEATURE_TEMP_INPUT);
157 SUBFEAT_NUM_SYS = subfeature_sys->number;
158 BitOn(SENSOR_DISP, SYS);
159 chip_found = 1;
164 if(chip_found == -1) {
165 name = sensors_get_detected_chips(NULL, &chip_nr);
168 if(chip_found == -1) {
169 fprintf(stderr,"wmgtemp: Unable to find temperature sensing feature.\n");
170 exit(0);
173 /* output the name of the sensor if found. */
174 if(quiet == 0)
175 printf("wmgtemp: Primary Sensor - %s on %s\n", name->prefix, sensors_get_adapter_name(&name->bus));
177 if(swap_types) {
178 if(quiet == 0)
179 printf("wmgtemp: swapping temps\n");
180 tmp_swap = SUBFEAT_NUM_SYS;
181 SUBFEAT_NUM_SYS = SUBFEAT_NUM_CPU;
182 SUBFEAT_NUM_CPU = tmp_swap;
183 tmp_swap = SENSOR_DISP;
184 if(IsOn(tmp_swap, CPU)) {
185 BitOn(SENSOR_DISP, SYS);
186 } else {
187 BitOff(SENSOR_DISP, SYS);
189 if(IsOn(tmp_swap, SYS)) {
190 BitOn(SENSOR_DISP, CPU);
191 } else {
192 BitOff(SENSOR_DISP, CPU);
196 chip_nr = 0;
198 openXwindow(argc, argv, wmgtemp_interface_xpm, wmgtemp_interface_mask_bits,
199 wmgtemp_interface_mask_width, wmgtemp_interface_mask_height);
201 AddMouseRegion(0, 2, 12, 61, 51); /* Graph area */
202 AddMouseRegion(1, 34, 2, 51, 11); /* CPU temp area */
203 AddMouseRegion(2, 34, 52, 51, 61); /* SYS temp area */
204 AddMouseRegion(3, 10, CPU_YPOS, 28, CPU_YPOS + 7); /* CPU label area */
205 AddMouseRegion(4, 10, SYS_YPOS, 28, SYS_YPOS + 7); /* SYS label area */
206 AddMouseRegion(5, 55, CPU_YPOS, 60, CPU_YPOS + 7); /* CPU C/K/F scale indicator */
207 AddMouseRegion(6, 55, SYS_YPOS, 60, SYS_YPOS + 7); /* SYS C/K/F scale indicator */
209 // Add blanking of SYS and CPU for chip type.
210 // <<==---
211 if(!IsOn(SENSOR_DISP, CPU)) {
212 blank_type(CPU);
214 if(!IsOn(SENSOR_DISP, SYS)) {
215 blank_type(SYS);
218 draw_scale_indicator();
220 // Initialise the temperature arrays.
221 for(i = 0; i < 59; i++) {
222 cpu_history[i] = -1;
223 sys_history[i] = -1;
226 do_sensors(0);
227 RedrawWindow();
229 process_xevents();
231 return 0;
234 void draw_scale_indicator() {
235 if(IsOn(SENSOR_DISP, TSCALE_CELCIUS)) {
236 if(IsOn(SENSOR_DISP, CPU)) {
237 copyXPMArea(61, 65, 5, 7, 55, CPU_YPOS);
238 copyXPMArea(70, 2, 2, 2, 52, CPU_YPOS);
240 if(IsOn(SENSOR_DISP, SYS)) {
241 copyXPMArea(61, 65, 5, 7, 55, SYS_YPOS);
242 copyXPMArea(70, 2, 2, 2, 52, SYS_YPOS);
245 else if(IsOn(SENSOR_DISP, TSCALE_FAHRENHEIT)) {
246 if(IsOn(SENSOR_DISP, CPU)) {
247 copyXPMArea(67, 65, 5, 7, 55, CPU_YPOS);
248 copyXPMArea(70, 2, 2, 2, 52, CPU_YPOS);
250 if(IsOn(SENSOR_DISP, SYS)) {
251 copyXPMArea(67, 65, 5, 7, 55, SYS_YPOS);
252 copyXPMArea(70, 2, 2, 2, 52, SYS_YPOS);
255 else if(IsOn(SENSOR_DISP, TSCALE_KELVIN)) {
256 if(IsOn(SENSOR_DISP, CPU)) {
257 copyXPMArea(73, 65, 5, 7, 55, CPU_YPOS);
258 copyXPMArea(70, 0, 2, 2, 52, CPU_YPOS);
260 if(IsOn(SENSOR_DISP, SYS)) {
261 copyXPMArea(73, 65, 5, 7, 55, SYS_YPOS);
262 copyXPMArea(70, 0, 2, 2, 52, SYS_YPOS);
267 void process_xevents() {
268 int button_area = 0;
269 int* xfds = NULL;
270 int fdcount = 0;
271 struct pollfd* pfds = NULL;
272 XEvent Event;
273 Status ret;
274 time_t lastupdate = 0;
276 ret = XInternalConnectionNumbers(display, &xfds, &fdcount);
277 if(!ret) {
278 fdcount = 0;
279 if(xfds) {
280 XFree(xfds);
282 xfds = NULL;
285 int i;
286 pfds = (struct pollfd*)malloc((fdcount+1)*sizeof(struct pollfd));
287 if(!pfds) {
288 perror("malloc");
289 exit(EXIT_FAILURE);
292 for(i=0; i < fdcount; ++i) {
293 pfds[i].fd = xfds[i];
294 pfds[i].events = POLLIN | POLLPRI;
297 if(xfds) {
298 XFree(xfds);
301 pfds[fdcount].fd = ConnectionNumber(display);
302 pfds[fdcount].events = POLLIN | POLLPRI;
304 while(1) {
305 poll(pfds, fdcount + 1, delay * 1000);
307 if(time(NULL) - lastupdate >= delay) {
308 lastupdate = time(NULL);
309 do_sensors(0);
312 while(XPending(display)) {
313 XNextEvent(display, &Event);
314 switch(Event.type) {
315 case Expose:
316 RedrawWindow();
317 break;
318 case DestroyNotify:
319 XCloseDisplay(display);
320 exit(0);
321 break;
322 case ButtonRelease:
323 button_area = CheckMouseRegion(Event.xbutton.x, Event.xbutton.y);
324 switch(button_area) {
325 case 0:
326 if(IsOn(SENSOR_DISP, GRAPH_LINE)) {
327 BitOff(SENSOR_DISP, GRAPH_LINE);
328 BitOn(SENSOR_DISP, GRAPH_BLOCK);
330 else if(IsOn(SENSOR_DISP, GRAPH_BLOCK)) {
331 BitOff(SENSOR_DISP, GRAPH_BLOCK);
332 BitOn(SENSOR_DISP, GRAPH_LINE);
334 update_display();
335 RedrawWindow();
336 break;
337 case 1:
338 if(IsOn(SENSOR_DISP, HIGH_CPU)) {
339 BitOff(SENSOR_DISP, HIGH_CPU);
340 blank_max(CPU);
342 else {
343 BitOn(SENSOR_DISP, HIGH_CPU);
344 draw_max(CPU);
346 update_display();
347 RedrawWindow();
348 break;
349 case 2:
350 if(IsOn(SENSOR_DISP, HIGH_SYS)) {
351 BitOff(SENSOR_DISP, HIGH_SYS);
352 blank_max(SYS);
354 else {
355 BitOn(SENSOR_DISP, HIGH_SYS);
356 draw_max(SYS);
358 update_display();
359 RedrawWindow();
360 break;
361 case 3:
362 if(SUBFEAT_NUM_CPU) {
363 if(IsOn(SENSOR_DISP, CPU)) {
364 BitOff(SENSOR_DISP, CPU);
365 blank_type(CPU);
367 else {
368 BitOn(SENSOR_DISP, CPU);
369 draw_type(CPU);
370 draw_scale_indicator();
373 update_display();
374 RedrawWindow();
375 break;
376 case 4:
377 if(SUBFEAT_NUM_SYS) {
378 if(IsOn(SENSOR_DISP, SYS)) {
379 BitOff(SENSOR_DISP, SYS);
380 blank_type(SYS);
382 else {
383 BitOn(SENSOR_DISP, SYS);
384 draw_type(SYS);
385 draw_scale_indicator();
388 update_display();
389 RedrawWindow();
390 break;
391 case 5:
392 case 6:
393 cycle_temptype();
394 draw_scale_indicator();
395 update_display();
396 RedrawWindow();
397 break;
399 break;
405 void do_sensors(int val) {
407 update_sensor_data();
408 update_display();
409 RedrawWindow();
411 if(execat != 0 && cpu_history[58] >= execat && !execed) {
412 execed = 1;
413 execCommand(exec_app);
415 if(execat != 0 && cpu_history[58] < execat && execed) {
416 execed = 0;
421 void update_sensor_data() {
422 int i = 0;
423 double cpu_high = highest_temp(cpu_history);
424 double sys_high = highest_temp(sys_history);
426 /* Shift the arrays */
427 for(i = 0; i < 58; i++) {
428 cpu_history[i] = cpu_history[i + 1];
429 sys_history[i] = sys_history[i + 1];
432 // Read the new values from the sensors into the temperature arrays.
433 if(IsOn(SENSOR_DISP, SYS)) sensors_get_value(name, SUBFEAT_NUM_SYS, &sys_history[58]);
434 if(IsOn(SENSOR_DISP, CPU)) sensors_get_value(name, SUBFEAT_NUM_CPU, &cpu_history[58]);
436 // Update the run high/low values.
437 if(cpu_high > run_cpu_high)
438 run_cpu_high = cpu_high;
439 if(sys_high > run_sys_high)
440 run_sys_high = sys_high;
444 void update_display() {
445 int j = 0;
448 // Rescale the display if needed.
449 while(recompute_range(highest_temp(cpu_history), lowest_temp(cpu_history),
450 highest_temp(sys_history), lowest_temp(sys_history)));
452 // Display warning.
453 draw_warning_lights(cpu_history[58]);
455 // ReDraw temperature numbers
456 if(IsOn(SENSOR_DISP, CPU)) {
457 copyXPMArea(78, 65, 5, 7, 34, CPU_YPOS);
458 copyXPMArea(78, 65, 5, 7, 40, CPU_YPOS);
459 copyXPMArea(78, 65, 5, 7, 46, CPU_YPOS);
460 draw_temp(TEMPTODISPLAYSCALE(IsOn(SENSOR_DISP, HIGH_CPU) == 0 ? cpu_history[58] : run_cpu_high, SENSOR_DISP), CPU);
462 if(IsOn(SENSOR_DISP, SYS)) {
463 copyXPMArea(78, 65, 5, 7, 34, SYS_YPOS);
464 copyXPMArea(78, 65, 5, 7, 40, SYS_YPOS);
465 copyXPMArea(78, 65, 5, 7, 46, SYS_YPOS);
466 draw_temp(TEMPTODISPLAYSCALE(IsOn(SENSOR_DISP, HIGH_SYS) == 0 ? sys_history[58] : run_sys_high, SENSOR_DISP), SYS);
470 // ReDraw the graph
471 for(j = 0; j < 59; j++) {
472 // Clear a line
473 copyXPMArea(65, 0, 1, 39, j + 2, 12);
475 if(sys_history[j] < cpu_history[j]) {
476 // Draw the temperatures on the graph.
477 if(IsOn(SENSOR_DISP, CPU)) {
478 add_to_graph(cpu_history[j], CPU, 1, range_upper - range_lower, j + 2);
480 if(IsOn(SENSOR_DISP, SYS)) {
481 add_to_graph(sys_history[j], SYS, 0, range_upper - range_lower, j + 2);
484 else {
485 if(IsOn(SENSOR_DISP, SYS)) {
486 add_to_graph(sys_history[j], SYS, 0, range_upper - range_lower, j + 2);
488 if(IsOn(SENSOR_DISP, CPU)) {
489 add_to_graph(cpu_history[j], CPU, 1, range_upper - range_lower, j + 2);
494 // Draw range lines if needed
495 if(range_upper > display_max) {
496 draw_range_line(display_max, range_upper - range_lower, D_MAX);
498 if(range_lower < display_min) {
499 draw_range_line(display_min, range_upper - range_lower, D_MIN);
503 int recompute_range(double cpu_high, double cpu_low, double sys_high, double sys_low)
505 short modified = 0;
507 if(IsOn(SENSOR_DISP, CPU)) {
508 if(cpu_high > range_upper) {
509 range_upper += range_step;
510 modified = 1;
512 if(cpu_low < range_lower) {
513 range_lower -= range_step;
514 modified = 1;
517 if(IsOn(SENSOR_DISP, SYS)) {
518 if(sys_high > range_upper) {
519 range_upper += range_step;
520 modified = 1;
522 if(sys_low < range_lower) {
523 range_lower -= range_step;
524 modified = 1;
528 // --------
529 if(IsOn(SENSOR_DISP, CPU) && IsOn(SENSOR_DISP, SYS)) {
530 if((cpu_high < (range_upper - range_step) &&
531 sys_high < (range_upper - range_step)) &&
532 (range_upper - range_step) >= display_max) {
533 range_upper -= range_step;
534 modified = 1;
536 if((cpu_low > (range_lower + range_step) &&
537 sys_low > (range_lower + range_step)) &&
538 (range_lower + range_step) <= display_min ) {
539 range_lower += range_step;
540 modified = 1;
543 else if(IsOn(SENSOR_DISP, CPU) && !IsOn(SENSOR_DISP, SYS)) {
544 if(cpu_high < (range_upper - range_step) &&
545 (range_upper - range_step) >= display_max) {
546 range_upper -= range_step;
547 modified = 1;
549 if(cpu_low > (range_lower + range_step) &&
550 (range_lower + range_step) <= display_min) {
551 range_lower += range_step;
552 modified = 1;
555 else if(!IsOn(SENSOR_DISP, CPU) && IsOn(SENSOR_DISP, SYS)) {
556 if(sys_high < (range_upper - range_step) &&
557 (range_upper - range_step) >= display_max) {
558 range_upper -= range_step;
559 modified = 1;
561 if(sys_low > (range_lower + range_step) &&
562 (range_lower + range_step) <= display_min) {
563 range_lower += range_step;
564 modified = 1;
568 return modified;
571 double highest_temp(double *temp_array) {
572 int i = 0;
573 double high = 0;
574 for(i = 0; i < 59; i++) {
575 if(temp_array[i] > high)
576 high = temp_array[i];
578 return high;
581 double lowest_temp(double *temp_array) {
582 int i = 0;
583 double low = 500;
584 for(i = 0; i < 59; i++) {
585 if((temp_array[i] < low) && (temp_array[i] != -1))
586 low = temp_array[i];
588 return low;
591 void add_to_graph(double temp, int type, short blank, double range, int pos) {
592 double each = (double)39 / range;
593 short length = each * (temp - range_lower);
595 // Draw the graphs
596 // if(IsOn(SENSOR_DISP, GRAPH_BLOCK)) {
597 // copyXPMArea(type == CPU ? 67 : 68, 0, 1, length, pos, 51 - length);
598 // }
599 // else if(IsOn(SENSOR_DISP, GRAPH_LINE)) {
600 // copyXPMArea(type == CPU ? 67 : 68, 0, 1, 1, pos, 51 - length);
601 // }
603 // Do not draw the graphs if the temperature data does not make sense
604 // Orginially used the code above but change supplied to fix issues seen by Ben Spencer.
605 // Couldn't be arsed to find the real cause as I don't use the app myself anymore.
606 // Doesn't seem to break anything though
607 if ((temp >= range_lower) && (temp <= range_upper)) {
608 // Draw the graphs
609 if(IsOn(SENSOR_DISP, GRAPH_BLOCK)) {
610 copyXPMArea(type == CPU ? 67 : 68, 0, 1, length, pos, 51 - length);
612 else if(IsOn(SENSOR_DISP, GRAPH_LINE)) {
613 copyXPMArea(type == CPU ? 67 : 68, 0, 1, 1, pos, 51 - length);
619 void draw_temp(short value, int type) {
620 short digit;
622 if(value > 0) {
623 digit = value % 10;
624 copyXPMArea((digit * 6) + 1, 65, 5, 7, 46, type == CPU ? CPU_YPOS : SYS_YPOS);
625 if(value > 9) {
626 digit = ((value % 100) - digit) / 10;
627 copyXPMArea((digit * 6) + 1, 65, 5, 7, 40, type == CPU ? CPU_YPOS : SYS_YPOS);
628 if(value > 99) {
629 digit = (value - (value % 100)) / 100;
630 copyXPMArea((digit * 6) + 1, 65, 5, 7, 34, type == CPU ? CPU_YPOS : SYS_YPOS);
637 void draw_clear_temps() {
638 if(IsOn(SENSOR_DISP, CPU)) {
639 copyXPMArea(78, 65, 5, 7, 34, CPU_YPOS);
640 copyXPMArea(78, 65, 5, 7, 40, CPU_YPOS);
641 copyXPMArea(78, 65, 5, 7, 46, CPU_YPOS);
643 if(IsOn(SENSOR_DISP, SYS)) {
644 copyXPMArea(78, 65, 5, 7, 34, SYS_YPOS);
645 copyXPMArea(78, 65, 5, 7, 40, SYS_YPOS);
646 copyXPMArea(78, 65, 5, 7, 46, SYS_YPOS);
650 void draw_range_line(double temp, double range, short type) {
651 double each = (double)39 / range;
652 short length = each * (temp - range_lower);
654 copyXPMArea(0, type == D_MAX ? 73 : 74, 59, 1, 2, 51 - length);
658 int init_sensors() {
659 FILE *config_file;
660 int res;
662 if (rc_config) {
663 config_file = fopen(rc_config, "r");
665 if(config_file == NULL) {
666 fprintf(stderr, "Error opening %s\n", rc_config);
667 return 0;
669 } else {
670 config_file = NULL; /* Use libsensors default */
673 res = sensors_init(config_file);
675 if(res != 0) {
676 fprintf(stderr,"Error initializing sensors: %s\n", sensors_strerror(res));
677 return 0;
680 if(config_file && fclose(config_file))
681 perror("Error closing sensors config");
683 return 1;
686 void display_usage() {
687 printf("wmgtemp v"PACKAGE_VERSION"\n" \
688 "Usage: wmgtemp [options]\n" \
689 "Options:\n" \
690 " -S, --sensorconf=PATH Specify sensors config file PATH\n" \
691 " [Default: autodetect]\n" \
692 " -s, --scale=SCALE Display temperatures in SCALE\n" \
693 " SCALE=kelvin, fahrenheit\n" \
694 " [Default: celcius]\n" \
695 " -g, --graph=STYLE Display graph as STYLE\n" \
696 " STYLE=line, block\n" \
697 " [Default: line]\n" \
698 " -H, --high=TEMP Display red warning light at TEMP degrees celcius\n" \
699 " [Default: 50]\n" \
700 " -w, --warn=TEMP Display amber warning light at TEMP degrees celcius\n" \
701 " [Default: 45]\n" \
702 " -u, --update=SEC Update the display every SEC seconds\n" \
703 " [Default: 1]\n" \
704 " -m, --min=TEMP Set lower bound of the graph to TEMP degrees celcius\n" \
705 " [Default: 20]\n" \
706 " -M, --max=TEMP Set upper bound of the graph to TEMP degrees celcius\n" \
707 " [Default: 35]\n" \
708 " -1, --feature1=F1 Set the feature for CPU\n" \
709 " [Default: temp1]\n" \
710 " -2, --feature2=F2 Set the feature for SYS\n" \
711 " [Default: temp2]\n" \
712 " -c, --chip=NAME Use sensor chip matching NAME\n" \
713 " [Default: use any]\n" \
714 " -a, --execat=TEMP Execute a command at TEMP degrees celcius\n" \
715 " -e, --exec=COMMAND Execute COMMAND when 'execat' temperature is reached\n" \
716 " -t, --swap Swap CPU and SYS temps\n" \
717 " -q, --quiet Don't display any messages\n" \
718 " -h, --help Displays this help screen\n");
721 void draw_warning_lights(double current_temp) {
722 if(current_temp >= warn_temp && IsOn(SENSOR_DISP, WARN_NONE)) {
723 // Switch from ok to warning.
724 BitOff(SENSOR_DISP, WARN_NONE);
725 BitOn(SENSOR_DISP, WARN_WARN);
726 copyXPMArea(10, 75, 5, 5, 4, 4);
728 if(current_temp < warn_temp && IsOn(SENSOR_DISP, WARN_WARN)) {
729 // Switch from warning to ok.
730 BitOff(SENSOR_DISP, WARN_WARN);
731 BitOn(SENSOR_DISP, WARN_NONE);
732 copyXPMArea(0, 75, 5, 5, 4, 4);
734 if(current_temp >= high_temp && IsOn(SENSOR_DISP, WARN_WARN)) {
735 // Switch from warning to high.
736 BitOff(SENSOR_DISP, WARN_WARN);
737 BitOn(SENSOR_DISP, WARN_HIGH);
738 copyXPMArea(15, 75, 5, 5, 4, 4);
740 if(current_temp < high_temp && IsOn(SENSOR_DISP, WARN_HIGH)) {
741 // Switch from high to warning.
742 BitOff(SENSOR_DISP, WARN_HIGH);
743 BitOn(SENSOR_DISP, WARN_WARN);
744 copyXPMArea(10, 75, 5, 5, 4, 4);
748 void blank_type(int type) {
749 switch(type) {
750 case CPU:
751 copyXPMArea(78, 65, 5, 7, 11, CPU_YPOS);
752 copyXPMArea(78, 65, 5, 7, 17, CPU_YPOS);
753 copyXPMArea(78, 65, 5, 7, 23, CPU_YPOS);
755 copyXPMArea(70, 0, 2, 2, 52, CPU_YPOS);
757 copyXPMArea(78, 65, 5, 7, 34, CPU_YPOS);
758 copyXPMArea(78, 65, 5, 7, 40, CPU_YPOS);
759 copyXPMArea(78, 65, 5, 7, 46, CPU_YPOS);
761 copyXPMArea(78, 65, 5, 7, 55, CPU_YPOS);
762 break;
763 case SYS:
764 copyXPMArea(78, 65, 5, 7, 11, SYS_YPOS);
765 copyXPMArea(78, 65, 5, 7, 17, SYS_YPOS);
766 copyXPMArea(78, 65, 5, 7, 23, SYS_YPOS);
768 copyXPMArea(70, 0, 2, 2, 52, SYS_YPOS);
770 copyXPMArea(78, 65, 5, 7, 34, SYS_YPOS);
771 copyXPMArea(78, 65, 5, 7, 40, SYS_YPOS);
772 copyXPMArea(78, 65, 5, 7, 46, SYS_YPOS);
774 copyXPMArea(78, 65, 5, 7, 55, SYS_YPOS);
775 break;
779 void draw_max(int type) {
780 // copyXPMArea(1, 81, 17, 7, 11, type == CPU ? CPU_YPOS : SYS_YPOS);
781 copyXPMArea(24, 75, 4, 3, 29, type == CPU ? CPU_YPOS : SYS_YPOS);
784 void blank_max(int type) {
785 // copyXPMArea(1, 81, 17, 7, 11, type == CPU ? CPU_YPOS : SYS_YPOS);
786 copyXPMArea(20, 75, 4, 3, 29, type == CPU ? CPU_YPOS : SYS_YPOS);
789 void draw_type(int type) {
790 switch(type) {
791 case CPU:
792 copyXPMArea(65, 40, 17, 7, 11, CPU_YPOS);
793 break;
794 case SYS:
795 copyXPMArea(65, 47, 17, 7, 11, SYS_YPOS);
796 break;
800 void cycle_temptype() {
801 if(IsOn(SENSOR_DISP, TSCALE_CELCIUS)) {
802 BitOff(SENSOR_DISP, TSCALE_CELCIUS);
803 BitOn(SENSOR_DISP, TSCALE_KELVIN);
805 else if(IsOn(SENSOR_DISP, TSCALE_KELVIN)) {
806 BitOff(SENSOR_DISP, TSCALE_KELVIN);
807 BitOn(SENSOR_DISP, TSCALE_FAHRENHEIT);
809 else if(IsOn(SENSOR_DISP, TSCALE_FAHRENHEIT)) {
810 BitOff(SENSOR_DISP, TSCALE_FAHRENHEIT);
811 BitOn(SENSOR_DISP, TSCALE_CELCIUS);
816 int process_config(int argc, char **argv) {
817 char *rc_graph = NULL;
818 char *rc_scale = NULL;
819 char *rc_high = NULL;
820 char *rc_warn = NULL;
821 char *rc_min = NULL;
822 char *rc_max = NULL;
823 char *rc_execat = NULL;
824 char *rc_exec = NULL;
825 char *rc_delay = NULL;
826 char *rc_swap = NULL;
827 char *rc_feature1 = NULL;
828 char *rc_feature2 = NULL;
829 char *rc_quiet = NULL;
830 char *rc_chip = NULL;
831 short parse_ok = 1;
832 int opt_index;
833 int opt;
834 char *p;
835 char temp[128];
837 rckeys wmgtemp_keys[] = {
838 { "graph", &rc_graph },
839 { "scale", &rc_scale },
840 { "high", &rc_high },
841 { "warn", &rc_warn },
842 { "min", &rc_min },
843 { "max", &rc_max },
844 { "execat", &rc_execat },
845 { "exec", &rc_exec },
846 { "update", &rc_delay },
847 { "swap", &rc_swap },
848 { "quiet", &rc_quiet },
849 { "feature1", &rc_feature1 },
850 { "feature2", &rc_feature2 },
851 { "chip", &rc_chip },
852 { "sensorconf", &rc_config },
853 { NULL, NULL }
856 static struct option long_options[] = {
857 {"graph", required_argument, 0, 'g'},
858 {"scale", required_argument, 0, 's'},
859 {"high", required_argument, 0, 'H'},
860 {"warn", required_argument, 0, 'w'},
861 {"min", required_argument, 0, 'm'},
862 {"max", required_argument, 0, 'M'},
863 {"execat", required_argument, 0, 'a'},
864 {"exec", required_argument, 0, 'e'},
865 {"update", required_argument, 0, 'u'},
866 {"feature1", required_argument, 0, '1'},
867 {"feature2", required_argument, 0, '2'},
868 {"chip", required_argument, 0, 'c'},
869 {"sensorconf", required_argument, 0, 'S'},
870 {"swap", no_argument, 0, 't'},
871 {"quiet", no_argument, 0, 'q'},
872 {"help", no_argument, 0, 'h'},
873 {0, 0, 0, 0}
876 p = getenv("HOME");
877 strcpy(temp, p);
878 strcat(temp, "/.wmgtemprc");
879 parse_rcfile(temp, wmgtemp_keys);
881 // Do getopt stuff.
882 while ((opt = getopt_long(argc, argv, OPT_STRING, long_options, &opt_index)) != -1) {
883 switch(opt) {
884 case 'g':
885 rc_graph = strdup(optarg);
886 break;
887 case 's':
888 rc_scale = strdup(optarg);
889 break;
890 case 'H':
891 rc_high = strdup(optarg);
892 break;
893 case 'w':
894 rc_warn = strdup(optarg);
895 break;
896 case 'm':
897 rc_min = strdup(optarg);
898 break;
899 case 'M':
900 rc_max = strdup(optarg);
901 break;
902 case 'a':
903 rc_execat = strdup(optarg);
904 break;
905 case 'e':
906 rc_exec = strdup(optarg);
907 break;
908 case 'u':
909 rc_delay = strdup(optarg);
910 break;
911 case '1':
912 rc_feature1 = strdup(optarg);
913 break;
914 case '2':
915 rc_feature2 = strdup(optarg);
916 break;
917 case 'c':
918 rc_chip = strdup(optarg);
919 break;
920 case 'S':
921 rc_config = strdup(optarg);
922 break;
923 case 'q':
924 rc_quiet = "y";
925 break;
926 case 't':
927 rc_swap = "y";
928 break;
929 case 'h':
930 display_usage();
931 exit(0);
932 default:
933 display_usage();
934 exit(-1);
938 if(rc_quiet != NULL) {
939 if(!strncmp(rc_quiet, "y", 1)) {
940 quiet = 1;
944 if(rc_feature1 != NULL) {
945 sensor_feature1 = strdup(rc_feature1);
947 if(rc_feature2 != NULL) {
948 sensor_feature2 = strdup(rc_feature2);
950 if(rc_chip != NULL) {
951 sensor_chip = strdup(rc_chip);
954 if(rc_graph != NULL) {
955 if(!strncmp(rc_graph, "l", 1)) {
956 BitOff(SENSOR_DISP, GRAPH_BLOCK);
957 BitOn(SENSOR_DISP, GRAPH_LINE);
959 else if(!strncmp(rc_graph, "b", 1)) {
960 BitOff(SENSOR_DISP, GRAPH_LINE);
961 BitOn(SENSOR_DISP, GRAPH_BLOCK);
963 else {
964 printf("Invalid graph type: %s\n", rc_graph);
965 parse_ok = 0;
968 if(rc_scale != NULL) {
969 if(!strncmp(rc_scale, "c", 1)) {
971 else if(!strncmp(rc_scale, "f", 1)) {
972 BitOff(SENSOR_DISP, TSCALE_KELVIN);
973 BitOff(SENSOR_DISP, TSCALE_CELCIUS);
974 BitOn(SENSOR_DISP, TSCALE_FAHRENHEIT);
976 else if(!strncmp(rc_scale, "k", 1)) {
977 BitOff(SENSOR_DISP, TSCALE_CELCIUS);
978 BitOff(SENSOR_DISP, TSCALE_FAHRENHEIT);
979 BitOn(SENSOR_DISP, TSCALE_KELVIN);
981 else {
982 printf("Invalid scale type: %s\n", rc_scale);
983 parse_ok = 0;
987 if(rc_high != NULL) {
988 high_temp = (double)atoi(rc_high);
989 if(!high_temp) {
990 printf("Invalid temperature\n");
991 parse_ok = 0;
993 else {
994 if(quiet == 0)
995 printf("wmgtemp: high temp set to %d degrees celcius.\n", (int)high_temp);
998 if(rc_warn != NULL) {
999 warn_temp = (double)atoi(rc_warn);
1000 if(!warn_temp) {
1001 printf("Invalid temperature\n");
1002 parse_ok = 0;
1004 else {
1005 if(quiet == 0)
1006 printf("wmgtemp: warning temp set to %d degrees celcius.\n", (int)warn_temp);
1009 if(rc_max != NULL) {
1010 display_max = range_upper = (double)atoi(rc_max);
1011 if(!range_upper) {
1012 printf("Invalid temperature\n");
1013 parse_ok = 0;
1015 else {
1016 if(quiet == 0)
1017 printf("wmgtemp: Upper range set to %d degrees celcius.\n", (int)range_upper);
1020 if(rc_min != NULL) {
1021 display_min = range_lower = (double)atoi(rc_min);
1022 if(!range_lower) {
1023 printf("Invalid temperature\n");
1024 parse_ok = 0;
1026 else {
1027 if(quiet == 0)
1028 printf("wmgtemp: Lower range set to %d degrees celcius.\n", (int)range_lower);
1031 if(rc_delay != NULL) {
1032 delay = atoi(rc_delay);
1033 if(!delay) {
1034 printf("Invalid delay\n");
1035 parse_ok = 0;
1037 else {
1038 if(quiet == 0)
1039 printf("wmgtemp: update delay set to %d seconds.\n", delay);
1042 if(rc_execat != NULL) {
1043 execat = (double)atoi(rc_execat);
1044 if(!execat) {
1045 printf("Invalid temperature\n");
1046 parse_ok = 0;
1048 else {
1049 if(rc_exec != NULL) {
1050 if(strcmp(rc_exec, "")) {
1051 exec_app = strdup(rc_exec);
1052 printf("wmgtemp: Executing \"%s\" at %d degrees celcius.\n", exec_app, (int)execat);
1054 else {
1055 printf("You must supply an command to execute\n");
1056 parse_ok = 0;
1059 else {
1060 printf("You must supply an command to execute\n");
1061 parse_ok = 0;
1065 if(rc_swap != NULL) {
1066 if(!strncmp(rc_swap, "y", 1)) {
1067 swap_types = 1;
1069 else if(!strncmp(rc_swap, "n", 1)) {
1070 swap_types = 0;
1072 else {
1073 printf("Supply 'y' or 'n' for swap temps\n");
1074 parse_ok = 0;
1078 return parse_ok;