remove libds stuffs
[platform/core/uifw/libds-tizen.git] / clients / simple-dpms.c
1 /*
2 Copyright (C) 2015 - 2016 Samsung Electronics co., Ltd. All Rights Reserved.
3
4 Contact:
5       SooChan Lim <sc1.lim@samsung.com>
6       Changyeon Lee <cyeon.lee@samsung.com>
7       JunKyeong Kim <jk0430.kim@samsung.com>
8       Boram Park <boram1288.park@samsung.com>
9
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the "Software"),
12 to deal in the Software without restriction, including without limitation
13 the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 and/or sell copies of the Software, and to permit persons to whom the
15 Software is furnished to do so, subject to the following conditions:
16
17 The above copyright notice and this permission notice (including the next
18 paragraph) shall be included in all copies or substantial portions of the
19 Software.
20
21 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 DEALINGS IN THE SOFTWARE.
28 */
29
30 #include <stdint.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <sys/param.h>
38 #include <sys/mman.h>
39 #include <stdlib.h>
40 #include <sys/syscall.h>
41 #include <poll.h>
42
43 #include <wayland-client.h>
44 #include <tizen-extension-client-protocol.h>
45 #include <tizen-dpms-client-protocol.h>
46
47
48 struct wl_dpms_info {
49         char *app_name;
50
51         struct wl_display *display;
52         struct wl_registry *registry;
53         struct wl_output *output;
54         struct tizen_dpms_manager *tz_dpms_mng;
55         int got_dpms_state;
56 };
57
58 void
59 usage(const char *app_name)
60 {
61         printf("usage: %s \n", app_name);
62         printf("%s output_num option(set1/get0) state\nex)\n", app_name);
63         printf("%s 1 0  => set dpms_on\n", app_name);
64         printf("%s 1 3  => set dpms_off\n", app_name);
65         printf("%s 0 0  => get state\n", app_name);
66 }
67
68 static struct wl_dpms_info *
69 _create_wl_dpms_info (void)
70 {
71         struct wl_dpms_info *test_info = NULL;
72
73         test_info = calloc(1, sizeof(struct wl_dpms_info));
74         if (test_info == NULL) {
75                 printf("alloc fail");
76                 return NULL;
77         }
78
79         return test_info;
80 }
81
82 static void
83 _destroy_wl_dpms_info (struct wl_dpms_info *test_info)
84 {
85         if (!test_info) return;
86
87         if (test_info->app_name)
88                 free(test_info->app_name);
89         if (test_info->tz_dpms_mng)
90                 tizen_dpms_manager_destroy(test_info->tz_dpms_mng);
91         if (test_info->registry)
92                 wl_registry_destroy(test_info->registry);
93         if (test_info->display)
94                 wl_display_disconnect(test_info->display);
95
96         free(test_info);
97 }
98
99 static void
100 dpms_handle_set_state(void *data, struct tizen_dpms_manager *tz_dpms, uint32_t mode, uint32_t error)
101 {
102         struct wl_dpms_info *test_info = (struct wl_dpms_info *)data;
103         printf("dpms_set_state_cb - mode:%d, error:%d\n", mode, error);
104         test_info->got_dpms_state = 1;
105 }
106
107 static void
108 dpms_handle_get_state(void *data, struct tizen_dpms_manager *tz_dpms, uint32_t mode, uint32_t error)
109 {
110         struct wl_dpms_info *test_info = (struct wl_dpms_info *)data;
111         printf("dpms_get_state_cb - mode:%d, error:%d\n", mode, error);
112         test_info->got_dpms_state = 1;
113 }
114
115 static const struct tizen_dpms_manager_listener dpms_listener = {
116         dpms_handle_set_state,
117         dpms_handle_get_state
118 };
119
120 static void
121 handle_global(void *data, struct wl_registry *registry,
122                                 uint32_t name, const char *interface, uint32_t version)
123 {
124         struct wl_dpms_info *test_info = (struct wl_dpms_info *)data;
125
126         if (strcmp(interface, "wl_output") == 0) {
127                 test_info->output = wl_registry_bind(registry, name, &wl_output_interface, 2);
128                 if (!test_info->output)
129                         printf("bind wl_output fail\n");
130                 else
131                         printf("bind wl_output\n");
132         } else if (strcmp(interface, "tizen_dpms_manager") == 0) {
133                 test_info->tz_dpms_mng = wl_registry_bind(registry, name, &tizen_dpms_manager_interface, 1);
134                 if (!test_info->tz_dpms_mng)
135                         printf("bind tizen_dpms_manager fail\n");
136                 else {
137                         tizen_dpms_manager_add_listener(test_info->tz_dpms_mng, &dpms_listener, test_info);
138                         printf("bind tizen_dpms_manager\n");
139                 }
140         }
141 }
142
143 static void
144 handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
145 {
146 }
147
148 static const struct wl_registry_listener registry_listener = {
149         handle_global,
150         handle_global_remove
151 };
152
153 int
154 main(int argc, char *argv[])
155 {
156         struct wl_dpms_info *test_info = NULL;
157         char *opt = NULL;
158         int option;
159         char *sta = NULL;
160         int state = 0;
161
162         test_info = _create_wl_dpms_info();
163         if (test_info == NULL) return 0;
164
165         test_info->app_name = strdup(argv[0]);
166         if (test_info->app_name == NULL) {
167                 printf("alloc fail");
168                 goto done;
169         }
170         if (argc != 3) {
171                 usage(test_info->app_name);
172                 goto done;
173         }
174
175         opt = strdup(argv[1]);
176         if (opt == NULL) {
177                 printf("alloc fail");
178                 goto done;
179         }
180         option = opt[0] - '0';
181         free(opt);
182         if (!(option == 0 || option == 1)) {
183                 usage(test_info->app_name);
184                 goto done;
185         }
186
187         if (option == 1) {
188                 sta = strdup(argv[2]);
189                 if (sta == NULL) {
190                         printf("alloc fail");
191                         goto done;
192                 }
193                 state = sta [0] - '0';
194                 free(sta);
195                 if (!(state == 0 || state == 1 || state == 2 || state == 3)) {
196                         usage(test_info->app_name);
197                         goto done;
198                 }
199         }
200
201         test_info->display = wl_display_connect(NULL);
202         if (test_info->display == NULL) {
203                 printf("wl_display_connect fail");
204                 goto done;
205         }
206
207         test_info->registry = wl_display_get_registry(test_info->display);
208         if (test_info->registry == NULL) {
209                 printf("wl_display_get_registry fail");
210                 goto done;
211         }
212
213         wl_registry_add_listener(test_info->registry, &registry_listener, test_info);
214         wl_display_roundtrip(test_info->display);
215
216         if (!test_info->output || !test_info->tz_dpms_mng) {
217                 printf("bind fail\n");
218                 goto done;
219         }
220
221         if (option == 1) {
222                 tizen_dpms_manager_set_dpms(test_info->tz_dpms_mng, test_info->output, state);
223                 printf("set dpms %d\n", state);
224         } else {
225                 tizen_dpms_manager_get_dpms(test_info->tz_dpms_mng, test_info->output);
226                 printf("get dpms\n");
227         }
228
229         test_info->got_dpms_state = 0;
230         while (!test_info->got_dpms_state) {
231                 wl_display_roundtrip(test_info->display);
232         }
233
234 done:
235         _destroy_wl_dpms_info(test_info);
236
237         return 0;
238 }
239