Bulid fix for nvidia support.
[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 "x11.h"
36 #include <NVCtrl/NVCtrlLib.h>
37
38 const int nvidia_query_to_attr[] = {NV_CTRL_GPU_CORE_TEMPERATURE,
39                                     NV_CTRL_GPU_CORE_THRESHOLD,
40                                     NV_CTRL_AMBIENT_TEMPERATURE,
41                                     NV_CTRL_GPU_CURRENT_CLOCK_FREQS,
42                                     NV_CTRL_GPU_CURRENT_CLOCK_FREQS,
43                                     NV_CTRL_IMAGE_SETTINGS};
44
45 typedef enum _QUERY_ID {
46         NV_TEMP,
47         NV_TEMP_THRESHOLD,
48         NV_TEMP_AMBIENT,
49         NV_GPU_FREQ,
50         NV_MEM_FREQ,
51         NV_IMAGE_QUALITY
52 } QUERY_ID;
53
54 struct nvidia_s {
55         int interval;
56         int print_as_float;
57         QUERY_ID type;
58 };
59
60 static Display *nvdisplay;
61
62 static int get_nvidia_value(QUERY_ID qid){
63         int tmp;
64         Display *dpy = nvdisplay ? nvdisplay : display;
65         if(!dpy || !XNVCTRLQueryAttribute(dpy, 0, 0, nvidia_query_to_attr[qid], &tmp)){
66                 return -1;
67         }
68         /* FIXME: when are the low 2 bytes of NV_GPU_FREQ needed? */
69         if (qid == NV_GPU_FREQ)
70                 return tmp >> 16;
71         if (qid == NV_MEM_FREQ)
72                 return tmp & 0xFFFF;
73         return tmp;
74 }
75
76 int set_nvidia_type(struct text_object *obj, const char *arg)
77 {
78         struct nvidia_s *nvs;
79
80         nvs = obj->data.opaque = malloc(sizeof(struct nvidia_s));
81         memset(nvs, 0, sizeof(struct nvidia_s));
82
83         switch(arg[0]) {
84                 case 't':                              // temp or threshold
85                         nvs->print_as_float = 1;
86                         if (arg[1] == 'e')
87                                 nvs->type = NV_TEMP;
88                         else if (arg[1] == 'h')
89                                 nvs->type = NV_TEMP_THRESHOLD;
90                         else
91                                 return 1;
92                         break;
93                 case 'a':                              // ambient temp
94                         nvs->print_as_float = 1;
95                         nvs->type = NV_TEMP_AMBIENT;
96                         break;
97                 case 'g':                              // gpufreq
98                         nvs->type = NV_GPU_FREQ;
99                         break;
100                 case 'm':                              // memfreq
101                         nvs->type = NV_MEM_FREQ;
102                         break;
103                 case 'i':                              // imagequality
104                         nvs->type = NV_IMAGE_QUALITY;
105                         break;
106                 default:
107                         return 1;
108         }
109         return 0;
110 }
111
112 void print_nvidia_value(struct text_object *obj, char *p, int p_max_size)
113 {
114         int value;
115         struct nvidia_s *nvs = obj->data.opaque;
116
117         if (!nvs ||
118             (value = get_nvidia_value(nvs->type)) == -1) {
119                 snprintf(p, p_max_size, "N/A");
120                 return;
121         }
122         if (nvs->type == NV_TEMP)
123                 temp_print(p, p_max_size, (double)value, TEMP_CELSIUS);
124         else if (nvs->print_as_float &&
125                         value > 0 && value < 100)
126                 snprintf(p, p_max_size, "%.1f", (float)value);
127         else
128                 snprintf(p, p_max_size, "%d", value);
129 }
130
131 void free_nvidia(struct text_object *obj)
132 {
133         if (obj->data.opaque) {
134                 free(obj->data.opaque);
135                 obj->data.opaque = NULL;
136         }
137 }
138
139 void set_nvidia_display(const char *disp)
140 {
141         if(nvdisplay) {
142                 XCloseDisplay(nvdisplay);
143                 nvdisplay = NULL;
144         }
145         if(disp) {
146                 if ((nvdisplay = XOpenDisplay(disp)) == NULL) {
147                         CRIT_ERR(NULL, NULL, "can't open nvidia display: %s", XDisplayName(disp));
148                 }
149         }
150 }
151