src/video/dga/SDL_dgavideo.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 May 2006 06:54:20 +0000
changeset 1784 45669d4efd02
parent 1575 3ba88cb7eb1b
child 1785 dcec47a019e2
permissions -rw-r--r--
Disable the screensaver in DGA mode, which is even more important than X11...
slouken@0
     1
/*
slouken@1312
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@0
     4
slouken@1312
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1312
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@1312
    10
    This library is distributed in the hope that it will be useful,
slouken@1312
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1312
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@1312
    19
    Sam Lantinga
slouken@1312
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@1402
    22
#include "SDL_config.h"
slouken@0
    23
slouken@0
    24
/* DGA 2.0 based SDL video driver implementation.
slouken@0
    25
*/
slouken@0
    26
slouken@1338
    27
#include <stdio.h>
slouken@1338
    28
slouken@0
    29
#include <X11/Xlib.h>
slouken@1361
    30
#include "../Xext/extensions/xf86dga.h"
slouken@0
    31
slouken@0
    32
#include "SDL_video.h"
slouken@0
    33
#include "SDL_mouse.h"
slouken@1361
    34
#include "../SDL_sysvideo.h"
slouken@1361
    35
#include "../SDL_pixels_c.h"
slouken@1361
    36
#include "../../events/SDL_events_c.h"
slouken@0
    37
#include "SDL_dgavideo.h"
slouken@0
    38
#include "SDL_dgamouse_c.h"
slouken@0
    39
#include "SDL_dgaevents_c.h"
slouken@0
    40
icculus@1168
    41
/* get function pointers... */
icculus@1168
    42
#include "../x11/SDL_x11dyn.h"
icculus@1168
    43
slouken@1784
    44
/* Heheh we're using X11 event code */
slouken@1784
    45
extern void X11_SaveScreenSaver(Display *display, int *saved_timeout, BOOL *dpms);
slouken@1784
    46
extern void X11_DisableScreenSaver(Display *display);
slouken@1784
    47
extern void X11_RestoreScreenSaver(Display *display, int saved_timeout, BOOL dpms);
slouken@1784
    48
slouken@0
    49
/* Initialization/Query functions */
slouken@0
    50
static int DGA_VideoInit(_THIS, SDL_PixelFormat *vformat);
slouken@0
    51
static SDL_Rect **DGA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
slouken@0
    52
static SDL_Surface *DGA_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
slouken@0
    53
static int DGA_SetColors(_THIS, int firstcolor, int ncolors,
slouken@0
    54
			 SDL_Color *colors);
slouken@0
    55
static int DGA_SetGammaRamp(_THIS, Uint16 *ramp);
slouken@0
    56
static void DGA_VideoQuit(_THIS);
slouken@0
    57
slouken@0
    58
/* Hardware surface functions */
slouken@101
    59
static int DGA_InitHWSurfaces(_THIS, SDL_Surface *screen, Uint8 *base, int size);
slouken@0
    60
static void DGA_FreeHWSurfaces(_THIS);
slouken@0
    61
static int DGA_AllocHWSurface(_THIS, SDL_Surface *surface);
slouken@0
    62
static int DGA_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color);
slouken@0
    63
static int DGA_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst);
slouken@0
    64
static int DGA_LockHWSurface(_THIS, SDL_Surface *surface);
slouken@0
    65
static void DGA_UnlockHWSurface(_THIS, SDL_Surface *surface);
slouken@0
    66
static void DGA_FreeHWSurface(_THIS, SDL_Surface *surface);
slouken@0
    67
static int DGA_FlipHWSurface(_THIS, SDL_Surface *surface);
slouken@0
    68
slouken@0
    69
/* DGA driver bootstrap functions */
slouken@0
    70
slouken@0
    71
static int DGA_Available(void)
slouken@0
    72
{
icculus@1168
    73
	const char *display = NULL;
icculus@1168
    74
	Display *dpy = NULL;
icculus@1168
    75
	int available = 0;
slouken@0
    76
slouken@0
    77
	/* The driver is available is available if the display is local
slouken@0
    78
	   and the DGA 2.0+ extension is available, and we can map mem.
slouken@0
    79
	*/
icculus@1168
    80
	if ( SDL_X11_LoadSymbols() ) {
icculus@1575
    81
		if ( (SDL_strncmp(XDisplayName(display), ":", 1) == 0) ||
icculus@1575
    82
		     (SDL_strncmp(XDisplayName(display), "unix:", 5) == 0) ) {
icculus@1575
    83
			dpy = XOpenDisplay(display);
icculus@1168
    84
			if ( dpy ) {
icculus@1168
    85
				int events, errors, major, minor;
slouken@0
    86
icculus@1168
    87
				if ( SDL_NAME(XDGAQueryExtension)(dpy, &events, &errors) &&
icculus@1168
    88
				     SDL_NAME(XDGAQueryVersion)(dpy, &major, &minor) ) {
icculus@1168
    89
					int screen;
slouken@0
    90
icculus@1168
    91
					screen = DefaultScreen(dpy);
icculus@1168
    92
					if ( (major >= 2) && 
icculus@1168
    93
					     SDL_NAME(XDGAOpenFramebuffer)(dpy, screen) ) {
icculus@1168
    94
						available = 1;
icculus@1168
    95
						SDL_NAME(XDGACloseFramebuffer)(dpy, screen);
icculus@1168
    96
					}
slouken@0
    97
				}
icculus@1575
    98
				XCloseDisplay(dpy);
slouken@0
    99
			}
slouken@0
   100
		}
icculus@1168
   101
		SDL_X11_UnloadSymbols();
slouken@0
   102
	}
slouken@0
   103
	return(available);
slouken@0
   104
}
slouken@0
   105
slouken@0
   106
static void DGA_DeleteDevice(SDL_VideoDevice *device)
slouken@0
   107
{
icculus@1168
   108
	if (device != NULL) {
slouken@1336
   109
		SDL_free(device->hidden);
slouken@1336
   110
		SDL_free(device);
icculus@1168
   111
		SDL_X11_UnloadSymbols();
icculus@1168
   112
	}
slouken@0
   113
}
slouken@0
   114
slouken@0
   115
