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 Lesser General Public
79682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License as published by the Free Software Foundation; either
89682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    version 2.1 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    Lesser General Public License for more details.
149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    You should have received a copy of the GNU Lesser General Public
169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    License along with this library; if not, write to the Free Software
179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Sam Lantinga
209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    slouken@libsdl.org
219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	MGA CRTC2 support by Thomas Jarosch - tomj@simonv.com
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	CRTC2 support is inspired by mplayer's dfbmga driver
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	written by Ville Syrj��<syrjala@sci.fi>
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_config.h"
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* DirectFB video driver implementation.
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall*/
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <fcntl.h>
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/mman.h>
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <directfb.h>
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <directfb_version.h>
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mouse.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysvideo.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_pixels_c.h"
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_DirectFB_video.h"
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_DirectFB_events.h"
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_DirectFB_yuv.h"
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* The implementation dependent data for the window manager cursor */
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct WMcursor {
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	int unused;
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Initialization/Query functions */
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_SetColors(_THIS, int firstcolor, int ncolors,
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 SDL_Color *colors);
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DirectFB_VideoQuit(_THIS);
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Hardware surface functions */
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface);
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface);
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface);
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface);
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst);
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                SDL_Surface *dst, SDL_Rect *dstrect);
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key);
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha);
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface);
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_ShowWMCursor(_THIS, WMcursor *cursor);
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Various screen update functions available */
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects);
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* This is the rect EnumModes2 uses */
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct DirectFBEnumRect {
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	SDL_Rect r;
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	struct DirectFBEnumRect* next;
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic struct DirectFBEnumRect *enumlist = NULL;
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* DirectFB driver bootstrap functions */
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_Available(void)
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 1;
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DirectFB_DeleteDevice(SDL_VideoDevice *device)
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_free(device->hidden);
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_free(device);
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_VideoDevice *DirectFB_CreateDevice(int devindex)
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_VideoDevice *device;
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Initialize all variables that we clean on shutdown */
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (device)
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_memset (device, 0, (sizeof *device));
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      device->hidden = (struct SDL_PrivateVideoData *) malloc (sizeof (*device->hidden));
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (device == NULL  ||  device->hidden == NULL)
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_OutOfMemory();
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (device)
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          free (device);
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return(0);
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_memset (device->hidden, 0, sizeof (*device->hidden));
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Set the function pointers */
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->VideoInit = DirectFB_VideoInit;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->ListModes = DirectFB_ListModes;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->SetVideoMode = DirectFB_SetVideoMode;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->SetColors = DirectFB_SetColors;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->UpdateRects = NULL;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->CreateYUVOverlay = DirectFB_CreateYUVOverlay;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->VideoQuit = DirectFB_VideoQuit;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->AllocHWSurface = DirectFB_AllocHWSurface;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->CheckHWBlit = DirectFB_CheckHWBlit;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->FillHWRect = DirectFB_FillHWRect;
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->SetHWColorKey = DirectFB_SetHWColorKey;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->SetHWAlpha = DirectFB_SetHWAlpha;
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->LockHWSurface = DirectFB_LockHWSurface;
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->UnlockHWSurface = DirectFB_UnlockHWSurface;
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->FlipHWSurface = DirectFB_FlipHWSurface;
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->FreeHWSurface = DirectFB_FreeHWSurface;
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->ShowWMCursor = DirectFB_ShowWMCursor;
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->SetCaption = NULL;
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->SetIcon = NULL;
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->IconifyWindow = NULL;
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->GrabInput = NULL;
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->GetWMInfo = NULL;
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->InitOSKeymap = DirectFB_InitOSKeymap;
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->PumpEvents = DirectFB_PumpEvents;
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->free = DirectFB_DeleteDevice;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return device;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallVideoBootStrap DirectFB_bootstrap = {
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  "directfb", "DirectFB",
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DirectFB_Available, DirectFB_CreateDevice
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic DFBSurfacePixelFormat GetFormatForBpp (int bpp, IDirectFBDisplayLayer *layer)
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBDisplayLayerConfig dlc;
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int                   bytes = (bpp + 7) / 8;
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  layer->GetConfiguration (layer, &dlc);
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (bytes == DFB_BYTES_PER_PIXEL(dlc.pixelformat) && bytes > 1)
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return dlc.pixelformat;
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  switch (bytes)
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case 1:
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return DSPF_LUT8;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case 2:
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return DSPF_RGB16;
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case 3:
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return DSPF_RGB24;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case 4:
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return DSPF_RGB32;
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return DSPF_UNKNOWN;
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic DFBEnumerationResult EnumModesCallback (int  width,
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                               int  height,
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                               int  bpp,
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                               void *data)
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_VideoDevice *this = (SDL_VideoDevice *)data;
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  struct DirectFBEnumRect *enumrect;
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HIDDEN->nummodes++;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (enumlist && enumlist->r.w == width && enumlist->r.h == height)
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return DFENUM_OK;
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  enumrect = SDL_calloc(1, sizeof(struct DirectFBEnumRect));
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!enumrect)
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_OutOfMemory();
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return DFENUM_CANCEL;
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  enumrect->r.w  = (Uint16)width;
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  enumrect->r.h  = (Uint16)height;
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  enumrect->next = enumlist;
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  enumlist = enumrect;
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return DFENUM_OK;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstruct private_hwdata {
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IDirectFBSurface *surface;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IDirectFBPalette *palette;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid SetDirectFBerror (const char *function, DFBResult code)
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  const char *error = DirectFBErrorString (code);
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (error)
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("%s: %s", function, error);
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  else
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_SetError("Unknown error code from %s", function);
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic DFBSurfacePixelFormat SDLToDFBPixelFormat (SDL_PixelFormat *format)
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (format->Rmask && format->Gmask && format->Bmask)
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      switch (format->BitsPerPixel)
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case 8:
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          return DSPF_LUT8;
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case 16:
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (format->Rmask == 0xF800 &&
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              format->Gmask == 0x07E0 &&
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              format->Bmask == 0x001F)
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return DSPF_RGB16;
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          /* fall through */
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case 15:
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (format->Rmask == 0x7C00 &&
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              format->Gmask == 0x03E0 &&
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              format->Bmask == 0x001F)
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return DSPF_ARGB1555;
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          break;
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case 24:
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (format->Rmask == 0xFF0000 &&
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              format->Gmask == 0x00FF00 &&
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              format->Bmask == 0x0000FF)
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            return DSPF_RGB24;
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          break;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case 32:
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (format->Rmask == 0xFF0000 &&
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              format->Gmask == 0x00FF00 &&
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              format->Bmask == 0x0000FF)
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              if (format->Amask == 0xFF000000)
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                return DSPF_ARGB;
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              else
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                return DSPF_RGB32;
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          break;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  else
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      switch (format->BitsPerPixel)
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	{
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        case 8:
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          return DSPF_LUT8;
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case 15:
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  return DSPF_ARGB1555;
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case 16:
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  return DSPF_RGB16;
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case 24:
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  return DSPF_RGB24;
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	case 32:
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  return DSPF_RGB32;
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return DSPF_UNKNOWN;
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Palette *AllocatePalette(int size)
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_Palette *palette;
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_Color   *colors;
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  palette = SDL_calloc (1, sizeof(SDL_Palette));
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!palette)
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_OutOfMemory();
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return NULL;
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  colors = SDL_calloc (size, sizeof(SDL_Color));
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!colors)
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_OutOfMemory();
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return NULL;
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  palette->ncolors = size;
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  palette->colors  = colors;
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return palette;
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DFBToSDLPixelFormat (DFBSurfacePixelFormat pixelformat, SDL_PixelFormat *format)
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  format->Amask = format->Rmask = format->Gmask = format->Bmask = 0;
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  format->BitsPerPixel = format->BytesPerPixel = 0;
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  switch (pixelformat)
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case DSPF_A8:
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      format->Amask = 0x000000FF;
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case DSPF_ARGB1555:
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      format->Rmask = 0x00007C00;
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      format->Gmask = 0x000003E0;
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      format->Bmask = 0x0000001F;
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case DSPF_RGB16:
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      format->Rmask = 0x0000F800;
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      format->Gmask = 0x000007E0;
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      format->Bmask = 0x0000001F;
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case DSPF_ARGB:
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      format->Amask = 0; /* apps don't seem to like that:  0xFF000000; */
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* fall through */
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case DSPF_RGB24:
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case DSPF_RGB32:
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      format->Rmask = 0x00FF0000;
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      format->Gmask = 0x0000FF00;
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      format->Bmask = 0x000000FF;
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case DSPF_LUT8:
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      format->Rmask = 0x000000FF;
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      format->Gmask = 0x000000FF;
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      format->Bmask = 0x000000FF;
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (!format->palette)
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        format->palette = AllocatePalette(256);
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    default:
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      fprintf (stderr, "SDL_DirectFB: Unsupported pixelformat (0x%08x)!\n", pixelformat);
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return -1;
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  format->BitsPerPixel  = DFB_BYTES_PER_PIXEL(pixelformat) * 8;
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  format->BytesPerPixel = DFB_BYTES_PER_PIXEL(pixelformat);
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 0;
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat)
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int                      i;
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBResult                ret;
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if (DIRECTFB_MAJOR_VERSION == 0) && (DIRECTFB_MINOR_VERSION == 9) && (DIRECTFB_MICRO_VERSION < 23)
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBCardCapabilities      caps;
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBGraphicsDeviceDescription caps;
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBDisplayLayerConfig    dlc;
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  struct DirectFBEnumRect *rect;
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IDirectFB               *dfb    = NULL;
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IDirectFBDisplayLayer   *layer  = NULL;
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IDirectFBEventBuffer    *events = NULL;
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HIDDEN->c2layer = NULL, HIDDEN->c2frame = NULL;
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HIDDEN->enable_mga_crtc2 = 0;
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HIDDEN->mga_crtc2_stretch_overscan = 1;
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ret = DirectFBInit (NULL, NULL);
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ret)
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SetDirectFBerror ("DirectFBInit", ret);
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      goto error;
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ret = DirectFBCreate (&dfb);
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ret)
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SetDirectFBerror ("DirectFBCreate", ret);
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      goto error;
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ret = dfb->GetDisplayLayer (dfb, DLID_PRIMARY, &layer);
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ret)
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SetDirectFBerror ("dfb->GetDisplayLayer", ret);
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      goto error;
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ret = dfb->CreateInputEventBuffer (dfb, DICAPS_ALL, DFB_FALSE, &events);
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ret)
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SetDirectFBerror ("dfb->CreateEventBuffer", ret);
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      goto error;
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  layer->EnableCursor (layer, 1);
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Query layer configuration to determine the current mode and pixelformat */
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  layer->GetConfiguration (layer, &dlc);
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* If current format is not supported use LUT8 as the default */
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (DFBToSDLPixelFormat (dlc.pixelformat, vformat))
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    DFBToSDLPixelFormat (DSPF_LUT8, vformat);
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Enumerate the available fullscreen modes */
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ret = dfb->EnumVideoModes (dfb, EnumModesCallback, this);
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ret)
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SetDirectFBerror ("dfb->EnumVideoModes", ret);
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      goto error;
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HIDDEN->modelist = SDL_calloc (HIDDEN->nummodes + 1, sizeof(SDL_Rect *));
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!HIDDEN->modelist)
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_OutOfMemory();
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      goto error;
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  for (i = 0, rect = enumlist; rect; ++i, rect = rect->next )
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->modelist[i] = &rect->r;
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HIDDEN->modelist[i] = NULL;
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Query card capabilities to get the video memory size */
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#if (DIRECTFB_MAJOR_VERSION == 0) && (DIRECTFB_MINOR_VERSION == 9) && (DIRECTFB_MICRO_VERSION < 23)
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  dfb->GetCardCapabilities (dfb, &caps);
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#else
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  dfb->GetDeviceDescription (dfb, &caps);
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->info.wm_available = 1;
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->info.hw_available = 1;
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->info.blit_hw      = 1;
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->info.blit_hw_CC   = 1;
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->info.blit_hw_A    = 1;
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->info.blit_fill    = 1;
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->info.video_mem    = caps.video_memory / 1024;
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->info.current_w    = dlc.width;
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->info.current_h    = dlc.height;
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HIDDEN->initialized = 1;
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HIDDEN->dfb         = dfb;
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HIDDEN->layer       = layer;
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HIDDEN->eventbuffer = events;
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (SDL_getenv("SDL_DIRECTFB_MGA_CRTC2") != NULL)
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    HIDDEN->enable_mga_crtc2 = 1;
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (HIDDEN->enable_mga_crtc2)
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      DFBDisplayLayerConfig      dlc;
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      DFBDisplayLayerConfigFlags failed;
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ret = dfb->GetDisplayLayer (dfb, 2, &HIDDEN->c2layer);
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (ret)
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          SetDirectFBerror ("dfb->GetDisplayLayer(CRTC2)", ret);
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          goto error;
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ret = HIDDEN->layer->SetCooperativeLevel(HIDDEN->layer, DLSCL_EXCLUSIVE);
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (ret)
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          SetDirectFBerror ("layer->SetCooperativeLevel(CRTC2, EXCLUSIVE)", ret);
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          goto error;
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ret = HIDDEN->c2layer->SetCooperativeLevel(HIDDEN->c2layer, DLSCL_EXCLUSIVE);
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (ret)
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          SetDirectFBerror ("c2layer->SetCooperativeLevel(CRTC2, EXCLUSIVE)", ret);
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          goto error;
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2layer->SetOpacity(HIDDEN->c2layer, 0x0);
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* Init the surface here as it got a fixed size */
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      dlc.flags      = DLCONF_PIXELFORMAT | DLCONF_BUFFERMODE;
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      dlc.buffermode = DLBM_BACKVIDEO;
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      dlc.pixelformat = DSPF_RGB32;
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ret = HIDDEN->c2layer->TestConfiguration( HIDDEN->c2layer, &dlc, &failed );
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (ret)
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          SetDirectFBerror ("c2layer->TestConfiguration", ret);
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          goto error;
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ret = HIDDEN->c2layer->SetConfiguration( HIDDEN->c2layer, &dlc );
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (ret)
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          SetDirectFBerror ("c2layer->SetConfiguration", ret);
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          goto error;
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ret = HIDDEN->c2layer->GetSurface( HIDDEN->c2layer, &HIDDEN->c2frame );
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (ret)
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          SetDirectFBerror ("c2layer->GetSurface", ret);
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          goto error;
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2framesize.x = 0;
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2framesize.y = 0;
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2frame->GetSize( HIDDEN->c2frame, &HIDDEN->c2framesize.w, &HIDDEN->c2framesize.h);
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2frame->SetBlittingFlags( HIDDEN->c2frame, DSBLIT_NOFX );
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2frame->SetColor( HIDDEN->c2frame, 0, 0, 0, 0xff );
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* Clear CRTC2 */
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2frame->Clear(HIDDEN->c2frame, 0, 0, 0, 0xff );
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, 0 );
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2frame->Clear(HIDDEN->c2frame, 0, 0, 0, 0xff );
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, 0 );
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2frame->Clear(HIDDEN->c2frame, 0, 0, 0, 0xff );
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2layer->SetOpacity(HIDDEN->c2layer, 0xFF );
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* Check if overscan is possibly set */
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (SDL_getenv("SDL_DIRECTFB_MGA_OVERSCAN") != NULL)
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    float overscan = 0;
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    if (SDL_sscanf(SDL_getenv("SDL_DIRECTFB_MGA_OVERSCAN"), "%f", &overscan) == 1)
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               if (overscan > 0 && overscan < 2)
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  HIDDEN->mga_crtc2_stretch_overscan = overscan;
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      #ifdef DIRECTFB_CRTC2_DEBUG
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("CRTC2 overscan: %f\n", HIDDEN->mga_crtc2_stretch_overscan);
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      #endif
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 0;
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall error:
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (events)
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    events->Release (events);
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (HIDDEN->c2frame)
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    HIDDEN->c2frame->Release (HIDDEN->c2frame);
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (HIDDEN->c2layer)
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    HIDDEN->c2layer->Release (HIDDEN->c2layer);
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (layer)
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    layer->Release (layer);
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (dfb)
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dfb->Release (dfb);
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return -1;
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (flags & SDL_FULLSCREEN)
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return HIDDEN->modelist;
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  else
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (SDLToDFBPixelFormat (format) != DSPF_UNKNOWN)
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return (SDL_Rect**) -1;
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return NULL;
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBResult              ret;
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBSurfaceDescription  dsc;
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBSurfacePixelFormat  pixelformat;
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IDirectFBSurface      *surface;
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf (stderr, "SDL DirectFB_SetVideoMode: %dx%d@%d, flags: 0x%08x\n",
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall           width, height, bpp, flags);
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  flags |= SDL_FULLSCREEN;
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Release previous primary surface */
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (current->hwdata && current->hwdata->surface)
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      current->hwdata->surface->Release (current->hwdata->surface);
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      current->hwdata->surface = NULL;
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* And its palette if present */
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (current->hwdata->palette)
6109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
6119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          current->hwdata->palette->Release (current->hwdata->palette);
6129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          current->hwdata->palette = NULL;
6139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
6149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  else if (!current->hwdata)
6169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
6179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* Allocate the hardware acceleration data */
6189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      current->hwdata = (struct private_hwdata *) SDL_calloc (1, sizeof(*current->hwdata));
6199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (!current->hwdata)
6209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
6219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          SDL_OutOfMemory();
6229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          return NULL;
6239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	}
6249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Set cooperative level depending on flag SDL_FULLSCREEN */
6279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (flags & SDL_FULLSCREEN)
6289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
6299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ret = HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_FULLSCREEN);
6309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (ret && !HIDDEN->enable_mga_crtc2)
6319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
6329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          DirectFBError ("dfb->SetCooperativeLevel", ret);
6339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          flags &= ~SDL_FULLSCREEN;
6349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
6359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  else
6379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
6389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Set video mode */
6409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
6419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ret)
6429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
6439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (flags & SDL_FULLSCREEN)
6449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
6459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          flags &= ~SDL_FULLSCREEN;
6469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
6479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
6489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
6499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (ret)
6519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
6529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          SetDirectFBerror ("dfb->SetVideoMode", ret);
6539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          return NULL;
6549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
6559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Create primary surface */
6589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  dsc.flags       = DSDESC_CAPS | DSDESC_PIXELFORMAT;
6599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  dsc.caps        = DSCAPS_PRIMARY | ((flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0);
6609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  dsc.pixelformat = GetFormatForBpp (bpp, HIDDEN->layer);
6619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
6639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ret && (flags & SDL_DOUBLEBUF))
6649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
6659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* Try without double buffering */
6669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      dsc.caps &= ~DSCAPS_FLIPPING;
6679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface);
6689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ret)
6709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
6719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SetDirectFBerror ("dfb->CreateSurface", ret);
6729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return NULL;
6739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  current->w     = width;
6769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  current->h     = height;
6779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  current->flags = SDL_HWSURFACE | SDL_PREALLOC;
6789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (flags & SDL_FULLSCREEN)
6809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
6819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      current->flags |= SDL_FULLSCREEN;
6829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      this->UpdateRects = DirectFB_DirectUpdate;
6839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
6849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  else
6859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->UpdateRects = DirectFB_WindowedUpdate;
6869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (dsc.caps & DSCAPS_FLIPPING)
6889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    current->flags |= SDL_DOUBLEBUF;
6899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  surface->GetPixelFormat (surface, &pixelformat);
6919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBToSDLPixelFormat (pixelformat, current->format);
6939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Get the surface palette (if supported) */
6959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (DFB_PIXELFORMAT_IS_INDEXED( pixelformat ))
6969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
6979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      surface->GetPalette (surface, &current->hwdata->palette);
6989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      current->flags |= SDL_HWPALETTE;
7009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
7019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  current->hwdata->surface = surface;
7039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* MGA CRTC2 stuff */
7059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (HIDDEN->enable_mga_crtc2)
7069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
7079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* no stretching if c2ssize == c2framesize */
7089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2ssize.x = 0, HIDDEN->c2ssize.y = 0;
7099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2ssize.w = width;
7109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2ssize.h = height;
7119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2dsize.x = 0, HIDDEN->c2dsize.y = 0;
7139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2dsize.w = width;
7149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2dsize.h = height;
7159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->mga_crtc2_stretch = 0;
7179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (SDL_getenv("SDL_DIRECTFB_MGA_STRETCH") != NULL)
7199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
7209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    /* Normally assume a picture aspect ratio of 4:3 */
7219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    int zoom_aspect_x = 4, zoom_aspect_y = 3, i, j;
7229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	    for (i = 1; i < 20; i++)
7249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	      {
7259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		for (j = 1; j < 10; j++)
7269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  {
7279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    if ((float)width/(float)i*(float)j == height)
7289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      {
7299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			zoom_aspect_x = i;
7309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			zoom_aspect_y = j;
7319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			/* break the loop */
7339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			i = 21;
7349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			break;
7359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      }
7369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		  }
7379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	      }
7389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            #ifdef DIRECTFB_CRTC2_DEBUG
7409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            printf("Source resolution: X: %d, Y: %d, Aspect ratio: %d:%d\n", width, height, zoom_aspect_x, zoom_aspect_y);
7419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            printf("CRTC2 resolution: X: %d, Y: %d\n", HIDDEN->c2framesize.w, HIDDEN->c2framesize.h);
7429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            #endif
7439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          /* don't stretch only slightly smaller/larger images */
7459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if ((float)width < (float)HIDDEN->c2framesize.w*0.95 || (float)height < (float)HIDDEN->c2framesize.h*0.95)
7469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
7479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              while ((float)HIDDEN->c2dsize.w < (float)HIDDEN->c2framesize.w*HIDDEN->mga_crtc2_stretch_overscan && (float)HIDDEN->c2dsize.h < (float)HIDDEN->c2framesize.h*HIDDEN->mga_crtc2_stretch_overscan)
7489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
7499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                   HIDDEN->c2dsize.w+=zoom_aspect_x;
7509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                   HIDDEN->c2dsize.h+=zoom_aspect_y;
7519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
7529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              /* one step down */
7549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                HIDDEN->c2dsize.w-=zoom_aspect_x;
7559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                HIDDEN->c2dsize.h-=zoom_aspect_y;
7569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              #ifdef DIRECTFB_CRTC2_DEBUG
7589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              printf("Stretched resolution: X: %d, Y: %d\n", HIDDEN->c2dsize.w, HIDDEN->c2dsize.h);
7599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              #endif
7609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              HIDDEN->mga_crtc2_stretch = 1;
7629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            }
7639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          else if ((float)width > (float)HIDDEN->c2framesize.w*0.95 || (float)height > (float)HIDDEN->c2framesize.h*0.95)
7649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            {
7659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall               while ((float)HIDDEN->c2dsize.w > (float)HIDDEN->c2framesize.w*HIDDEN->mga_crtc2_stretch_overscan || (float)HIDDEN->c2dsize.h > (float)HIDDEN->c2framesize.h*HIDDEN->mga_crtc2_stretch_overscan)
7669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                {
7679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              HIDDEN->c2dsize.w-=zoom_aspect_x;
7689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              HIDDEN->c2dsize.h-=zoom_aspect_y;
7699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                }
7709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              #ifdef DIRECTFB_CRTC2_DEBUG
7729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              printf("Down-Stretched resolution: X: %d, Y: %d\n", HIDDEN->c2dsize.w, HIDDEN->c2dsize.h);
7739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              #endif
7749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall              HIDDEN->mga_crtc2_stretch = 1;
7769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall             } else {
7779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          #ifdef DIRECTFB_CRTC2_DEBUG
7789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          printf("Not stretching image\n");
7799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          #endif
7809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
7819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      /* Panning */
7839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (HIDDEN->c2framesize.w > HIDDEN->c2dsize.w)
7849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        HIDDEN->c2dsize.x = (HIDDEN->c2framesize.w - HIDDEN->c2dsize.w)  / 2;
7859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      else
7869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        HIDDEN->c2dsize.x = (HIDDEN->c2dsize.w - HIDDEN->c2framesize.w)  / 2;
7879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (HIDDEN->c2framesize.h > HIDDEN->c2dsize.h)
7899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        HIDDEN->c2dsize.y = (HIDDEN->c2framesize.h - HIDDEN->c2dsize.h)  / 2;
7909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      else
7919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        HIDDEN->c2dsize.y = (HIDDEN->c2dsize.h - HIDDEN->c2framesize.h)  / 2;
7929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      #ifdef DIRECTFB_CRTC2_DEBUG
7949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    printf("CRTC2 position X: %d, Y: %d\n", HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
7959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      #endif
7969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall   }
7979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
7989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
7999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return current;
8009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface)
8039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBResult             ret;
8059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBSurfaceDescription dsc;
8069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /*  fprintf(stderr, "SDL: DirectFB_AllocHWSurface (%dx%d@%d, flags: 0x%08x)\n",
8089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      surface->w, surface->h, surface->format->BitsPerPixel, surface->flags);*/
8099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (surface->w < 8 || surface->h < 8)
8119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
8129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* fill surface description */
8149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  dsc.flags  = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
8159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  dsc.width  = surface->w;
8169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  dsc.height = surface->h;
8179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  dsc.caps   = (surface->flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0;
8189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* find the right pixelformat */
8209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  dsc.pixelformat = SDLToDFBPixelFormat (surface->format);
8219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (dsc.pixelformat == DSPF_UNKNOWN)
8229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
8239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Allocate the hardware acceleration data */
8259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  surface->hwdata = (struct private_hwdata *) SDL_calloc (1, sizeof(*surface->hwdata));
8269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (surface->hwdata == NULL)
8279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
8289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SDL_OutOfMemory();
8299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return -1;
8309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
8319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Create the surface */
8339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface->hwdata->surface);
8349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ret)
8359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
8369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SetDirectFBerror ("dfb->CreateSurface", ret);
8379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      free (surface->hwdata);
8389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      surface->hwdata = NULL;
8399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return -1;
8409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
8419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  surface->flags |= SDL_HWSURFACE | SDL_PREALLOC;
8439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 0;
8459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface)
8489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (surface->hwdata && HIDDEN->initialized)
8509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
8519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      surface->hwdata->surface->Release (surface->hwdata->surface);
8529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      free (surface->hwdata);
8539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      surface->hwdata = NULL;
8549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
8559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
8589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /*  fprintf(stderr, "SDL: DirectFB_CheckHWBlit (src->hwdata: %p, dst->hwdata: %p)\n",
8609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      src->hwdata, dst->hwdata);*/
8619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!src->hwdata || !dst->hwdata)
8639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
8649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  src->flags |= SDL_HWACCEL;
8669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  src->map->hw_blit = DirectFB_HWAccelBlit;
8679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 1;
8699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
8709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
8729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                SDL_Surface *dst, SDL_Rect *dstrect)
8739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
8749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBSurfaceBlittingFlags flags = DSBLIT_NOFX;
8759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBRectangle sr = { srcrect->x, srcrect->y, srcrect->w, srcrect->h };
8779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBRectangle dr = { dstrect->x, dstrect->y, dstrect->w, dstrect->h };
8789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IDirectFBSurface *surface = dst->hwdata->surface;
8809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (src->flags & SDL_SRCCOLORKEY)
8829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
8839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      flags |= DSBLIT_SRC_COLORKEY;
8849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      DirectFB_SetHWColorKey (NULL, src, src->format->colorkey);
8859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
8869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (src->flags & SDL_SRCALPHA)
8889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
8899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      flags |= DSBLIT_BLEND_COLORALPHA;
8909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      surface->SetColor (surface, 0xff, 0xff, 0xff, src->format->alpha);
8919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
8929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  surface->SetBlittingFlags (surface, flags);
8949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
8959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (sr.w == dr.w && sr.h == dr.h)
8969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    surface->Blit (surface, src->hwdata->surface, &sr, dr.x, dr.y);
8979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  else
8989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    surface->StretchBlit (surface, src->hwdata->surface, &sr, &dr);
8999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 0;
9019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
9049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_PixelFormat  *fmt     = dst->format;
9069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IDirectFBSurface *surface = dst->hwdata->surface;
9079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* ugly */
9099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  surface->SetColor (surface,
9109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     (color & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
9119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     (color & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
9129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                     (color & fmt->Bmask) << (fmt->Bloss - fmt->Bshift), 0xFF);
9139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  surface->FillRectangle (surface, dstrect->x, dstrect->y, dstrect->w, dstrect->h);
9149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 0;
9169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_SetHWColorKey(_THIS, SDL_Surface *src, Uint32 key)
9199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_PixelFormat  *fmt     = src->format;
9219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IDirectFBSurface *surface = src->hwdata->surface;
9229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (fmt->BitsPerPixel == 8)
9249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    surface->SetSrcColorKeyIndex (surface, key);
9259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  else
9269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    /* ugly */
9279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    surface->SetSrcColorKey (surface,
9289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
9299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
9309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                             (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
9319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 0;
9339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha)
9369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 0;
9389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface)
9419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (HIDDEN->enable_mga_crtc2)
9439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
9449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       int rtn = surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, 0);
9459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       if (HIDDEN->mga_crtc2_stretch)
9469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, surface->hwdata->surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize);
9479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       else
9489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         HIDDEN->c2frame->Blit(HIDDEN->c2frame, surface->hwdata->surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
9499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
9519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       return rtn;
9529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
9539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  else
9549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall     return surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, DSFLIP_WAITFORSYNC);
9559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface)
9589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBResult  ret;
9609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  void      *data;
9619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int        pitch;
9629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  ret = surface->hwdata->surface->Lock (surface->hwdata->surface,
9649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall                                        DSLF_WRITE, &data, &pitch);
9659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ret)
9669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
9679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      SetDirectFBerror ("surface->Lock", ret);
9689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return -1;
9699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
9709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  surface->pixels = data;
9729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  surface->pitch  = pitch;
9739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 0;
9759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface)
9789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  surface->hwdata->surface->Unlock (surface->hwdata->surface);
9809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  surface->pixels = NULL;
9819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
9849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (HIDDEN->enable_mga_crtc2)
9869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
9879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       if (HIDDEN->mga_crtc2_stretch)
9889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, this->screen->hwdata->surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize);
9899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       else
9909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall         HIDDEN->c2frame->Blit(HIDDEN->c2frame, this->screen->hwdata->surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
9919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall       HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
9939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
9949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
9959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
9969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects)
9979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
9989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  DFBRegion         region;
9999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int               i;
10009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int               region_valid = 0;
10019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IDirectFBSurface *surface = this->screen->hwdata->surface;
10029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  for (i=0; i<numrects; ++i)
10049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
10059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      int x2, y2;
10069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if ( ! rects[i].w ) /* Clipped? */
10089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        continue;
10099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      x2 = rects[i].x + rects[i].w - 1;
10119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      y2 = rects[i].y + rects[i].h - 1;
10129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (region_valid)
10149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
10159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (rects[i].x < region.x1)
10169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            region.x1 = rects[i].x;
10179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (rects[i].y < region.y1)
10199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            region.y1 = rects[i].y;
10209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (x2 > region.x2)
10229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            region.x2 = x2;
10239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (y2 > region.y2)
10259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            region.y2 = y2;
10269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
10279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      else
10289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
10299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            region.x1 = rects[i].x;
10309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            region.y1 = rects[i].y;
10319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            region.x2 = x2;
10329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            region.y2 = y2;
10339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            region_valid = 1;
10359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
10369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (region_valid)
10399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
10409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (HIDDEN->enable_mga_crtc2)
10419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
10429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          if (HIDDEN->mga_crtc2_stretch)
10439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            HIDDEN->c2frame->StretchBlit(HIDDEN->c2frame, surface, &HIDDEN->c2ssize, &HIDDEN->c2dsize);
10449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          else
10459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall            HIDDEN->c2frame->Blit(HIDDEN->c2frame, surface, NULL, HIDDEN->c2dsize.x, HIDDEN->c2dsize.y);
10469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          HIDDEN->c2frame->Flip(HIDDEN->c2frame, NULL, DSFLIP_WAITFORSYNC);
10489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
10499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      else
10509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        surface->Flip (surface, &region, DSFLIP_WAITFORSYNC);
10519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
10539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint DirectFB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
10559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
10569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  IDirectFBPalette *palette = this->screen->hwdata->palette;
10579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!palette)
10599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
10609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (firstcolor > 255)
10629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
10639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (firstcolor + ncolors > 256)
10659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    ncolors = 256 - firstcolor;
10669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ncolors > 0)
10689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
10699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      int      i;
10709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      DFBColor entries[ncolors];
10719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      for (i=0; i<ncolors; i++)
10739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        {
10749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          entries[i].a = 0xff;
10759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          entries[i].r = colors[i].r;
10769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          entries[i].g = colors[i].g;
10779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall          entries[i].b = colors[i].b;
10789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        }
10799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      palette->SetEntries (palette, entries, ncolors, firstcolor);
10819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
10829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 1;
10849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
10859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DirectFB_VideoQuit(_THIS)
10879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
10889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  struct DirectFBEnumRect *rect    = enumlist;
10899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (this->screen && this->screen->hwdata)
10919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
10929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      IDirectFBSurface        *surface = this->screen->hwdata->surface;
10939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      IDirectFBPalette        *palette = this->screen->hwdata->palette;
10949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (palette)
10969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        palette->Release (palette);
10979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
10989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (surface)
10999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall        surface->Release (surface);
11009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      this->screen->hwdata->surface = NULL;
11029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      this->screen->hwdata->palette = NULL;
11039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (HIDDEN->c2frame)
11069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
11079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2frame->Release (HIDDEN->c2frame);
11089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2frame = NULL;
11099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (HIDDEN->eventbuffer)
11129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
11139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->eventbuffer->Release (HIDDEN->eventbuffer);
11149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->eventbuffer = NULL;
11159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (HIDDEN->c2layer)
11189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
11199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2layer->Release (HIDDEN->c2layer);
11209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->c2layer = NULL;
11219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (HIDDEN->layer)
11249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
11259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->layer->Release (HIDDEN->layer);
11269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->layer = NULL;
11279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (HIDDEN->dfb)
11309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
11319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->dfb->Release (HIDDEN->dfb);
11329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->dfb = NULL;
11339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Free video mode list */
11369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (HIDDEN->modelist)
11379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
11389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      free (HIDDEN->modelist);
11399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->modelist = NULL;
11409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Free mode enumeration list */
11439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  while (rect)
11449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
11459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      struct DirectFBEnumRect *next = rect->next;
11469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      free (rect);
11479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      rect = next;
11489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  enumlist = NULL;
11509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  HIDDEN->initialized = 0;
11529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint DirectFB_ShowWMCursor(_THIS, WMcursor *cursor)
11569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
11579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* We can only hide or show the default cursor */
11589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if ( cursor == NULL )
11599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
11609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->layer->SetCursorOpacity(HIDDEN->layer, 0x00);
11619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    else
11639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    {
11649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      HIDDEN->layer->SetCursorOpacity(HIDDEN->layer, 0xFF);
11659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
11669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 1;
11679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
11689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
11699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid DirectFB_FinalQuit(void)
11709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
11719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1172