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