static SDL_VideoDevice *DGA_CreateDevice(int devindex)
slouken@0
   116
{
icculus@1168
   117
	SDL_VideoDevice *device = NULL;
slouken@0
   118
slouken@0
   119
	/* Initialize all variables that we clean on shutdown */
icculus@1168
   120
	if (SDL_X11_LoadSymbols()) {
slouken@1336
   121
		device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
icculus@1168
   122
		if ( device ) {
slouken@1336
   123
			SDL_memset(device, 0, (sizeof *device));
icculus@1168
   124
			device->hidden = (struct SDL_PrivateVideoData *)
slouken@1336
   125
					SDL_malloc((sizeof *device->hidden));
icculus@1168
   126
		}
icculus@1168
   127
		if ( (device == NULL) || (device->hidden == NULL) ) {
icculus@1168
   128
			SDL_OutOfMemory();
icculus@1168
   129
			if ( device ) {
slouken@1336
   130
				SDL_free(device);
icculus@1168
   131
			}
icculus@1168
   132
			SDL_X11_UnloadSymbols();
icculus@1168
   133
			return(0);
icculus@1168
   134
		}
slouken@1336
   135
		SDL_memset(device->hidden, 0, (sizeof *device->hidden));
icculus@1168
   136
icculus@1168
   137
		/* Set the function pointers */
icculus@1168
   138
		device->VideoInit = DGA_VideoInit;
icculus@1168
   139
		device->ListModes = DGA_ListModes;
icculus@1168
   140
		device->SetVideoMode = DGA_SetVideoMode;
icculus@1168
   141
		device->SetColors = DGA_SetColors;
icculus@1168
   142
		device->UpdateRects = NULL;
icculus@1168
   143
		device->VideoQuit = DGA_VideoQuit;
icculus@1168
   144
		device->AllocHWSurface = DGA_AllocHWSurface;
icculus@1168
   145
		device->CheckHWBlit = DGA_CheckHWBlit;
icculus@1168
   146
		device->FillHWRect = DGA_FillHWRect;
icculus@1168
   147
		device->SetHWColorKey = NULL;
icculus@1168
   148
		device->SetHWAlpha = NULL;
icculus@1168
   149
		device->LockHWSurface = DGA_LockHWSurface;
icculus@1168
   150
		device->UnlockHWSurface = DGA_UnlockHWSurface;
icculus@1168
   151
		device->FlipHWSurface = DGA_FlipHWSurface;
icculus@1168
   152
		device->FreeHWSurface = DGA_FreeHWSurface;
icculus@1168
   153
		device->SetGammaRamp = DGA_SetGammaRamp;
icculus@1168
   154
		device->GetGammaRamp = NULL;
icculus@1168
   155
		device->SetCaption = NULL;
icculus@1168
   156
		device->SetIcon = NULL;
icculus@1168
   157
		device->IconifyWindow = NULL;
icculus@1168
   158
		device->GrabInput = NULL;
icculus@1168
   159
		device->GetWMInfo = NULL;
icculus@1168
   160
		device->InitOSKeymap = DGA_InitOSKeymap;
icculus@1168
   161
		device->PumpEvents = DGA_PumpEvents;
icculus@1168
   162
icculus@1168
   163
		device->free = DGA_DeleteDevice;
slouken@0
   164
	}
slouken@0
   165
slouken@0
   166
	return device;
slouken@0
   167
}
slouken@0
   168
slouken@0
   169
VideoBootStrap DGA_bootstrap = {
slouken@0
   170
	"dga", "XFree86 DGA 2.0",
slouken@0
   171
	DGA_Available, DGA_CreateDevice
slouken@0
   172
};
slouken@0
   173
slouken@0
   174
static int DGA_AddMode(_THIS, int bpp, int w, int h)
slouken@0
   175
{
slouken@0
   176
	SDL_Rect *mode;
slouken@0
   177
	int i, index;
slouken@0
   178
	int next_mode;
slouken@0
   179
slouken@0
   180
	/* Check to see if we already have this mode */
slouken@0
   181
	if ( bpp < 8 ) {  /* Not supported */
slouken@0
   182
		return(0);
slouken@0
   183
	}
slouken@0
   184
	index = ((bpp+7)/8)-1;
slouken@0
   185
	for ( i=0; i<SDL_nummodes[index]; ++i ) {
slouken@0
   186
		mode = SDL_modelist[index][i];
slouken@0
   187
		if ( (mode->w == w) && (mode->h == h) ) {
slouken@0
   188
			return(0);
slouken@0
   189
		}
slouken@0
   190
	}
slouken@0
   191
slouken@0
   192
	/* Set up the new video mode rectangle */
slouken@1336
   193
	mode = (SDL_Rect *)SDL_malloc(sizeof *mode);
slouken@0
   194
	if ( mode == NULL ) {
slouken@0
   195
		SDL_OutOfMemory();
slouken@0
   196
		return(-1);
slouken@0
   197
	}
slouken@0
   198
	mode->x = 0;
slouken@0
   199
	mode->y = 0;
slouken@0
   200
	mode->w = w;
slouken@0
   201
	mode->h = h;
slouken@0
   202
slouken@0
   203
	/* Allocate the new list of modes, and fill in the new mode */
slouken@0
   204
	next_mode = SDL_nummodes[index];
slouken@0
   205
	SDL_modelist[index] = (SDL_Rect **)
slouken@1336
   206
	       SDL_realloc(SDL_modelist[index], (1+next_mode+1)*sizeof(SDL_Rect *));
slouken@0
   207
	if ( SDL_modelist[index] == NULL ) {
slouken@0
   208
		SDL_OutOfMemory();
slouken@0
   209
		SDL_nummodes[index] = 0;
slouken@1336
   210
		SDL_free(mode);
slouken@0
   211
		return(-1);
slouken@0
   212
	}
slouken@0
   213
	SDL_modelist[index][next_mode] = mode;
slouken@0
   214
	SDL_modelist[index][next_mode+1] = NULL;
slouken@0
   215
	SDL_nummodes[index]++;
slouken@0
   216
slouken@0
   217
	return(0);
slouken@0
   218
}
slouken@0
   219
slouken@0
   220
/* This whole function is a hack. :) */
slouken@0
   221
static Uint32 get_video_size(_THIS)
slouken@0
   222
{
slouken@0
   223
	/* This is a non-exported function from libXxf86dga.a */
slouken@292
   224
	extern unsigned char *SDL_NAME(XDGAGetMappedMemory)(int screen);
slouken@0
   225
	FILE *proc;
slouken@0
   226
	unsigned long mem;
slouken@0
   227
	unsigned start, stop;
slouken@0
   228
	char line[BUFSIZ];
slouken@0
   229
	Uint32 size;
slouken@0
   230
slouken@292
   231
	mem = (unsigned long)SDL_NAME(XDGAGetMappedMemory)(DGA_Screen);
slouken@0
   232
	size = 0;
slouken@0
   233
	proc = fopen("/proc/self/maps", "r");
slouken@0
   234
	if ( proc ) {
slouken@0
   235
		while ( fgets(line, sizeof(line)-1, proc) ) {
slouken@1336
   236
			SDL_sscanf(line, "%x-%x", &start, &stop);
slouken@0
   237
			if ( start == mem ) {
slouken@0
   238
				size = (Uint32)((stop-start)/1024);
slouken@0
   239
				break;
slouken@0
   240
			}
slouken@0
   241
		}
slouken@0
   242
		fclose(proc);
slouken@0
   243
	}
slouken@0
   244
	return(size);
slouken@0
   245
}
slouken@0
   246
slouken@0
   247
#ifdef DGA_DEBUG
slouken@292
   248
static void PrintMode(SDL_NAME(XDGAMode) *mode)
slouken@0
   249
{
slouken@0
   250
	printf("Mode: %s (%dx%d) at %d bpp (%f refresh, %d pitch) num: %d\n",
slouken@0
   251
		mode->name,
slouken@0
   252
		mode->viewportWidth, mode->viewportHeight,
slouken@0
   253
		mode->depth == 24 ? mode->bitsPerPixel : mode->depth,
slouken@0
   254
		mode->verticalRefresh, mode->bytesPerScanline, mode->num);
slouken@0
   255
	printf("\tRGB: 0x%8.8x 0x%8.8x 0x%8.8x (%d - %s)\n",
slouken@0
   256
		mode->redMask, mode->greenMask, mode->blueMask,
slouken@0
   257
		mode->visualClass,
slouken@0
   258
		mode->visualClass == TrueColor ? "truecolor" :
slouken@0
   259
		mode->visualClass == DirectColor ? "directcolor" :
slouken@0
   260
		mode->visualClass == PseudoColor ? "pseudocolor" : "unknown");
slouken@0
   261
	printf("\tFlags: ");
slouken@0
   262
	if ( mode->flags & XDGAConcurrentAccess )
slouken@0
   263
		printf(" XDGAConcurrentAccess");
slouken@0
   264
	if ( mode->flags & XDGASolidFillRect )
slouken@0
   265
		printf(" XDGASolidFillRect");
slouken@0
   266
	if ( mode->flags & XDGABlitRect )
slouken@0
   267
		printf(" XDGABlitRect");
slouken@0
   268
	if ( mode->flags & XDGABlitTransRect )
slouken@0
   269
		printf(" XDGABlitTransRect");
slouken@0
   270
	if ( mode->flags & XDGAPixmap )
slouken@0
   271
		printf(" XDGAPixmap");
slouken@0
   272
	if ( mode->flags & XDGAInterlaced )
slouken@0
   273
		printf(" XDGAInterlaced");
slouken@0
   274
	if ( mode->flags & XDGADoublescan )
slouken@0
   275
		printf(" XDGADoublescan");
slouken@0
   276
	if ( mode->viewportFlags & XDGAFlipRetrace )
slouken@0
   277
		printf(" XDGAFlipRetrace");
slouken@0
   278
	if ( mode->viewportFlags & XDGAFlipImmediate )
slouken@0
   279
		printf(" XDGAFlipImmediate");
slouken@0
   280
	printf("\n");
slouken@0
   281
}
slouken@0
   282
