standalone: allow scaler to cut off the letterbox
[pcsx_rearmed.git] / frontend / plat_omap.c
... / ...
CommitLineData
1/*
2 * (C) GraÅžvydas "notaz" Ignotas, 2010-2012
3 *
4 * This work is licensed under the terms of the GNU GPLv2 or later.
5 * See the COPYING file in the top-level directory.
6 */
7
8#include <stdio.h>
9#include <stdlib.h>
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <fcntl.h>
13#include <sys/ioctl.h>
14#include <unistd.h>
15#include <linux/omapfb.h>
16
17#include "libpicofe/menu.h"
18#include "libpicofe/input.h"
19#include "libpicofe/linux/fbdev.h"
20#include "libpicofe/linux/xenv.h"
21#include "plugin_lib.h"
22#include "pl_gun_ts.h"
23#include "plat.h"
24#include "plat_omap.h"
25#include "menu.h"
26
27static struct vout_fbdev *main_fb, *layer_fb;
28
29static int omap_setup_layer_(int fd, int enabled, int x, int y, int w, int h)
30{
31 struct omapfb_plane_info pi = { 0, };
32 struct omapfb_mem_info mi = { 0, };
33 int ret;
34
35 ret = ioctl(fd, OMAPFB_QUERY_PLANE, &pi);
36 if (ret != 0) {
37 perror("QUERY_PLANE");
38 return -1;
39 }
40
41 ret = ioctl(fd, OMAPFB_QUERY_MEM, &mi);
42 if (ret != 0) {
43 perror("QUERY_MEM");
44 return -1;
45 }
46
47 /* must disable when changing stuff */
48 if (pi.enabled) {
49 pi.enabled = 0;
50 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
51 if (ret != 0)
52 perror("SETUP_PLANE");
53 }
54
55 // upto 1024x512 (2x resolution enhancement)
56 if (mi.size < 1024*512*2 * 3) {
57 mi.size = 1024*512*2 * 3;
58 ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi);
59 if (ret != 0) {
60 perror("SETUP_MEM");
61 return -1;
62 }
63 }
64
65 pi.pos_x = x;
66 pi.pos_y = y;
67 pi.out_width = w;
68 pi.out_height = h;
69 pi.enabled = enabled;
70
71 ret = ioctl(fd, OMAPFB_SETUP_PLANE, &pi);
72 if (ret != 0) {
73 perror("SETUP_PLANE");
74 return -1;
75 }
76
77 return 0;
78}
79
80static int omap_enable_layer(int enabled)
81{
82 int x = g_layer_x, y = g_layer_y;
83 int w = g_layer_w, h = g_layer_h;
84
85 // it's not allowed for the layer to be partially offscreen,
86 // instead it is faked by plat_gvideo_set_mode()
87 if (x < 0) { w += x; x = 0; }
88 if (y < 0) { h += y; y = 0; }
89 if (x + w > 800) w = 800 - x;
90 if (y + h > 480) h = 480 - y;
91
92 if (enabled)
93 pl_set_gun_rect(x, y, w, h);
94
95 return omap_setup_layer_(vout_fbdev_get_fd(layer_fb),
96 enabled, x, y, w, h);
97}
98
99void plat_omap_gvideo_open(void)
100{
101 omap_enable_layer(1);
102
103 // try to align redraws to vsync
104 vout_fbdev_wait_vsync(layer_fb);
105}
106
107void *plat_gvideo_set_mode(int *w_in, int *h_in, int *bpp)
108{
109 int l = 0, r = 0, t = 0, b = 0;
110 int w = *w_in, h = *h_in;
111 void *buf;
112
113 if (g_scaler == SCALE_1_1 || g_scaler == SCALE_2_2) {
114 if (w > g_menuscreen_w)
115 l = r = (w - g_menuscreen_w) / 2;
116 if (h > g_menuscreen_h)
117 t = b = (h - g_menuscreen_h) / 2;
118 }
119 else if (g_scaler == SCALE_CUSTOM) {
120 int right = g_layer_x + g_layer_w;
121 int bottom = g_layer_y + g_layer_h;
122 if (g_layer_x < 0)
123 l = -g_layer_x * w / g_menuscreen_w;
124 if (g_layer_y < 0)
125 t = -g_layer_y * h / g_menuscreen_h;
126 if (right > g_menuscreen_w)
127 r = (right - g_menuscreen_w) * w / g_menuscreen_w;
128 if (bottom > g_menuscreen_h)
129 b = (bottom - g_menuscreen_h) * h / g_menuscreen_h;
130 }
131 w -= l + r;
132 h -= t + b;
133
134 buf = vout_fbdev_resize(layer_fb, w, h, *bpp,
135 l, r, t, b, 3);
136
137 vout_fbdev_clear(layer_fb);
138
139 omap_enable_layer(1);
140
141 return buf;
142}
143
144void *plat_gvideo_flip(void)
145{
146 return vout_fbdev_flip(layer_fb);
147}
148
149void plat_gvideo_close(void)
150{
151 omap_enable_layer(0);
152}
153
154void plat_video_menu_enter(int is_rom_loaded)
155{
156 g_menuscreen_ptr = vout_fbdev_resize(main_fb,
157 g_menuscreen_w, g_menuscreen_h, 16, 0, 0, 0, 0, 3);
158 if (g_menuscreen_ptr == NULL)
159 fprintf(stderr, "warning: vout_fbdev_resize failed\n");
160
161 xenv_update(NULL, NULL, NULL, NULL);
162}
163
164void plat_video_menu_begin(void)
165{
166}
167
168void plat_video_menu_end(void)
169{
170 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
171}
172
173void plat_video_menu_leave(void)
174{
175 /* have to get rid of panning so that plugins that
176 * use fb0 and don't ever pan can work. */
177 vout_fbdev_clear(main_fb);
178 g_menuscreen_ptr = vout_fbdev_resize(main_fb,
179 g_menuscreen_w, g_menuscreen_h, 16, 0, 0, 0, 0, 1);
180 if (g_menuscreen_ptr == NULL)
181 fprintf(stderr, "warning: vout_fbdev_resize failed\n");
182}
183
184void plat_minimize(void)
185{
186 int ret;
187
188 ret = vout_fbdev_save(layer_fb);
189 if (ret != 0) {
190 printf("minimize: layer/fb handling failed\n");
191 return;
192 }
193
194 xenv_minimize();
195
196 in_set_config_int(0, IN_CFG_BLOCKING, 0); /* flush event queue */
197 omap_enable_layer(0); /* restore layer mem */
198 vout_fbdev_restore(layer_fb);
199}
200
201void *plat_prepare_screenshot(int *w, int *h, int *bpp)
202{
203 return NULL;
204}
205
206void plat_omap_init(void)
207{
208 const char *main_fb_name, *layer_fb_name;
209 int fd, ret, w, h;
210
211 main_fb_name = getenv("FBDEV_MAIN");
212 if (main_fb_name == NULL)
213 main_fb_name = "/dev/fb0";
214
215 layer_fb_name = getenv("FBDEV_LAYER");
216 if (layer_fb_name == NULL)
217 layer_fb_name = "/dev/fb1";
218
219 // must set the layer up first to be able to use it
220 fd = open(layer_fb_name, O_RDWR);
221 if (fd == -1) {
222 fprintf(stderr, "%s: ", layer_fb_name);
223 perror("open");
224 exit(1);
225 }
226
227 g_layer_x = 80, g_layer_y = 0;
228 g_layer_w = 640, g_layer_h = 480;
229
230 ret = omap_setup_layer_(fd, 0, g_layer_x, g_layer_y, g_layer_w, g_layer_h);
231 close(fd);
232 if (ret != 0) {
233 fprintf(stderr, "failed to set up layer, exiting.\n");
234 exit(1);
235 }
236
237 xenv_init(NULL, "PCSX-ReARMed");
238
239 w = h = 0;
240 main_fb = vout_fbdev_init(main_fb_name, &w, &h, 16, 2);
241 if (main_fb == NULL) {
242 fprintf(stderr, "couldn't init fb: %s\n", main_fb_name);
243 exit(1);
244 }
245
246 g_menuscreen_w = g_menuscreen_pp = w;
247 g_menuscreen_h = h;
248 g_menuscreen_ptr = vout_fbdev_flip(main_fb);
249 pl_rearmed_cbs.screen_w = w;
250 pl_rearmed_cbs.screen_h = h;
251
252 w = 640;
253 h = 512;
254 layer_fb = vout_fbdev_init(layer_fb_name, &w, &h, 16, 3);
255 if (layer_fb == NULL) {
256 fprintf(stderr, "couldn't init fb: %s\n", layer_fb_name);
257 goto fail0;
258 }
259
260 return;
261
262fail0:
263 vout_fbdev_finish(main_fb);
264 exit(1);
265}
266
267void plat_omap_finish(void)
268{
269 omap_enable_layer(0);
270 vout_fbdev_finish(layer_fb);
271 vout_fbdev_finish(main_fb);
272 xenv_finish();
273}
274