File: sensors-interface-common.c

package info (click to toggle)
xfce4-sensors-plugin 1.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,652 kB
  • sloc: sh: 11,446; ansic: 5,108; makefile: 225
file content (176 lines) | stat: -rw-r--r-- 5,278 bytes parent folder | download | duplicates (2)
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
/* $Id$ */
/*  Copyright 2004-2010 Fabian Nowak (timystery@arcor.de)
 *
 *  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/* Note for programmers and editors: Try to use 4 spaces instead of Tab! */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

/* Xfce includes */
// #include <libxfce4panel/xfce-panel-enums.h>
#include <gtk/gtk.h>
#include <libxfce4panel/xfce-panel-plugin.h>

/* Local/package includes */
#include <configuration.h>
#include <sensors-interface-common.h>
#include <middlelayer.h>
#include <cpu.h>

t_sensors *
sensors_new (XfcePanelPlugin *plugin, gchar *plugin_config_file)
{
    t_sensors *sensors;
    gint result;
    t_chip *chip;
    t_chipfeature *chipfeature;

    TRACE ("enters sensors_new");

    sensors = g_new0 (t_sensors, 1);
    sensors->plugin_config_file = plugin_config_file; /* important as we check against NULL frequently */

    /* init xfce sensors stuff width default values */
    sensors_init_default_values (sensors, plugin);

    /* get suppressmessages */
    sensors_read_preliminary_config(plugin, sensors);

    /* read all sensors from libraries */
    result = initialize_all (&(sensors->chips), &(sensors->suppressmessage));
    if (result==0)
      return NULL;

    sensors->num_sensorchips = sensors->chips->len;

    /* error handling for no sensors */
    if (!sensors->chips || sensors->num_sensorchips <= 0) {
        if (!sensors->chips)
            sensors->chips = g_ptr_array_new ();

        chip = g_new ( t_chip, 1);
        g_ptr_array_add (sensors->chips, chip);
        chip->chip_features = g_ptr_array_new();
        chipfeature = g_new (t_chipfeature, 1);

        chipfeature->address = 0;
        chip->sensorId = g_strdup(_("No sensors found!"));
        chip->description = g_strdup(_("No sensors found!"));
        chip->num_features = 1;
        chipfeature->color = g_strdup("#000000");
        /* g_free (chipfeature->name); */
        chipfeature->name = g_strdup("No sensor");
        chipfeature->valid = TRUE;
        /* g_free (chipfeature->formatted_value); */
        chipfeature->formatted_value = g_strdup("0.0");
        chipfeature->raw_value = 0.0;
        chipfeature->min_value = 0;
        chipfeature->max_value = 7000;
        chipfeature->show = FALSE;

        g_ptr_array_add (chip->chip_features, chipfeature);
    }

    TRACE ("leaves sensors_new");

    return sensors;
}



void
sensors_init_default_values  (t_sensors *sensors, XfcePanelPlugin *plugin)
{
    TRACE ("enters sensors_init_default_values");

    sensors->show_title = TRUE;
    sensors->show_labels = TRUE;
    sensors->display_values_type = DISPLAY_TEXT;
    sensors->bars_created = FALSE;
    sensors->font_size = g_strdup("medium");
    sensors->font_size_numerical = 2;
    sensors->lines_size = 3;

    sensors->show_colored_bars = TRUE;
    sensors->sensors_refresh_time = 60;
    sensors->scale = CELSIUS;

    sensors->plugin = plugin; // we prefer storing NULL in here in case it is NULL.


    /* double-click improvement */
    sensors->exec_command = TRUE;
    sensors->command_name = g_strdup("xfce4-sensors");
    sensors->doubleclick_id = 0;

    /* show units */
    sensors->show_units = TRUE;

    sensors->suppressmessage = FALSE;

    sensors->show_smallspacings = FALSE;

    font = NULL;

    TRACE ("leaves sensors_init_default_values");
}


void
format_sensor_value (t_tempscale scale, t_chipfeature *chipfeature,
                     double sensorFeature, gchar **help)
{
    /* TRACE ("enters format_sensor_value"); */

    switch (chipfeature->class) {
        case TEMPERATURE:
           if (scale == FAHRENHEIT) {
                *help = g_strdup_printf(_("%.1f °F"),
                            (float) (sensorFeature * 9/5 + 32) );
           } else { /* Celsius */
                *help = g_strdup_printf(_("%.1f °C"), sensorFeature);
           }
           break;

        case VOLTAGE:
               *help = g_strdup_printf(_("%+.2f V"), sensorFeature);
               break;

        case ENERGY:
               *help = g_strdup_printf(_("%.0f mWh"), sensorFeature);
               break;

        case STATE:
                if (sensorFeature==0.0)
                    *help = g_strdup (_("off"));
                else
                    *help = g_strdup (_("on"));
               break;

        case SPEED:
               *help = g_strdup_printf(_("%.0f rpm"), sensorFeature);
               break;

        default:
                *help = g_strdup_printf("%+.2f", sensorFeature);
               break;
    } /* end switch */

    /* TRACE ("leaves format_sensor_value"); */
}