split menu so it can be reused in other projects
[libpicofe.git] / linux / fbdev.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2009-2010
3  *
4  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
5  * See the COPYING file in the top-level directory.
6  */
7
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <sys/ioctl.h>
15 #include <sys/mman.h>
16 #include <unistd.h>
17 #include <linux/fb.h>
18 #include <linux/matroxfb.h>
19
20 #include "fbdev.h"
21
22 struct vout_fbdev {
23         int     fd;
24         void    *mem;
25         size_t  mem_size;
26         struct  fb_var_screeninfo fbvar_old;
27         struct  fb_var_screeninfo fbvar_new;
28         int     buffer_write;
29         int     fb_size;
30         int     buffer_count;
31         int     top_border, bottom_border;
32 };
33
34 void *vout_fbdev_flip(struct vout_fbdev *fbdev)
35 {
36         int draw_buf;
37
38         if (fbdev->buffer_count < 2)
39                 return fbdev->mem;
40
41         draw_buf = fbdev->buffer_write;
42         fbdev->buffer_write++;
43         if (fbdev->buffer_write >= fbdev->buffer_count)
44                 fbdev->buffer_write = 0;
45
46         fbdev->fbvar_new.yoffset = 
47                 (fbdev->top_border + fbdev->fbvar_new.yres + fbdev->bottom_border) * draw_buf +
48                 fbdev->top_border;
49
50         ioctl(fbdev->fd, FBIOPAN_DISPLAY, &fbdev->fbvar_new);
51
52         return (char *)fbdev->mem + fbdev->fb_size * fbdev->buffer_write;
53 }
54
55 void vout_fbdev_wait_vsync(struct vout_fbdev *fbdev)
56 {
57         int arg = 0;
58         ioctl(fbdev->fd, FBIO_WAITFORVSYNC, &arg);
59 }
60
61 int vout_fbdev_resize(struct vout_fbdev *fbdev, int w, int h, int bpp,
62                       int left_border, int right_border, int top_border, int bottom_border, int buffer_cnt)
63 {
64         int w_total = left_border + w + right_border;
65         int h_total = top_border + h + bottom_border;
66         size_t mem_size;
67         int ret;
68
69         // unblank to be sure the mode is really accepted
70         ioctl(fbdev->fd, FBIOBLANK, FB_BLANK_UNBLANK);
71
72         if (fbdev->fbvar_new.bits_per_pixel != bpp ||
73                         w != fbdev->fbvar_new.xres ||
74                         h != fbdev->fbvar_new.yres ||
75                         w_total != fbdev->fbvar_new.xres_virtual ||
76                         h_total > fbdev->fbvar_new.yres_virtual ||
77                         left_border != fbdev->fbvar_new.xoffset) {
78                 fbdev->fbvar_new.xres = w;
79                 fbdev->fbvar_new.yres = h;
80                 fbdev->fbvar_new.xres_virtual = w_total;
81                 fbdev->fbvar_new.yres_virtual = h_total;
82                 fbdev->fbvar_new.xoffset = left_border;
83                 fbdev->fbvar_new.bits_per_pixel = bpp;
84                 printf(" switching to %dx%d@%d\n", w, h, bpp);
85                 ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
86                 if (ret == -1) {
87                         perror("FBIOPUT_VSCREENINFO ioctl");
88                         return -1;
89                 }
90         }
91
92         fbdev->buffer_count = buffer_cnt;
93
94         if (fbdev->fbvar_new.yres_virtual < h_total * fbdev->buffer_count) {
95                 fbdev->fbvar_new.yres_virtual = h_total * fbdev->buffer_count;
96                 ret = ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_new);
97                 if (ret == -1) {
98                         fbdev->buffer_count = 1;
99                         fprintf(stderr, "Warning: failed to increase virtual resolution, "
100                                         "doublebuffering disabled\n");
101                 }
102         }
103
104         fbdev->fb_size = w_total * h_total * bpp / 8;
105         fbdev->top_border = top_border;
106         fbdev->bottom_border = bottom_border;
107
108         mem_size = fbdev->fb_size * fbdev->buffer_count;
109         if (fbdev->mem_size >= mem_size)
110                 return 0;
111
112         if (fbdev->mem != NULL)
113                 munmap(fbdev->mem, fbdev->mem_size);
114
115         fbdev->mem = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
116         if (fbdev->mem == MAP_FAILED && fbdev->buffer_count > 1) {
117                 fprintf(stderr, "Warning: can't map %zd bytes, doublebuffering disabled\n", fbdev->mem_size);
118                 fbdev->buffer_count = 1;
119                 mem_size = fbdev->fb_size;
120                 fbdev->mem = mmap(0, mem_size, PROT_WRITE|PROT_READ, MAP_SHARED, fbdev->fd, 0);
121         }
122         if (fbdev->mem == MAP_FAILED) {
123                 fbdev->mem = NULL;
124                 fbdev->mem_size = 0;
125                 perror("mmap framebuffer");
126                 return -1;
127         }
128
129         fbdev->mem_size = mem_size;
130         return 0;
131 }
132
133 void vout_fbdev_clear(struct vout_fbdev *fbdev)
134 {
135         memset(fbdev->mem, 0, fbdev->mem_size);
136 }
137
138 void vout_fbdev_clear_lines(struct vout_fbdev *fbdev, int y, int count)
139 {
140         int stride = fbdev->fbvar_new.xres_virtual * fbdev->fbvar_new.bits_per_pixel / 8;
141         int i;
142
143         if (y + count > fbdev->top_border + fbdev->fbvar_new.yres)
144                 count = fbdev->top_border + fbdev->fbvar_new.yres - y;
145
146         if (y >= 0 && count > 0)
147                 for (i = 0; i < fbdev->buffer_count; i++)
148                         memset((char *)fbdev->mem + fbdev->fb_size * i + y * stride, 0, stride * count);
149 }
150
151 int vout_fbdev_get_fd(struct vout_fbdev *fbdev)
152 {
153         return fbdev->fd;
154 }
155
156 struct vout_fbdev *vout_fbdev_init(const char *fbdev_name, int *w, int *h, int bpp, int buffer_cnt)
157 {
158         struct vout_fbdev *fbdev;
159         int req_w, req_h;
160         int ret;
161
162         fbdev = calloc(1, sizeof(*fbdev));
163         if (fbdev == NULL)
164                 return NULL;
165
166         fbdev->fd = open(fbdev_name, O_RDWR);
167         if (fbdev->fd == -1) {
168                 fprintf(stderr, "%s: ", fbdev_name);
169                 perror("open");
170                 goto fail_open;
171         }
172
173         ret = ioctl(fbdev->fd, FBIOGET_VSCREENINFO, &fbdev->fbvar_old);
174         if (ret == -1) {
175                 perror("FBIOGET_VSCREENINFO ioctl");
176                 goto fail;
177         }
178
179         fbdev->fbvar_new = fbdev->fbvar_old;
180
181         req_w = fbdev->fbvar_new.xres;
182         if (*w != 0)
183                 req_w = *w;
184         req_h = fbdev->fbvar_new.yres;
185         if (*h != 0)
186                 req_h = *h;
187
188         ret = vout_fbdev_resize(fbdev, req_w, req_h, bpp, 0, 0, 0, 0, buffer_cnt);
189         if (ret != 0)
190                 goto fail;
191
192         printf("%s: %ix%i@%d\n", fbdev_name, fbdev->fbvar_new.xres, fbdev->fbvar_new.yres,
193                 fbdev->fbvar_new.bits_per_pixel);
194         *w = fbdev->fbvar_new.xres;
195         *h = fbdev->fbvar_new.yres;
196
197         memset(fbdev->mem, 0, fbdev->mem_size);
198
199         // some checks
200         ret = 0;
201         ret = ioctl(fbdev->fd, FBIO_WAITFORVSYNC, &ret);
202         if (ret != 0)
203                 fprintf(stderr, "Warning: vsync doesn't seem to be supported\n");
204
205         if (fbdev->buffer_count > 1) {
206                 fbdev->buffer_write = 0;
207                 fbdev->fbvar_new.yoffset = fbdev->fbvar_new.yres * (fbdev->buffer_count - 1);
208                 ret = ioctl(fbdev->fd, FBIOPAN_DISPLAY, &fbdev->fbvar_new);
209                 if (ret != 0) {
210                         fbdev->buffer_count = 1;
211                         fprintf(stderr, "Warning: can't pan display, doublebuffering disabled\n");
212                 }
213         }
214
215         printf("fbdev initialized.\n");
216         return fbdev;
217
218 fail:
219         close(fbdev->fd);
220 fail_open:
221         free(fbdev);
222         return NULL;
223 }
224
225 void vout_fbdev_finish(struct vout_fbdev *fbdev)
226 {
227         ioctl(fbdev->fd, FBIOPUT_VSCREENINFO, &fbdev->fbvar_old);
228         if (fbdev->mem != MAP_FAILED)
229                 munmap(fbdev->mem, fbdev->mem_size);
230         if (fbdev->fd >= 0)
231                 close(fbdev->fd);
232         fbdev->mem = NULL;
233         fbdev->fd = -1;
234         free(fbdev);
235 }
236