2.0 alpha
[platform/core/system/devman.git] / src / display_wd.c
1 /*
2  *  devman
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: DongGi Jang <dg0402.jang@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20 */
21
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdbool.h>
26 #include <strings.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <linux/limits.h>
30 #include <poll.h>
31 #include <errno.h>
32 #include <signal.h>
33 #include <vconf.h>
34 #include <vconf-keys.h>
35
36 #include "devlog.h"
37 #include "devman.h"
38 #include "devman_internal.h"
39
40 char fifo_path[NAME_MAX];
41 struct pollfd fifo_pollfd;
42
43 static void sig_quit(int signo)
44 {
45         fprintf(stderr, "[DISPLAY_WD] display_wd will be exit for signal %d\n", signo);
46         if(fifo_pollfd.fd >= 0)
47                 close(fifo_pollfd.fd);
48         if (access(fifo_path, F_OK) == 0)
49                 unlink(fifo_path);
50 }
51
52 int main(void)
53 {
54         int fd = -1;
55         int ret = -1;
56         int val = -1;
57         int auto_brightness_state = -1;
58         fifo_pollfd.fd = -1;
59         setsid();
60
61         signal(SIGPIPE, sig_quit);
62
63         snprintf(fifo_path, NAME_MAX, "%s.%d", DISPLAY_WD_FIFO, getppid());
64         fifo_pollfd.fd = open(fifo_path, O_WRONLY);
65         if (fifo_pollfd.fd < 0) {
66                 fprintf(stderr, "[DISPLAY_WD] Cannot open the fifo file - %s.\n",
67                         fifo_path);
68                 return -1;
69         }
70
71         /* waitting for parent process ready */
72         usleep(10000);
73         ret = write(fifo_pollfd.fd, "ack", strlen("ack") + 1);
74
75         close(fifo_pollfd.fd);
76
77         fifo_pollfd.fd = open(fifo_path, O_RDONLY);
78         if (fifo_pollfd.fd < 0) {
79                 fprintf(stderr, "[DISPLAY_WD] Cannot open the fifo file - %s.\n",
80                         fifo_path);
81                 return -1;
82         }
83
84         fifo_pollfd.events = (POLLIN | POLLHUP);
85         ret = 0;
86         while (ret != DISPLAY_WD_CANCEL) {
87                 if (poll(&fifo_pollfd, 1, -1) < 0) {
88                         fprintf(stderr,
89                                 "[DISPLAY_WD] Cannot poll the fifo file - %s\n",
90                                 fifo_path);
91                         close(fifo_pollfd.fd);
92                         return -1;
93                 }
94                 if (fifo_pollfd.revents & POLLIN) {
95                         read(fifo_pollfd.fd, &ret, sizeof(int));
96                         if (ret == DISPLAY_WD_CANCEL) {
97                                 fprintf(stderr,
98                                         "[DISPLAY_WD] Canceled. - %s, %d\n",
99                                         fifo_path, ret);
100                                 close(fifo_pollfd.fd);
101                                 return -1;
102                         }
103                 }
104                 if (fifo_pollfd.revents & POLLHUP)
105                         break;
106         }
107         close(fifo_pollfd.fd);
108         unlink(fifo_path);
109
110         ret = vconf_get_int(VCONFKEY_SETAPPL_LCD_BRIGHTNESS, &val);
111
112         if (ret == 0 && val > 0) {
113                 device_set_property(DEVTYPE_DISPLAY0, DISPLAY_PROP_BRIGHTNESS, val);
114                 device_set_property(DEVTYPE_LED, LED_PROP_BRIGHTNESS, 0);
115         }
116
117         if (vconf_get_int(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, &auto_brightness_state) == 0) {
118                 if (auto_brightness_state == SETTING_BRIGHTNESS_AUTOMATIC_PAUSE) {
119                         vconf_set_int(VCONFKEY_SETAPPL_BRIGHTNESS_AUTOMATIC_INT, SETTING_BRIGHTNESS_AUTOMATIC_ON);
120                 }
121         }
122
123         return 0;
124 }