Initialize Tizen 2.3
[framework/system/deviced.git] / src / display / alpm.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 #include <stdbool.h>
21 #include <fcntl.h>
22
23 #include "util.h"
24 #include "core.h"
25 #include "display-ops.h"
26 #include "weaks.h"
27 #include "core/edbus-handler.h"
28
29 #define ON              "on"
30 #define OFF             "off"
31
32 #define SIGNAL_ALPM_ON                  "ALPMOn"
33 #define SIGNAL_ALPM_OFF                 "ALPMOff"
34 #define CLOCK_START                     "clockstart"
35 #define CLOCK_END                       "clockend"
36 #define ALPM_MAX_TIME                   30      /* minutes */
37
38 static char *alpm_path;
39 static int update_count;
40 static int alpm_state;
41
42 void broadcast_alpm_state(int state)
43 {
44         char *signal;
45
46         signal = (state == true ? SIGNAL_ALPM_ON : SIGNAL_ALPM_OFF);
47
48         broadcast_edbus_signal(DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
49             signal, NULL, NULL);
50 }
51
52 int alpm_get_state(void)
53 {
54         char state[4];
55         int ret, alpm;
56
57         if (!alpm_path)
58                 return -ENODEV;
59
60         ret = sys_get_str(alpm_path, state);
61         if (ret < 0)
62                 return ret;
63
64         if (!strncmp(state, ON, strlen(ON)))
65                 alpm = true;
66         else if (!strncmp(state, OFF, strlen(OFF)))
67                 alpm = false;
68         else
69                 alpm = -EINVAL;
70
71         return alpm;
72 }
73
74 int alpm_set_state(int on)
75 {
76         if (!alpm_path)
77                 return -ENODEV;
78
79         broadcast_alpm_state(on);
80
81         update_count = 0;
82
83         _D("ALPM is %s", (on ? ON : OFF));
84
85         alpm_state = on;
86
87         return sys_set_str(alpm_path, (on ? ON : OFF));
88 }
89
90 static void start_clock(void)
91 {
92         struct timespec now_time;
93         static struct timespec start_time;
94         int diff_time;
95
96         if (pm_cur_state == S_NORMAL ||
97             pm_cur_state == S_LCDDIM ||
98             alpm_state == false)
99                 return;
100
101         _D("lcd on");
102
103         if (update_count == 0)
104                 clock_gettime(CLOCK_REALTIME, &start_time);
105
106         update_count++;
107
108         _D("clock update count : %d", update_count);
109
110         clock_gettime(CLOCK_REALTIME, &now_time);
111         diff_time = (now_time.tv_sec - start_time.tv_sec) / 60;
112
113         if (diff_time >= ALPM_MAX_TIME) {
114                 _D("Exit ALPM state, LCD off");
115                 alpm_set_state(false);
116                 backlight_ops.on();
117                 backlight_ops.off();
118                 return;
119         }
120
121         /* change pmstate in order that apps can know the state */
122         set_setting_pmstate(S_NORMAL);
123         backlight_ops.on();
124
125         sleep(1);
126
127         _D("lcd off");
128         set_setting_pmstate(S_LCDOFF);
129         backlight_ops.off();
130
131         _D("finished!");
132 }
133
134 static void end_clock(void)
135 {
136         if (pm_cur_state == S_NORMAL ||
137             pm_cur_state == S_LCDDIM ||
138             alpm_state == false)
139                 return;
140
141         _D("end clock : lcd off");
142 }
143
144 int set_alpm_screen(char *screen)
145 {
146         if (!screen)
147                 return -EINVAL;
148
149         if (!alpm_path)
150                 return -ENODEV;
151
152         if (!strncmp(screen, CLOCK_START,
153             strlen(CLOCK_START))) {
154                 start_clock();
155         } else if (!strncmp(screen, CLOCK_END,
156             strlen(CLOCK_END))) {
157                 end_clock();
158         }
159
160         return 0;
161 }
162
163 static void alpm_init(void *data)
164 {
165         int fd;
166
167         alpm_path = getenv("ALPM_NODE");
168
169         /* Check alpm node is valid */
170         fd = open(alpm_path, O_RDONLY);
171         if (fd < 0) {
172                 _E("alpm node is invalid");
173                 alpm_path = NULL;
174                 return;
175         } else {
176                 _I("alpm path[%s] is initialized!", alpm_path);
177         }
178         close(fd);
179 }
180
181 static void alpm_exit(void *data)
182 {
183         alpm_set_state(false);
184 }
185
186 static const struct display_ops display_alpm_ops = {
187         .name     = "alpm",
188         .init     = alpm_init,
189         .exit     = alpm_exit,
190 };
191
192 DISPLAY_OPS_REGISTER(&display_alpm_ops)
193