display: display-state-transition: Relocate fuctions related to transition timer...
[platform/core/system/deviced.git] / src / display / display-state-transition.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2023 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  * @file        display-state-transition.c
21  * @brief       This file has functions related to display state transition
22  */
23
24 #include "device-interface.h"
25 #include "display-state-transition.h"
26 #include "display-plugin.h"
27 #include "power/power-suspend.h"
28 #include "shared/log.h"
29
30 static guint state_transition_timer_id;
31
32 static int trans_table[S_END][EVENT_END] = {
33         /* Timeout,   Input */
34         { S_START,    S_START    }, /* S_START */
35         { S_LCDDIM,   S_NORMAL   }, /* S_NORMAL */
36         { S_LCDOFF,   S_NORMAL   }, /* S_LCDDIM */
37         { S_SLEEP,    S_NORMAL   }, /* S_LCDOFF */
38         { S_SLEEP,    S_STANDBY  }, /* S_STANDBY */
39         { S_LCDOFF,   S_NORMAL   }, /* S_SLEEP */
40         { S_POWEROFF, S_POWEROFF }, /* S_POWEROFF */
41 };
42
43 static bool is_display_state_valid(enum state_t display_state)
44 {
45         return (display_state >= S_START && display_state < S_END);
46 }
47
48 static bool is_device_event_type_valid(int event_type)
49 {
50         return (event_type >= EVENT_TIMEOUT && event_type < EVENT_END);
51 }
52
53 int display_state_transition_get_next_transition_display_state(enum state_t from_state, enum state_t *to_state, int evt_type)
54 {
55         if (!to_state || !is_display_state_valid(from_state) || !is_device_event_type_valid(evt_type))
56                 return -EINVAL;
57
58         *to_state = trans_table[from_state][evt_type];
59         return 0;
60 }
61
62 int display_state_transition_set_transition_table_display_state(enum state_t display_state, enum state_t set_state, int evt_type)
63 {
64         if (!is_display_state_valid(display_state) || !is_device_event_type_valid(evt_type) || !is_display_state_valid(set_state))
65                 return -EINVAL;
66
67         trans_table[display_state][evt_type] = set_state;
68         return 0;
69 }
70
71 static void remove_state_transition(void)
72 {
73         if (state_transition_timer_id) {
74                 g_source_remove(state_transition_timer_id);
75                 state_transition_timer_id = 0;
76         }
77 }
78
79 static gboolean state_transition_timeout_handler(void *data)
80 {
81         const char *state_name = NULL;
82         display_plugin_state_get_name(get_pm_cur_state(), &state_name);
83         _I("Time out state %s", state_name);
84
85         remove_state_transition();
86
87         display_plugin_state_do_default_trans(get_pm_cur_state(), EVENT_TIMEOUT);
88         return G_SOURCE_REMOVE;
89 }
90
91 bool display_state_transition_is_there_state_transition_timer(void)
92 {
93         return (state_transition_timer_id != 0);
94 }
95
96 int display_state_transition_reset_state_transition_timeout(int timeout)
97 {
98         bool timeout_enable;
99         if (!display_plugin_config_get_timeout_enable(&timeout_enable) && (!timeout_enable))
100                 return -EPERM;
101
102         if ((get_pm_cur_state() == S_LCDOFF)
103         && (is_emulator() == true || timeout_sleep_support == false))
104                 return -EPERM;
105
106         _I("Reset timeout(%d ms) pm_cur_state(%d).", timeout, get_pm_cur_state());
107         remove_state_transition();
108
109         if (trans_table[get_pm_cur_state()][EVENT_TIMEOUT] == get_pm_cur_state())
110                 return -EPERM;
111
112         if (timeout > 0)
113                 state_transition_timer_id = g_timeout_add(timeout,
114                         state_transition_timeout_handler, NULL);
115         else if (timeout == 0)
116                 display_plugin_state_do_default_trans(get_pm_cur_state(), EVENT_TIMEOUT);
117
118         return 0;
119 }