summaryrefslogtreecommitdiff
path: root/plugins/iot/display/auto-brightness.c
blob: 88ff66a5ecb652577289c7e2ac46a5f3ff657b6c (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
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/*
 * deviced
 *
 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include <stdbool.h>
#include <stdio.h>
#include <math.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <vconf.h>
#include <sensor.h>


#include "util.h"
#include "core.h"
#include "display-ops.h"
#include "device-node.h"
#include "setting.h"
#include "core/device-notifier.h"
#include "core/config-parser.h"

#define METHOD_CHECK_SUPPORTED	"CheckSupported"

#define DISP_FORCE_SHIFT	12
#define DISP_FORCE_CMD(prop, force)	(((force) << DISP_FORCE_SHIFT) | prop)

#define SAMPLING_INTERVAL	1	/* 1 sec */
#define MAX_SAMPLING_COUNT	3
#define MAX_FAULT		5
#define DEFAULT_AUTOMATIC_BRT	5
#define AUTOMATIC_DEVIDE_VAL	10
#define AUTOMATIC_DELAY_TIME	500	/* 0.5 sec */

#define RADIAN_VALUE		(57.2957)
#define ROTATION_90		90
#define WORKING_ANGLE_MIN	0
#define WORKING_ANGLE_MAX	20

#define BOARD_CONF_FILE "/etc/deviced/display.conf"

#define ON_LUX		-1
#define OFF_LUX		-1
#define ON_COUNT	1
#define OFF_COUNT	1

static int (*_default_action) (int);
static guint alc_timeout_id = 0;
static guint update_timeout;
static sensor_listener_h light_listener;
static sensor_listener_h accel_listener;
static int fault_count;
static int automatic_brt = DEFAULT_AUTOMATIC_BRT;
static int min_brightness = PM_MIN_BRIGHTNESS;
static char *min_brightness_name = 0;

/* light sensor */
static float lmax, lmin;

static bool update_working_position(void)
{
	sensor_event_s data;
	int ret;
	float x, y, z, pitch, realg;

	if (!display_conf.accel_sensor_on)
		return false;

	ret = sensor_listener_read_data(accel_listener, &data);
	if (ret != SENSOR_ERROR_NONE) {
		_E("Failed to get accelerometer data: %d", ret);
		return true;
	}

	x = data.values[0];
	y = data.values[1];
	z = data.values[2];

	realg = (float)sqrt((x * x) + (y * y) + (z * z));
	pitch = ROTATION_90 - abs((int) (asin(z / realg) * RADIAN_VALUE));

	_D("Accel data x=%f y=%f z=%f pitch=%f", x, y, z, pitch);

	if (pitch >= WORKING_ANGLE_MIN && pitch <= WORKING_ANGLE_MAX)
		return true;
	return false;
}

static void alc_set_brightness(int setting, int value, float light)
{
	static float old;
	int position, tmp_value = 0, ret;

	ret = backlight_ops.get_brightness(&tmp_value);
	if (ret < 0) {
		_E("Failed to get display brightness.");
		return;
	}

	if (value < min_brightness)
		value = min_brightness;

	if (tmp_value != value) {
		if (!setting && min_brightness == PM_MIN_BRIGHTNESS &&
		    display_conf.accel_sensor_on == true) {
			position = update_working_position();
			if (!position && (old > light)) {
				_D("It's not working position, "
				    "LCD isn't getting dark.");
				return;
			}
		}
		int diff, step;

		diff = value - tmp_value;
		if (abs(diff) < display_conf.brightness_change_step)
			step = (diff > 0 ? 1 : -1);
		else
			step = (int)ceil(diff /
			    (float)display_conf.brightness_change_step);

		_D("%d", step);
		while (tmp_value != value) {
			if (step == 0) break;

			tmp_value += step;
			if ((step > 0 && tmp_value > value) ||
			    (step < 0 && tmp_value < value))
				tmp_value = value;

			backlight_ops.set_default_brt(tmp_value);
			backlight_ops.update();
		}
		_I("Load light data(%f). auto brt=%d min brightness=%d "
		    "brightness=%d", light, automatic_brt, min_brightness, value);
		old = light;
	}
}

static bool check_brightness_changed(int value)
{
	int i;
	static int values[MAX_SAMPLING_COUNT], count = 0;

	if (count >= MAX_SAMPLING_COUNT || count < 0)
		count = 0;

	values[count++] = value;

	for (i = 0; i < MAX_SAMPLING_COUNT - 1; i++)
		if (values[i] != values[i+1])
			return false;
	return true;
}

static bool alc_update_brt(bool setting)
{
	int value = 0;
	int ret = -1;
	sensor_event_s light_data;
	int index;
	float light;

	ret = sensor_listener_read_data(light_listener, &light_data);
	if (ret != SENSOR_ERROR_NONE) {
		_E("Failed to read light sensor data: %d", ret);
		goto out;
	}

	index = light_data.value_count - 1;
	if (index < 0 || light_data.values[index] < 0) {
		_E("Invalid light sensor data.");
		goto out;
	}

	light = light_data.values[index];
	ret = backlight_ops.get_brightness_by_light_sensor(
			lmax, lmin, light, &value);
	if (ret == -ENOTSUP) {
		_E("Not supported to handle the light data.");
		goto out;
	}

	if (ret < 0 || value < PM_MIN_BRIGHTNESS || value > PM_MAX_BRIGHTNESS) {
		_E("Failed to load light data. light=%f value=%d: %d", light, value, ret);
		goto out;
	}

	fault_count = 0;

	if (display_conf.continuous_sampling &&
		!check_brightness_changed(value) &&
		!setting)
		return true;

	alc_set_brightness(setting, value, light);

	return true;

out:
	fault_count++;

	if ((fault_count > MAX_FAULT) && !(pm_status_flag & PWROFF_FLAG)) {
		if (alc_timeout_id) {
			g_source_remove(alc_timeout_id);
			alc_timeout_id = 0;
		}
		ret = vconf_set_int(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT,
		    SETTING_BRIGHTNESS_AUTOMATIC_OFF);
		if (ret < 0)
			_E("Failed to set vconf value for automatic brightness: %d", vconf_get_ext_errno());
		_E("Fault counts is over %d, disable automatic brightness.", MAX_FAULT);
		return false;
	}
	return true;
}

static gboolean alc_handler(void *data)
{
	if (pm_cur_state != S_NORMAL) {
		if (alc_timeout_id > 0)
			g_source_remove(alc_timeout_id);
		alc_timeout_id = 0;
		return G_SOURCE_REMOVE;
	}

	if (!alc_update_brt(false))
		return G_SOURCE_REMOVE;

	if (alc_timeout_id != 0)
		return G_SOURCE_CONTINUE;

	return G_SOURCE_REMOVE;
}

static int alc_action(int timeout)
{
	/* sampling timer add */
	if (alc_timeout_id == 0 && !(pm_status_flag & PWRSV_FLAG)) {
		display_info.update_auto_brightness(true);

		alc_timeout_id =
		    g_timeout_add_seconds(display_conf.lightsensor_interval,
			    alc_handler, NULL);
	}

	if (_default_action != NULL)
		return _default_action(timeout);

	/* unreachable code */
	return -1;
}

static int connect_sensor(void)
{
	int ret;
	sensor_h sensor;
	sensor_h *list = NULL;
	int cnt = 0;

	_I("Connect with sensor fw.");
	/* light sensor */
	ret = sensor_get_sensor_list(SENSOR_LIGHT, &list, &cnt);
	if (ret != SENSOR_ERROR_NONE) {
		_E("Failed to get light sensor list: %d", ret);
		goto error;
	}

	/* TODO
	 * Sensor apis will be fixed
	 * to provide which sensor is effective among sensors */
	if (cnt == 3) /* three light sensors exist */
		sensor = list[2];
	else
		sensor = list[0];

	free(list);

	ret = sensor_get_min_range(sensor, &lmin);
	if (ret != SENSOR_ERROR_NONE) {
		_E("Failed to get light sensor min range: %d", ret);
		goto error;
	}
	ret = sensor_get_max_range(sensor, &lmax);
	if (ret != SENSOR_ERROR_NONE) {
		_E("Failed to get light sensor max range: %d", ret);
		goto error;
	}
	_I("Light sensor min=%f max=%f", lmin, lmax);

	ret = sensor_create_listener(sensor, &light_listener);
	if (ret != SENSOR_ERROR_NONE) {
		_E("Failed to create listener(light).");
		goto error;
	}
	sensor_listener_set_option(light_listener, SENSOR_OPTION_ALWAYS_ON);
	ret = sensor_listener_start(light_listener);
	if (ret != SENSOR_ERROR_NONE) {
		_E("Failed to start light sensor.");
		sensor_destroy_listener(light_listener);
		light_listener = 0;
		goto error;
	}

	if (!display_conf.accel_sensor_on)
		goto success;

	/* accelerometer sensor */
	ret = sensor_get_default_sensor(SENSOR_ACCELEROMETER, &sensor);
	if (ret != SENSOR_ERROR_NONE) {
		_E("Failed to get default accel sensor.");
		goto error;
	}
	ret = sensor_create_listener(&sensor, &accel_listener);
	if (ret != SENSOR_ERROR_NONE) {
		_E("Failed to create listener(accel).");
		goto error;
	}
	sensor_listener_set_option(accel_listener, SENSOR_OPTION_ALWAYS_ON);
	ret = sensor_listener_start(accel_listener);
	if (ret != SENSOR_ERROR_NONE) {
		_E("Failed to start accel sensor.");
		sensor_destroy_listener(accel_listener);
		accel_listener = 0;
		goto error;
	}

success:
	fault_count = 0;
	return 0;

error:
	if (light_listener > 0) {
		sensor_listener_stop(light_listener);
		sensor_destroy_listener(light_listener);
		light_listener = 0;
	}
	if (display_conf.accel_sensor_on && accel_listener > 0) {
		sensor_listener_stop(accel_listener);
		sensor_destroy_listener(accel_listener);
		accel_listener = 0;
	}
	return -EIO;
}

static int disconnect_sensor(void)
{
	_I("Disconnect with sensor fw.");
	/* light sensor*/
	if (light_listener > 0) {
		sensor_listener_stop(light_listener);
		sensor_destroy_listener(light_listener);
		light_listener = 0;
	}

	/* accelerometer sensor*/
	if (display_conf.accel_sensor_on && accel_listener > 0) {
		sensor_listener_stop(accel_listener);
		sensor_destroy_listener(accel_listener);
		accel_listener = 0;
	}

	if (_default_action != NULL) {
		states[S_NORMAL].action = _default_action;
		_default_action = NULL;
	}
	if (alc_timeout_id > 0) {
		g_source_remove(alc_timeout_id);
		alc_timeout_id = 0;
	}

	return 0;
}

void set_brightness_changed_state(void)
{
	if (pm_status_flag & PWRSV_FLAG) {
		pm_status_flag |= BRTCH_FLAG;
		_D("Brightness changed in low battery,"
		    "escape dim state (light).");
	}
}

static int set_autobrightness_state(int status)
{
	int ret = -1;
	int brt = -1;
	int default_brt = -1;

	if (status == SETTING_BRIGHTNESS_AUTOMATIC_ON) {
		if (connect_sensor() < 0)
			return -1;

		/* escape dim state if it's in low battery.*/
		set_brightness_changed_state();

		/* change alc action func */
		if (_default_action == NULL)
			_default_action = states[S_NORMAL].action;
		states[S_NORMAL].action = alc_action;

		display_info.update_auto_brightness(true);

		alc_timeout_id =
		    g_timeout_add_seconds(display_conf.lightsensor_interval,
			    alc_handler, NULL);
	} else if (status == SETTING_BRIGHTNESS_AUTOMATIC_PAUSE) {
		_I("Auto brightness paused.");
		disconnect_sensor();
	} else {
		disconnect_sensor();
		/* escape dim state if it's in low battery.*/
		set_brightness_changed_state();

		ret = get_setting_brightness(&default_brt);
		if (ret != 0 || (default_brt < PM_MIN_BRIGHTNESS || default_brt > PM_MAX_BRIGHTNESS)) {
			_I("Failed to read vconf value for brightness.");
			brt = PM_DEFAULT_BRIGHTNESS;
			if (default_brt < PM_MIN_BRIGHTNESS || default_brt > PM_MAX_BRIGHTNESS) {
				ret = vconf_set_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, brt);
				if (ret < 0)
					_E("Failed to set vconf value for lcd brightness: %d", vconf_get_ext_errno());
			}
			default_brt = brt;
		}

		backlight_ops.set_default_brt(default_brt);
		backlight_ops.update();
	}

	return 0;
}

