aboutsummaryrefslogtreecommitdiffstats
path: root/src/modules/temperature/e_mod_config.c
blob: 597184ec8f79c358b89506ba77069a560e0d16a0 (plain) (blame)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "e.h"
#include "e_mod_main.h"

#define FAR_2_CEL(x) ((x - 32) / 9.0) * 5.0
#define CEL_2_FAR(x) (x * 9.0 / 5.0) + 32

struct _E_Config_Dialog_Data 
{
   struct 
     {
        int interval;
     } poll;

   int unit_method;
#ifdef HAVE_EEZE
   int backend;
#endif
   struct 
     {
        int low, high;
     } temp;

   int sensor;
   Eina_List *sensors;

   Evas_Object *o_high, *o_low;

   Config_Face *inst;
};

/* local function prototypes */
static void *_create_data(E_Config_Dialog *cfd);
static void _fill_data_tempget(E_Config_Dialog_Data *cfdata);
static void _fill_sensors(E_Config_Dialog_Data *cfdata, const char *name);
static void _free_data(E_Config_Dialog *cfd EINA_UNUSED, E_Config_Dialog_Data *cfdata);
static Evas_Object *_basic_create(E_Config_Dialog *cfd, Evas *evas, E_Config_Dialog_Data *cfdata);
static int _basic_apply(E_Config_Dialog *cfd, E_Config_Dialog_Data *cfdata);
static void _cb_display_changed(void *data, Evas_Object *obj EINA_UNUSED);

static Eina_List *
temperature_get_bus_files(const char *bus)
{
   Eina_List *result;
   Eina_List *therms;
   char path[PATH_MAX];
   char busdir[PATH_MAX];
   char *name;

   result = NULL;

   snprintf(busdir, sizeof(busdir), "/sys/bus/%s/devices", bus);
   /* Look through all the devices for the given bus. */
   therms = ecore_file_ls(busdir);

   EINA_LIST_FREE(therms, name)
     {
        Eina_List *files;
        char *file;

        /* Search each device for temp*_input, these should be
         * temperature devices. */
        snprintf(path, sizeof(path), "%s/%s", busdir, name);
        files = ecore_file_ls(path);
        EINA_LIST_FREE(files, file)
          {
             if ((!strncmp("temp", file, 4)) &&
                 (!strcmp("_input", &file[strlen(file) - 6])))
               {
                  char *f;

                  snprintf(path, sizeof(path),
                           "%s/%s/%s", busdir, name, file);
                  f = strdup(path);
                  if (f) result = eina_list_append(result, f);
               }
             free(file);
          }
        free(name);
     }
   return result;
}


void 
config_temperature_module(Config_Face *inst) 
{
   E_Config_Dialog_View *v;
   char buff[PATH_MAX];

   v = E_NEW(E_Config_Dialog_View, 1);
   v->create_cfdata = _create_data;
   v->free_cfdata = _free_data;
   v->basic.create_widgets = _basic_create;
   v->basic.apply_cfdata = _basic_apply;

   snprintf(buff, sizeof(buff), 
            "%s/e-module-temperature.edj", inst->module->dir);
   inst->config_dialog = 
     e_config_dialog_new(NULL, 
                         _("Temperature Settings"), "E", 
                         "_e_mod_temperature_config_dialog", buff, 0, v, inst);
}

/* local function prototypes */
static void *
_create_data(E_Config_Dialog *cfd) 
{
   E_Config_Dialog_Data *cfdata;

   cfdata = E_NEW(E_Config_Dialog_Data, 1);
   cfdata->inst = cfd->data;
   _fill_data_tempget(cfdata);
   return cfdata;
}

