summaryrefslogtreecommitdiff
path: root/src/modules/sysinfo/thermal/thermal.c
blob: ae39c6924d488cd390c6e399c81144ef53b23a2c (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
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
#include "thermal.h"

static void
_thermal_thread_free(Tempthread *tth)
{
#if defined(HAVE_EEZE)
   const char *s;
#endif
   if (!tth) return;

   eina_stringshare_del(tth->sensor_name);
   eina_stringshare_del(tth->sensor_path);
#if defined(HAVE_EEZE)
   EINA_LIST_FREE(tth->tempdevs, s) eina_stringshare_del(s);
#endif
   free(tth->extn);
   free(tth);
}

static void
_thermal_face_level_set(Instance *inst, double level)
{
   Edje_Message_Float msg;

   if (level < 0.0) level = 0.0;
   else if (level > 1.0) level = 1.0;
   msg.val = level;
   edje_object_message_send(elm_layout_edje_get(inst->cfg->thermal.o_gadget), EDJE_MESSAGE_FLOAT, 1, &msg);
}

static void
_thermal_apply(Instance *inst, int temp)
{
   char buf[64];

   if (inst->cfg->thermal.temp == temp) return;
   inst->cfg->thermal.temp = temp;
   if (temp != -999)
     {
        if (inst->cfg->thermal.units == FAHRENHEIT) temp = (temp * 9.0 / 5.0) + 32;

        if (!inst->cfg->thermal.have_temp)
          {
             /* enable therm object */
             elm_layout_signal_emit(inst->cfg->thermal.o_gadget, "e,state,known", "");
             inst->cfg->thermal.have_temp = EINA_TRUE;
          }
        if (inst->cfg->thermal.units == FAHRENHEIT)
          snprintf(buf, sizeof(buf), "%i°F", temp);
        else
          snprintf(buf, sizeof(buf), "%i°C", temp);

        _thermal_face_level_set(inst,
                                    (double)(temp - inst->cfg->thermal.low) /
                                    (double)(inst->cfg->thermal.high - inst->cfg->thermal.low));
        elm_layout_text_set(inst->cfg->thermal.o_gadget, "e.text.reading", buf);
     }
   else
     {
        if (inst->cfg->thermal.have_temp)
          {
             /* disable therm object */
             elm_layout_signal_emit(inst->cfg->thermal.o_gadget, "e,state,unknown", "");
             elm_layout_text_set(inst->cfg->thermal.o_gadget, "e.text.reading", "N/A");
             _thermal_face_level_set(inst, 0.5);
             inst->cfg->thermal.have_temp = EINA_FALSE;
          }
     }
}

#if defined(HAVE_EEZE)
static Eina_Bool
_thermal_udev_poll(void *data)
{
   Tempthread *tth = data;
   int temp = thermal_udev_get(tth);

   _thermal_apply(tth->inst, temp);
   return EINA_TRUE;
}
#endif

#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
static void
_thermal_check_sysctl(void *data, Ecore_Thread *th)
{
   Tempthread *tth = data;
   int ptemp = -500, temp;

   for (;;)
     {
        if (ecore_thread_check(th)) break;
        temp = thermal_sysctl_get(tth);
        if (ptemp != temp) ecore_thread_feedback(th, (void *)((long)temp));
        ptemp = temp;
        usleep((1000000.0 / 8.0) * (double)tth->poll_interval);
        if (ecore_thread_check(th)) break;
     }
}
#endif

#if !defined(HAVE_EEZE) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__DragonFly__)
static void
_thermal_check_fallback(void *data, Ecore_Thread *th)
{
   Tempthread *tth = data;
   int ptemp = -500, temp;
   for (;;)
     {
        if (ecore_thread_check(th)) break;
        temp = thermal_fallback_get(tth);
        if (ptemp != temp) ecore_thread_feedback(th, (void *)((long)temp));
        ptemp = temp;
        usleep((1000000.0 / 8.0) * (double)tth->poll_interval);
        if (ecore_thread_check(th)) break;
     }
}
#endif

#if !defined(HAVE_EEZE)
static void
_thermal_check_notify(void *data, Ecore_Thread *th, void *msg)
{
   Tempthread *tth  = data;
   Instance *inst = tth->inst;
   int temp = (int)((long)msg);
   if (th != inst->cfg->thermal.th) return;
   _thermal_apply(inst, temp);
}

static void
_thermal_check_done(void *data, Ecore_Thread *th EINA_UNUSED)
{
   _thermal_thread_free(data);
}
#endif

void
_thermal_config_updated(Instance *inst)
{
   Tempthread *tth;

   if (inst->cfg->thermal.th) ecore_thread_cancel(inst->cfg->thermal.th);

   tth = calloc(1, sizeof(Tempthread));
   tth->poll_interval = inst->cfg->thermal.poll_interval;
   tth->sensor_type = inst->cfg->thermal.sensor_type;
   tth->inst = inst;
   if (inst->cfg->thermal.sensor_name)
     tth->sensor_name = eina_stringshare_add(inst->cfg->thermal.sensor_name);

#if defined(HAVE_EEZE)
   _thermal_udev_poll(tth);
   inst->cfg->thermal.poller = ecore_poller_add(ECORE_POLLER_CORE, inst->cfg->thermal.poll_interval,
                                     _thermal_udev_poll, tth);
   inst->cfg->thermal.tth = tth;
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
    inst->cfg->thermal.th = ecore_thread_feedback_run(_thermal_check_sysctl,
                                        _thermal_check_notify,
                                        _thermal_check_done,
                                        _thermal_check_done,
                                        tth, EINA_TRUE);  
#else 
   inst->cfg->thermal.th = ecore_thread_feedback_run(_thermal_check_fallback,
                                        _thermal_check_notify,
                                        _thermal_check_done,
                                        _thermal_check_done,
                                        tth, EINA_TRUE);
#endif
}

static void
_thermal_face_shutdown(Instance *inst)
{
   if (inst->cfg->thermal.th) ecore_thread_cancel(inst->cfg->thermal.th);
   if (inst->cfg->thermal.sensor_name) eina_stringshare_del(inst->cfg->thermal.sensor_name);
#if defined(HAVE_EEZE)
   if (inst->cfg->thermal.poller)
     {
        E_FREE_FUNC(inst->cfg->thermal.poller, ecore_poller_del);
        _thermal_thread_free(inst->cfg->thermal.tth);
     }
#endif
}

static void
_thermal_resize_cb(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_data EINA_UNUSED)
{
   Evas_Coord w, h;
   Instance *inst = data;

   edje_object_parts_extends_calc(elm_layout_edje_get(inst->cfg->thermal.o_gadget), 0, 0, &w, &h);
   if (inst->cfg->esm == E_SYSINFO_MODULE_THERMAL)
     evas_object_size_hint_aspect_set(inst->o_main, EVAS_ASPECT_CONTROL_BOTH, w, h);
   else
     evas_object_size_hint_aspect_set(inst->cfg->thermal.o_gadget, EVAS_ASPECT_CONTROL_BOTH, w, h);
}

static void
_thermal_removed_cb(void *data, Evas_Object *obj EINA_UNUSED, void *event_data)
{
   Instance *inst = data;

   if (inst->o_main != event_data) return;

   _thermal_face_shutdown(inst);

#if defined(HAVE_EEZE)
   eeze_shutdown();
#endif

   evas_object_event_callback_del_full(inst->o_main, EVAS_CALLBACK_DEL, sysinfo_thermal_remove, data);

   sysinfo_config->items = eina_list_remove(sysinfo_config->items, inst->cfg);
   E_FREE(inst->cfg);
}

void
sysinfo_thermal_remove(void *data, Evas *e EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *event_data EINA_UNUSED)
{
   Instance *inst = data;

   _thermal_face_shutdown(inst);

#if defined(HAVE_EEZE)
   eeze_shutdown();
#endif
}

static void
_thermal_created_cb(void *data, Evas_Object *obj, void *event_data EINA_UNUSED)
{
   Instance *inst = data;
   E_Gadget_Site_Orient orient = e_gadget_site_orient_get(e_gadget_site_get(inst->o_main));

   inst->cfg->thermal.temp = 900;
   inst->cfg->thermal.have_temp = EINA_FALSE;

   inst->cfg->thermal.o_gadget = elm_layout_add(inst->o_main);
   if (orient == E_GADGET_SITE_ORIENT_VERTICAL)
     e_theme_edje_object_set(inst->cfg->thermal.o_gadget,
                               "base/theme/modules/thermal",
                               "e/modules/thermal/main_vert");
   else
     e_theme_edje_object_set(inst->cfg->thermal.o_gadget, "base/theme/modules/thermal",
                           "e/modules/thermal/main");
   E_EXPAND(inst->cfg->thermal.o_gadget);
   E_FILL(inst->cfg->thermal.o_gadget);
   elm_box_pack_end(inst->o_main, inst->cfg->thermal.o_gadget);
   evas_object_event_callback_add(inst->cfg->thermal.o_gadget, EVAS_CALLBACK_RESIZE, _thermal_resize_cb, inst);
   evas_object_show(inst->cfg->thermal.o_gadget);
   evas_object_smart_callback_del_full(obj, "gadget_created", _thermal_created_cb, data);
   _thermal_config_updated(inst);
}

Evas_Object *
sysinfo_thermal_create(Evas_Object *parent, Instance *inst)
{
   inst->cfg->thermal.temp = 900;
   inst->cfg->thermal.have_temp = EINA_FALSE;

   inst->cfg->thermal.o_gadget = elm_layout_add(parent);
   e_theme_edje_object_set(inst->cfg->thermal.o_gadget, "base/theme/modules/temperature",
                           "e/modules/temperature/main");
   E_EXPAND(inst->cfg->thermal.o_gadget);
   E_FILL(inst->cfg->thermal.o_gadget);
   evas_object_event_callback_add(inst->cfg->thermal.o_gadget, EVAS_CALLBACK_RESIZE, _thermal_resize_cb, inst);
   evas_object_show(inst->cfg->thermal.o_gadget);
   _thermal_config_updated(inst);

   return inst->cfg->thermal.o_gadget;
}

static Config_Item *
_conf_item_get(int *id)
{
   Config_Item *ci;
   Eina_List *l;

   if (*id > 0)
     {
        EINA_LIST_FOREACH(sysinfo_config->items, l, ci)
          if (*id == ci->id && ci->esm == E_SYSINFO_MODULE_THERMAL) return ci;
     }

   ci = E_NEW(Config_Item, 1);

   if (*id != -1)
     ci->id = eina_list_count(sysinfo_config->items)+1;
   else
     ci->id = -1;

   ci->esm = E_SYSINFO_MODULE_THERMAL;
   ci->thermal.poll_interval = 128;
   ci->thermal.low = 30;
   ci->thermal.high = 80;
   ci->thermal.sensor_type = SENSOR_TYPE_NONE;
   ci->thermal.sensor_name = NULL;
   ci->thermal.units = CELSIUS;

   sysinfo_config->items = eina_list_append(sysinfo_config->items, ci);

   return ci;
}

Evas_Object *
thermal_create(Evas_Object *parent, int *id, E_Gadget_Site_Orient orient EINA_UNUSED)
{
   Instance *inst;

   inst = E_NEW(Instance, 1);
   inst->cfg = _conf_item_get(id);
   *id = inst->cfg->id;
   inst->o_main = elm_box_add(parent);
   E_EXPAND(inst->o_main);
   evas_object_smart_callback_add(parent, "gadget_created", _thermal_created_cb, inst);
   evas_object_smart_callback_add(parent, "gadget_removed", _thermal_removed_cb, inst);
   evas_object_event_callback_add(inst->o_main, EVAS_CALLBACK_DEL, sysinfo_thermal_remove, inst);
   evas_object_show(inst->o_main);

#if defined(HAVE_EEZE)
   eeze_init();
#endif

   if (inst->cfg->id < 0) return inst->o_main;
   sysinfo_instances =
     eina_list_append(sysinfo_instances, inst);

   return inst->o_main;
}