1/*
2    SDL - Simple DirectMedia Layer
3    Copyright (C) 1997-2012 Sam Lantinga
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with this library; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19    Sam Lantinga
20    slouken@libsdl.org
21*/
22#include "SDL_config.h"
23
24/* Dummy SDL video driver implementation; this is just enough to make an
25 *  SDL-based application THINK it's got a working video driver, for
26 *  applications that call SDL_Init(SDL_INIT_VIDEO) when they don't need it,
27 *  and also for use as a collection of stubs when porting SDL to a new
28 *  platform for which you haven't yet written a valid video driver.
29 *
30 * This is also a great way to determine bottlenecks: if you think that SDL
31 *  is a performance problem for a given platform, enable this driver, and
32 *  then see if your application runs faster without video overhead.
33 *
34 * Initial work by Ryan C. Gordon (icculus@icculus.org). A good portion
35 *  of this was cut-and-pasted from Stephane Peter's work in the AAlib
36 *  SDL video driver.  Renamed to "DUMMY" by Sam Lantinga.
37 */
38
39#include "SDL_video.h"
40#include "SDL_mouse.h"
41#include "../SDL_sysvideo.h"
42#include "../SDL_pixels_c.h"
43#include "../../events/SDL_events_c.h"
44
45#include "SDL_nullvideo.h"
46#include "SDL_nullevents_c.h"
47#include "SDL_nullmouse_c.h"
48
49#define DUMMYVID_DRIVER_NAME "dummy"
50
51/* Initialization/Query functions */
52static int DUMMY_VideoInit(_THIS, SDL_PixelFormat *vformat);
53static SDL_Rect **DUMMY_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
54static SDL_Surface *DUMMY_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
55static int DUMMY_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
56static void DUMMY_VideoQuit(_THIS);
57
58/* Hardware surface functions */
59static int DUMMY_AllocHWSurface(_THIS, SDL_Surface *surface);
60static int DUMMY_LockHWSurface(_THIS, SDL_Surface *surface);
61static void DUMMY_UnlockHWSurface(_THIS, SDL_Surface *surface);
62static void DUMMY_FreeHWSurface(_THIS, SDL_Surface *surface);
63
64/* etc. */
65static void DUMMY_UpdateRects(_THIS, int numrects, SDL_Rect *rects);
66
67/* DUMMY driver bootstrap functions */
68
69static int DUMMY_Available(void)
70{
71	const char *envr = SDL_getenv("SDL_VIDEODRIVER");
72	if ((envr) && (SDL_strcmp(envr, DUMMYVID_DRIVER_NAME) == 0)) {
73		return(1);
74	}
75
76	return(0);
77}
78
79static void DUMMY_DeleteDevice(SDL_VideoDevice *device)
80{
81	SDL_free(device->hidden);
82	SDL_free(device);
83}
84
85static SDL_VideoDevice *DUMMY_CreateDevice(int devindex)
86{
87	SDL_VideoDevice *device;
88
89	/* Initialize all variables that we clean on shutdown */
90	device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
91	if ( device ) {
92		SDL_memset(device, 0, (sizeof *device));
93		device->hidden = (struct SDL_PrivateVideoData *)
94				SDL_malloc((sizeof *device->hidden));
95	}
96	if ( (device == NULL) || (device->hidden == NULL) ) {
97		SDL_OutOfMemory();
98		if ( device ) {
99			SDL_free(device);
100		}
101		return(0);
102	}
103	SDL_memset(device->hidden, 0, (sizeof *device->hidden));
104
105	/* Set the function pointers */
106	device->VideoInit = DUMMY_VideoInit;
107	device->ListModes = DUMMY_ListModes;
108	device->SetVideoMode = DUMMY_SetVideoMode;
109	device->CreateYUVOverlay = NULL;
110	device->SetColors = DUMMY_SetColors;
111	device->UpdateRects = DUMMY_UpdateRects;
112	device->VideoQuit = DUMMY_VideoQuit;
113	device->AllocHWSurface = DUMMY_AllocHWSurface;
114	device->CheckHWBlit = NULL;
115	device->FillHWRect = NULL;
116	device->SetHWColorKey = NULL;
117	device->SetHWAlpha = NULL;
118	device->LockHWSurface = DUMMY_LockHWSurface;
119	device->UnlockHWSurface = DUMMY_UnlockHWSurface;
120	device->FlipHWSurface = NULL;
121	device->FreeHWSurface = DUMMY_FreeHWSurface;
122	device->SetCaption = NULL;
123	device->SetIcon = NULL;
124	device->IconifyWindow = NULL;
125	device->GrabInput = NULL;
126	device->GetWMInfo = NULL;
127	device->InitOSKeymap = DUMMY_InitOSKeymap;
128	device->PumpEvents = DUMMY_PumpEvents;
129
130	device->free = DUMMY_DeleteDevice;
131
132	return device;
133}
134
135VideoBootStrap DUMMY_bootstrap = {
136	DUMMYVID_DRIVER_NAME, "SDL dummy video driver",
137	DUMMY_Available, DUMMY_CreateDevice
138};
139
140
141int DUMMY_VideoInit(_THIS, SDL_PixelFormat *vformat)
142{
143	/*
144	fprintf(stderr, "WARNING: You are using the SDL dummy video driver!\n");
145	*/
146
147	/* Determine the screen depth (use default 8-bit depth) */
148	/* we change this during the SDL_SetVideoMode implementation... */
149	vformat->BitsPerPixel = 8;
150	vformat->BytesPerPixel = 1;
151
152	/* We're done! */
153	return(0);
154}
155
156SDL_Rect **DUMMY_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
157{
158   	 return (SDL_Rect **) -1;
159}
160
161SDL_Surface *DUMMY_SetVideoMode(_THIS, SDL_Surface *current,
162				int width, int height, int bpp, Uint32 flags)
163{
164	if ( this->hidden->buffer ) {
165		SDL_free( this->hidden->buffer );
166	}
167
168	this->hidden->buffer = SDL_malloc(width * height * (bpp / 8));
169	if ( ! this->hidden->buffer ) {
170		SDL_SetError("Couldn't allocate buffer for requested mode");
171		return(NULL);
172	}
173
174/* 	printf("Setting mode %dx%d\n", width, height); */
175
176	SDL_memset(this->hidden->buffer, 0, width * height * (bpp / 8));
177
178	/* Allocate the new pixel format for the screen */
179	if ( ! SDL_ReallocFormat(current, bpp, 0, 0, 0, 0) ) {
180		SDL_free(this->hidden->buffer);
181		this->hidden->buffer = NULL;
182		SDL_SetError("Couldn't allocate new pixel format for requested mode");
183		return(NULL);
184	}
185
186	/* Set up the new mode framebuffer */
187	current->flags = flags & SDL_FULLSCREEN;
188	this->hidden->w = current->w = width;
189	this->hidden->h = current->h = height;
190	current->pitch = current->w * (bpp / 8);
191	current->pixels = this->hidden->buffer;
192
193	/* We're done */
194	return(current);
195}
196
197/* We don't actually allow hardware surfaces other than the main one */
198static int DUMMY_AllocHWSurface(_THIS, SDL_Surface *surface)
199{
200	return(-1);
201}
202static void DUMMY_FreeHWSurface(_THIS, SDL_Surface *surface)
203{
204	return;
205}
206
207/* We need to wait for vertical retrace on page flipped displays */
208static int DUMMY_LockHWSurface(_THIS, SDL_Surface *surface)
209{
210	return(0);
211}
212
213static void DUMMY_UnlockHWSurface(_THIS, SDL_Surface *surface)
214{
215	return;
216}
217
218static void DUMMY_UpdateRects(_THIS, int numrects, SDL_Rect *rects)
219{
220	/* do nothing. */
221}
222
223int DUMMY_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
224{
225	/* do nothing of note. */
226	return(1);
227}
228
229/* Note:  If we are terminated, this could be called in the middle of
230   another SDL video routine -- notably UpdateRects.
231*/
232void DUMMY_VideoQuit(_THIS)
233{
234	if (this->screen->pixels != NULL)
235	{
236		SDL_free(this->screen->pixels);
237		this->screen->pixels = NULL;
238	}
239}
240