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