eolian: rename is_ref API to is_ptr to match syntax
[platform/upstream/efl.git] / src / lib / ecore_x / xcb / ecore_xcb_dpms.c
1 #include "ecore_xcb_private.h"
2 #ifdef ECORE_XCB_DAMAGE
3 # include <xcb/dpms.h>
4 #endif
5
6 /* local variables */
7 static Eina_Bool _dpms_avail = EINA_FALSE;
8
9 void
10 _ecore_xcb_dpms_init(void)
11 {
12    LOGFN(__FILE__, __LINE__, __FUNCTION__);
13
14 #ifdef ECORE_XCB_DPMS
15    xcb_prefetch_extension_data(_ecore_xcb_conn, &xcb_dpms_id);
16 #endif
17 }
18
19 void
20 _ecore_xcb_dpms_finalize(void)
21 {
22 #ifdef ECORE_XCB_DPMS
23    const xcb_query_extension_reply_t *ext_reply;
24 #endif
25
26    LOGFN(__FILE__, __LINE__, __FUNCTION__);
27
28 #ifdef ECORE_XCB_DPMS
29    ext_reply = xcb_get_extension_data(_ecore_xcb_conn, &xcb_dpms_id);
30    if ((ext_reply) && (ext_reply->present))
31      {
32         xcb_dpms_get_version_cookie_t cookie;
33         xcb_dpms_get_version_reply_t *reply;
34
35         cookie =
36           xcb_dpms_get_version_unchecked(_ecore_xcb_conn,
37                                          XCB_DPMS_MAJOR_VERSION,
38                                          XCB_DPMS_MINOR_VERSION);
39         reply = xcb_dpms_get_version_reply(_ecore_xcb_conn, cookie, NULL);
40         if (reply)
41           {
42              if (reply->server_major_version >= 1)
43                _dpms_avail = EINA_TRUE;
44              free(reply);
45           }
46      }
47 #endif
48 }
49
50 /**
51  * @defgroup Ecore_X_DPMS_Group X DPMS Extension Functions
52  * @ingroup Ecore_X_Group
53  *
54  * Functions related to the X DPMS Extension
55  */
56
57 /**
58  * Checks if the DPMS extension is available or not.
59  *
60  * @return @c EINA_TRUE if the DPMS extension is available,
61  * @c EINA_FALSE otherwise.
62  *
63  * Return @c EINA_TRUE if the X server supports the DPMS Extension version 1.0,
64  * @c EINA_FALSE otherwise.
65  *
66  * @ingroup Ecore_X_DPMS_Group
67  */
68 EAPI Eina_Bool
69 ecore_x_dpms_query(void)
70 {
71 //   LOGFN(__FILE__, __LINE__, __FUNCTION__);
72
73      return _dpms_avail;
74 }
75
76 /**
77  * Checks if the X server is capable of DPMS.
78  * @return @c 1 if the X server is capable of DPMS, @c 0 otherwise.
79  * @ingroup Ecore_X_DPMS_Group
80  */
81 EAPI Eina_Bool
82 ecore_x_dpms_capable_get(void)
83 {
84    Eina_Bool ret = EINA_FALSE;
85 #ifdef ECORE_XCB_DPMS
86    xcb_dpms_capable_cookie_t cookie;
87    xcb_dpms_capable_reply_t *reply;
88 #endif
89
90    LOGFN(__FILE__, __LINE__, __FUNCTION__);
91    CHECK_XCB_CONN;
92
93    if (!_dpms_avail) return EINA_FALSE;
94
95 #ifdef ECORE_XCB_DPMS
96    cookie = xcb_dpms_capable_unchecked(_ecore_xcb_conn);
97    reply = xcb_dpms_capable_reply(_ecore_xcb_conn, cookie, NULL);
98    if (reply)
99      {
100         ret = reply->capable;
101         free(reply);
102      }
103 #endif
104
105    return ret;
106 }
107
108 /**
109  * Checks the DPMS state of the display.
110  * @return @c EINA_TRUE if DPMS is enabled, @c EINA_FALSE otherwise.
111  * @ingroup Ecore_X_DPMS_Group
112  */
113 EAPI Eina_Bool
114 ecore_x_dpms_enabled_get(void)
115 {
116    Eina_Bool ret = EINA_FALSE;
117 #ifdef ECORE_XCB_DPMS
118    xcb_dpms_info_cookie_t cookie;
119    xcb_dpms_info_reply_t *reply;
120 #endif
121
122    LOGFN(__FILE__, __LINE__, __FUNCTION__);
123    CHECK_XCB_CONN;
124
125    if (!_dpms_avail) return EINA_FALSE;
126
127 #ifdef ECORE_XCB_DPMS
128    cookie = xcb_dpms_info_unchecked(_ecore_xcb_conn);
129    reply = xcb_dpms_info_reply(_ecore_xcb_conn, cookie, NULL);
130    if (!reply) return EINA_FALSE;
131    if (reply->state) ret = EINA_TRUE;
132    free(reply);
133 #endif
134
135    return ret;
136 }
137
138 /**
139  * Sets the DPMS state of the display.
140  * @param enabled @c 0 to disable DPMS characteristics of the server, enable it otherwise.
141  * @ingroup Ecore_X_DPMS_Group
142  */
143 EAPI void
144 ecore_x_dpms_enabled_set(int enabled)
145 {
146    LOGFN(__FILE__, __LINE__, __FUNCTION__);
147    CHECK_XCB_CONN;
148
149    if (!_dpms_avail) return;
150
151 #ifdef ECORE_XCB_DPMS
152    if (enabled)
153      xcb_dpms_enable(_ecore_xcb_conn);
154    else
155      xcb_dpms_disable(_ecore_xcb_conn);
156 #endif
157 }
158
159 /**
160  * Gets the timeouts. The values are in unit of seconds.
161  * @param standby Amount of time of inactivity before standby mode will be invoked.
162  * @param suspend Amount of time of inactivity before the screen is placed into suspend mode.
163  * @param off     Amount of time of inactivity before the monitor is shut off.
164  * @ingroup Ecore_X_DPMS_Group
165  */
166 EAPI void
167 ecore_x_dpms_timeouts_get(unsigned int *standby,
168                           unsigned int *suspend,
169                           unsigned int *off)
170 {
171 #ifdef ECORE_XCB_DPMS
172    xcb_dpms_get_timeouts_cookie_t cookie;
173    xcb_dpms_get_timeouts_reply_t *reply;
174 #endif
175
176    LOGFN(__FILE__, __LINE__, __FUNCTION__);
177    CHECK_XCB_CONN;
178
179    if (standby) *standby = 0;
180    if (suspend) *suspend = 0;
181    if (off) *off = 0;
182
183    if (!_dpms_avail) return;
184
185 #ifdef ECORE_XCB_DPMS
186    cookie = xcb_dpms_get_timeouts_unchecked(_ecore_xcb_conn);
187    reply = xcb_dpms_get_timeouts_reply(_ecore_xcb_conn, cookie, NULL);
188    if (!reply) return;
189    if (standby) *standby = reply->standby_timeout;
190    if (suspend) *suspend = reply->suspend_timeout;
191    if (off) *off = reply->off_timeout;
192    free(reply);
193 #endif
194 }
195
196 /**
197  * Sets the timeouts. The values are in unit of seconds.
198  *
199  * @param standby Amount of time of inactivity before standby mode will be invoked.
200  * @param suspend Amount of time of inactivity before the screen is placed into suspend mode.
201  * @param off     Amount of time of inactivity before the monitor is shut off.
202  * @return @c EINA_TRUE on success, @c EINA_FALSE on failure.
203  * @ingroup Ecore_X_DPMS_Group
204  */
205 EAPI Eina_Bool
206 ecore_x_dpms_timeouts_set(unsigned int standby,
207                           unsigned int suspend,
208                           unsigned int off)
209 {
210    LOGFN(__FILE__, __LINE__, __FUNCTION__);
211    CHECK_XCB_CONN;
212
213    if (!_dpms_avail) return EINA_FALSE;
214
215 #ifdef ECORE_XCB_DPMS
216    // FIXME: Add request check
217    xcb_dpms_set_timeouts(_ecore_xcb_conn, standby, suspend, off);
218    return EINA_TRUE;
219 #endif
220
221    return EINA_FALSE;
222 }
223
224 /**
225  * Returns the amount of time of inactivity before standby mode is invoked.
226  * @return The standby timeout value.
227  * @ingroup Ecore_X_DPMS_Group
228  */
229 EAPI unsigned int
230 ecore_x_dpms_timeout_standby_get(void)
231 {
232    unsigned int standby = 0;
233
234    LOGFN(__FILE__, __LINE__, __FUNCTION__);
235
236    ecore_x_dpms_timeouts_get(&standby, NULL, NULL);
237    return standby;
238 }
239
240 /**
241  * Returns the amount of time of inactivity before the second level of
242  * power saving is invoked.
243  * @return The suspend timeout value.
244  * @ingroup Ecore_X_DPMS_Group
245  */
246 EAPI unsigned int
247 ecore_x_dpms_timeout_suspend_get(void)
248 {
249    unsigned int suspend = 0;
250
251    LOGFN(__FILE__, __LINE__, __FUNCTION__);
252
253    ecore_x_dpms_timeouts_get(NULL, &suspend, NULL);
254    return suspend;
255 }
256
257 /**
258  * Returns the amount of time of inactivity before the third and final
259  * level of power saving is invoked.
260  * @return The off timeout value.
261  * @ingroup Ecore_X_DPMS_Group
262  */
263 EAPI unsigned int
264 ecore_x_dpms_timeout_off_get(void)
265 {
266    unsigned int off = 0;
267
268    LOGFN(__FILE__, __LINE__, __FUNCTION__);
269
270    ecore_x_dpms_timeouts_get(NULL, NULL, &off);
271    return off;
272 }
273
274 /**
275  * Sets the standby timeout (in unit of seconds).
276  * @param new_timeout Amount of time of inactivity before standby mode will be invoked.
277  * @ingroup Ecore_X_DPMS_Group
278  */
279 EAPI void
280 ecore_x_dpms_timeout_standby_set(unsigned int new_timeout)
281 {
282    unsigned int standby = 0, suspend = 0, off = 0;
283
284    LOGFN(__FILE__, __LINE__, __FUNCTION__);
285
286    ecore_x_dpms_timeouts_get(&standby, &suspend, &off);
287    ecore_x_dpms_timeouts_set(new_timeout, suspend, off);
288 }
289
290 /**
291  * Sets the suspend timeout (in unit of seconds).
292  * @param new_timeout Amount of time of inactivity before the screen is placed into suspend mode.
293  * @ingroup Ecore_X_DPMS_Group
294  */
295 EAPI void
296 ecore_x_dpms_timeout_suspend_set(unsigned int new_timeout)
297 {
298    unsigned int standby = 0, suspend = 0, off = 0;
299
300    LOGFN(__FILE__, __LINE__, __FUNCTION__);
301
302    ecore_x_dpms_timeouts_get(&standby, &suspend, &off);
303    ecore_x_dpms_timeouts_set(standby, new_timeout, off);
304 }
305
306 /**
307  * Sets the off timeout (in unit of seconds).
308  * @param new_timeout     Amount of time of inactivity before the monitor is shut off.
309  * @ingroup Ecore_X_DPMS_Group
310  */
311 EAPI void
312 ecore_x_dpms_timeout_off_set(unsigned int new_timeout)
313 {
314    unsigned int standby = 0, suspend = 0, off = 0;
315
316    LOGFN(__FILE__, __LINE__, __FUNCTION__);
317
318    ecore_x_dpms_timeouts_get(&standby, &suspend, &off);
319    ecore_x_dpms_timeouts_set(standby, suspend, new_timeout);
320 }
321
322 /**
323  * Check the DPMS power level.
324  * @return @c 0 if DPMS is :In Use
325  * @return @c 1 if DPMS is :Blanked, low power
326  * @return @c 2 if DPMS is :Blanked, lower power
327  * @return @c 3 if DPMS is :Shut off, awaiting activity
328  * @return @c -1 otherwise.
329 */
330 EAPI Ecore_X_Dpms_Mode 
331 ecore_x_dpms_power_level_get(void)
332 {
333    Ecore_X_Dpms_Mode ret = -1;
334 #ifdef ECORE_XCB_DPMS
335    xcb_dpms_info_cookie_t cookie;
336    xcb_dpms_info_reply_t *reply;
337 #endif
338
339    LOGFN(__FILE__, __LINE__, __FUNCTION__);
340    CHECK_XCB_CONN;
341
342    if (!_dpms_avail) return ret;
343
344 #ifdef ECORE_XCB_DPMS
345    cookie = xcb_dpms_info_unchecked(_ecore_xcb_conn);
346    reply = xcb_dpms_info_reply(_ecore_xcb_conn, cookie, NULL);
347    if (!reply) return -1;
348
349    ret = reply->power_level;
350    free(reply);
351 #endif
352
353    return ret;
354 }