summaryrefslogtreecommitdiff
path: root/plugins/aticonfig/aticonfig-plugin.c
blob: 0fedc05d2cb287dd27aaa465c0bed65c6639f5c6 (plain)
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
/*
 * Copyright (C) 2009 Jaap Versteegh <j.r.versteegh@gmail.com>
 * Copyright (C) 2005-2009 Alex Murray <murray.alex@gmail.com>
 *
 * 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 2 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif /* HAVE_CONFIG_H */

#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif /* HAVE_STDIO_H */

#ifdef HAVE_TIME_H
#include <time.h>
#endif /* HAVE_TIME_H */

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */

#include <glib.h>
#include <glib/gi18n.h>
#include "aticonfig-plugin.h"

const gchar *plugin_name = "aticonfig";

#define GPU_CORE_TEMP "CoreTemp"
#define MAX_GPUS 4
#define SENSOR_ID_PREFIX "ATIGPU"

static gdouble gpu_temps[MAX_GPUS];
static int num_gpus = 0;

static int ati_get_temps(gdouble temps[], int max_temps)
{
    double temp;
    int read_count;
    int gpu_no = 0;

#ifdef HAVE_STDIO_H
    FILE *aticonfig = popen(ATICONFIG_EXE
      " --adapter=all --od-gettemperature", "r");
    if (aticonfig == NULL) {
        return 0;
    }
    while ((read_count = fscanf(aticonfig, "Temperature - %lf", &temp)) != EOF) {
        if (read_count < 1) {
            getc(aticonfig);
        }
        else {
            temps[gpu_no] = (gdouble)temp;
            if (++gpu_no >= max_temps)
                break;
        }
    }
    pclose(aticonfig);
#endif

    return gpu_no;
}

static void ati_update_temps(void)
{
#ifdef HAVE_TIME_H
    static time_t last = 0;
    time_t now = time(NULL);
    /* Only update when more than two seconds have passed since last update */
    if (timediff(now, last) > 2) {
#endif
        num_gpus = ati_get_temps(&gpu_temps, MAX_GPUS);
#ifdef HAVE_TIME_H
        last = now;
    }
#endif
}


static GList *aticonfig_plugin_init(void)
{
    GList *sensors = NULL;

    g_debug("Initializing aticonfig plugin\n");

    int sensor_count = ati_get_temps(&gpu_temps, MAX_GPUS);

    int i;
    for (i = 0; i < sensor_count; i++) {
        gchar *id = g_strdup_printf("%s%d%s", SENSOR_ID_PREFIX, i, GPU_CORE_TEMP);
        sensors_applet_plugin_add_sensor(&sensors,
                                        GPU_CORE_TEMP,
                                        id,
                                        _("GPU"),
                                        TEMP_SENSOR,
                                        TRUE,
                                        GPU_ICON,
                                        DEFAULT_GRAPH_COLOR);
        g_free(id);
    }

    return sensors;
}

static gdouble aticonfig_plugin_get_sensor_value(const gchar *path,
                                                const gchar *id,
                                                SensorType type,
                                                GError **error)
{

    if (g_ascii_strcasecmp(path, GPU_CORE_TEMP) != 0 || type != TEMP_SENSOR) {
        g_set_error(error, SENSORS_APPLET_PLUGIN_ERROR,
              0, "Invalid sensor value request to aticonfig plugin");
        return 0;
    }
    ati_update_temps();

    int i = g_ascii_strtoll(id + strlen(SENSOR_ID_PREFIX), NULL, 10);
    if (i < 0 || i >= num_gpus) {
        g_set_error(error, SENSORS_APPLET_PLUGIN_ERROR,
              0, "Sensor index out of range in aticonfig plugin");
        return 0;
    }
    return gpu_temps[i];
}

const gchar *sensors_applet_plugin_name(void)
{
    return plugin_name;
}

GList *sensors_applet_plugin_init(void)
{
    return aticonfig_plugin_init();
}

gdouble sensors_applet_plugin_get_sensor_value(const gchar *path,
                                                const gchar *id,
                                                SensorType type,
                                                GError **error)
{
    return aticonfig_plugin_get_sensor_value(path, id, type, error);
}