touchscreen controls
[drnoksnes] / platform / sdlv.cpp
1 #include <stdio.h>
2
3 #include <X11/Xlib.h>
4 #include <X11/Xutil.h>
5 #include <X11/extensions/Xsp.h>
6 #include <SDL.h>
7 #include <SDL_syswm.h>
8
9 #include "snes9x.h"
10 #include "platform.h"
11 #include "display.h"
12 #include "gfx.h"
13 #include "ppu.h"
14
15 #define DIE(format, ...) do { \
16                 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
17                 fprintf(stderr, format "\n", ## __VA_ARGS__); \
18                 abort(); \
19         } while (0);
20
21 static SDL_Surface *screen;
22
23 static SDL_Rect windowSize, screenSize;
24 static bool gotWindowSize, gotScreenSize;
25
26 /** Inside the surface, where are we drawing */
27 static SDL_Rect renderArea;
28
29 #ifdef MAEMO
30 static void setDoubling(bool enable)
31 {
32         SDL_SysWMinfo wminfo;
33         SDL_VERSION(&wminfo.version);
34         if ( SDL_GetWMInfo(&wminfo) ) {
35                 Display *dpy = wminfo.info.x11.display;
36                 XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
37                 XFlush(dpy);
38         }
39 }
40 #endif
41
42 static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
43 {
44         result.x = areaW / 2 - w / 2;
45         result.w = w;
46         result.y = areaH / 2 - h / 2;
47         result.h = h;
48 }
49
50 static void calculateScreenSize()
51 {
52         SDL_SysWMinfo wminfo;
53         SDL_VERSION(&wminfo.version);
54
55         if ( SDL_GetWMInfo(&wminfo) ) {
56                 Display *dpy = wminfo.info.x11.display;
57                 Window w;
58                 SDL_Rect* size;
59                 XWindowAttributes xwa;
60
61                 if (Config.fullscreen) {
62                         w =  wminfo.info.x11.fswindow;
63                         size = &screenSize;
64                         gotScreenSize = true;
65                 } else {
66                         w =  wminfo.info.x11.wmwindow;
67                         size = &windowSize;
68                         gotWindowSize = true;
69                 }
70
71                 XGetWindowAttributes(dpy, w, &xwa);
72                 size->x = xwa.x;
73                 size->y = xwa.y;
74                 size->w = xwa.width;
75                 size->h = xwa.height;
76         }
77 }
78
79 void S9xSetTitle(const char *title)
80 {
81         SDL_SysWMinfo info;
82         SDL_VERSION(&info.version);
83         if ( SDL_GetWMInfo(&info) ) {
84                 Display *dpy = info.info.x11.display;
85                 Window win;
86                 if (dpy) {
87                         win = info.info.x11.fswindow;
88                         if (win) XStoreName(dpy, win, title);
89                         win = info.info.x11.wmwindow;
90                         if (win) XStoreName(dpy, win, title);
91                 }
92         }
93 }
94
95 static void freeVideoSurface()
96 {
97         screen = 0; // There's no need to free the screen surface.
98         GFX.Screen = 0;
99         
100         free(GFX.SubScreen); GFX.SubScreen = 0;
101         free(GFX.ZBuffer); GFX.ZBuffer = 0;
102         free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
103 }
104
105 static void setupVideoSurface()
106 {
107         // Real surface area.
108         unsigned gameWidth = IMAGE_WIDTH;
109         unsigned gameHeight = IMAGE_HEIGHT;
110         // SDL Window/Surface size (bigger, so we can get mouse events there).
111         unsigned winWidth, winHeight;
112
113 #ifdef MAEMO
114         if ((Config.fullscreen && !gotScreenSize) ||
115                 (!Config.fullscreen && !gotWindowSize)) {
116                 // Do a first try, in order to get window/screen size
117                 screen = SDL_SetVideoMode(gameWidth, gameHeight, 16,
118                         SDL_SWSURFACE | SDL_RESIZABLE |
119                         (Config.fullscreen ? SDL_FULLSCREEN : 0));
120                 if (!screen) DIE("SDL_SetVideoMode: %s", SDL_GetError());
121                 calculateScreenSize();
122         }
123         if (Config.fullscreen) {
124                 winWidth = screenSize.w;
125                 winHeight = screenSize.h;
126         } else {
127                 winWidth = windowSize.w;
128                 winHeight = windowSize.h;
129         }
130         
131         // So, can we enable Xsp?
132         if (gameWidth * 2 < winWidth && gameHeight * 2 < winHeight) {
133                 Config.xsp = true;
134         } else  {
135                 Config.xsp = false;
136                 setDoubling(false); // Before switching video modes; avoids flicker.
137         }
138 #else
139         winWidth = gameWidth;
140         winHeight = gameHeight;
141 #endif
142
143         // Safeguard
144         if (gameHeight > winHeight || gameWidth > winWidth)
145                 DIE("Video is larger than window size!");
146
147         screen = SDL_SetVideoMode(winWidth, winHeight,
148                                                                 Settings.SixteenBit ? 16 : 8,
149                                                                 SDL_SWSURFACE |
150                                                                 (Config.fullscreen ? SDL_FULLSCREEN : 0));
151         if (!screen)
152                 DIE("SDL_SetVideoMode: %s", SDL_GetError());
153         
154         SDL_ShowCursor(SDL_DISABLE);
155
156         // We get pitch surface values from SDL
157         GFX.RealPitch = GFX.Pitch = screen->pitch;
158         GFX.ZPitch = GFX.Pitch / 2; // gfx & tile.cpp depend on this, unfortunately.
159         GFX.PixSize = screen->format->BitsPerPixel / 8;
160
161         // Ok, calculate renderArea
162 #ifdef MAEMO
163         if (Config.xsp) {
164                 setDoubling(true);
165                 centerRectangle(renderArea, winWidth, winHeight,
166                         gameWidth * 2, gameHeight * 2);
167                 renderArea.w /= 2;
168                 renderArea.h /= 2;
169         } else {
170                 centerRectangle(renderArea, winWidth, winHeight, gameWidth, gameHeight);
171         }
172 #else
173         centerRectangle(renderArea, winWidth, winHeight, gameWidth, gameHeight);
174 #endif
175         
176         GFX.Screen = ((uint8*) screen->pixels)
177                 + (renderArea.x * GFX.PixSize)
178                 + (renderArea.y * GFX.Pitch);
179         GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
180         GFX.ZBuffer =  (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
181         GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
182
183         GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
184         GFX.PPL = GFX.Pitch >> 1;
185         GFX.PPLx2 = GFX.Pitch;
186
187         printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s %s\n",
188                 gameWidth, gameHeight,
189                 screen->w, screen->h, screen->format->BitsPerPixel,
190                 Config.fullscreen ? "fullscreen" : "windowed",
191                 Config.xsp ? "with pixel doubling" : "");
192 }
193
194 void S9xInitDisplay(int argc, const char ** argv)
195 {       
196         if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) 
197                 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
198
199         setupVideoSurface();
200 }
201
202 void S9xDeinitDisplay()
203 {
204         freeVideoSurface();     
205         SDL_QuitSubSystem(SDL_INIT_VIDEO);
206 }
207
208 void S9xVideoToggleFullscreen()
209 {
210         Config.fullscreen = !Config.fullscreen;
211         freeVideoSurface();
212         setupVideoSurface();
213 }
214
215 void S9xVideoOutputFocus(bool hasFocus)
216 {
217         if (Config.xsp) {
218                 setDoubling(hasFocus);
219         } 
220 }
221
222 void S9xVideoGetWindowSize(unsigned int* w, unsigned int* h)
223 {
224         if (Config.fullscreen) {
225                 *w = screenSize.w;
226                 *h = screenSize.h;
227         } else {
228                 *w = windowSize.w;
229                 *h = windowSize.h;
230         }
231 }
232
233 // This is here for completeness, but palette mode is useless on N8x0
234 void S9xSetPalette ()
235 {
236         if (Settings.SixteenBit) return;
237         
238         SDL_Color colors[256];
239         int brightness = IPPU.MaxBrightness *138;
240         for (int i = 0; i < 256; i++)
241         {
242                 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
243                 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
244                 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
245         }
246         
247         SDL_SetColors(screen, colors, 0, 256);
248 }
249
250 bool8_32 S9xInitUpdate ()
251 {
252         if(SDL_MUSTLOCK(screen)) 
253         {
254                 if(SDL_LockSurface(screen) < 0) {
255                         DIE("Failed to lock SDL surface: %s", SDL_GetError());
256                 }
257         }
258
259         return TRUE;
260 }
261
262 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
263 {
264         if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
265
266         SDL_UpdateRects(screen, 1, &renderArea);
267
268         return TRUE;
269 }
270