device: Merge public api on Tizen 2.3 into tizen branch
[platform/core/api/device.git] / src / display.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License. 
15  */
16
17
18 #include <stdio.h>
19 #include <errno.h>
20 #include <vconf.h>
21
22 #include "display.h"
23 #include "common.h"
24 #include "dbus.h"
25
26 #define METHOD_GET_DISPLAY_COUNT    "GetDisplayCount"
27 #define METHOD_GET_MAX_BRIGHTNESS   "GetMaxBrightness"
28 #define METHOD_GET_BRIGHTNESS   "GetBrightness"
29 #define METHOD_SET_BRIGHTNESS   "SetBrightness"
30 #define METHOD_CHANGE_STATE             "changestate"
31
32 #define STR_LCD_OFF   "lcdoff"
33 #define STR_LCD_DIM   "lcddim"
34 #define STR_LCD_ON    "lcdon"
35
36 static int display_cnt = -1;
37 struct display {
38         int max;
39 } *display_arr;
40
41 static int alloc_display(void)
42 {
43         int i;
44
45         if (display_cnt < 0)
46                 return -ENODEV;
47
48         display_arr = malloc(sizeof(struct display) * display_cnt);
49         if (!display_arr)
50                 return -ENOMEM;
51
52         for (i = 0; i < display_cnt; ++i)
53                 display_arr[i].max = -1;
54
55         return 0;
56 }
57
58 int device_display_get_numbers(int *device_number)
59 {
60         int ret;
61
62         if (!device_number)
63                 return DEVICE_ERROR_INVALID_PARAMETER;
64
65         /* if it is a first request */
66         if (display_cnt < 0) {
67                 ret = dbus_method_sync(DEVICED_BUS_NAME,
68                                 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
69                                 METHOD_GET_DISPLAY_COUNT, NULL, NULL);
70                 if (ret < 0)
71                         return errno_to_device_error(ret);
72                 display_cnt = ret;
73                 alloc_display();
74         }
75
76         *device_number = display_cnt;
77         _I("device_number : %d", *device_number);
78         return DEVICE_ERROR_NONE;
79 }
80
81 int device_display_get_max_brightness(int display_index, int *max_brightness)
82 {
83         int ret;
84
85         if (!max_brightness)
86                 return DEVICE_ERROR_INVALID_PARAMETER;
87
88         if (display_cnt < 0)
89                 device_display_get_numbers(&display_cnt);
90
91         if (display_index < 0 || display_index >= display_cnt)
92                 return DEVICE_ERROR_INVALID_PARAMETER;
93
94         if (!display_arr && alloc_display() < 0)
95                 return DEVICE_ERROR_OPERATION_FAILED;
96
97         if (display_arr[display_index].max < 0) {
98                 ret = dbus_method_sync(DEVICED_BUS_NAME,
99                                 DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
100                                 METHOD_GET_MAX_BRIGHTNESS, NULL, NULL);
101                 if (ret < 0)
102                         return errno_to_device_error(ret);
103                 display_arr[display_index].max = ret;
104         }
105
106         *max_brightness = display_arr[display_index].max;
107         return DEVICE_ERROR_NONE;
108 }
109
110 int device_display_get_brightness(int display_index, int *brightness)
111 {
112         int ret;
113
114         if (!brightness)
115                 return DEVICE_ERROR_INVALID_PARAMETER;
116
117         if (display_cnt < 0)
118                 device_display_get_numbers(&display_cnt);
119
120         if (display_index < 0 || display_index >= display_cnt)
121                 return DEVICE_ERROR_INVALID_PARAMETER;
122
123         ret = dbus_method_sync(DEVICED_BUS_NAME,
124                         DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
125                         METHOD_GET_BRIGHTNESS, NULL, NULL);
126         if (ret < 0)
127                 return errno_to_device_error(ret);
128
129         *brightness = ret;
130         return DEVICE_ERROR_NONE;
131 }
132
133 int device_display_set_brightness(int display_index, int brightness)
134 {
135         char *arr[1];
136         char str_val[32];
137         int ret, max;
138
139         if (display_cnt < 0)
140                 device_display_get_numbers(&display_cnt);
141
142         if (display_index < 0 || display_index >= display_cnt)
143                 return DEVICE_ERROR_INVALID_PARAMETER;
144
145         if (display_arr[display_index].max < 0)
146                 device_display_get_max_brightness(display_index, &max);
147
148         if (brightness < 0 || brightness > display_arr[display_index].max)
149                 return DEVICE_ERROR_INVALID_PARAMETER;
150
151         snprintf(str_val, sizeof(str_val), "%d", brightness);
152         arr[0] = str_val;
153
154         ret = dbus_method_sync(DEVICED_BUS_NAME,
155                         DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
156                         METHOD_SET_BRIGHTNESS, "i", arr);
157         if (ret < 0)
158                 return errno_to_device_error(ret);
159
160         return DEVICE_ERROR_NONE;
161 }
162
163 int device_display_get_state(display_state_e *state)
164 {
165         int ret, val;
166
167         if (!state)
168                 return DEVICE_ERROR_INVALID_PARAMETER;
169
170         ret = vconf_get_int(VCONFKEY_PM_STATE, &val);
171         if (ret < 0)
172                 return DEVICE_ERROR_OPERATION_FAILED;
173
174         if (val == VCONFKEY_PM_STATE_NORMAL)
175                 *state = DISPLAY_STATE_NORMAL;
176         else if (val == VCONFKEY_PM_STATE_LCDDIM)
177                 *state = DISPLAY_STATE_SCREEN_DIM;
178         else if (val == VCONFKEY_PM_STATE_LCDOFF)
179                 *state = DISPLAY_STATE_SCREEN_OFF;
180         else
181                 return DEVICE_ERROR_OPERATION_FAILED;
182
183         return DEVICE_ERROR_NONE;
184 }
185
186 static char *get_state_str(display_state_e state)
187 {
188         switch (state) {
189         case DISPLAY_STATE_NORMAL:
190                 return STR_LCD_ON;
191         case DISPLAY_STATE_SCREEN_DIM:
192                 return STR_LCD_DIM;
193         case DISPLAY_STATE_SCREEN_OFF:
194                 return STR_LCD_OFF;
195         default:
196                 break;
197         }
198         return NULL;
199 }
200
201 int device_display_change_state(display_state_e state)
202 {
203         char *str, *arr[1];
204         int ret;
205
206         if (state < DISPLAY_STATE_NORMAL || state > DISPLAY_STATE_SCREEN_OFF)
207                 return DEVICE_ERROR_INVALID_PARAMETER;
208
209         str = get_state_str(state);
210         if (!str)
211                 return DEVICE_ERROR_INVALID_PARAMETER;
212
213         arr[0] = str;
214
215         ret = dbus_method_sync(DEVICED_BUS_NAME,
216                         DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY,
217                         METHOD_CHANGE_STATE, "s", arr);
218         if (ret < 0)
219                 return errno_to_device_error(ret);
220
221         return DEVICE_ERROR_NONE;
222 }