static void set_alc_function(keynode_t *key_nodes, void *data)
{
	int status, ret;

	if (key_nodes == NULL) {
		_E("Wrong parameter, key_nodes is null.");
		return;
	}

	status = vconf_keynode_get_int(key_nodes);

	switch (status) {
	case SETTING_BRIGHTNESS_AUTOMATIC_OFF:
	case SETTING_BRIGHTNESS_AUTOMATIC_ON:
	case SETTING_BRIGHTNESS_AUTOMATIC_PAUSE:
		ret = set_autobrightness_state(status);
		_D("Set auto brightness: %d", ret);
		break;
	default:
		_E("Invalid value(%d)", status);
	}
}

static void set_alc_automatic_brt(keynode_t *key_nodes, void *data)
{
	if (key_nodes == NULL) {
		_E("Wrong parameter, key_nodes is null.");
		return;
	}
	automatic_brt = vconf_keynode_get_int(key_nodes) / AUTOMATIC_DEVIDE_VAL;
	_D("Automatic brt(%d)", automatic_brt);

	alc_update_brt(true);
}

static gboolean update_handler(void *data)
{
	int ret, on;

	update_timeout = 0;

	if (pm_cur_state != S_NORMAL)
		return G_SOURCE_REMOVE;

	ret = vconf_get_int(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, &on);
	if (ret < 0 || on != SETTING_BRIGHTNESS_AUTOMATIC_ON) {
		if (ret < 0)
			_E("Failed to get vconf value for automatic brightness: %d", vconf_get_ext_errno());
		return G_SOURCE_REMOVE;
	}

	_D("Auto brightness is working.");
	alc_update_brt(true);

	return G_SOURCE_REMOVE;
}