#endif /* DGA_DEBUG */
slouken@0
   283
slouken@0
   284
static int cmpmodes(const void *va, const void *vb)
slouken@0
   285
{
slouken@292
   286
    const SDL_NAME(XDGAMode) *a = (const SDL_NAME(XDGAMode) *)va;
slouken@292
   287
    const SDL_NAME(XDGAMode) *b = (const SDL_NAME(XDGAMode) *)vb;
slouken@0
   288
slouken@0
   289
    /* Prefer DirectColor visuals for otherwise equal modes */
slouken@0
   290
    if ( (a->viewportWidth == b->viewportWidth) &&
slouken@0
   291
         (b->viewportHeight == a->viewportHeight) ) {
slouken@966
   292
        if ( a->visualClass == DirectColor )
slouken@966
   293
            return -1;
slouken@966
   294
        if ( b->visualClass == DirectColor )
slouken@966
   295
            return 1;
slouken@966
   296
        return 0;
slouken@966
   297
    } else if ( a->viewportWidth == b->viewportWidth ) {
slouken@966
   298
        return b->viewportHeight - a->viewportHeight;
slouken@0
   299
    } else {
slouken@966
   300
        return b->viewportWidth - a->viewportWidth;
slouken@0
   301
    }
slouken@0
   302
}
slouken@292
   303
static void UpdateHWInfo(_THIS, SDL_NAME(XDGAMode) *mode)
slouken@0
   304
{
slouken@0
   305
	this->info.wm_available = 0;
slouken@0
   306
	this->info.hw_available = 1;
slouken@0
   307
	if ( mode->flags & XDGABlitRect ) {
slouken@0
   308
		this->info.blit_hw = 1;
slouken@0
   309
	} else {
slouken@0
   310
		this->info.blit_hw = 0;
slouken@0
   311
	}
slouken@0
   312
	if ( mode->flags & XDGABlitTransRect ) {
slouken@0
   313
		this->info.blit_hw_CC = 1;
slouken@0
   314
	} else {
slouken@0
   315
		this->info.blit_hw_CC = 0;
slouken@0
   316
	}
slouken@0
   317
	if ( mode->flags & XDGASolidFillRect ) {
slouken@0
   318
		this->info.blit_fill = 1;
slouken@0
   319
	} else {
slouken@0
   320
		this->info.blit_fill = 0;
slouken@0
   321
	}
slouken@0
   322
	this->info.video_mem = get_video_size(this);
slouken@0
   323
}
slouken@0
   324
slouken@0
   325
static int DGA_VideoInit(_THIS, SDL_PixelFormat *vformat)
slouken@0
   326
{
slouken@0
   327
	const char *display;
slouken@0
   328
	int event_base, error_base;
slouken@0
   329
	int major_version, minor_version;
slouken@0
   330
	Visual *visual;
slouken@292
   331
	SDL_NAME(XDGAMode) *modes;
slouken@0
   332
	int i, num_modes;
slouken@0
   333
slouken@0
   334
	/* Open the X11 display */
slouken@0
   335
	display = NULL;		/* Get it from DISPLAY environment variable */
slouken@0
   336
icculus@1575
   337
	DGA_Display = XOpenDisplay(display);
slouken@0
   338
	if ( DGA_Display == NULL ) {
slouken@0
   339
		SDL_SetError("Couldn't open X11 display");
slouken@0
   340
		return(-1);
slouken@0
   341
	}
slouken@0
   342
slouken@0
   343
	/* Check for the DGA extension */
slouken@292
   344
	if ( ! SDL_NAME(XDGAQueryExtension)(DGA_Display, &event_base, &error_base) ||
slouken@292
   345
	     ! SDL_NAME(XDGAQueryVersion)(DGA_Display, &major_version, &minor_version) ) {
slouken@0
   346
		SDL_SetError("DGA extension not available");
icculus@1575
   347
		XCloseDisplay(DGA_Display);
slouken@0
   348
		return(-1);
slouken@0
   349
	}
slouken@0
   350
	if ( major_version < 2 ) {
slouken@0
   351
		SDL_SetError("DGA driver requires DGA 2.0 or newer");
icculus@1575
   352
		XCloseDisplay(DGA_Display);
slouken@0
   353
		return(-1);
slouken@0
   354
	}
slouken@0
   355
	DGA_event_base = event_base;
slouken@0
   356
slouken@1545
   357
	/* Determine the current screen size */
slouken@1545
   358
	this->info.current_w = DisplayWidth(DGA_Display, DGA_Screen);
slouken@1545
   359
	this->info.current_h = DisplayHeight(DGA_Display, DGA_Screen);
slouken@1545
   360
slouken@0
   361
	/* Determine the current screen depth */
slouken@0
   362
	visual = DefaultVisual(DGA_Display, DGA_Screen);
slouken@0
   363
	{
slouken@0
   364
		XPixmapFormatValues *pix_format;
slouken@0
   365
		int i, num_formats;
slouken@0
   366
slouken@0
   367
		vformat->BitsPerPixel = DefaultDepth(DGA_Display, DGA_Screen);
icculus@1575
   368
		pix_format = XListPixmapFormats(DGA_Display, &num_formats);
slouken@0
   369
		if ( pix_format == NULL ) {
slouken@0
   370
			SDL_SetError("Couldn't determine screen formats");
icculus@1575
   371
			XCloseDisplay(DGA_Display);
slouken@0
   372
			return(-1);
slouken@0
   373
		}
slouken@0
   374
		for ( i=0; i<num_formats; ++i ) {
slouken@0
   375
			if ( vformat->BitsPerPixel == pix_format[i].depth )
slouken@0
   376
				break;
slouken@0
   377
		}
slouken@0
   378
		if ( i != num_formats )
slouken@0
   379
			vformat->BitsPerPixel = pix_format[i].bits_per_pixel;
icculus@1575
   380
		XFree((char *)pix_format);
slouken@0
   381
	}
slouken@0
   382
	if ( vformat->BitsPerPixel > 8 ) {
slouken@0
   383
		vformat->Rmask = visual->red_mask;
slouken@0
   384
		vformat->Gmask = visual->green_mask;
slouken@0
   385
		vformat->Bmask = visual->blue_mask;
slouken@0
   386
	}
slouken@0
   387
slouken@0
   388
	/* Open access to the framebuffer */
slouken@292
   389
	if ( ! SDL_NAME(XDGAOpenFramebuffer)(DGA_Display, DGA_Screen) ) {
slouken@0
   390
		SDL_SetError("Unable to map the video memory");
icculus@1575
   391
		XCloseDisplay(DGA_Display);
slouken@0
   392
		return(-1);
slouken@0
   393
	}
slouken@0
   394
slouken@1784
   395
	/* Save DPMS and screensaver settings */
slouken@1784
   396
	X11_SaveScreenSaver(DGA_Display, &screensaver_timeout, &dpms_enabled);
slouken@1784
   397
	X11_DisableScreenSaver(DGA_Display);
slouken@1784
   398
slouken@0
   399
	/* Query for the list of available video modes */
slouken@292
   400
	modes = SDL_NAME(XDGAQueryModes)(DGA_Display, DGA_Screen, &num_modes);
slouken@1338
   401
	SDL_qsort(modes, num_modes, sizeof *modes, cmpmodes);
slouken@0
   402
	for ( i=0; i<num_modes; ++i ) {
slouken@0
   403
#ifdef DGA_DEBUG
slouken@0
   404
		PrintMode(&modes[i]);
slouken@0
   405
#endif
slouken@0
   406
		if ( (modes[i].visualClass == PseudoColor) ||
slouken@0
   407
		     (modes[i].visualClass == DirectColor) ||
slouken@0
   408
		     (modes[i].visualClass == TrueColor) ) {
slouken@0
   409
			DGA_AddMode(this, modes[i].bitsPerPixel,
slouken@0
   410
			            modes[i].viewportWidth,
slouken@0
   411
			            modes[i].viewportHeight);
slouken@0
   412
		}
slouken@0
   413
	}
slouken@0
   414
	UpdateHWInfo(this, modes);
icculus@1575
   415
	XFree(modes);
slouken@0
   416
slouken@0
   417
	/* Create the hardware surface lock mutex */
slouken@0
   418
	hw_lock = SDL_CreateMutex();
slouken@0
   419
	if ( hw_lock == NULL ) {
slouken@0
   420
		SDL_SetError("Unable to create lock mutex");
slouken@0
   421
		DGA_VideoQuit(this);
slouken@0
   422
		return(-1);
slouken@0
   423
	}
slouken@0
   424
slouken@101
   425
#ifdef LOCK_DGA_DISPLAY
slouken@101
   426
	/* Create the event lock so we're thread-safe.. :-/ */
slouken@101
   427
	event_lock = SDL_CreateMutex();
slouken@101
   428
#endif /* LOCK_DGA_DISPLAY */
slouken@101
   429
slouken@0
   430
	/* We're done! */
slouken@0
   431
	return(0);
slouken@0
   432
}
slouken@0
   433
