Fix removing the config and sending a SIGUSR1 results in segfault
[monky] / src / nvidia.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2008 Markus Meissner
13  * Copyright (c) 2005-2010 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 #include "conky.h"
32 #include "logging.h"
33 #include "nvidia.h"
34 #include "temphelper.h"
35 #include <NVCtrl/NVCtrlLib.h>
36
37 const int nvidia_query_to_attr[] = {NV_CTRL_GPU_CORE_TEMPERATURE,
38                                     NV_CTRL_GPU_CORE_THRESHOLD,
39                                     NV_CTRL_AMBIENT_TEMPERATURE,
40                                     NV_CTRL_GPU_CURRENT_CLOCK_FREQS,
41                                     NV_CTRL_GPU_CURRENT_CLOCK_FREQS,
42                                     NV_CTRL_IMAGE_SETTINGS};
43
44 typedef enum _QUERY_ID {
45         NV_TEMP,
46         NV_TEMP_THRESHOLD,
47         NV_TEMP_AMBIENT,
48         NV_GPU_FREQ,
49         NV_MEM_FREQ,
50         NV_IMAGE_QUALITY
51 } QUERY_ID;
52
53 struct nvidia_s {
54         int interval;
55         int print_as_float;
56         QUERY_ID type;
57 };
58
59 static int get_nvidia_value(QUERY_ID qid, Display *dpy){
60         int tmp;
61         if(!XNVCTRLQueryAttribute(dpy, 0, 0, nvidia_query_to_attr[qid], &tmp)){
62                 return -1;
63         }
64         /* FIXME: when are the low 2 bytes of NV_GPU_FREQ needed? */
65         if (qid == NV_GPU_FREQ)
66                 return tmp >> 16;
67         if (qid == NV_MEM_FREQ)
68                 return tmp & 0xFFFF;
69         return tmp;
70 }
71
72 int set_nvidia_type(struct text_object *obj, const char *arg)
73 {
74         struct nvidia_s *nvs;
75
76         nvs = obj->data.opaque = malloc(sizeof(struct nvidia_s));
77         memset(nvs, 0, sizeof(struct nvidia_s));
78
79         switch(arg[0]) {
80                 case 't':                              // temp or threshold
81                         nvs->print_as_float = 1;
82                         if (arg[1] == 'e')
83                                 nvs->type = NV_TEMP;
84                         else if (arg[1] == 'h')
85                                 nvs->type = NV_TEMP_THRESHOLD;
86                         else
87                                 return 1;
88                         break;
89                 case 'a':                              // ambient temp
90                         nvs->print_as_float = 1;
91                         nvs->type = NV_TEMP_AMBIENT;
92                         break;
93                 case 'g':                              // gpufreq
94                         nvs->type = NV_GPU_FREQ;
95                         break;
96                 case 'm':                              // memfreq
97                         nvs->type = NV_MEM_FREQ;
98                         break;
99                 case 'i':                              // imagequality
100                         nvs->type = NV_IMAGE_QUALITY;
101                         break;
102                 default:
103                         return 1;
104         }
105         return 0;
106 }
107
108 void print_nvidia_value(struct text_object *obj, Display *dpy, char *p, int p_max_size)
109 {
110         int value;
111         struct nvidia_s *nvs = obj->data.opaque;
112
113         if (!nvs ||
114             (value = get_nvidia_value(nvs->type, dpy)) == -1) {
115                 snprintf(p, p_max_size, "N/A");
116                 return;
117         }
118         if (nvs->type == NV_TEMP)
119                 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
120         else if (nvs->print_as_float &&
121                         value > 0 && value < 100)
122                 snprintf(p, p_max_size, "%.1f", (float)value);
123         else
124                 snprintf(p, p_max_size, "%d", value);
125 }
126
127 void free_nvidia(struct text_object *obj)
128 {
129         if (obj->data.opaque) {
130                 free(obj->data.opaque);
131                 obj->data.opaque = NULL;
132         }
133 }
134