Home | History | Annotate | Download | only in epoc
      1 /*
      2     SDL - Simple DirectMedia Layer
      3     Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  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 (at) libsdl.org
     21 */
     22 #include "SDL_config.h"
     23 
     24 /*
     25     SDL_epocvideo.cpp
     26     Epoc based SDL video driver implementation
     27 
     28     Epoc version by Hannu Viitala (hannu.j.viitala (at) mbnet.fi)
     29 */
     30 
     31 extern "C" {
     32 #include "SDL_timer.h"
     33 #include "SDL_video.h"
     34 #undef NULL
     35 #include "../SDL_pixels_c.h"
     36 };
     37 
     38 #include "SDL_epocvideo.h"
     39 #include "SDL_epocevents_c.h"
     40 
     41 #include <hal.h>
     42 #include <coedef.h>
     43 
     44 /* For debugging */
     45 
     46 void RDebug_Print_b(char* error_str, void* param)
     47     {
     48     TBuf8<128> error8((TUint8*)error_str);
     49     TBuf<128> error;
     50     error.Copy(error8);
     51     if (param) //!! Do not work if the parameter is really 0!!
     52         RDebug::Print(error, param);
     53     else
     54         RDebug::Print(error);
     55     }
     56 
     57 extern "C" void RDebug_Print(char* error_str, void* param)
     58     {
     59     RDebug_Print_b(error_str, param);
     60     }
     61 
     62 
     63 int Debug_AvailMem2()
     64     {
     65     //User::CompressAllHeaps();
     66     TMemoryInfoV1Buf membuf;
     67     User::LeaveIfError(UserHal::MemoryInfo(membuf));
     68     TMemoryInfoV1 minfo = membuf();
     69 	return(minfo.iFreeRamInBytes);
     70     }
     71 
     72 extern "C" int Debug_AvailMem()
     73     {
     74     return(Debug_AvailMem2());
     75     }
     76 
     77 
     78 extern "C" {
     79 
     80 /* Initialization/Query functions */
     81 
     82 static int EPOC_VideoInit(_THIS, SDL_PixelFormat *vformat);
     83 static SDL_Rect **EPOC_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
     84 static SDL_Surface *EPOC_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
     85 static int EPOC_SetColors(_THIS, int firstcolor, int ncolors,
     86 			  SDL_Color *colors);
     87 static void EPOC_VideoQuit(_THIS);
     88 
     89 /* Hardware surface functions */
     90 
     91 static int EPOC_AllocHWSurface(_THIS, SDL_Surface *surface);
     92 static int EPOC_LockHWSurface(_THIS, SDL_Surface *surface);
     93 static int EPOC_FlipHWSurface(_THIS, SDL_Surface *surface);
     94 static void EPOC_UnlockHWSurface(_THIS, SDL_Surface *surface);
     95 static void EPOC_FreeHWSurface(_THIS, SDL_Surface *surface);
     96 static void EPOC_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
     97 
     98 static int EPOC_Available(void);
     99 static SDL_VideoDevice *EPOC_CreateDevice(int devindex);
    100 
    101 /* Mouse functions */
    102 
    103 static WMcursor *EPOC_CreateWMCursor(_THIS, Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y);
    104 static void EPOC_FreeWMCursor(_THIS, WMcursor *cursor);
    105 static int EPOC_ShowWMCursor(_THIS, WMcursor *cursor);
    106 
    107 
    108 
    109 /* !! Table for fast conversion from 8 bit to 12 bit */
    110 static TUint16 EPOC_HWPalette_256_to_4k[256];
    111 
    112 VideoBootStrap EPOC_bootstrap = {
    113 	"epoc", "EPOC system",
    114     EPOC_Available, EPOC_CreateDevice
    115 };
    116 
    117 const TUint32 WindowClientHandle = 9210; //!!
    118 
    119 /* Epoc video driver bootstrap functions */
    120 
    121 static int EPOC_Available(void)
    122 {
    123     return 1; /* Always available */
    124 }
    125 
    126 static void EPOC_DeleteDevice(SDL_VideoDevice *device)
    127 {
    128 	SDL_free(device->hidden);
    129 	SDL_free(device);
    130 }
    131 
    132 static SDL_VideoDevice *EPOC_CreateDevice(int devindex)
    133 {
    134 	SDL_VideoDevice *device;
    135 
    136 	/* Allocate all variables that we free on delete */
    137 	device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
    138 	if ( device ) {
    139 		SDL_memset(device, 0, (sizeof *device));
    140 		device->hidden = (struct SDL_PrivateVideoData *)
    141 				SDL_malloc((sizeof *device->hidden));
    142 	}
    143 	if ( (device == NULL) || (device->hidden == NULL) ) {
    144 		SDL_OutOfMemory();
    145 		if ( device ) {
    146 			SDL_free(device);
    147 		}
    148 		return(0);
    149 	}
    150 	SDL_memset(device->hidden, 0, (sizeof *device->hidden));
    151 
    152 	/* Set the function pointers */
    153 	device->VideoInit = EPOC_VideoInit;
    154 	device->ListModes = EPOC_ListModes;
    155 	device->SetVideoMode = EPOC_SetVideoMode;
    156 	device->SetColors = EPOC_SetColors;
    157 	device->UpdateRects = NULL;
    158 	device->VideoQuit = EPOC_VideoQuit;
    159 	device->AllocHWSurface = EPOC_AllocHWSurface;
    160 	device->CheckHWBlit = NULL;
    161 	device->FillHWRect = NULL;
    162 	device->SetHWColorKey = NULL;
    163 	device->SetHWAlpha = NULL;
    164 	device->LockHWSurface = EPOC_LockHWSurface;
    165 	device->UnlockHWSurface = EPOC_UnlockHWSurface;
    166 	device->FlipHWSurface = EPOC_FlipHWSurface;
    167 	device->FreeHWSurface = EPOC_FreeHWSurface;
    168 	device->SetIcon = NULL;
    169 	device->SetCaption = NULL;
    170 	device->GetWMInfo = NULL;
    171 	device->FreeWMCursor = EPOC_FreeWMCursor;
    172 	device->CreateWMCursor = EPOC_CreateWMCursor;
    173 	device->ShowWMCursor = EPOC_ShowWMCursor;
    174 	device->WarpWMCursor = NULL;
    175 	device->InitOSKeymap = EPOC_InitOSKeymap;
    176 	device->PumpEvents = EPOC_PumpEvents;
    177 	device->free = EPOC_DeleteDevice;
    178 
    179 	return device;
    180 }
    181 
    182 
    183 int GetBpp(TDisplayMode displaymode)
    184 {
    185     TInt numColors = TDisplayModeUtils::NumDisplayModeColors(displaymode);
    186     TInt bitsPerPixel = 1;
    187     for (TInt32 i = 2; i < numColors; i <<= 1, bitsPerPixel++);
    188     return bitsPerPixel;
    189 }
    190 
    191 void ConstructWindowL(_THIS)
    192 {
    193 	TInt	error;
    194 
    195 	error = Private->EPOC_WsSession.Connect();
    196 	User::LeaveIfError(error);
    197 	Private->EPOC_WsScreen=new(ELeave) CWsScreenDevice(Private->EPOC_WsSession);
    198 	User::LeaveIfError(Private->EPOC_WsScreen->Construct());
    199 	User::LeaveIfError(Private->EPOC_WsScreen->CreateContext(Private->EPOC_WindowGc));
    200 
    201 	Private->EPOC_WsWindowGroup=RWindowGroup(Private->EPOC_WsSession);
    202 	User::LeaveIfError(Private->EPOC_WsWindowGroup.Construct(WindowClientHandle));
    203 	Private->EPOC_WsWindowGroup.SetOrdinalPosition(0);
    204 
    205     //!!
    206     TBuf<32> winGroupName;
    207     winGroupName.Append(0);
    208     winGroupName.Append(0);
    209     winGroupName.Append(0);// uid
    210     winGroupName.Append(0);
    211     winGroupName.Append(_L("SDL")); // caption
    212     winGroupName.Append(0);
    213     winGroupName.Append(0); //doc name
    214 	Private->EPOC_WsWindowGroup.SetName(winGroupName); //!!
    215 
    216 	Private->EPOC_WsWindow=RWindow(Private->EPOC_WsSession);
    217 	User::LeaveIfError(Private->EPOC_WsWindow.Construct(Private->EPOC_WsWindowGroup,WindowClientHandle));
    218 	Private->EPOC_WsWindow.SetBackgroundColor(KRgbWhite);
    219     Private->EPOC_WsWindow.Activate();
    220 	Private->EPOC_WsWindow.SetSize(Private->EPOC_WsScreen->SizeInPixels());
    221 	Private->EPOC_WsWindow.SetVisible(ETrue);
    222 
    223     Private->EPOC_WsWindowGroupID = Private->EPOC_WsWindowGroup.Identifier();
    224     Private->EPOC_IsWindowFocused = EFalse;
    225 }
    226 
    227 
    228 int EPOC_VideoInit(_THIS, SDL_PixelFormat *vformat)
    229 {
    230     // !!TODO:handle leave functions!
    231 
    232     int i;
    233 
    234 	/* Initialize all variables that we clean on shutdown */
    235 
    236 	for ( i=0; i<SDL_NUMMODES; ++i ) {
    237 		Private->SDL_modelist[i] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
    238 		Private->SDL_modelist[i]->x = Private->SDL_modelist[i]->y = 0;
    239 	}
    240 	/* Modes sorted largest to smallest !!TODO:sorting order??*/
    241 	Private->SDL_modelist[0]->w = 640; Private->SDL_modelist[0]->h = 200;
    242 	Private->SDL_modelist[1]->w = 320; Private->SDL_modelist[1]->h = 200;
    243 	Private->SDL_modelist[2]->w = 640; Private->SDL_modelist[2]->h = 400;
    244 	Private->SDL_modelist[3]->w = 640; Private->SDL_modelist[3]->h = 480;
    245 	Private->SDL_modelist[4] = NULL;
    246 
    247     /* Construct Epoc window */
    248 
    249     ConstructWindowL(_this);
    250 
    251     /* Initialise Epoc frame buffer */
    252 
    253     TDisplayMode displayMode = Private->EPOC_WsScreen->DisplayMode();
    254 
    255     #ifndef __WINS__
    256 
    257     TScreenInfoV01 screenInfo;
    258 	TPckg<TScreenInfoV01> sInfo(screenInfo);
    259 	UserSvr::ScreenInfo(sInfo);
    260 
    261 	Private->EPOC_ScreenSize		= screenInfo.iScreenSize;
    262 	Private->EPOC_DisplayMode		= displayMode;
    263     Private->EPOC_HasFrameBuffer	= screenInfo.iScreenAddressValid;
    264 	Private->EPOC_FrameBuffer		= Private->EPOC_HasFrameBuffer ? (TUint8*) screenInfo.iScreenAddress : NULL;
    265 	Private->EPOC_BytesPerPixel	    = ((GetBpp(displayMode)-1) / 8) + 1;
    266 	Private->EPOC_BytesPerScanLine	= screenInfo.iScreenSize.iWidth * Private->EPOC_BytesPerPixel;
    267 
    268     /* It seems that in SA1100 machines for 8bpp displays there is a 512 palette table at the
    269      * beginning of the frame buffer. E.g. Series 7 and Netbook.
    270      * In 12 bpp machines the table has 16 entries.
    271 	 */
    272 	if (Private->EPOC_HasFrameBuffer && GetBpp(displayMode) == 8)
    273 		Private->EPOC_FrameBuffer += 512;
    274 	if (Private->EPOC_HasFrameBuffer && GetBpp(displayMode) == 12)
    275 		Private->EPOC_FrameBuffer += 16 * 2;
    276 
    277     #else /* defined __WINS__ */
    278 
    279     /* Create bitmap, device and context for screen drawing */
    280     Private->EPOC_ScreenSize        = Private->EPOC_WsScreen->SizeInPixels();
    281 
    282 	Private->EPOC_Bitmap = new (ELeave) CWsBitmap(Private->EPOC_WsSession);
    283 	Private->EPOC_Bitmap->Create(Private->EPOC_ScreenSize, displayMode);
    284 
    285 	Private->EPOC_DisplayMode	    = displayMode;
    286     Private->EPOC_HasFrameBuffer    = ETrue;
    287     Private->EPOC_FrameBuffer       = NULL; /* Private->EPOC_Bitmap->DataAddress() can change any time */
    288 	Private->EPOC_BytesPerPixel	    = ((GetBpp(displayMode)-1) / 8) + 1;
    289 	Private->EPOC_BytesPerScanLine  = Private->EPOC_WsScreen->SizeInPixels().iWidth * Private->EPOC_BytesPerPixel;
    290 
    291     #endif /* __WINS__ */
    292 
    293     _this->info.current_w = Private->EPOC_ScreenSize.iWidth;
    294     _this->info.current_h = Private->EPOC_ScreenSize.iHeight;
    295 
    296     /* The "best" video format should be returned to caller. */
    297 
    298     vformat->BitsPerPixel       = /*!!GetBpp(displayMode) */ 8;
    299     vformat->BytesPerPixel      = /*!!Private->EPOC_BytesPerPixel*/ 1;
    300 
    301     /* Activate events for me */
    302 
    303 	Private->EPOC_WsEventStatus = KRequestPending;
    304 	Private->EPOC_WsSession.EventReady(&Private->EPOC_WsEventStatus);
    305 	Private->EPOC_RedrawEventStatus = KRequestPending;
    306 	Private->EPOC_WsSession.RedrawReady(&Private->EPOC_RedrawEventStatus);
    307     Private->EPOC_WsWindow.PointerFilter(EPointerFilterDrag, 0);
    308 
    309     Private->EPOC_ScreenOffset = 0;
    310 
    311     //!! TODO: error handling
    312     //if (ret != KErrNone)
    313     //    return(-1);
    314     //else
    315 	    return(0);
    316 }
    317 
    318 
    319 SDL_Rect **EPOC_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
    320 {
    321     if (format->BitsPerPixel == 12 || format->BitsPerPixel == 8)
    322         return Private->SDL_modelist;
    323     return NULL;
    324 }
    325 
    326 int EPOC_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
    327 {
    328     for(int i = firstcolor; i < ncolors; i++) {
    329 	    // 4k value: 000rgb
    330 	    TUint16 color4K = 0;
    331         color4K |= (colors[i].r & 0x0000f0) << 4;
    332 	    color4K |= (colors[i].g & 0x0000f0);
    333 	    color4K |= (colors[i].b & 0x0000f0) >> 4;
    334         EPOC_HWPalette_256_to_4k[i] = color4K;
    335     }
    336 	return(0);
    337 }
    338 
    339 
    340 SDL_Surface *EPOC_SetVideoMode(_THIS, SDL_Surface *current,
    341 				int width, int height, int bpp, Uint32 flags)
    342 {
    343     /* Check parameters */
    344     if (!((width == 640 && height == 200 && bpp == 12) ||
    345           (width == 640 && height == 400 && bpp == 12) ||
    346           (width == 640 && height == 480 && bpp == 12) ||
    347           (width == 320 && height == 200 && bpp == 12) ||
    348           (width == 640 && height == 200 && bpp == 8) ||
    349           (width == 640 && height == 400 && bpp == 8) ||
    350           (width == 640 && height == 480 && bpp == 8) ||
    351           (width == 320 && height == 200 && bpp == 8))) {
    352 		SDL_SetError("Requested video mode is not supported");
    353         return NULL;
    354     }
    355 
    356     if (current && current->pixels) {
    357         SDL_free(current->pixels);
    358         current->pixels = NULL;
    359     }
    360 	if ( ! SDL_ReallocFormat(current, bpp, 0, 0, 0, 0) ) {
    361 		return(NULL);
    362 	}
    363 
    364     /* Set up the new mode framebuffer */
    365     if (bpp == 8)
    366 	    current->flags = (SDL_FULLSCREEN|SDL_SWSURFACE|SDL_PREALLOC|SDL_HWPALETTE);
    367     else // 12 bpp
    368 	    current->flags = (SDL_FULLSCREEN|SDL_SWSURFACE|SDL_PREALLOC);
    369 	current->w = width;
    370 	current->h = height;
    371     int numBytesPerPixel = ((bpp-1)>>3) + 1;
    372 	current->pitch = numBytesPerPixel * width; // Number of bytes in scanline
    373 	current->pixels = SDL_malloc(width * height * numBytesPerPixel);
    374 	SDL_memset(current->pixels, 0, width * height * numBytesPerPixel);
    375 
    376 	/* Set the blit function */
    377 	_this->UpdateRects = EPOC_DirectUpdate;
    378 
    379     /* Must buffer height be shrinked to screen by 2 ? */
    380     if (current->h >= 400)
    381         Private->EPOC_ShrinkedHeight = ETrue;
    382 
    383     /* Centralize game window on device screen  */
    384     Private->EPOC_ScreenOffset = (Private->EPOC_ScreenSize.iWidth - current->w) / 2;
    385 
    386 	/* We're done */
    387 	return(current);
    388 }
    389 
    390 void RedrawWindowL(_THIS)
    391 {
    392     SDL_Rect fullScreen;
    393     fullScreen.x = 0;
    394     fullScreen.y = 0;
    395     fullScreen.w = _this->screen->w;
    396     fullScreen.h = _this->screen->h;
    397 
    398 #ifdef __WINS__
    399 	    TBitmapUtil lock(Private->EPOC_Bitmap);
    400         lock.Begin(TPoint(0,0)); // Lock bitmap heap
    401 	    Private->EPOC_WindowGc->Activate(Private->EPOC_WsWindow);
    402 #endif
    403 
    404     if (fullScreen.w < Private->EPOC_ScreenSize.iWidth
    405         && fullScreen.w < Private->EPOC_ScreenSize.iWidth) {
    406         /* Draw blue stripes background */
    407 #ifdef __WINS__
    408         TUint16* screenBuffer = (TUint16*)Private->EPOC_Bitmap->DataAddress();
    409 #else
    410         TUint16* screenBuffer = (TUint16*)Private->EPOC_FrameBuffer;
    411 #endif
    412         for (int y=0; y < Private->EPOC_ScreenSize.iHeight; y++) {
    413             for (int x=0; x < Private->EPOC_ScreenSize.iWidth; x++) {
    414                 TUint16 color = ((x+y)>>1) & 0xf; /* Draw pattern */
    415                 *screenBuffer++ = color;
    416             }
    417         }
    418     }
    419 
    420 
    421     /* Tell the system that something has been drawn */
    422 	TRect  rect = TRect(Private->EPOC_WsWindow.Size());
    423   	Private->EPOC_WsWindow.Invalidate(rect);
    424 
    425 #ifdef __WINS__
    426 	Private->EPOC_WsWindow.BeginRedraw(rect);
    427 	Private->EPOC_WindowGc->BitBlt(TPoint(), Private->EPOC_Bitmap);
    428 	Private->EPOC_WsWindow.EndRedraw();
    429 	Private->EPOC_WindowGc->Deactivate();
    430     lock.End(); // Unlock bitmap heap
    431 	Private->EPOC_WsSession.Flush();
    432 #endif
    433 
    434     /* Draw current buffer */
    435     EPOC_DirectUpdate(_this, 1, &fullScreen);
    436 }
    437 
    438 
    439 /* We don't actually allow hardware surfaces other than the main one */
    440 static int EPOC_AllocHWSurface(_THIS, SDL_Surface *surface)
    441 {
    442 	return(-1);
    443 }
    444 static void EPOC_FreeHWSurface(_THIS, SDL_Surface *surface)
    445 {
    446 	return;
    447 }
    448 
    449 static int EPOC_LockHWSurface(_THIS, SDL_Surface *surface)
    450 {
    451 	return(0);
    452 }
    453 static void EPOC_UnlockHWSurface(_THIS, SDL_Surface *surface)
    454 {
    455 	return;
    456 }
    457 
    458 static int EPOC_FlipHWSurface(_THIS, SDL_Surface *surface)
    459 {
    460 	return(0);
    461 }
    462 
    463 static void EPOC_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
    464 {
    465     TInt focusWindowGroupId = Private->EPOC_WsSession.GetFocusWindowGroup();
    466     if (focusWindowGroupId != Private->EPOC_WsWindowGroupID) {
    467 
    468         /* Force focus window to redraw again for cleaning away SDL screen graphics */
    469 
    470 
    471         TInt pos = Private->EPOC_WsWindowGroup.OrdinalPosition();
    472         Private->EPOC_WsWindowGroup.SetOrdinalPosition(0, KMaxTInt);
    473        	TRect  rect = TRect(Private->EPOC_WsWindow.Size());
    474         Private->EPOC_WsWindow.Invalidate(rect);
    475         Private->EPOC_WsWindowGroup.SetOrdinalPosition(pos, ECoeWinPriorityNormal);
    476 
    477         /* If this is not the topmost window, wait here! Sleep for 1 second to give cpu to
    478            multitasking and poll for being the topmost window.
    479         */
    480         while (Private->EPOC_WsSession.GetFocusWindowGroup() != Private->EPOC_WsWindowGroupID)
    481             SDL_Delay(1000);
    482 
    483         RedrawWindowL(_this);
    484     }
    485 
    486 	TInt i;
    487     TInt sourceNumBytesPerPixel = ((_this->screen->format->BitsPerPixel-1)>>3) + 1;
    488     TInt targetNumBytesPerPixel = Private->EPOC_BytesPerPixel;
    489     TInt fixedOffset = Private->EPOC_ScreenOffset;
    490     TInt screenW = _this->screen->w;
    491     TInt screenH = _this->screen->h;
    492     TInt sourceScanlineLength = screenW;
    493     if (Private->EPOC_ShrinkedHeight) {  /* simulate 400 pixel height in 200 pixel screen */
    494         sourceScanlineLength <<= 1;
    495         screenH >>= 1;
    496     }
    497     TInt targetScanlineLength = Private->EPOC_ScreenSize.iWidth;
    498 #ifdef __WINS__
    499 	TBitmapUtil lock(Private->EPOC_Bitmap);
    500     lock.Begin(TPoint(0,0)); // Lock bitmap heap
    501 	Private->EPOC_WindowGc->Activate(Private->EPOC_WsWindow);
    502     TUint16* screenBuffer = (TUint16*)Private->EPOC_Bitmap->DataAddress();
    503 #else
    504     TUint16* screenBuffer = (TUint16*)Private->EPOC_FrameBuffer;
    505 #endif
    506 
    507 
    508 	/* Render the rectangles in the list */
    509 
    510 	for ( i=0; i < numrects; ++i ) {
    511         SDL_Rect rect2;
    512         const SDL_Rect& currentRect = rects[i];
    513         rect2.x = currentRect.x;
    514         rect2.y = currentRect.y;
    515         rect2.w = currentRect.w;
    516         rect2.h = currentRect.h;
    517 
    518         if (rect2.w <= 0 || rect2.h <= 0) /* sanity check */
    519             continue;
    520 
    521         if (Private->EPOC_ShrinkedHeight) {  /* simulate 400 pixel height in 200 pixel screen */
    522             rect2.y >>= 1;
    523             if (!(rect2.h >>= 1))
    524                 rect2.h = 1; // always at least 1 pixel height!
    525         }
    526 
    527         /* All variables are measured in pixels */
    528 
    529         /* Check rects validity, i.e. upper and lower bounds */
    530         TInt maxX = Min(screenW - 1, rect2.x + rect2.w - 1);
    531         TInt maxY = Min(screenH - 1, rect2.y + rect2.h - 1);
    532         if (maxX < 0 || maxY < 0) /* sanity check */
    533             continue;
    534         maxY = Min(maxY, 199);
    535 
    536         TInt sourceRectWidth = maxX - rect2.x + 1;
    537         TInt sourceRectWidthInBytes = sourceRectWidth * sourceNumBytesPerPixel;
    538         TInt sourceRectHeight = maxY - rect2.y + 1;
    539         TInt sourceStartOffset = rect2.x + rect2.y * sourceScanlineLength;
    540         TInt targetStartOffset = fixedOffset + rect2.x + rect2.y * targetScanlineLength;
    541 
    542         // !! Nokia9210 native mode: 12 bpp --> 12 bpp
    543         if (_this->screen->format->BitsPerPixel == 12) {
    544 	        TUint16* bitmapLine = (TUint16*)_this->screen->pixels + sourceStartOffset;
    545             TUint16* screenMemory = screenBuffer + targetStartOffset;
    546             for(TInt y = 0 ; y < sourceRectHeight ; y++) {
    547                 __ASSERT_DEBUG(screenMemory < (screenBuffer
    548                     + Private->EPOC_ScreenSize.iWidth * Private->EPOC_ScreenSize.iHeight),
    549                     User::Panic(_L("SDL"), KErrCorrupt));
    550                 __ASSERT_DEBUG(screenMemory >= screenBuffer,
    551                     User::Panic(_L("SDL"), KErrCorrupt));
    552                 __ASSERT_DEBUG(bitmapLine < ((TUint16*)_this->screen->pixels +
    553                     + (_this->screen->w * _this->screen->h)),
    554                     User::Panic(_L("SDL"), KErrCorrupt));
    555                 __ASSERT_DEBUG(bitmapLine >=  (TUint16*)_this->screen->pixels,
    556                     User::Panic(_L("SDL"), KErrCorrupt));
    557 		        Mem::Copy(screenMemory, bitmapLine, sourceRectWidthInBytes);
    558 		        bitmapLine += sourceScanlineLength;
    559 		        screenMemory += targetScanlineLength;
    560             }
    561         }
    562         // !! 256 color paletted mode: 8 bpp  --> 12 bpp
    563         else {
    564 	        TUint8* bitmapLine = (TUint8*)_this->screen->pixels + sourceStartOffset;
    565             TUint16* screenMemory = screenBuffer + targetStartOffset;
    566             for(TInt y = 0 ; y < sourceRectHeight ; y++) {
    567                 TUint8* bitmapPos = bitmapLine; /* 1 byte per pixel */
    568                 TUint16* screenMemoryLinePos = screenMemory; /* 2 bytes per pixel */
    569                 /* Convert each pixel from 256 palette to 4k color values */
    570                 for(TInt x = 0 ; x < sourceRectWidth ; x++) {
    571                     __ASSERT_DEBUG(screenMemoryLinePos < (screenBuffer
    572                         + (Private->EPOC_ScreenSize.iWidth * Private->EPOC_ScreenSize.iHeight)),
    573                         User::Panic(_L("SDL"), KErrCorrupt));
    574                     __ASSERT_DEBUG(screenMemoryLinePos >= screenBuffer,
    575                         User::Panic(_L("SDL"), KErrCorrupt));
    576                     __ASSERT_DEBUG(bitmapPos < ((TUint8*)_this->screen->pixels +
    577                         + (_this->screen->w * _this->screen->h)),
    578                         User::Panic(_L("SDL"), KErrCorrupt));
    579                     __ASSERT_DEBUG(bitmapPos >= (TUint8*)_this->screen->pixels,
    580                         User::Panic(_L("SDL"), KErrCorrupt));
    581                     *screenMemoryLinePos = EPOC_HWPalette_256_to_4k[*bitmapPos];
    582                     bitmapPos++;
    583                     screenMemoryLinePos++;
    584                 }
    585 		        bitmapLine += sourceScanlineLength;
    586 		        screenMemory += targetScanlineLength;
    587             }
    588 	    }
    589 
    590     }
    591 
    592 #ifdef __WINS__
    593 
    594 	TRect  rect = TRect(Private->EPOC_WsWindow.Size());
    595 	Private->EPOC_WsWindow.Invalidate(rect);
    596 	Private->EPOC_WsWindow.BeginRedraw(rect);
    597 	Private->EPOC_WindowGc->BitBlt(TPoint(), Private->EPOC_Bitmap);
    598 	Private->EPOC_WsWindow.EndRedraw();
    599 	Private->EPOC_WindowGc->Deactivate();
    600     lock.End(); // Unlock bitmap heap
    601 	Private->EPOC_WsSession.Flush();
    602 
    603 #endif
    604 
    605     /* Update virtual cursor */
    606     //!!Private->EPOC_WsSession.SetPointerCursorPosition(Private->EPOC_WsSession.PointerCursorPosition());
    607 
    608     return;
    609 }
    610 
    611 
    612 /* Note:  If we are terminated, this could be called in the middle of
    613    another SDL video routine -- notably UpdateRects.
    614 */
    615 void EPOC_VideoQuit(_THIS)
    616 {
    617 	int i;
    618 
    619 	/* Free video mode lists */
    620 	for ( i=0; i<SDL_NUMMODES; ++i ) {
    621 		if ( Private->SDL_modelist[i] != NULL ) {
    622 			SDL_free(Private->SDL_modelist[i]);
    623 			Private->SDL_modelist[i] = NULL;
    624 		}
    625 	}
    626 
    627     if ( _this->screen && (_this->screen->flags & SDL_HWSURFACE) ) {
    628 		/* Direct screen access, no memory buffer */
    629 		_this->screen->pixels = NULL;
    630 	}
    631 
    632     if (_this->screen && _this->screen->pixels) {
    633         SDL_free(_this->screen->pixels);
    634         _this->screen->pixels = NULL;
    635     }
    636 
    637     /* Free Epoc resources */
    638 
    639     /* Disable events for me */
    640 	if (Private->EPOC_WsEventStatus != KRequestPending)
    641 		Private->EPOC_WsSession.EventReadyCancel();
    642 	if (Private->EPOC_RedrawEventStatus != KRequestPending)
    643 		Private->EPOC_WsSession.RedrawReadyCancel();
    644 
    645     #ifdef __WINS__
    646 	delete Private->EPOC_Bitmap;
    647 	Private->EPOC_Bitmap = NULL;
    648     #endif
    649 
    650 	if (Private->EPOC_WsWindow.WsHandle())
    651 		Private->EPOC_WsWindow.Close();
    652 
    653 	if (Private->EPOC_WsWindowGroup.WsHandle())
    654 		Private->EPOC_WsWindowGroup.Close();
    655 
    656 	delete Private->EPOC_WindowGc;
    657 	Private->EPOC_WindowGc = NULL;
    658 
    659 	delete Private->EPOC_WsScreen;
    660 	Private->EPOC_WsScreen = NULL;
    661 
    662 	if (Private->EPOC_WsSession.WsHandle())
    663 		Private->EPOC_WsSession.Close();
    664 }
    665 
    666 
    667 WMcursor *EPOC_CreateWMCursor(_THIS, Uint8 *data, Uint8 *mask, int w, int h, int hot_x, int hot_y)
    668 {
    669 	return (WMcursor *) 9210; // it's ok to return something unuseful but true
    670 }
    671 
    672 void EPOC_FreeWMCursor(_THIS, WMcursor *cursor)
    673 {
    674     /* Disable virtual cursor */
    675     HAL::Set(HAL::EMouseState, HAL::EMouseState_Invisible);
    676     Private->EPOC_WsSession.SetPointerCursorMode(EPointerCursorNone);
    677 }
    678 
    679 int EPOC_ShowWMCursor(_THIS, WMcursor *cursor)
    680 {
    681 
    682     if (cursor ==  (WMcursor *)9210) {
    683         /* Enable virtual cursor */
    684 	    HAL::Set(HAL::EMouseState, HAL::EMouseState_Visible);
    685 	    Private->EPOC_WsSession.SetPointerCursorMode(EPointerCursorNormal);
    686     }
    687     else {
    688         /* Disable virtual cursor */
    689         HAL::Set(HAL::EMouseState, HAL::EMouseState_Invisible);
    690         Private->EPOC_WsSession.SetPointerCursorMode(EPointerCursorNone);
    691     }
    692 
    693 	return(1);
    694 }
    695 
    696 }; // extern "C"
    697 
    698