slouken@0
   434
SDL_Rect **DGA_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
slouken@0
   435
{
slouken@0
   436
	return(SDL_modelist[((format->BitsPerPixel+7)/8)-1]);
slouken@0
   437
}
slouken@0
   438
slouken@0
   439
/* Various screen update functions available */
slouken@0
   440
static void DGA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
slouken@0
   441
slouken@0
   442
SDL_Surface *DGA_SetVideoMode(_THIS, SDL_Surface *current,
slouken@0
   443
				int width, int height, int bpp, Uint32 flags)
slouken@0
   444
{
slouken@292
   445
	SDL_NAME(XDGAMode) *modes;
slouken@0
   446
	int i, num_modes;
slouken@292
   447
	SDL_NAME(XDGADevice) *mode;
slouken@0
   448
	int screen_len;
slouken@0
   449
	Uint8 *surfaces_mem;
slouken@0
   450
	int surfaces_len;
slouken@0
   451
slouken@0
   452
	/* Free any previous colormap */
slouken@0
   453
	if ( DGA_colormap ) {
icculus@1575
   454
		XFreeColormap(DGA_Display, DGA_colormap);
slouken@0
   455
		DGA_colormap = 0;
slouken@0
   456
	}
slouken@0
   457
slouken@0
   458
	/* Search for a matching video mode */
slouken@292
   459
	modes = SDL_NAME(XDGAQueryModes)(DGA_Display, DGA_Screen, &num_modes);
slouken@1338
   460
	SDL_qsort(modes, num_modes, sizeof *modes, cmpmodes);
slouken@0
   461
	for ( i=0; i<num_modes; ++i ) {
slouken@0
   462
		int depth;
slouken@0
   463
slouken@0
   464
		
slouken@0
   465
		depth = modes[i].depth;
slouken@0
   466
		if ( depth == 24 ) { /* Distinguish between 24 and 32 bpp */
slouken@0
   467
			depth = modes[i].bitsPerPixel;
slouken@0
   468
		}
slouken@0
   469
		if ( (depth == bpp) &&
slouken@0
   470
		     (modes[i].viewportWidth == width) &&
slouken@0
   471
		     (modes[i].viewportHeight == height) &&
slouken@0
   472
		     ((modes[i].visualClass == PseudoColor) ||
slouken@0
   473
		      (modes[i].visualClass == DirectColor) ||
slouken@0
   474
		      (modes[i].visualClass == TrueColor)) ) {
slouken@0
   475
			break;
slouken@0
   476
		}
slouken@0
   477
	}
slouken@0
   478
	if ( i == num_modes ) {
slouken@0
   479
		SDL_SetError("No matching video mode found");
slouken@0
   480
		return(NULL);
slouken@0
   481
	}
slouken@0
   482
slouken@0
   483
	/* Set the video mode */
slouken@292
   484
	mode = SDL_NAME(XDGASetMode)(DGA_Display, DGA_Screen, modes[i].num);
icculus@1575
   485
	XFree(modes);
slouken@0
   486
	if ( mode == NULL ) {
slouken@0
   487
		SDL_SetError("Unable to switch to requested mode");
slouken@0
   488
		return(NULL);
slouken@0
   489
	}
slouken@559
   490
	DGA_visualClass = mode->mode.visualClass;
slouken@0
   491
	memory_base = (Uint8 *)mode->data;
slouken@0
   492
	memory_pitch = mode->mode.bytesPerScanline;
slouken@0
   493
slouken@0
   494
	/* Set up the new mode framebuffer */
slouken@0
   495
	current->flags = (SDL_FULLSCREEN|SDL_HWSURFACE);
slouken@0
   496
	current->w = mode->mode.viewportWidth;
slouken@0
   497
	current->h = mode->mode.viewportHeight;
slouken@0
   498
	current->pitch = memory_pitch;
slouken@0
   499
	current->pixels = memory_base;
slouken@0
   500
	if ( ! SDL_ReallocFormat(current, mode->mode.bitsPerPixel,
slouken@0
   501
	                                  mode->mode.redMask,
slouken@0
   502
	                                  mode->mode.greenMask,
slouken@0
   503
	                                  mode->mode.blueMask, 0) ) {
slouken@0
   504
		return(NULL);
slouken@0
   505
	}
slouken@0
   506
	screen_len = current->h*current->pitch;
slouken@0
   507
slouken@0
   508
	/* Create a colormap if necessary */
slouken@0
   509
	if ( (DGA_visualClass == PseudoColor) ||
slouken@0
   510
             (DGA_visualClass == DirectColor) ) {
slouken@292
   511
		DGA_colormap = SDL_NAME(XDGACreateColormap)(DGA_Display, DGA_Screen,
slouken@0
   512
							mode, AllocAll);
slouken@0
   513
		if ( DGA_visualClass == PseudoColor ) {
slouken@0
   514
			current->flags |= SDL_HWPALETTE;
slouken@0
   515
		} else {
slouken@0
   516
	    		/* Initialize the colormap to the identity mapping */
slouken@0
   517
	    		SDL_GetGammaRamp(0, 0, 0);
slouken@0
   518
	    		this->screen = current;
slouken@0
   519
	    		DGA_SetGammaRamp(this, this->gamma);
slouken@0
   520
			this->screen = NULL;
slouken@0
   521
		}
slouken@0
   522
	} else {
slouken@292
   523
		DGA_colormap = SDL_NAME(XDGACreateColormap)(DGA_Display, DGA_Screen,
slouken@0
   524
							mode, AllocNone);
slouken@0
   525
	}
slouken@292
   526
	SDL_NAME(XDGAInstallColormap)(DGA_Display, DGA_Screen, DGA_colormap);
slouken@0
   527
slouken@0
   528
	/* Update the hardware capabilities */
slouken@0
   529
	UpdateHWInfo(this, &mode->mode);
slouken@0
   530
slouken@0
   531
	/* Set up the information for hardware surfaces */
slouken@0
   532
	surfaces_mem = (Uint8 *)current->pixels + screen_len;
slouken@0
   533
	surfaces_len = (mode->mode.imageHeight*current->pitch - screen_len);
slouken@0
   534
slouken@0
   535
	/* Update for double-buffering, if we can */
slouken@292
   536
	SDL_NAME(XDGASetViewport)(DGA_Display, DGA_Screen, 0, 0, XDGAFlipRetrace);
slouken@0
   537
	if ( flags & SDL_DOUBLEBUF ) {
slouken@0
   538
		if ( mode->mode.imageHeight >= (current->h*2) ) {
slouken@0
   539
			current->flags |= SDL_DOUBLEBUF;
slouken@0
   540
			flip_page = 0;
slouken@0
   541
			flip_yoffset[0] = 0;
slouken@0
   542
			flip_yoffset[1] = current->h;
slouken@0
   543
			flip_address[0] = memory_base;
slouken@0
   544
			flip_address[1] = memory_base+screen_len;
slouken@0
   545
			surfaces_mem += screen_len;
slouken@0
   546
			surfaces_len -= screen_len;
slouken@0
   547
		}
slouken@0
   548
	}
slouken@0
   549
slouken@0
   550
	/* Allocate memory tracking for hardware surfaces */
slouken@0
   551
	DGA_FreeHWSurfaces(this);
slouken@0
   552
	if ( surfaces_len < 0 ) {
slouken@0
   553
		surfaces_len = 0;
slouken@0
   554
	}
slouken@101
   555
	DGA_InitHWSurfaces(this, current, surfaces_mem, surfaces_len);
slouken@0
   556
slouken@202
   557
	/* Expose the back buffer as surface memory */
slouken@202
   558
	if ( current->flags & SDL_DOUBLEBUF ) {
slouken@202
   559
		this->screen = current;
slouken@202
   560
		DGA_FlipHWSurface(this, current);
slouken@202
   561
		this->screen = NULL;
slouken@202
   562
	}
slouken@202
   563
slouken@0
   564
	/* Set the update rectangle function */
slouken@0
   565
	this->UpdateRects = DGA_DirectUpdate;
slouken@0
   566
slouken@0
   567
	/* Enable mouse and keyboard support */
slouken@0
   568
	{ long input_mask;
slouken@0
   569
	  input_mask = (KeyPressMask | KeyReleaseMask);
slouken@0
   570
	  input_mask |= (ButtonPressMask | ButtonReleaseMask);
slouken@0
   571
	  input_mask |= PointerMotionMask;
slouken@292
   572
	  SDL_NAME(XDGASelectInput)(DGA_Display, DGA_Screen, input_mask);
slouken@0
   573
	}
slouken@0
   574
slouken@0
   575
	/* We're done */
slouken@0
   576
	return(current);
slouken@0
   577
}
slouken@0
   578
