non-composited mode
[drnoksnes] / platform / sdlv.cpp
1 #include <stdio.h>
2
3 #include <X11/Xlib.h>
4 #include <X11/Xutil.h>
5 #include <SDL.h>
6 #include <SDL_syswm.h>
7
8 #include "snes9x.h"
9 #include "platform.h"
10 #include "display.h"
11 #include "gfx.h"
12 #include "ppu.h"
13 #include "sdlv.h"
14 #include "scaler.h"
15
16 #define DIE(format, ...) do { \
17                 fprintf(stderr, "Died at %s:%d: ", __FILE__, __LINE__ ); \
18                 fprintf(stderr, format "\n", ## __VA_ARGS__); \
19                 abort(); \
20         } while (0);
21
22 struct gui GUI;
23
24 SDL_Surface* screen;
25
26 static SDL_Rect windowSize, screenSize;
27 static bool gotWindowSize, gotScreenSize;
28
29 /** The current scaler object */
30 static Scaler* scaler;
31
32 static void calculateScreenSize()
33 {
34         SDL_SysWMinfo wminfo;
35         SDL_VERSION(&wminfo.version);
36
37         if ( SDL_GetWMInfo(&wminfo) ) {
38                 Display *dpy = wminfo.info.x11.display;
39                 Window w;
40                 SDL_Rect* size;
41                 XWindowAttributes xwa;
42
43                 if (Config.fullscreen) {
44                         w =  wminfo.info.x11.fswindow;
45                         size = &screenSize;
46                         gotScreenSize = true;
47                 } else {
48                         w =  wminfo.info.x11.wmwindow;
49                         size = &windowSize;
50                         gotWindowSize = true;
51                 }
52
53                 XGetWindowAttributes(dpy, w, &xwa);
54                 size->x = xwa.x;
55                 size->y = xwa.y;
56                 size->w = xwa.width;
57                 size->h = xwa.height;
58         }
59 }
60
61 void S9xSetTitle(const char *title)
62 {
63         SDL_SysWMinfo info;
64         SDL_VERSION(&info.version);
65         if ( SDL_GetWMInfo(&info) ) {
66                 Display *dpy = info.info.x11.display;
67                 Window win;
68                 if (dpy) {
69                         win = info.info.x11.fswindow;
70                         if (win) XStoreName(dpy, win, title);
71                         win = info.info.x11.wmwindow;
72                         if (win) XStoreName(dpy, win, title);
73                 }
74         }
75 }
76
77 static void freeVideoSurface()
78 {
79         screen = 0; // There's no need to free the screen surface.
80         GFX.Screen = 0;
81
82         free(GFX.SubScreen); GFX.SubScreen = 0;
83         free(GFX.ZBuffer); GFX.ZBuffer = 0;
84         free(GFX.SubZBuffer); GFX.SubZBuffer = 0;
85
86         delete scaler; scaler = 0;
87 }
88
89 static void setupVideoSurface()
90 {
91         // Real surface area.
92         const unsigned gameWidth = IMAGE_WIDTH;
93         const unsigned gameHeight = IMAGE_HEIGHT;
94
95 #ifdef MAEMO
96         if ((Config.fullscreen && !gotScreenSize) ||
97                 (!Config.fullscreen && !gotWindowSize)) {
98                 // Do a first try, in order to get window/screen size
99                 screen = SDL_SetVideoMode(gameWidth, gameHeight, 16,
100                         SDL_SWSURFACE | SDL_RESIZABLE |
101                         (Config.fullscreen ? SDL_FULLSCREEN : 0));
102                 if (!screen) DIE("SDL_SetVideoMode: %s", SDL_GetError());
103                 calculateScreenSize();
104         }
105         if (Config.fullscreen) {
106                 GUI.Width = screenSize.w;
107                 GUI.Height = screenSize.h;
108         } else {
109                 GUI.Width = windowSize.w;
110                 GUI.Height = windowSize.h;
111         }
112 #else
113         GUI.Width = gameWidth;
114         GUI.Height = gameHeight;
115 #endif
116
117         // Safeguard
118         if (gameHeight > GUI.Height || gameWidth > GUI.Width)
119                 DIE("Video is larger than window size!");
120
121         const ScalerFactory* sFactory =
122                 searchForScaler(Settings.SixteenBit ? 16 : 8, gameWidth, gameHeight);
123
124         screen = SDL_SetVideoMode(GUI.Width, GUI.Height,
125                                                                 Settings.SixteenBit ? 16 : 8,
126                                                                 SDL_SWSURFACE |
127                                                                 (Config.fullscreen ? SDL_FULLSCREEN : 0));
128         if (!screen)
129                 DIE("SDL_SetVideoMode: %s", SDL_GetError());
130         
131         SDL_ShowCursor(SDL_DISABLE);
132
133         scaler = sFactory->instantiate(screen, gameWidth, gameHeight);
134
135         // We get pitch surface values from SDL
136         GFX.RealPitch = GFX.Pitch = scaler->getDrawBufferPitch();
137         GFX.ZPitch = GFX.Pitch / 2; // gfx & tile.cpp depend on this, unfortunately.
138         GFX.PixSize = screen->format->BitsPerPixel / 8;
139         
140         GFX.Screen = scaler->getDrawBuffer();
141         GFX.SubScreen = (uint8 *) malloc(GFX.Pitch * IMAGE_HEIGHT);
142         GFX.ZBuffer =  (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
143         GFX.SubZBuffer = (uint8 *) malloc(GFX.ZPitch * IMAGE_HEIGHT);
144
145         GFX.Delta = (GFX.SubScreen - GFX.Screen) >> 1;
146         GFX.PPL = GFX.Pitch >> 1;
147         GFX.PPLx2 = GFX.Pitch;
148
149         scaler->getRenderedGUIArea(GUI.RenderX, GUI.RenderY, GUI.RenderW, GUI.RenderH);
150         GUI.Scale = scaler->getRatio();
151
152         printf("Video: %dx%d (%dx%d output), %hu bits per pixel, %s, %s\n",
153                 gameWidth, gameHeight,
154                 screen->w, screen->h, screen->format->BitsPerPixel,
155                 Config.fullscreen ? "fullscreen" : "windowed",
156                 scaler->getName());
157 }
158
159 static void drawOnscreenControls()
160 {
161         if (Config.touchscreenInput) {
162                 S9xInputScreenChanged();
163                 if (Config.touchscreenShow) {
164                         scaler->pause();
165                         SDL_FillRect(screen, NULL, 0);
166                         S9xInputScreenDraw(Settings.SixteenBit ? 2 : 1,
167                                                                 screen->pixels, screen->pitch);
168                         SDL_Flip(screen);
169                         scaler->resume();
170                 }
171         }
172 }
173
174 void S9xInitDisplay(int argc, const char ** argv)
175 {       
176         if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) 
177                 DIE("SDL_InitSubSystem(VIDEO): %s", SDL_GetError());
178
179         setupVideoSurface();
180         drawOnscreenControls();
181 }
182
183 void S9xDeinitDisplay()
184 {
185         freeVideoSurface();     
186         SDL_QuitSubSystem(SDL_INIT_VIDEO);
187 }
188
189 void S9xVideoToggleFullscreen()
190 {
191         freeVideoSurface();
192         Config.fullscreen = !Config.fullscreen;
193         setupVideoSurface();
194         drawOnscreenControls();
195 }
196
197 void S9xVideoOutputFocus(bool hasFocus)
198 {
199 #if MAEMO
200         if (scaler) {
201                 if (hasFocus) {
202                         scaler->resume();
203                 } else {
204                         scaler->pause();
205                 }
206         }
207 #endif
208 }
209
210 // This is here for completeness, but palette mode is useless on N8x0
211 void S9xSetPalette ()
212 {
213         if (Settings.SixteenBit) return;
214         
215         SDL_Color colors[256];
216         int brightness = IPPU.MaxBrightness *138;
217         for (int i = 0; i < 256; i++)
218         {
219                 colors[i].r = ((PPU.CGDATA[i] >> 0) & 0x1F) * brightness;
220                 colors[i].g = ((PPU.CGDATA[i] >> 5) & 0x1F) * brightness;
221                 colors[i].b = ((PPU.CGDATA[i] >> 10) & 0x1F) * brightness;
222         }
223         
224         SDL_SetColors(screen, colors, 0, 256);
225 }
226
227 bool8_32 S9xInitUpdate ()
228 {
229         scaler->prepare();
230
231         return TRUE;
232 }
233
234 bool8_32 S9xDeinitUpdate (int width, int height, bool8_32 sixteenBit)
235 {
236         scaler->finish();
237
238         return TRUE;
239 }
240