Tizen 2.1 base
[framework/uifw/ecore.git] / src / lib / ecore_fb / ecore_fb.c
1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4
5 #include "Ecore_Fb.h"
6 #include "ecore_fb_private.h"
7
8 static void _ecore_fb_size_get(int *w, int *h);
9
10 static int _ecore_fb_init_count = 0;
11 static int _ecore_fb_console_w = 0;
12 static int _ecore_fb_console_h = 0;
13
14 /**
15  * @addtogroup Ecore_FB_Group Ecore_FB - Frame buffer convenience functions.
16  *
17  * @{
18  */
19
20 static sighandler_t oldhand = NULL;
21
22 static void
23 nosigint(int val __UNUSED__)
24 {
25 }
26
27 /**
28  * @brief Initialize the Ecore_Fb library.
29  *
30  * @param name Device target name.
31  * @return 1 or greater on success, 0 on error.
32  *
33  * This function sets up all the Ecore_Fb library. It returns 0 on
34  * failure, otherwise it returns the number of times it has already
35  * been called.
36  *
37  * When Ecore_Fb is not used anymore, call ecore_fb_shutdown() to shut down
38  * the Ecore_Fb library.
39  */
40 EAPI int
41 ecore_fb_init(const char *name __UNUSED__)
42 {
43    if (++_ecore_fb_init_count != 1)
44       return _ecore_fb_init_count;
45
46    if (!ecore_fb_vt_init())
47       return --_ecore_fb_init_count;
48
49    if (!oldhand)
50      {
51         oldhand = signal(SIGINT, nosigint);
52      }
53    
54    _ecore_fb_size_get(&_ecore_fb_console_w, &_ecore_fb_console_h);
55
56    return _ecore_fb_init_count;
57 }
58
59 /**
60  * @brief Shut down the Ecore_Fb library.
61  *
62  * @return 0 when the library is completely shut down, 1 or
63  * greater otherwise.
64  *
65  * This function shuts down the Ecore_Fb library. It returns 0 when it has
66  * been called the same number of times than ecore_fb_init().
67  */
68 EAPI int
69 ecore_fb_shutdown(void)
70 {
71    if (--_ecore_fb_init_count != 0)
72       return _ecore_fb_init_count;
73
74    if (oldhand)
75      {
76         signal(SIGINT, oldhand);
77         oldhand = NULL;
78      }
79    
80    ecore_fb_vt_shutdown();
81
82    return _ecore_fb_init_count;
83 }
84
85
86 /**
87  * @brief Retrieve the width and height of the current frame buffer in
88  * pixels.
89  *
90  * @param w Pointer to an integer in which to store the width.
91  * @param h Pointer to an interge in which to store the height.
92  *
93  * This function retrieves the size of the current frame buffer in
94  * pixels. @p w and @p h can be buffers that will be filled with the
95  * corresponding values. If one of them is @c NULL, nothing will be
96  * done for that parameter.
97  */
98 EAPI void
99 ecore_fb_size_get(int *w, int *h)
100 {
101    if (w) *w = _ecore_fb_console_w;
102    if (h) *h = _ecore_fb_console_h;
103 }
104
105 static void
106 _ecore_fb_size_get(int *w, int *h)
107 {
108    struct fb_var_screeninfo fb_var;
109    int fb;
110
111    fb = open("/dev/fb0", O_RDWR);
112    if (fb < 0)
113       goto exit;
114
115    if (ioctl(fb, FBIOGET_VSCREENINFO, &fb_var) == -1)
116       goto err_ioctl;
117
118    *w = fb_var.xres;
119    *h = fb_var.yres;
120
121 err_ioctl:
122    close(fb);
123 exit:
124    return;
125 }
126
127 /**
128  * @}
129  */