slouken@0
   579
#ifdef DGA_DEBUG
slouken@0
   580
static void DGA_DumpHWSurfaces(_THIS)
slouken@0
   581
{
slouken@0
   582
	vidmem_bucket *bucket;
slouken@0
   583
slouken@0
   584
	printf("Memory left: %d (%d total)\n", surfaces_memleft, surfaces_memtotal);
slouken@0
   585
	printf("\n");
slouken@0
   586
	printf("         Base  Size\n");
slouken@0
   587
	for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
slouken@0
   588
		printf("Bucket:  %p, %d (%s)\n", bucket->base, bucket->size, bucket->used ? "used" : "free");
slouken@0
   589
		if ( bucket->prev ) {
slouken@0
   590
			if ( bucket->base != bucket->prev->base+bucket->prev->size ) {
slouken@0
   591
				printf("Warning, corrupt bucket list! (prev)\n");
slouken@0
   592
			}
slouken@0
   593
		} else {
slouken@0
   594
			if ( bucket != &surfaces ) {
slouken@0
   595
				printf("Warning, corrupt bucket list! (!prev)\n");
slouken@0
   596
			}
slouken@0
   597
		}
slouken@0
   598
		if ( bucket->next ) {
slouken@0
   599
			if ( bucket->next->base != bucket->base+bucket->size ) {
slouken@0
   600
				printf("Warning, corrupt bucket list! (next)\n");
slouken@0
   601
			}
slouken@0
   602
		}
slouken@0
   603
	}
slouken@0
   604
	printf("\n");
slouken@0
   605
}
slouken@0
   606
#endif
slouken@0
   607
slouken@101
   608
static int DGA_InitHWSurfaces(_THIS, SDL_Surface *screen, Uint8 *base, int size)
slouken@0
   609
{
slouken@101
   610
	vidmem_bucket *bucket;
slouken@101
   611
slouken@0
   612
	surfaces_memtotal = size;
slouken@0
   613
	surfaces_memleft = size;
slouken@101
   614
slouken@101
   615
	if ( surfaces_memleft > 0 ) {
slouken@1336
   616
		bucket = (vidmem_bucket *)SDL_malloc(sizeof(*bucket));
slouken@101
   617
		if ( bucket == NULL ) {
slouken@101
   618
			SDL_OutOfMemory();
slouken@101
   619
			return(-1);
slouken@101
   620
		}
slouken@101
   621
		bucket->prev = &surfaces;
slouken@101
   622
		bucket->used = 0;
slouken@101
   623
		bucket->dirty = 0;
slouken@101
   624
		bucket->base = base;
slouken@101
   625
		bucket->size = size;
slouken@101
   626
		bucket->next = NULL;
slouken@101
   627
	} else {
slouken@101
   628
		bucket = NULL;
slouken@101
   629
	}
slouken@101
   630
slouken@101
   631
	surfaces.prev = NULL;
slouken@101
   632
	surfaces.used = 1;
slouken@101
   633
	surfaces.dirty = 0;
slouken@101
   634
	surfaces.base = screen->pixels;
slouken@101
   635
	surfaces.size = (unsigned int)((long)base - (long)surfaces.base);
slouken@101
   636
	surfaces.next = bucket;
slouken@101
   637
	screen->hwdata = (struct private_hwdata *)&surfaces;
slouken@0
   638
	return(0);
slouken@0
   639
}
slouken@0
   640
static void DGA_FreeHWSurfaces(_THIS)
slouken@0
   641
{
slouken@0
   642
	vidmem_bucket *bucket, *freeable;
slouken@0
   643
slouken@0
   644
	bucket = surfaces.next;
slouken@0
   645
	while ( bucket ) {
slouken@0
   646
		freeable = bucket;
slouken@0
   647
		bucket = bucket->next;
slouken@1336
   648
		SDL_free(freeable);
slouken@0
   649
	}
slouken@0
   650
	surfaces.next = NULL;
slouken@0
   651
}
slouken@0
   652
slouken@105
   653
static __inline__ void DGA_AddBusySurface(SDL_Surface *surface)
slouken@101
   654
{
slouken@101
   655
	((vidmem_bucket *)surface->hwdata)->dirty = 1;
slouken@101
   656
}
slouken@101
   657
slouken@105
   658
static __inline__ int DGA_IsSurfaceBusy(SDL_Surface *surface)
slouken@101
   659
{
slouken@101
   660
	return ((vidmem_bucket *)surface->hwdata)->dirty;
slouken@101
   661
}
slouken@101
   662
slouken@105
   663
