ee59d193c15cb6f16cbe5fa47881352f2304dfc1
[platform/hal/backend/emulator/device-emulator.git] / hw / display / display.c
1 /*
2  * device-node
3  *
4  * Copyright (c) 2015 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 <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <linux/limits.h>
25
26 #include <hal/device/hal-display-interface.h>
27 #include <hal/hal-common-interface.h>
28 #include <libsyscommon/file.h>
29
30 #include "common.h"
31
32 #ifndef BACKLIGHT_PATH
33 #define BACKLIGHT_PATH  "/sys/class/backlight/emulator"
34 #endif
35
36 static int display_get_max_brightness(int *val)
37 {
38         static int max = -1;
39         int r;
40
41         if (!val)
42                 return -EINVAL;
43
44         if (max < 0) {
45                 r = sys_get_int(BACKLIGHT_PATH"/max_brightness", &max);
46                 if (r < 0)
47                         return r;
48         }
49
50         *val = max;
51         return 0;
52 }
53
54 static int display_get_brightness(int *brightness)
55 {
56         int r, v;
57
58         if (!brightness) {
59                 _E("wrong parameter");
60                 return -EINVAL;
61         }
62
63         r = sys_get_int(BACKLIGHT_PATH"/brightness", &v);
64         if (r < 0) {
65                 _E("fail to get brightness (errno:%d)", r);
66                 return r;
67         }
68
69         *brightness = v;
70         return 0;
71 }
72
73 static int display_set_brightness(int brightness)
74 {
75         int r, max;
76
77         r = display_get_max_brightness(&max);
78         if (r < 0) {
79                 _E("fail to get max brightness (errno:%d)", r);
80                 return r;
81         }
82
83         if (brightness < 0 || brightness > max) {
84                 _E("wrong parameter");
85                 return -EINVAL;
86         }
87
88         r = sys_set_int(BACKLIGHT_PATH"/brightness", brightness);
89         if (r < 0) {
90                 _E("fail to set brightness (errno:%d)", r);
91                 return r;
92         }
93
94         return 0;
95 }
96
97 static int display_init(void **data)
98 {
99         hal_backend_display_funcs *display_funcs;
100
101         display_funcs = calloc(1, sizeof(hal_backend_display_funcs));
102         if (!display_funcs)
103                 return -ENOMEM;
104
105         display_funcs->get_max_brightness = display_get_max_brightness;
106         display_funcs->get_brightness = display_get_brightness;
107         display_funcs->set_brightness = display_set_brightness;
108
109         *data = (void *)display_funcs;
110
111         return 0;
112 }
113
114 static int display_exit(void *data)
115 {
116         if (!data)
117                 return 0;
118
119         free(data);
120         return 0;
121 }
122
123 hal_backend EXPORT hal_backend_device_display_data = {
124         .name = "display",
125         .vendor = "EMUL",
126         .abi_version = HAL_ABI_VERSION_TIZEN_6_5,
127         .init = display_init,
128         .exit = display_exit,
129 };