2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
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.
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.
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
22 #include "SDL_config.h"
25 File added by Alan Buckley (alan_baa@hotmail.com) for RISC OS compatability
28 Implements RISC OS Wimp display.
31 #include "SDL_video.h"
32 #include "SDL_mouse.h"
33 #include "../SDL_sysvideo.h"
34 #include "../SDL_pixels_c.h"
35 #include "../../events/SDL_events_c.h"
37 #include "SDL_riscostask.h"
38 #include "SDL_riscosvideo.h"
39 #include "SDL_riscosevents_c.h"
40 #include "SDL_riscosmouse_c.h"
45 /* Initialization/Query functions */
46 SDL_Rect **WIMP_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
47 SDL_Surface *WIMP_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
48 int WIMP_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
49 void WIMP_SetWMCaption(_THIS, const char *title, const char *icon);
52 extern unsigned char *WIMP_CreateBuffer(int width, int height, int bpp);
53 extern void WIMP_PumpEvents(_THIS);
54 extern void WIMP_PlotSprite(_THIS, int x, int y);
55 extern void WIMP_SetupPlotInfo(_THIS);
56 extern void WIMP_SetFocus(int win);
59 static void WIMP_UpdateRects(_THIS, int numrects, SDL_Rect *rects);
61 /* RISC OS Wimp handling helpers */
62 void WIMP_ReadModeInfo(_THIS);
63 unsigned int WIMP_SetupWindow(_THIS, SDL_Surface *surface);
64 void WIMP_SetDeviceMode(_THIS);
65 void WIMP_DeleteWindow(_THIS);
67 /* FULLSCREEN function required for wimp/fullscreen toggling */
68 extern int FULLSCREEN_SetMode(int width, int height, int bpp);
70 /* Currently need to set this up here as it only works if you
71 start up in a Wimp mode */
72 extern int RISCOS_ToggleFullScreen(_THIS, int fullscreen);
74 extern int riscos_backbuffer;
75 extern int mouseInWindow;
76 extern int riscos_closeaction;
78 /* Following needed to ensure window is shown immediately */
80 extern void WIMP_Poll(_THIS, int waitTime);
82 SDL_Surface *WIMP_SetVideoMode(_THIS, SDL_Surface *current,
83 int width, int height, int bpp, Uint32 flags)
89 int bytesPerPixel = 1;
91 /* Don't support double buffering in Wimp mode */
92 flags &= ~SDL_DOUBLEBUF;
93 flags &= ~SDL_HWSURFACE;
98 /* Emulated palette using ColourTrans */
99 flags |= SDL_HWPALETTE;
118 SDL_SetError("Pixel depth not supported");
123 /* printf("Setting mode %dx%d\n", width, height);*/
125 /* Allocate the new pixel format for the screen */
126 if ( ! SDL_ReallocFormat(current, bpp, Rmask, Gmask, Bmask, 0) ) {
127 SDL_SetError("Couldn't allocate new pixel format for requested mode");
131 /* Set up the new mode framebuffer */
133 this->hidden->height = current->h = height;
135 if (bpp == 15) bpp = 16;
136 buffer = WIMP_CreateBuffer(width, height, bpp);
139 SDL_SetError("Couldn't create sprite for video memory");
143 this->hidden->bank[0] = buffer + 60; /* Start of sprite data */
144 if (bpp == 8) this->hidden->bank[0] += 2048; /* 8bpp sprite have palette first */
146 this->hidden->bank[1] = buffer; /* Start of buffer */
148 /* Remember sprite buffer so it can be freed later */
149 if (this->hidden->alloc_bank) SDL_free(this->hidden->alloc_bank);
150 this->hidden->alloc_bank = buffer;
152 current->pitch = width * bytesPerPixel;
153 if ((current->pitch & 3))
155 /* Sprites are 32bit word aligned */
156 current->pitch += (4 - (current->pitch & 3));
159 current->flags = flags | SDL_PREALLOC;
161 WIMP_ReadModeInfo(this);
163 SDL_memset(this->hidden->bank[0], 0, height * current->pitch);
165 this->hidden->current_bank = 0;
166 current->pixels = this->hidden->bank[0];
169 if (WIMP_SetupWindow(this, current) == 0)
171 SDL_SetError("Unable to create window to display surface");
175 /* Reset device functions for the wimp */
176 WIMP_SetDeviceMode(this);
178 /* Needs to set up plot info after window has been created */
179 /* Not sure why, but plots don't work if I do it earlier */
180 WIMP_SetupPlotInfo(this);
182 /* Poll until window is shown */
184 /* We wait until it gets the focus, but give up after 5 seconds
185 in case the focus is prevented in any way.
187 Uint32 now = SDL_GetTicks();
188 while (!hasFocus && SDL_GetTicks() - now < 5000)
199 void WIMP_ReadModeInfo(_THIS)
201 _kernel_swi_regs regs;
205 vars[0] = 4; /* XEig */
206 vars[1] = 5; /* YEig */
207 vars[2] = 9; /* Log base 2 bpp */
208 vars[3] = 11; /* Screen Width - 1 */
209 vars[4] = 12; /* Screen Depth - 1 */
210 vars[5] = -1; /* Terminate list */
212 regs.r[0] = (int)vars;
213 regs.r[1] = (int)vals;
214 _kernel_swi(OS_ReadVduVariables, ®s, ®s);
215 this->hidden->xeig = vals[0];
216 this->hidden->yeig = vals[1];
217 this->hidden->screen_bpp = 1 << vals[2];
218 this->hidden->screen_width = vals[3] + 1;
219 this->hidden->screen_height = vals[4] + 1;
222 /* Set device function to call the correct versions for running
225 void WIMP_SetDeviceMode(_THIS)
227 if (this->UpdateRects == WIMP_UpdateRects) return; /* Already set up */
229 this->SetColors = WIMP_SetColors;
230 this->UpdateRects = WIMP_UpdateRects;
232 this->FlipHWSurface = NULL;
234 this->SetCaption = WIMP_SetWMCaption;
235 this->SetIcon = NULL;
236 this->IconifyWindow = NULL;
238 this->ShowWMCursor = WIMP_ShowWMCursor;
239 this->WarpWMCursor = WIMP_WarpWMCursor;
241 this->ToggleFullScreen = RISCOS_ToggleFullScreen;
243 this->PumpEvents = WIMP_PumpEvents;
246 /* Setup the Window to display the surface */
247 unsigned int WIMP_SetupWindow(_THIS, SDL_Surface *surface)
249 _kernel_swi_regs regs;
251 int *window_block = window_data+1;
252 int x = (this->hidden->screen_width - surface->w) / 2;
253 int y = (this->hidden->screen_height - surface->h) / 2;
254 int xeig = this->hidden->xeig;
255 int yeig = this->hidden->yeig;
259 /* Always delete the window and recreate on a change */
260 if (this->hidden->window_handle) WIMP_DeleteWindow(this);
262 /* Setup window co-ordinates */
263 window_block[0] = x << xeig;
264 window_block[1] = y << yeig;
265 window_block[2] = window_block[0] + (surface->w << xeig);
266 window_block[3] = window_block[1] + (surface->h << yeig);
269 window_block[4] = 0; /* Scroll offsets */
271 window_block[6] = -1; /* Open on top of window stack */
273 window_block[7] = 0x85040042; /* Window flags */
274 if (riscos_closeaction != 0) window_block[7] |= 0x2000000;
276 /* TODO: Take into account surface->flags */
278 window_block[8] = 0xff070207; /* Window colours */
279 window_block[9] = 0x000c0103;
280 window_block[10] = 0; /* Work area minimum */
281 window_block[11] = -surface->h << yeig;
282 window_block[12] = surface->w << xeig; /* Work area maximum */
283 window_block[13] = 0;
284 window_block[14] = 0x2700013d; /* Title icon flags */
285 window_block[15] = 0x00003000; /* Work area flags - Mouse click down reported */
286 window_block[16] = 1; /* Sprite area control block pointer */
287 window_block[17] = 0x00100010; /* Minimum window size (width & height) (16x16)*/
288 window_block[18] = (int)this->hidden->title; /* Title data */
289 window_block[19] = -1;
290 window_block[20] = 256;
291 window_block[21] = 0; /* Number of icons */
293 regs.r[1] = (unsigned int)(window_block);
295 /* Create the window */
296 if (_kernel_swi(Wimp_CreateWindow, ®s, ®s) == NULL)
298 this->hidden->window_handle = window_data[0] = regs.r[0];
300 /* Show the window on the screen */
301 regs.r[1] = (unsigned int)window_data;
302 if (_kernel_swi(Wimp_OpenWindow, ®s, ®s) == NULL)
304 WIMP_SetFocus(this->hidden->window_handle);
307 WIMP_DeleteWindow(this);
311 return this->hidden->window_handle;
314 /* Destroy the Window */
316 void WIMP_DeleteWindow(_THIS)
318 _kernel_swi_regs regs;
319 regs.r[1] = (unsigned int)&(this->hidden->window_handle);
320 _kernel_swi(Wimp_DeleteWindow, ®s, ®s);
321 this->hidden->window_handle = 0;
325 void WIMP_UpdateRects(_THIS, int numrects, SDL_Rect *rects)
327 _kernel_swi_regs regs;
328 int update_block[12];
329 int xeig = this->hidden->xeig;
330 int yeig = this->hidden->yeig;
332 update_block[0] = this->hidden->window_handle;
334 for (j = 0; j < numrects; j++)
336 update_block[1] = rects[j].x << xeig; /* Min X */
337 update_block[4] = -(rects[j].y << yeig);
338 update_block[3] = update_block[1] + (rects[j].w << xeig);
339 update_block[2] = update_block[4] - (rects[j].h << yeig);
341 regs.r[1] = (int)update_block;
342 /* Update window can fail if called before first poll */
343 if (_kernel_swi(Wimp_UpdateWindow, ®s, ®s) == 0)
347 WIMP_PlotSprite(this, update_block[1], update_block[2]);
348 _kernel_swi(Wimp_GetRectangle, ®s, ®s);
355 int WIMP_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
357 unsigned int *pal = (unsigned int *)(this->hidden->bank[1]+60);
362 for (j = 0; j < ncolors; j++)
364 *pal = (((unsigned int)colors->r) << 8)
365 + (((unsigned int)colors->g) << 16)
366 + (((unsigned int)colors->b) << 24);
372 WIMP_SetupPlotInfo(this);
374 /* Need to refresh the window */
377 update.w = SDL_VideoSurface->w;
378 update.h = SDL_VideoSurface->h;
379 WIMP_UpdateRects(this, 1, &update);
384 void WIMP_SetWMCaption(_THIS, const char *title, const char *icon)
386 _kernel_swi_regs regs;
388 SDL_strlcpy(this->hidden->title, title, SDL_arraysize(this->hidden->title));
390 if (RISCOS_GetWimpVersion() < 380)
394 regs.r[1] = (int)block;
395 _kernel_swi(Wimp_GetCaretPosition, ®s, ®s);
396 if (block[0] == (int)this->hidden->window_handle)
399 _kernel_swi(Wimp_SetCaretPosition, ®s,®s);
402 regs.r[0] = this->hidden->window_handle;
406 _kernel_swi(Wimp_SetCaretPosition, ®s,®s);
408 regs.r[0] = block[0];
409 regs.r[1] = block[1];
410 regs.r[2] = block[2];
411 regs.r[3] = block[3];
412 regs.r[4] = block[4];
413 regs.r[5] = block[5];
414 _kernel_swi(Wimp_SetCaretPosition, ®s,®s);
417 regs.r[0] = this->hidden->window_handle;
418 regs.r[1] = 0x4b534154; /* "TASK" */
419 regs.r[2] = 3; /* Redraw title */
420 _kernel_swi(Wimp_ForceRedraw, ®s, ®s);
424 void WIMP_RefreshDesktop(_THIS)
426 int width = this->hidden->screen_width << this->hidden->xeig;
427 int height = this->hidden->screen_height << this->hidden->yeig;
428 _kernel_swi_regs regs;
429 regs.r[0] = -1; /* Whole screen */
434 _kernel_swi(Wimp_ForceRedraw, ®s, ®s);
437 /* Toggle to window from full screen */
438 int WIMP_ToggleFromFullScreen(_THIS)
440 int width = this->screen->w;
441 int height = this->screen->h;
442 int bpp = this->screen->format->BitsPerPixel;
445 char *old_alloc_bank;
447 /* Ensure flags are OK */
448 this->screen->flags &= ~(SDL_DOUBLEBUF|SDL_HWSURFACE);
450 if (this->hidden->bank[0] == this->hidden->alloc_bank || riscos_backbuffer == 0)
452 /* Need to create a sprite for the screen and copy the data to it */
454 buffer = WIMP_CreateBuffer(width, height, bpp);
455 data = buffer + 60; /* Start of sprite data */
456 if (bpp == 8) data += 2048; /* 8bpp sprite have palette first */
458 if (buffer == NULL) return 0;
459 SDL_memcpy(data, this->hidden->bank[0], width * height * this->screen->format->BytesPerPixel);
461 /* else We've switch to full screen before so we already have a sprite */
463 old_bank[0] = this->hidden->bank[0];
464 old_bank[1] = this->hidden->bank[1];
465 old_alloc_bank = this->hidden->alloc_bank;
467 if (buffer != NULL) this->hidden->alloc_bank = buffer;
469 this->hidden->bank[1] = this->hidden->alloc_bank;
470 this->hidden->bank[0] = this->hidden->bank[1] + 60; /* Start of sprite data */
471 if (bpp == 8) this->hidden->bank[0] += 2048; /* 8bpp sprite have palette first */
473 this->hidden->current_bank = 0;
474 this->screen->pixels = this->hidden->bank[0];
476 RISCOS_RestoreWimpMode();
477 WIMP_ReadModeInfo(this);
478 if (WIMP_SetupWindow(this, this->screen))
480 WIMP_SetDeviceMode(this);
481 WIMP_SetupPlotInfo(this);
483 if (riscos_backbuffer == 0) riscos_backbuffer = 1;
485 if (buffer && old_alloc_bank) SDL_free(old_alloc_bank);
490 /* Drop back to full screen mode on failure */
491 this->hidden->bank[0] = old_bank[0];
492 this->hidden->bank[1] = old_bank[1];
493 this->hidden->alloc_bank = old_alloc_bank;
494 if (buffer) SDL_free(buffer);
496 RISCOS_StoreWimpMode();
497 FULLSCREEN_SetMode(width, height, bpp);