svn update: 48958 (latest:48959)
[framework/uifw/ecore.git] / src / lib / ecore_x / xcb / ecore_xcb_screensaver.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_ScreenSaver_Group X ScreenSaver extension
10  *
11  * These functions use the ScreenSaver extension of the X server
12   */
13
14
15 #ifdef ECORE_XCB_SCREENSAVER
16 static int _screensaver_available = 0;
17 static xcb_screensaver_query_version_cookie_t _ecore_xcb_screensaver_init_cookie;
18 #endif /* ECORE_XCB_SCREENSAVER */
19
20
21 /* To avoid round trips, the initialization is separated in 2
22    functions: _ecore_xcb_screensaver_init and
23    _ecore_xcb_screensaver_init_finalize. The first one gets the cookies and
24    the second one gets the replies and set the atoms. */
25
26 void
27 _ecore_x_screensaver_init(const xcb_query_extension_reply_t *reply)
28 {
29 #ifdef ECORE_XCB_SCREENSAVER
30    if (reply && (reply->present))
31       _ecore_xcb_screensaver_init_cookie = xcb_screensaver_query_version_unchecked(_ecore_xcb_conn, 1, 1);
32 #endif /* ECORE_XCB_SCREENSAVER */
33 }
34
35 void
36 _ecore_x_screensaver_init_finalize(void)
37 {
38 #ifdef ECORE_XCB_SCREENSAVER
39    xcb_screensaver_query_version_reply_t *reply;
40
41    reply = xcb_screensaver_query_version_reply(_ecore_xcb_conn,
42                                                _ecore_xcb_screensaver_init_cookie, NULL);
43
44    if (reply)
45      {
46         if ((reply->server_major_version >= 1) &&
47             (reply->server_minor_version >= 1))
48           _screensaver_available = 1;
49         free(reply);
50      }
51 #endif /* ECORE_XCB_SCREENSAVER */
52 }
53
54
55 /**
56  * Return whether the X server supports the ScreenSaver Extension.
57  * @return 1 if the X ScreenSaver Extension is available, 0 otherwise.
58  *
59  * Return 1 if the X server supports the ScreenSaver Extension version 1.0,
60  * 0 otherwise.
61  * @ingroup Ecore_X_ScreenSaver_Group
62  */
63 EAPI int
64 ecore_x_screensaver_event_available_get(void)
65 {
66    return 1;
67 }
68
69
70 /**
71  * Sends the QueryInfo request.
72  * @ingroup Ecore_X_ScreenSaver_Group
73  */
74 EAPI void
75 ecore_x_screensaver_idle_time_prefetch(void)
76 {
77 #ifdef ECORE_XCB_SCREENSAVER
78    xcb_screensaver_query_info_cookie_t cookie;
79
80    cookie = xcb_screensaver_query_info_unchecked(_ecore_xcb_conn, ((xcb_screen_t *)_ecore_xcb_screen)->root);
81    _ecore_xcb_cookie_cache(cookie.sequence);
82 #endif /* ECORE_XCB_SCREENSAVER */
83 }
84
85
86 /**
87  * Gets the reply of the QueryInfo request sent by ecore_x_get_screensaver_prefetch().
88  * @ingroup Ecore_X_ScreenSaver_Group
89  */
90 EAPI void
91 ecore_x_screensaver_idle_time_fetch(void)
92 {
93 #ifdef ECORE_XCB_SCREENSAVER
94    xcb_screensaver_query_info_cookie_t cookie;
95    xcb_screensaver_query_info_reply_t *reply;
96
97    cookie.sequence = _ecore_xcb_cookie_get();
98    reply = xcb_screensaver_query_info_reply(_ecore_xcb_conn, cookie, NULL);
99    _ecore_xcb_reply_cache(reply);
100 #endif /* ECORE_XCB_SCREENSAVER */
101 }
102
103
104 /**
105  * Get the number of seconds since the last input was received.
106  * @return The number of seconds.
107  *
108  * Get the number of milliseconds since the last input was received
109  * from the user on any of the input devices.
110  *
111  * To use this function, you must call before, and in order,
112  * ecore_x_get_screensaver_prefetch(), which sends the GetScreenSaver request,
113  * then ecore_x_get_screensaver_fetch(), which gets the reply.
114  * @ingroup Ecore_X_ScreenSaver_Group
115  */
116 EAPI int
117 ecore_x_screensaver_idle_time_get(void)
118 {
119   int idle = 0;
120 #ifdef ECORE_XCB_SCREENSAVER
121    xcb_screensaver_query_info_reply_t *reply;
122
123    reply = _ecore_xcb_reply_get();
124
125    if (!reply) return 0;
126
127    /* FIXME: check if it is ms_since_user_input or ms_until_server */
128    idle = reply->ms_since_user_input / 1000;
129 #endif /* ECORE_XCB_SCREENSAVER */
130
131    return idle;
132 }
133
134
135 /**
136  * Set the parameters of the screen saver.
137  * @param timeout  The timeout, in second.
138  * @param interval The interval, in seconds.
139  * @param blank    0 to disable screen blanking, otherwise enable it.
140  * @param expose   Allow Expose generation event or not.
141  *
142  * Set the parameters of the screen saver. @p timeout is the timeout,
143  * in seconds, until the screen saver turns on. @p interval is the
144  * interval, in seconds, between screen saver alterations. @p blank
145  * specifies how to enable screen blanking. @p expose specifies the
146  * screen save control values.
147  * @ingroup Ecore_X_ScreenSaver_Group
148  */
149 EAPI void
150 ecore_x_screensaver_set(int timeout,
151                         int interval,
152                         int blank,
153                         int expose)
154 {
155    xcb_set_screen_saver(_ecore_xcb_conn,
156                         (int16_t)timeout,
157                         (int16_t)interval,
158                         (uint8_t)blank,
159                         (uint8_t)expose);
160 }
161
162
163 /**
164  * Sends the GetScreenSaver request.
165  * @ingroup Ecore_X_ScreenSaver_Group
166  */
167 EAPI void
168 ecore_x_get_screensaver_prefetch(void)
169 {
170    xcb_get_screen_saver_cookie_t cookie;
171
172    cookie = xcb_get_screen_saver_unchecked(_ecore_xcb_conn);
173    _ecore_xcb_cookie_cache(cookie.sequence);
174 }
175
176
177 /**
178  * Gets the reply of the GetScreenSaver request sent by ecore_x_get_screensaver_prefetch().
179  * @ingroup Ecore_X_ScreenSaver_Group
180  */
181 EAPI void
182 ecore_x_get_screensaver_fetch(void)
183 {
184    xcb_get_screen_saver_cookie_t cookie;
185    xcb_get_screen_saver_reply_t *reply;
186
187    cookie.sequence = _ecore_xcb_cookie_get();
188    reply = xcb_get_screen_saver_reply(_ecore_xcb_conn, cookie, NULL);
189    _ecore_xcb_reply_cache(reply);
190 }
191
192
193 /**
194  * Set the timeout of the screen saver.
195  * @param  timeout The timeout to set.
196  *
197  * Set the @p timeout, in seconds, until the screen saver turns on.
198  *
199  * To use this function, you must call before, and in order,
200  * ecore_x_get_screensaver_prefetch(), which sends the GetScreenSaver request,
201  * then ecore_x_get_screensaver_fetch(), which gets the reply.
202  * @ingroup Ecore_X_ScreenSaver_Group
203  */
204 EAPI void
205 ecore_x_screensaver_timeout_set(int timeout)
206 {
207    xcb_get_screen_saver_reply_t *reply;
208
209    reply = _ecore_xcb_reply_get();
210    if (!reply) return;
211
212    xcb_set_screen_saver(_ecore_xcb_conn,
213                         (int16_t)timeout,
214                         reply->interval,
215                         reply->prefer_blanking,
216                         reply->allow_exposures);
217 }
218
219
220 /**
221  * Get the timeout of the screen saver.
222  * @return The timeout.
223  *
224  * Get the @p timeout, in seconds, until the screen saver turns on.
225  *
226  * To use this function, you must call before, and in order,
227  * ecore_x_get_screensaver_prefetch(), which sends the GetScreenSaver request,
228  * then ecore_x_get_screensaver_fetch(), which gets the reply.
229  * @ingroup Ecore_X_ScreenSaver_Group
230  */
231 EAPI int
232 ecore_x_screensaver_timeout_get(void)
233 {
234    xcb_get_screen_saver_reply_t *reply;
235
236    reply = _ecore_xcb_reply_get();
237    if (!reply) return 0.0;
238
239    return (int)reply->timeout;
240 }
241
242
243 /**
244  * Set the interval of the screen saver.
245  * @param  interval The interval to set.
246  *
247  * Set the @p interval, in seconds, between screen saver alterations.
248  *
249  * To use this function, you must call before, and in order,
250  * ecore_x_get_screensaver_prefetch(), which sends the GetScreenSaver request,
251  * then ecore_x_get_screensaver_fetch(), which gets the reply.
252  * @ingroup Ecore_X_ScreenSaver_Group
253  */
254 EAPI void
255 ecore_x_screensaver_interval_set(int interval)
256 {
257    xcb_get_screen_saver_reply_t *reply;
258
259    reply = _ecore_xcb_reply_get();
260    if (!reply) return;
261
262    xcb_set_screen_saver(_ecore_xcb_conn,
263                         reply->timeout,
264                         (int16_t)interval,
265                         reply->prefer_blanking,
266                         reply->allow_exposures);
267 }
268
269
270 /**
271  * Get the interval of the screen saver.
272  * @return The interval.
273  *
274  * Get the @p interval, in seconds, between screen saver alterations.
275  *
276  * To use this function, you must call before, and in order,
277  * ecore_x_get_screensaver_prefetch(), which sends the GetScreenSaver request,
278  * then ecore_x_get_screensaver_fetch(), which gets the reply.
279  * @ingroup Ecore_X_ScreenSaver_Group
280  */
281 EAPI int
282 ecore_x_screensaver_interval_get(void)
283 {
284    xcb_get_screen_saver_reply_t *reply;
285
286    reply = _ecore_xcb_reply_get();
287    if (!reply) return 0.0;
288
289    return (int)reply->interval;
290 }
291
292
293 /**
294  * Set the screen blanking.
295  * @param  blank The blank to set.
296  *
297  * @p blank specifies how to enable screen blanking.
298  *
299  * To use this function, you must call before, and in order,
300  * ecore_x_get_screensaver_prefetch(), which sends the GetScreenSaver request,
301  * then ecore_x_get_screensaver_fetch(), which gets the reply.
302  * @ingroup Ecore_X_ScreenSaver_Group
303  */
304 EAPI void
305 ecore_x_screensaver_blank_set(int blank)
306 {
307    xcb_get_screen_saver_reply_t *reply;
308
309    reply = _ecore_xcb_reply_get();
310    if (!reply) return;
311
312    xcb_set_screen_saver(_ecore_xcb_conn,
313                         reply->timeout,
314                         reply->interval,
315                         (uint8_t)blank,
316                         reply->allow_exposures);
317 }
318
319
320 /**
321  * Get the screen blanking.
322  * @return The blanking.
323  *
324  * Get the screen blanking.
325  *
326  * To use this function, you must call before, and in order,
327  * ecore_x_get_screensaver_prefetch(), which sends the GetScreenSaver request,
328  * then ecore_x_get_screensaver_fetch(), which gets the reply.
329  * @ingroup Ecore_X_ScreenSaver_Group
330  */
331 EAPI int
332 ecore_x_screensaver_blank_get(void)
333 {
334    xcb_get_screen_saver_reply_t *reply;
335
336    reply = _ecore_xcb_reply_get();
337    if (!reply) return 0.0;
338
339    return (int)reply->prefer_blanking;
340 }
341
342
343 /**
344  * Set the screen save control values.
345  * @param  expose The expose to set.
346  *
347  * Set the screen save control values.
348  *
349  * To use this function, you must call before, and in order,
350  * ecore_x_get_screensaver_prefetch(), which sends the GetScreenSaver request,
351  * then ecore_x_get_screensaver_fetch(), which gets the reply.
352  * @ingroup Ecore_X_ScreenSaver_Group
353  */
354 EAPI void
355 ecore_x_screensaver_expose_set(int expose)
356 {
357    xcb_get_screen_saver_reply_t *reply;
358
359    reply = _ecore_xcb_reply_get();
360    if (!reply) return;
361
362    xcb_set_screen_saver(_ecore_xcb_conn,
363                         reply->timeout,
364                         reply->interval,
365                         reply->prefer_blanking,
366                         (uint8_t)expose);
367 }
368
369
370 /**
371  * Get the screen save control values.
372  * @return The expose.
373  *
374  * Get the screen save control values.
375  *
376  * To use this function, you must call before, and in order,
377  * ecore_x_get_screensaver_prefetch(), which sends the GetScreenSaver request,
378  * then ecore_x_get_screensaver_fetch(), which gets the reply.
379  * @ingroup Ecore_X_ScreenSaver_Group
380  */
381 EAPI int
382 ecore_x_screensaver_expose_get(void)
383 {
384    xcb_get_screen_saver_reply_t *reply;
385
386    reply = _ecore_xcb_reply_get();
387    if (!reply) return 0.0;
388
389    return (int)reply->allow_exposures;
390 }
391
392
393 /**
394  * Specifies if the Screen Saver NotifyMask event should be generated.
395  * @param on 0 to disable the generation of the event, otherwise enable it.
396  *
397  * Specifies if the Screen Saver NotifyMask event on the screen
398  * associated with drawable should be generated for this client. If
399  * @p on is set to @c 0, the generation is disabled, otherwise, it is
400  * enabled.
401  * @ingroup Ecore_X_ScreenSaver_Group
402  */
403 EAPI void
404 ecore_x_screensaver_event_listen_set(int on)
405 {
406 #ifdef ECORE_XCB_SCREENSAVER
407    xcb_screensaver_select_input(_ecore_xcb_conn,
408                                 ((xcb_screen_t *)_ecore_xcb_screen)->root,
409                                 on ? XCB_SCREENSAVER_EVENT_NOTIFY_MASK : 0);
410 #endif /* ECORE_XCB_SCREENSAVER */
411 }