2 * (C) GraÅžvydas "notaz" Ignotas, 2010
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.
13 #include <sys/types.h>
16 #include <sys/ioctl.h>
22 #include "linux/fbdev.h"
23 #include "linux/oshide.h"
25 struct omapfb_saved_layer {
26 struct omapfb_plane_info pi;
27 struct omapfb_mem_info mi;
30 static const char *get_fb_device(void)
32 const char *fbname = getenv("SDL_FBDEV");
39 static int osdl_setup_omapfb(int fd, int enabled, int x, int y, int w, int h, int mem)
41 struct omapfb_plane_info pi;
42 struct omapfb_mem_info mi;
45 ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi);
47 err_perror("QUERY_PLANE");
51 ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi);
53 err_perror("QUERY_MEM");
57 /* must disable when changing stuff */
60 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
62 err_perror("SETUP_PLANE");
66 ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
68 err_perror("SETUP_MEM");
78 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
80 err_perror("SETUP_PLANE");
81 err("(%d %d %d %d)\n", x, y, w, h);
88 static int read_sysfs(const char *fname, char *buff, size_t size)
93 f = fopen(fname, "r");
95 err_perror("open %s: ", fname);
99 ret = fread(buff, 1, size - 1, f);
102 err_perror("read %s: ", fname);
107 for (ret--; ret >= 0 && isspace(buff[ret]); ret--)
113 int osdl_video_detect_screen(struct SDL_PrivateVideoData *pdata)
115 int fb_id, overlay_id = -1, screen_id = -1;
116 struct fb_var_screeninfo fbvar;
117 char buff[64], screen_name[64];
124 fbname = get_fb_device();
126 /* Figure out screen resolution, we need to know default resolution
127 * to report to SDL and for centering stuff.
128 * The only way to achieve this seems to be walking some sysfs files.. */
129 ret = stat(fbname, &status);
131 err_perror("can't stat %s", fbname);
134 fb_id = minor(status.st_rdev);
136 snprintf(buff, sizeof(buff), "/sys/class/graphics/fb%d/overlays", fb_id);
137 f = fopen(buff, "r");
139 err("can't open %s, skip screen detection", buff);
143 ret = fscanf(f, "%d", &overlay_id);
146 err("can't parse %s, skip screen detection", buff);
150 snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/overlay%d/manager", overlay_id);
151 ret = read_sysfs(buff, screen_name, sizeof(screen_name));
153 err("skip screen detection");
158 snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/display%d/name", i);
159 ret = read_sysfs(buff, buff, sizeof(buff));
163 if (strcmp(screen_name, buff) == 0) {
170 err("could not find screen");
174 snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/display%d/timings", screen_id);
175 f = fopen(buff, "r");
177 err("can't open %s, skip screen detection", buff);
181 ret = fscanf(f, "%*d,%d/%*d/%*d/%*d,%d/%*d/%*d/%*d", &w, &h);
184 err("can't parse %s (%d), skip screen detection", buff, ret);
188 log("detected %dx%d '%s' (%d) screen attached to fb %d and overlay %d",
189 w, h, screen_name, screen_id, fb_id, overlay_id);
196 /* attempt to extract this from FB then */
197 fd = open(fbname, O_RDWR);
199 err_perror("open %s", fbname);
203 ret = ioctl(fd, FBIOGET_VSCREENINFO, &fbvar);
206 err_perror("ioctl %s", fbname);
210 if (fbvar.xres == 0 || fbvar.yres == 0) {
211 err("VSCREENINFO has nothing meaningful");
215 pdata->screen_w = fbvar.xres;
216 pdata->screen_h = fbvar.yres;
220 static int osdl_setup_omap_layer(struct SDL_PrivateVideoData *pdata,
221 const char *fbname, int width, int height, int bpp)
223 int x = 0, y = 0, w = width, h = height; /* layer size and pos */
224 int screen_w = w, screen_h = h;
228 if (pdata->screen_w != 0)
229 screen_w = pdata->screen_w;
230 if (pdata->screen_h != 0)
231 screen_h = pdata->screen_h;
233 fd = open(fbname, O_RDWR);
235 err_perror("open %s", fbname);
239 /* FIXME: assuming layer doesn't change here */
240 if (pdata->saved_layer == NULL) {
241 struct omapfb_saved_layer *slayer;
242 slayer = calloc(1, sizeof(*slayer));
246 ret = ioctl(fd, OMAPFB_QUERY_PLANE, &slayer->pi);
248 err_perror("QUERY_PLANE");
252 ret = ioctl(fd, OMAPFB_QUERY_MEM, &slayer->mi);
254 err_perror("QUERY_MEM");
258 pdata->saved_layer = slayer;
261 tmp = getenv("SDL_OMAP_LAYER_SIZE");
264 if (strcasecmp(tmp, "fullscreen") == 0)
265 w = screen_w, h = screen_h;
266 else if (sscanf(tmp, "%dx%d", &w_, &h_) == 2)
269 err("layer size specified incorrectly, "
270 "should be like 800x480");
273 x = screen_w / 2 - w / 2;
274 y = screen_h / 2 - h / 2;
275 ret = osdl_setup_omapfb(fd, 1, x, y, w, h, width * height * ((bpp + 7) / 8) * 3);
281 int osdl_video_set_mode(struct SDL_PrivateVideoData *pdata, int width, int height, int bpp)
288 fbname = get_fb_device();
290 if (pdata->fbdev != NULL) {
291 vout_fbdev_finish(pdata->fbdev);
295 omapsdl_config_from_env();
297 ret = osdl_setup_omap_layer(pdata, fbname, width, height, bpp);
301 pdata->fbdev = vout_fbdev_init(fbname, &width, &height, 0);
302 if (pdata->fbdev == NULL)
305 if (!pdata->oshide_done) {
307 pdata->oshide_done = 1;
313 void *osdl_video_flip(struct SDL_PrivateVideoData *pdata)
315 if (pdata->fbdev == NULL)
318 if (gcfg_force_vsync)
319 vout_fbdev_wait_vsync(pdata->fbdev);
321 return vout_fbdev_flip(pdata->fbdev);
324 void osdl_video_finish(struct SDL_PrivateVideoData *pdata)
326 static const char *fbname;
328 fbname = get_fb_device();
329 if (pdata->fbdev != NULL) {
330 vout_fbdev_finish(pdata->fbdev);
334 /* restore the OMAP layer */
335 if (pdata->saved_layer != NULL) {
336 struct omapfb_saved_layer *slayer = pdata->saved_layer;
339 fd = open(fbname, O_RDWR);
341 int enabled = slayer->pi.enabled;
343 /* be sure to disable while setting up */
344 slayer->pi.enabled = 0;
345 ioctl(fd, OMAPFB_SETUP_PLANE, &slayer->pi);
346 ioctl(fd, OMAPFB_SETUP_MEM, &slayer->mi);
348 slayer->pi.enabled = enabled;
349 ioctl(fd, OMAPFB_SETUP_PLANE, &slayer->pi);
354 pdata->saved_layer = NULL;
357 if (pdata->oshide_done) {
359 pdata->oshide_done = 0;