display: headed: Remove unnecessary header
[platform/core/system/deviced.git] / plugins / iot-headed / display / core.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 /**
21  * @file        core.c
22  * @brief       Power manager main loop.
23  *
24  * This file includes Main loop, the FSM, signal processing.
25  */
26 #include <signal.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <limits.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <stdbool.h>
34 #include <time.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <dlfcn.h>
39 #include <fnmatch.h>
40 #include <vconf-keys.h>
41 #include <sys/time.h>
42 #include <libsyscommon/list.h>
43 #include <libsyscommon/resource-manager.h>
44 #include <system/syscommon-plugin-deviced-common-interface.h>
45 #include <system/syscommon-plugin-deviced-display-interface.h>
46 #include <device/display-internal.h>
47 #include <hal/device/hal-device-power.h>
48
49 #include <libsyscommon/log.h>
50 #include "device-interface.h"
51
52 /**
53  * @addtogroup POWER_MANAGER
54  * @{
55  */
56
57 static enum device_ops_status status = DEVICE_OPS_STATUS_UNINIT;
58
59 static struct state states[DEVICED_DISPLAY_STATE_END] = {
60         { DEVICED_DISPLAY_STATE_START,    "DEVICED_DISPLAY_STATE_START",    NULL,          NULL,           NULL,          NULL            },
61         { DEVICED_DISPLAY_STATE_ON,   "DEVICED_DISPLAY_STATE_ON",   NULL,          NULL,           NULL,          NULL            },
62         { DEVICED_DISPLAY_STATE_DIM,   "DEVICED_DISPLAY_STATE_DIM",   NULL,          NULL,           NULL,          NULL            },
63         { DEVICED_DISPLAY_STATE_OFF,   "DEVICED_DISPLAY_STATE_OFF",   NULL,          NULL,           NULL,          NULL            },
64         { DEVICED_DISPLAY_STATE_SLEEP,    "DEVICED_DISPLAY_STATE_SLEEP",    NULL,          NULL,           NULL,          NULL            },
65 };
66
67 #define LOCK_SCREEN_WATING_TIME         300     /* 0.3 second */
68 #define LONG_PRESS_INTERVAL             2       /* 2 seconds */
69 #define SAMPLING_INTERVAL               1       /* 1 sec */
70 #define BRIGHTNESS_CHANGE_STEP          10
71 #define LCD_ALWAYS_ON                   0
72 #define ACCEL_SENSOR_ON                 1
73 #define CONTINUOUS_SAMPLING             1
74 #define LCDOFF_TIMEOUT                  300     /* milli second */
75
76 static struct display_config display_conf = {
77         .lock_wait_time         = LOCK_SCREEN_WATING_TIME,
78         .longpress_interval     = LONG_PRESS_INTERVAL,
79         .lightsensor_interval   = SAMPLING_INTERVAL,
80         .lcdoff_timeout         = LCDOFF_TIMEOUT,
81         .pm_default_brightness = 80,
82         .brightness_change_step = BRIGHTNESS_CHANGE_STEP,
83         .lcd_always_on          = LCD_ALWAYS_ON,
84         .dimming                = 1,
85         .framerate_app          = {0, 0, 0, 0},
86         .control_display        = 0,
87         .powerkey_doublepress   = 0,
88         .accel_sensor_on        = ACCEL_SENSOR_ON,
89         .continuous_sampling    = CONTINUOUS_SAMPLING,
90         .timeout_enable         = true,
91         .input_support          = true,
92         .display_init_direction = DISPLAY_INIT_DIRECTION_HORIZONTAL,
93         .aod_enter_level        = 40,
94         .aod_tsp                = true,
95         .touch_wakeup           = false,
96         .display_on_usb_conn_changed = true,
97         .display_dpms_type      = DISPLAY_DPMS_TYPE_WINDOW_MANAGER,
98 };
99
100 inline const struct display_config* get_var_display_config()
101 {
102         return &display_conf;
103 }
104
105 inline struct state* state_st(enum deviced_display_state state)
106 {
107         return &states[state];
108 }
109
110 static void init_display_states(void *data)
111 {
112         struct display_plugin *dp = (struct display_plugin *) data;
113         for(int i = 0; i < DEVICED_DISPLAY_STATE_END; i++)
114                 dp->display_states[i] = &states[i];
115 }
116 /**
117  * Power manager Main
118  *
119  */
120 static int display_probe(void *data)
121 {
122         struct display_plugin *dp = (struct display_plugin *) data;
123         assert(dp);
124
125         dp->config = &display_conf;
126         init_display_states(dp);
127         setup_display_plugin_backlight_ops(dp);
128         dp->set_dim_state = NULL;
129         dp->get_device_flags = NULL;
130         dp->lcd_on_procedure = NULL;
131         dp->lcd_off_procedure = NULL;
132         dp->custom_lcd_on = NULL;
133         dp->custom_lcd_off = NULL;
134         dp->display_on_by_reason = NULL;
135         dp->display_off_by_reason = NULL;
136         dp->default_saving_mode = NULL;
137         dp->is_lcdon_blocked = is_lcdon_blocked;
138         dp->proc_change_state = NULL;
139
140         /* check display feature */
141         if (!is_feature_display_supported())
142                 return -ENODEV;
143
144         return 0;
145 }
146
147 static void display_init(void *data)
148 {
149         int ret;
150
151         /* load configutation */
152         ret = display_load_config(&display_conf);
153         if (ret < 0)
154                 _W("Failed to load '%s', use default value: %d",
155                     DISPLAY_CONF_FILE, ret);
156 }
157
158 static void display_exit(void *data)
159 {
160         return;
161 }
162
163 static int display_start(enum device_flags flags)
164 {
165         /* NORMAL MODE */
166         if (flags & NORMAL_MODE) {
167                 if (flags & LCD_PANEL_OFF_MODE)
168                         /* standby on */
169                         syscommon_resman_set_resource_attr_int(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
170                                 DEVICED_DISPLAY_ATTR_INT_DPMS_STATE, DEVICED_DPMS_STANDBY);
171                 else
172                         /* normal lcd on */
173                         syscommon_resman_set_resource_attr_int(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
174                                 DEVICED_DISPLAY_ATTR_INT_DPMS_STATE, DEVICED_DPMS_ON);
175
176                 return 0;
177         }
178
179         /* CORE LOGIC MODE */
180         if (!(flags & CORE_LOGIC_MODE))
181                 return 0;
182
183         display_get_display_ops_status(&status);
184         if (status == DEVICE_OPS_STATUS_START)
185                 return -EALREADY;
186
187         if (display_probe(NULL) < 0)
188                 return -EPERM;
189
190         display_init(NULL);
191
192         return 0;
193 }
194
195 static int display_stop(enum device_flags flags)
196 {
197         /* NORMAL MODE */
198         if (flags & NORMAL_MODE) {
199                 syscommon_resman_set_resource_attr_int(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
200                         DEVICED_DISPLAY_ATTR_INT_DPMS_STATE, DEVICED_DPMS_OFF);
201                 return 0;
202         } else if (flags & FORCE_OFF_MODE) {
203                 syscommon_resman_set_resource_attr_int(SYSCOMMON_RESOURCE_ID(DEVICED_RESOURCE_TYPE_DISPLAY),
204                         DEVICED_DISPLAY_ATTR_INT_DPMS_STATE, DEVICED_DPMS_FORCE_OFF);
205                 return 0;
206         }
207
208         /* CORE LOGIC MODE */
209         if (!(flags & CORE_LOGIC_MODE))
210                 return 0;
211
212         display_get_display_ops_status(&status);
213         if (status == DEVICE_OPS_STATUS_STOP)
214                 return -EALREADY;
215
216         display_exit(NULL);
217
218         return 0;
219 }
220
221 static int display_status(void)
222 {
223         return status;
224 }
225
226 static const struct device_ops display_plugin_device_ops = {
227         .disable_auto_init = true,
228         DECLARE_NAME_LEN("display-plugin"),
229         .probe    = display_probe,
230         .init     = display_init,
231         .exit     = display_exit,
232         .start    = display_start,
233         .stop     = display_stop,
234         .status   = display_status,
235 };
236
237 DEVICE_OPS_REGISTER(&display_plugin_device_ops)
238 /**
239  * @}
240  */