Remove trivial unnecessary build dependency
[apps/core/preloaded/lockscreen.git] / src / display.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <device/display.h>
18 #include <device/callback.h>
19 #include <Ecore.h>
20
21 #include "display.h"
22 #include "log.h"
23
24 #define LOCK_LCD_OFF_TIMEOUT_TIME 10
25
26 static Ecore_Timer *lcd_off_timer;
27 int LOCKSCREEN_EVENT_DISPLAY_STATUS_CHANGED;
28 static int init_count;
29 static int display_off;
30
31 static Eina_Bool _time_elapsed(void *data)
32 {
33         int ret = device_display_change_state(DISPLAY_STATE_SCREEN_OFF);
34         if (ret != DEVICE_ERROR_NONE) {
35                 ERR("device_display_change_state failed: %s", get_error_message(ret));
36         }
37
38         lcd_off_timer = NULL;
39         return ECORE_CALLBACK_CANCEL;
40 }
41
42 static void _timer_reset(void)
43 {
44         if (lcd_off_timer) {
45                 ecore_timer_reset(lcd_off_timer);
46         } else {
47                 lcd_off_timer = ecore_timer_add(LOCK_LCD_OFF_TIMEOUT_TIME, _time_elapsed, NULL);
48         }
49 }
50
51 static void _display_status_changed(device_callback_e type, void *value, void *user_data)
52 {
53         if (type != DEVICE_CALLBACK_DISPLAY_STATE)
54                 return;
55
56         display_state_e state = (display_state_e)value;
57
58         switch (state) {
59                 case DISPLAY_STATE_NORMAL:
60                 case DISPLAY_STATE_SCREEN_DIM:
61                         INF("Display on");
62                         _timer_reset();
63                         display_off = false;
64                 break;
65                 case DISPLAY_STATE_SCREEN_OFF:
66                         INF("Display off");
67                         display_off = true;
68                 break;
69         }
70
71         ecore_event_add(LOCKSCREEN_EVENT_DISPLAY_STATUS_CHANGED, NULL, NULL, NULL);
72 }
73
74 int lockscreen_display_init(void)
75 {
76         display_state_e state;
77
78         if (!init_count) {
79                 LOCKSCREEN_EVENT_DISPLAY_STATUS_CHANGED = ecore_event_type_new();
80                 int ret = device_add_callback(DEVICE_CALLBACK_DISPLAY_STATE, _display_status_changed, NULL);
81                 if (ret != DEVICE_ERROR_NONE) {
82                         ERR("device_add_callback failed: %s", get_error_message(ret));
83                         return 1;
84                 }
85                 ret = device_display_get_state(&state);
86                 if (ret != DEVICE_ERROR_NONE) {
87                         ERR("device_display_get_state failed: %s", get_error_message(ret));
88                         device_remove_callback(DEVICE_CALLBACK_DISPLAY_STATE, _display_status_changed);
89                         return 1;
90                 }
91
92                 switch (state) {
93                         case DISPLAY_STATE_NORMAL:
94                         case DISPLAY_STATE_SCREEN_DIM:
95                                 display_off = false;
96                                 break;
97                         case DISPLAY_STATE_SCREEN_OFF:
98                                 display_off = true;
99                                 break;
100                 }
101
102                 _timer_reset();
103         }
104
105         init_count++;
106         return 0;
107 }
108
109 void lockscreen_display_shutdown(void)
110 {
111         if (init_count) {
112                 init_count--;
113
114                 if (!init_count) {
115                         if (lcd_off_timer) ecore_timer_del(lcd_off_timer);
116                         device_remove_callback(DEVICE_CALLBACK_DISPLAY_STATE, _display_status_changed);
117                 }
118         }
119 }
120
121 void lockscreen_display_timer_freeze(void)
122 {
123         if (lcd_off_timer) {
124                 ecore_timer_freeze(lcd_off_timer);
125         }
126 }
127
128 void lockscreen_display_timer_renew(void)
129 {
130         if (lcd_off_timer) {
131                 ecore_timer_thaw(lcd_off_timer);
132                 ecore_timer_reset(lcd_off_timer);
133         }
134 }
135
136 bool lockscreen_display_is_off(void)
137 {
138         return display_off;
139 }