snes mouse support
[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 struct gui GUI;
22
23 static SDL_Surface *screen;
24
25 static SDL_Rect windowSize, screenSize;
26 static bool gotWindowSize, gotScreenSize;
27
28 /** Inside the surface, where are we drawing */
29 static SDL_Rect renderArea;
30
31 #ifdef MAEMO
32 static void setDoubling(bool enable)
33 {
34         SDL_SysWMinfo wminfo;
35         SDL_VERSION(&wminfo.version);
36         if ( SDL_GetWMInfo(&wminfo) ) {
37                 Display *dpy = wminfo.info.x11.display;
38                 XSPSetPixelDoubling(dpy, 0, enable ? 1 : 0);
39                 XFlush(dpy);
40         }
41 }
42 #endif
43
44 static void centerRectangle(SDL_Rect& result, int areaW, int areaH, int w, int h)
45 {
46         result.x = areaW / 2 - w / 2;
47         result.w = w;
48         result.y = areaH / 2 - h / 2;
49         result.h = h;
50 }
51
52 static void calculateScreenSize()
53 {
54         SDL_SysWMinfo wminfo;
55         SDL_VERSION(&wminfo.version);
56
57         if ( SDL_GetWMInfo(&wminfo) ) {
58                 Display *dpy = wminfo.info.x11.display;
59                 Window w;
60                 SDL_Rect* size;
61                 XWindowAttributes xwa;
62
63                 if (Config.fullscreen) {
64                         w =  wminfo.info.x11.fswindow;
65                         size = &screenSize;
66                         gotScreenSize = true;
67                 } else {
68                         w =  wminfo.info.x11.wmwindow;
69                         size = &windowSize;
70                         gotWindowSize = true;
71                 }
72
73                 XGetWindowAttributes(dpy, w, &xwa);
74                 size->x = xwa.x;
75                 size->y = xwa.y;
76                 size->w = xwa.width;
77                 size->h = xwa.height;
78         }
79 }
80
81 void S9xSetTitle(const char *title)
82 {
83         SDL_SysWMinfo info;
84         SDL_VERSION(&info.version);
85         if ( SDL_GetWMInfo(&info) ) {
86                 Display *dpy = info.info.x11.display;
87                 Window win;
88                 if (dpy) {
89                         win = info.info.x11.fswindow;
90                         if (win) XStoreName(dpy, win, title);
91                         win = info.info.x11.wmwindow;
92                         if (win) XStoreName(dpy, win, title);
93                 }
94         }
95 }
96
97 static void freeVideoSurface()
98 {
99         screen = 0; // There's no need to free the screen surface.
100         GFX.Screen = 0;
101         
102         free(GFX.SubScreen); GFX.SubScreen = 0;
103         free(GFX.ZBuffer); GFX.ZBuffer = 0;
104         free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
105 }
106
107 static void setupVideoSurface()
108 {
109         // Real surface area.
110         const unsigned gameWidth = IMAGE_WIDTH;
111         const unsigned gameHeight = IMAGE_HEIGHT;
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                 GUI.Width = screenSize.w;
125                 GUI.Height = screenSize.h;
126         } else {
127                 GUI.Width = windowSize.w;
128                 GUI.Height = windowSize.h;
129         }
130         
131         // So, can we enable Xsp?
132         if (gameWidth * 2 < GUI.Width && gameHeight * 2 < GUI.Height) {
133                 Config.xsp = true;
134         } else  {
135                 Config.xsp = false;
136                 setDoubling(false); // Before switching video modes; avoids flicker.
137         }
138 #else
139         GUI.Width = gameWidth;
140         GUI.Height = gameHeight;
141 #endif
142
143         // Safeguard
144         if (gameHeight > GUI.Height || gameWidth > GUI.Width)
145                 DIE("Video is larger than window size!");
146
147         screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
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, GUI.Width, GUI.Height,
166                         gameWidth * 2, gameHeight * 2);
167         } else {
168                 centerRectangle(renderArea, GUI.Width, GUI.Height,
169                         gameWidth, gameHeight);
170         }
171 #else
172         centerRectangle(renderArea, GUI.Width, GUI.Height, gameWidth, gameHeight);
173 #endif
174         
175         GFX.Screen = ((uint8*) screen->pixels)
176                 + (renderArea.x * GFX.PixSize)
177                 + (renderArea.y * GFX.Pitch);
178         GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
179         GFX.ZBuffer =  (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
180         GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
181
182         GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
183         GFX.PPL = GFX.Pitch >> 1;
184         GFX.PPLx2 = GFX.Pitch;
185
186         GUI.RenderX = renderArea.x;
187         GUI.RenderY = renderArea.y;
188         GUI.RenderW = renderArea.w;
189         GUI.RenderH = renderArea.h;
190
191 #ifdef MAEMO
192         if (Config.xsp) {
193                 // Do not update 2x the area.
194                 renderArea.w /= 2;
195                 renderArea.h /= 2;
196         }
197 #endif
198
199         printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s %s\n",
200                 gameWidth, gameHeight,
201                 screen->w, screen->h, screen->format->BitsPerPixel,
202                 Config.fullscreen ? "fullscreen" : "windowed",
203                 Config.xsp ? "with pixel doubling" : "");
204 }
205
206 void S9xInitDisplay(int argc, const char ** argv)
207 {       
208         if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) 
209                 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
210
211         setupVideoSurface();
212 }
213
214 void S9xDeinitDisplay()
215 {
216         freeVideoSurface();     
217         SDL_QuitSubSystem(SDL_INIT_VIDEO);
218 }
219
220 void S9xVideoToggleFullscreen()
221 {
222         Config.fullscreen = !Config.fullscreen;
223         freeVideoSurface();
224         setupVideoSurface();
225 }
226
227 void S9xVideoOutputFocus(bool hasFocus)
228 {
229         if (Config.xsp) {
230                 setDoubling(hasFocus);
231         } 
232 }
233
234 // This is here for completeness, but palette mode is useless on N8x0
235 void S9xSetPalette ()
236 {
237         if (Settings.SixteenBit) return;
238         
239         SDL_Color colors[256];
240         int brightness = IPPU.MaxBrightness *138;
241         for (int i = 0; i < 256; i++)
242         {
243                 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
244                 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
245                 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
246         }
247         
248         SDL_SetColors(screen, colors, 0, 256);
249 }
250
251 bool8_32 S9xInitUpdate ()
252 {
253         if(SDL_MUSTLOCK(screen)) 
254         {
255                 if(SDL_LockSurface(screen) < 0) {
256                         DIE("Failed to lock SDL surface: %s", SDL_GetError());
257                 }
258         }
259
260         return TRUE;
261 }
262
263 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
264 {
265         if(SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen);
266
267         SDL_UpdateRects(screen, 1, &renderArea);
268
269         return TRUE;
270 }
271