remove libds stuffs
[platform/core/uifw/libds-tizen.git] / src / dpms.c
1 #include <assert.h>
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <wayland-server.h>
5 #include <libds/log.h>
6 #include <libds/output.h>
7 #include <tizen-dpms-server-protocol.h>
8 #include "libds-tizen/dpms.h"
9 #include "util.h"
10
11 struct ds_tizen_dpms
12 {
13     struct wl_global *global;
14     struct wl_resource *res;
15
16     struct wl_listener destroy;
17
18     struct {
19         struct wl_signal destroy;
20         struct wl_signal set_dpms;
21         struct wl_signal get_dpms;
22     } events;
23
24     bool binded;
25 };
26
27 #define TIZEN_DPMS_VERSION 1
28
29 static void dpms_handle_display_destroy(struct wl_listener *listener,
30         void *data);
31 static void dpms_bind(struct wl_client *wl_client, void *data,
32         uint32_t verison, uint32_t id);
33
34 WL_EXPORT struct ds_tizen_dpms *
35 ds_tizen_dpms_create(struct wl_display *display)
36 {
37     struct ds_tizen_dpms *dpms;
38
39     dpms = calloc(1, sizeof *dpms);
40     if (!dpms) {
41         ds_err("dpms create fail : memory alloc failed");
42         return NULL;
43     }
44
45     dpms->global = wl_global_create(display, &tizen_dpms_manager_interface,
46             1, dpms, dpms_bind);
47     if (!dpms->global) {
48         ds_err("global create fail : tizen_dpms_manager_interface failed");
49         free(dpms);
50         return NULL;
51     }
52
53     wl_signal_init(&dpms->events.destroy);
54     wl_signal_init(&dpms->events.set_dpms);
55     wl_signal_init(&dpms->events.get_dpms);
56
57     dpms->destroy.notify = dpms_handle_display_destroy;
58     wl_display_add_destroy_listener(display, &dpms->destroy);
59
60     ds_inf("global create : tizen_dpms_manager(%p)", dpms);
61
62     return dpms;
63 }
64
65 WL_EXPORT void
66 ds_tizen_dpms_add_destroy_listener(struct ds_tizen_dpms *dpms,
67         struct wl_listener *listener)
68 {
69     wl_signal_add(&dpms->events.destroy, listener);
70 }
71
72 WL_EXPORT void
73 ds_tizen_dpms_add_set_dpms_listener(struct ds_tizen_dpms *dpms,
74         struct wl_listener *listener)
75 {
76     wl_signal_add(&dpms->events.set_dpms, listener);
77 }
78
79 WL_EXPORT void
80 ds_tizen_dpms_add_get_dpms_listener(struct ds_tizen_dpms *dpms,
81         struct wl_listener *listener)
82 {
83     wl_signal_add(&dpms->events.get_dpms, listener);
84 }
85
86 WL_EXPORT void
87 ds_tizen_dpms_send_set_result(struct ds_tizen_dpms *dpms,
88         enum ds_tizen_dpms_mode mode, enum ds_tizen_dpms_error error)
89 {
90     ds_dbg("dpms send set result : mode(%d), error(%d)", mode, error);
91     tizen_dpms_manager_send_set_state(dpms->res, mode, error);
92 }
93
94 WL_EXPORT void
95 ds_tizen_dpms_send_get_result(struct ds_tizen_dpms *dpms,
96         enum ds_tizen_dpms_mode mode, enum ds_tizen_dpms_error error)
97 {
98     ds_dbg("dpms send get result : mode(%d), error(%d)", mode, error);
99     tizen_dpms_manager_send_get_state(dpms->res, mode, error);
100 }
101
102 static void
103 dpms_handle_display_destroy(struct wl_listener *listener, void *data)
104 {
105     struct ds_tizen_dpms *dpms;
106
107     dpms = wl_container_of(listener, dpms, destroy);
108
109     ds_inf("global destroy : tizen_dpms_manager(%p)", dpms);
110
111     wl_signal_emit(&dpms->events.destroy, dpms);
112     wl_list_remove(&dpms->destroy.link);
113     wl_resource_set_user_data(dpms->res, NULL);
114     wl_global_destroy(dpms->global);
115     free(dpms);
116 }
117
118 static void
119 _tizen_dpms_manager_handle_destroy(struct wl_client *client,
120         struct wl_resource *resource)
121 {
122     ds_inf("tizen_dpms_manager cb_destroy (res:%p)", resource);
123     wl_resource_destroy(resource);
124 }
125
126 static void
127 _tizen_dpms_manager_handle_set_dpms(struct wl_client *client,
128         struct wl_resource *resource, struct wl_resource *output, uint32_t mode)
129 {
130     struct ds_tizen_dpms *dpms;
131
132     dpms = wl_resource_get_user_data(resource);
133
134     if (mode > DS_TIZEN_DPMS_MODE_OFF) {
135         ds_err("set dpms error : not supported mode(%d)", mode);
136         tizen_dpms_manager_send_set_state(resource, DS_TIZEN_DPMS_MODE_OFF,
137                 DS_TIZEN_DPMS_ERROR_INVALID_PARAMETER);
138         return;
139     }
140
141     struct ds_tizen_dpms_event event = {
142         .mode = mode,
143     };
144
145     wl_signal_emit(&dpms->events.set_dpms, &event);
146 }
147
148 static void
149 _tizen_dpms_manager_handle_get_dpms(struct wl_client *client,
150         struct wl_resource *resource, struct wl_resource *output)
151 {
152     struct ds_tizen_dpms *dpms;
153
154     dpms = wl_resource_get_user_data(resource);
155
156     wl_signal_emit(&dpms->events.get_dpms, NULL);
157 }
158
159 static const struct tizen_dpms_manager_interface dpms_impl =
160 {
161     _tizen_dpms_manager_handle_destroy,
162     _tizen_dpms_manager_handle_set_dpms,
163     _tizen_dpms_manager_handle_get_dpms,
164 };
165
166 static void
167 _tizen_dpms_client_cb_destroy(struct wl_resource *resource)
168 {
169     struct ds_tizen_dpms *dpms;
170
171     ds_inf("tizen_dpms_client_cb_destroy (res:%p)", resource);
172
173     dpms = wl_resource_get_user_data(resource);
174     if (dpms) {
175         dpms->binded = false;
176         dpms->res = NULL;
177     }
178 }
179
180 static void
181 dpms_bind(struct wl_client *client, void *data, uint32_t version,
182         uint32_t id)
183 {
184     struct ds_tizen_dpms *dpms = data;
185
186     if (dpms->binded == true) {
187         //support only one client.
188         ds_err("dpms bind error : already binded.");
189         return;
190     }
191
192     dpms->res = wl_resource_create(client, &tizen_dpms_manager_interface,
193             MIN(version, TIZEN_DPMS_VERSION), id);
194     if (dpms->res == NULL) {
195         ds_err("dpms bind error : wl_resource_create failed.");
196         wl_client_post_no_memory(client);
197         return;
198     }
199
200     wl_resource_set_implementation(dpms->res, &dpms_impl, dpms,
201             _tizen_dpms_client_cb_destroy);
202
203     dpms->binded = true;
204 }