static void update_auto_brightness(bool update)
{
	if (update_timeout) {
		g_source_remove(update_timeout);
		update_timeout = 0;
	}

	if (update) {
		update_timeout = g_timeout_add(AUTOMATIC_DELAY_TIME,
		    update_handler, NULL);
	}
}

static int prepare_lsensor(void *data)
{
	int status, ret;
	int brt = -1;

	ret = vconf_get_int(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, &status);
	if (ret == 0 && status == SETTING_BRIGHTNESS_AUTOMATIC_ON)
		set_autobrightness_state(status);
	else if (ret < 0)
		_E("Failed to get vconf value for automatic brightness: %d", vconf_get_ext_errno());

	/* add auto_brt_setting change handler */
	vconf_notify_key_changed(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT,
				 set_alc_function, NULL);

	ret = vconf_get_int(VCONFKEY_SETAPPL_LCD_AUTOMATIC_BRIGHTNESS, &brt);
	if (ret < 0)
		_E("Failed to get vconf value for automatic lcd brightness: %d", vconf_get_ext_errno());

	if (brt < PM_MIN_BRIGHTNESS || brt > PM_MAX_BRIGHTNESS) {
		_E("Failed to get automatic brightness.");
	} else {
		automatic_brt = brt / AUTOMATIC_DEVIDE_VAL;
		_I("Automatic brt(%d) init success.", automatic_brt);
	}

	vconf_notify_key_changed(VCONFKEY_SETAPPL_LCD_AUTOMATIC_BRIGHTNESS,
				set_alc_automatic_brt, NULL);

	return 0;
}

