-
Notifications
You must be signed in to change notification settings - Fork 10
/
sysfs-led-vanilla.c
426 lines (362 loc) · 13.9 KB
/
sysfs-led-vanilla.c
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/** @file sysfs-led-vanilla.c
*
* mce-plugin-libhybris - Libhybris plugin for Mode Control Entity
* <p>
* Copyright (C) 2013-2017 Jolla Ltd.
* <p>
* @author Simo Piiroinen <simo.piiroinen@jollamobile.com>
* @author Kimmo Lindholm <kimmo.lindholm@eke.fi>
*
* mce-plugin-libhybris is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
*
* mce-plugin-libhybris 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with mce-plugin-libhybris; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/* ========================================================================= *
* RGB led control: Jolla 1 backend
*
* Three channels, all of which:
* - must have 'brightness' control file
* - must have 'max_brightness' control file or nonzero fixed maximum
* - can have either
* o blink on/off delay control files
* o blink enabled/disable control control file
*
* Assumptions built into code:
*
* - The write() calls to sysfs controls return immediately from kernel to
* userspace, but the kernel side can stay busy with the change for few
* milliseconds -> Frequent intensity changes do not block mce process,
* so sw breathing is feasible. Minimum delay between led state changes
* must be enforced.
*
* - Blink controls for the R, G and B channels are independent. To avoid
* "rainbow patterns" when more than one channel is used the blink enabling
* for all of the channels as simultaneously as possible.
* ========================================================================= */
#include "sysfs-led-vanilla.h"
#include "sysfs-led-util.h"
#include "sysfs-val.h"
#include "plugin-config.h"
#include <stdio.h>
#include <string.h>
#include <glib.h>
/* ========================================================================= *
* PROTOTYPES
* ========================================================================= */
typedef struct
{
const char *max_brightness; // R
const char *brightness; // W
const char *blink_delay_on; // W
const char *blink_delay_off; // W
const char *blink; // W
int max_override;// value to use if max_brightness path is NULL
} led_paths_vanilla_t;
typedef struct
{
sysfsval_t *cached_max_brightness;
sysfsval_t *cached_brightness;
sysfsval_t *cached_blink_delay_on;
sysfsval_t *cached_blink_delay_off;
sysfsval_t *cached_blink;
} led_channel_vanilla_t;
/* ------------------------------------------------------------------------- *
* ONE_CHANNEL
* ------------------------------------------------------------------------- */
static void led_channel_vanilla_init (led_channel_vanilla_t *self);
static void led_channel_vanilla_close (led_channel_vanilla_t *self);
static bool led_channel_vanilla_probe (led_channel_vanilla_t *self, const led_paths_vanilla_t *path);
static void led_channel_vanilla_set_value (led_channel_vanilla_t *self, int value);
static void led_channel_vanilla_set_blink (led_channel_vanilla_t *self, int on_ms, int off_ms);
/* ------------------------------------------------------------------------- *
* ALL_CHANNELS
* ------------------------------------------------------------------------- */
static void led_control_vanilla_blink_cb (void *data, int on_ms, int off_ms);
static void led_control_vanilla_value_cb (void *data, int r, int g, int b);
static void led_control_vanilla_close_cb (void *data);
bool led_control_vanilla_probe (led_control_t *self);
/* ========================================================================= *
* ONE_CHANNEL
* ========================================================================= */
static void
led_channel_vanilla_init(led_channel_vanilla_t *self)
{
self->cached_max_brightness = sysfsval_create();
self->cached_brightness = sysfsval_create();
self->cached_blink_delay_on = sysfsval_create();
self->cached_blink_delay_off = sysfsval_create();
self->cached_blink = sysfsval_create();
}
static void
led_channel_vanilla_close(led_channel_vanilla_t *self)
{
sysfsval_delete(self->cached_max_brightness),
self->cached_max_brightness = 0;
sysfsval_delete(self->cached_brightness),
self->cached_brightness = 0;
sysfsval_delete(self->cached_blink_delay_on),
self->cached_blink_delay_on = 0;
sysfsval_delete(self->cached_blink_delay_off),
self->cached_blink_delay_off = 0;
sysfsval_delete(self->cached_blink),
self->cached_blink = 0;
}
static bool
led_channel_vanilla_probe(led_channel_vanilla_t *self,
const led_paths_vanilla_t *path)
{
bool res = false;
// maximum brightness can be read from file or given in config
if( sysfsval_open_ro(self->cached_max_brightness, path->max_brightness) )
sysfsval_refresh(self->cached_max_brightness);
if( path->max_override > 0 )
sysfsval_assume(self->cached_max_brightness, path->max_override);
if( sysfsval_get(self->cached_max_brightness) <= 0 )
goto cleanup;
// we always must have brightness control
if( !sysfsval_open_rw(self->cached_brightness, path->brightness) )
goto cleanup;
// on/off period controls are optional, but both
// are needed if one is present
if( sysfsval_open_rw(self->cached_blink_delay_on, path->blink_delay_on) ) {
if( !sysfsval_open_rw(self->cached_blink_delay_off, path->blink_delay_off) )
sysfsval_close(self->cached_blink_delay_on);
}
// having "blink" control file is optional
sysfsval_open_rw(self->cached_blink, path->blink);
res = true;
cleanup:
/* Always close the max_brightness file */
sysfsval_close(self->cached_max_brightness);
/* On failure close the other files too */
if( !res ) {
sysfsval_close(self->cached_brightness);
sysfsval_close(self->cached_blink_delay_on);
sysfsval_close(self->cached_blink_delay_off);
sysfsval_close(self->cached_blink);
}
return res;
}
static void
led_channel_vanilla_set_value(led_channel_vanilla_t *self, int value)
{
value = led_util_scale_value(value,
sysfsval_get(self->cached_max_brightness));
sysfsval_set(self->cached_brightness, value);
value = (sysfsval_get(self->cached_blink_delay_on) &&
sysfsval_get(self->cached_blink_delay_off));
sysfsval_set(self->cached_blink, value);
}
static void
led_channel_vanilla_set_blink(led_channel_vanilla_t *self,
int on_ms, int off_ms)
{
/* Note: Blinking config is taken in use when brightness
* sysfs is written to -> we need to invalidate
* cached brightness value if blinking changes
* are made.
*/
sysfsval_set(self->cached_blink_delay_on, on_ms);
sysfsval_set(self->cached_blink_delay_off, off_ms);
sysfsval_invalidate(self->cached_brightness);
/* Note: Blinking enabled/disabled needs to happen
* after the brightness has been set
*/
sysfsval_invalidate(self->cached_blink);
}
/* ========================================================================= *
* ALL_CHANNELS
* ========================================================================= */
#define VANILLA_CHANNELS 3
static void
led_control_vanilla_blink_cb(void *data, int on_ms, int off_ms)
{
led_channel_vanilla_t *channel = data;
led_channel_vanilla_set_blink(channel + 0, on_ms, off_ms);
led_channel_vanilla_set_blink(channel + 1, on_ms, off_ms);
led_channel_vanilla_set_blink(channel + 2, on_ms, off_ms);
}
static void
led_control_vanilla_value_cb(void *data, int r, int g, int b)
{
led_channel_vanilla_t *channel = data;
led_channel_vanilla_set_value(channel + 0, r);
led_channel_vanilla_set_value(channel + 1, g);
led_channel_vanilla_set_value(channel + 2, b);
}
static void
led_control_vanilla_close_cb(void *data)
{
led_channel_vanilla_t *channel = data;
led_channel_vanilla_close(channel + 0);
led_channel_vanilla_close(channel + 1);
led_channel_vanilla_close(channel + 2);
}
static bool
led_control_vanilla_static_probe(led_channel_vanilla_t *channel)
{
/** Sysfs control paths for RGB leds */
static const led_paths_vanilla_t paths[][VANILLA_CHANNELS] =
{
// vanilla
{
{
.blink_delay_on = "/sys/class/leds/led:rgb_red/blink_delay_on",
.blink_delay_off = "/sys/class/leds/led:rgb_red/blink_delay_off",
.brightness = "/sys/class/leds/led:rgb_red/brightness",
.max_brightness = "/sys/class/leds/led:rgb_red/max_brightness",
},
{
.blink_delay_on = "/sys/class/leds/led:rgb_green/blink_delay_on",
.blink_delay_off = "/sys/class/leds/led:rgb_green/blink_delay_off",
.brightness = "/sys/class/leds/led:rgb_green/brightness",
.max_brightness = "/sys/class/leds/led:rgb_green/max_brightness",
},
{
.blink_delay_on = "/sys/class/leds/led:rgb_blue/blink_delay_on",
.blink_delay_off = "/sys/class/leds/led:rgb_blue/blink_delay_off",
.brightness = "/sys/class/leds/led:rgb_blue/brightness",
.max_brightness = "/sys/class/leds/led:rgb_blue/max_brightness",
}
},
// i9300 (galaxy s3 international)
{
{
.blink_delay_on = "/sys/class/leds/led_r/delay_on",
.blink_delay_off = "/sys/class/leds/led_r/delay_off",
.brightness = "/sys/class/leds/led_r/brightness",
.max_brightness = "/sys/class/leds/led_r/max_brightness",
.blink = "/sys/class/leds/led_r/blink",
},
{
.blink_delay_on = "/sys/class/leds/led_g/delay_on",
.blink_delay_off = "/sys/class/leds/led_g/delay_off",
.brightness = "/sys/class/leds/led_g/brightness",
.max_brightness = "/sys/class/leds/led_g/max_brightness",
.blink = "/sys/class/leds/led_g/blink",
},
{
.blink_delay_on = "/sys/class/leds/led_b/delay_on",
.blink_delay_off = "/sys/class/leds/led_b/delay_off",
.brightness = "/sys/class/leds/led_b/brightness",
.max_brightness = "/sys/class/leds/led_b/max_brightness",
.blink = "/sys/class/leds/led_b/blink",
}
},
// yuga (sony xperia z)
{
{
.brightness = "/sys/class/leds/lm3533-red/brightness",
.max_override = 255,
},
{
.brightness = "/sys/class/leds/lm3533-green/brightness",
.max_override = 255,
},
{
.brightness = "/sys/class/leds/lm3533-blue/brightness",
.max_override = 255,
},
},
// onyx (OnePlus X)
{
{
.brightness = "/sys/class/leds/red/brightness",
.max_brightness = "/sys/class/leds/red/max_brightness",
.blink_delay_on = "/sys/class/leds/red/pause_hi",
.blink_delay_off = "/sys/class/leds/red/pause_lo",
.blink = "/sys/class/leds/red/blink",
},
{
.brightness = "/sys/class/leds/green/brightness",
.max_brightness = "/sys/class/leds/green/max_brightness",
.blink_delay_on = "/sys/class/leds/green/pause_hi",
.blink_delay_off = "/sys/class/leds/green/pause_lo",
.blink = "/sys/class/leds/green/blink",
},
{
.brightness = "/sys/class/leds/blue/brightness",
.max_brightness = "/sys/class/leds/blue/max_brightness",
.blink_delay_on = "/sys/class/leds/blue/pause_hi",
.blink_delay_off = "/sys/class/leds/blue/pause_lo",
.blink = "/sys/class/leds/blue/blink",
},
},
};
bool ack = false;
for( size_t i = 0; i < G_N_ELEMENTS(paths); ++i ) {
if( led_channel_vanilla_probe(&channel[0], &paths[i][0]) &&
led_channel_vanilla_probe(&channel[1], &paths[i][1]) &&
led_channel_vanilla_probe(&channel[2], &paths[i][2]) ) {
ack = true;
break;
}
}
return ack;
}
static bool
led_control_vanilla_dynamic_probe(led_channel_vanilla_t *channel)
{
static const objconf_t vanilla_conf[] =
{
OBJCONF_FILE(led_paths_vanilla_t, brightness, Brightness),
OBJCONF_FILE(led_paths_vanilla_t, max_brightness, MaxBrightness),
OBJCONF_FILE(led_paths_vanilla_t, blink_delay_on, BlinkDelayOn),
OBJCONF_FILE(led_paths_vanilla_t, blink_delay_off, BlinkDelayOff),
OBJCONF_FILE(led_paths_vanilla_t, blink, Blink),
OBJCONF_STOP
};
static const char * const pfix[VANILLA_CHANNELS] =
{
"Red", "Green", "Blue"
};
bool ack = false;
led_paths_vanilla_t paths[VANILLA_CHANNELS];
memset(paths, 0, sizeof paths);
for( size_t i = 0; i < VANILLA_CHANNELS; ++i )
objconf_init(vanilla_conf, &paths[i]);
for( size_t i = 0; i < VANILLA_CHANNELS; ++i )
{
if( !objconf_parse(vanilla_conf, &paths[i], pfix[i]) )
goto cleanup;
if( !led_channel_vanilla_probe(channel+i, &paths[i]) )
goto cleanup;
}
ack = true;
cleanup:
for( size_t i = 0; i < VANILLA_CHANNELS; ++i )
objconf_quit(vanilla_conf, &paths[i]);
return ack;
}
bool
led_control_vanilla_probe(led_control_t *self)
{
static led_channel_vanilla_t channel[VANILLA_CHANNELS];
bool res = false;
led_channel_vanilla_init(channel+0);
led_channel_vanilla_init(channel+1);
led_channel_vanilla_init(channel+2);
self->name = "vanilla";
self->data = channel;
self->enable = 0;
self->blink = led_control_vanilla_blink_cb;
self->value = led_control_vanilla_value_cb;
self->close = led_control_vanilla_close_cb;
if( self->use_config )
res = led_control_vanilla_dynamic_probe(channel);
if( !res )
res = led_control_vanilla_static_probe(channel);
if( !res )
led_control_close(self);
return res;
}