static __inline__ void DGA_WaitBusySurfaces(_THIS)
slouken@101
   664
{
slouken@101
   665
	vidmem_bucket *bucket;
slouken@101
   666
slouken@101
   667
	/* Wait for graphic operations to complete */
slouken@292
   668
	SDL_NAME(XDGASync)(DGA_Display, DGA_Screen);
slouken@101
   669
slouken@101
   670
	/* Clear all surface dirty bits */
slouken@101
   671
	for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
slouken@101
   672
		bucket->dirty = 0;
slouken@101
   673
	}
slouken@101
   674
}
slouken@101
   675
slouken@0
   676
static int DGA_AllocHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   677
{
slouken@0
   678
	vidmem_bucket *bucket;
slouken@0
   679
	int size;
slouken@0
   680
	int extra;
slouken@101
   681
	int retval = 0;
slouken@0
   682
slouken@0
   683
/* Temporarily, we only allow surfaces the same width as display.
slouken@0
   684
   Some blitters require the pitch between two hardware surfaces
slouken@0
   685
   to be the same.  Others have interesting alignment restrictions.
slouken@0
   686
*/
slouken@0
   687
if ( surface->pitch > SDL_VideoSurface->pitch ) {
slouken@0
   688
	SDL_SetError("Surface requested wider than screen");
slouken@0
   689
	return(-1);
slouken@0
   690
}
slouken@0
   691
surface->pitch = SDL_VideoSurface->pitch;
slouken@0
   692
	size = surface->h * surface->pitch;
slouken@0
   693
#ifdef DGA_DEBUG
slouken@0
   694
	fprintf(stderr, "Allocating bucket of %d bytes\n", size);
slouken@0
   695
#endif
slouken@101
   696
	LOCK_DISPLAY();
slouken@0
   697
slouken@0
   698
	/* Quick check for available mem */
slouken@0
   699
	if ( size > surfaces_memleft ) {
slouken@0
   700
		SDL_SetError("Not enough video memory");
slouken@101
   701
		retval = -1;
slouken@101
   702
		goto done;
slouken@0
   703
	}
slouken@0
   704
slouken@0
   705
	/* Search for an empty bucket big enough */
slouken@0
   706
	for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
slouken@0
   707
		if ( ! bucket->used && (size <= bucket->size) ) {
slouken@0
   708
			break;
slouken@0
   709
		}
slouken@0
   710
	}
slouken@0
   711
	if ( bucket == NULL ) {
slouken@0
   712
		SDL_SetError("Video memory too fragmented");
slouken@101
   713
		retval = -1;
slouken@101
   714
		goto done;
slouken@0
   715
	}
slouken@0
   716
slouken@0
   717
	/* Create a new bucket for left-over memory */
slouken@0
   718
	extra = (bucket->size - size);
slouken@0
   719
	if ( extra ) {
slouken@0
   720
		vidmem_bucket *newbucket;
slouken@0
   721
slouken@0
   722
#ifdef DGA_DEBUG
slouken@0
   723
	fprintf(stderr, "Adding new free bucket of %d bytes\n", extra);
slouken@0
   724
#endif
slouken@1336
   725
		newbucket = (vidmem_bucket *)SDL_malloc(sizeof(*newbucket));
slouken@0
   726
		if ( newbucket == NULL ) {
slouken@0
   727
			SDL_OutOfMemory();
slouken@101
   728
			retval = -1;
slouken@101
   729
			goto done;
slouken@0
   730
		}
slouken@0
   731
		newbucket->prev = bucket;
slouken@0
   732
		newbucket->used = 0;
slouken@0
   733
		newbucket->base = bucket->base+size;
slouken@0
   734
		newbucket->size = extra;
slouken@0
   735
		newbucket->next = bucket->next;
slouken@0
   736
		if ( bucket->next ) {
slouken@0
   737
			bucket->next->prev = newbucket;
slouken@0
   738
		}
slouken@0
   739
		bucket->next = newbucket;
slouken@0
   740
	}
slouken@0
   741
slouken@0
   742
	/* Set the current bucket values and return it! */
slouken@0
   743
	bucket->used = 1;
slouken@0
   744
	bucket->size = size;
slouken@101
   745
	bucket->dirty = 0;
slouken@0
   746
#ifdef DGA_DEBUG
slouken@0
   747
	fprintf(stderr, "Allocated %d bytes at %p\n", bucket->size, bucket->base);
slouken@0
   748
#endif
slouken@0
   749
	surfaces_memleft -= size;
slouken@0
   750
	surface->flags |= SDL_HWSURFACE;
slouken@0
   751
	surface->pixels = bucket->base;
slouken@101
   752
	surface->hwdata = (struct private_hwdata *)bucket;
slouken@101
   753
done:
slouken@101
   754
	UNLOCK_DISPLAY();
slouken@101
   755
	return(retval);
slouken@0
   756
}
slouken@0
   757
static void DGA_FreeHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   758
{
slouken@0
   759
	vidmem_bucket *bucket, *freeable;
slouken@0
   760
slouken@0
   761
	/* Look for the bucket in the current list */
slouken@102
   762
	for ( bucket=&surfaces; bucket; bucket=bucket->next ) {
slouken@102
   763
		if ( bucket == (vidmem_bucket *)surface->hwdata ) {
slouken@102
   764
			break;
slouken@102
   765
		}
slouken@0
   766
	}
slouken@102
   767
	if ( bucket && bucket->used ) {
slouken@102
   768
		/* Add the memory back to the total */
slouken@0
   769
#ifdef DGA_DEBUG
slouken@0
   770
	printf("Freeing bucket of %d bytes\n", bucket->size);
slouken@0
   771
#endif
slouken@102
   772
		surfaces_memleft += bucket->size;
slouken@0
   773
slouken@102
   774
		/* Can we merge the space with surrounding buckets? */
slouken@102
   775
		bucket->used = 0;
slouken@102
   776
		if ( bucket->next && ! bucket->next->used ) {
slouken@0
   777
#ifdef DGA_DEBUG
slouken@0
   778
	printf("Merging with next bucket, for %d total bytes\n", bucket->size+bucket->next->size);
slouken@0
   779
#endif
slouken@102
   780
			freeable = bucket->next;
slouken@102
   781
			bucket->size += bucket->next->size;
slouken@102
   782
			bucket->next = bucket->next->next;
slouken@102
   783
			if ( bucket->next ) {
slouken@102
   784
				bucket->next->prev = bucket;
slouken@102
   785
			}
slouken@1336
   786
			SDL_free(freeable);
slouken@0
   787
		}
slouken@102
   788
		if ( bucket->prev && ! bucket->prev->used ) {
slouken@0
   789
#ifdef DGA_DEBUG
slouken@0
   790
	printf("Merging with previous bucket, for %d total bytes\n", bucket->prev->size+bucket->size);
slouken@0
   791
#endif
slouken@102
   792
			freeable = bucket;
slouken@102
   793
			bucket->prev->size += bucket->size;
slouken@102
   794
			bucket->prev->next = bucket->next;
slouken@102
   795
			if ( bucket->next ) {
slouken@102
   796
				bucket->next->prev = bucket->prev;
slouken@102
   797
			}
slouken@1336
   798
			SDL_free(freeable);
slouken@0
   799
		}
slouken@0
   800
	}
slouken@0
   801
	surface->pixels = NULL;
slouken@101
   802
	surface->hwdata = NULL;
slouken@0
   803
}
slouken@0
   804
slouken@105
   805
static __inline__ void DGA_dst_to_xy(_THIS, SDL_Surface *dst, int *x, int *y)
slouken@0
   806
{
slouken@0
   807
	*x = (long)((Uint8 *)dst->pixels - memory_base)%memory_pitch;
slouken@0
   808
	*y = (long)((Uint8 *)dst->pixels - memory_base)/memory_pitch;
slouken@0
   809
}
slouken@0
   810
