made the setting temperature unit dynamic
[psensor.git] / src / main.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301 USA
18  */
19 #include <locale.h>
20
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28
29 #include <gtk/gtk.h>
30
31 #include <config.h>
32
33 #include <amd.h>
34 #include <cfg.h>
35 #include <graph.h>
36 #include <hdd.h>
37 #include <lmsensor.h>
38 #include <notify_cmd.h>
39 #include <nvidia.h>
40 #include <pgtop2.h>
41 #include <pmutex.h>
42 #include <psensor.h>
43 #include <pudisks2.h>
44 #include <rsensor.h>
45 #include <slog.h>
46 #include <ui.h>
47 #include <ui_appindicator.h>
48 #include <ui_color.h>
49 #include <ui_graph.h>
50 #include <ui_notify.h>
51 #include <ui_pref.h>
52 #include <ui_sensorlist.h>
53 #include <ui_status.h>
54 #include <ui_unity.h>
55
56 static const char *program_name;
57
58 static void print_version(void)
59 {
60         printf("psensor %s\n", VERSION);
61         printf(_("Copyright (C) %s jeanfi@gmail.com\n"
62                  "License GPLv2: GNU GPL version 2 or later "
63                  "<http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>\n"
64                  "This is free software: you are free to change and"
65                  " redistribute it.\n"
66                  "There is NO WARRANTY, to the extent permitted by law.\n"),
67                "2010-2014");
68 }
69
70 static void print_help(void)
71 {
72         printf(_("Usage: %s [OPTION]...\n"), program_name);
73
74         puts(_("Psensor is a GTK+ application for monitoring hardware sensors, "
75                "including temperatures and fan speeds."));
76
77         puts("");
78         puts(_("Options:"));
79         puts(_("  -h, --help          display this help and exit\n"
80                "  -v, --version       display version information and exit"));
81
82         puts("");
83
84         puts(_(
85 "  -u, --url=URL       the URL of the psensor-server,\n"
86 "                      example: http://hostname:3131"));
87         puts(_(
88 "  -n, --new-instance  force the creation of a new Psensor application"));
89         puts("");
90
91         puts(_("  -d, --debug=LEVEL   "
92                "set the debug level, integer between 0 and 3"));
93
94         puts("");
95
96         printf(_("Report bugs to: %s\n"), PACKAGE_BUGREPORT);
97         puts("");
98         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
99 }
100
101 /*
102  * Updates the size of the sensor values if different than the
103  * configuration.
104  */
105 static void
106 update_psensor_values_size(struct psensor **sensors, struct config *cfg)
107 {
108         struct psensor **cur, *s;
109
110         for (cur = sensors; *cur; cur++) {
111                 s = *cur;
112
113                 if (s->values_max_length != cfg->sensor_values_max_length)
114                         psensor_values_resize(s,
115                                               cfg->sensor_values_max_length);
116         }
117 }
118
119 static void *update_measures(void *data)
120 {
121         struct psensor **sensors;
122         struct config *cfg;
123         int period;
124         struct ui_psensor *ui;
125
126         ui = (struct ui_psensor *)data;
127         cfg = ui->config;
128
129         while (1) {
130                 pmutex_lock(&ui->sensors_mutex);
131
132                 sensors = ui->sensors;
133                 if (!sensors)
134                         pthread_exit(NULL);
135
136                 update_psensor_values_size(sensors, cfg);
137
138                 lmsensor_psensor_list_update(sensors);
139
140                 remote_psensor_list_update(sensors);
141                 nvidia_psensor_list_update(sensors);
142                 amd_psensor_list_update(sensors);
143                 udisks2_psensor_list_update(sensors);
144                 gtop2_psensor_list_update(sensors);
145                 atasmart_psensor_list_update(sensors);
146                 hddtemp_psensor_list_update(sensors);
147
148                 psensor_log_measures(sensors);
149
150                 period = cfg->sensor_update_interval;
151
152                 pmutex_unlock(&ui->sensors_mutex);
153
154                 sleep(period);
155         }
156 }
157
158 static void indicators_update(struct ui_psensor *ui)
159 {
160         struct psensor **ss, *s;
161         bool attention;
162
163         attention = false;
164         ss = ui->sensors;
165         while (*ss) {
166                 s = *ss;
167
168                 if (s->alarm_raised && config_get_sensor_alarm_enabled(s->id)) {
169                         attention = true;
170                         break;
171                 }
172
173                 ss++;
174         }
175
176         if (is_appindicator_supported())
177                 ui_appindicator_update(ui, attention);
178
179         if (is_status_supported())
180                 ui_status_update(ui, attention);
181 }
182
183 static gboolean ui_refresh_thread(gpointer data)
184 {
185         struct config *cfg;
186         gboolean ret;
187         struct ui_psensor *ui = (struct ui_psensor *)data;
188         int use_celsius;
189
190         ret = TRUE;
191         cfg = ui->config;
192
193         pmutex_lock(&ui->sensors_mutex);
194
195         graph_update(ui->sensors, ui->w_graph, ui->config, ui->main_window);
196
197         ui_sensorlist_update(ui, 0);
198
199         if (is_appindicator_supported() || is_status_supported())
200                 indicators_update(ui);
201
202         if (config_get_temperature_unit() == CELSIUS)
203                 use_celsius = 1;
204         else
205                 use_celsius = 0;
206         ui_unity_launcher_entry_update(ui->sensors,
207                                        !cfg->unity_launcher_count_disabled,
208                                        use_celsius);
209
210         if (ui->graph_update_interval != cfg->graph_update_interval) {
211                 ui->graph_update_interval = cfg->graph_update_interval;
212                 ret = FALSE;
213         }
214
215         pmutex_unlock(&ui->sensors_mutex);
216
217         if (ret == FALSE)
218                 g_timeout_add(1000 * ui->graph_update_interval,
219                               ui_refresh_thread, ui);
220
221         return ret;
222 }
223
224 static void cb_alarm_raised(struct psensor *sensor, void *data)
225 {
226         if (config_get_sensor_alarm_enabled(sensor->id)) {
227                 ui_notify(sensor, (struct ui_psensor *)data);
228                 notify_cmd(sensor);
229         }
230 }
231
232 static void
233 associate_cb_alarm_raised(struct psensor **sensors, struct ui_psensor *ui)
234 {
235         bool ret;
236         struct psensor *s;
237         double d, high_temp;
238
239         high_temp = config_get_default_high_threshold_temperature();
240
241         while (*sensors) {
242                 s = *sensors;
243
244                 s->cb_alarm_raised = cb_alarm_raised;
245                 s->cb_alarm_raised_data = ui;
246
247                 ret = config_get_sensor_alarm_high_threshold
248                         (s->id, &s->alarm_high_threshold);
249
250                 if (!ret) {
251                         if (s->max == UNKNOWN_DBL_VALUE) {
252                                 if (s->type & SENSOR_TYPE_TEMP)
253                                         s->alarm_high_threshold = high_temp;
254                         } else {
255                                 s->alarm_high_threshold = s->max;
256                         }
257                 }
258
259                 ret = config_get_sensor_alarm_low_threshold
260                         (s->id, &s->alarm_low_threshold);
261
262                 if (!ret && s->min != UNKNOWN_DBL_VALUE)
263                         s->alarm_low_threshold = s->min;
264
265                 sensors++;
266         }
267 }
268
269 static void associate_preferences(struct psensor **sensors)
270 {
271         struct psensor **sensor_cur = sensors;
272
273         while (*sensor_cur) {
274                 char *n;
275                 struct psensor *s = *sensor_cur;
276
277                 n = config_get_sensor_name(s->id);
278
279                 if (n) {
280                         free(s->name);
281                         s->name = n;
282                 }
283
284                 sensor_cur++;
285         }
286 }
287
288 static void log_init(void)
289 {
290         const char *dir;
291         char *path;
292
293         dir = get_psensor_user_dir();
294
295         if (!dir)
296                 return;
297
298         path = malloc(strlen(dir)+1+strlen("log")+1);
299         sprintf(path, "%s/%s", dir, "log");
300
301         log_open(path);
302
303         free(path);
304 }
305
306 static struct option long_options[] = {
307         {"version", no_argument, NULL, 'v'},
308         {"help", no_argument, NULL, 'h'},
309         {"url", required_argument, NULL, 'u'},
310         {"debug", required_argument, NULL, 'd'},
311         {"new-instance", no_argument, NULL, 'n'},
312         {NULL, 0, NULL, 0}
313 };
314
315 static gboolean initial_window_show(gpointer data)
316 {
317         struct ui_psensor *ui;
318
319         log_debug("initial_window_show()");
320
321         ui = (struct ui_psensor *)data;
322
323         log_debug("is_status_supported: %d", is_status_supported());
324         log_debug("is_appindicator_supported: %d",
325                    is_appindicator_supported());
326         log_debug("hide_on_startup: %d", ui->config->hide_on_startup);
327
328         if (!ui->config->hide_on_startup
329             || (!is_appindicator_supported() && !is_status_supported()))
330                 ui_window_show(ui);
331
332         ui_window_update(ui);
333
334         return FALSE;
335 }
336
337 static void log_glib_info(void)
338 {
339         log_debug("Compiled with GLib %d.%d.%d",
340                   GLIB_MAJOR_VERSION,
341                   GLIB_MINOR_VERSION,
342                   GLIB_MICRO_VERSION);
343
344         log_debug("Running with GLib %d.%d.%d",
345                   glib_major_version,
346                   glib_minor_version,
347                   glib_micro_version);
348 }
349
350 static void cb_activate(GApplication *application,
351                         gpointer data)
352 {
353         ui_window_show((struct ui_psensor *)data);
354 }
355
356 /*
357  * Release memory for Valgrind.
358  */
359 static void cleanup(struct ui_psensor *ui)
360 {
361         pmutex_lock(&ui->sensors_mutex);
362
363         log_debug("Cleanup...");
364
365         nvidia_cleanup();
366         amd_cleanup();
367         rsensor_cleanup();
368
369         psensor_list_free(ui->sensors);
370         ui->sensors = NULL;
371
372         ui_appindicator_cleanup();
373
374         ui_status_cleanup();
375
376         pmutex_unlock(&ui->sensors_mutex);
377
378         config_cleanup();
379
380         log_debug("Cleanup done, closing log");
381 }
382
383 /*
384  * Creates the list of sensors.
385  *
386  * 'url': remote psensor server url, null for local monitoring.
387  */
388 static struct psensor **create_sensors_list(const char *url)
389 {
390         struct psensor **sensors;
391
392         if (url) {
393                 if (rsensor_is_supported()) {
394                         rsensor_init();
395                         sensors = get_remote_sensors(url, 600);
396                 } else {
397                         log_err(_("Psensor has not been compiled with remote "
398                                   "sensor support."));
399                         exit(EXIT_FAILURE);
400                 }
401         } else {
402                 sensors = malloc(sizeof(struct psensor *));
403                 *sensors = NULL;
404
405                 if (config_is_lmsensor_enabled())
406                         lmsensor_psensor_list_append(&sensors, 600);
407
408                 if (config_is_hddtemp_enabled())
409                         hddtemp_psensor_list_append(&sensors, 600);
410
411                 if (config_is_libatasmart_enabled())
412                         atasmart_psensor_list_append(&sensors, 600);
413
414                 if (config_is_nvctrl_enabled())
415                         nvidia_psensor_list_append(&sensors, 600);
416
417                 if (config_is_atiadlsdk_enabled())
418                         amd_psensor_list_append(&sensors, 600);
419
420                 if (config_is_gtop2_enabled())
421                         gtop2_psensor_list_append(&sensors, 600);
422
423                 if (config_is_udisks2_enabled())
424                         udisks2_psensor_list_append(&sensors, 600);
425         }
426
427         associate_preferences(sensors);
428
429         return sensors;
430 }
431
432 int main(int argc, char **argv)
433 {
434         struct ui_psensor ui;
435         pthread_t thread;
436         int optc, cmdok, opti, new_instance, ret;
437         char *url = NULL;
438         GApplication *app;
439
440         program_name = argv[0];
441
442         setlocale(LC_ALL, "");
443
444 #if ENABLE_NLS
445         bindtextdomain(PACKAGE, LOCALEDIR);
446         textdomain(PACKAGE);
447 #endif
448
449         new_instance = 0;
450
451         cmdok = 1;
452         while ((optc = getopt_long(argc, argv, "vhd:u:n", long_options,
453                                    &opti)) != -1) {
454                 switch (optc) {
455                 case 'u':
456                         if (optarg)
457                                 url = strdup(optarg);
458                         break;
459                 case 'h':
460                         print_help();
461                         exit(EXIT_SUCCESS);
462                 case 'v':
463                         print_version();
464                         exit(EXIT_SUCCESS);
465                 case 'd':
466                         log_level = atoi(optarg);
467                         log_info(_("Enables debug mode."));
468                         break;
469                 case 'n':
470                         new_instance = 1;
471                         break;
472                 default:
473                         cmdok = 0;
474                         break;
475                 }
476         }
477
478         if (!cmdok || optind != argc) {
479                 fprintf(stderr, _("Try `%s --help' for more information.\n"),
480                         program_name);
481                 exit(EXIT_FAILURE);
482         }
483
484         log_init();
485
486         app = g_application_new("wpitchoune.psensor", 0);
487
488         g_application_register(app, NULL, NULL);
489
490         if (!new_instance && g_application_get_is_remote(app)) {
491                 g_application_activate(app);
492                 log_warn(_("A Psensor instance already exists."));
493                 exit(EXIT_SUCCESS);
494         }
495
496         g_signal_connect(app, "activate", G_CALLBACK(cb_activate), &ui);
497
498         log_glib_info();
499 #if !(GLIB_CHECK_VERSION(2, 31, 0))
500         /*
501          * Since GLib 2.31 g_thread_init call is deprecated and not
502          * needed.
503          */
504         log_debug("Calling g_thread_init(NULL)");
505         g_thread_init(NULL);
506 #endif
507
508         gtk_init(NULL, NULL);
509
510         pmutex_init(&ui.sensors_mutex);
511
512         ui.config = config_load();
513
514         ui.sensors = create_sensors_list(url);
515         associate_cb_alarm_raised(ui.sensors, &ui);
516
517         if (ui.config->slog_enabled)
518                 slog_activate(NULL,
519                               ui.sensors,
520                               &ui.sensors_mutex,
521                               config_get_slog_interval());
522
523         ui_status_init(&ui);
524         ui_status_set_visible(1);
525
526         /* main window */
527         ui_window_create(&ui);
528
529         ui_enable_alpha_channel(&ui);
530
531         ret = pthread_create(&thread, NULL, update_measures, &ui);
532
533         if (ret)
534                 log_err(_("Failed to create thread for monitoring sensors"));
535
536         ui.graph_update_interval = ui.config->graph_update_interval;
537
538         g_timeout_add(1000 * ui.graph_update_interval, ui_refresh_thread, &ui);
539
540         ui_appindicator_init(&ui);
541
542         gdk_notify_startup_complete();
543
544         /*
545          * hack, did not find a cleaner solution.
546          * wait 30s to ensure that the status icon is attempted to be
547          * drawn before determining whether the main window must be
548          * show.
549          */
550         if  (ui.config->hide_on_startup)
551                 g_timeout_add(30000, (GSourceFunc)initial_window_show, &ui);
552         else
553                 initial_window_show(&ui);
554
555         /* main loop */
556         gtk_main();
557
558         g_object_unref(app);
559         cleanup(&ui);
560
561         log_debug("Quitting...");
562         log_close();
563
564         if (url)
565                 free(url);
566
567         return 0;
568 }