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