Tizen 2.1 base
[framework/uifw/ecore.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  *
53  * Functions related to the X DPMS Extension
54  */
55
56 /**
57  * Checks if the DPMS extension is available or not.
58  *
59  * @return @c EINA_TRUE if the DPMS extension is available,
60  * @c EINA_FALSE otherwise.
61  *
62  * Return @c EINA_TRUE if the X server supports the DPMS Extension version 1.0,
63  * @c EINA_FALSE otherwise.
64  *
65  * @ingroup Ecore_X_DPMS_Group
66  */
67 EAPI Eina_Bool
68 ecore_x_dpms_query(void)
69 {
70 //   LOGFN(__FILE__, __LINE__, __FUNCTION__);
71
72      return _dpms_avail;
73 }
74
75 /**
76  * Checks if the X server is capable of DPMS.
77  * @return @c 1 if the X server is capable of DPMS, @c 0 otherwise.
78  * @ingroup Ecore_X_DPMS_Group
79  */
80 EAPI Eina_Bool
81 ecore_x_dpms_capable_get(void)
82 {
83    Eina_Bool ret = EINA_FALSE;
84 #ifdef ECORE_XCB_DPMS
85    xcb_dpms_capable_cookie_t cookie;
86    xcb_dpms_capable_reply_t *reply;
87 #endif
88
89    LOGFN(__FILE__, __LINE__, __FUNCTION__);
90    CHECK_XCB_CONN;
91
92    if (!_dpms_avail) return EINA_FALSE;
93
94 #ifdef ECORE_XCB_DPMS
95    cookie = xcb_dpms_capable_unchecked(_ecore_xcb_conn);
96    reply = xcb_dpms_capable_reply(_ecore_xcb_conn, cookie, NULL);
97    if (reply)
98      {
99         ret = reply->capable;
100         free(reply);
101      }
102 #endif
103
104    return ret;
105 }
106
107 /**
108  * Checks the DPMS state of the display.
109  * @return @c EINA_TRUE if DPMS is enabled, @c EINA_FALSE otherwise.
110  * @ingroup Ecore_X_DPMS_Group
111  */
112 EAPI Eina_Bool
113 ecore_x_dpms_enabled_get(void)
114 {
115    Eina_Bool ret = EINA_FALSE;
116 #ifdef ECORE_XCB_DPMS
117    xcb_dpms_info_cookie_t cookie;
118    xcb_dpms_info_reply_t *reply;
119 #endif
120
121    LOGFN(__FILE__, __LINE__, __FUNCTION__);
122    CHECK_XCB_CONN;
123
124    if (!_dpms_avail) return EINA_FALSE;
125
126 #ifdef ECORE_XCB_DPMS
127    cookie = xcb_dpms_info_unchecked(_ecore_xcb_conn);
128    reply = xcb_dpms_info_reply(_ecore_xcb_conn, cookie, NULL);
129    if (!reply) return EINA_FALSE;
130    if (reply->state) ret = EINA_TRUE;
131    free(reply);
132 #endif
133
134    return ret;
135 }
136
137 /**
138  * Sets the DPMS state of the display.
139  * @param enabled @c 0 to disable DPMS characteristics of the server, enable it otherwise.
140  * @ingroup Ecore_X_DPMS_Group
141  */
142 EAPI void
143 ecore_x_dpms_enabled_set(int enabled)
144 {
145    LOGFN(__FILE__, __LINE__, __FUNCTION__);
146    CHECK_XCB_CONN;
147
148    if (!_dpms_avail) return;
149
150 #ifdef ECORE_XCB_DPMS
151    if (enabled)
152      xcb_dpms_enable(_ecore_xcb_conn);
153    else
154      xcb_dpms_disable(_ecore_xcb_conn);
155 #endif
156 }
157
158 /**
159  * Gets the timeouts. The values are in unit of seconds.
160  * @param standby Amount of time of inactivity before standby mode will be invoked.
161  * @param suspend Amount of time of inactivity before the screen is placed into suspend mode.
162  * @param off     Amount of time of inactivity before the monitor is shut off.
163  * @ingroup Ecore_X_DPMS_Group
164  */
165 EAPI void
166 ecore_x_dpms_timeouts_get(unsigned int *standby,
167                           unsigned int *suspend,
168                           unsigned int *off)
169 {
170 #ifdef ECORE_XCB_DPMS
171    xcb_dpms_get_timeouts_cookie_t cookie;
172    xcb_dpms_get_timeouts_reply_t *reply;
173 #endif
174
175    LOGFN(__FILE__, __LINE__, __FUNCTION__);
176    CHECK_XCB_CONN;
177
178    if (standby) *standby = 0;
179    if (suspend) *suspend = 0;
180    if (off) *off = 0;
181
182    if (!_dpms_avail) return;
183
184 #ifdef ECORE_XCB_DPMS
185    cookie = xcb_dpms_get_timeouts_unchecked(_ecore_xcb_conn);
186    reply = xcb_dpms_get_timeouts_reply(_ecore_xcb_conn, cookie, NULL);
187    if (!reply) return;
188    if (standby) *standby = reply->standby_timeout;
189    if (suspend) *suspend = reply->suspend_timeout;
190    if (off) *off = reply->off_timeout;
191    free(reply);
192 #endif
193 }
194
195 /**
196  * Sets the timeouts. The values are in unit of seconds.
197  *
198  * @param standby Amount of time of inactivity before standby mode will be invoked.
199  * @param suspend Amount of time of inactivity before the screen is placed into suspend mode.
200  * @param off     Amount of time of inactivity before the monitor is shut off.
201  * @return @c EINA_TRUE on success, @c EINA_FALSE on failure.
202  * @ingroup Ecore_X_DPMS_Group
203  */
204 EAPI Eina_Bool
205 ecore_x_dpms_timeouts_set(unsigned int standby,
206                           unsigned int suspend,
207                           unsigned int off)
208 {
209    LOGFN(__FILE__, __LINE__, __FUNCTION__);
210    CHECK_XCB_CONN;
211
212    if (!_dpms_avail) return EINA_FALSE;
213
214 #ifdef ECORE_XCB_DPMS
215    // FIXME: Add request check
216    xcb_dpms_set_timeouts(_ecore_xcb_conn, standby, suspend, off);
217    return EINA_TRUE;
218 #endif
219
220    return EINA_FALSE;
221 }
222
223 /**
224  * Returns the amount of time of inactivity before standby mode is invoked.
225  * @return The standby timeout value.
226  * @ingroup Ecore_X_DPMS_Group
227  */
228 EAPI unsigned int
229 ecore_x_dpms_timeout_standby_get(void)
230 {
231    unsigned int standby = 0;
232
233    LOGFN(__FILE__, __LINE__, __FUNCTION__);
234
235    ecore_x_dpms_timeouts_get(&standby, NULL, NULL);
236    return standby;
237 }
238
239 /**
240  * Returns the amount of time of inactivity before the second level of
241  * power saving is invoked.
242  * @return The suspend timeout value.
243  * @ingroup Ecore_X_DPMS_Group
244  */
245 EAPI unsigned int
246 ecore_x_dpms_timeout_suspend_get(void)
247 {
248    unsigned int suspend = 0;
249
250    LOGFN(__FILE__, __LINE__, __FUNCTION__);
251
252    ecore_x_dpms_timeouts_get(NULL, &suspend, NULL);
253    return suspend;
254 }
255
256 /**
257  * Returns the amount of time of inactivity before the third and final
258  * level of power saving is invoked.
259  * @return The off timeout value.
260  * @ingroup Ecore_X_DPMS_Group
261  */
262 EAPI unsigned int
263 ecore_x_dpms_timeout_off_get(void)
264 {
265    unsigned int off = 0;
266
267    LOGFN(__FILE__, __LINE__, __FUNCTION__);
268
269    ecore_x_dpms_timeouts_get(NULL, NULL, &off);
270    return off;
271 }
272
273 /**
274  * Sets the standby timeout (in unit of seconds).
275  * @param new_timeout Amount of time of inactivity before standby mode will be invoked.
276  * @ingroup Ecore_X_DPMS_Group
277  */
278 EAPI void
279 ecore_x_dpms_timeout_standby_set(unsigned int new_timeout)
280 {
281    unsigned int standby = 0, suspend = 0, off = 0;
282
283    LOGFN(__FILE__, __LINE__, __FUNCTION__);
284
285    ecore_x_dpms_timeouts_get(&standby, &suspend, &off);
286    ecore_x_dpms_timeouts_set(new_timeout, suspend, off);
287 }
288
289 /**
290  * Sets the suspend timeout (in unit of seconds).
291  * @param new_timeout Amount of time of inactivity before the screen is placed into suspend mode.
292  * @ingroup Ecore_X_DPMS_Group
293  */
294 EAPI void
295 ecore_x_dpms_timeout_suspend_set(unsigned int new_timeout)
296 {
297    unsigned int standby = 0, suspend = 0, off = 0;
298
299    LOGFN(__FILE__, __LINE__, __FUNCTION__);
300
301    ecore_x_dpms_timeouts_get(&standby, &suspend, &off);
302    ecore_x_dpms_timeouts_set(standby, new_timeout, off);
303 }
304
305 /**
306  * Sets the off timeout (in unit of seconds).
307  * @param new_timeout     Amount of time of inactivity before the monitor is shut off.
308  * @ingroup Ecore_X_DPMS_Group
309  */
310 EAPI void
311 ecore_x_dpms_timeout_off_set(unsigned int new_timeout)
312 {
313    unsigned int standby = 0, suspend = 0, off = 0;
314
315    LOGFN(__FILE__, __LINE__, __FUNCTION__);
316
317    ecore_x_dpms_timeouts_get(&standby, &suspend, &off);
318    ecore_x_dpms_timeouts_set(standby, suspend, new_timeout);
319 }
320