Initialize Tizen 2.3
[framework/uifw/ecore.git] / mobile / src / lib / ecore_x / xlib / ecore_x_randr_11.c
1 /*
2  * vim:ts=8:sw=3:sts=8:expandtab:cino=>5n-3f0^-2{2
3  */
4
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif /* ifdef HAVE_CONFIG_H */
8
9 #include "ecore_x_private.h"
10 #include "ecore_x_randr.h"
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <string.h>
15
16 #define Ecore_X_Randr_None 0
17 #ifdef ECORE_XRANDR
18
19 #define RANDR_1_1          ((1 << 16) | 1)
20
21 #define RANDR_VALIDATE_ROOT(screen,                                  \
22                             root) ((screen =                         \
23                                       XRRRootToScreen(_ecore_x_disp, \
24                                                       root)) != -1)
25 #define RANDR_CHECK_1_1_RET(ret)  if (_randr_version < RANDR_1_1) \
26     return ret
27
28 extern XRRScreenResources *(*_ecore_x_randr_get_screen_resources)(Display *
29                                                                   dpy,
30                                                                   Window
31                                                                   window);
32 extern int _randr_version;
33 #endif /* ifdef ECORE_XRANDR */
34
35 /*
36  * @param root window which's primary output will be queried
37  */
38 EAPI Ecore_X_Randr_Orientation
39 ecore_x_randr_screen_primary_output_orientations_get(Ecore_X_Window root)
40 {
41 #ifdef ECORE_XRANDR
42    Rotation rot = Ecore_X_Randr_None, crot;
43
44    LOGFN(__FILE__, __LINE__, __FUNCTION__);
45    rot =
46      XRRRotations(_ecore_x_disp, XRRRootToScreen(_ecore_x_disp,
47                                                  root), &crot);
48    return rot;
49 #else /* ifdef ECORE_XRANDR */
50    return Ecore_X_Randr_None;
51 #endif /* ifdef ECORE_XRANDR */
52 }
53
54 /*
55  * @param root window which's primary output will be queried
56  * @return the current orientation of the root window's screen primary output
57  */
58 EAPI Ecore_X_Randr_Orientation
59 ecore_x_randr_screen_primary_output_orientation_get(Ecore_X_Window root)
60 {
61 #ifdef ECORE_XRANDR
62    Rotation crot = Ecore_X_Randr_None;
63    XRRRotations(_ecore_x_disp, XRRRootToScreen(_ecore_x_disp,
64                                                root), &crot);
65    return crot;
66 #else /* ifdef ECORE_XRANDR */
67    return Ecore_X_Randr_None;
68 #endif /* ifdef ECORE_XRANDR */
69 }
70
71 /*
72  * @brief Sets a given screen's primary output's orientation.
73  *
74  * @param root Window which's screen's primary output will be queried.
75  * @param orientation orientation which should be set for the root window's
76  * screen primary output.
77  * @return @c EINA_TRUE if the primary output's orientation could be
78  * successfully altered.
79  */
80 EAPI Eina_Bool
81 ecore_x_randr_screen_primary_output_orientation_set(
82   Ecore_X_Window root,
83   Ecore_X_Randr_Orientation
84   orientation)
85 {
86 #ifdef ECORE_XRANDR
87    XRRScreenConfiguration *xrr_screen_cfg = NULL;
88    int sizeid;
89    Rotation crot;
90    Eina_Bool ret = EINA_FALSE;
91    if (!(xrr_screen_cfg = XRRGetScreenInfo(_ecore_x_disp, root)))
92      return EINA_FALSE;
93
94    sizeid = XRRConfigCurrentConfiguration(xrr_screen_cfg, &crot);
95    if (!XRRSetScreenConfig(_ecore_x_disp, xrr_screen_cfg, root, sizeid,
96                            orientation, CurrentTime))
97      ret = EINA_TRUE;
98
99    if (xrr_screen_cfg)
100      XRRFreeScreenConfigInfo(xrr_screen_cfg);
101
102    return ret;
103 #else /* ifdef ECORE_XRANDR */
104    return EINA_FALSE;
105 #endif /* ifdef ECORE_XRANDR */
106 }
107
108 /*
109  * @brief gets a screen's primary output's possible sizes
110  * @param root window which's primary output will be queried
111  * @param num number of sizes reported as supported by the screen's primary output
112  * @return an array of sizes reported as supported by the screen's primary output or - if query failed - NULL
113  */
114 EAPI Ecore_X_Randr_Screen_Size_MM *
115 ecore_x_randr_screen_primary_output_sizes_get(Ecore_X_Window root,
116                                               int *num)
117 {
118 #ifdef ECORE_XRANDR
119    Ecore_X_Randr_Screen_Size_MM *ret = NULL;
120    XRRScreenSize *sizes;
121    int i, n;
122
123    /* we don't have to free sizes, because they're hold in a cache inside X*/
124    sizes =
125      XRRSizes(_ecore_x_disp, XRRRootToScreen(_ecore_x_disp,
126                                              root), &n);
127    if ((!sizes) || (n <= 0)) return NULL;
128    ret = calloc(n, sizeof(Ecore_X_Randr_Screen_Size_MM));
129    if (!ret)
130      return NULL;
131
132    if (num)
133      *num = n;
134
135    for (i = 0; i < n; i++)
136      {
137         ret[i].width = sizes[i].width;
138         ret[i].height = sizes[i].height;
139         ret[i].width_mm = sizes[i].mwidth;
140         ret[i].height_mm = sizes[i].mheight;
141      }
142    return ret;
143 #else /* ifdef ECORE_XRANDR */
144    return NULL;
145 #endif /* ifdef ECORE_XRANDR */
146 }
147
148 EAPI void
149 ecore_x_randr_screen_primary_output_current_size_get(Ecore_X_Window root,
150                                                      int *w,
151                                                      int *h,
152                                                      int *w_mm,
153                                                      int *h_mm,
154                                                      int *size_index)
155 {
156 #ifdef ECORE_XRANDR
157    XRRScreenSize *sizes;
158    XRRScreenConfiguration *sc = NULL;
159    int idx;
160    Rotation orientation;
161    int n;
162
163    if (!(sc = XRRGetScreenInfo(_ecore_x_disp, root)))
164      {
165         ERR("Couldn't get screen information for %d", root);
166         return;
167      }
168
169    idx = XRRConfigCurrentConfiguration(sc, &orientation);
170
171    sizes =
172      XRRSizes(_ecore_x_disp, XRRRootToScreen(_ecore_x_disp,
173                                              root), &n);
174    if ((idx < n) && (idx >= 0))
175      {
176         if (w)
177           *w = sizes[idx].width;
178
179         if (h)
180           *h = sizes[idx].height;
181
182         if (w_mm)
183           *w_mm = sizes[idx].mwidth;
184
185         if (h_mm)
186           *h_mm = sizes[idx].mheight;
187
188         if (size_index)
189           *size_index = idx;
190      }
191
192    XRRFreeScreenConfigInfo(sc);
193 #endif /* ifdef ECORE_XRANDR */
194 }
195
196 /*
197  * @brief Sets a given screen's primary output size, but disables all other
198  * outputs at the same time.
199  *
200  * @param root Window which's primary output will be queried.
201  * @param size_index Within the list of sizes reported as supported by the root
202  * window's screen primary output.
203  * @return @c EINA_TRUE on success, @c EINA_FALSE on failure due to e.g.
204  * invalid times.
205  */
206 EAPI Eina_Bool
207 ecore_x_randr_screen_primary_output_size_set(Ecore_X_Window root,
208                                              int size_index)
209 {
210 #ifdef ECORE_XRANDR
211    XRRScreenConfiguration *sc = NULL;
212    Eina_Bool ret = EINA_FALSE;
213    int nsizes = 0;
214
215    if (size_index >= 0 && _ecore_x_randr_root_validate(root))
216      {
217         XRRSizes(_ecore_x_disp, XRRRootToScreen(_ecore_x_disp,
218                                                   root), &nsizes);
219
220         if (size_index < nsizes)
221           {
222              sc = XRRGetScreenInfo(_ecore_x_disp, root);
223              if (!XRRSetScreenConfig(_ecore_x_disp, sc,
224                                      root, size_index,
225                                      ECORE_X_RANDR_ORIENTATION_ROT_0, CurrentTime))
226                {
227                   ret = EINA_TRUE;
228                }
229
230              if (sc)
231                XRRFreeScreenConfigInfo(sc);
232           }
233      }
234
235    return ret;
236 #else /* ifdef ECORE_XRANDR */
237    return EINA_FALSE;
238 #endif /* ifdef ECORE_XRANDR */
239 }
240
241 /*
242  * @param root window which's primary output will be queried
243  * @return currently used refresh rate or - if request failed or RandRR is not available - 0.0
244  */
245 EAPI Ecore_X_Randr_Refresh_Rate
246 ecore_x_randr_screen_primary_output_current_refresh_rate_get(
247   Ecore_X_Window root)
248 {
249 #ifdef ECORE_XRANDR
250    Ecore_X_Randr_Refresh_Rate ret = 0.0;
251    XRRScreenConfiguration *sc = NULL;
252
253    if (!_ecore_x_randr_root_validate(root) ||
254        !(sc = XRRGetScreenInfo(_ecore_x_disp, root)))
255      return ret;
256
257    ret = XRRConfigCurrentRate(sc);
258    if (sc)
259      XRRFreeScreenConfigInfo(sc);
260
261    return ret;
262 #else /* ifdef ECORE_XRANDR */
263    return 0.0;
264 #endif /* ifdef ECORE_XRANDR */
265 }
266
267 /*
268  * @param root window which's primary output will be queried
269  * @param size_index referencing the size to query valid refresh rates for
270  * @return currently used refresh rate or - if request failed or RandRR is not available - NULL
271  */
272 EAPI Ecore_X_Randr_Refresh_Rate *
273 ecore_x_randr_screen_primary_output_refresh_rates_get(Ecore_X_Window root,
274                                                       int size_index,
275                                                       int *num)
276 {
277 #ifdef ECORE_XRANDR
278    Ecore_X_Randr_Refresh_Rate *ret = NULL, *rates = NULL;
279    Ecore_X_Randr_Screen scr;
280    int n;
281
282    if (num
283        && RANDR_VALIDATE_ROOT(scr, root)
284        && (rates = XRRRates(_ecore_x_disp, scr, size_index, &n)))
285      {
286         if (rates && (ret = malloc(sizeof(Ecore_X_Randr_Refresh_Rate) * n)))
287           {
288              memcpy(ret, rates, (sizeof(Ecore_X_Randr_Refresh_Rate) * n));
289              *num = n;
290           }
291      }
292
293    return ret;
294 #else /* ifdef ECORE_XRANDR */
295    return NULL;
296 #endif /* ifdef ECORE_XRANDR */
297 }
298
299 //>= 1.1
300 /*
301  * @brief Sets the current primary output's refresh rate.
302  *
303  * @param root Window which's primary output will be queried.
304  * @param size_index Referencing the size to be set.
305  * @param rate The refresh rate to be set.
306  * @return @c EINA_TRUE on success, @c EINA_FALSE otherwise.
307  */
308 EAPI Eina_Bool
309 ecore_x_randr_screen_primary_output_refresh_rate_set(
310   Ecore_X_Window root,
311   int size_index,
312   Ecore_X_Randr_Refresh_Rate
313   rate)
314 {
315 #ifdef ECORE_XRANDR
316    RANDR_CHECK_1_1_RET(EINA_FALSE);
317    Eina_Bool ret = EINA_FALSE;
318    XRRScreenConfiguration *sc = NULL;
319
320    if (!(sc = XRRGetScreenInfo(_ecore_x_disp, root)))
321      return ret;
322
323    if (!XRRSetScreenConfigAndRate(_ecore_x_disp, sc,
324                                   root, size_index,
325                                   RR_Rotate_0, rate, CurrentTime))
326      ret = EINA_TRUE;
327
328    XRRFreeScreenConfigInfo(sc);
329    return ret;
330 #else /* ifdef ECORE_XRANDR */
331    return EINA_FALSE;
332 #endif /* ifdef ECORE_XRANDR */
333 }
334