19682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
29682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL - Simple DirectMedia Layer
39682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Copyright (C) 1997-2012 Sam Lantinga
49682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
59682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is free software; you can redistribute it and/or
69682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    modify it under the terms of the GNU Library General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2 of the License, or (at your option) any later version.
99682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    This library is distributed in the hope that it will be useful,
119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    but WITHOUT ANY WARRANTY; without even the implied warranty of
129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Library General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Library General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     File added by Alan Buckley (alan_baa@hotmail.com) for RISC OS compatability
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	 27 March 2003
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     Implements Pumping of events and WIMP polling
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL.h"
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_syswm.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_sysevents.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_riscosvideo.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_riscosevents_c.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_riscosmouse_c.h"
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../timer/SDL_timer_c.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "memory.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "stdlib.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "ctype.h"
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "kernel.h"
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "swis.h"
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "unixlib/os.h"
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !SDL_THREADS_DISABLED
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <pthread.h>
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Local functions */
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid WIMP_Poll(_THIS, int waitTime);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid WIMP_SetFocus(int win);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* SDL_riscossprite functions */
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid WIMP_PlotSprite(_THIS, int x, int y);
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid WIMP_ModeChanged(_THIS);
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid WIMP_PaletteChanged(_THIS);
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern void WIMP_PollMouse(_THIS);
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern void RISCOS_PollKeyboard();
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_THREADS_DISABLED
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Timer running function */
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern void RISCOS_CheckTimer();
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern int riscos_using_threads;
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Mouse cursor handling */
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern void WIMP_ReshowCursor(_THIS);
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallextern void WIMP_RestoreWimpCursor();
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint hasFocus = 0;
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint mouseInWindow = 0;
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Flag to ensure window is correct size after a mode change */
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int resizeOnOpen = 0;
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid WIMP_PumpEvents(_THIS)
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WIMP_Poll(this, 0);
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (hasFocus) RISCOS_PollKeyboard();
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (mouseInWindow) WIMP_PollMouse(this);
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_THREADS_DISABLED
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_timer_running) RISCOS_CheckTimer();
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid WIMP_Poll(_THIS, int waitTime)
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	_kernel_swi_regs regs;
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int message[64];
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	unsigned int code;
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int pollMask = 0;
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int doPoll = 1;
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int sysEvent;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int sdlWindow = this->hidden->window_handle;
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (this->PumpEvents != WIMP_PumpEvents) return;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (waitTime > 0)
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		_kernel_swi(OS_ReadMonotonicTime, &regs, &regs);
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		waitTime += regs.r[0];
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    while (doPoll)
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !SDL_THREADS_DISABLED
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       /* Stop thread callbacks while program is paged out */
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       if (riscos_using_threads) __pthread_stop_ticker();
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        if (waitTime <= 0)
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	regs.r[0] = pollMask; /* Poll Mask */
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	/* For no wait time mask out null event so we wait until something happens */
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	if (waitTime < 0) regs.r[0] |= 1;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	regs.r[1] = (int)message;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	_kernel_swi(Wimp_Poll, &regs, &regs);
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        } else
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	regs.r[0] = pollMask;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	regs.r[1] = (int)message;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	regs.r[2] = waitTime;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	_kernel_swi(Wimp_PollIdle, &regs, &regs);
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		/* Flag to specify if we post a SDL_SysWMEvent */
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sysEvent = 0;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        code = (unsigned int)regs.r[0];
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	switch(code)
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case 0:  /* Null Event - drop out for standard processing*/
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   doPoll = 0;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   break;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case 1:     /* Redraw window */
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           _kernel_swi(Wimp_RedrawWindow, &regs,&regs);
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   if (message[0] == sdlWindow)
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   {
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 while (regs.r[0])
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 {
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           	    WIMP_PlotSprite(this, message[1], message[2]);
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           	    _kernel_swi(Wimp_GetRectangle, &regs, &regs);
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                 }
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	   } else
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  {
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	/* TODO: Currently we just eat them - we may need to pass them on */
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	while (regs.r[0])
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	{
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                        _kernel_swi(Wimp_GetRectangle, &regs, &regs);
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	}
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  }
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          break;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 2:		/* Open window */
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   if ( resizeOnOpen && message[0] == sdlWindow)
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   {
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      /* Ensure window is correct size */
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      resizeOnOpen = 0;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      message[3] = message[1] + (this->screen->w << this->hidden->xeig);
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      message[4] = message[2] + (this->screen->h << this->hidden->yeig);
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   }
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	_kernel_swi(Wimp_OpenWindow, &regs, &regs);
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       	    break;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 3:		/* Close window */
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (message[0] == sdlWindow)
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* Documentation makes it looks as if the following line is correct:
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				**    if (SDL_PrivateQuit() == 1) _kernel_swi(Wimp_CloseWindow, &regs, &regs);
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				** However some programs don't process this message and so sit there invisibly
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				** in the background so I just post the quit message and hope the application
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				** does the correct thing.
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				*/
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateQuit();
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				sysEvent = 1;
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	doPoll = 0;
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	break;
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 4: /* Pointer_Leaving_Window */
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (message[0] == sdlWindow)
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				mouseInWindow = 0;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				//TODO: Lose buttons / dragging
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				 /* Reset to default pointer */
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				 WIMP_RestoreWimpCursor();
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				 SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				sysEvent = 1;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 5: /* Pointer_Entering_Window */
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (message[0] == sdlWindow)
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				mouseInWindow = 1;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				WIMP_ReshowCursor(this);
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else sysEvent = 1;
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 6:		/* Mouse_Click */
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (hasFocus == 0)
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			   /* First click gives focus if it's not a menu */
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			   /* we only count non-menu clicks on a window that has the focus */
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			   WIMP_SetFocus(message[3]);
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			} else
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				doPoll = 0; // So PollMouse gets a chance to pick it up
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   break;
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 7: /* User_Drag_Box - Used for mouse release */
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			//TODO: May need to implement this in the future
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			sysEvent = 1;
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 8: /* Keypressed */
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			doPoll = 0; /* PollKeyboard should pick it up */
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (message[0] != sdlWindow) sysEvent = 1;
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/*TODO: May want to always pass F12 etc to the wimp
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				regs.r[0] = message[6];
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				_kernel_swi(Wimp_ProcessKey, &regs, &regs);
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			*/
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 11: /* Lose Caret */
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 hasFocus = 0;
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 if (message[0] == sdlWindow) SDL_PrivateAppActive(0, SDL_APPINPUTFOCUS);
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 else sysEvent = 1;
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 break;
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 12: /* Gain Caret */
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 hasFocus = 1;
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 if (message[0] == sdlWindow) SDL_PrivateAppActive(1, SDL_APPINPUTFOCUS);
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 else sysEvent = 1;
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 break;
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 17:
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		case 18:
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			sysEvent = 1; /* All messages are passed on */
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			switch(message[4])
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			{
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 0: /* Quit Event */
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				/* No choice - have to quit */
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			   SDL_Quit();
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        	   exit(0);
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			   break;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 8: /* Pre Quit */
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				SDL_PrivateQuit();
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 0x400c1: /* Mode change */
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				WIMP_ModeChanged(this);
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				resizeOnOpen = 1;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			case 9:      /* Palette changed */
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				WIMP_PaletteChanged(this);
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				break;
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			}
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		default:
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Pass unknown events on */
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			sysEvent = 1;
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (sysEvent)
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	        SDL_SysWMmsg wmmsg;
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_VERSION(&wmmsg.version);
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			wmmsg.eventCode = code;
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			SDL_memcpy(wmmsg.pollBlock, message, 64 * sizeof(int));
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* Fall out of polling loop if message is successfully posted */
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			if (SDL_PrivateSysWMEvent(&wmmsg)) doPoll = 0;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if !SDL_THREADS_DISABLED
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		if (riscos_using_threads)
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		{
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                   /* Restart ticker here so other thread can not interfere
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                      with the Redraw processing */
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   if (riscos_using_threads) __pthread_start_ticker();
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                   /* Give other threads a better chance of running */
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		   pthread_yield();
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		}
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Set focus to specified window */
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid WIMP_SetFocus(int win)
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	_kernel_swi_regs regs;
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	regs.r[0] = win;
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	regs.r[1] = -1; /* Icon handle */
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	regs.r[2] = 0;  /* X-offset we just put it at position 0 */
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	regs.r[3] = 0;  /* Y-offset as above */
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	regs.r[4] = 1 << 25; /* Caret is invisible */
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	regs.r[5] = 0;  /* index into string */
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	_kernel_swi(Wimp_SetCaretPosition, &regs, &regs);
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/** Run background task while in a sleep command */
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid RISCOS_BackgroundTasks(void)
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (current_video && current_video->hidden->window_handle)
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		WIMP_Poll(current_video, 0);
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if SDL_THREADS_DISABLED
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	if (SDL_timer_running) RISCOS_CheckTimer();
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
331