769c6d4ddaa582361c67c8e8d8d5872241d2ab76
[platform/core/system/deviced.git] / src / display / plugin-common / poll.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        poll.c
22  * @brief        Power Manager poll implementation
23  *
24  */
25
26 #include <stdio.h>
27 #include "util.h"
28 #include "core.h"
29 #include "device-interface.h"
30 #include "poll.h"
31 #include "display-ops.h"
32 #include "display-plugin.h"
33 #include "shared/plugin.h"
34
35 static PMMsg recv_data;
36
37 int check_dimstay(int next_state, int flag)
38 {
39         if (next_state != LCD_OFF)
40                 return false;
41
42         if (!(flag & GOTO_STATE_NOW))
43                 return false;
44
45         if (!(get_pm_status_flag() & DIMSTAY_FLAG))
46                 return false;
47
48         return true;
49 }
50
51 static enum state_t get_state(int s_bits)
52 {
53         switch (s_bits) {
54         case LCD_NORMAL:
55                 return S_NORMAL;
56         case LCD_DIM:
57                 return S_LCDDIM;
58         case LCD_OFF:
59                 return S_LCDOFF;
60         case STANDBY:
61                 return S_STANDBY;
62         case SUSPEND:
63                 return S_SLEEP;
64         default:
65                 return -EINVAL;
66         }
67 }
68
69 static int __pm_lock_internal(pid_t pid, int s_bits, int flag, int timeout)
70 {
71         int cond;
72
73         cond = get_state(s_bits);
74         if (cond < 0)
75                 return cond;
76
77         if (!display_plugin_state_is_there_default_trans(cond))
78                 return -ENOTSUP;
79
80         cond = SET_COND_REQUEST(cond, PM_REQUEST_LOCK);
81
82         if (flag & GOTO_STATE_NOW)
83                 /* if the flag is true, go to the locking state directly */
84                 cond = SET_COND_FLAG(cond, PM_REQUEST_CHANGE);
85
86         if (flag & HOLD_KEY_BLOCK)
87                 cond = SET_COND_FLAG(cond, PM_FLAG_BLOCK_HOLDKEY);
88
89         recv_data.pid = pid;
90         recv_data.cond = cond;
91         recv_data.timeout = timeout;
92
93         poll_callback(PM_CONTROL_EVENT, &recv_data);
94
95         return 0;
96 }
97
98 static int __pm_unlock_internal(pid_t pid, int s_bits, int flag)
99 {
100         int cond;
101
102         cond = get_state(s_bits);
103         if (cond < 0)
104                 return cond;
105
106         if (!display_plugin_state_is_there_default_trans(cond))
107                 return -ENOTSUP;
108
109         cond = SET_COND_REQUEST(cond, PM_REQUEST_UNLOCK);
110
111         if (flag & PM_KEEP_TIMER)
112                 cond = SET_COND_FLAG(cond, PM_FLAG_KEEP_TIMER);
113
114         if (flag & PM_RESET_TIMER)
115                 cond = SET_COND_FLAG(cond, PM_FLAG_RESET_TIMER);
116
117         recv_data.pid = pid;
118         recv_data.cond = cond;
119         recv_data.timeout = 0;
120
121         poll_callback(PM_CONTROL_EVENT, &recv_data);
122
123         return 0;
124 }
125
126 static int __pm_change_internal(pid_t pid, int s_bits)
127 {
128         int cond, ret;
129         const int display_on = LCD_NORMAL | LCD_DIM;
130
131         cond = get_state(s_bits);
132         if (cond < 0)
133                 return cond;
134
135         ret = is_lcdon_blocked();
136         if (ret != LCDON_BLOCK_NONE && (cond & display_on)) {
137                 _W("LCDON is blocked, %d.", ret);
138                 return -ENOTSUP;
139         }
140
141         if (!display_plugin_state_is_there_default_trans(cond))
142                 return -ENOTSUP;
143
144         cond = SET_COND_REQUEST(cond, PM_REQUEST_CHANGE);
145
146         recv_data.pid = pid;
147         recv_data.cond = cond;
148         recv_data.timeout = 0;
149
150         poll_callback(PM_CONTROL_EVENT, &recv_data);
151
152         return 0;
153 }
154
155 void init_pm_internal(void *data)
156 {
157         struct display_plugin *dp = (struct display_plugin *) data;
158
159         dp->pm_lock_internal = __pm_lock_internal;
160         dp->pm_unlock_internal = __pm_unlock_internal;
161         dp->pm_change_internal = __pm_change_internal;
162 }