summaryrefslogtreecommitdiff
path: root/lib/sensors-applet-plugin.c
blob: 9a32b5e90f8750341124b15c011cae6a408f16b4 (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
157
/*
 * 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
 */

#include <sensors-applet/sensors-applet-plugin.h>

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

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

extern const gchar *plugin_name;

/* recursive function to find sensors in a given path */
void sensors_applet_plugin_find_sensors(GList **sensors,
                                        const gchar *path,
                                        SensorsAppletPluginTestSensorFunc test_sensor) {
        GDir *dir;
	const gchar* new_file;
	gchar *new_path;

	if (g_file_test(path, G_FILE_TEST_IS_REGULAR)) {
                /* also test can actually open file for
                   reading */
                if (access(path, R_OK) == 0) {
                        test_sensor(sensors, path);
                }
		
	}
	/* if is a directory (but not a symlinked dir as this
	   will lead us in circular loops) descend into it and look
	   for a sensor dir
	*/
	if (g_file_test(path, G_FILE_TEST_IS_DIR) && !g_file_test(path, G_FILE_TEST_IS_SYMLINK)) {
		dir = g_dir_open(path, 0, NULL);
		if (dir != NULL) {
			while(NULL != (new_file = g_dir_read_name(dir))) {
				new_path = g_build_filename(path, new_file, NULL);
				sensors_applet_plugin_find_sensors(sensors, 
                                                                   new_path,
                                                                   test_sensor);
				g_free(new_path);
			}
			g_dir_close(dir);
		}
	}
}

/* for error handling */
GQuark sensors_applet_plugin_error_quark(void) {
	static GQuark quark = 0;
	gchar *string;
        
	if (quark == 0) {
		string = g_strdup_printf("%s-plugin-error", plugin_name);
		quark = g_quark_from_string(string);
		g_free(string);
	}

	return quark;
}

void sensors_applet_plugin_default_sensor_limits(SensorType type, 
                                                 gdouble *low_value, 
                                                 gdouble *high_value)
{
        switch (type) {
        case TEMP_SENSOR:
                *low_value = 20.0;
                *high_value = 60.0;
                break;
        case FAN_SENSOR:
                *low_value = 600.0;
                *high_value = 3000.0;
                break;
        default:
                *low_value = 0.0;
                *high_value = 0.0;
        }
}

void sensors_applet_plugin_add_sensor(GList **sensors,
                                      const gchar *path,
                                      const gchar *id,
                                      const gchar *label,
                                      SensorType type,
                                      gboolean enable,
                                      IconType icon,
                                      const gchar *graph_color) 
{
        SensorsAppletSensorInfo *info;
        
        info = g_malloc0(sizeof(*info));

        info->path = g_strdup(path);
        info->id = g_strdup(id);
        info->label = g_strdup(label);
        info->type = type;
        info->enable = enable;
        sensors_applet_plugin_default_sensor_limits(type,
                                                    &(info->low_value),
                                                    &(info->high_value));
        info->multiplier = 1.0;
        info->offset = 0.0;
        info->icon = icon;
        info->graph_color = g_strdup(graph_color);

        *sensors = g_list_append(*sensors, info);
}

void sensors_applet_plugin_add_sensor_with_limits(GList **sensors,
                                                  const gchar *path,
                                                  const gchar *id,
                                                  const gchar *label,
                                                  SensorType type,
                                                  gboolean enable,
                                                  gdouble low_value,
                                                  gdouble high_value,
                                                  IconType icon,
                                                  const gchar *graph_color) 
{
        SensorsAppletSensorInfo *info;
        
        info = g_malloc0(sizeof(*info));

        info->path = g_strdup(path);
        info->id = g_strdup(id);
        info->label = g_strdup(label);
        info->type = type;
        info->enable = enable;
        info->low_value = low_value;
        info->high_value = high_value;
        info->multiplier = 1.0;
        info->offset = 0.0;
        info->icon = icon;
        info->graph_color = g_strdup(graph_color);

        *sensors = g_list_append(*sensors, info);
}