aboutsummaryrefslogtreecommitdiffstats
path: root/applets/brightness/brightness.c
blob: a523b29eb11a42f9079e0afc4a6e197d939d039f (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
/* 
 * Author: Manuel Teira <manuel.teira@telefonica.net>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * Licensed under the GPL v2 or greater.
 */

#include <config.h>
#include <string.h>
#include <errno.h>
#include <matchbox-panel/mb-panel.h>
#include <gtk/gtk.h>

typedef struct {
        GtkWidget *scale;
        gint32 current_brightness;
        gint32 max_brightness;
        gchar *sysfs_mbn;
        gchar *sysfs_bn;
} BrightnessApplet;

static gint32 sysfs_value_get(const gchar *path)
{
        FILE *fd;
        if ((fd = fopen(path, "r")) == NULL) {
                g_debug("Error in sysfs_value_get for path %s: %s",
                        path, g_strerror(errno));
                return -1;
        }
        gint32 value;
        (void)fscanf(fd, "%d", &value);
        fclose(fd);
        return value;
}

static gboolean sysfs_value_set(const gchar *path, gint32 value)
{
        FILE *fd;
        if ((fd = fopen(path, "w")) == NULL) {
                g_debug ("Error in sysfs_value_set for path %s: %s",
                        path, g_strerror(errno));
                return FALSE;
        }
        fprintf(fd, "%d", value);
        fclose(fd);
        return TRUE;
}

/* Applet destroyed */
static void
brightness_applet_free(BrightnessApplet *applet)
{
        g_free(applet->sysfs_mbn);
        g_free(applet->sysfs_bn);
        g_slice_free(BrightnessApplet, applet);
}

static void
brightness_changed_cb(GtkScaleButton *scale,
                      gdouble val,
                      BrightnessApplet *applet)
{
        gint32 b = (val * applet->max_brightness) / 100;
        if (b != applet->current_brightness) {
                if (sysfs_value_set(applet->sysfs_bn, b)) {
                        applet->current_brightness = b;
                }
        }
}

static gboolean
applet_initialize(BrightnessApplet *applet)
{
        GError *error = NULL;
        gchar *base = g_build_filename("/sys", "class", "backlight", NULL);
        GDir *dir = g_dir_open(base, 0, &error);
        if (dir) {
                const gchar *entry = NULL;
                gchar *path = NULL;
                while ((entry = g_dir_read_name(dir)) != NULL) {
                        path = g_build_filename(base, entry, NULL);

                        if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
                                applet->sysfs_bn = 
                                        g_build_filename(path, 
                                                         "brightness", NULL);
                                applet->sysfs_mbn =
                                        g_build_filename(path, 
                                                         "max_brightness", NULL);

                                if (g_file_test(applet->sysfs_bn, 
                                                G_FILE_TEST_IS_REGULAR) &&
                                    g_file_test(applet->sysfs_mbn, 
                                                G_FILE_TEST_IS_REGULAR)) {

                                        break;
                                } else {
                                        g_free(applet->sysfs_bn);
                                        g_free(applet->sysfs_mbn);
                                        applet->sysfs_bn = NULL;
                                        applet->sysfs_mbn = NULL;
                                }
                        }
                        g_free(path);
                }
                g_dir_close(dir);
        } else {
                g_error("Error opening directory %s", base);
        }
        
        if (applet->sysfs_bn && applet->sysfs_mbn) {
                applet->max_brightness = sysfs_value_get(applet->sysfs_mbn);
                applet->current_brightness = sysfs_value_get(applet->sysfs_bn);
                return TRUE;
        } else {
                return FALSE;
        }
}

G_MODULE_EXPORT GtkWidget *
mb_panel_applet_create(const char *id, GtkOrientation orientation)
{
        BrightnessApplet *applet;
        GtkWidget *scale;
        GtkIconTheme *theme;
        static const gchar *icons[] = {
            "brightness-min",
            "brightness-max",
            "brightness-min",
            "brightness-medium",
            "brightness-max",
            NULL};
            
        theme = gtk_icon_theme_get_default();
        gtk_icon_theme_append_search_path(theme, DATADIR);
            
        applet = g_slice_new(BrightnessApplet);
        scale = gtk_scale_button_new(GTK_ICON_SIZE_BUTTON,
                                     1, 100, 1,
                                     icons);
        applet->scale = scale;
        gtk_widget_set_name(scale, "MatchboxPanelBrightness");

        if (!applet_initialize(applet)) {
            g_error("Error initializing applet");
            brightness_applet_free(applet);
            return NULL;
        }
        gdouble val = (100 * applet->current_brightness) / 
                applet->max_brightness;
        gtk_scale_button_set_value(GTK_SCALE_BUTTON(scale), val);
        g_signal_connect(scale,
                         "value-changed",
                         G_CALLBACK(brightness_changed_cb),
                         applet);
        g_object_weak_ref(G_OBJECT(scale),
                          (GWeakNotify) brightness_applet_free,
                          applet);

        gtk_widget_show(scale);

        return scale;
}