69af03a2 |
1 | /* |
6469a8c4 |
2 | * (C) GraÅžvydas "notaz" Ignotas, 2010-2012 |
69af03a2 |
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> |
799b0b87 |
13 | #include <sys/ioctl.h> |
69af03a2 |
14 | #include <unistd.h> |
69af03a2 |
15 | #include <linux/omapfb.h> |
16 | |
cc56203b |
17 | #include "libpicofe/menu.h" |
18 | #include "libpicofe/input.h" |
19 | #include "libpicofe/linux/fbdev.h" |
20 | #include "libpicofe/linux/xenv.h" |
69af03a2 |
21 | #include "plugin_lib.h" |
4c08b9e7 |
22 | #include "pl_gun_ts.h" |
55b0eeea |
23 | #include "plat.h" |
ab423939 |
24 | #include "plat_omap.h" |
6469a8c4 |
25 | #include "menu.h" |
69af03a2 |
26 | |
6469a8c4 |
27 | static struct vout_fbdev *main_fb, *layer_fb; |
69af03a2 |
28 | |
366631aa |
29 | static int omap_setup_layer_(int fd, int enabled, int x, int y, int w, int h) |
69af03a2 |
30 | { |
2c886904 |
31 | struct omapfb_plane_info pi = { 0, }; |
32 | struct omapfb_mem_info mi = { 0, }; |
69af03a2 |
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 | |
99d767a0 |
55 | // upto 1024x512 (2x resolution enhancement) |
56 | if (mi.size < 1024*512*2 * 3) { |
57 | mi.size = 1024*512*2 * 3; |
96d9fde1 |
58 | ret = ioctl(fd, OMAPFB_SETUP_MEM, &mi); |
59 | if (ret != 0) { |
60 | perror("SETUP_MEM"); |
61 | return -1; |
62 | } |
69af03a2 |
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 | |
6469a8c4 |
80 | static int omap_enable_layer(int enabled) |
69af03a2 |
81 | { |
96867f0e |
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 | |
4c08b9e7 |
92 | if (enabled) |
96867f0e |
93 | pl_set_gun_rect(x, y, w, h); |
4c08b9e7 |
94 | |
96867f0e |
95 | return omap_setup_layer_(vout_fbdev_get_fd(layer_fb), |
96 | enabled, x, y, w, h); |
69af03a2 |
97 | } |
98 | |
ab423939 |
99 | void plat_omap_gvideo_open(void) |
6469a8c4 |
100 | { |
101 | omap_enable_layer(1); |
102 | |
103 | // try to align redraws to vsync |
104 | vout_fbdev_wait_vsync(layer_fb); |
105 | } |
106 | |
5f6c916d |
107 | void *plat_gvideo_set_mode(int *w_in, int *h_in, int *bpp) |
6469a8c4 |
108 | { |
f6edea0c |
109 | int l = 0, r = 0, t = 0, b = 0; |
5f6c916d |
110 | int w = *w_in, h = *h_in; |
6469a8c4 |
111 | void *buf; |
112 | |
efcf1f73 |
113 | if (g_scaler == SCALE_1_1 || g_scaler == SCALE_2_2) { |
96867f0e |
114 | if (w > g_menuscreen_w) |
5f6c916d |
115 | l = r = (w - g_menuscreen_w) / 2; |
96867f0e |
116 | if (h > g_menuscreen_h) |
5f6c916d |
117 | t = b = (h - g_menuscreen_h) / 2; |
f6edea0c |
118 | } |
96867f0e |
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; |
f6edea0c |
133 | |
5f6c916d |
134 | buf = vout_fbdev_resize(layer_fb, w, h, *bpp, |
ad3d2988 |
135 | l, r, t, b, 3, 1); |
6469a8c4 |
136 | |
92a5fe88 |
137 | vout_fbdev_clear(layer_fb); |
138 | |
6469a8c4 |
139 | omap_enable_layer(1); |
140 | |
141 | return buf; |
142 | } |
143 | |
144 | void *plat_gvideo_flip(void) |
145 | { |
146 | return vout_fbdev_flip(layer_fb); |
147 | } |
148 | |
149 | void plat_gvideo_close(void) |
150 | { |
151 | omap_enable_layer(0); |
152 | } |
153 | |
fba06457 |
154 | void plat_video_menu_enter(int is_rom_loaded) |
155 | { |
156 | g_menuscreen_ptr = vout_fbdev_resize(main_fb, |
ad3d2988 |
157 | g_menuscreen_w, g_menuscreen_h, 16, 0, 0, 0, 0, 3, 0); |
fba06457 |
158 | if (g_menuscreen_ptr == NULL) |
159 | fprintf(stderr, "warning: vout_fbdev_resize failed\n"); |
ad3d2988 |
160 | vout_fbdev_clear(main_fb); |
0b49a8f7 |
161 | |
3a321131 |
162 | xenv_update(NULL, NULL, NULL, NULL); |
fba06457 |
163 | } |
164 | |
69af03a2 |
165 | void plat_video_menu_begin(void) |
166 | { |
167 | } |
168 | |
169 | void plat_video_menu_end(void) |
170 | { |
171 | g_menuscreen_ptr = vout_fbdev_flip(main_fb); |
172 | } |
173 | |
fba06457 |
174 | void plat_video_menu_leave(void) |
175 | { |
176 | /* have to get rid of panning so that plugins that |
177 | * use fb0 and don't ever pan can work. */ |
fba06457 |
178 | g_menuscreen_ptr = vout_fbdev_resize(main_fb, |
ad3d2988 |
179 | g_menuscreen_w, g_menuscreen_h, 16, 0, 0, 0, 0, 1, 0); |
fba06457 |
180 | if (g_menuscreen_ptr == NULL) |
181 | fprintf(stderr, "warning: vout_fbdev_resize failed\n"); |
ad3d2988 |
182 | vout_fbdev_clear(main_fb); |
fba06457 |
183 | } |
184 | |
a805c855 |
185 | void plat_minimize(void) |
186 | { |
36dfb787 |
187 | int ret; |
188 | |
189 | ret = vout_fbdev_save(layer_fb); |
190 | if (ret != 0) { |
191 | printf("minimize: layer/fb handling failed\n"); |
192 | return; |
193 | } |
194 | |
a805c855 |
195 | xenv_minimize(); |
36dfb787 |
196 | |
61f97bb0 |
197 | in_set_config_int(0, IN_CFG_BLOCKING, 0); /* flush event queue */ |
36dfb787 |
198 | omap_enable_layer(0); /* restore layer mem */ |
199 | vout_fbdev_restore(layer_fb); |
a805c855 |
200 | } |
201 | |
6469a8c4 |
202 | void *plat_prepare_screenshot(int *w, int *h, int *bpp) |
203 | { |
204 | return NULL; |
205 | } |
206 | |
ab423939 |
207 | void plat_omap_init(void) |
69af03a2 |
208 | { |
209 | const char *main_fb_name, *layer_fb_name; |
69af03a2 |
210 | int fd, ret, w, h; |
211 | |
212 | main_fb_name = getenv("FBDEV_MAIN"); |
213 | if (main_fb_name == NULL) |
214 | main_fb_name = "/dev/fb0"; |
215 | |
216 | layer_fb_name = getenv("FBDEV_LAYER"); |
217 | if (layer_fb_name == NULL) |
218 | layer_fb_name = "/dev/fb1"; |
219 | |
220 | // must set the layer up first to be able to use it |
221 | fd = open(layer_fb_name, O_RDWR); |
222 | if (fd == -1) { |
223 | fprintf(stderr, "%s: ", layer_fb_name); |
224 | perror("open"); |
225 | exit(1); |
226 | } |
227 | |
6469a8c4 |
228 | g_layer_x = 80, g_layer_y = 0; |
229 | g_layer_w = 640, g_layer_h = 480; |
230 | |
366631aa |
231 | ret = omap_setup_layer_(fd, 0, g_layer_x, g_layer_y, g_layer_w, g_layer_h); |
69af03a2 |
232 | close(fd); |
233 | if (ret != 0) { |
234 | fprintf(stderr, "failed to set up layer, exiting.\n"); |
235 | exit(1); |
236 | } |
237 | |
3a321131 |
238 | xenv_init(NULL, "PCSX-ReARMed"); |
69af03a2 |
239 | |
240 | w = h = 0; |
241 | main_fb = vout_fbdev_init(main_fb_name, &w, &h, 16, 2); |
242 | if (main_fb == NULL) { |
243 | fprintf(stderr, "couldn't init fb: %s\n", main_fb_name); |
244 | exit(1); |
245 | } |
246 | |
a4da039c |
247 | g_menuscreen_w = g_menuscreen_pp = w; |
69af03a2 |
248 | g_menuscreen_h = h; |
249 | g_menuscreen_ptr = vout_fbdev_flip(main_fb); |
bb88ec28 |
250 | pl_rearmed_cbs.screen_w = w; |
251 | pl_rearmed_cbs.screen_h = h; |
69af03a2 |
252 | |
253 | w = 640; |
0b49a8f7 |
254 | h = 512; |
69af03a2 |
255 | layer_fb = vout_fbdev_init(layer_fb_name, &w, &h, 16, 3); |
256 | if (layer_fb == NULL) { |
257 | fprintf(stderr, "couldn't init fb: %s\n", layer_fb_name); |
258 | goto fail0; |
259 | } |
260 | |
69af03a2 |
261 | return; |
262 | |
69af03a2 |
263 | fail0: |
264 | vout_fbdev_finish(main_fb); |
265 | exit(1); |
69af03a2 |
266 | } |
267 | |
ab423939 |
268 | void plat_omap_finish(void) |
bd6267e6 |
269 | { |
270 | omap_enable_layer(0); |
271 | vout_fbdev_finish(layer_fb); |
272 | vout_fbdev_finish(main_fb); |
0b49a8f7 |
273 | xenv_finish(); |
bd6267e6 |
274 | } |
275 | |