f641fccb |
1 | /* |
2 | * (C) GraÅžvydas "notaz" Ignotas, 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 <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 "omapsdl.h" |
21 | #include "omapfb.h" |
22 | #include "linux/fbdev.h" |
23 | #include "linux/oshide.h" |
24 | |
25 | struct omapfb_saved_layer { |
26 | struct omapfb_plane_info pi; |
27 | struct omapfb_mem_info mi; |
28 | }; |
29 | |
b9a19b44 |
30 | static const char *get_fb_device(void) |
31 | { |
32 | const char *fbname = getenv("SDL_FBDEV"); |
33 | if (fbname == NULL) |
34 | fbname = "/dev/fb1"; |
35 | |
36 | return fbname; |
37 | } |
38 | |
f641fccb |
39 | static int osdl_setup_omapfb(int fd, int enabled, int x, int y, int w, int h, int mem) |
40 | { |
41 | struct omapfb_plane_info pi; |
42 | struct omapfb_mem_info mi; |
43 | int ret; |
44 | |
45 | ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi); |
46 | if (ret != 0) { |
7b66578c |
47 | err_perror("QUERY_PLANE"); |
f641fccb |
48 | return -1; |
49 | } |
50 | |
51 | ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi); |
52 | if (ret != 0) { |
7b66578c |
53 | err_perror("QUERY_MEM"); |
f641fccb |
54 | return -1; |
55 | } |
56 | |
57 | /* must disable when changing stuff */ |
58 | if (pi.enabled) { |
59 | pi.enabled = 0; |
60 | ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi); |
61 | if (ret != 0) |
7b66578c |
62 | err_perror("SETUP_PLANE"); |
f641fccb |
63 | } |
64 | |
65 | mi.size = mem; |
66 | ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi); |
67 | if (ret != 0) { |
7b66578c |
68 | err_perror("SETUP_MEM"); |
f641fccb |
69 | return -1; |
70 | } |
71 | |
72 | pi.pos_x = x; |
73 | pi.pos_y = y; |
74 | pi.out_width = w; |
75 | pi.out_height = h; |
76 | pi.enabled = enabled; |
77 | |
78 | ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi); |
79 | if (ret != 0) { |
7b66578c |
80 | err_perror("SETUP_PLANE"); |
f641fccb |
81 | return -1; |
82 | } |
83 | |
84 | return 0; |
85 | } |
86 | |
87 | static int read_sysfs(const char *fname, char *buff, size_t size) |
88 | { |
89 | FILE *f; |
90 | int ret; |
91 | |
92 | f = fopen(fname, "r"); |
93 | if (f == NULL) { |
7b66578c |
94 | err_perror("open %s: ", fname); |
f641fccb |
95 | return -1; |
96 | } |
97 | |
98 | ret = fread(buff, 1, size - 1, f); |
99 | fclose(f); |
100 | if (ret <= 0) { |
7b66578c |
101 | err_perror("read %s: ", fname); |
f641fccb |
102 | return -1; |
103 | } |
104 | |
105 | buff[ret] = 0; |
106 | for (ret--; ret >= 0 && isspace(buff[ret]); ret--) |
107 | buff[ret] = 0; |
108 | |
109 | return 0; |
110 | } |
111 | |
b9a19b44 |
112 | int osdl_video_detect_screen(struct SDL_PrivateVideoData *pdata) |
f641fccb |
113 | { |
f641fccb |
114 | int fb_id, overlay_id = -1, screen_id = -1; |
b9a19b44 |
115 | struct fb_var_screeninfo fbvar; |
f641fccb |
116 | char buff[64], screen_name[64]; |
b9a19b44 |
117 | const char *fbname; |
f641fccb |
118 | struct stat status; |
b9a19b44 |
119 | int fd, i, ret; |
120 | int w, h; |
f641fccb |
121 | FILE *f; |
122 | |
b9a19b44 |
123 | fbname = get_fb_device(); |
f641fccb |
124 | |
b9a19b44 |
125 | /* Figure out screen resolution, we need to know default resolution |
126 | * to report to SDL and for centering stuff. |
f641fccb |
127 | * The only way to achieve this seems to be walking some sysfs files.. */ |
128 | ret = stat(fbname, &status); |
129 | if (ret != 0) { |
7b66578c |
130 | err_perror("can't stat %s", fbname); |
b9a19b44 |
131 | goto skip_screen; |
f641fccb |
132 | } |
133 | fb_id = minor(status.st_rdev); |
134 | |
135 | snprintf(buff, sizeof(buff), "/sys/class/graphics/fb%d/overlays", fb_id); |
136 | f = fopen(buff, "r"); |
137 | if (f == NULL) { |
7b66578c |
138 | err("can't open %s, skip screen detection", buff); |
f641fccb |
139 | goto skip_screen; |
140 | } |
141 | |
142 | ret = fscanf(f, "%d", &overlay_id); |
143 | fclose(f); |
144 | if (ret != 1) { |
7b66578c |
145 | err("can't parse %s, skip screen detection", buff); |
f641fccb |
146 | goto skip_screen; |
147 | } |
148 | |
149 | snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/overlay%d/manager", overlay_id); |
150 | ret = read_sysfs(buff, screen_name, sizeof(screen_name)); |
151 | if (ret < 0) { |
7b66578c |
152 | err("skip screen detection"); |
f641fccb |
153 | goto skip_screen; |
154 | } |
155 | |
156 | for (i = 0; ; i++) { |
157 | snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/display%d/name", i); |
158 | ret = read_sysfs(buff, buff, sizeof(buff)); |
159 | if (ret < 0) |
160 | break; |
161 | |
162 | if (strcmp(screen_name, buff) == 0) { |
163 | screen_id = i; |
164 | break; |
165 | } |
166 | } |
167 | |
168 | if (screen_id < 0) { |
7b66578c |
169 | err("could not find screen"); |
f641fccb |
170 | goto skip_screen; |
171 | } |
172 | |
173 | snprintf(buff, sizeof(buff), "/sys/devices/platform/omapdss/display%d/timings", screen_id); |
174 | f = fopen(buff, "r"); |
175 | if (f == NULL) { |
7b66578c |
176 | err("can't open %s, skip screen detection", buff); |
f641fccb |
177 | goto skip_screen; |
178 | } |
179 | |
b9a19b44 |
180 | ret = fscanf(f, "%*d,%d/%*d/%*d/%*d,%d/%*d/%*d/%*d", &w, &h); |
f641fccb |
181 | fclose(f); |
182 | if (ret != 2) { |
7b66578c |
183 | err("can't parse %s (%d), skip screen detection", buff, ret); |
f641fccb |
184 | goto skip_screen; |
185 | } |
186 | |
7b66578c |
187 | log("detected %dx%d '%s' (%d) screen attached to fb %d and overlay %d", |
b9a19b44 |
188 | w, h, screen_name, screen_id, fb_id, overlay_id); |
189 | |
190 | pdata->screen_w = w; |
191 | pdata->screen_h = h; |
192 | return 0; |
f641fccb |
193 | |
194 | skip_screen: |
b9a19b44 |
195 | /* attempt to extract this from FB then */ |
196 | fd = open(fbname, O_RDWR); |
197 | if (fd == -1) { |
198 | err_perror("open %s", fbname); |
199 | return -1; |
200 | } |
201 | |
202 | ret = ioctl(fd, FBIOGET_VSCREENINFO, &fbvar); |
203 | close(fd); |
204 | if (ret == -1) { |
205 | err_perror("ioctl %s", fbname); |
206 | return -1; |
207 | } |
208 | |
209 | if (fbvar.xres == 0 || fbvar.yres == 0) { |
210 | err("VSCREENINFO has nothing meaningful"); |
211 | return -1; |
212 | } |
213 | |
214 | pdata->screen_w = fbvar.xres; |
215 | pdata->screen_h = fbvar.yres; |
216 | return 0; |
217 | } |
218 | |
219 | static int osdl_setup_omap_layer(struct SDL_PrivateVideoData *pdata, |
220 | const char *fbname, int width, int height, int bpp) |
221 | { |
222 | int x = 0, y = 0, w = width, h = height; /* layer size and pos */ |
223 | int screen_w = w, screen_h = h; |
224 | const char *tmp; |
225 | int ret, fd; |
226 | |
227 | if (pdata->screen_w != 0) |
228 | screen_w = pdata->screen_w; |
229 | if (pdata->screen_h != 0) |
230 | screen_h = pdata->screen_h; |
231 | |
232 | fd = open(fbname, O_RDWR); |
233 | if (fd == -1) { |
234 | err_perror("open %s", fbname); |
235 | return -1; |
236 | } |
237 | |
238 | /* FIXME: assuming layer doesn't change here */ |
239 | if (pdata->saved_layer == NULL) { |
240 | struct omapfb_saved_layer *slayer; |
241 | slayer = calloc(1, sizeof(*slayer)); |
242 | if (slayer == NULL) |
243 | return -1; |
244 | |
245 | ret = ioctl(fd, OMAPFB_QUERY_PLANE, &slayer->pi); |
246 | if (ret != 0) { |
247 | err_perror("QUERY_PLANE"); |
248 | return -1; |
249 | } |
250 | |
251 | ret = ioctl(fd, OMAPFB_QUERY_MEM, &slayer->mi); |
252 | if (ret != 0) { |
253 | err_perror("QUERY_MEM"); |
254 | return -1; |
255 | } |
256 | |
257 | pdata->saved_layer = slayer; |
258 | } |
259 | |
f641fccb |
260 | tmp = getenv("SDL_OMAP_LAYER_SIZE"); |
261 | if (tmp != NULL) { |
262 | int w_, h_; |
263 | if (strcasecmp(tmp, "fullscreen") == 0) |
264 | w = screen_w, h = screen_h; |
265 | else if (sscanf(tmp, "%dx%d", &w_, &h_) == 2) |
266 | w = w_, h = h_; |
267 | else |
7b66578c |
268 | err("layer size specified incorrectly, " |
269 | "should be like 800x480"); |
f641fccb |
270 | } |
271 | |
272 | x = screen_w / 2 - w / 2; |
273 | y = screen_h / 2 - h / 2; |
274 | ret = osdl_setup_omapfb(fd, 1, x, y, w, h, width * height * ((bpp + 7) / 8) * 3); |
275 | close(fd); |
276 | |
277 | return ret; |
278 | } |
279 | |
280 | int osdl_video_set_mode(struct SDL_PrivateVideoData *pdata, int width, int height, int bpp) |
281 | { |
282 | const char *fbname; |
283 | int ret; |
284 | |
285 | bpp = 16; // FIXME |
286 | |
b9a19b44 |
287 | fbname = get_fb_device(); |
f641fccb |
288 | |
289 | if (pdata->fbdev != NULL) { |
290 | vout_fbdev_finish(pdata->fbdev); |
291 | pdata->fbdev = NULL; |
292 | } |
293 | |
294 | omapsdl_config_from_env(); |
295 | |
296 | ret = osdl_setup_omap_layer(pdata, fbname, width, height, bpp); |
297 | if (ret < 0) |
298 | return -1; |
299 | |
300 | pdata->fbdev = vout_fbdev_init(fbname, &width, &height, 0); |
301 | if (pdata->fbdev == NULL) |
302 | return -1; |
303 | |
304 | if (!pdata->oshide_done) { |
305 | oshide_init(); |
306 | pdata->oshide_done = 1; |
307 | } |
308 | |
309 | return 0; |
310 | } |
311 | |
312 | void *osdl_video_flip(struct SDL_PrivateVideoData *pdata) |
313 | { |
314 | if (pdata->fbdev == NULL) |
315 | return NULL; |
316 | |
317 | if (gcfg_force_vsync) |
318 | vout_fbdev_wait_vsync(pdata->fbdev); |
319 | |
320 | return vout_fbdev_flip(pdata->fbdev); |
321 | } |
322 | |
323 | void osdl_video_finish(struct SDL_PrivateVideoData *pdata) |
324 | { |
325 | static const char *fbname; |
326 | |
b9a19b44 |
327 | fbname = get_fb_device(); |
f641fccb |
328 | if (pdata->fbdev != NULL) { |
329 | vout_fbdev_finish(pdata->fbdev); |
330 | pdata->fbdev = NULL; |
331 | } |
332 | |
333 | /* restore the OMAP layer */ |
334 | if (pdata->saved_layer != NULL) { |
335 | struct omapfb_saved_layer *slayer = pdata->saved_layer; |
336 | int fd; |
337 | |
338 | fd = open(fbname, O_RDWR); |
339 | if (fd != -1) { |
340 | int enabled = slayer->pi.enabled; |
341 | |
342 | /* be sure to disable while setting up */ |
343 | slayer->pi.enabled = 0; |
344 | ioctl(fd, OMAPFB_SETUP_PLANE, &slayer->pi); |
345 | ioctl(fd, OMAPFB_SETUP_MEM, &slayer->mi); |
346 | if (enabled) { |
347 | slayer->pi.enabled = enabled; |
348 | ioctl(fd, OMAPFB_SETUP_PLANE, &slayer->pi); |
349 | } |
350 | close(fd); |
351 | } |
352 | free(slayer); |
353 | pdata->saved_layer = NULL; |
354 | } |
355 | |
356 | if (pdata->oshide_done) { |
357 | oshide_finish(); |
358 | pdata->oshide_done = 0; |
359 | } |
360 | } |
361 | |