1 /* Setup if we need temperature? */
2 #include "e_wizard.h"
3 #include "e_wizard_api.h"
4 
5 #if defined(__FreeBSD__) || defined(__DragonFly__)
6 # include <sys/types.h>
7 # include <sys/sysctl.h>
8 #endif
9 
10 #ifdef HAVE_EEZE
11 # include <Eeze.h>
12 #endif
13 /*
14    static char *
15    read_file(const char *file)
16    {
17    FILE *f = fopen(file, "r");
18    size_t len;
19    char buf[4096], *p;
20    if (!f) return NULL;
21    len = fread(buf, 1, sizeof(buf) - 1, f);
22    if (len == 0)
23      {
24         fclose(f);
25         return NULL;
26      }
27    buf[len] = 0;
28    for (p = buf; *p; p++)
29      {
30         if (p[0] == '\n') p[0] = 0;
31      }
32    fclose(f);
33    return strdup(buf);
34    }
35  */
36 
37 #if defined (__FreeBSD__) || defined (__OpenBSD__) || defined(__DragonFly__)
38 #else
39 static Eina_List *
_wizard_temperature_get_bus_files(const char * bus)40 _wizard_temperature_get_bus_files(const char *bus)
41 {
42    Eina_List *result;
43    Eina_List *therms;
44    char path[PATH_MAX + PATH_MAX + 3];
45    char busdir[PATH_MAX];
46    char *name;
47 
48    result = NULL;
49 
50    snprintf(busdir, sizeof(busdir), "/sys/bus/%s/devices", bus);
51    /* Look through all the devices for the given bus. */
52    therms = ecore_file_ls(busdir);
53 
54    EINA_LIST_FREE(therms, name)
55      {
56         Eina_List *files;
57         char *file;
58 
59         /* Search each device for temp*_input, these should be
60          * temperature devices. */
61         snprintf(path, sizeof(path), "%s/%s", busdir, name);
62         files = ecore_file_ls(path);
63         EINA_LIST_FREE(files, file)
64           {
65              if ((!strncmp("temp", file, 4)) &&
66                  (!strcmp("_input", &file[strlen(file) - 6])))
67                {
68                   char *f;
69 
70                   snprintf(path, sizeof(path),
71                            "%s/%s/%s", busdir, name, file);
72                   f = strdup(path);
73                   if (f) result = eina_list_append(result, f);
74                }
75              free(file);
76           }
77         free(name);
78      }
79    return result;
80 }
81 #endif
82 
83 /*
84 E_API int
85 wizard_page_init(E_Wizard_Page *pg EINA_UNUSED, Eina_Bool *need_xdg_desktops EINA_UNUSED, Eina_Bool *need_xdg_icons EINA_UNUSED)
86 {
87    return 1;
88 }
89 
90 E_API int
91 wizard_page_shutdown(E_Wizard_Page *pg EINA_UNUSED)
92 {
93    return 1;
94 }
95 */
96 E_API int
wizard_page_show(E_Wizard_Page * pg EINA_UNUSED)97 wizard_page_show(E_Wizard_Page *pg EINA_UNUSED)
98 {
99 #ifdef HAVE_EEZE
100    Eina_List *tempdevs = NULL;
101 #endif
102    int hav_temperature = 0;
103 #if defined (__FreeBSD__) || defined (__OpenBSD__) || defined(__DragonFly__)
104    // figure out on bsd if we have temp sensors
105 #else
106    const char *sensor_path[] = {
107       "/proc/omnibook/temperature",
108       "/proc/acpi/thermal_zone",    //LINUX_ACPI Directory
109       "/sys/class/thermal",         //LINUX_SYS Directory
110       "/sys/devices/temperatures/cpu_temperature",
111       "/sys/devices/temperatures/sensor1_temperature",
112       "/sys/devices/platform/coretemp.0/temp1_input",
113       "/sys/devices/platform/thinkpad_hwmon/temp1_input",
114       NULL
115    };
116 
117 #ifdef HAVE_EEZE
118    tempdevs = eeze_udev_find_by_type(EEZE_UDEV_TYPE_IS_IT_HOT_OR_IS_IT_COLD_SENSOR, NULL);
119 #endif
120    if (tempdevs && (eina_list_count(tempdevs)))
121      hav_temperature = 1;
122    else
123      {
124         int i = 0;
125 
126         while(sensor_path[i] != NULL)
127           {
128              if (ecore_file_exists(sensor_path[i]))
129                {
130                   hav_temperature = 1;
131                   break;
132                }
133              i++;
134           }
135 
136         if (!hav_temperature)
137           {
138              Eina_List *therms;
139 
140              therms = _wizard_temperature_get_bus_files("i2c");
141              if (therms)
142                {
143                   char *name;
144                   if ((name = eina_list_data_get(therms)))
145                     {
146                        if (ecore_file_exists(name))
147                          {
148                             hav_temperature = 1;
149                          }
150                     }
151                   eina_list_free(therms);
152                }
153           }
154 
155         if (!hav_temperature)
156           {
157              Eina_List *therms;
158 
159              therms = _wizard_temperature_get_bus_files("pci");
160              if (therms)
161                {
162                   char *name;
163                   if ((name = eina_list_data_get(therms)))
164                     {
165                        if (ecore_file_exists(name))
166                          {
167                             hav_temperature = 1;
168                          }
169                     }
170                   eina_list_free(therms);
171                }
172           }
173      }
174 #endif
175    if (!hav_temperature)
176      {
177         E_Config_Module *em;
178         Eina_List *l;
179 
180         EINA_LIST_FOREACH(e_config->modules, l, em)
181           {
182              if (!em->name) continue;
183              if (!strcmp(em->name, "temperature"))
184                {
185                   e_config->modules = eina_list_remove_list
186                       (e_config->modules, l);
187                   if (em->name) eina_stringshare_del(em->name);
188                   free(em);
189                   break;
190                }
191           }
192         e_config_save_queue();
193      }
194    return 0; /* 1 == show ui, and wait for user, 0 == just continue */
195 }
196 /*
197 E_API int
198 wizard_page_hide(E_Wizard_Page *pg EINA_UNUSED)
199 {
200    return 1;
201 }
202 
203 E_API int
204 wizard_page_apply(E_Wizard_Page *pg EINA_UNUSED)
205 {
206    return 1;
207 }
208 */
209