forked from mer-hybris/mce-plugin-libhybris
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hybris-lights.c
466 lines (360 loc) · 11.9 KB
/
hybris-lights.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/** @file hybris-lights.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>
*
* 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
*/
#include "hybris-lights.h"
#include "plugin-logging.h"
#include "plugin-api.h"
#include <system/window.h>
#include <hardware/lights.h>
/* ========================================================================= *
* PROTOTYPES
* ========================================================================= */
/* ------------------------------------------------------------------------- *
* UTILITIES
* ------------------------------------------------------------------------- */
static int clamp_to_range (int lo, int hi, int val);
/* ------------------------------------------------------------------------- *
* LIGHTS_PLUGIN
* ------------------------------------------------------------------------- */
bool hybris_plugin_lights_load (void);
void hybris_plugin_lights_unload (void);
static int hybris_plugin_lights_open_device (const char *id, struct light_device_t **pdevice);
static void hybris_plugin_lights_close_device (struct light_device_t **pdevice);
/* ------------------------------------------------------------------------- *
* DISPLAY_BACKLIGHT
* ------------------------------------------------------------------------- */
bool hybris_device_backlight_init (void);
void hybris_device_backlight_quit (void);
bool hybris_device_backlight_set_brightness(int level);
/* ------------------------------------------------------------------------- *
* KEYBOARD_BACKLIGHT
* ------------------------------------------------------------------------- */
bool hybris_device_keypad_init (void);
void hybris_device_keypad_quit (void);
bool hybris_device_keypad_set_brightness (int level);
/* ------------------------------------------------------------------------- *
* INDICATOR_LED
* ------------------------------------------------------------------------- */
bool hybris_device_indicator_init (void);
void hybris_device_indicator_quit (void);
bool hybris_device_indicator_set_pattern (int r, int g, int b, int ms_on, int ms_off);
/* ========================================================================= *
* UTILITIES
* ========================================================================= */
/** Clamp integer values to given range
*
* @param lo minimum value allowed
* @param hi maximum value allowed
* @param val value to clamp
*
* @return val clamped to [lo, hi]
*/
static int
clamp_to_range(int lo, int hi, int val)
{
return val <= lo ? lo : val <= hi ? val : hi;
}
/* ========================================================================= *
* LIGHTS_PLUGIN
* ========================================================================= */
/** Handle for libhybris lights plugin */
static const struct hw_module_t *hybris_plugin_lights_handle = 0;
/** Load libhybris lights plugin
*
* @return true on success, false on failure
*/
bool
hybris_plugin_lights_load(void)
{
static bool done = false;
if( done ) {
goto cleanup;
}
done = true;
hw_get_module(LIGHTS_HARDWARE_MODULE_ID, &hybris_plugin_lights_handle);
if( !hybris_plugin_lights_handle ) {
mce_log(LL_WARN, "failed to open lights module");
goto cleanup;
}
mce_log(LL_DEBUG, "hybris_plugin_lights_handle = %p",
hybris_plugin_lights_handle);
cleanup:
return hybris_plugin_lights_handle != 0;
}
/** Unload libhybris lights plugin
*/
void
hybris_plugin_lights_unload(void)
{
/* cleanup dependencies */
hybris_device_backlight_quit();
hybris_device_keypad_quit();
hybris_device_indicator_quit();
/* actually unload the module */
// FIXME: how to unload libhybris modules?
}
/** Convenience function for opening a light device
*
* Similar to what we might or might not have available from hardware/lights.h
*/
static int
hybris_plugin_lights_open_device(const char *id, struct light_device_t **pdevice)
{
int ack = false;
if( !id || !pdevice ) {
goto cleanup;
}
if( !hybris_plugin_lights_load() ) {
goto cleanup;
}
const struct hw_module_t *module = hybris_plugin_lights_handle;
if( !module->methods->open(module, id, (struct hw_device_t**)pdevice) ) {
goto cleanup;
}
ack = true;
cleanup:
return ack;
}
/** Convenience function for closing a light device
*
* Similar to what we might or might not have available from hardware/lights.h
*/
static void
hybris_plugin_lights_close_device(struct light_device_t **pdevice)
{
struct light_device_t *device;
if( (device = *pdevice) ) {
*pdevice = 0, device->common.close(&device->common);
}
}
/* ========================================================================= *
* DISPLAY_BACKLIGHT
* ========================================================================= */
/** Pointer to libhybris frame display backlight device object */
static struct light_device_t *hybris_device_backlight_handle = 0;
/** Initialize libhybris display backlight device object
*
* @return true on success, false on failure
*/
bool
hybris_device_backlight_init(void)
{
static bool done = false;
if( done ) {
goto cleanup;
}
done = true;
hybris_plugin_lights_open_device(LIGHT_ID_BACKLIGHT,
&hybris_device_backlight_handle);
if( !hybris_device_backlight_handle ) {
mce_log(LL_WARN, "failed to open backlight device");
goto cleanup;
}
mce_log(LL_DEBUG, "hybris_device_backlight_handle -> %p",
hybris_device_backlight_handle);
cleanup:
return hybris_device_backlight_handle != 0;
}
/** Release libhybris display backlight device object
*/
void
hybris_device_backlight_quit(void)
{
hybris_plugin_lights_close_device(&hybris_device_backlight_handle);
}
/** Set display backlight brightness via libhybris
*
* @param level 0=off ... 255=maximum brightness
*
* @return true on success, false on failure
*/
bool
hybris_device_backlight_set_brightness(int level)
{
bool ack = false;
if( !hybris_device_backlight_init() ) {
goto cleanup;
}
unsigned lev = clamp_to_range(0, 255, level);
struct light_state_t lst;
memset(&lst, 0, sizeof lst);
lst.color = (0xff << 24) | (lev << 16) | (lev << 8) | (lev << 0);
lst.flashMode = LIGHT_FLASH_NONE;
lst.flashOnMS = 0;
lst.flashOffMS = 0;
lst.brightnessMode = BRIGHTNESS_MODE_USER;
if( hybris_device_backlight_handle->set_light(hybris_device_backlight_handle, &lst) < 0 ) {
goto cleanup;
}
ack = true;
cleanup:
mce_log(LL_DEBUG, "brightness(%d) -> %s",
level, ack ? "success" : "failure");
return ack;
}
/* ========================================================================= *
* KEYBOARD_BACKLIGHT
* ========================================================================= */
/** Pointer to libhybris frame keypad backlight device object */
static struct light_device_t *hybris_device_keypad_handle = 0;
/** Initialize libhybris keypad backlight device object
*
* @return true on success, false on failure
*/
bool
hybris_device_keypad_init(void)
{
static bool done = false;
if( done ) {
goto cleanup;
}
done = true;
hybris_plugin_lights_open_device(LIGHT_ID_KEYBOARD, &hybris_device_keypad_handle);
if( !hybris_device_keypad_handle ) {
mce_log(LL_WARN, "failed to open keypad backlight device");
goto cleanup;
}
mce_log(LL_DEBUG, "hybris_device_keypad_handle -> %p",
hybris_device_keypad_handle);
cleanup:
return hybris_device_keypad_handle != 0;
}
/** Release libhybris keypad backlight device object
*/
void
hybris_device_keypad_quit(void)
{
hybris_plugin_lights_close_device(&hybris_device_keypad_handle);
}
/** Set display keypad brightness via libhybris
*
* @param level 0=off ... 255=maximum brightness
*
* @return true on success, false on failure
*/
bool
hybris_device_keypad_set_brightness(int level)
{
bool ack = false;
if( !hybris_device_keypad_init() ) {
goto cleanup;
}
unsigned lev = clamp_to_range(0, 255, level);
struct light_state_t lst;
memset(&lst, 0, sizeof lst);
lst.color = (0xff << 24) | (lev << 16) | (lev << 8) | (lev << 0);
lst.flashMode = LIGHT_FLASH_NONE;
lst.flashOnMS = 0;
lst.flashOffMS = 0;
lst.brightnessMode = BRIGHTNESS_MODE_USER;
if( hybris_device_keypad_handle->set_light(hybris_device_keypad_handle, &lst) < 0 ) {
goto cleanup;
}
ack = true;
cleanup:
mce_log(LL_DEBUG, "brightness(%d) -> %s",
level, ack ? "success" : "failure");
return ack;
}
/* ========================================================================= *
* INDICATOR_LED
* ========================================================================= */
/** Pointer to libhybris frame indicator led device object */
static struct light_device_t *hybris_device_indicator_handle = 0;
/** Initialize libhybris indicator led device object
*
* @return true on success, false on failure
*/
bool
hybris_device_indicator_init(void)
{
static bool done = false;
if( done ) {
goto cleanup;
}
done = true;
hybris_plugin_lights_open_device(LIGHT_ID_NOTIFICATIONS,
&hybris_device_indicator_handle);
if( !hybris_device_indicator_handle ) {
mce_log(LL_WARN, "failed to open indicator led device");
goto cleanup;
}
mce_log(LL_DEBUG, "hybris_device_indicator_handle -> %p",
hybris_device_indicator_handle);
cleanup:
return hybris_device_indicator_handle != 0;
}
/** Release libhybris indicator led device object
*/
void
hybris_device_indicator_quit(void)
{
hybris_plugin_lights_close_device(&hybris_device_indicator_handle);
}
/** Set indicator led pattern via libhybris
*
* @param r red intensity 0 ... 255
* @param g green intensity 0 ... 255
* @param b blue intensity 0 ... 255
* @param ms_on milliseconds to keep the led on, or 0 for no flashing
* @param ms_on milliseconds to keep the led off, or 0 for no flashing
*
* @return true on success, false on failure
*/
bool
hybris_device_indicator_set_pattern(int r, int g, int b, int ms_on, int ms_off)
{
bool ack = false;
if( !hybris_device_indicator_init() ) {
goto cleanup;
}
r = clamp_to_range(0, 255, r);
g = clamp_to_range(0, 255, g);
b = clamp_to_range(0, 255, b);
ms_on = clamp_to_range(0, 60000, ms_on);
ms_off = clamp_to_range(0, 60000, ms_off);
if( ms_on < 50 || ms_off < 50 ) {
ms_on = ms_off = 0;
}
struct light_state_t lst;
memset(&lst, 0, sizeof lst);
lst.color = (0xff << 24) | (r << 16) | (g << 8) | (b << 0);
lst.brightnessMode = BRIGHTNESS_MODE_USER;
if( ms_on > 0 && ms_off > 0 ) {
lst.flashMode = LIGHT_FLASH_HARDWARE;
lst.flashOnMS = ms_on;
lst.flashOffMS = ms_off;
}
else {
lst.flashMode = LIGHT_FLASH_NONE;
lst.flashOnMS = 0;
lst.flashOffMS = 0;
}
if( hybris_device_indicator_handle->set_light(hybris_device_indicator_handle, &lst) < 0 ) {
goto cleanup;
}
ack = true;
cleanup:
mce_log(LL_DEBUG, "pattern(%d,%d,%d,%d,%d) -> %s",
r,g,b, ms_on, ms_off , ack ? "success" : "failure");
return ack;
}