Debian Codegraph @

src/gui/sdlmain.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 2 — not the installed (fully patched) source.
2 Debian patches touch this file
1 /*
2 * Copyright (C) 2002-2014 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: sdlmain.cpp,v 1.154 2009-06-01 10:25:51 qbix79 Exp $ */
20
21 #ifndef _GNU_SOURCE
22 #define _GNU_SOURCE
23 #endif
24
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <stdarg.h>
30 #include <sys/types.h>
31 #ifdef WIN32
32 #include <signal.h>
33 #include <process.h>
34 #endif
35
36 #include "cross.h"
37 #include "SDL.h"
38
39 #include "dosbox.h"
40 #include "video.h"
41 #include "mouse.h"
42 #include "pic.h"
43 #include "timer.h"
44 #include "setup.h"
45 #include "support.h"
46 #include "debug.h"
47 #include "mapper.h"
48 #include "vga.h"
49 #include "keyboard.h"
50 #include "cpu.h"
51 #include "cross.h"
52 #include "control.h"
53
54 #define MAPPERFILE "mapper-" VERSION ".map"
55 //#define DISABLE_JOYSTICK
56
57 #if C_OPENGL
58 #include "SDL_opengl.h"
59
60 #ifndef APIENTRY
61 #define APIENTRY
62 #endif
63 #ifndef APIENTRYP
64 #define APIENTRYP APIENTRY *
65 #endif
66
67 #ifndef GL_ARB_pixel_buffer_object
68 #define GL_ARB_pixel_buffer_object 1
69 #define GL_PIXEL_PACK_BUFFER_ARB 0x88EB
70 #define GL_PIXEL_UNPACK_BUFFER_ARB 0x88EC
71 #define GL_PIXEL_PACK_BUFFER_BINDING_ARB 0x88ED
72 #define GL_PIXEL_UNPACK_BUFFER_BINDING_ARB 0x88EF
73 #endif
74
75 #ifndef GL_ARB_vertex_buffer_object
76 #define GL_ARB_vertex_buffer_object 1
77 typedef void (APIENTRYP PFNGLGENBUFFERSARBPROC) (GLsizei n, GLuint *buffers);
78 typedef void (APIENTRYP PFNGLBINDBUFFERARBPROC) (GLenum target, GLuint buffer);
79 typedef void (APIENTRYP PFNGLDELETEBUFFERSARBPROC) (GLsizei n, const GLuint *buffers);
80 typedef void (APIENTRYP PFNGLBUFFERDATAARBPROC) (GLenum target, GLsizeiptr size, const GLvoid *data, GLenum usage);
81 typedef GLvoid* (APIENTRYP PFNGLMAPBUFFERARBPROC) (GLenum target, GLenum access);
82 typedef GLboolean (APIENTRYP PFNGLUNMAPBUFFERARBPROC) (GLenum target);
83 #endif
84
85 PFNGLGENBUFFERSARBPROC glGenBuffersARB = NULL;
86 PFNGLBINDBUFFERARBPROC glBindBufferARB = NULL;
87 PFNGLDELETEBUFFERSARBPROC glDeleteBuffersARB = NULL;
88 PFNGLBUFFERDATAARBPROC glBufferDataARB = NULL;
89 PFNGLMAPBUFFERARBPROC glMapBufferARB = NULL;
90 PFNGLUNMAPBUFFERARBPROC glUnmapBufferARB = NULL;
91
92 #endif //C_OPENGL
93
94 #if !(ENVIRON_INCLUDED)
95 extern char** environ;
96 #endif
97
98 #ifdef WIN32
99 #ifndef WIN32_LEAN_AND_MEAN
100 #define WIN32_LEAN_AND_MEAN
101 #endif
102 #include <windows.h>
103 #if (HAVE_DDRAW_H)
104 #include <ddraw.h>
105 struct private_hwdata {
106 LPDIRECTDRAWSURFACE3 dd_surface;
107 LPDIRECTDRAWSURFACE3 dd_writebuf;
108 };
109 #endif
110
111 #define STDOUT_FILE TEXT("stdout.txt")
112 #define STDERR_FILE TEXT("stderr.txt")
113 #define DEFAULT_CONFIG_FILE "/dosbox.conf"
114 #elif defined(MACOSX)
115 #define DEFAULT_CONFIG_FILE "/Library/Preferences/DOSBox Preferences"
116 #else /*linux freebsd*/
117 #define DEFAULT_CONFIG_FILE "/.dosboxrc"
118 #endif
119
120 #if C_SET_PRIORITY
121 #include <sys/resource.h>
122 #define PRIO_TOTAL (PRIO_MAX-PRIO_MIN)
123 #endif
124
125 #ifdef OS2
126 #define INCL_DOS
127 #define INCL_WIN
128 #include <os2.h>
129 #endif
130
131 enum SCREEN_TYPES {
132 SCREEN_SURFACE,
133 SCREEN_SURFACE_DDRAW,
134 SCREEN_OVERLAY,
135 SCREEN_OPENGL
136 };
137
138 enum PRIORITY_LEVELS {
139 PRIORITY_LEVEL_PAUSE,
140 PRIORITY_LEVEL_LOWEST,
141 PRIORITY_LEVEL_LOWER,
142 PRIORITY_LEVEL_NORMAL,
143 PRIORITY_LEVEL_HIGHER,
144 PRIORITY_LEVEL_HIGHEST
145 };
146
147
148 struct SDL_Block {
149 bool inited;
150 bool active; //If this isn't set don't draw
151 bool updating;
152 struct {
153 Bit32u width;
154 Bit32u height;
155 Bit32u bpp;
156 Bitu flags;
157 double scalex,scaley;
158 GFX_CallBack_t callback;
159 } draw;
160 bool wait_on_error;
161 struct {
162 struct {
163 Bit16u width, height;
164 bool fixed;
165 } full;
166 struct {
167 Bit16u width, height;
168 } window;
169 Bit8u bpp;
170 bool fullscreen;
171 bool doublebuf;
172 SCREEN_TYPES type;
173 SCREEN_TYPES want_type;
174 } desktop;
175 #if C_OPENGL
176 struct {
177 Bitu pitch;
178 void * framebuf;
179 GLuint buffer;
180 GLuint texture;
181 GLuint displaylist;
182 GLint max_texsize;
183 bool bilinear;
184 bool packed_pixel;
185 bool paletted_texture;
186 bool pixel_buffer_object;
187 } opengl;
188 #endif
189 struct {
190 SDL_Surface * surface;
191 #if (HAVE_DDRAW_H) && defined(WIN32)
192 RECT rect;
193 #endif
194 } blit;
195 struct {
196 PRIORITY_LEVELS focus;
197 PRIORITY_LEVELS nofocus;
198 } priority;
199 SDL_Rect clip;
200 SDL_Surface * surface;
201 SDL_Overlay * overlay;
202 SDL_cond *cond;
203 struct {
204 bool autolock;
205 bool autoenable;
206 bool requestlock;
207 bool locked;
208 Bitu sensitivity;
209 } mouse;
210 SDL_Rect updateRects[1024];
211 Bitu num_joysticks;
212 #if defined (WIN32)
213 bool using_windib;
214 // Time when sdl regains focus (alt-tab) in windowed mode
215 Bit32u focus_ticks;
216 #endif
217 // state of alt-keys for certain special handlings
218 Bit8u laltstate;
219 Bit8u raltstate;
220 };
221
222 static SDL_Block sdl;
223
224 #define SETMODE_SAVES 1 //Don't set Video Mode if nothing changes.
225 #define SETMODE_SAVES_CLEAR 1 //Clear the screen, when the Video Mode is reused
226 SDL_Surface* SDL_SetVideoMode_Wrap(int width,int height,int bpp,Bit32u flags){
227 #if SETMODE_SAVES
228 static int i_height = 0;
229 static int i_width = 0;
230 static int i_bpp = 0;
231 static Bit32u i_flags = 0;
232 if (sdl.surface != NULL && height == i_height && width == i_width && bpp == i_bpp && flags == i_flags) {
233 // I don't see a difference, so disabled for now, as the code isn't finished either
234 #if SETMODE_SAVES_CLEAR
235 //TODO clear it.
236 #if C_OPENGL
237 if ((flags & SDL_OPENGL)==0)
238 SDL_FillRect(sdl.surface,NULL,SDL_MapRGB(sdl.surface->format,0,0,0));
239 else {
240 glClearColor (0.0, 0.0, 0.0, 1.0);
241 glClear(GL_COLOR_BUFFER_BIT);
242 SDL_GL_SwapBuffers();
243 }
244 #else //C_OPENGL
245 SDL_FillRect(sdl.surface,NULL,SDL_MapRGB(sdl.surface->format,0,0,0));
246 #endif //C_OPENGL
247 #endif //SETMODE_SAVES_CLEAR
248 return sdl.surface;
249 }
250
251
252 #ifdef WIN32
253 //SDL seems to crash if we are in OpenGL mode currently and change to exactly the same size without OpenGL.
254 //This happens when DOSBox is in textmode with aspect=true and output=opengl and the mapper is started.
255 //The easiest solution is to change the size. The mapper doesn't care. (PART PXX)
256
257 //Also we have to switch back to windowed mode first, as else it may crash as well.
258 //Bug: we end up with a locked mouse cursor, but at least that beats crashing. (output=opengl,aspect=true,fullscreen=true)
259 if((i_flags&SDL_OPENGL) && !(flags&SDL_OPENGL) && (i_flags&SDL_FULLSCREEN) && !(flags&SDL_FULLSCREEN)){
260 GFX_SwitchFullScreen();
261 return SDL_SetVideoMode_Wrap(width,height,bpp,flags);
262 }
263
264 //PXX
265 if ((i_flags&SDL_OPENGL) && !(flags&SDL_OPENGL) && height==i_height && width==i_width && height==480) {
266 height++;
267 }
268 #endif //WIN32
269 #endif //SETMODE_SAVES
270 SDL_Surface* s = SDL_SetVideoMode(width,height,bpp,flags);
271 #if SETMODE_SAVES
272 if (s == NULL) return s; //Only store when successful
273 i_height = height;
274 i_width = width;
275 i_bpp = bpp;
276 i_flags = flags;
277 #endif
278 return s;
279 }
280
281 extern const char* RunningProgram;
282 extern bool CPU_CycleAutoAdjust;
283 //Globals for keyboard initialisation
284 bool startup_state_numlock=false;
285 bool startup_state_capslock=false;
286
287 void GFX_SetTitle(Bit32s cycles,Bits frameskip,bool paused){
288 char title[200]={0};
289 static Bit32s internal_cycles=0;
290 static Bits internal_frameskip=0;
291 if(cycles != -1) internal_cycles = cycles;
292 if(frameskip != -1) internal_frameskip = frameskip;
293 if(CPU_CycleAutoAdjust) {
294 sprintf(title,"DOSBox %s, Cpu speed: max %3d%% cycles, Frameskip %2d, Program: %8s",VERSION,internal_cycles,internal_frameskip,RunningProgram);
295 } else {
296 sprintf(title,"DOSBox %s, Cpu speed: %8d cycles, Frameskip %2d, Program: %8s",VERSION,internal_cycles,internal_frameskip,RunningProgram);
297 }
298
299 if(paused) strcat(title," PAUSED");
300 SDL_WM_SetCaption(title,VERSION);
301 }
302
303 static unsigned char logo[32*32*4]= {
304 #include "dosbox_logo.h"
305 };
306 static void GFX_SetIcon() {
307 #if !defined(MACOSX)
308 /* Set Icon (must be done before any sdl_setvideomode call) */
309 /* But don't set it on OS X, as we use a nicer external icon there. */
310 /* Made into a separate call, so it can be called again when we restart the graphics output on win32 */
311 #ifdef WORDS_BIGENDIAN
312 SDL_Surface* logos= SDL_CreateRGBSurfaceFrom((void*)logo,32,32,32,128,0xff000000,0x00ff0000,0x0000ff00,0);
313 #else
314 SDL_Surface* logos= SDL_CreateRGBSurfaceFrom((void*)logo,32,32,32,128,0x000000ff,0x0000ff00,0x00ff0000,0);
315 #endif
316 SDL_WM_SetIcon(logos,NULL);
317 #endif
318 }
319
320
321 static void KillSwitch(bool pressed) {
322 if (!pressed)
323 return;
324 throw 1;
325 }
326
327 static void PauseDOSBox(bool pressed) {
328 if (!pressed)
329 return;
330 GFX_SetTitle(-1,-1,true);
331 bool paused = true;
332 KEYBOARD_ClrBuffer();
333 SDL_Delay(500);
334 SDL_Event event;
335 while (SDL_PollEvent(&event)) {
336 // flush event queue.
337 }
338
339 while (paused) {
340 SDL_WaitEvent(&event); // since we're not polling, cpu usage drops to 0.
341 switch (event.type) {
342
343 case SDL_QUIT: KillSwitch(true); break;
344 case SDL_KEYDOWN: // Must use Pause/Break Key to resume.
345 case SDL_KEYUP:
346 if(event.key.keysym.sym == SDLK_PAUSE) {
347
348 paused = false;
349 GFX_SetTitle(-1,-1,false);
350 break;
351 }
352 #if defined (MACOSX)
353 if (event.key.keysym.sym == SDLK_q && (event.key.keysym.mod == KMOD_RMETA || event.key.keysym.mod == KMOD_LMETA) ) {
354 /* On macs, all aps exit when pressing cmd-q */
355 KillSwitch(true);
356 break;
357 }
358 #endif
359 }
360 }
361 }
362
363 #if defined (WIN32)
364 bool GFX_SDLUsingWinDIB(void) {
365 return sdl.using_windib;
366 }
367 #endif
368
369 /* Reset the screen with current values in the sdl structure */
370 Bitu GFX_GetBestMode(Bitu flags) {
371 Bitu testbpp,gotbpp;
372 switch (sdl.desktop.want_type) {
373 case SCREEN_SURFACE:
374 check_surface:
375 flags &= ~GFX_LOVE_8; //Disable love for 8bpp modes
376 /* Check if we can satisfy the depth it loves */
377 if (flags & GFX_LOVE_8) testbpp=8;
378 else if (flags & GFX_LOVE_15) testbpp=15;
379 else if (flags & GFX_LOVE_16) testbpp=16;
380 else if (flags & GFX_LOVE_32) testbpp=32;
381 else testbpp=0;
382 #if (HAVE_DDRAW_H) && defined(WIN32)
383 check_gotbpp:
384 #endif
385 if (sdl.desktop.fullscreen) gotbpp=SDL_VideoModeOK(640,480,testbpp,SDL_FULLSCREEN|SDL_HWSURFACE|SDL_HWPALETTE);
386 else gotbpp=sdl.desktop.bpp;
387 /* If we can't get our favorite mode check for another working one */
388 switch (gotbpp) {
389 case 8:
390 if (flags & GFX_CAN_8) flags&=~(GFX_CAN_15|GFX_CAN_16|GFX_CAN_32);
391 break;
392 case 15:
393 if (flags & GFX_CAN_15) flags&=~(GFX_CAN_8|GFX_CAN_16|GFX_CAN_32);
394 break;
395 case 16:
396 if (flags & GFX_CAN_16) flags&=~(GFX_CAN_8|GFX_CAN_15|GFX_CAN_32);
397 break;
398 case 24:
399 case 32:
400 if (flags & GFX_CAN_32) flags&=~(GFX_CAN_8|GFX_CAN_15|GFX_CAN_16);
401 break;
402 }
403 flags |= GFX_CAN_RANDOM;
404 break;
405 #if (HAVE_DDRAW_H) && defined(WIN32)
406 case SCREEN_SURFACE_DDRAW:
407 if (!(flags&(GFX_CAN_15|GFX_CAN_16|GFX_CAN_32))) goto check_surface;
408 if (flags & GFX_LOVE_15) testbpp=15;
409 else if (flags & GFX_LOVE_16) testbpp=16;
410 else if (flags & GFX_LOVE_32) testbpp=32;
411 else testbpp=0;
412 flags|=GFX_SCALING;
413 goto check_gotbpp;
414 #endif
415 case SCREEN_OVERLAY:
416 //We only accept 32bit output from the scalers here
417 //Can't handle true color inputs
418 if (flags & GFX_RGBONLY || !(flags&GFX_CAN_32)) goto check_surface;
419 flags|=GFX_SCALING;
420 flags&=~(GFX_CAN_8|GFX_CAN_15|GFX_CAN_16);
421 break;
422 #if C_OPENGL
423 case SCREEN_OPENGL:
424 //We only accept 32bit output from the scalers here
425 if (!(flags&GFX_CAN_32)) goto check_surface;
426 flags|=GFX_SCALING;
427 flags&=~(GFX_CAN_8|GFX_CAN_15|GFX_CAN_16);
428 break;
429 #endif
430 default:
431 goto check_surface;
432 break;
433 }
434 return flags;
435 }
436
437
438 void GFX_ResetScreen(void) {
439 GFX_Stop();
440 if (sdl.draw.callback)
441 (sdl.draw.callback)( GFX_CallBackReset );
442 GFX_Start();
443 CPU_Reset_AutoAdjust();
444 }
445
446 static int int_log2 (int val) {
447 int log = 0;
448 while ((val >>= 1) != 0)
449 log++;
450 return log;
451 }
452
453
454 static SDL_Surface * GFX_SetupSurfaceScaled(Bit32u sdl_flags, Bit32u bpp) {
455 Bit16u fixedWidth;
456 Bit16u fixedHeight;
457
458 if (sdl.desktop.fullscreen) {
459 fixedWidth = sdl.desktop.full.fixed ? sdl.desktop.full.width : 0;
460 fixedHeight = sdl.desktop.full.fixed ? sdl.desktop.full.height : 0;
461 sdl_flags |= SDL_FULLSCREEN|SDL_HWSURFACE;
462 } else {
463 fixedWidth = sdl.desktop.window.width;
464 fixedHeight = sdl.desktop.window.height;
465 sdl_flags |= SDL_HWSURFACE;
466 }
467 if (fixedWidth && fixedHeight) {
468 double ratio_w=(double)fixedWidth/(sdl.draw.width*sdl.draw.scalex);
469 double ratio_h=(double)fixedHeight/(sdl.draw.height*sdl.draw.scaley);
470 if ( ratio_w < ratio_h) {
471 sdl.clip.w=fixedWidth;
472 sdl.clip.h=(Bit16u)(sdl.draw.height*sdl.draw.scaley*ratio_w + 0.1); //possible rounding issues
473 } else {
474 /*
475 * The 0.4 is there to correct for rounding issues.
476 * (partly caused by the rounding issues fix in RENDER_SetSize)
477 */
478 sdl.clip.w=(Bit16u)(sdl.draw.width*sdl.draw.scalex*ratio_h + 0.4);
479 sdl.clip.h=(Bit16u)fixedHeight;
480 }
481 if (sdl.desktop.fullscreen)
482 sdl.surface = SDL_SetVideoMode_Wrap(fixedWidth,fixedHeight,bpp,sdl_flags);
483 else
484 sdl.surface = SDL_SetVideoMode_Wrap(sdl.clip.w,sdl.clip.h,bpp,sdl_flags);
485 if (sdl.surface && sdl.surface->flags & SDL_FULLSCREEN) {
486 sdl.clip.x=(Sint16)((sdl.surface->w-sdl.clip.w)/2);
487 sdl.clip.y=(Sint16)((sdl.surface->h-sdl.clip.h)/2);
488 } else {
489 sdl.clip.x = 0;
490 sdl.clip.y = 0;
491 }
492 return sdl.surface;
493 } else {
494 sdl.clip.x=0;sdl.clip.y=0;
495 sdl.clip.w=(Bit16u)(sdl.draw.width*sdl.draw.scalex);
496 sdl.clip.h=(Bit16u)(sdl.draw.height*sdl.draw.scaley);
497 sdl.surface=SDL_SetVideoMode_Wrap(sdl.clip.w,sdl.clip.h,bpp,sdl_flags);
498 return sdl.surface;
499 }
500 }
501
502 Bitu GFX_SetSize(Bitu width,Bitu height,Bitu flags,double scalex,double scaley,GFX_CallBack_t callback) {
503 if (sdl.updating)
504 GFX_EndUpdate( 0 );
505
506 sdl.draw.width=width;
507 sdl.draw.height=height;
508 sdl.draw.callback=callback;
509 sdl.draw.scalex=scalex;
510 sdl.draw.scaley=scaley;
511
512 Bitu bpp=0;
513 Bitu retFlags = 0;
514
515 if (sdl.blit.surface) {
516 SDL_FreeSurface(sdl.blit.surface);
517 sdl.blit.surface=0;
518 }
519 switch (sdl.desktop.want_type) {
520 case SCREEN_SURFACE:
521 dosurface:
522 if (flags & GFX_CAN_8) bpp=8;
523 if (flags & GFX_CAN_15) bpp=15;
524 if (flags & GFX_CAN_16) bpp=16;
525 if (flags & GFX_CAN_32) bpp=32;
526 sdl.desktop.type=SCREEN_SURFACE;
527 sdl.clip.w=width;
528 sdl.clip.h=height;
529 if (sdl.desktop.fullscreen) {
530 if (sdl.desktop.full.fixed) {
531 sdl.clip.x=(Sint16)((sdl.desktop.full.width-width)/2);
532 sdl.clip.y=(Sint16)((sdl.desktop.full.height-height)/2);
533 sdl.surface=SDL_SetVideoMode_Wrap(sdl.desktop.full.width,sdl.desktop.full.height,bpp,
534 SDL_FULLSCREEN | ((flags & GFX_CAN_RANDOM) ? SDL_SWSURFACE : SDL_HWSURFACE) |
535 (sdl.desktop.doublebuf ? SDL_DOUBLEBUF|SDL_ASYNCBLIT : 0) | SDL_HWPALETTE);
536 if (sdl.surface == NULL) E_Exit("Could not set fullscreen video mode %ix%i-%i: %s",sdl.desktop.full.width,sdl.desktop.full.height,bpp,SDL_GetError());
537 } else {
538 sdl.clip.x=0;sdl.clip.y=0;
539 sdl.surface=SDL_SetVideoMode_Wrap(width,height,bpp,
540 SDL_FULLSCREEN | ((flags & GFX_CAN_RANDOM) ? SDL_SWSURFACE : SDL_HWSURFACE) |
541 (sdl.desktop.doublebuf ? SDL_DOUBLEBUF|SDL_ASYNCBLIT : 0)|SDL_HWPALETTE);
542 if (sdl.surface == NULL)
543 E_Exit("Could not set fullscreen video mode %ix%i-%i: %s",width,height,bpp,SDL_GetError());
544 }
545 } else {
546 sdl.clip.x=0;sdl.clip.y=0;
547 sdl.surface=SDL_SetVideoMode_Wrap(width,height,bpp,(flags & GFX_CAN_RANDOM) ? SDL_SWSURFACE : SDL_HWSURFACE);
548 #ifdef WIN32
549 if (sdl.surface == NULL) {
550 SDL_QuitSubSystem(SDL_INIT_VIDEO);
551 if (!sdl.using_windib) {
552 LOG_MSG("Failed to create hardware surface.\nRestarting video subsystem with windib enabled.");
553 putenv("SDL_VIDEODRIVER=windib");
554 sdl.using_windib=true;
555 } else {
556 LOG_MSG("Failed to create hardware surface.\nRestarting video subsystem with directx enabled.");
557 putenv("SDL_VIDEODRIVER=directx");
558 sdl.using_windib=false;
559 }
560 SDL_InitSubSystem(SDL_INIT_VIDEO);
561 GFX_SetIcon(); //Set Icon again
562 sdl.surface = SDL_SetVideoMode_Wrap(width,height,bpp,SDL_HWSURFACE);
563 if(sdl.surface) GFX_SetTitle(-1,-1,false); //refresh title.
564 }
565 #endif
566 if (sdl.surface == NULL)
567 E_Exit("Could not set windowed video mode %ix%i-%i: %s",width,height,bpp,SDL_GetError());
568 }
569 if (sdl.surface) {
570 switch (sdl.surface->format->BitsPerPixel) {
571 case 8:
572 retFlags = GFX_CAN_8;
573 break;
574 case 15:
575 retFlags = GFX_CAN_15;
576 break;
577 case 16:
578 retFlags = GFX_CAN_16;
579 break;
580 case 32:
581 retFlags = GFX_CAN_32;
582 break;
583 }
584 if (retFlags && (sdl.surface->flags & SDL_HWSURFACE))
585 retFlags |= GFX_HARDWARE;
586 if (retFlags && (sdl.surface->flags & SDL_DOUBLEBUF)) {
587 sdl.blit.surface=SDL_CreateRGBSurface(SDL_HWSURFACE,
588 sdl.draw.width, sdl.draw.height,
589 sdl.surface->format->BitsPerPixel,
590 sdl.surface->format->Rmask,
591 sdl.surface->format->Gmask,
592 sdl.surface->format->Bmask,
593 0);
594 /* If this one fails be ready for some flickering... */
595 }
596 }
597 break;
598 #if (HAVE_DDRAW_H) && defined(WIN32)
599 case SCREEN_SURFACE_DDRAW:
600 if (flags & GFX_CAN_15) bpp=15;
601 if (flags & GFX_CAN_16) bpp=16;
602 if (flags & GFX_CAN_32) bpp=32;
603 if (!GFX_SetupSurfaceScaled((sdl.desktop.doublebuf && sdl.desktop.fullscreen) ? SDL_DOUBLEBUF : 0,bpp)) goto dosurface;
604 sdl.blit.rect.top=sdl.clip.y;
605 sdl.blit.rect.left=sdl.clip.x;
606 sdl.blit.rect.right=sdl.clip.x+sdl.clip.w;
607 sdl.blit.rect.bottom=sdl.clip.y+sdl.clip.h;
608 sdl.blit.surface=SDL_CreateRGBSurface(SDL_HWSURFACE,sdl.draw.width,sdl.draw.height,
609 sdl.surface->format->BitsPerPixel,
610 sdl.surface->format->Rmask,
611 sdl.surface->format->Gmask,
612 sdl.surface->format->Bmask,
613 0);
614 if (!sdl.blit.surface || (!sdl.blit.surface->flags&SDL_HWSURFACE)) {
615 if (sdl.blit.surface) {
616 SDL_FreeSurface(sdl.blit.surface);
617 sdl.blit.surface=0;
618 }
619 LOG_MSG("Failed to create ddraw surface, back to normal surface.");
620 goto dosurface;
621 }
622 switch (sdl.surface->format->BitsPerPixel) {
623 case 15:
624 retFlags = GFX_CAN_15 | GFX_SCALING | GFX_HARDWARE;
625 break;
626 case 16:
627 retFlags = GFX_CAN_16 | GFX_SCALING | GFX_HARDWARE;
628 break;
629 case 32:
630 retFlags = GFX_CAN_32 | GFX_SCALING | GFX_HARDWARE;
631 break;
632 }
633 sdl.desktop.type=SCREEN_SURFACE_DDRAW;
634 break;
635 #endif
636 case SCREEN_OVERLAY:
637 if (sdl.overlay) {
638 SDL_FreeYUVOverlay(sdl.overlay);
639 sdl.overlay=0;
640 }
641 if (!(flags&GFX_CAN_32) || (flags & GFX_RGBONLY)) goto dosurface;
642 if (!GFX_SetupSurfaceScaled(0,0)) goto dosurface;
643 sdl.overlay=SDL_CreateYUVOverlay(width*2,height,SDL_UYVY_OVERLAY,sdl.surface);
644 if (!sdl.overlay) {
645 LOG_MSG("SDL:Failed to create overlay, switching back to surface");
646 goto dosurface;
647 }
648 sdl.desktop.type=SCREEN_OVERLAY;
649 retFlags = GFX_CAN_32 | GFX_SCALING | GFX_HARDWARE;
650 break;
651 #if C_OPENGL
652 case SCREEN_OPENGL:
653 {
654 if (sdl.opengl.pixel_buffer_object) {
655 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, 0);
656 if (sdl.opengl.buffer) glDeleteBuffersARB(1, &sdl.opengl.buffer);
657 } else if (sdl.opengl.framebuf) {
658 free(sdl.opengl.framebuf);
659 }
660 sdl.opengl.framebuf=0;
661 if (!(flags&GFX_CAN_32)) goto dosurface;
662 int texsize=2 << int_log2(width > height ? width : height);
663 if (texsize>sdl.opengl.max_texsize) {
664 LOG_MSG("SDL:OPENGL:No support for texturesize of %d, falling back to surface",texsize);
665 goto dosurface;
666 }
667 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
668 #if SDL_VERSION_ATLEAST(1, 2, 11)
669 SDL_GL_SetAttribute( SDL_GL_SWAP_CONTROL, 0 );
670 #endif
671 GFX_SetupSurfaceScaled(SDL_OPENGL,0);
672 if (!sdl.surface || sdl.surface->format->BitsPerPixel<15) {
673 LOG_MSG("SDL:OPENGL:Can't open drawing surface, are you running in 16bpp(or higher) mode?");
674 goto dosurface;
675 }
676 /* Create the texture and display list */
677 if (sdl.opengl.pixel_buffer_object) {
678 glGenBuffersARB(1, &sdl.opengl.buffer);
679 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, sdl.opengl.buffer);
680 glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_EXT, width*height*4, NULL, GL_STREAM_DRAW_ARB);
681 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, 0);
682 } else {
683 sdl.opengl.framebuf=malloc(width*height*4); //32 bit color
684 }
685 sdl.opengl.pitch=width*4;
686 // glViewport(sdl.clip.x,sdl.clip.y,sdl.clip.w,sdl.clip.h);
687 if(sdl.clip.x ==0 && sdl.clip.y ==0 && sdl.desktop.fullscreen && !sdl.desktop.full.fixed && (sdl.clip.w != sdl.surface->w || sdl.clip.h != sdl.surface->h)) {
688 // LOG_MSG("attempting to fix the centering to %d %d %d %d",(sdl.surface->w-sdl.clip.w)/2,(sdl.surface->h-sdl.clip.h)/2,sdl.clip.w,sdl.clip.h);
689 glViewport((sdl.surface->w-sdl.clip.w)/2,(sdl.surface->h-sdl.clip.h)/2,sdl.clip.w,sdl.clip.h);
690 }
691 else {
692 glViewport(sdl.clip.x,sdl.clip.y,sdl.clip.w,sdl.clip.h);
693 }
694 glMatrixMode (GL_PROJECTION);
695 glDeleteTextures(1,&sdl.opengl.texture);
696 glGenTextures(1,&sdl.opengl.texture);
697 glBindTexture(GL_TEXTURE_2D,sdl.opengl.texture);
698 // No borders
699 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
700 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
701 if (sdl.opengl.bilinear) {
702 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
703 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
704 } else {
705 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
706 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
707 }
708
709 Bit8u* emptytex = new Bit8u[texsize * texsize * 4];
710 memset((void*) emptytex, 0, texsize * texsize * 4);
711 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, texsize, texsize, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, (const GLvoid*)emptytex);
712 delete [] emptytex;
713
714 glClearColor (0.0, 0.0, 0.0, 1.0);
715 glClear(GL_COLOR_BUFFER_BIT);
716 SDL_GL_SwapBuffers();
717 glClear(GL_COLOR_BUFFER_BIT);
718 glShadeModel (GL_FLAT);
719 glDisable (GL_DEPTH_TEST);
720 glDisable (GL_LIGHTING);
721 glDisable(GL_CULL_FACE);
722 glEnable(GL_TEXTURE_2D);
723 glMatrixMode (GL_MODELVIEW);
724 glLoadIdentity ();
725
726 GLfloat tex_width=((GLfloat)(width)/(GLfloat)texsize);
727 GLfloat tex_height=((GLfloat)(height)/(GLfloat)texsize);
728
729 if (glIsList(sdl.opengl.displaylist)) glDeleteLists(sdl.opengl.displaylist, 1);
730 sdl.opengl.displaylist = glGenLists(1);
731 glNewList(sdl.opengl.displaylist, GL_COMPILE);
732 glBindTexture(GL_TEXTURE_2D, sdl.opengl.texture);
733 glBegin(GL_QUADS);
734 // lower left
735 glTexCoord2f(0,tex_height); glVertex2f(-1.0f,-1.0f);
736 // lower right
737 glTexCoord2f(tex_width,tex_height); glVertex2f(1.0f, -1.0f);
738 // upper right
739 glTexCoord2f(tex_width,0); glVertex2f(1.0f, 1.0f);
740 // upper left
741 glTexCoord2f(0,0); glVertex2f(-1.0f, 1.0f);
742 glEnd();
743 glEndList();
744 sdl.desktop.type=SCREEN_OPENGL;
745 retFlags = GFX_CAN_32 | GFX_SCALING;
746 if (sdl.opengl.pixel_buffer_object)
747 retFlags |= GFX_HARDWARE;
748 break;
749 }//OPENGL
750 #endif //C_OPENGL
751 default:
752 goto dosurface;
753 break;
754 }//CASE
755 if (retFlags)
756 GFX_Start();
757 if (!sdl.mouse.autoenable) SDL_ShowCursor(sdl.mouse.autolock?SDL_DISABLE:SDL_ENABLE);
758 return retFlags;
759 }
760
761 void GFX_CaptureMouse(void) {
762 sdl.mouse.locked=!sdl.mouse.locked;
763 if (sdl.mouse.locked) {
764 SDL_WM_GrabInput(SDL_GRAB_ON);
765 SDL_ShowCursor(SDL_DISABLE);
766 } else {
767 SDL_WM_GrabInput(SDL_GRAB_OFF);
768 if (sdl.mouse.autoenable || !sdl.mouse.autolock) SDL_ShowCursor(SDL_ENABLE);
769 }
770 mouselocked=sdl.mouse.locked;
771 }
772
773 bool mouselocked; //Global variable for mapper
774 static void CaptureMouse(bool pressed) {
775 if (!pressed)
776 return;
777 GFX_CaptureMouse();
778 }
779
780 void GFX_SwitchFullScreen(void) {
781 sdl.desktop.fullscreen=!sdl.desktop.fullscreen;
782 if (sdl.desktop.fullscreen) {
783 if (!sdl.mouse.locked) GFX_CaptureMouse();
784 } else {
785 if (sdl.mouse.locked) GFX_CaptureMouse();
786 }
787 GFX_ResetScreen();
788 }
789
790 static void SwitchFullScreen(bool pressed) {
791 if (!pressed)
792 return;
793 GFX_SwitchFullScreen();
794 }
795
796
797 bool GFX_StartUpdate(Bit8u * & pixels,Bitu & pitch) {
798 if (!sdl.active || sdl.updating)
799 return false;
800 switch (sdl.desktop.type) {
801 case SCREEN_SURFACE:
802 if (sdl.blit.surface) {
803 if (SDL_MUSTLOCK(sdl.blit.surface) && SDL_LockSurface(sdl.blit.surface))
804 return false;
805 pixels=(Bit8u *)sdl.blit.surface->pixels;
806 pitch=sdl.blit.surface->pitch;
807 } else {
808 if (SDL_MUSTLOCK(sdl.surface) && SDL_LockSurface(sdl.surface))
809 return false;
810 pixels=(Bit8u *)sdl.surface->pixels;
811 pixels+=sdl.clip.y*sdl.surface->pitch;
812 pixels+=sdl.clip.x*sdl.surface->format->BytesPerPixel;
813 pitch=sdl.surface->pitch;
814 }
815 sdl.updating=true;
816 return true;
817 #if (HAVE_DDRAW_H) && defined(WIN32)
818 case SCREEN_SURFACE_DDRAW:
819 if (SDL_LockSurface(sdl.blit.surface)) {
820 // LOG_MSG("SDL Lock failed");
821 return false;
822 }
823 pixels=(Bit8u *)sdl.blit.surface->pixels;
824 pitch=sdl.blit.surface->pitch;
825 sdl.updating=true;
826 return true;
827 #endif
828 case SCREEN_OVERLAY:
829 if (SDL_LockYUVOverlay(sdl.overlay)) return false;
830 pixels=(Bit8u *)*(sdl.overlay->pixels);
831 pitch=*(sdl.overlay->pitches);
832 sdl.updating=true;
833 return true;
834 #if C_OPENGL
835 case SCREEN_OPENGL:
836 if(sdl.opengl.pixel_buffer_object) {
837 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, sdl.opengl.buffer);
838 pixels=(Bit8u *)glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, GL_WRITE_ONLY);
839 } else
840 pixels=(Bit8u *)sdl.opengl.framebuf;
841 pitch=sdl.opengl.pitch;
842 sdl.updating=true;
843 return true;
844 #endif
845 default:
846 break;
847 }
848 return false;
849 }
850
851
852 void GFX_EndUpdate( const Bit16u *changedLines ) {
853 #if (HAVE_DDRAW_H) && defined(WIN32)
854 int ret;
855 #endif
856 if (!sdl.updating)
857 return;
858 sdl.updating=false;
859 switch (sdl.desktop.type) {
860 case SCREEN_SURFACE:
861 if (SDL_MUSTLOCK(sdl.surface)) {
862 if (sdl.blit.surface) {
863 SDL_UnlockSurface(sdl.blit.surface);
864 int Blit = SDL_BlitSurface( sdl.blit.surface, 0, sdl.surface, &sdl.clip );
865 LOG(LOG_MISC,LOG_WARN)("BlitSurface returned %d",Blit);
866 } else {
867 SDL_UnlockSurface(sdl.surface);
868 }
869 SDL_Flip(sdl.surface);
870 } else if (changedLines) {
871 Bitu y = 0, index = 0, rectCount = 0;
872 while (y < sdl.draw.height) {
873 if (!(index & 1)) {
874 y += changedLines[index];
875 } else {
876 SDL_Rect *rect = &sdl.updateRects[rectCount++];
877 rect->x = sdl.clip.x;
878 rect->y = sdl.clip.y + y;
879 rect->w = (Bit16u)sdl.draw.width;
880 rect->h = changedLines[index];
881 #if 0
882 if (rect->h + rect->y > sdl.surface->h) {
883 LOG_MSG("WTF %d + %d >%d",rect->h,rect->y,sdl.surface->h);
884 }
885 #endif
886 y += changedLines[index];
887 }
888 index++;
889 }
890 if (rectCount)
891 SDL_UpdateRects( sdl.surface, rectCount, sdl.updateRects );
892 }
893 break;
894 #if (HAVE_DDRAW_H) && defined(WIN32)
895 case SCREEN_SURFACE_DDRAW:
896 SDL_UnlockSurface(sdl.blit.surface);
897 ret=IDirectDrawSurface3_Blt(
898 sdl.surface->hwdata->dd_writebuf,&sdl.blit.rect,
899 sdl.blit.surface->hwdata->dd_surface,0,
900 DDBLT_WAIT, NULL);
901 switch (ret) {
902 case DD_OK:
903 break;
904 case DDERR_SURFACELOST:
905 IDirectDrawSurface3_Restore(sdl.blit.surface->hwdata->dd_surface);
906 IDirectDrawSurface3_Restore(sdl.surface->hwdata->dd_surface);
907 break;
908 default:
909 LOG_MSG("DDRAW:Failed to blit, error %X",ret);
910 }
911 SDL_Flip(sdl.surface);
912 break;
913 #endif
914 case SCREEN_OVERLAY:
915 SDL_UnlockYUVOverlay(sdl.overlay);
916 SDL_DisplayYUVOverlay(sdl.overlay,&sdl.clip);
917 break;
918 #if C_OPENGL
919 case SCREEN_OPENGL:
920 // Clear drawing area. Some drivers (on Linux) have more than 2 buffers and the screen might
921 // be dirty because of other programs.
922 glClearColor (0.0, 0.0, 0.0, 1.0);
923 glClear(GL_COLOR_BUFFER_BIT);
924 if (sdl.opengl.pixel_buffer_object) {
925 glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT);
926 glBindTexture(GL_TEXTURE_2D, sdl.opengl.texture);
927 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0,
928 sdl.draw.width, sdl.draw.height, GL_BGRA_EXT,
929 GL_UNSIGNED_INT_8_8_8_8_REV, 0);
930 glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_EXT, 0);
931 glCallList(sdl.opengl.displaylist);
932 SDL_GL_SwapBuffers();
933 } else if (changedLines) {
934 Bitu y = 0, index = 0;
935 glBindTexture(GL_TEXTURE_2D, sdl.opengl.texture);
936 while (y < sdl.draw.height) {
937 if (!(index & 1)) {
938 y += changedLines[index];
939 } else {
940 Bit8u *pixels = (Bit8u *)sdl.opengl.framebuf + y * sdl.opengl.pitch;
941 Bitu height = changedLines[index];
942 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, y,
943 sdl.draw.width, height, GL_BGRA_EXT,
944 GL_UNSIGNED_INT_8_8_8_8_REV, pixels );
945 y += height;
946 }
947 index++;
948 }
949 glCallList(sdl.opengl.displaylist);
950 SDL_GL_SwapBuffers();
951 }
952 break;
953 #endif
954 default:
955 break;
956 }
957 }
958
959
960 void GFX_SetPalette(Bitu start,Bitu count,GFX_PalEntry * entries) {
961 /* I should probably not change the GFX_PalEntry :) */
962 if (sdl.surface->flags & SDL_HWPALETTE) {
963 if (!SDL_SetPalette(sdl.surface,SDL_PHYSPAL,(SDL_Color *)entries,start,count)) {
964 E_Exit("SDL:Can't set palette");
965 }
966 } else {
967 if (!SDL_SetPalette(sdl.surface,SDL_LOGPAL,(SDL_Color *)entries,start,count)) {
968 E_Exit("SDL:Can't set palette");
969 }
970 }
971 }
972
973 Bitu GFX_GetRGB(Bit8u red,Bit8u green,Bit8u blue) {
974 switch (sdl.desktop.type) {
975 case SCREEN_SURFACE:
976 case SCREEN_SURFACE_DDRAW:
977 return SDL_MapRGB(sdl.surface->format,red,green,blue);
978 case SCREEN_OVERLAY:
979 {
980 Bit8u y = ( 9797*(red) + 19237*(green) + 3734*(blue) ) >> 15;
981 Bit8u u = (18492*((blue)-(y)) >> 15) + 128;
982 Bit8u v = (23372*((red)-(y)) >> 15) + 128;
983 #ifdef WORDS_BIGENDIAN
984 return (y << 0) | (v << 8) | (y << 16) | (u << 24);
985 #else
986 return (u << 0) | (y << 8) | (v << 16) | (y << 24);
987 #endif
988 }
989 case SCREEN_OPENGL:
990 // return ((red << 0) | (green << 8) | (blue << 16)) | (255 << 24);
991 //USE BGRA
992 return ((blue << 0) | (green << 8) | (red << 16)) | (255 << 24);
993 }
994 return 0;
995 }
996
997 void GFX_Stop() {
998 if (sdl.updating)
999 GFX_EndUpdate( 0 );
1000 sdl.active=false;
1001 }
1002
1003 void GFX_Start() {
1004 sdl.active=true;
1005 }
1006
1007 static void GUI_ShutDown(Section * /*sec*/) {
1008 GFX_Stop();
1009 if (sdl.draw.callback) (sdl.draw.callback)( GFX_CallBackStop );
1010 if (sdl.mouse.locked) GFX_CaptureMouse();
1011 if (sdl.desktop.fullscreen) GFX_SwitchFullScreen();
1012 }
1013
1014
1015 static void SetPriority(PRIORITY_LEVELS level) {
1016
1017 #if C_SET_PRIORITY
1018 // Do nothing if priorties are not the same and not root, else the highest
1019 // priority can not be set as users can only lower priority (not restore it)
1020
1021 if((sdl.priority.focus != sdl.priority.nofocus ) &&
1022 (getuid()!=0) ) return;
1023
1024 #endif
1025 switch (level) {
1026 #ifdef WIN32
1027 case PRIORITY_LEVEL_PAUSE: // if DOSBox is paused, assume idle priority
1028 case PRIORITY_LEVEL_LOWEST:
1029 SetPriorityClass(GetCurrentProcess(),IDLE_PRIORITY_CLASS);
1030 break;
1031 case PRIORITY_LEVEL_LOWER:
1032 SetPriorityClass(GetCurrentProcess(),BELOW_NORMAL_PRIORITY_CLASS);
1033 break;
1034 case PRIORITY_LEVEL_NORMAL:
1035 SetPriorityClass(GetCurrentProcess(),NORMAL_PRIORITY_CLASS);
1036 break;
1037 case PRIORITY_LEVEL_HIGHER:
1038 SetPriorityClass(GetCurrentProcess(),ABOVE_NORMAL_PRIORITY_CLASS);
1039 break;
1040 case PRIORITY_LEVEL_HIGHEST:
1041 SetPriorityClass(GetCurrentProcess(),HIGH_PRIORITY_CLASS);
1042 break;
1043 #elif C_SET_PRIORITY
1044 /* Linux use group as dosbox has mulitple threads under linux */
1045 case PRIORITY_LEVEL_PAUSE: // if DOSBox is paused, assume idle priority
1046 case PRIORITY_LEVEL_LOWEST:
1047 setpriority (PRIO_PGRP, 0,PRIO_MAX);
1048 break;
1049 case PRIORITY_LEVEL_LOWER:
1050 setpriority (PRIO_PGRP, 0,PRIO_MAX-(PRIO_TOTAL/3));
1051 break;
1052 case PRIORITY_LEVEL_NORMAL:
1053 setpriority (PRIO_PGRP, 0,PRIO_MAX-(PRIO_TOTAL/2));
1054 break;
1055 case PRIORITY_LEVEL_HIGHER:
1056 setpriority (PRIO_PGRP, 0,PRIO_MAX-((3*PRIO_TOTAL)/5) );
1057 break;
1058 case PRIORITY_LEVEL_HIGHEST:
1059 setpriority (PRIO_PGRP, 0,PRIO_MAX-((3*PRIO_TOTAL)/4) );
1060 break;
1061 #endif
1062 default:
1063 break;
1064 }
1065 }
1066
1067 extern Bit8u int10_font_14[256 * 14];
1068 static void OutputString(Bitu x,Bitu y,const char * text,Bit32u color,Bit32u color2,SDL_Surface * output_surface) {
1069 Bit32u * draw=(Bit32u*)(((Bit8u *)output_surface->pixels)+((y)*output_surface->pitch))+x;
1070 while (*text) {
1071 Bit8u * font=&int10_font_14[(*text)*14];
1072 Bitu i,j;
1073 Bit32u * draw_line=draw;
1074 for (i=0;i<14;i++) {
1075 Bit8u map=*font++;
1076 for (j=0;j<8;j++) {
1077 if (map & 0x80) *((Bit32u*)(draw_line+j))=color; else *((Bit32u*)(draw_line+j))=color2;
1078 map<<=1;
1079 }
1080 draw_line+=output_surface->pitch/4;
1081 }
1082 text++;
1083 draw+=8;
1084 }
1085 }
1086
1087 #include "dosbox_splash.h"
1088
1089 //extern void UI_Run(bool);
1090 static void GUI_StartUp(Section * sec) {
1091 sec->AddDestroyFunction(&GUI_ShutDown);
1092 Section_prop * section=static_cast<Section_prop *>(sec);
1093 sdl.active=false;
1094 sdl.updating=false;
1095
1096 GFX_SetIcon();
1097
1098 sdl.desktop.fullscreen=section->Get_bool("fullscreen");
1099 sdl.wait_on_error=section->Get_bool("waitonerror");
1100
1101 Prop_multival* p=section->Get_multival("priority");
1102 std::string focus = p->GetSection()->Get_string("active");
1103 std::string notfocus = p->GetSection()->Get_string("inactive");
1104
1105 if (focus == "lowest") { sdl.priority.focus = PRIORITY_LEVEL_LOWEST; }
1106 else if (focus == "lower") { sdl.priority.focus = PRIORITY_LEVEL_LOWER; }
1107 else if (focus == "normal") { sdl.priority.focus = PRIORITY_LEVEL_NORMAL; }
1108 else if (focus == "higher") { sdl.priority.focus = PRIORITY_LEVEL_HIGHER; }
1109 else if (focus == "highest") { sdl.priority.focus = PRIORITY_LEVEL_HIGHEST; }
1110
1111 if (notfocus == "lowest") { sdl.priority.nofocus=PRIORITY_LEVEL_LOWEST; }
1112 else if (notfocus == "lower") { sdl.priority.nofocus=PRIORITY_LEVEL_LOWER; }
1113 else if (notfocus == "normal") { sdl.priority.nofocus=PRIORITY_LEVEL_NORMAL; }
1114 else if (notfocus == "higher") { sdl.priority.nofocus=PRIORITY_LEVEL_HIGHER; }
1115 else if (notfocus == "highest") { sdl.priority.nofocus=PRIORITY_LEVEL_HIGHEST; }
1116 else if (notfocus == "pause") {
1117 /* we only check for pause here, because it makes no sense
1118 * for DOSBox to be paused while it has focus
1119 */
1120 sdl.priority.nofocus=PRIORITY_LEVEL_PAUSE;
1121 }
1122
1123 SetPriority(sdl.priority.focus); //Assume focus on startup
1124 sdl.mouse.locked=false;
1125 mouselocked=false; //Global for mapper
1126 sdl.mouse.requestlock=false;
1127 sdl.desktop.full.fixed=false;
1128 const char* fullresolution=section->Get_string("fullresolution");
1129 sdl.desktop.full.width = 0;
1130 sdl.desktop.full.height = 0;
1131 if(fullresolution && *fullresolution) {
1132 char res[100];
1133 safe_strncpy( res, fullresolution, sizeof( res ));
1134 fullresolution = lowcase (res);//so x and X are allowed
1135 if (strcmp(fullresolution,"original")) {
1136 sdl.desktop.full.fixed = true;
1137 if (strcmp(fullresolution,"desktop")) { //desktop = 0x0
1138 char* height = const_cast<char*>(strchr(fullresolution,'x'));
1139 if (height && * height) {
1140 *height = 0;
1141 sdl.desktop.full.height = (Bit16u)atoi(height+1);
1142 sdl.desktop.full.width = (Bit16u)atoi(res);
1143 }
1144 }
1145 }
1146 }
1147
1148 sdl.desktop.window.width = 0;
1149 sdl.desktop.window.height = 0;
1150 const char* windowresolution=section->Get_string("windowresolution");
1151 if(windowresolution && *windowresolution) {
1152 char res[100];
1153 safe_strncpy( res,windowresolution, sizeof( res ));
1154 windowresolution = lowcase (res);//so x and X are allowed
1155 if(strcmp(windowresolution,"original")) {
1156 char* height = const_cast<char*>(strchr(windowresolution,'x'));
1157 if(height && *height) {
1158 *height = 0;
1159 sdl.desktop.window.height = (Bit16u)atoi(height+1);
1160 sdl.desktop.window.width = (Bit16u)atoi(res);
1161 }
1162 }
1163 }
1164 sdl.desktop.doublebuf=section->Get_bool("fulldouble");
1165 #if SDL_VERSION_ATLEAST(1, 2, 10)
1166 #ifdef WIN32
1167 const SDL_VideoInfo* vidinfo = SDL_GetVideoInfo();
1168 if (vidinfo) {
1169 int sdl_w = vidinfo->current_w;
1170 int sdl_h = vidinfo->current_h;
1171 int win_w = GetSystemMetrics(SM_CXSCREEN);
1172 int win_h = GetSystemMetrics(SM_CYSCREEN);
1173 if (sdl_w != win_w && sdl_h != win_h)
1174 LOG_MSG("\nWindows dpi/blurry apps scaling detected! The screen might be too large or not\n"
1175 "show properly, please see the DOSBox options file (fullresolution) for details.\n");
1176 }
1177 #else
1178 if (!sdl.desktop.full.width || !sdl.desktop.full.height){
1179 //Can only be done on the very first call! Not restartable.
1180 //On windows don't use it as SDL returns the values without taking in account the dpi scaling
1181 const SDL_VideoInfo* vidinfo = SDL_GetVideoInfo();
1182 if (vidinfo) {
1183 sdl.desktop.full.width = vidinfo->current_w;
1184 sdl.desktop.full.height = vidinfo->current_h;
1185 }
1186 }
1187 #endif
1188 #endif
1189
1190 if (!sdl.desktop.full.width) {
1191 #ifdef WIN32
1192 sdl.desktop.full.width=(Bit16u)GetSystemMetrics(SM_CXSCREEN);
1193 #else
1194 LOG_MSG("Your fullscreen resolution can NOT be determined, it's assumed to be 1024x768.\nPlease edit the configuration file if this value is wrong.");
1195 sdl.desktop.full.width=1024;
1196 #endif
1197 }
1198 if (!sdl.desktop.full.height) {
1199 #ifdef WIN32
1200 sdl.desktop.full.height=(Bit16u)GetSystemMetrics(SM_CYSCREEN);
1201 #else
1202 sdl.desktop.full.height=768;
1203 #endif
1204 }
1205 sdl.mouse.autoenable=section->Get_bool("autolock");
1206 if (!sdl.mouse.autoenable) SDL_ShowCursor(SDL_DISABLE);
1207 sdl.mouse.autolock=false;
1208 sdl.mouse.sensitivity=section->Get_int("sensitivity");
1209 std::string output=section->Get_string("output");
1210
1211 /* Setup Mouse correctly if fullscreen */
1212 if(sdl.desktop.fullscreen) GFX_CaptureMouse();
1213
1214 if (output == "surface") {
1215 sdl.desktop.want_type=SCREEN_SURFACE;
1216 #if (HAVE_DDRAW_H) && defined(WIN32)
1217 } else if (output == "ddraw") {
1218 sdl.desktop.want_type=SCREEN_SURFACE_DDRAW;
1219 #endif
1220 } else if (output == "overlay") {
1221 sdl.desktop.want_type=SCREEN_OVERLAY;
1222 #if C_OPENGL
1223 } else if (output == "opengl") {
1224 sdl.desktop.want_type=SCREEN_OPENGL;
1225 sdl.opengl.bilinear=true;
1226 } else if (output == "openglnb") {
1227 sdl.desktop.want_type=SCREEN_OPENGL;
1228 sdl.opengl.bilinear=false;
1229 #endif
1230 } else {
1231 LOG_MSG("SDL:Unsupported output device %s, switching back to surface",output.c_str());
1232 sdl.desktop.want_type=SCREEN_SURFACE;//SHOULDN'T BE POSSIBLE anymore
1233 }
1234
1235 sdl.overlay=0;
1236 #if C_OPENGL
1237 if(sdl.desktop.want_type==SCREEN_OPENGL){ /* OPENGL is requested */
1238 sdl.surface=SDL_SetVideoMode_Wrap(640,400,0,SDL_OPENGL);
1239 if (sdl.surface == NULL) {
1240 LOG_MSG("Could not initialize OpenGL, switching back to surface");
1241 sdl.desktop.want_type=SCREEN_SURFACE;
1242 } else {
1243 sdl.opengl.buffer=0;
1244 sdl.opengl.framebuf=0;
1245 sdl.opengl.texture=0;
1246 sdl.opengl.displaylist=0;
1247 glGetIntegerv (GL_MAX_TEXTURE_SIZE, &sdl.opengl.max_texsize);
1248 glGenBuffersARB = (PFNGLGENBUFFERSARBPROC)SDL_GL_GetProcAddress("glGenBuffersARB");
1249 glBindBufferARB = (PFNGLBINDBUFFERARBPROC)SDL_GL_GetProcAddress("glBindBufferARB");
1250 glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)SDL_GL_GetProcAddress("glDeleteBuffersARB");
1251 glBufferDataARB = (PFNGLBUFFERDATAARBPROC)SDL_GL_GetProcAddress("glBufferDataARB");
1252 glMapBufferARB = (PFNGLMAPBUFFERARBPROC)SDL_GL_GetProcAddress("glMapBufferARB");
1253 glUnmapBufferARB = (PFNGLUNMAPBUFFERARBPROC)SDL_GL_GetProcAddress("glUnmapBufferARB");
1254 const char * gl_ext = (const char *)glGetString (GL_EXTENSIONS);
1255 if(gl_ext && *gl_ext){
1256 sdl.opengl.packed_pixel=(strstr(gl_ext,"EXT_packed_pixels") != NULL);
1257 sdl.opengl.paletted_texture=(strstr(gl_ext,"EXT_paletted_texture") != NULL);
1258 sdl.opengl.pixel_buffer_object=(strstr(gl_ext,"GL_ARB_pixel_buffer_object") != NULL ) &&
1259 glGenBuffersARB && glBindBufferARB && glDeleteBuffersARB && glBufferDataARB &&
1260 glMapBufferARB && glUnmapBufferARB;
1261 } else {
1262 sdl.opengl.packed_pixel=sdl.opengl.paletted_texture=false;
1263 }
1264 }
1265 } /* OPENGL is requested end */
1266
1267 #endif //OPENGL
1268 /* Initialize screen for first time */
1269 sdl.surface=SDL_SetVideoMode_Wrap(640,400,0,0);
1270 if (sdl.surface == NULL) E_Exit("Could not initialize video: %s",SDL_GetError());
1271 sdl.desktop.bpp=sdl.surface->format->BitsPerPixel;
1272 if (sdl.desktop.bpp==24) {
1273 LOG_MSG("SDL:You are running in 24 bpp mode, this will slow down things!");
1274 }
1275 GFX_Stop();
1276 SDL_WM_SetCaption("DOSBox",VERSION);
1277
1278 /* The endian part is intentionally disabled as somehow it produces correct results without according to rhoenie*/
1279 //#if SDL_BYTEORDER == SDL_BIG_ENDIAN
1280 // Bit32u rmask = 0xff000000;
1281 // Bit32u gmask = 0x00ff0000;
1282 // Bit32u bmask = 0x0000ff00;
1283 //#else
1284 Bit32u rmask = 0x000000ff;
1285 Bit32u gmask = 0x0000ff00;
1286 Bit32u bmask = 0x00ff0000;
1287 //#endif
1288
1289 /* Please leave the Splash screen stuff in working order in DOSBox. We spend a lot of time making DOSBox. */
1290 SDL_Surface* splash_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, 640, 400, 32, rmask, gmask, bmask, 0);
1291 if (splash_surf) {
1292 SDL_FillRect(splash_surf, NULL, SDL_MapRGB(splash_surf->format, 0, 0, 0));
1293
1294 Bit8u* tmpbufp = new Bit8u[640*400*3];
1295 GIMP_IMAGE_RUN_LENGTH_DECODE(tmpbufp,gimp_image.rle_pixel_data,640*400,3);
1296 for (Bitu y=0; y<400; y++) {
1297
1298 Bit8u* tmpbuf = tmpbufp + y*640*3;
1299 Bit32u * draw=(Bit32u*)(((Bit8u *)splash_surf->pixels)+((y)*splash_surf->pitch));
1300 for (Bitu x=0; x<640; x++) {
1301 //#if SDL_BYTEORDER == SDL_BIG_ENDIAN
1302 // *draw++ = tmpbuf[x*3+2]+tmpbuf[x*3+1]*0x100+tmpbuf[x*3+0]*0x10000+0x00000000;
1303 //#else
1304 *draw++ = tmpbuf[x*3+0]+tmpbuf[x*3+1]*0x100+tmpbuf[x*3+2]*0x10000+0x00000000;
1305 //#endif
1306 }
1307 }
1308
1309 bool exit_splash = false;
1310
1311 static Bitu max_splash_loop = 600;
1312 static Bitu splash_fade = 100;
1313 static bool use_fadeout = true;
1314
1315 for (Bit32u ct = 0,startticks = GetTicks();ct < max_splash_loop;ct = GetTicks()-startticks) {
1316 SDL_Event evt;
1317 while (SDL_PollEvent(&evt)) {
1318 if (evt.type == SDL_QUIT) {
1319 exit_splash = true;
1320 break;
1321 }
1322 }
1323 if (exit_splash) break;
1324
1325 if (ct<1) {
1326 SDL_FillRect(sdl.surface, NULL, SDL_MapRGB(sdl.surface->format, 0, 0, 0));
1327 SDL_SetAlpha(splash_surf, SDL_SRCALPHA,255);
1328 SDL_BlitSurface(splash_surf, NULL, sdl.surface, NULL);
1329 SDL_Flip(sdl.surface);
1330 } else if (ct>=max_splash_loop-splash_fade) {
1331 if (use_fadeout) {
1332 SDL_FillRect(sdl.surface, NULL, SDL_MapRGB(sdl.surface->format, 0, 0, 0));
1333 SDL_SetAlpha(splash_surf, SDL_SRCALPHA, (Bit8u)((max_splash_loop-1-ct)*255/(splash_fade-1)));
1334 SDL_BlitSurface(splash_surf, NULL, sdl.surface, NULL);
1335 SDL_Flip(sdl.surface);
1336 }
1337 }
1338
1339 }
1340
1341 if (use_fadeout) {
1342 SDL_FillRect(sdl.surface, NULL, SDL_MapRGB(sdl.surface->format, 0, 0, 0));
1343 SDL_Flip(sdl.surface);
1344 }
1345 SDL_FreeSurface(splash_surf);
1346 delete [] tmpbufp;
1347
1348 }
1349
1350 /* Get some Event handlers */
1351 MAPPER_AddHandler(KillSwitch,MK_f9,MMOD1,"shutdown","ShutDown");
1352 MAPPER_AddHandler(CaptureMouse,MK_f10,MMOD1,"capmouse","Cap Mouse");
1353 MAPPER_AddHandler(SwitchFullScreen,MK_return,MMOD2,"fullscr","Fullscreen");
1354 #if C_DEBUG
1355 /* Pause binds with activate-debugger */
1356 #else
1357 MAPPER_AddHandler(&PauseDOSBox, MK_pause, MMOD2, "pause", "Pause");
1358 #endif
1359 /* Get Keyboard state of numlock and capslock */
1360 SDLMod keystate = SDL_GetModState();
1361 if(keystate&KMOD_NUM) startup_state_numlock = true;
1362 if(keystate&KMOD_CAPS) startup_state_capslock = true;
1363 }
1364
1365 void Mouse_AutoLock(bool enable) {
1366 sdl.mouse.autolock=enable;
1367 if (sdl.mouse.autoenable) sdl.mouse.requestlock=enable;
1368 else {
1369 SDL_ShowCursor(enable?SDL_DISABLE:SDL_ENABLE);
1370 sdl.mouse.requestlock=false;
1371 }
1372 }
1373
1374 static void HandleMouseMotion(SDL_MouseMotionEvent * motion) {
1375 if (sdl.mouse.locked || !sdl.mouse.autoenable)
1376 Mouse_CursorMoved((float)motion->xrel*sdl.mouse.sensitivity/100.0f,
1377 (float)motion->yrel*sdl.mouse.sensitivity/100.0f,
1378 (float)(motion->x-sdl.clip.x)/(sdl.clip.w-1)*sdl.mouse.sensitivity/100.0f,
1379 (float)(motion->y-sdl.clip.y)/(sdl.clip.h-1)*sdl.mouse.sensitivity/100.0f,
1380 sdl.mouse.locked);
1381 }
1382
1383 static void HandleMouseButton(SDL_MouseButtonEvent * button) {
1384 switch (button->state) {
1385 case SDL_PRESSED:
1386 if (sdl.mouse.requestlock && !sdl.mouse.locked) {
1387 GFX_CaptureMouse();
1388 // Dont pass klick to mouse handler
1389 break;
1390 }
1391 if (!sdl.mouse.autoenable && sdl.mouse.autolock && button->button == SDL_BUTTON_MIDDLE) {
1392 GFX_CaptureMouse();
1393 break;
1394 }
1395 switch (button->button) {
1396 case SDL_BUTTON_LEFT:
1397 Mouse_ButtonPressed(0);
1398 break;
1399 case SDL_BUTTON_RIGHT:
1400 Mouse_ButtonPressed(1);
1401 break;
1402 case SDL_BUTTON_MIDDLE:
1403 Mouse_ButtonPressed(2);
1404 break;
1405 }
1406 break;
1407 case SDL_RELEASED:
1408 switch (button->button) {
1409 case SDL_BUTTON_LEFT:
1410 Mouse_ButtonReleased(0);
1411 break;
1412 case SDL_BUTTON_RIGHT:
1413 Mouse_ButtonReleased(1);
1414 break;
1415 case SDL_BUTTON_MIDDLE:
1416 Mouse_ButtonReleased(2);
1417 break;
1418 }
1419 break;
1420 }
1421 }
1422
1423 void GFX_LosingFocus(void) {
1424 sdl.laltstate=SDL_KEYUP;
1425 sdl.raltstate=SDL_KEYUP;
1426 MAPPER_LosingFocus();
1427 }
1428
1429 #if defined(MACOSX)
1430 #define DB_POLLSKIP 3
1431 #else
1432 //Not used yet, see comment below
1433 #define DB_POLLSKIP 1
1434 #endif
1435
1436 #if defined(LINUX)
1437 #define SDL_XORG_FIX 1
1438 #else
1439 #define SDL_XORG_FIX 0
1440 #endif
1441
1442 void GFX_Events() {
1443 //Don't poll too often. This can be heavy on the OS, especially Macs.
1444 //In idle mode 3000-4000 polls are done per second without this check.
1445 //Macs, with this code, max 250 polls per second. (non-macs unused default max 500)
1446 //Currently not implemented for all platforms, given the ALT-TAB stuff for WIN32.
1447 #if defined (MACOSX)
1448 static int last_check = 0;
1449 int current_check = GetTicks();
1450 if (current_check - last_check <= DB_POLLSKIP) return;
1451 last_check = current_check;
1452 #endif
1453
1454 SDL_Event event;
1455 #if defined (REDUCE_JOYSTICK_POLLING)
1456 static int poll_delay = 0;
1457 int time = GetTicks();
1458 if (time - poll_delay > 20) {
1459 poll_delay = time;
1460 if (sdl.num_joysticks > 0) SDL_JoystickUpdate();
1461 MAPPER_UpdateJoysticks();
1462 }
1463 #endif
1464 while (SDL_PollEvent(&event)) {
1465 #if SDL_XORG_FIX
1466 // Special code for broken SDL with Xorg 1.20.1, where pairs of inputfocus gain and loss events are generated
1467 // when locking the mouse in windowed mode.
1468 if (event.type == SDL_ACTIVEEVENT && event.active.state == SDL_APPINPUTFOCUS && event.active.gain == 0) {
1469 SDL_Event test; //Check if the next event would undo this one.
1470 if (SDL_PeepEvents(&test,1,SDL_PEEKEVENT,SDL_ACTIVEEVENTMASK) == 1 && test.active.state == SDL_APPINPUTFOCUS && test.active.gain == 1) {
1471 // Skip both events.
1472 SDL_PeepEvents(&test,1,SDL_GETEVENT,SDL_ACTIVEEVENTMASK);
1473 continue;
1474 }
1475 }
1476 #endif
1477
1478 switch (event.type) {
1479 case SDL_ACTIVEEVENT:
1480 if (event.active.state & SDL_APPINPUTFOCUS) {
1481 if (event.active.gain) {
1482 #ifdef WIN32
1483 if (!sdl.desktop.fullscreen) sdl.focus_ticks = GetTicks();
1484 #endif
1485 if (sdl.desktop.fullscreen && !sdl.mouse.locked)
1486 GFX_CaptureMouse();
1487 SetPriority(sdl.priority.focus);
1488 CPU_Disable_SkipAutoAdjust();
1489 } else {
1490 if (sdl.mouse.locked) {
1491 #ifdef WIN32
1492 if (sdl.desktop.fullscreen) {
1493 VGA_KillDrawing();
1494 sdl.desktop.fullscreen=false;
1495 GFX_ResetScreen();
1496 }
1497 #endif
1498 GFX_CaptureMouse();
1499 }
1500 SetPriority(sdl.priority.nofocus);
1501 GFX_LosingFocus();
1502 CPU_Enable_SkipAutoAdjust();
1503 }
1504 }
1505
1506 /* Non-focus priority is set to pause; check to see if we've lost window or input focus
1507 * i.e. has the window been minimised or made inactive?
1508 */
1509 if (sdl.priority.nofocus == PRIORITY_LEVEL_PAUSE) {
1510 if ((event.active.state & (SDL_APPINPUTFOCUS | SDL_APPACTIVE)) && (!event.active.gain)) {
1511 /* Window has lost focus, pause the emulator.
1512 * This is similar to what PauseDOSBox() does, but the exit criteria is different.
1513 * Instead of waiting for the user to hit Alt-Break, we wait for the window to
1514 * regain window or input focus.
1515 */
1516 bool paused = true;
1517 SDL_Event ev;
1518
1519 GFX_SetTitle(-1,-1,true);
1520 KEYBOARD_ClrBuffer();
1521 // SDL_Delay(500);
1522 // while (SDL_PollEvent(&ev)) {
1523 // flush event queue.
1524 // }
1525
1526 while (paused) {
1527 // WaitEvent waits for an event rather than polling, so CPU usage drops to zero
1528 SDL_WaitEvent(&ev);
1529
1530 switch (ev.type) {
1531 case SDL_QUIT: throw(0); break; // a bit redundant at linux at least as the active events gets before the quit event.
1532 case SDL_ACTIVEEVENT: // wait until we get window focus back
1533 if (ev.active.state & (SDL_APPINPUTFOCUS | SDL_APPACTIVE)) {
1534 // We've got focus back, so unpause and break out of the loop
1535 if (ev.active.gain) {
1536 paused = false;
1537 GFX_SetTitle(-1,-1,false);
1538 }
1539
1540 /* Now poke a "release ALT" command into the keyboard buffer
1541 * we have to do this, otherwise ALT will 'stick' and cause
1542 * problems with the app running in the DOSBox.
1543 */
1544 KEYBOARD_AddKey(KBD_leftalt, false);
1545 KEYBOARD_AddKey(KBD_rightalt, false);
1546 }
1547 break;
1548 }
1549 }
1550 }
1551 }
1552 break;
1553 case SDL_MOUSEMOTION:
1554 HandleMouseMotion(&event.motion);
1555 break;
1556 case SDL_MOUSEBUTTONDOWN:
1557 case SDL_MOUSEBUTTONUP:
1558 HandleMouseButton(&event.button);
1559 break;
1560 case SDL_VIDEORESIZE:
1561 // HandleVideoResize(&event.resize);
1562 break;
1563 case SDL_QUIT:
1564 throw(0);
1565 break;
1566 case SDL_VIDEOEXPOSE:
1567 if (sdl.draw.callback) sdl.draw.callback( GFX_CallBackRedraw );
1568 break;
1569 #ifdef WIN32
1570 case SDL_KEYDOWN:
1571 case SDL_KEYUP:
1572 // ignore event alt+tab
1573 if (event.key.keysym.sym==SDLK_LALT) sdl.laltstate = event.key.type;
1574 if (event.key.keysym.sym==SDLK_RALT) sdl.raltstate = event.key.type;
1575 if (((event.key.keysym.sym==SDLK_TAB)) &&
1576 ((sdl.laltstate==SDL_KEYDOWN) || (sdl.raltstate==SDL_KEYDOWN))) break;
1577 // This can happen as well.
1578 if (((event.key.keysym.sym == SDLK_TAB )) && (event.key.keysym.mod & KMOD_ALT)) break;
1579 // ignore tab events that arrive just after regaining focus. (likely the result of alt-tab)
1580 if ((event.key.keysym.sym == SDLK_TAB) && (GetTicks() - sdl.focus_ticks < 2)) break;
1581 #endif
1582 #if defined (MACOSX)
1583 case SDL_KEYDOWN:
1584 case SDL_KEYUP:
1585 /* On macs CMD-Q is the default key to close an application */
1586 if (event.key.keysym.sym == SDLK_q && (event.key.keysym.mod == KMOD_RMETA || event.key.keysym.mod == KMOD_LMETA) ) {
1587 KillSwitch(true);
1588 break;
1589 }
1590 #endif
1591 default:
1592 void MAPPER_CheckEvent(SDL_Event * event);
1593 MAPPER_CheckEvent(&event);
1594 }
1595 }
1596 }
1597
1598 #if defined (WIN32)
1599 static BOOL WINAPI ConsoleEventHandler(DWORD event) {
1600 switch (event) {
1601 case CTRL_SHUTDOWN_EVENT:
1602 case CTRL_LOGOFF_EVENT:
1603 case CTRL_CLOSE_EVENT:
1604 case CTRL_BREAK_EVENT:
1605 raise(SIGTERM);
1606 return TRUE;
1607 case CTRL_C_EVENT:
1608 default: //pass to the next handler
1609 return FALSE;
1610 }
1611 }
1612 #endif
1613
1614
1615 /* static variable to show wether there is not a valid stdout.
1616 * Fixes some bugs when -noconsole is used in a read only directory */
1617 static bool no_stdout = false;
1618 void GFX_ShowMsg(char const* format,...) {
1619 char buf[512];
1620 va_list msg;
1621 va_start(msg,format);
1622 vsprintf(buf,format,msg);
1623 strcat(buf,"\n");
1624 va_end(msg);
1625 if(!no_stdout) printf("%s",buf); //Else buf is parsed again.
1626 }
1627
1628
1629 void Config_Add_SDL() {
1630 Section_prop * sdl_sec=control->AddSection_prop("sdl",&GUI_StartUp);
1631 sdl_sec->AddInitFunction(&MAPPER_StartUp);
1632 Prop_bool* Pbool;
1633 Prop_string* Pstring;
1634 Prop_int* Pint;
1635 Prop_multival* Pmulti;
1636
1637 Pbool = sdl_sec->Add_bool("fullscreen",Property::Changeable::Always,false);
1638 Pbool->Set_help("Start dosbox directly in fullscreen. (Press ALT-Enter to go back)");
1639
1640 Pbool = sdl_sec->Add_bool("fulldouble",Property::Changeable::Always,false);
1641 Pbool->Set_help("Use double buffering in fullscreen. It can reduce screen flickering, but it can also result in a slow DOSBox.");
1642
1643 Pstring = sdl_sec->Add_string("fullresolution",Property::Changeable::Always,"original");
1644 Pstring->Set_help("What resolution to use for fullscreen: original, desktop or fixed size (e.g. 1024x768).\n"
1645 " Using your monitor's native resolution (desktop) with aspect=true might give the best results.\n"
1646 " If you end up with small window on a large screen, try an output different from surface.\n"
1647 " On Windows 10 with display scaling (Scale and layout) set to a value above 100%, it is recommended\n"
1648 " to use a lower full/windowresolution, in order to avoid window size problems.");
1649 Pstring = sdl_sec->Add_string("windowresolution",Property::Changeable::Always,"original");
1650 Pstring->Set_help("Scale the window to this size IF the output device supports hardware scaling.\n"
1651 " (output=surface does not!)");
1652
1653 const char* outputs[] = {
1654 "surface", "overlay",
1655 #if C_OPENGL
1656 "opengl", "openglnb",
1657 #endif
1658 #if (HAVE_DDRAW_H) && defined(WIN32)
1659 "ddraw",
1660 #endif
1661 0 };
1662 #if C_OPENGL && defined(MACOSX)
1663 Pstring = sdl_sec->Add_string("output",Property::Changeable::Always,"opengl");
1664 #else
1665 Pstring = sdl_sec->Add_string("output",Property::Changeable::Always,"surface");
1666 #endif
1667 Pstring->Set_help("What video system to use for output.");
1668 Pstring->Set_values(outputs);
1669
1670 Pbool = sdl_sec->Add_bool("autolock",Property::Changeable::Always,true);
1671 Pbool->Set_help("Mouse will automatically lock, if you click on the screen. (Press CTRL-F10 to unlock)");
1672
1673 Pint = sdl_sec->Add_int("sensitivity",Property::Changeable::Always,100);
1674 Pint->SetMinMax(1,1000);
1675 Pint->Set_help("Mouse sensitivity.");
1676
1677 Pbool = sdl_sec->Add_bool("waitonerror",Property::Changeable::Always, true);
1678 Pbool->Set_help("Wait before closing the console if dosbox has an error.");
1679
1680 Pmulti = sdl_sec->Add_multi("priority", Property::Changeable::Always, ",");
1681 Pmulti->SetValue("higher,normal");
1682 Pmulti->Set_help("Priority levels for dosbox. Second entry behind the comma is for when dosbox is not focused/minimized.\n"
1683 " pause is only valid for the second entry.");
1684
1685 const char* actt[] = { "lowest", "lower", "normal", "higher", "highest", "pause", 0};
1686 Pstring = Pmulti->GetSection()->Add_string("active",Property::Changeable::Always,"higher");
1687 Pstring->Set_values(actt);
1688
1689 const char* inactt[] = { "lowest", "lower", "normal", "higher", "highest", "pause", 0};
1690 Pstring = Pmulti->GetSection()->Add_string("inactive",Property::Changeable::Always,"normal");
1691 Pstring->Set_values(inactt);
1692
1693 Pstring = sdl_sec->Add_path("mapperfile",Property::Changeable::Always,MAPPERFILE);
1694 Pstring->Set_help("File used to load/save the key/event mappings from. Resetmapper only works with the defaul value.");
1695
1696 Pbool = sdl_sec->Add_bool("usescancodes",Property::Changeable::Always,true);
1697 Pbool->Set_help("Avoid usage of symkeys, might not work on all operating systems.");
1698 }
1699
1700 static void show_warning(char const * const message) {
1701 bool textonly = true;
1702 #ifdef WIN32
1703 textonly = false;
1704 if ( !sdl.inited && SDL_Init(SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE) < 0 ) textonly = true;
1705 sdl.inited = true;
1706 #endif
1707 printf("%s", message);
1708 if(textonly) return;
1709 if(!sdl.surface) sdl.surface = SDL_SetVideoMode_Wrap(640,400,0,0);
1710 if(!sdl.surface) return;
1711 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
1712 Bit32u rmask = 0xff000000;
1713 Bit32u gmask = 0x00ff0000;
1714 Bit32u bmask = 0x0000ff00;
1715 #else
1716 Bit32u rmask = 0x000000ff;
1717 Bit32u gmask = 0x0000ff00;
1718 Bit32u bmask = 0x00ff0000;
1719 #endif
1720 SDL_Surface* splash_surf = SDL_CreateRGBSurface(SDL_SWSURFACE, 640, 400, 32, rmask, gmask, bmask, 0);
1721 if (!splash_surf) return;
1722
1723 int x = 120,y = 20;
1724 std::string m(message),m2;
1725 std::string::size_type a,b,c,d;
1726
1727 while(m.size()) { //Max 50 characters. break on space before or on a newline
1728 c = m.find('\n');
1729 d = m.rfind(' ',50);
1730 if(c>d) a=b=d; else a=b=c;
1731 if( a != std::string::npos) b++;
1732 m2 = m.substr(0,a); m.erase(0,b);
1733 OutputString(x,y,m2.c_str(),0xffffffff,0,splash_surf);
1734 y += 20;
1735 }
1736
1737 SDL_BlitSurface(splash_surf, NULL, sdl.surface, NULL);
1738 SDL_Flip(sdl.surface);
1739 SDL_Delay(12000);
1740 }
1741
1742 static void launcheditor() {
1743 std::string path,file;
1744 Cross::CreatePlatformConfigDir(path);
1745 Cross::GetPlatformConfigName(file);
1746 path += file;
1747 FILE* f = fopen(path.c_str(),"r");
1748 if(!f && !control->PrintConfig(path.c_str())) {
1749 printf("tried creating %s. but failed.\n",path.c_str());
1750 exit(1);
1751 }
1752 if(f) fclose(f);
1753 /* if(edit.empty()) {
1754 printf("no editor specified.\n");
1755 exit(1);
1756 }*/
1757 std::string edit;
1758 while(control->cmdline->FindString("-editconf",edit,true)) //Loop until one succeeds
1759 execlp(edit.c_str(),edit.c_str(),path.c_str(),(char*) 0);
1760 //if you get here the launching failed!
1761 printf("can't find editor(s) specified at the command line.\n");
1762 exit(1);
1763 }
1764 static void launchcaptures(std::string const& edit) {
1765 std::string path,file;
1766 Section* t = control->GetSection("dosbox");
1767 if(t) file = t->GetPropValue("captures");
1768 if(!t || file == NO_SUCH_PROPERTY) {
1769 printf("Config system messed up.\n");
1770 exit(1);
1771 }
1772 Cross::CreatePlatformConfigDir(path);
1773 path += file;
1774 Cross::CreateDir(path);
1775 struct stat cstat;
1776 if(stat(path.c_str(),&cstat) || (cstat.st_mode & S_IFDIR) == 0) {
1777 printf("%s doesn't exists or isn't a directory.\n",path.c_str());
1778 exit(1);
1779 }
1780 /* if(edit.empty()) {
1781 printf("no editor specified.\n");
1782 exit(1);
1783 }*/
1784
1785 execlp(edit.c_str(),edit.c_str(),path.c_str(),(char*) 0);
1786 //if you get here the launching failed!
1787 printf("can't find filemanager %s\n",edit.c_str());
1788 exit(1);
1789 }
1790
1791 static void printconfiglocation() {
1792 std::string path,file;
1793 Cross::CreatePlatformConfigDir(path);
1794 Cross::GetPlatformConfigName(file);
1795 path += file;
1796
1797 FILE* f = fopen(path.c_str(),"r");
1798 if(!f && !control->PrintConfig(path.c_str())) {
1799 printf("tried creating %s. but failed",path.c_str());
1800 exit(1);
1801 }
1802 if(f) fclose(f);
1803 printf("%s\n",path.c_str());
1804 exit(0);
1805 }
1806
1807 static void eraseconfigfile() {
1808 FILE* f = fopen("dosbox.conf","r");
1809 if(f) {
1810 fclose(f);
1811 show_warning("Warning: dosbox.conf exists in current working directory.\nThis will override the configuration file at runtime.\n");
1812 }
1813 std::string path,file;
1814 Cross::GetPlatformConfigDir(path);
1815 Cross::GetPlatformConfigName(file);
1816 path += file;
1817 f = fopen(path.c_str(),"r");
1818 if(!f) exit(0);
1819 fclose(f);
1820 unlink(path.c_str());
1821 exit(0);
1822 }
1823
1824 static void erasemapperfile() {
1825 FILE* g = fopen("dosbox.conf","r");
1826 if(g) {
1827 fclose(g);
1828 show_warning("Warning: dosbox.conf exists in current working directory.\nKeymapping might not be properly reset.\n"
1829 "Please reset configuration as well and delete the dosbox.conf.\n");
1830 }
1831
1832 std::string path,file=MAPPERFILE;
1833 Cross::GetPlatformConfigDir(path);
1834 path += file;
1835 FILE* f = fopen(path.c_str(),"r");
1836 if(!f) exit(0);
1837 fclose(f);
1838 unlink(path.c_str());
1839 exit(0);
1840 }
1841
1842
1843
1844 //extern void UI_Init(void);
1845 int main(int argc, char* argv[]) {
1846 try {
1847 CommandLine com_line(argc,argv);
1848 Config myconf(&com_line);
1849 control=&myconf;
1850 /* Init the configuration system and add default values */
1851 Config_Add_SDL();
1852 DOSBOX_Init();
1853
1854 std::string editor;
1855 if(control->cmdline->FindString("-editconf",editor,false)) launcheditor();
1856 if(control->cmdline->FindString("-opencaptures",editor,true)) launchcaptures(editor);
1857 if(control->cmdline->FindExist("-eraseconf")) eraseconfigfile();
1858 if(control->cmdline->FindExist("-resetconf")) eraseconfigfile();
1859 if(control->cmdline->FindExist("-erasemapper")) erasemapperfile();
1860 if(control->cmdline->FindExist("-resetmapper")) erasemapperfile();
1861
1862 /* Can't disable the console with debugger enabled */
1863 #if defined(WIN32) && !(C_DEBUG)
1864 if (control->cmdline->FindExist("-noconsole")) {
1865 FreeConsole();
1866 /* Redirect standard input and standard output */
1867 if(freopen(STDOUT_FILE, "w", stdout) == NULL)
1868 no_stdout = true; // No stdout so don't write messages
1869 freopen(STDERR_FILE, "w", stderr);
1870 setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */
1871 setbuf(stderr, NULL); /* No buffering */
1872 } else {
1873 if (AllocConsole()) {
1874 fclose(stdin);
1875 fclose(stdout);
1876 fclose(stderr);
1877 freopen("CONIN$","r",stdin);
1878 freopen("CONOUT$","w",stdout);
1879 freopen("CONOUT$","w",stderr);
1880 }
1881 SetConsoleTitle("DOSBox Status Window");
1882 }
1883 #endif //defined(WIN32) && !(C_DEBUG)
1884 if (control->cmdline->FindExist("-version") ||
1885 control->cmdline->FindExist("--version") ) {
1886 printf("\nDOSBox version %s, copyright 2002-2019 DOSBox Team.\n\n",VERSION);
1887 printf("DOSBox is written by the DOSBox Team (See AUTHORS file))\n");
1888 printf("DOSBox comes with ABSOLUTELY NO WARRANTY. This is free software,\n");
1889 printf("and you are welcome to redistribute it under certain conditions;\n");
1890 printf("please read the COPYING file thoroughly before doing so.\n\n");
1891 return 0;
1892 }
1893 if(control->cmdline->FindExist("-printconf")) printconfiglocation();
1894
1895 #if C_DEBUG
1896 DEBUG_SetupConsole();
1897 #endif
1898
1899 #if defined(WIN32)
1900 SetConsoleCtrlHandler((PHANDLER_ROUTINE) ConsoleEventHandler,TRUE);
1901 #endif
1902
1903 #ifdef OS2
1904 PPIB pib;
1905 PTIB tib;
1906 DosGetInfoBlocks(&tib, &pib);
1907 if (pib->pib_ultype == 2) pib->pib_ultype = 3;
1908 setbuf(stdout, NULL);
1909 setbuf(stderr, NULL);
1910 #endif
1911
1912 /* Display Welcometext in the console */
1913 LOG_MSG("DOSBox version %s",VERSION);
1914 LOG_MSG("Copyright 2002-2019 DOSBox Team, published under GNU GPL.");
1915 LOG_MSG("---");
1916
1917 /* Init SDL */
1918 #if SDL_VERSION_ATLEAST(1, 2, 14)
1919 /* Or debian/ubuntu with older libsdl version as they have done this themselves, but then differently.
1920 * with this variable they will work correctly. I've only tested the 1.2.14 behaviour against the windows version
1921 * of libsdl
1922 */
1923 putenv(const_cast<char*>("SDL_DISABLE_LOCK_KEYS=1"));
1924 #endif
1925 // Don't init timers, GetTicks seems to work fine and they can use a fair amount of power (Macs again)
1926 // Please report problems with audio and other things.
1927 if ( SDL_Init( SDL_INIT_AUDIO|SDL_INIT_VIDEO | /*SDL_INIT_TIMER |*/ SDL_INIT_CDROM
1928 |SDL_INIT_NOPARACHUTE
1929 ) < 0 ) E_Exit("Can't init SDL %s",SDL_GetError());
1930 sdl.inited = true;
1931
1932 #ifndef DISABLE_JOYSTICK
1933 //Initialise Joystick separately. This way we can warn when it fails instead
1934 //of exiting the application
1935 if( SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0 ) LOG_MSG("Failed to init joystick support");
1936 #endif
1937
1938 sdl.laltstate = SDL_KEYUP;
1939 sdl.raltstate = SDL_KEYUP;
1940
1941 #if defined (WIN32)
1942 #if SDL_VERSION_ATLEAST(1, 2, 10)
1943 sdl.using_windib=true;
1944 #else
1945 sdl.using_windib=false;
1946 #endif
1947 char sdl_drv_name[128];
1948 if (getenv("SDL_VIDEODRIVER")==NULL) {
1949 if (SDL_VideoDriverName(sdl_drv_name,128)!=NULL) {
1950 sdl.using_windib=false;
1951 if (strcmp(sdl_drv_name,"directx")!=0) {
1952 SDL_QuitSubSystem(SDL_INIT_VIDEO);
1953 putenv("SDL_VIDEODRIVER=directx");
1954 if (SDL_InitSubSystem(SDL_INIT_VIDEO)<0) {
1955 putenv("SDL_VIDEODRIVER=windib");
1956 if (SDL_InitSubSystem(SDL_INIT_VIDEO)<0) E_Exit("Can't init SDL Video %s",SDL_GetError());
1957 sdl.using_windib=true;
1958 }
1959 }
1960 }
1961 } else {
1962 char* sdl_videodrv = getenv("SDL_VIDEODRIVER");
1963 if (strcmp(sdl_videodrv,"directx")==0) sdl.using_windib = false;
1964 else if (strcmp(sdl_videodrv,"windib")==0) sdl.using_windib = true;
1965 }
1966 if (SDL_VideoDriverName(sdl_drv_name,128)!=NULL) {
1967 if (strcmp(sdl_drv_name,"windib")==0) LOG_MSG("SDL_Init: Starting up with SDL windib video driver.\n Try to update your video card and directx drivers!");
1968 }
1969 #endif
1970 sdl.num_joysticks=SDL_NumJoysticks();
1971
1972 /* Parse configuration files */
1973 std::string config_file,config_path;
1974 bool parsed_anyconfigfile = false;
1975 //First Parse -userconf
1976 if(control->cmdline->FindExist("-userconf",true)){
1977 config_file.clear();
1978 Cross::GetPlatformConfigDir(config_path);
1979 Cross::GetPlatformConfigName(config_file);
1980 config_path += config_file;
1981 if(control->ParseConfigFile(config_path.c_str())) parsed_anyconfigfile = true;
1982 if(!parsed_anyconfigfile) {
1983 //Try to create the userlevel configfile.
1984 config_file.clear();
1985 Cross::CreatePlatformConfigDir(config_path);
1986 Cross::GetPlatformConfigName(config_file);
1987 config_path += config_file;
1988 if(control->PrintConfig(config_path.c_str())) {
1989 LOG_MSG("CONFIG: Generating default configuration.\nWriting it to %s",config_path.c_str());
1990 //Load them as well. Makes relative paths much easier
1991 if(control->ParseConfigFile(config_path.c_str())) parsed_anyconfigfile = true;
1992 }
1993 }
1994 }
1995
1996 //Second parse -conf entries
1997 while(control->cmdline->FindString("-conf",config_file,true))
1998 if (control->ParseConfigFile(config_file.c_str())) parsed_anyconfigfile = true;
1999
2000 //if none found => parse localdir conf
2001 config_file = "dosbox.conf";
2002 if (!parsed_anyconfigfile && control->ParseConfigFile(config_file.c_str())) parsed_anyconfigfile = true;
2003
2004 //if none found => parse userlevel conf
2005 if(!parsed_anyconfigfile) {
2006 config_file.clear();
2007 Cross::GetPlatformConfigDir(config_path);
2008 Cross::GetPlatformConfigName(config_file);
2009 config_path += config_file;
2010 if(control->ParseConfigFile(config_path.c_str())) parsed_anyconfigfile = true;
2011 }
2012
2013 if(!parsed_anyconfigfile) {
2014 //Try to create the userlevel configfile.
2015 config_file.clear();
2016 Cross::CreatePlatformConfigDir(config_path);
2017 Cross::GetPlatformConfigName(config_file);
2018 config_path += config_file;
2019 if(control->PrintConfig(config_path.c_str())) {
2020 LOG_MSG("CONFIG: Generating default configuration.\nWriting it to %s",config_path.c_str());
2021 //Load them as well. Makes relative paths much easier
2022 control->ParseConfigFile(config_path.c_str());
2023 } else {
2024 LOG_MSG("CONFIG: Using default settings. Create a configfile to change them");
2025 }
2026 }
2027
2028
2029 #if (ENVIRON_LINKED)
2030 control->ParseEnv(environ);
2031 #endif
2032 // UI_Init();
2033 // if (control->cmdline->FindExist("-startui")) UI_Run(false);
2034 /* Init all the sections */
2035 control->Init();
2036 /* Some extra SDL Functions */
2037 Section_prop * sdl_sec=static_cast<Section_prop *>(control->GetSection("sdl"));
2038
2039 if (control->cmdline->FindExist("-fullscreen") || sdl_sec->Get_bool("fullscreen")) {
2040 if(!sdl.desktop.fullscreen) { //only switch if not allready in fullscreen
2041 GFX_SwitchFullScreen();
2042 }
2043 }
2044
2045 /* Init the keyMapper */
2046 MAPPER_Init();
2047 if (control->cmdline->FindExist("-startmapper")) MAPPER_RunInternal();
2048 /* Start up main machine */
2049 control->StartUp();
2050 /* Shutdown everything */
2051 } catch (char * error) {
2052 GFX_ShowMsg("Exit to error: %s",error);
2053 fflush(NULL);
2054 if(sdl.wait_on_error) {
2055 //TODO Maybe look for some way to show message in linux?
2056 #if (C_DEBUG)
2057 GFX_ShowMsg("Press enter to continue");
2058 fflush(NULL);
2059 fgetc(stdin);
2060 #elif defined(WIN32)
2061 Sleep(5000);
2062 #endif
2063 }
2064
2065 }
2066 catch (int){
2067 ;//nothing pressed killswitch
2068 }
2069 catch(...){
2070 //Force visible mouse to end user. Somehow this sometimes doesn't happen
2071 SDL_WM_GrabInput(SDL_GRAB_OFF);
2072 SDL_ShowCursor(SDL_ENABLE);
2073 throw;//dunno what happened. rethrow for sdl to catch
2074 }
2075 //Force visible mouse to end user. Somehow this sometimes doesn't happen
2076 SDL_WM_GrabInput(SDL_GRAB_OFF);
2077 SDL_ShowCursor(SDL_ENABLE);
2078
2079 SDL_Quit();//Let's hope sdl will quit as well when it catches an exception
2080 return 0;
2081 }
2082
2083 void GFX_GetSize(int &width, int &height, bool &fullscreen) {
2084 width = sdl.draw.width;
2085 height = sdl.draw.height;
2086 fullscreen = sdl.desktop.fullscreen;
2087 }