src/video/riscos/SDL_wimpvideo.c
changeset 630 550bccdf04bd
child 634 44d574ed4780
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/video/riscos/SDL_wimpvideo.c	Thu May 29 04:44:13 2003 +0000
     1.3 @@ -0,0 +1,492 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997, 1998, 1999, 2000, 2001  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@devolution.com
    1.24 +*/
    1.25 +
    1.26 +/*
    1.27 +     File added by Alan Buckley (alan_baa@hotmail.com) for RISCOS compatability
    1.28 +	 27 March 2003
    1.29 +
    1.30 +     Implements RISCOS wimp display.
    1.31 +*/
    1.32 +
    1.33 +#include <stdio.h>
    1.34 +#include <stdlib.h>
    1.35 +#include <string.h>
    1.36 +
    1.37 +#include "SDL.h"
    1.38 +#include "SDL_error.h"
    1.39 +#include "SDL_video.h"
    1.40 +#include "SDL_mouse.h"
    1.41 +#include "SDL_sysvideo.h"
    1.42 +#include "SDL_pixels_c.h"
    1.43 +#include "SDL_events_c.h"
    1.44 +
    1.45 +#include "SDL_riscostask.h"
    1.46 +#include "SDL_riscosvideo.h"
    1.47 +#include "SDL_riscosevents_c.h"
    1.48 +#include "SDL_riscosmouse_c.h"
    1.49 +
    1.50 +#include "kernel.h"
    1.51 +#include "swis.h"
    1.52 +
    1.53 +/* Initialization/Query functions */
    1.54 +SDL_Rect **WIMP_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
    1.55 +SDL_Surface *WIMP_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
    1.56 +int WIMP_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
    1.57 +void WIMP_SetWMCaption(_THIS, const char *title, const char *icon);
    1.58 +
    1.59 +
    1.60 +extern unsigned char *WIMP_CreateBuffer(int width, int height, int bpp);
    1.61 +extern void WIMP_PumpEvents(_THIS);
    1.62 +extern void WIMP_PlotSprite(_THIS, int x, int y);
    1.63 +extern void WIMP_SetupPlotInfo(_THIS);
    1.64 +extern void WIMP_SetFocus(int win);
    1.65 +
    1.66 +/* etc. */
    1.67 +static void WIMP_UpdateRects(_THIS, int numrects, SDL_Rect *rects);
    1.68 +
    1.69 +/* RISC OS Wimp handling helpers */
    1.70 +void WIMP_ReadModeInfo(_THIS);
    1.71 +unsigned int WIMP_SetupWindow(_THIS, SDL_Surface *surface);
    1.72 +void WIMP_SetDeviceMode(_THIS);
    1.73 +void WIMP_DeleteWindow(_THIS);
    1.74 +
    1.75 +/* FULLSCREEN function required for wimp/fullscreen toggling */
    1.76 +extern int FULLSCREEN_SetMode(int width, int height, int bpp);
    1.77 +
    1.78 +/* Currently need to set this up here as it only works if you
    1.79 +   start up in a Wimp mode */
    1.80 +extern int RISCOS_ToggleFullScreen(_THIS, int fullscreen);
    1.81 +
    1.82 +extern int riscos_backbuffer;
    1.83 +extern int mouseInWindow;
    1.84 +extern int riscos_closeaction;
    1.85 +
    1.86 +
    1.87 +SDL_Surface *WIMP_SetVideoMode(_THIS, SDL_Surface *current,
    1.88 +				int width, int height, int bpp, Uint32 flags)
    1.89 +{
    1.90 +   Uint32 Rmask = 0;
    1.91 +   Uint32 Gmask = 0;
    1.92 +   Uint32 Bmask = 0;
    1.93 +   char *buffer = NULL;
    1.94 +   int bytesPerPixel = 1;
    1.95 +
    1.96 +   /* Don't support double buffering in Wimp mode */
    1.97 +   flags &= ~SDL_DOUBLEBUF;
    1.98 +   flags &= ~SDL_HWSURFACE;
    1.99 +
   1.100 +   switch(bpp)
   1.101 +   {
   1.102 +	case 8:
   1.103 +		/* Emulated palette using ColourTrans */
   1.104 +		flags |= SDL_HWPALETTE;
   1.105 +		break;
   1.106 +
   1.107 +	case 15:
   1.108 +	case 16:
   1.109 +		Bmask = 0x00007c00;
   1.110 +		Gmask = 0x000003e0;
   1.111 +		Rmask = 0x0000001f;
   1.112 +		bytesPerPixel = 2;
   1.113 +		break;
   1.114 +
   1.115 +	case 32:
   1.116 +		Bmask = 0x00ff0000;
   1.117 +		Gmask = 0x0000ff00;
   1.118 +		Rmask = 0x000000ff;
   1.119 +		bytesPerPixel = 4;
   1.120 +		break;
   1.121 +
   1.122 +	default:
   1.123 +		SDL_SetError("Pixel depth not supported");
   1.124 +		return NULL;
   1.125 +		break;
   1.126 +   }
   1.127 +
   1.128 +/* 	printf("Setting mode %dx%d\n", width, height);*/
   1.129 +
   1.130 +	/* Allocate the new pixel format for the screen */
   1.131 +	if ( ! SDL_ReallocFormat(current, bpp, Rmask, Gmask, Bmask, 0) ) {
   1.132 +		SDL_SetError("Couldn't allocate new pixel format for requested mode");
   1.133 +		return(NULL);
   1.134 +	}
   1.135 +
   1.136 +	/* Set up the new mode framebuffer */
   1.137 +	current->w = width;
   1.138 +	this->hidden->height = current->h = height;
   1.139 +
   1.140 +	if (bpp == 15) bpp = 16;
   1.141 +	buffer = WIMP_CreateBuffer(width, height, bpp);
   1.142 +	if (buffer == NULL)
   1.143 +	{
   1.144 +		SDL_SetError("Couldn't create sprite for video memory");
   1.145 +		return (NULL);
   1.146 +	}
   1.147 +
   1.148 +	this->hidden->bank[0] = buffer + 60; /* Start of sprite data */
   1.149 +	if (bpp == 8) this->hidden->bank[0] += 2048; /* 8bpp sprite have palette first */
   1.150 +
   1.151 +	this->hidden->bank[1] = buffer;      /* Start of buffer */
   1.152 +
   1.153 +	/* Remember sprite buffer so it can be freed later */
   1.154 +	if (this->hidden->alloc_bank) free(this->hidden->alloc_bank);
   1.155 +	this->hidden->alloc_bank = buffer;
   1.156 +
   1.157 +	current->pitch = width * bytesPerPixel;
   1.158 +	if ((current->pitch & 3))
   1.159 +	{
   1.160 +		/* Sprites are 32bit word aligned */
   1.161 +		current->pitch += (4 - (current->pitch & 3));
   1.162 +	}
   1.163 +
   1.164 +  	current->flags = flags | SDL_PREALLOC;
   1.165 +
   1.166 +	WIMP_ReadModeInfo(this);
   1.167 +	
   1.168 +    memset(this->hidden->bank[0], 0, height * current->pitch);
   1.169 +
   1.170 +	this->hidden->current_bank = 0;
   1.171 +	current->pixels = this->hidden->bank[0];
   1.172 +
   1.173 +
   1.174 +	if (WIMP_SetupWindow(this, current) == 0)
   1.175 +	{
   1.176 +		SDL_SetError("Unable to create window to display surface");
   1.177 +		return NULL;
   1.178 +	}
   1.179 +
   1.180 +	/* Reset device functions for the wimp */
   1.181 +	WIMP_SetDeviceMode(this);
   1.182 +
   1.183 +    /* Needs to set up plot info after window has been created */
   1.184 +    /* Not sure why, but plots don't work if I do it earlier */
   1.185 +    WIMP_SetupPlotInfo(this);
   1.186 +
   1.187 +	/* We're done */
   1.188 +	return(current);
   1.189 +}
   1.190 +
   1.191 +
   1.192 +void WIMP_ReadModeInfo(_THIS)
   1.193 +{
   1.194 +	_kernel_swi_regs regs;
   1.195 +	int vars[6];
   1.196 +	int vals[5];
   1.197 +
   1.198 +	vars[0] = 4;  /* XEig */
   1.199 +	vars[1] = 5;  /* YEig */
   1.200 +	vars[2] = 9;  /* Log base 2 bpp */
   1.201 +	vars[3] = 11; /* Screen Width - 1 */
   1.202 +	vars[4] = 12; /* Screen Depth - 1 */
   1.203 +	vars[5] = -1; /* Terminate list */
   1.204 +
   1.205 +	regs.r[0] = (int)vars;
   1.206 +	regs.r[1] = (int)vals;
   1.207 +	_kernel_swi(OS_ReadVduVariables, &regs, &regs);
   1.208 +	this->hidden->xeig = vals[0];
   1.209 +	this->hidden->yeig = vals[1];
   1.210 +	this->hidden->screen_bpp = 1 << vals[2];
   1.211 +	this->hidden->screen_width = vals[3] + 1;
   1.212 +	this->hidden->screen_height = vals[4] + 1;
   1.213 +}
   1.214 +
   1.215 +/* Set device function to call the correct versions for running
   1.216 +   in a wimp window */
   1.217 +
   1.218 +void WIMP_SetDeviceMode(_THIS)
   1.219 +{
   1.220 +	if (this->UpdateRects == WIMP_UpdateRects) return; /* Already set up */
   1.221 +
   1.222 +	this->SetColors   = WIMP_SetColors;
   1.223 +	this->UpdateRects = WIMP_UpdateRects;
   1.224 +
   1.225 +	this->FlipHWSurface = NULL;
   1.226 +
   1.227 +	this->SetCaption = WIMP_SetWMCaption;
   1.228 +	this->SetIcon = NULL;
   1.229 +	this->IconifyWindow = NULL;
   1.230 +	
   1.231 +	this->ShowWMCursor = WIMP_ShowWMCursor;
   1.232 +	this->WarpWMCursor = WIMP_WarpWMCursor;
   1.233 +
   1.234 +        this->ToggleFullScreen = RISCOS_ToggleFullScreen;
   1.235 +
   1.236 +	this->PumpEvents = WIMP_PumpEvents;	
   1.237 +}
   1.238 +
   1.239 +/* Setup the Window to display the surface */
   1.240 +unsigned int WIMP_SetupWindow(_THIS, SDL_Surface *surface)
   1.241 +{
   1.242 +	_kernel_swi_regs regs;
   1.243 +	int window_data[23];
   1.244 +    int	*window_block = window_data+1;
   1.245 +	int x = (this->hidden->screen_width - surface->w) / 2;
   1.246 +	int y = (this->hidden->screen_height - surface->h) / 2;
   1.247 +	int xeig = this->hidden->xeig;
   1.248 +	int yeig = this->hidden->yeig;
   1.249 +
   1.250 +    mouseInWindow = 0;
   1.251 +    
   1.252 +	/* Always delete the window and recreate on a change */
   1.253 +	if (this->hidden->window_handle) WIMP_DeleteWindow(this);
   1.254 +
   1.255 +	/* Setup window co-ordinates */
   1.256 +   window_block[0] = x << xeig;
   1.257 +   window_block[1] = y << yeig;
   1.258 +   window_block[2] = window_block[0] + (surface->w << xeig);
   1.259 +   window_block[3] = window_block[1] + (surface->h << yeig);
   1.260 +
   1.261 +   
   1.262 +   window_block[4] = 0;				  /* Scroll offsets */
   1.263 +   window_block[5] = 0;
   1.264 +   window_block[6] = -1;			  /* Open on top of window stack */
   1.265 +
   1.266 +   window_block[7] = 0x85040042;      /* Window flags */
   1.267 +   if (riscos_closeaction != 0) window_block[7] |= 0x2000000;
   1.268 +
   1.269 +   /* TODO: Take into account surface->flags */
   1.270 +
   1.271 +   window_block[8] = 0xff070207;      /* Window colours */
   1.272 +   window_block[9] = 0x000c0103;
   1.273 +   window_block[10] = 0;                    /* Work area minimum */
   1.274 +   window_block[11] = -surface->h << yeig;
   1.275 +   window_block[12] = surface->w << xeig;   /* Work area maximum */
   1.276 +   window_block[13] = 0;
   1.277 +   window_block[14] = 0x2700013d;    /* Title icon flags */
   1.278 +   window_block[15] = 0x00003000;	 /* Work area flags - Mouse click down reported */
   1.279 +   window_block[16] = 1;             /* Sprite area control block pointer */
   1.280 +   window_block[17] = 0x00100010;	 /* Minimum window size (width & height) (16x16)*/
   1.281 +   window_block[18] = (int)this->hidden->title;    /* Title data */
   1.282 +   window_block[19] = -1;
   1.283 +   window_block[20] = 256;
   1.284 +   window_block[21] = 0;			 /* Number of icons */
   1.285 +
   1.286 +   regs.r[1] = (unsigned int)(window_block);
   1.287 +   
   1.288 +   /* Create the window */
   1.289 +   if (_kernel_swi(Wimp_CreateWindow, &regs, &regs) == NULL)
   1.290 +   {
   1.291 +	   this->hidden->window_handle = window_data[0] = regs.r[0];
   1.292 +
   1.293 +	   /* Show the window on the screen */
   1.294 +	   regs.r[1] = (unsigned int)window_data;
   1.295 +       if (_kernel_swi(Wimp_OpenWindow, &regs, &regs) == NULL)
   1.296 +       {
   1.297 +          WIMP_SetFocus(this->hidden->window_handle);
   1.298 +       } else
   1.299 +       {
   1.300 +		  WIMP_DeleteWindow(this);
   1.301 +	   }
   1.302 +   }
   1.303 +   
   1.304 +   return this->hidden->window_handle;
   1.305 +}
   1.306 +
   1.307 +/* Destroy the Window */
   1.308 +
   1.309 +void WIMP_DeleteWindow(_THIS)
   1.310 +{
   1.311 +	_kernel_swi_regs regs;
   1.312 +    regs.r[1] = (unsigned int)&(this->hidden->window_handle);
   1.313 +	_kernel_swi(Wimp_DeleteWindow, &regs, &regs);
   1.314 +	this->hidden->window_handle = 0;
   1.315 +}
   1.316 +
   1.317 +
   1.318 +void WIMP_UpdateRects(_THIS, int numrects, SDL_Rect *rects)
   1.319 +{
   1.320 +	_kernel_swi_regs regs;
   1.321 +	int update_block[12];
   1.322 +	int xeig = this->hidden->xeig;
   1.323 +	int yeig = this->hidden->yeig;
   1.324 +	int j;
   1.325 +	update_block[0] = this->hidden->window_handle;
   1.326 +
   1.327 +	for (j = 0; j < numrects; j++)
   1.328 +	{
   1.329 +		update_block[1] = rects[j].x << xeig; /* Min X */
   1.330 +		update_block[4] = -(rects[j].y << yeig);
   1.331 +		update_block[3] = update_block[1] + (rects[j].w << xeig);
   1.332 +		update_block[2] = update_block[4] - (rects[j].h << yeig);
   1.333 +
   1.334 +		regs.r[1] = (int)update_block;
   1.335 +		/* Update window can fail if called before first poll */
   1.336 +		if (_kernel_swi(Wimp_UpdateWindow, &regs, &regs) == 0)
   1.337 +		{
   1.338 +			while (regs.r[0])
   1.339 +			{
   1.340 +				WIMP_PlotSprite(this, update_block[1], update_block[2]);
   1.341 +				_kernel_swi(Wimp_GetRectangle, &regs, &regs);
   1.342 +			}
   1.343 +		}
   1.344 +	}
   1.345 +}
   1.346 +
   1.347 +
   1.348 +int WIMP_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
   1.349 +{
   1.350 +   unsigned int *pal = (unsigned int *)(this->hidden->bank[1]+60);
   1.351 +   int j;
   1.352 +   SDL_Rect update;
   1.353 +
   1.354 +   pal += firstcolor*2;
   1.355 +   for (j = 0; j < ncolors; j++)
   1.356 +   {
   1.357 +      *pal = (((unsigned int)colors->r) << 8)
   1.358 +             + (((unsigned int)colors->g) << 16)
   1.359 +             + (((unsigned int)colors->b) << 24);
   1.360 +      pal[1] = *pal;
   1.361 +      pal += 2;
   1.362 +      colors++;
   1.363 +   }
   1.364 +
   1.365 +   WIMP_SetupPlotInfo(this);
   1.366 +
   1.367 +   /* Need to refresh the window */
   1.368 +   update.x = 0;
   1.369 +   update.y = 0;
   1.370 +   update.w = SDL_VideoSurface->w;
   1.371 +   update.h = SDL_VideoSurface->h;
   1.372 +   WIMP_UpdateRects(this, 1, &update);
   1.373 +      
   1.374 +	return 1;
   1.375 +}
   1.376 +
   1.377 +void WIMP_SetWMCaption(_THIS, const char *title, const char *icon)
   1.378 +{
   1.379 +	_kernel_swi_regs regs;
   1.380 +
   1.381 +	strncpy(this->hidden->title, title, 255);
   1.382 +	this->hidden->title[255] = 0;
   1.383 +
   1.384 +	if (RISCOS_GetWimpVersion() < 380)
   1.385 +	{
   1.386 +		int block[6];
   1.387 +
   1.388 +		regs.r[1] = (int)block;
   1.389 +		_kernel_swi(Wimp_GetCaretPosition, &regs, &regs);
   1.390 +		if (block[0] == (int)this->hidden->window_handle)
   1.391 +		{
   1.392 +			regs.r[0] = -1;
   1.393 +			_kernel_swi(Wimp_SetCaretPosition, &regs,&regs);
   1.394 +		} else
   1.395 +		{
   1.396 +			regs.r[0] = this->hidden->window_handle;
   1.397 +			regs.r[1] = -1;
   1.398 +			regs.r[2] = -1;
   1.399 +			regs.r[3] = -1;
   1.400 +			_kernel_swi(Wimp_SetCaretPosition, &regs,&regs);
   1.401 +		}
   1.402 +		regs.r[0] = block[0];
   1.403 +		regs.r[1] = block[1];
   1.404 +		regs.r[2] = block[2];
   1.405 +		regs.r[3] = block[3];
   1.406 +		regs.r[4] = block[4];
   1.407 +		regs.r[5] = block[5];
   1.408 +		_kernel_swi(Wimp_SetCaretPosition, &regs,&regs);
   1.409 +	} else
   1.410 +	{
   1.411 +		regs.r[0] = this->hidden->window_handle;
   1.412 +		regs.r[1] = 0x4b534154; /* "TASK" */
   1.413 +		regs.r[2] = 3; /* Redraw title */
   1.414 +		_kernel_swi(Wimp_ForceRedraw, &regs, &regs);
   1.415 +	}
   1.416 +}
   1.417 +
   1.418 +void WIMP_RefreshDesktop(_THIS)
   1.419 +{
   1.420 +   int width = this->hidden->screen_width << this->hidden->xeig;
   1.421 +   int height = this->hidden->screen_height << this->hidden->yeig;
   1.422 +   _kernel_swi_regs regs;
   1.423 +   regs.r[0] = -1; /* Whole screen */
   1.424 +   regs.r[1] = 0;
   1.425 +   regs.r[2] = 0;
   1.426 +   regs.r[3] = width;
   1.427 +   regs.r[4] = height;
   1.428 +   _kernel_swi(Wimp_ForceRedraw, &regs, &regs);
   1.429 +}
   1.430 +
   1.431 +/* Toggle to window from full screen */
   1.432 +int WIMP_ToggleFromFullScreen(_THIS)
   1.433 +{     
   1.434 +   int width = this->screen->w;
   1.435 +   int height = this->screen->h;
   1.436 +   int bpp = this->screen->format->BitsPerPixel;
   1.437 +   char *buffer = NULL;
   1.438 +   char *old_bank[2];
   1.439 +   char *old_alloc_bank;
   1.440 +
   1.441 +   /* Ensure flags are OK */
   1.442 +   this->screen->flags &= ~(SDL_DOUBLEBUF|SDL_HWSURFACE);
   1.443 +
   1.444 +   if (this->hidden->bank[0] == this->hidden->alloc_bank || riscos_backbuffer == 0)
   1.445 +   {
   1.446 +      /* Need to create a sprite for the screen and copy the data to it */
   1.447 +      char *data;
   1.448 +      buffer = WIMP_CreateBuffer(width, height, bpp);
   1.449 +      data = buffer + 60;         /* Start of sprite data */
   1.450 +      if (bpp == 8) data += 2048;  /* 8bpp sprite have palette first */
   1.451 +
   1.452 +      if (buffer == NULL) return 0;
   1.453 +      memcpy(data, this->hidden->bank[0], width * height * this->screen->format->BytesPerPixel);
   1.454 +   }
   1.455 +   /* else We've switch to full screen before so we already have a sprite */
   1.456 +
   1.457 +   old_bank[0] = this->hidden->bank[0];
   1.458 +   old_bank[1] = this->hidden->bank[1];
   1.459 +   old_alloc_bank = this->hidden->alloc_bank;
   1.460 +
   1.461 +   if (buffer != NULL) this->hidden->alloc_bank = buffer;
   1.462 +
   1.463 +   this->hidden->bank[1] = this->hidden->alloc_bank;
   1.464 +   this->hidden->bank[0] = this->hidden->bank[1] + 60; /* Start of sprite data */
   1.465 +   if (bpp == 8) this->hidden->bank[0] += 2048; /* 8bpp sprite have palette first */
   1.466 +
   1.467 +   this->hidden->current_bank = 0;
   1.468 +   this->screen->pixels = this->hidden->bank[0];
   1.469 +
   1.470 +   RISCOS_RestoreWimpMode();
   1.471 +   WIMP_ReadModeInfo(this);
   1.472 +   if (WIMP_SetupWindow(this, this->screen))
   1.473 +   {
   1.474 +      WIMP_SetDeviceMode(this);
   1.475 +      WIMP_SetupPlotInfo(this);
   1.476 +
   1.477 +      riscos_backbuffer = 1;
   1.478 +
   1.479 +      if (buffer && old_alloc_bank) free(old_alloc_bank);
   1.480 +
   1.481 +      return 1;
   1.482 +   } else
   1.483 +   {
   1.484 +      /* Drop back to full screen mode on failure */
   1.485 +      this->hidden->bank[0] = old_bank[0];
   1.486 +      this->hidden->bank[1] = old_bank[1];
   1.487 +      this->hidden->alloc_bank = old_alloc_bank;
   1.488 +      if (buffer) free(buffer);
   1.489 +      
   1.490 +      RISCOS_StoreWimpMode();
   1.491 +      FULLSCREEN_SetMode(width, height, bpp);
   1.492 +   }
   1.493 +
   1.494 +   return 0;
   1.495 +}