src/video/riscos/SDL_wimpvideo.c
author Cameron Cawley <ccawley2011@gmail.com>
Tue, 18 Jun 2019 23:55:01 +0100
branchSDL-1.2
changeset 12913 d07b4feae69c
parent 12786 4e73be7b4787
permissions -rw-r--r--
riscos: Improve error reporting
     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 Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 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     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /*
    25      File added by Alan Buckley (alan_baa@hotmail.com) for RISC OS compatability
    26 	 27 March 2003
    27 
    28      Implements RISC OS Wimp display.
    29 */
    30 
    31 #include "SDL_video.h"
    32 #include "SDL_mouse.h"
    33 #include "SDL_timer.h"
    34 #include "../SDL_sysvideo.h"
    35 #include "../SDL_pixels_c.h"
    36 #include "../../events/SDL_events_c.h"
    37 
    38 #include "SDL_riscostask.h"
    39 #include "SDL_riscosvideo.h"
    40 #include "SDL_riscosevents_c.h"
    41 #include "SDL_riscosmouse_c.h"
    42 
    43 #include "kernel.h"
    44 #include "swis.h"
    45 
    46 static int WIMP_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
    47 static void WIMP_SetWMCaption(_THIS, const char *title, const char *icon);
    48 static void WIMP_UpdateRects(_THIS, int numrects, SDL_Rect *rects);
    49 
    50 /* RISC OS Wimp handling helpers */
    51 static unsigned int WIMP_SetupWindow(_THIS, SDL_Surface *surface);
    52 static void WIMP_SetDeviceMode(_THIS);
    53 
    54 
    55 SDL_Surface *WIMP_SetVideoMode(_THIS, SDL_Surface *current,
    56 				int width, int height, int bpp, Uint32 flags)
    57 {
    58 	unsigned char *buffer = NULL;
    59 	int bytesPerPixel;
    60 	const RISCOS_SDL_PixelFormat *fmt;
    61 
    62 	/* Don't support double buffering in Wimp mode */
    63 	flags &= ~SDL_DOUBLEBUF;
    64 	flags &= ~SDL_HWSURFACE;
    65 
    66 	/* Identify the current pixel format */
    67 	fmt = RISCOS_CurrentPixelFormat();
    68 	/* If it's the same (approximate) BPP as the desired BPP, use it directly (less overhead in sprite rendering) */
    69 	if ((fmt == NULL) || (fmt->sdl_bpp != ((bpp+1)&~1)))
    70 	{
    71 		/* Not a good match - look for a supported format which is correct */
    72 		fmt = WIMP_FindSupportedSpriteFormat(bpp);
    73 		if (fmt == NULL)
    74 		{
    75 			SDL_SetError("Pixel depth %d not supported", bpp);
    76 			return NULL;
    77 		}
    78 	}
    79 
    80 	if (fmt->sdl_bpp == 8)
    81 	{
    82 		/* Emulated palette using ColourTrans */
    83 		flags |= SDL_HWPALETTE;
    84 	}
    85 	bytesPerPixel = 1 << (fmt->ro.log2bpp - 3);
    86 
    87 /* 	printf("Setting mode %dx%d\n", width, height);*/
    88 
    89 	/* Allocate the new pixel format for the screen */
    90 	if ( ! SDL_ReallocFormat(current, fmt->sdl_bpp, fmt->rmask, fmt->gmask, fmt->bmask, 0) ) {
    91 		return(NULL);
    92 	}
    93 
    94 	/* Set up the new mode framebuffer */
    95 	current->w = width;
    96 	this->hidden->height = current->h = height;
    97 
    98 	if (bpp == 15) bpp = 16;
    99 	buffer = WIMP_CreateBuffer(width, height, &fmt->ro);
   100 	if (buffer == NULL)
   101 	{
   102 		return (NULL);
   103 	}
   104 
   105 	this->hidden->bank[0] = buffer + 60; /* Start of sprite data */
   106 	if (bpp == 8) this->hidden->bank[0] += 2048; /* 8bpp sprite have palette first */
   107 
   108 	this->hidden->bank[1] = buffer;      /* Start of buffer */
   109 
   110 	/* Remember sprite buffer so it can be freed later */
   111 	if (this->hidden->alloc_bank) SDL_free(this->hidden->alloc_bank);
   112 	this->hidden->alloc_bank = buffer;
   113 
   114 	current->pitch = width * bytesPerPixel;
   115 	if ((current->pitch & 3))
   116 	{
   117 		/* Sprites are 32bit word aligned */
   118 		current->pitch += (4 - (current->pitch & 3));
   119 	}
   120 	this->hidden->format = fmt;
   121 
   122   	current->flags = flags | SDL_PREALLOC;
   123 
   124 	WIMP_ReadModeInfo(this);
   125 	
   126     SDL_memset(this->hidden->bank[0], 0, height * current->pitch);
   127 
   128 	this->hidden->current_bank = 0;
   129 	current->pixels = this->hidden->bank[0];
   130 
   131 
   132 	if (WIMP_SetupWindow(this, current) == 0)
   133 	{
   134 		return NULL;
   135 	}
   136 
   137 	/* Reset device functions for the wimp */
   138 	WIMP_SetDeviceMode(this);
   139 
   140 	/* Needs to set up plot info after window has been created */
   141 	/* Not sure why, but plots don't work if I do it earlier */
   142 	WIMP_SetupPlotInfo(this);
   143 
   144 	/* Poll until window is shown */
   145 	{
   146 	   /* We wait until it gets the focus, but give up after 5 seconds
   147 	      in case the focus is prevented in any way.
   148 	   */
   149 	   Uint32 now = SDL_GetTicks();
   150 	   while (!hasFocus && SDL_GetTicks() - now < 5000)
   151 	   {
   152 	      WIMP_Poll(this, 0);
   153 	   }
   154 	}
   155 
   156 	/* We're done */
   157 	return(current);
   158 }
   159 
   160 
   161 void WIMP_ReadModeInfo(_THIS)
   162 {
   163 	_kernel_swi_regs regs;
   164 	int vars[6];
   165 	int vals[5];
   166 
   167 	vars[0] = 4;  /* XEig */
   168 	vars[1] = 5;  /* YEig */
   169 	vars[2] = 9;  /* Log base 2 bpp */
   170 	vars[3] = 11; /* Screen Width - 1 */
   171 	vars[4] = 12; /* Screen Depth - 1 */
   172 	vars[5] = -1; /* Terminate list */
   173 
   174 	regs.r[0] = (int)vars;
   175 	regs.r[1] = (int)vals;
   176 	_kernel_swi(OS_ReadVduVariables, &regs, &regs);
   177 	this->hidden->xeig = vals[0];
   178 	this->hidden->yeig = vals[1];
   179 	this->hidden->screen_width = vals[3] + 1;
   180 	this->hidden->screen_height = vals[4] + 1;
   181 }
   182 
   183 /* Set device function to call the correct versions for running
   184    in a wimp window */
   185 
   186 void WIMP_SetDeviceMode(_THIS)
   187 {
   188 	if (this->UpdateRects == WIMP_UpdateRects) return; /* Already set up */
   189 
   190 	this->SetColors   = WIMP_SetColors;
   191 	this->UpdateRects = WIMP_UpdateRects;
   192 
   193 	this->FlipHWSurface = NULL;
   194 
   195 	this->SetCaption = WIMP_SetWMCaption;
   196 	this->SetIcon = NULL;
   197 	this->IconifyWindow = NULL;
   198 	
   199 	this->ShowWMCursor = WIMP_ShowWMCursor;
   200 	this->WarpWMCursor = WIMP_WarpWMCursor;
   201 
   202 /* Currently need to set this up here as it only works if you
   203    start up in a Wimp mode */
   204         this->ToggleFullScreen = RISCOS_ToggleFullScreen;
   205 
   206 	this->PumpEvents = WIMP_PumpEvents;	
   207 }
   208 
   209 /* Setup the Window to display the surface */
   210 unsigned int WIMP_SetupWindow(_THIS, SDL_Surface *surface)
   211 {
   212 	_kernel_swi_regs regs;
   213 	_kernel_oserror *error;
   214 	int window_data[23];
   215     int	*window_block = window_data+1;
   216 	int x = ((this->hidden->screen_width << this->hidden->xeig) - (surface->w << 1)) / 2;
   217 	int y = ((this->hidden->screen_height << this->hidden->yeig) - (surface->h << 1)) / 2;    
   218 
   219 	/* Always delete the window and recreate on a change */
   220 	if (this->hidden->window_handle) WIMP_DeleteWindow(this);
   221 
   222 	/* Setup window co-ordinates */
   223    window_block[0] = x;
   224    window_block[1] = y;
   225    window_block[2] = window_block[0] + (surface->w << 1);
   226    window_block[3] = window_block[1] + (surface->h << 1);
   227 
   228    
   229    window_block[4] = 0;				  /* Scroll offsets */
   230    window_block[5] = 0;
   231    window_block[6] = -1;			  /* Open on top of window stack */
   232 
   233    window_block[7] = 0x85040042;      /* Window flags */
   234    if (riscos_closeaction != 0) window_block[7] |= 0x2000000;
   235 
   236    /* TODO: Take into account surface->flags */
   237 
   238    window_block[8] = 0xff070207;      /* Window colours */
   239    window_block[9] = 0x000c0103;
   240    window_block[10] = 0;                    /* Work area minimum */
   241    window_block[11] = -surface->h << 1;
   242    window_block[12] = surface->w << 1;   /* Work area maximum */
   243    window_block[13] = 0;
   244    window_block[14] = 0x2700013d;    /* Title icon flags */
   245    window_block[15] = 0x00003000;	 /* Work area flags - Mouse click down reported */
   246    window_block[16] = 1;             /* Sprite area control block pointer */
   247    window_block[17] = 0x00100010;	 /* Minimum window size (width & height) (16x16)*/
   248    window_block[18] = (int)this->hidden->title;    /* Title data */
   249    window_block[19] = -1;
   250    window_block[20] = 256;
   251    window_block[21] = 0;			 /* Number of icons */
   252 
   253    regs.r[1] = (unsigned int)(window_block);
   254    
   255    /* Create the window */
   256    error = _kernel_swi(Wimp_CreateWindow, &regs, &regs);
   257    if (error == NULL)
   258    {
   259 	   this->hidden->window_handle = window_data[0] = regs.r[0];
   260 
   261 	   /* Show the window on the screen */
   262 	   regs.r[1] = (unsigned int)window_data;
   263        if (_kernel_swi(Wimp_OpenWindow, &regs, &regs) == NULL)
   264        {
   265           WIMP_SetFocus(this->hidden->window_handle);
   266        } else
   267        {
   268 		  WIMP_DeleteWindow(this);
   269 	   }
   270    } else {
   271        SDL_SetError("Unable to create window: %s (%i)", error->errmess, error->errnum);
   272    }
   273 
   274    return this->hidden->window_handle;
   275 }
   276 
   277 /* Destroy the Window */
   278 
   279 void WIMP_DeleteWindow(_THIS)
   280 {
   281 	_kernel_swi_regs regs;
   282     regs.r[1] = (unsigned int)&(this->hidden->window_handle);
   283 	_kernel_swi(Wimp_DeleteWindow, &regs, &regs);
   284 	this->hidden->window_handle = 0;
   285 }
   286 
   287 
   288 void WIMP_UpdateRects(_THIS, int numrects, SDL_Rect *rects)
   289 {
   290 	_kernel_swi_regs regs;
   291 	int update_block[12];
   292 	int j;
   293 	update_block[0] = this->hidden->window_handle;
   294 
   295 	for (j = 0; j < numrects; j++)
   296 	{
   297 		update_block[1] = rects[j].x << 1; /* Min X */
   298 		update_block[4] = -(rects[j].y << 1);
   299 		update_block[3] = update_block[1] + (rects[j].w << 1);
   300 		update_block[2] = update_block[4] - (rects[j].h << 1);
   301 
   302 		regs.r[1] = (int)update_block;
   303 		/* Update window can fail if called before first poll */
   304 		if (_kernel_swi(Wimp_UpdateWindow, &regs, &regs) == 0)
   305 		{
   306 			while (regs.r[0])
   307 			{
   308 				WIMP_PlotSprite(this, update_block[1], update_block[2]);
   309 				_kernel_swi(Wimp_GetRectangle, &regs, &regs);
   310 			}
   311 		}
   312 	}
   313 }
   314 
   315 
   316 int WIMP_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
   317 {
   318    unsigned int *pal = (unsigned int *)(this->hidden->bank[1]+60);
   319    int j;
   320    SDL_Rect update;
   321 
   322    pal += firstcolor*2;
   323    for (j = 0; j < ncolors; j++)
   324    {
   325       *pal = (((unsigned int)colors->r) << 8)
   326              + (((unsigned int)colors->g) << 16)
   327              + (((unsigned int)colors->b) << 24);
   328       pal[1] = *pal;
   329       pal += 2;
   330       colors++;
   331    }
   332 
   333    WIMP_SetupPlotInfo(this);
   334 
   335    /* Need to refresh the window */
   336    update.x = 0;
   337    update.y = 0;
   338    update.w = SDL_VideoSurface->w;
   339    update.h = SDL_VideoSurface->h;
   340    WIMP_UpdateRects(this, 1, &update);
   341       
   342 	return 1;
   343 }
   344 
   345 void WIMP_SetWMCaption(_THIS, const char *title, const char *icon)
   346 {
   347 	_kernel_swi_regs regs;
   348 
   349 	SDL_strlcpy(this->hidden->title, title, SDL_arraysize(this->hidden->title));
   350 
   351 	if (RISCOS_GetWimpVersion() < 380)
   352 	{
   353 		int block[6];
   354 
   355 		regs.r[1] = (int)block;
   356 		_kernel_swi(Wimp_GetCaretPosition, &regs, &regs);
   357 		if (block[0] == (int)this->hidden->window_handle)
   358 		{
   359 			regs.r[0] = -1;
   360 			_kernel_swi(Wimp_SetCaretPosition, &regs,&regs);
   361 		} else
   362 		{
   363 			regs.r[0] = this->hidden->window_handle;
   364 			regs.r[1] = -1;
   365 			regs.r[2] = -1;
   366 			regs.r[3] = -1;
   367 			_kernel_swi(Wimp_SetCaretPosition, &regs,&regs);
   368 		}
   369 		regs.r[0] = block[0];
   370 		regs.r[1] = block[1];
   371 		regs.r[2] = block[2];
   372 		regs.r[3] = block[3];
   373 		regs.r[4] = block[4];
   374 		regs.r[5] = block[5];
   375 		_kernel_swi(Wimp_SetCaretPosition, &regs,&regs);
   376 	} else
   377 	{
   378 		regs.r[0] = this->hidden->window_handle;
   379 		regs.r[1] = 0x4b534154; /* "TASK" */
   380 		regs.r[2] = 3; /* Redraw title */
   381 		_kernel_swi(Wimp_ForceRedraw, &regs, &regs);
   382 	}
   383 }
   384 
   385 /* Toggle to window from full screen */
   386 int WIMP_ToggleFromFullScreen(_THIS)
   387 {     
   388    int width = this->screen->w;
   389    int height = this->screen->h;
   390    int bpp = this->screen->format->BitsPerPixel;
   391    unsigned char *buffer = NULL;
   392    unsigned char *old_bank[2];
   393    unsigned char *old_alloc_bank;
   394 
   395    /* Ensure flags are OK */
   396    this->screen->flags &= ~(SDL_DOUBLEBUF|SDL_HWSURFACE);
   397 
   398    if (this->hidden->bank[0] == this->hidden->alloc_bank || riscos_backbuffer == 0)
   399    {
   400       /* Need to create a sprite for the screen and copy the data to it */
   401       unsigned char *data;
   402       buffer = WIMP_CreateBuffer(width, height, &this->hidden->format->ro);
   403       data = buffer + 60;         /* Start of sprite data */
   404       if (bpp == 8) data += 2048;  /* 8bpp sprite have palette first */
   405 
   406       if (buffer == NULL) return 0;
   407       SDL_memcpy(data, this->hidden->bank[0], width * height * this->screen->format->BytesPerPixel);
   408    }
   409    /* else We've switch to full screen before so we already have a sprite */
   410 
   411    old_bank[0] = this->hidden->bank[0];
   412    old_bank[1] = this->hidden->bank[1];
   413    old_alloc_bank = this->hidden->alloc_bank;
   414 
   415    if (buffer != NULL) this->hidden->alloc_bank = buffer;
   416 
   417    this->hidden->bank[1] = this->hidden->alloc_bank;
   418    this->hidden->bank[0] = this->hidden->bank[1] + 60; /* Start of sprite data */
   419    if (bpp == 8) this->hidden->bank[0] += 2048; /* 8bpp sprite have palette first */
   420 
   421    this->hidden->current_bank = 0;
   422    this->screen->pixels = this->hidden->bank[0];
   423 
   424    RISCOS_RestoreWimpMode();
   425    WIMP_ReadModeInfo(this);
   426    if (WIMP_SetupWindow(this, this->screen))
   427    {
   428       WIMP_SetDeviceMode(this);
   429       WIMP_SetupPlotInfo(this);
   430 
   431       if (riscos_backbuffer == 0) riscos_backbuffer = 1;
   432 
   433       if (buffer && old_alloc_bank) SDL_free(old_alloc_bank);
   434 
   435       return 1;
   436    } else
   437    {
   438       /* Drop back to full screen mode on failure */
   439       this->hidden->bank[0] = old_bank[0];
   440       this->hidden->bank[1] = old_bank[1];
   441       this->hidden->alloc_bank = old_alloc_bank;
   442       if (buffer) SDL_free(buffer);
   443       
   444       RISCOS_StoreWimpMode();
   445       FULLSCREEN_SetMode(width, height, bpp);
   446    }
   447 
   448    return 0;
   449 }