static void 
_fill_data_tempget(E_Config_Dialog_Data *cfdata) 
{
   cfdata->unit_method = cfdata->inst->units;
   cfdata->poll.interval = cfdata->inst->poll_interval;
   cfdata->temp.low = cfdata->inst->low;
   cfdata->temp.high = cfdata->inst->high;
   cfdata->sensor = 0;
#ifdef HAVE_EEZE
   cfdata->backend = cfdata->inst->backend;
   if (cfdata->backend == TEMPGET)
     {
#endif
        switch (cfdata->inst->sensor_type) 
          {
           case SENSOR_TYPE_NONE:
           case SENSOR_TYPE_FREEBSD:
           case SENSOR_TYPE_OMNIBOOK:
           case SENSOR_TYPE_LINUX_MACMINI:
           case SENSOR_TYPE_LINUX_PBOOK:
           case SENSOR_TYPE_LINUX_INTELCORETEMP:
             break;
           case SENSOR_TYPE_LINUX_I2C:
             _fill_sensors(cfdata, "i2c");
             break;
           case SENSOR_TYPE_LINUX_PCI:
             _fill_sensors(cfdata, "pci");
             break;
           case SENSOR_TYPE_LINUX_ACPI: 
               {
                  Eina_List *l;

                  if ((l = ecore_file_ls("/proc/acpi/thermal_zone")))
                    {
                       char *name;
                       int n = 0;

                       EINA_LIST_FREE(l, name) 
                         {
                            cfdata->sensors = 
                              eina_list_append(cfdata->sensors, name);
                            if (!strcmp(cfdata->inst->sensor_name, name))
                              cfdata->sensor = n;
                            n++;
                         }
                    }
                  break;
               }
           case SENSOR_TYPE_LINUX_SYS: 
               {
                  Eina_List *l;
                  
                  if ((l = ecore_file_ls("/sys/class/thermal")))
                    {
                       char *name;
                       int n = 0;
                       
                       EINA_LIST_FREE(l, name) 
                         {
                            if (!strncmp(name, "thermal", 7))
                              {
                                 cfdata->sensors = 
                                    eina_list_append(cfdata->sensors, name);
                                 if (!strcmp(cfdata->inst->sensor_name, name))
                                    cfdata->sensor = n;
                                 n++;
                              }
                         }
                    }
                  break;
               }
           default:
             break;
          }
#ifdef HAVE_EEZE
   }
#endif
}

static void 
_fill_sensors(E_Config_Dialog_Data *cfdata, const char *name) 
{
   Eina_List *therms, *l;
   char *n;

   if (!name) return;
   if ((therms = temperature_get_bus_files(name)))
     {
        char path[PATH_MAX];

        EINA_LIST_FREE(therms, n) 
          {
             if (ecore_file_exists(n)) 
               {
                  int len;

                  sprintf(path, "%s", ecore_file_file_get(n));
                  len = strlen(path);
                  if (len > 6) path[len - 6] = '\0';
                  cfdata->sensors = 
                    eina_list_append(cfdata->sensors, strdup(path));
               }
             free(n);
          }
     }
   EINA_LIST_FOREACH(cfdata->sensors, l, n) 
     {
        if (!strcmp(cfdata->inst->sensor_name, n)) break;
        cfdata->sensor++;
     }
}

static void 
_free_data(E_Config_Dialog *cfd EINA_UNUSED, E_Config_Dialog_Data *cfdata) 
{
   char *sensor;

   cfdata->inst->config_dialog = NULL;
   EINA_LIST_FREE(cfdata->sensors, sensor)
     free(sensor);
   E_FREE(cfdata);
}