slouken@0
   811
static int DGA_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color)
slouken@0
   812
{
slouken@0
   813
	int x, y;
slouken@0
   814
	unsigned int w, h;
slouken@0
   815
slouken@0
   816
	/* Don't fill the visible part of the screen, wait until flipped */
slouken@101
   817
	LOCK_DISPLAY();
slouken@0
   818
	if ( was_flipped && (dst == this->screen) ) {
slouken@292
   819
		while ( SDL_NAME(XDGAGetViewportStatus)(DGA_Display, DGA_Screen) )
slouken@0
   820
			/* Keep waiting for the hardware ... */ ;
slouken@0
   821
		was_flipped = 0;
slouken@0
   822
	}
slouken@105
   823
	DGA_dst_to_xy(this, dst, &x, &y);
slouken@0
   824
	x += rect->x;
slouken@0
   825
	y += rect->y;
slouken@0
   826
	w = rect->w;
slouken@0
   827
	h = rect->h;
slouken@0
   828
#if 0
slouken@0
   829
  printf("Hardware accelerated rectangle fill: %dx%d at %d,%d\n", w, h, x, y);
slouken@0
   830
#endif
slouken@292
   831
	SDL_NAME(XDGAFillRectangle)(DGA_Display, DGA_Screen, x, y, w, h, color);
slouken@540
   832
	if ( !(this->screen->flags & SDL_DOUBLEBUF) ) {
icculus@1575
   833
		XFlush(DGA_Display);
slouken@540
   834
	}
slouken@105
   835
	DGA_AddBusySurface(dst);
slouken@101
   836
	UNLOCK_DISPLAY();
slouken@0
   837
	return(0);
slouken@0
   838
}
slouken@0
   839
slouken@0
   840
static int HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
slouken@0
   841
                       SDL_Surface *dst, SDL_Rect *dstrect)
slouken@0
   842
{
slouken@0
   843
	SDL_VideoDevice *this;
slouken@0
   844
	int srcx, srcy;
slouken@0
   845
	int dstx, dsty;
slouken@0
   846
	unsigned int w, h;
slouken@0
   847
slouken@0
   848
	this = current_video;
slouken@0
   849
	/* Don't blit to the visible part of the screen, wait until flipped */
slouken@101
   850
	LOCK_DISPLAY();
slouken@0
   851
	if ( was_flipped && (dst == this->screen) ) {
slouken@292
   852
		while ( SDL_NAME(XDGAGetViewportStatus)(DGA_Display, DGA_Screen) )
slouken@0
   853
			/* Keep waiting for the hardware ... */ ;
slouken@0
   854
		was_flipped = 0;
slouken@0
   855
	}
slouken@105
   856
	DGA_dst_to_xy(this, src, &srcx, &srcy);
slouken@0
   857
	srcx += srcrect->x;
slouken@0
   858
	srcy += srcrect->y;
slouken@105
   859
	DGA_dst_to_xy(this, dst, &dstx, &dsty);
slouken@0
   860
	dstx += dstrect->x;
slouken@0
   861
	dsty += dstrect->y;
slouken@0
   862
	w = srcrect->w;
slouken@0
   863
	h = srcrect->h;
slouken@0
   864
#if 0
slouken@0
   865
  printf("Blitting %dx%d from %d,%d to %d,%d\n", w, h, srcx, srcy, dstx, dsty);
slouken@0
   866
#endif
slouken@0
   867
	if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
slouken@292
   868
		SDL_NAME(XDGACopyTransparentArea)(DGA_Display, DGA_Screen,
slouken@0
   869
			srcx, srcy, w, h, dstx, dsty, src->format->colorkey);
slouken@0
   870
	} else {
slouken@292
   871
		SDL_NAME(XDGACopyArea)(DGA_Display, DGA_Screen,
slouken@0
   872
			srcx, srcy, w, h, dstx, dsty);
slouken@0
   873
	}
slouken@540
   874
	if ( !(this->screen->flags & SDL_DOUBLEBUF) ) {
icculus@1575
   875
		XFlush(DGA_Display);
slouken@540
   876
	}
slouken@105
   877
	DGA_AddBusySurface(src);
slouken@105
   878
	DGA_AddBusySurface(dst);
slouken@101
   879
	UNLOCK_DISPLAY();
slouken@0
   880
	return(0);
slouken@0
   881
}
slouken@0
   882
slouken@0
   883
static int DGA_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
slouken@0
   884
{
slouken@0
   885
	int accelerated;
slouken@0
   886
slouken@0
   887
	/* Set initial acceleration on */
slouken@0
   888
	src->flags |= SDL_HWACCEL;
slouken@0
   889
slouken@0
   890
	/* Set the surface attributes */
slouken@0
   891
	if ( (src->flags & SDL_SRCALPHA) == SDL_SRCALPHA ) {
slouken@0
   892
		if ( ! this->info.blit_hw_A ) {
slouken@0
   893
			src->flags &= ~SDL_HWACCEL;
slouken@0
   894
		}
slouken@0
   895
	}
slouken@0
   896
	if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) {
slouken@0
   897
		if ( ! this->info.blit_hw_CC ) {
slouken@0
   898
			src->flags &= ~SDL_HWACCEL;
slouken@0
   899
		}
slouken@0
   900
	}
slouken@0
   901
slouken@0
   902
	/* Check to see if final surface blit is accelerated */
slouken@0
   903
	accelerated = !!(src->flags & SDL_HWACCEL);
slouken@0
   904
	if ( accelerated ) {
slouken@0
   905
		src->map->hw_blit = HWAccelBlit;
slouken@0
   906
	}
slouken@0
   907
	return(accelerated);
slouken@0
   908
}
slouken@0
   909
slouken@101
   910
static __inline__ void DGA_WaitFlip(_THIS)
slouken@0
   911
{
slouken@0
   912
	if ( was_flipped ) {
slouken@292
   913
		while ( SDL_NAME(XDGAGetViewportStatus)(DGA_Display, DGA_Screen) )
slouken@0
   914
			/* Keep waiting for the hardware ... */ ;
slouken@0
   915
		was_flipped = 0;
slouken@0
   916
	}
slouken@0
   917
}
slouken@0
   918
slouken@0
   919
static int DGA_LockHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   920
{
slouken@101
   921
	if ( surface == this->screen ) {
slouken@0
   922
		SDL_mutexP(hw_lock);
slouken@101
   923
		LOCK_DISPLAY();
slouken@105
   924
		if ( DGA_IsSurfaceBusy(surface) ) {
slouken@105
   925
			DGA_WaitBusySurfaces(this);
slouken@101
   926
		}
slouken@101
   927
		DGA_WaitFlip(this);
slouken@101
   928
		UNLOCK_DISPLAY();
slouken@101
   929
	} else {
slouken@105
   930
		if ( DGA_IsSurfaceBusy(surface) ) {
slouken@101
   931
			LOCK_DISPLAY();
slouken@105
   932
			DGA_WaitBusySurfaces(this);
slouken@101
   933
			UNLOCK_DISPLAY();
slouken@101
   934
		}
slouken@0
   935
	}
slouken@0
   936
	return(0);
slouken@0
   937
}
slouken@0
   938
static void DGA_UnlockHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   939
{
slouken@101
   940
	if ( surface == this->screen ) {
slouken@0
   941
		SDL_mutexV(hw_lock);
slouken@0
   942
	}
slouken@0
   943
}
slouken@0
   944
slouken@0
   945