static void update_brightness_direct(void)
{
	int ret, status;

	ret = vconf_get_int(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, &status);
	if (ret == 0 && status == SETTING_BRIGHTNESS_AUTOMATIC_ON)
		alc_update_brt(true);
	else if (ret < 0)
		_E("Failed to get vconf value for automatic brightness: %d", vconf_get_ext_errno());
}

static int set_autobrightness_min(int val, char *name)
{
	if (!name)
		return -EINVAL;

	if (val < PM_MIN_BRIGHTNESS || val > PM_MAX_BRIGHTNESS)
		return -EINVAL;

	min_brightness = val;

	if (min_brightness_name) {
		free(min_brightness_name);
		min_brightness_name = 0;
	}
	min_brightness_name = strndup(name, strlen(name));

	update_brightness_direct();

	_I("Auto brightness min value changed. min_brightness=%d min_brightness_name=%s",
	    min_brightness, min_brightness_name);

	return 0;
}

static void reset_autobrightness_min(GDBusConnection *conn,
	const gchar     *sender,
	const gchar     *unique_name,
	gpointer         data)
{
	if (!sender)
		return;

	if (!min_brightness_name)
		return;

	if (strcmp(sender, min_brightness_name))
		return;

	_I("Change to default min brightness. before=%d changed=%d brightness_name=%s", min_brightness,
	    PM_MIN_BRIGHTNESS, min_brightness_name);
	min_brightness = PM_MIN_BRIGHTNESS;
	if (min_brightness_name) {
		free(min_brightness_name);
		min_brightness_name = 0;
	}

	update_brightness_direct();
}