static Evas_Object *
_basic_create(E_Config_Dialog *cfd EINA_UNUSED, Evas *evas, E_Config_Dialog_Data *cfdata) 
{
   Evas_Object *otb, *ol, *ow;
   E_Radio_Group *rg;

   otb = e_widget_toolbook_add(evas, 24, 24);

   if (cfdata->sensors) 
     {
        Eina_List *l;
        char *name;
        int n = 0;

        ol = e_widget_list_add(evas, 0, 0);
        rg = e_widget_radio_group_new(&(cfdata->sensor));
        EINA_LIST_FOREACH(cfdata->sensors, l, name) 
          {
             ow = e_widget_radio_add(evas, _(name), n, rg);
             e_widget_list_object_append(ol, ow, 1, 0, 0.5);
             n++;
          }
        e_widget_toolbook_page_append(otb, NULL, _("Sensors"), ol, 
                                      1, 0, 1, 0, 0.5, 0.0);
     }

   ol = e_widget_list_add(evas, 0, 0);
   rg = e_widget_radio_group_new(&(cfdata->unit_method));
   ow = e_widget_radio_add(evas, _("Celsius"), CELSIUS, rg);
   e_widget_on_change_hook_set(ow, _cb_display_changed, cfdata);
   e_widget_list_object_append(ol, ow, 1, 1, 0.5);
   ow = e_widget_radio_add(evas, _("Fahrenheit"), FAHRENHEIT, rg);
   e_widget_on_change_hook_set(ow, _cb_display_changed, cfdata);
   e_widget_list_object_append(ol, ow, 1, 1, 0.5);
   e_widget_toolbook_page_append(otb, NULL, _("Display Units"), ol, 
                                 0, 0, 0, 0, 0.5, 0.0);

   ol = e_widget_list_add(evas, 0, 0);
   ow = e_widget_slider_add(evas, 1, 0, _("%1.0f ticks"), 1, 1024, 4, 0, 
                            NULL, &(cfdata->poll.interval), 150);
   e_widget_list_object_append(ol, ow, 1, 1, 0.5);
   e_widget_toolbook_page_append(otb, NULL, _("Check Interval"), ol, 
                                 1, 0, 1, 0, 0.5, 0.0);

   ol = e_widget_list_add(evas, 0, 0);
   ow = e_widget_label_add(evas, _("High Temperature"));
   e_widget_list_object_append(ol, ow, 1, 1, 0.5);
   if (cfdata->unit_method == FAHRENHEIT)
     cfdata->o_high = 
     e_widget_slider_add(evas, 1, 0, _("%1.0f F"), 0, 230, 5, 0, 
                         NULL, &(cfdata->temp.high), 150);
   else
     cfdata->o_high = 
     e_widget_slider_add(evas, 1, 0, _("%1.0f C"), 0, 110, 5, 0, 
                         NULL, &(cfdata->temp.high), 150);
   e_widget_list_object_append(ol, cfdata->o_high, 1, 1, 0.5);

   ow = e_widget_label_add(evas, _("Low Temperature"));
   e_widget_list_object_append(ol, ow, 1, 1, 0.5);
   if (cfdata->unit_method == FAHRENHEIT)
     cfdata->o_low = 
     e_widget_slider_add(evas, 1, 0, _("%1.0f F"), 0, 200, 5, 0, 
                         NULL, &(cfdata->temp.low), 150);
   else
     cfdata->o_low = 
     e_widget_slider_add(evas, 1, 0, _("%1.0f C"), 0, 95, 5, 0, 
                         NULL, &(cfdata->temp.low), 150);
   e_widget_list_object_append(ol, cfdata->o_low, 1, 1, 0.5);

   e_widget_toolbook_page_append(otb, NULL, _("Temperatures"), ol, 
                                 1, 0, 1, 0, 0.5, 0.0);
#ifdef HAVE_EEZE
   ol = e_widget_list_add(evas, 0, 0);
   rg = e_widget_radio_group_new(&(cfdata->backend));
   ow = e_widget_radio_add(evas, _("Internal"), TEMPGET, rg);
   e_widget_list_object_append(ol, ow, 1, 1, 0.5);
   ow = e_widget_radio_add(evas, _("udev"), UDEV, rg);
   e_widget_list_object_append(ol, ow, 1, 1, 0.5);
   e_widget_toolbook_page_append(otb, NULL, _("Hardware"), ol, 
                                 0, 0, 0, 0, 0.5, 0.0);
#endif
   e_widget_toolbook_page_show(otb, 0);
   return otb;
}

static int 
_basic_apply(E_Config_Dialog *cfd EINA_UNUSED, E_Config_Dialog_Data *cfdata) 
{
   cfdata->inst->poll_interval = cfdata->poll.interval;
   cfdata->inst->units = cfdata->unit_method;
   cfdata->inst->low = cfdata->temp.low;
   cfdata->inst->high = cfdata->temp.high;
#ifdef HAVE_EEZE
   cfdata->inst->backend = cfdata->backend;
#endif

   eina_stringshare_replace(&cfdata->inst->sensor_name, 
                            eina_list_nth(cfdata->sensors, cfdata->sensor));

   e_config_save_queue();
   temperature_face_update_config(cfdata->inst);
   return 1;
}

static void 
_cb_display_changed(void *data, Evas_Object *obj EINA_UNUSED) 
{
   E_Config_Dialog_Data *cfdata;
   int val;

   if (!(cfdata = data)) return;
   if (cfdata->unit_method == FAHRENHEIT)
     {
        e_widget_slider_value_range_set(cfdata->o_low, 0, 200);
        e_widget_slider_value_range_set(cfdata->o_high, 0, 230);
        e_widget_slider_value_int_get(cfdata->o_low, &val);
        e_widget_slider_value_int_set(cfdata->o_low, CEL_2_FAR(val));
        e_widget_slider_value_int_get(cfdata->o_high, &val);
        e_widget_slider_value_int_set(cfdata->o_high, CEL_2_FAR(val));
        e_widget_slider_value_format_display_set(cfdata->o_low, _("%1.0f F"));
        e_widget_slider_value_format_display_set(cfdata->o_high, _("%1.0f F"));
     }
   else
     {
        e_widget_slider_value_range_set(cfdata->o_low, 0, 95);
        e_widget_slider_value_range_set(cfdata->o_high, 0, 110);
        e_widget_slider_value_int_get(cfdata->o_low, &val);
        e_widget_slider_value_int_set(cfdata->o_low, FAR_2_CEL(val));
        e_widget_slider_value_int_get(cfdata->o_high, &val);
        e_widget_slider_value_int_set(cfdata->o_high, FAR_2_CEL(val));
        e_widget_slider_value_format_display_set(cfdata->o_low, _("%1.0f C"));
        e_widget_slider_value_format_display_set(cfdata->o_high, _("%1.0f C"));
     }
}