static int DGA_FlipHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   946
{
slouken@0
   947
	/* Wait for vertical retrace and then flip display */
slouken@101
   948
	LOCK_DISPLAY();
slouken@105
   949
	if ( DGA_IsSurfaceBusy(this->screen) ) {
slouken@105
   950
		DGA_WaitBusySurfaces(this);
slouken@101
   951
	}
slouken@101
   952
	DGA_WaitFlip(this);
slouken@292
   953
	SDL_NAME(XDGASetViewport)(DGA_Display, DGA_Screen,
slouken@0
   954
	                0, flip_yoffset[flip_page], XDGAFlipRetrace);
icculus@1575
   955
	XFlush(DGA_Display);
slouken@101
   956
	UNLOCK_DISPLAY();
slouken@0
   957
	was_flipped = 1;
slouken@0
   958
	flip_page = !flip_page;
slouken@0
   959
slouken@0
   960
	surface->pixels = flip_address[flip_page];
slouken@0
   961
	return(0);
slouken@0
   962
}
slouken@0
   963
slouken@0
   964
static void DGA_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@0
   965
{
slouken@0
   966
	/* The application is already updating the visible video memory */
slouken@0
   967
	return;
slouken@0
   968
}
slouken@0
   969
slouken@0
   970
static int DGA_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
slouken@0
   971
{
slouken@0
   972
        int i;
slouken@0
   973
	XColor  *xcmap;
slouken@0
   974
slouken@0
   975
	/* This happens on initialization */
slouken@0
   976
	if ( ! DGA_colormap ) {
slouken@0
   977
		return(0);
slouken@0
   978
	}
slouken@1338
   979
	xcmap = SDL_stack_alloc(XColor, ncolors);
slouken@0
   980
	for ( i=0; i<ncolors; ++i ) {
slouken@0
   981
		xcmap[i].pixel = firstcolor + i;
slouken@0
   982
		xcmap[i].red   = (colors[i].r<<8)|colors[i].r;
slouken@0
   983
		xcmap[i].green = (colors[i].g<<8)|colors[i].g;
slouken@0
   984
		xcmap[i].blue  = (colors[i].b<<8)|colors[i].b;
slouken@0
   985
		xcmap[i].flags = (DoRed|DoGreen|DoBlue);
slouken@0
   986
	}
slouken@101
   987
	LOCK_DISPLAY();
icculus@1575
   988
	XStoreColors(DGA_Display, DGA_colormap, xcmap, ncolors);
icculus@1575
   989
	XSync(DGA_Display, False);
slouken@101
   990
	UNLOCK_DISPLAY();
slouken@1338
   991
	SDL_stack_free(xcmap);
slouken@0
   992
slouken@0
   993
	/* That was easy. :) */
slouken@0
   994
	return(1);
slouken@0
   995
}
slouken@0
   996
slouken@0
   997
int DGA_SetGammaRamp(_THIS, Uint16 *ramp)
slouken@0
   998
{
slouken@0
   999
	int i, ncolors;
slouken@0
  1000
	XColor xcmap[256];
slouken@0
  1001
slouken@0
  1002
	/* See if actually setting the gamma is supported */
slouken@0
  1003
	if ( DGA_visualClass != DirectColor ) {
slouken@0
  1004
	    SDL_SetError("Gamma correction not supported on this visual");
slouken@0
  1005
	    return(-1);
slouken@0
  1006
	}
slouken@0
  1007
slouken@0
  1008
	/* Calculate the appropriate palette for the given gamma ramp */
slouken@0
  1009
	if ( this->screen->format->BitsPerPixel <= 16 ) {
slouken@0
  1010
		ncolors = 64; /* Is this right? */
slouken@0
  1011
	} else {
slouken@0
  1012
		ncolors = 256;
slouken@0
  1013
	}
slouken@0
  1014
	for ( i=0; i<ncolors; ++i ) {
slouken@0
  1015
		Uint8 c = (256 * i / ncolors);
slouken@0
  1016
		xcmap[i].pixel = SDL_MapRGB(this->screen->format, c, c, c);
slouken@0
  1017
		xcmap[i].red   = ramp[0*256+c];
slouken@0
  1018
		xcmap[i].green = ramp[1*256+c];
slouken@0
  1019
		xcmap[i].blue  = ramp[2*256+c];
slouken@0
  1020
		xcmap[i].flags = (DoRed|DoGreen|DoBlue);
slouken@0
  1021
	}
slouken@101
  1022
	LOCK_DISPLAY();
icculus@1575
  1023
	XStoreColors(DGA_Display, DGA_colormap, xcmap, ncolors);
icculus@1575
  1024
	XSync(DGA_Display, False);
slouken@101
  1025
	UNLOCK_DISPLAY();
slouken@0
  1026
	return(0);
slouken@0
  1027
}
slouken@0
  1028
slouken@0
  1029
void DGA_VideoQuit(_THIS)
slouken@0
  1030
{
slouken@0
  1031
	int i, j;
slouken@0
  1032
slouken@0
  1033
	if ( DGA_Display ) {
slouken@0
  1034
		/* Free colormap, if necessary */
slouken@0
  1035
		if ( DGA_colormap ) {
icculus@1575
  1036
			XFreeColormap(DGA_Display, DGA_colormap);
slouken@0
  1037
			DGA_colormap = 0;
slouken@0
  1038
		}
slouken@0
  1039
slouken@0
  1040
		/* Unmap memory and reset video mode */
slouken@292
  1041
		SDL_NAME(XDGACloseFramebuffer)(DGA_Display, DGA_Screen);
slouken@0
  1042
		if ( this->screen ) {
slouken@0
  1043
			/* Tell SDL not to free the pixels */
slouken@0
  1044
			this->screen->pixels = NULL;
slouken@0
  1045
		}
slouken@292
  1046
		SDL_NAME(XDGASetMode)(DGA_Display, DGA_Screen, 0);
slouken@0
  1047
slouken@0
  1048
		/* Clear the lock mutex */
slouken@0
  1049
		if ( hw_lock != NULL ) {
slouken@0
  1050
			SDL_DestroyMutex(hw_lock);
slouken@0
  1051
			hw_lock = NULL;
slouken@0
  1052
		}
slouken@101
  1053
#ifdef LOCK_DGA_DISPLAY
slouken@101
  1054
		if ( event_lock != NULL ) {
slouken@101
  1055
			SDL_DestroyMutex(event_lock);
slouken@101
  1056
			event_lock = NULL;
slouken@101
  1057
		}
slouken@101
  1058
#endif /* LOCK_DGA_DISPLAY */
slouken@101
  1059
slouken@0
  1060
		/* Clean up defined video modes */
slouken@0
  1061
		for ( i=0; i<NUM_MODELISTS; ++i ) {
slouken@0
  1062
			if ( SDL_modelist[i] != NULL ) {
slouken@0
  1063
				for ( j=0; SDL_modelist[i][j]; ++j ) {
slouken@1336
  1064
					SDL_free(SDL_modelist[i][j]);
slouken@0
  1065
				}
slouken@1336
  1066
				SDL_free(SDL_modelist[i]);
slouken@0
  1067
				SDL_modelist[i] = NULL;
slouken@0
  1068
			}
slouken@0
  1069
		}
slouken@0
  1070
slouken@0
  1071
		/* Clean up the memory bucket list */
slouken@0
  1072
		DGA_FreeHWSurfaces(this);
slouken@0
  1073
slouken@1784
  1074
		/* Restore DPMS and screensaver settings */
slouken@1784
  1075
		X11_RestoreScreenSaver(DGA_Display, screensaver_timeout, dpms_enabled);
slouken@1784
  1076
slouken@0
  1077
		/* Close up the display */
icculus@1575
  1078
		XCloseDisplay(DGA_Display);
slouken@0
  1079
	}
slouken@0
  1080
}