static int lcd_changed_cb(void *data)
{
	int lcd_state;

	if (!data)
		return 0;
	lcd_state = *(int *)data;
	if (lcd_state == S_LCDOFF && alc_timeout_id > 0) {
		g_source_remove(alc_timeout_id);
		alc_timeout_id = 0;
	}

	return 0;
}

static int booting_done_cb(void *data)
{
	int state;

	if (!data)
		return 0;

	state = *(int *)data;
	if (state != true)
		return 0;

	/* get light data from sensor fw */
	prepare_lsensor(NULL);

	return 0;
}

static void exit_lsensor(void)
{
	vconf_ignore_key_changed(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT,
	    set_alc_function);

	vconf_ignore_key_changed(VCONFKEY_SETAPPL_LCD_AUTOMATIC_BRIGHTNESS,
	    set_alc_automatic_brt);

	set_autobrightness_state(SETTING_BRIGHTNESS_AUTOMATIC_OFF);
}

static void auto_brightness_init(void *data)
{
	display_info.update_auto_brightness = update_auto_brightness;
	display_info.set_autobrightness_min = set_autobrightness_min;
	display_info.reset_autobrightness_min = reset_autobrightness_min;

	register_notifier(DEVICE_NOTIFIER_LCD, lcd_changed_cb);
	register_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done_cb);
}

static void auto_brightness_exit(void *data)
{
	exit_lsensor();

	unregister_notifier(DEVICE_NOTIFIER_LCD, lcd_changed_cb);
	unregister_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done_cb);
}

static const struct display_ops display_autobrightness_ops = {
	.name     = "auto-brightness",
	.init     = auto_brightness_init,
	.exit     = auto_brightness_exit,
};

DISPLAY_OPS_REGISTER(&display_autobrightness_ops)