don't kill hints on minimize
[sdl_omap.git] / src / video / omapdss / osdl_video.c
1 /*
2  * (C) GraÅžvydas "notaz" Ignotas, 2010-2012
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 <strings.h>
12 #include <ctype.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <sys/ioctl.h>
17 #include <unistd.h>
18 #include <linux/fb.h>
19
20 #include "osdl.h"
21 #include "omapfb.h"
22 #include "linux/fbdev.h"
23 #include "linux/xenv.h"
24
25 #define MIN(a, b) ( ((a) < (b)) ? (a) : (b) )
26
27 struct omapfb_state {
28         struct omapfb_plane_info pi;
29         struct omapfb_mem_info mi;
30         struct omapfb_plane_info pi_old;
31         struct omapfb_mem_info mi_old;
32 };
33
34 static const char *get_fb_device(void)
35 {
36         const char *fbname = getenv("SDL_FBDEV");
37         if (fbname == NULL)
38                 fbname = "/dev/fb1";
39
40         return fbname;
41 }
42
43 static int osdl_setup_omapfb(struct omapfb_state *ostate, int fd, int enabled,
44         int x, int y, int w, int h, int mem, int *buffer_count)
45 {
46         struct omapfb_plane_info pi;
47         struct omapfb_mem_info mi;
48         int mem_blocks = *buffer_count;
49         unsigned int size_cur;
50         int ret;
51
52         memset(&pi, 0, sizeof(pi));
53         memset(&mi, 0, sizeof(mi));
54
55         ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi);
56         if (ret != 0) {
57                 err_perror("QUERY_PLANE");
58                 return -1;
59         }
60
61         ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi);
62         if (ret != 0) {
63                 err_perror("QUERY_MEM");
64                 return -1;
65         }
66         size_cur = mi.size;
67
68         /* must disable when changing stuff */
69         if (pi.enabled) {
70                 pi.enabled = 0;
71                 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
72                 if (ret != 0)
73                         err_perror("SETUP_PLANE");
74         }
75
76         /* allocate more mem, if needed */
77         for (; size_cur < mem * mem_blocks && mem_blocks > 0; mem_blocks--) {
78                 mi.size = mem * mem_blocks;
79                 ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
80                 if (ret == 0)
81                         break;
82                 mi.size = size_cur;
83         }
84         if (mem_blocks <= 0) {
85                 err("failed to allocate at least %d bytes of vram:\n", mem);
86                 err_perror("SETUP_MEM");
87                 return -1;
88         }
89
90         *buffer_count = mem_blocks;
91
92         pi.pos_x = x;
93         pi.pos_y = y;
94         pi.out_width = w;
95         pi.out_height = h;
96         pi.enabled = enabled;
97
98         ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
99         if (ret != 0) {
100                 err_perror("SETUP_PLANE");
101                 err("(%d %d %d %d)\n", x, y, w, h);
102                 return -1;
103         }
104
105         ostate->pi = pi;
106         ostate->mi = mi;
107         return 0;
108 }
109
110 static int osdl_setup_omapfb_enable(struct omapfb_state *ostate,
111         int fd, int enabled)
112 {
113         int ret;
114
115         ostate->pi.enabled = enabled;
116         ret = ioctl(fd, OMAPFB_SETUP_PLANE, &ostate->pi);
117         if (ret != 0)
118                 err_perror("SETUP_PLANE");
119
120         return ret;
121 }
122
123 static int read_sysfs(const char *fname, char *buff, size_t size)
124 {
125         FILE *f;
126         int ret;
127
128         f = fopen(fname, "r");
129         if (f == NULL) {
130                 err_perror("open %s: ", fname);
131                 return -1;
132         }
133
134         ret = fread(buff, 1, size - 1, f);
135         fclose(f);
136         if (ret <= 0) {
137                 err_perror("read %s: ", fname);
138                 return -1;
139         }
140
141         buff[ret] = 0;
142         for (ret--; ret >= 0 && isspace(buff[ret]); ret--)
143                 buff[ret] = 0;
144
145         return 0;
146 }
147
148 int read_vscreeninfo(const char *fbname, int *w, int *h)
149 {
150         struct fb_var_screeninfo fbvar;
151         int ret, fd;
152
153         fd = open(fbname, O_RDWR);
154         if (fd == -1) {
155                 err_perror("open %s", fbname);
156                 return -1;
157         }
158
159         ret = ioctl(fd, FBIOGET_VSCREENINFO, &fbvar);
160         close(fd);
161
162         if (ret == -1) {
163                 err_perror("ioctl %s", fbname);
164                 return -1;
165         }
166
167         if (fbvar.xres == 0 || fbvar.yres == 0)
168                 return -1;
169
170         *w = fbvar.xres;
171         *h = fbvar.yres;
172         return 0;
173 }
174
175 int osdl_video_detect_screen(struct SDL_PrivateVideoData *pdata)
176 {
177         int fb_id, overlay_id = -1, screen_id = -1;
178         char buff[64], screen_name[64];
179         const char *fbname;
180         struct stat status;
181         int fd, i, ret;
182         int w, h;
183         FILE *f;
184
185         pdata->phys_w = pdata->phys_h = 0;
186
187         fbname = get_fb_device();
188
189         /* Figure out screen resolution, we need to know default resolution
190          * to report to SDL and for centering stuff.
191          * The only way to achieve this seems to be walking some sysfs files.. */
192         ret = stat(fbname, &status);
193         if (ret != 0) {
194                 err_perror("can't stat %s", fbname);
195                 goto skip_screen;
196         }
197         fb_id = minor(status.st_rdev);
198
199         snprintf(buff, sizeof(buff), "/sys/class/graphics/fb%d/overlays", fb_id);
200         f = fopen(buff, "r");
201         if (f == NULL) {
202                 err("can't open %s, skip screen detection", buff);
203                 goto skip_screen;
204         }
205
206         ret = fscanf(f, "%d", &overlay_id);
207         fclose(f);
208         if (ret != 1) {
209                 err("can't parse %s, skip screen detection", buff);
210                 goto skip_screen;
211         }
212
213         snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/overlay%d/manager", overlay_id);
214         ret = read_sysfs(buff, screen_name, sizeof(screen_name));
215         if (ret < 0) {
216                 err("skip screen detection");
217                 goto skip_screen;
218         }
219
220         for (i = 0; ; i++) {
221                 snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/display%d/name", i);
222                 ret = read_sysfs(buff, buff, sizeof(buff));
223                 if (ret < 0)
224                         break;
225
226                 if (strcmp(screen_name, buff) == 0) {
227                         screen_id = i;
228                         break;
229                 }
230         }
231
232         if (screen_id < 0) {
233                 err("could not find screen");
234                 goto skip_screen;
235         }
236
237         snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/display%d/timings", screen_id);
238         f = fopen(buff, "r");
239         if (f == NULL) {
240                 err("can't open %s, skip screen detection", buff);
241                 goto skip_screen;
242         }
243
244         ret = fscanf(f, "%*d,%d/%*d/%*d/%*d,%d/%*d/%*d/%*d", &w, &h);
245         fclose(f);
246         if (ret != 2) {
247                 err("can't parse %s (%d), skip screen detection", buff, ret);
248                 goto skip_screen;
249         }
250
251         log("detected %dx%d '%s' (%d) screen attached to fb %d and overlay %d",
252                 w, h, screen_name, screen_id, fb_id, overlay_id);
253
254         pdata->phys_w = w;
255         pdata->phys_h = h;
256         return 0;
257
258 skip_screen:
259         /* attempt to extract this from FB then */
260         ret = read_vscreeninfo(fbname, &pdata->phys_w, &pdata->phys_h);
261         if (ret != 0 && strcmp(fbname, "/dev/fb0") != 0) {
262                 /* last resort */
263                 ret = read_vscreeninfo("/dev/fb0", &pdata->phys_w, &pdata->phys_h);
264         }
265
266         if (ret != 0) {
267                 err("VSCREENINFO has nothing meaningful");
268                 return -1;
269         }
270
271         return 0;
272 }
273
274 static int osdl_setup_omap_layer(struct SDL_PrivateVideoData *pdata,
275                 const char *fbname, int width, int height, int bpp, int *buffer_count)
276 {
277         int x = 0, y = 0, w = width, h = height; /* layer size and pos */
278         int screen_w = w, screen_h = h;
279         int tmp_w, tmp_h;
280         const char *tmp;
281         int retval = -1;
282         int ret, fd;
283
284         pdata->layer_x = pdata->layer_y = pdata->layer_w = pdata->layer_h = 0;
285
286         if (pdata->phys_w != 0)
287                 screen_w = pdata->phys_w;
288         if (pdata->phys_h != 0)
289                 screen_h = pdata->phys_h;
290
291         fd = open(fbname, O_RDWR);
292         if (fd == -1) {
293                 err_perror("open %s", fbname);
294                 return -1;
295         }
296
297         /* FIXME: assuming layer doesn't change here */
298         if (pdata->saved_layer == NULL) {
299                 struct omapfb_state *slayer;
300                 slayer = calloc(1, sizeof(*slayer));
301                 if (slayer == NULL)
302                         goto out;
303
304                 ret = ioctl(fd, OMAPFB_QUERY_PLANE, &slayer->pi_old);
305                 if (ret != 0) {
306                         err_perror("QUERY_PLANE");
307                         goto out;
308                 }
309
310                 ret = ioctl(fd, OMAPFB_QUERY_MEM, &slayer->mi_old);
311                 if (ret != 0) {
312                         err_perror("QUERY_MEM");
313                         goto out;
314                 }
315
316                 pdata->saved_layer = slayer;
317         }
318
319         tmp = getenv("SDL_OMAP_LAYER_SIZE");
320         if (tmp != NULL) {
321                 if (strcasecmp(tmp, "fullscreen") == 0) {
322                         w = screen_w, h = screen_h;
323                 }
324                 else if (strcasecmp(tmp, "scaled") == 0) {
325                         float factor = MIN(((float)screen_w) / width, ((float)screen_h) / height);
326                         w = (int)(factor*width), h = (int)(factor*height);
327                 }
328                 else if (strcasecmp(tmp, "pixelperfect") == 0) {
329                         float factor = MIN(((float)screen_w) / width, ((float)screen_h) / height);
330                         w = ((int)factor) * width, h = ((int)factor) * height;
331                         /* factor < 1.f => 0x0 layer, so fall back to 'scaled' */
332                         if (!w || !h) {
333                                 w = (int)(factor * width), h = (int)(factor * height);
334                         }
335                 }
336                 else if (sscanf(tmp, "%dx%d", &tmp_w, &tmp_h) == 2) {
337                         w = tmp_w, h = tmp_h;
338                 }
339                 else {
340                         err("layer size specified incorrectly, "
341                                 "should be like 800x480");
342                 }
343         }
344
345         /* the layer can't be set larger than screen */
346         tmp_w = w, tmp_h = h;
347         if (w > screen_w)
348                 w = screen_w;
349         if (h > screen_h)
350                 h = screen_h;
351         if (w != tmp_w || h != tmp_h)
352                 log("layer resized %dx%d -> %dx%d to fit screen", tmp_w, tmp_h, w, h);
353
354         x = screen_w / 2 - w / 2;
355         y = screen_h / 2 - h / 2;
356         ret = osdl_setup_omapfb(pdata->saved_layer, fd, 0, x, y, w, h,
357                                 width * height * ((bpp + 7) / 8), buffer_count);
358         if (ret == 0) {
359                 pdata->layer_x = x;
360                 pdata->layer_y = y;
361                 pdata->layer_w = w;
362                 pdata->layer_h = h;
363         }
364
365         retval = ret;
366 out:
367         close(fd);
368         return retval;
369 }
370
371 void *osdl_video_set_mode(struct SDL_PrivateVideoData *pdata,
372                           int border_l, int border_r, int border_t, int border_b,
373                           int width, int height, int bpp, int *doublebuf,
374                           const char *wm_title)
375 {
376         int buffers_try, buffers_set;
377         const char *fbname;
378         void *result;
379         int ret;
380
381         fbname = get_fb_device();
382
383         if (pdata->fbdev != NULL) {
384                 vout_fbdev_finish(pdata->fbdev);
385                 pdata->fbdev = NULL;
386         }
387
388         buffers_try = buffers_set = 1;
389         if (*doublebuf)
390                 /* actually try tripple buffering for reduced chance of tearing */
391                 buffers_try = buffers_set = 3;
392
393         ret = osdl_setup_omap_layer(pdata, fbname, width, height, bpp, &buffers_set);
394         if (ret < 0)
395                 goto fail;
396
397         pdata->fbdev = vout_fbdev_init(fbname, &width, &height, bpp, buffers_set);
398         if (pdata->fbdev == NULL)
399                 goto fail;
400
401         if (border_l | border_r | border_t | border_b) {
402                 width -= border_l + border_r;
403                 height -= border_t + border_b;
404                 result = vout_fbdev_resize(pdata->fbdev, width, height, bpp,
405                                 border_l, border_r, border_t, border_b, buffers_set);
406         }
407         else {
408                 result = osdl_video_flip(pdata);
409         }
410         if (result == NULL)
411                 goto fail;
412
413         if (!pdata->xenv_up) {
414                 int xenv_flags = XENV_CAP_KEYS | XENV_CAP_MOUSE;
415                 ret = xenv_init(&xenv_flags, wm_title);
416                 if (ret == 0) {
417                         pdata->xenv_up = 1;
418                         pdata->xenv_mouse = (xenv_flags & XENV_CAP_MOUSE) ? 1 : 0;
419                 }
420         }
421
422         ret = osdl_setup_omapfb_enable(pdata->saved_layer,
423                 vout_fbdev_get_fd(pdata->fbdev), 1);
424         if (ret != 0) {
425                 err("layer enable failed");
426                 goto fail;
427         }
428
429         if (buffers_try != buffers_set) {
430                 log("only %d/%d buffers available, expect tearing\n",
431                         buffers_set, buffers_try);
432                 *doublebuf = buffers_set > 1;
433         }
434
435         return result;
436
437 fail:
438         osdl_video_finish(pdata);
439         return NULL;
440 }
441
442 void *osdl_video_flip(struct SDL_PrivateVideoData *pdata)
443 {
444         void *ret;
445
446         if (pdata->fbdev == NULL)
447                 return NULL;
448
449         ret = vout_fbdev_flip(pdata->fbdev);
450
451         if (pdata->cfg_force_vsync)
452                 vout_fbdev_wait_vsync(pdata->fbdev);
453
454         return ret;
455 }
456
457 void *osdl_video_get_active_buffer(struct SDL_PrivateVideoData *pdata)
458 {
459         if (pdata->fbdev == NULL)
460                 return NULL;
461
462         return vout_fbdev_get_active_mem(pdata->fbdev);
463 }
464
465 int osdl_video_pause(struct SDL_PrivateVideoData *pdata, int is_pause)
466 {
467         struct omapfb_state *state = pdata->saved_layer;
468         struct omapfb_plane_info pi;
469         struct omapfb_mem_info mi;
470         int enabled;
471         int fd = -1;
472         int ret;
473
474         if (pdata->fbdev != NULL)
475                 fd = vout_fbdev_get_fd(pdata->fbdev);
476         if (fd == -1) {
477                 err("bad fd %d", fd);
478                 return -1;
479         }
480         if (state == NULL) {
481                 err("missing layer state\n");
482                 return -1;
483         }
484
485         if (is_pause) {
486                 ret = vout_fbdev_save(pdata->fbdev);
487                 if (ret != 0)
488                         return ret;
489                 pi = state->pi_old;
490                 mi = state->mi_old;
491                 enabled = pi.enabled;
492         } else {
493                 pi = state->pi;
494                 mi = state->mi;
495                 enabled = 1;
496         }
497         pi.enabled = 0;
498         ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
499         if (ret != 0) {
500                 err_perror("SETUP_PLANE");
501                 return -1;
502         }
503
504         ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
505         if (ret != 0)
506                 err_perror("SETUP_MEM");
507
508         if (!is_pause) {
509                 ret = vout_fbdev_restore(pdata->fbdev);
510                 if (ret != 0) {
511                         err("fbdev_restore failed\n");
512                         return ret;
513                 }
514         }
515
516         if (enabled) {
517                 pi.enabled = 1;
518                 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
519                 if (ret != 0) {
520                         err_perror("SETUP_PLANE");
521                         return -1;
522                 }
523         }
524
525         return 0;
526 }
527
528 void osdl_video_finish(struct SDL_PrivateVideoData *pdata)
529 {
530         static const char *fbname;
531
532         fbname = get_fb_device();
533         if (pdata->fbdev != NULL) {
534                 vout_fbdev_finish(pdata->fbdev);
535                 pdata->fbdev = NULL;
536         }
537
538         /* restore the OMAP layer */
539         if (pdata->saved_layer != NULL) {
540                 struct omapfb_state *slayer = pdata->saved_layer;
541                 int fd;
542
543                 fd = open(fbname, O_RDWR);
544                 if (fd != -1) {
545                         int enabled = slayer->pi_old.enabled;
546
547                         /* be sure to disable while setting up */
548                         slayer->pi_old.enabled = 0;
549                         ioctl(fd, OMAPFB_SETUP_PLANE, &slayer->pi_old);
550                         ioctl(fd, OMAPFB_SETUP_MEM, &slayer->mi_old);
551                         if (enabled) {
552                                 slayer->pi_old.enabled = enabled;
553                                 ioctl(fd, OMAPFB_SETUP_PLANE, &slayer->pi_old);
554                         }
555                         close(fd);
556                 }
557                 free(slayer);
558                 pdata->saved_layer = NULL;
559         }
560
561         if (pdata->xenv_up) {
562                 xenv_finish();
563                 pdata->xenv_up = 0;
564         }
565 }
566