4ffd5b14d41afb4ba1d4cd1c106f58a9ed32874e
[profile/ivi/ecore.git] / src / lib / ecore_x / xcb / ecore_xcb_dpms.c
1 #include "ecore_xcb_private.h"
2
3 /**
4  * @defgroup Ecore_X_DPMS_Group X DPMS Extension Functions
5  *
6  * Functions related to the X DPMS extension.
7  */
8
9 #ifdef ECORE_XCB_DPMS
10 static int _dpms_available = 0;
11 static xcb_dpms_get_version_cookie_t _ecore_xcb_dpms_init_cookie;
12 #endif /* ECORE_XCB_DPMS */
13
14 /* To avoid round trips, the initialization is separated in 2
15    functions: _ecore_xcb_dpms_init and
16    _ecore_xcb_dpms_init_finalize. The first one gets the cookies and
17    the second one gets the replies. */
18
19 void
20 _ecore_x_dpms_init(const xcb_query_extension_reply_t *reply)
21 {
22 #ifdef ECORE_XCB_DPMS
23    if (reply && (reply->present))
24       _ecore_xcb_dpms_init_cookie = xcb_dpms_get_version_unchecked(_ecore_xcb_conn, 0, 0);
25
26 #endif /* ECORE_XCB_DPMS */
27 } /* _ecore_x_dpms_init */
28
29 void
30 _ecore_x_dpms_init_finalize(void)
31 {
32 #ifdef ECORE_XCB_DPMS
33    xcb_dpms_get_version_reply_t *reply;
34
35    reply = xcb_dpms_get_version_reply(_ecore_xcb_conn,
36                                       _ecore_xcb_dpms_init_cookie, NULL);
37
38    if (reply)
39      {
40         if (reply->server_major_version >= 1)
41            _dpms_available = 1;
42
43         free(reply);
44      }
45
46 #endif /* ECORE_XCB_DPMS */
47 } /* _ecore_x_dpms_init_finalize */
48
49 /**
50  * Checks if the DPMS extension is available or not.
51  * @return @c 1 if the DPMS extension is available, @c 0 otherwise.
52  *
53  * Return 1 if the X server supports the DPMS Extension version 1.0,
54  * 0 otherwise.
55  * @ingroup Ecore_X_DPMS_Group
56  */
57 EAPI int
58 ecore_x_dpms_query(void)
59 {
60 #ifdef ECORE_XCB_DPMS
61    return _dpms_available;
62 #else /* ifdef ECORE_XCB_DPMS */
63    return 0;
64 #endif /* ECORE_XCB_DPMS */
65 } /* ecore_x_dpms_query */
66
67 /**
68  * Sends the DPMSCapable request.
69  * @ingroup Ecore_X_DPMS_Group
70  */
71 EAPI void
72 ecore_x_dpms_capable_get_prefetch(void)
73 {
74 #ifdef ECORE_XCB_DPMS
75    xcb_dpms_capable_cookie_t cookie;
76
77    cookie = xcb_dpms_capable_unchecked(_ecore_xcb_conn);
78    _ecore_xcb_cookie_cache(cookie.sequence);
79 #endif /* ECORE_XCB_DPMS */
80 } /* ecore_x_dpms_capable_get_prefetch */
81
82 /**
83  * Gets the reply of the DPMSCapable request sent by ecore_x_dpms_capable_get_prefetch().
84  * @ingroup Ecore_X_DPMS_Group
85  */
86 EAPI void
87 ecore_x_dpms_capable_get_fetch(void)
88 {
89 #ifdef ECORE_XCB_DPMS
90    xcb_dpms_capable_cookie_t cookie;
91    xcb_dpms_capable_reply_t *reply;
92
93    cookie.sequence = _ecore_xcb_cookie_get();
94    reply = xcb_dpms_capable_reply(_ecore_xcb_conn, cookie, NULL);
95    _ecore_xcb_reply_cache(reply);
96 #endif /* ECORE_XCB_DPMS */
97 } /* ecore_x_dpms_capable_get_fetch */
98
99 /**
100  * Checks if the X server is capable of DPMS.
101  * @return @c 1 if the X server is capable of DPMS, @c 0 otherwise.
102  *
103  * To use this function, you must call before, and in order,
104  * ecore_x_dpms_capable_get_prefetch(), which sends the DPMSCapable request,
105  * then ecore_x_dpms_capable_get_fetch(), which gets the reply.
106  * @ingroup Ecore_X_DPMS_Group
107  */
108 EAPI int
109 ecore_x_dpms_capable_get(void)
110 {
111    int capable = 0;
112 #ifdef ECORE_XCB_DPMS
113    xcb_dpms_capable_reply_t *reply;
114
115    reply = _ecore_xcb_reply_get();
116    if (!reply)
117       return 0;
118
119    capable = reply->capable;
120 #endif /* ECORE_XCB_DPMS */
121
122    return capable;
123 } /* ecore_x_dpms_capable_get */
124
125 /**
126  * Sends the DPMSInfo request.
127  * @ingroup Ecore_X_DPMS_Group
128  */
129 EAPI void
130 ecore_x_dpms_enable_get_prefetch(void)
131 {
132 #ifdef ECORE_XCB_DPMS
133    xcb_dpms_info_cookie_t cookie;
134
135    cookie = xcb_dpms_info_unchecked(_ecore_xcb_conn);
136    _ecore_xcb_cookie_cache(cookie.sequence);
137 #endif /* ECORE_XCB_DPMS */
138 } /* ecore_x_dpms_enable_get_prefetch */
139
140 /**
141  * Gets the reply of the DPMSInfo request sent by ecore_x_dpms_enable_get_prefetch().
142  * @ingroup Ecore_X_DPMS_Group
143  */
144 EAPI void
145 ecore_x_dpms_enable_get_fetch(void)
146 {
147 #ifdef ECORE_XCB_DPMS
148    xcb_dpms_info_cookie_t cookie;
149    xcb_dpms_info_reply_t *reply;
150
151    cookie.sequence = _ecore_xcb_cookie_get();
152    reply = xcb_dpms_info_reply(_ecore_xcb_conn, cookie, NULL);
153    _ecore_xcb_reply_cache(reply);
154 #endif /* ECORE_XCB_DPMS */
155 } /* ecore_x_dpms_enable_get_fetch */
156
157 /**
158  * Checks the DPMS state of the display.
159  * @return @c 1 if DPMS is enabled, @c 0 otherwise.
160  *
161  * To use this function, you must call before, and in order,
162  * ecore_x_dpms_enapable_get_prefetch(), which sends the DPMSInfo request,
163  * then ecore_x_dpms_enapable_get_fetch(), which gets the reply.
164  * @ingroup Ecore_X_DPMS_Group
165  */
166 EAPI int
167 ecore_x_dpms_enable_get(void)
168 {
169    int enable = 0;
170 #ifdef ECORE_XCB_DPMS
171    xcb_dpms_info_reply_t *reply;
172
173    reply = _ecore_xcb_reply_get();
174    if (!reply)
175       return 0;
176
177    enable = reply->state;
178 #endif /* ECORE_XCB_DPMS */
179
180    return enable;
181 } /* ecore_x_dpms_enable_get */
182
183 /**
184  * Sets the DPMS state of the display.
185  * @param enabled @c 0 to disable DPMS characteristics of the server, enable it otherwise.
186  * @ingroup Ecore_X_DPMS_Group
187  */
188 EAPI void
189 ecore_x_dpms_enabled_set(int enabled)
190 {
191 #ifdef ECORE_XCB_DPMS
192    if (enabled)
193       xcb_dpms_enable(_ecore_xcb_conn);
194    else
195       xcb_dpms_disable(_ecore_xcb_conn);
196
197 #endif /* ECORE_XCB_DPMS */
198 } /* ecore_x_dpms_enabled_set */
199
200 /**
201  * Sets the timeouts. The values are in unit of seconds.
202  * @param standby Amount of time of inactivity before standby mode will be invoked.
203  * @param suspend Amount of time of inactivity before the screen is placed into suspend mode.
204  * @param off     Amount of time of inactivity before the monitor is shut off.
205  * @return        Returns always 1.
206  * @ingroup Ecore_X_DPMS_Group
207  */
208 EAPI int
209 ecore_x_dpms_timeouts_set(unsigned int standby,
210                           unsigned int suspend,
211                           unsigned int off)
212 {
213 #ifdef ECORE_XCB_DPMS
214    xcb_dpms_set_timeouts(_ecore_xcb_conn, standby, suspend, off);
215 #endif /* ECORE_XCB_DPMS */
216
217    return 1;
218 } /* ecore_x_dpms_timeouts_set */
219
220 /**
221  * Sends the DPMSGetTimeouts request.
222  * @ingroup Ecore_X_DPMS_Group
223  */
224 EAPI void
225 ecore_x_dpms_timeouts_get_prefetch(void)
226 {
227 #ifdef ECORE_XCB_DPMS
228    xcb_dpms_get_timeouts_cookie_t cookie;
229
230    cookie = xcb_dpms_get_timeouts_unchecked(_ecore_xcb_conn);
231    _ecore_xcb_cookie_cache(cookie.sequence);
232 #endif /* ECORE_XCB_DPMS */
233 } /* ecore_x_dpms_timeouts_get_prefetch */
234
235 /**
236  * Gets the reply of the DPMSGetTimeouts request sent by ecore_x_dpms_timeouts_get_prefetch().
237  * @ingroup Ecore_X_DPMS_Group
238  */
239 EAPI void
240 ecore_x_dpms_timeouts_get_fetch(void)
241 {
242 #ifdef ECORE_XCB_DPMS
243    xcb_dpms_get_timeouts_cookie_t cookie;
244    xcb_dpms_get_timeouts_reply_t *reply;
245
246    cookie.sequence = _ecore_xcb_cookie_get();
247    reply = xcb_dpms_get_timeouts_reply(_ecore_xcb_conn, cookie, NULL);
248    _ecore_xcb_reply_cache(reply);
249 #endif /* ECORE_XCB_DPMS */
250 } /* ecore_x_dpms_timeouts_get_fetch */
251
252 /**
253  * Gets the timeouts. The values are in unit of seconds.
254  * @param standby Amount of time of inactivity before standby mode will be invoked.
255  * @param suspend Amount of time of inactivity before the screen is placed into suspend mode.
256  * @param off     Amount of time of inactivity before the monitor is shut off.
257  * @ingroup Ecore_X_DPMS_Group
258  */
259 EAPI void
260 ecore_x_dpms_timeouts_get(unsigned int *standby,
261                           unsigned int *suspend,
262                           unsigned int *off)
263 {
264 #ifdef ECORE_XCB_DPMS
265    xcb_dpms_get_timeouts_reply_t *reply;
266
267    reply = _ecore_xcb_reply_get();
268    if (reply)
269      {
270         if (standby)
271            *standby = reply->standby_timeout;
272
273         if (suspend)
274            *suspend = reply->suspend_timeout;
275
276         if (off)
277            *off = 0;
278      }
279    else
280 #endif /* ECORE_XCB_DPMS */
281    {
282       if (standby)
283          *standby = 0;
284
285       if (suspend)
286          *suspend = 0;
287
288       if (off)
289          *off = 0;
290    }
291 } /* ecore_x_dpms_timeouts_get */
292
293 /**
294  * Returns the amount of time of inactivity before standby mode is invoked.
295  * @return The standby timeout value.
296  *
297  * To use this function, you must call before, and in order,
298  * ecore_x_dpms_timeouts_get_prefetch(), which sends the DPMSGetTimeouts request,
299  * then ecore_x_dpms_timeouts_get_fetch(), which gets the reply.
300  * @ingroup Ecore_X_DPMS_Group
301  */
302 EAPI unsigned int
303 ecore_x_dpms_timeout_standby_get(void)
304 {
305    int standby = 0;
306 #ifdef ECORE_XCB_DPMS
307    xcb_dpms_get_timeouts_reply_t *reply;
308
309    reply = _ecore_xcb_reply_get();
310    if (!reply)
311       return 0;
312
313    standby = reply->standby_timeout;
314 #endif /* ECORE_XCB_DPMS */
315
316    return standby;
317 } /* ecore_x_dpms_timeout_standby_get */
318
319 /**
320  * Returns the amount of time of inactivity before the second level of
321  * power saving is invoked.
322  * @return The suspend timeout value.
323  *
324  * To use this function, you must call before, and in order,
325  * ecore_x_dpms_timeouts_get_prefetch(), which sends the DPMSGetTimeouts request,
326  * then ecore_x_dpms_timeouts_get_fetch(), which gets the reply.
327  * @ingroup Ecore_X_DPMS_Group
328  */
329 EAPI unsigned int
330 ecore_x_dpms_timeout_suspend_get(void)
331 {
332    int suspend = 0;
333 #ifdef ECORE_XCB_DPMS
334    xcb_dpms_get_timeouts_reply_t *reply;
335
336    reply = _ecore_xcb_reply_get();
337    if (!reply)
338       return 0;
339
340    suspend = reply->suspend_timeout;
341 #endif /* ECORE_XCB_DPMS */
342
343    return suspend;
344 } /* ecore_x_dpms_timeout_suspend_get */
345
346 /**
347  * Returns the amount of time of inactivity before the third and final
348  * level of power saving is invoked.
349  * @return The off timeout value.
350  *
351  * To use this function, you must call before, and in order,
352  * ecore_x_dpms_timeouts_get_prefetch(), which sends the DPMSGetTimeouts request,
353  * then ecore_x_dpms_timeouts_get_fetch(), which gets the reply.
354  * @ingroup Ecore_X_DPMS_Group
355  */
356 EAPI unsigned int
357 ecore_x_dpms_timeout_off_get(void)
358 {
359    int off = 0;
360 #ifdef ECORE_XCB_DPMS
361    xcb_dpms_get_timeouts_reply_t *reply;
362
363    reply = _ecore_xcb_reply_get();
364    if (!reply)
365       return 0;
366
367    off = reply->off_timeout;
368 #endif /* ECORE_XCB_DPMS */
369
370    return off;
371 } /* ecore_x_dpms_timeout_off_get */
372
373 /**
374  * Sets the standby timeout (in unit of seconds).
375  * @param new_standby Amount of time of inactivity before standby mode will be invoked.
376  *
377  * To use this function, you must call before, and in order,
378  * ecore_x_dpms_timeouts_get_prefetch(), which sends the DPMSGetTimeouts request,
379  * then ecore_x_dpms_timeouts_get_fetch(), which gets the reply.
380  * @ingroup Ecore_X_DPMS_Group
381  */
382 EAPI void
383 ecore_x_dpms_timeout_standby_set(unsigned int new_standby)
384 {
385 #ifdef ECORE_XCB_DPMS
386    xcb_dpms_get_timeouts_reply_t *reply;
387
388    reply = _ecore_xcb_reply_get();
389    if (!reply)
390       return;
391
392    xcb_dpms_set_timeouts(_ecore_xcb_conn,
393                          new_standby,
394                          reply->suspend_timeout,
395                          reply->off_timeout);
396 #endif /* ECORE_XCB_DPMS */
397 } /* ecore_x_dpms_timeout_standby_set */
398
399 /**
400  * Sets the suspend timeout (in unit of seconds).
401  * @param suspend Amount of time of inactivity before the screen is placed into suspend mode.
402  *
403  * To use this function, you must call before, and in order,
404  * ecore_x_dpms_timeouts_get_prefetch(), which sends the DPMSGetTimeouts request,
405  * then ecore_x_dpms_timeouts_get_fetch(), which gets the reply.
406  * @ingroup Ecore_X_DPMS_Group
407  */
408 EAPI void
409 ecore_x_dpms_timeout_suspend_set(unsigned int new_suspend)
410 {
411 #ifdef ECORE_XCB_DPMS
412    xcb_dpms_get_timeouts_reply_t *reply;
413
414    reply = _ecore_xcb_reply_get();
415    if (!reply)
416       return;
417
418    xcb_dpms_set_timeouts(_ecore_xcb_conn,
419                          reply->standby_timeout,
420                          new_suspend,
421                          reply->off_timeout);
422 #endif /* ECORE_XCB_DPMS */
423 } /* ecore_x_dpms_timeout_suspend_set */
424
425 /**
426  * Sets the off timeout (in unit of seconds).
427  * @param off     Amount of time of inactivity before the monitor is shut off.
428  *
429  * To use this function, you must call before, and in order,
430  * ecore_x_dpms_timeouts_get_prefetch(), which sends the DPMSGetTimeouts request,
431  * then ecore_x_dpms_timeouts_get_fetch(), which gets the reply.
432  * @ingroup Ecore_X_DPMS_Group
433  */
434 EAPI void
435 ecore_x_dpms_timeout_off_set(unsigned int new_off)
436 {
437 #ifdef ECORE_XCB_DPMS
438    xcb_dpms_get_timeouts_reply_t *reply;
439
440    reply = _ecore_xcb_reply_get();
441    if (!reply)
442       return;
443
444    xcb_dpms_set_timeouts(_ecore_xcb_conn,
445                          reply->standby_timeout,
446                          reply->suspend_timeout,
447                          new_off);
448 #endif /* ECORE_XCB_DPMS */
449 } /* ecore_x_dpms_timeout_off_set */
450