src/video/directfb/SDL_DirectFB_video.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 24 Aug 2002 15:29:06 +0000
changeset 464 1c4be4a16410
parent 297 f6ffac90895c
child 477 22581630aab7
permissions -rw-r--r--
Date: Fri, 23 Aug 2002 11:48:56 +0200
From: Denis Oliver Kropp
Subject: Another patch

this is another patch fixing key code mapping
along with some other fixes and better mode handling.
slouken@167
     1
/*
slouken@167
     2
	SDL - Simple DirectMedia Layer
slouken@297
     3
	Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@167
     4
slouken@167
     5
	This library is free software; you can redistribute it and/or
slouken@167
     6
	modify it under the terms of the GNU Library General Public
slouken@167
     7
	License as published by the Free Software Foundation; either
slouken@167
     8
	version 2 of the License, or (at your option) any later version.
slouken@167
     9
slouken@167
    10
	This library is distributed in the hope that it will be useful,
slouken@167
    11
	but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@167
    12
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@167
    13
	Library General Public License for more details.
slouken@167
    14
slouken@167
    15
	You should have received a copy of the GNU Library General Public
slouken@167
    16
	License along with this library; if not, write to the Free
slouken@167
    17
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@167
    18
slouken@167
    19
	Sam Lantinga
slouken@252
    20
	slouken@libsdl.org
slouken@167
    21
*/
slouken@167
    22
slouken@167
    23
#ifdef SAVE_RCSID
slouken@167
    24
static char rcsid =
slouken@167
    25
 "@(#) $Id$";
slouken@167
    26
#endif
slouken@167
    27
slouken@167
    28
/* DirectFB video driver implementation.
slouken@167
    29
*/
slouken@167
    30
slouken@167
    31
#include <stdlib.h>
slouken@464
    32
#include <string.h>
slouken@167
    33
#include <stdio.h>
slouken@167
    34
#include <fcntl.h>
slouken@167
    35
#include <unistd.h>
slouken@167
    36
#include <sys/mman.h>
slouken@167
    37
slouken@167
    38
#include <directfb.h>
slouken@167
    39
slouken@167
    40
#include "SDL.h"
slouken@167
    41
#include "SDL_error.h"
slouken@167
    42
#include "SDL_video.h"
slouken@167
    43
#include "SDL_mouse.h"
slouken@167
    44
#include "SDL_sysvideo.h"
slouken@167
    45
#include "SDL_pixels_c.h"
slouken@167
    46
#include "SDL_events_c.h"
slouken@167
    47
#include "SDL_DirectFB_video.h"
slouken@167
    48
#include "SDL_DirectFB_events.h"
slouken@167
    49
slouken@167
    50
slouken@167
    51
/* Initialization/Query functions */
slouken@167
    52
static int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat);
slouken@167
    53
static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
slouken@167
    54
static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
slouken@167
    55
static int DirectFB_SetColors(_THIS, int firstcolor, int ncolors,
slouken@167
    56
			 SDL_Color *colors);
slouken@167
    57
static void DirectFB_VideoQuit(_THIS);
slouken@167
    58
slouken@167
    59
/* Hardware surface functions */
slouken@167
    60
static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface);
slouken@167
    61
static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
slouken@167
    62
static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface);
slouken@167
    63
static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface);
slouken@167
    64
static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface);
slouken@167
    65
static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst);
slouken@167
    66
static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
slouken@167
    67
                                SDL_Surface *dst, SDL_Rect *dstrect);
slouken@167
    68
static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key);
slouken@167
    69
static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha);
slouken@167
    70
static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface);
slouken@167
    71
slouken@167
    72
/* Various screen update functions available */
slouken@167
    73
static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
slouken@167
    74
static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects);
slouken@167
    75
slouken@167
    76
/* This is the rect EnumModes2 uses */
slouken@167
    77
struct DirectFBEnumRect {
slouken@167
    78
	SDL_Rect r;
slouken@167
    79
	struct DirectFBEnumRect* next;
slouken@167
    80
};
slouken@167
    81
slouken@464
    82
static struct DirectFBEnumRect *enumlist = NULL;
slouken@167
    83
slouken@167
    84
slouken@167
    85
/* DirectFB driver bootstrap functions */
slouken@167
    86
slouken@167
    87
static int DirectFB_Available(void)
slouken@167
    88
{
slouken@167
    89
  return 1;
slouken@167
    90
}
slouken@167
    91
slouken@167
    92
static void DirectFB_DeleteDevice(SDL_VideoDevice *device)
slouken@167
    93
{
slouken@167
    94
  free(device->hidden);
slouken@167
    95
  free(device);
slouken@167
    96
}
slouken@167
    97
slouken@167
    98
static SDL_VideoDevice *DirectFB_CreateDevice(int devindex)
slouken@167
    99
{
slouken@167
   100
  SDL_VideoDevice *device;
slouken@167
   101
slouken@167
   102
  /* Initialize all variables that we clean on shutdown */
slouken@167
   103
  device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
slouken@167
   104
  if (device)
slouken@167
   105
    {
slouken@167
   106
      memset (device, 0, (sizeof *device));
slouken@167
   107
      device->hidden = (struct SDL_PrivateVideoData *) malloc (sizeof (*device->hidden));
slouken@167
   108
    }
slouken@167
   109
  if (device == NULL  ||  device->hidden == NULL)
slouken@167
   110
    {
slouken@167
   111
      SDL_OutOfMemory();
slouken@167
   112
      if (device)
slouken@167
   113
        {
slouken@167
   114
          free (device);
slouken@167
   115
        }
slouken@167
   116
      return(0);
slouken@167
   117
    }
slouken@167
   118
  memset (device->hidden, 0, sizeof (*device->hidden));
slouken@167
   119
slouken@167
   120
  /* Set the function pointers */
slouken@167
   121
  device->VideoInit = DirectFB_VideoInit;
slouken@167
   122
  device->ListModes = DirectFB_ListModes;
slouken@167
   123
  device->SetVideoMode = DirectFB_SetVideoMode;
slouken@167
   124
  device->SetColors = DirectFB_SetColors;
slouken@167
   125
  device->UpdateRects = NULL;
slouken@167
   126
  device->VideoQuit = DirectFB_VideoQuit;
slouken@167
   127
  device->AllocHWSurface = DirectFB_AllocHWSurface;
slouken@167
   128
  device->CheckHWBlit = DirectFB_CheckHWBlit;
slouken@167
   129
  device->FillHWRect = DirectFB_FillHWRect;
slouken@167
   130
  device->SetHWColorKey = DirectFB_SetHWColorKey;
slouken@167
   131
  device->SetHWAlpha = DirectFB_SetHWAlpha;
slouken@167
   132
  device->LockHWSurface = DirectFB_LockHWSurface;
slouken@167
   133
  device->UnlockHWSurface = DirectFB_UnlockHWSurface;
slouken@167
   134
  device->FlipHWSurface = DirectFB_FlipHWSurface;
slouken@167
   135
  device->FreeHWSurface = DirectFB_FreeHWSurface;
slouken@167
   136
  device->SetCaption = NULL;
slouken@167
   137
  device->SetIcon = NULL;
slouken@167
   138
  device->IconifyWindow = NULL;
slouken@167
   139
  device->GrabInput = NULL;
slouken@167
   140
  device->GetWMInfo = NULL;
slouken@167
   141
  device->InitOSKeymap = DirectFB_InitOSKeymap;
slouken@167
   142
  device->PumpEvents = DirectFB_PumpEvents;
slouken@167
   143
slouken@167
   144
  device->free = DirectFB_DeleteDevice;
slouken@167
   145
slouken@167
   146
  return device;
slouken@167
   147
}
slouken@167
   148
slouken@167
   149
VideoBootStrap DirectFB_bootstrap = {
slouken@167
   150
  "directfb", "DirectFB",
slouken@167
   151
  DirectFB_Available, DirectFB_CreateDevice
slouken@167
   152
};
slouken@167
   153
slouken@464
   154
static DFBSurfacePixelFormat GetFormatForBpp (int bpp, IDirectFBDisplayLayer *layer)
slouken@464
   155
{
slouken@464
   156
  DFBDisplayLayerConfig dlc;
slouken@464
   157
  int                   bytes = (bpp + 7) / 8;
slouken@464
   158
slouken@464
   159
  layer->GetConfiguration (layer, &dlc);
slouken@464
   160
slouken@464
   161
  if (bytes == DFB_BYTES_PER_PIXEL(dlc.pixelformat))
slouken@464
   162
    return dlc.pixelformat;
slouken@464
   163
slouken@464
   164
  switch (bytes)
slouken@464
   165
    {
slouken@464
   166
    case 1:
slouken@464
   167
      return DSPF_RGB332;
slouken@464
   168
    case 2:
slouken@464
   169
      return DSPF_RGB16;
slouken@464
   170
    case 3:
slouken@464
   171
      return DSPF_RGB24;
slouken@464
   172
    case 4:
slouken@464
   173
      return DSPF_RGB32;
slouken@464
   174
    }
slouken@464
   175
slouken@464
   176
  return DSPF_UNKNOWN;
slouken@464
   177
}
slouken@464
   178
slouken@167
   179
static DFBEnumerationResult EnumModesCallback (unsigned int  width,
slouken@167
   180
                                               unsigned int  height,
slouken@167
   181
                                               unsigned int  bpp,
slouken@167
   182
                                               void         *data)
slouken@167
   183
{
slouken@167
   184
  SDL_VideoDevice *this = (SDL_VideoDevice *)data;
slouken@167
   185
  struct DirectFBEnumRect *enumrect;
slouken@167
   186
slouken@464
   187
  HIDDEN->nummodes++;
slouken@464
   188
slouken@464
   189
  enumrect = calloc(1, sizeof(struct DirectFBEnumRect));
slouken@464
   190
  if (!enumrect)
slouken@167
   191
    {
slouken@464
   192
      SDL_OutOfMemory();
slouken@464
   193
      return DFENUM_CANCEL;
slouken@167
   194
    }
slouken@167
   195
slouken@464
   196
  enumrect->r.w  = width;
slouken@464
   197
  enumrect->r.h  = height;
slouken@464
   198
  enumrect->next = enumlist;
slouken@464
   199
slouken@464
   200
  enumlist = enumrect;
slouken@464
   201
slouken@167
   202
  return DFENUM_OK;
slouken@167
   203
}
slouken@167
   204
slouken@167
   205
struct private_hwdata {
slouken@167
   206
  IDirectFBSurface *surface;
slouken@167
   207
};
slouken@167
   208
slouken@167
   209
void SetDirectFBerror (const char *function, DFBResult code)
slouken@167
   210
{
slouken@167
   211
  const char *error = DirectFBErrorString (code);
slouken@167
   212
slouken@167
   213
  if (error)
slouken@167
   214
    SDL_SetError("%s: %s", function, error);
slouken@167
   215
  else
slouken@167
   216
    SDL_SetError("Unknown error code from %s", function);
slouken@167
   217
}
slouken@167
   218
slouken@167
   219
static DFBSurfacePixelFormat SDLToDFBPixelFormat (SDL_PixelFormat *format)
slouken@167
   220
{
slouken@167
   221
  if (format->Rmask && format->Gmask && format->Bmask)
slouken@167
   222
    {
slouken@167
   223
      switch (format->BitsPerPixel)
slouken@167
   224
        {
slouken@167
   225
        case 16:
slouken@167
   226
          if (format->Rmask == 0xF800 &&
slouken@167
   227
              format->Gmask == 0x07E0 &&
slouken@167
   228
              format->Bmask == 0x001F)
slouken@167
   229
            return DSPF_RGB16;
slouken@167
   230
          /* fall through */
slouken@167
   231
          
slouken@167
   232
        case 15:
slouken@167
   233
          if (format->Rmask == 0x7C00 &&
slouken@167
   234
              format->Gmask == 0x03E0 &&
slouken@167
   235
              format->Bmask == 0x001F)
slouken@167
   236
            return DSPF_RGB15;
slouken@167
   237
          break;
slouken@167
   238
          
slouken@464
   239
        case 8:
slouken@464
   240
          if (format->Rmask == 0xE0 &&
slouken@464
   241
              format->Gmask == 0x1C &&
slouken@464
   242
              format->Bmask == 0x03)
slouken@464
   243
            return DSPF_RGB332;
slouken@464
   244
          break;
slouken@464
   245
          
slouken@167
   246
        case 24:
slouken@167
   247
          if (format->Rmask == 0xFF0000 &&
slouken@167
   248
              format->Gmask == 0x00FF00 &&
slouken@167
   249
              format->Bmask == 0x0000FF)
slouken@167
   250
            return DSPF_RGB24;
slouken@167
   251
          break;
slouken@464
   252
slouken@167
   253
        case 32:
slouken@167
   254
          if (format->Rmask == 0xFF0000 &&
slouken@167
   255
              format->Gmask == 0x00FF00 &&
slouken@167
   256
              format->Bmask == 0x0000FF)
slouken@167
   257
            {
slouken@167
   258
              if (format->Amask == 0xFF000000)
slouken@167
   259
                return DSPF_ARGB;
slouken@167
   260
              else
slouken@167
   261
                return DSPF_RGB32;
slouken@167
   262
            }
slouken@167
   263
          break;
slouken@167
   264
        }
slouken@167
   265
    }
slouken@167
   266
  else
slouken@167
   267
    {
slouken@167
   268
      switch (format->BitsPerPixel)
slouken@167
   269
	{
slouken@464
   270
        case 8:
slouken@464
   271
          return DSPF_RGB332;
slouken@167
   272
	case 15:
slouken@167
   273
	  return DSPF_RGB15;
slouken@167
   274
	case 16:
slouken@167
   275
	  return DSPF_RGB16;
slouken@167
   276
	case 24:
slouken@167
   277
	  return DSPF_RGB24;
slouken@167
   278
	case 32:
slouken@167
   279
	  return DSPF_RGB32;
slouken@167
   280
	}
slouken@167
   281
    }
slouken@167
   282
slouken@167
   283
  return DSPF_UNKNOWN;
slouken@167
   284
}
slouken@167
   285
slouken@464
   286
static const __u8 lookup3to8[] = { 0x00, 0x24, 0x49, 0x6d, 0x92, 0xb6, 0xdb, 0xff };
slouken@464
   287
static const __u8 lookup2to8[] = { 0x00, 0x55, 0xaa, 0xff };
slouken@464
   288
slouken@464
   289
static SDL_Palette *GenerateRGB332Palette()
slouken@464
   290
{
slouken@464
   291
  int          i;
slouken@464
   292
  SDL_Palette *palette;
slouken@464
   293
  SDL_Color   *colors;
slouken@464
   294
slouken@464
   295
  palette = calloc (1, sizeof(SDL_Palette));
slouken@464
   296
  if (!palette)
slouken@464
   297
    {
slouken@464
   298
      SDL_OutOfMemory();
slouken@464
   299
      return NULL;
slouken@464
   300
    }
slouken@464
   301
slouken@464
   302
  colors = calloc (256, sizeof(SDL_Color));
slouken@464
   303
  if (!colors)
slouken@464
   304
    {
slouken@464
   305
      SDL_OutOfMemory();
slouken@464
   306
      return NULL;
slouken@464
   307
    }
slouken@464
   308
slouken@464
   309
  for (i=0; i<256; i++)
slouken@464
   310
    {
slouken@464
   311
      colors[i].r = lookup3to8[ i >> 5 ];
slouken@464
   312
      colors[i].g = lookup3to8[ (i >> 2) & 7 ];
slouken@464
   313
      colors[i].g = lookup2to8[ i & 3 ];
slouken@464
   314
    }
slouken@464
   315
slouken@464
   316
  palette->ncolors = 256;
slouken@464
   317
  palette->colors  = colors;
slouken@464
   318
slouken@464
   319
  return palette;
slouken@464
   320
}
slouken@464
   321
slouken@167
   322
static int DFBToSDLPixelFormat (DFBSurfacePixelFormat pixelformat, SDL_PixelFormat *format)
slouken@167
   323
{
slouken@167
   324
  format->Amask = format->Rmask = format->Gmask = format->Bmask = 0;
slouken@464
   325
  format->BitsPerPixel = format->BytesPerPixel = 0;
slouken@167
   326
slouken@167
   327
  switch (pixelformat)
slouken@167
   328
    {
slouken@167
   329
    case DSPF_A8:
slouken@167
   330
      format->Amask = 0x000000FF;
slouken@167
   331
      break;
slouken@464
   332
slouken@167
   333
    case DSPF_RGB15:
slouken@167
   334
      format->Rmask = 0x00007C00;
slouken@167
   335
      format->Gmask = 0x000003E0;
slouken@167
   336
      format->Bmask = 0x0000001F;
slouken@167
   337
      break;
slouken@464
   338
slouken@167
   339
    case DSPF_RGB16:
slouken@167
   340
      format->Rmask = 0x0000F800;
slouken@167
   341
      format->Gmask = 0x000007E0;
slouken@167
   342
      format->Bmask = 0x0000001F;
slouken@167
   343
      break;
slouken@464
   344
slouken@167
   345
    case DSPF_ARGB:
slouken@464
   346
      format->Amask = 0; /* apps don't seem to like that:  0xFF000000; */
slouken@167
   347
      /* fall through */
slouken@167
   348
    case DSPF_RGB24:
slouken@167
   349
    case DSPF_RGB32:
slouken@167
   350
      format->Rmask = 0x00FF0000;
slouken@167
   351
      format->Gmask = 0x0000FF00;
slouken@167
   352
      format->Bmask = 0x000000FF;
slouken@167
   353
      break;
slouken@464
   354
slouken@464
   355
    case DSPF_RGB332:
slouken@464
   356
      format->Rmask = 0x000000E0;
slouken@464
   357
      format->Gmask = 0x0000001C;
slouken@464
   358
      format->Bmask = 0x00000003;
slouken@464
   359
slouken@464
   360
      format->palette = GenerateRGB332Palette();
slouken@464
   361
      break;
slouken@464
   362
slouken@167
   363
    default:
slouken@167
   364
      return -1;
slouken@167
   365
    }
slouken@167
   366
slouken@464
   367
  format->BitsPerPixel  = DFB_BYTES_PER_PIXEL(pixelformat) * 8;
slouken@464
   368
  format->BytesPerPixel = DFB_BYTES_PER_PIXEL(pixelformat);
slouken@167
   369
slouken@167
   370
  return 0;
slouken@167
   371
}
slouken@167
   372
slouken@167
   373
slouken@167
   374
int DirectFB_VideoInit(_THIS, SDL_PixelFormat *vformat)
slouken@167
   375
{
slouken@464
   376
  int                      i;
slouken@464
   377
  DFBResult                ret;
slouken@464
   378
  DFBCardCapabilities      caps;
slouken@464
   379
  DFBDisplayLayerConfig    dlc;
slouken@464
   380
  DFBSurfacePixelFormat    format;
slouken@464
   381
  struct DirectFBEnumRect *rect;
slouken@464
   382
  IDirectFB               *dfb    = NULL;
slouken@464
   383
  IDirectFBDisplayLayer   *layer  = NULL;
slouken@464
   384
  IDirectFBEventBuffer    *events = NULL;
slouken@167
   385
slouken@167
   386
slouken@167
   387
  ret = DirectFBInit (NULL, NULL);
slouken@167
   388
  if (ret)
slouken@167
   389
    {
slouken@167
   390
      SetDirectFBerror ("DirectFBInit", ret);
slouken@464
   391
      goto error;
slouken@167
   392
    }
slouken@167
   393
slouken@167
   394
  ret = DirectFBCreate (&dfb);
slouken@167
   395
  if (ret)
slouken@167
   396
    {
slouken@167
   397
      SetDirectFBerror ("DirectFBCreate", ret);
slouken@464
   398
      goto error;
slouken@167
   399
    }
slouken@167
   400
slouken@167
   401
  ret = dfb->GetDisplayLayer (dfb, DLID_PRIMARY, &layer);
slouken@167
   402
  if (ret)
slouken@167
   403
    {
slouken@167
   404
      SetDirectFBerror ("dfb->GetDisplayLayer", ret);
slouken@464
   405
      goto error;
slouken@167
   406
    }
slouken@167
   407
slouken@464
   408
  ret = dfb->CreateEventBuffer (dfb, DICAPS_ALL, &events);
slouken@167
   409
  if (ret)
slouken@167
   410
    {
slouken@286
   411
      SetDirectFBerror ("dfb->CreateEventBuffer", ret);
slouken@464
   412
      goto error;
slouken@167
   413
    }
slouken@167
   414
  
slouken@167
   415
  layer->EnableCursor (layer, 1);
slouken@167
   416
slouken@167
   417
  /* Query layer configuration to determine the current mode and pixelformat */
slouken@167
   418
  layer->GetConfiguration (layer, &dlc);
slouken@167
   419
slouken@464
   420
  /* FIXME: Returning RGB332 as the default mode doesn't work (everything is black) */
slouken@464
   421
  if ((format = dlc.pixelformat) == DSPF_RGB332)
slouken@464
   422
    format = DSPF_RGB16;
slouken@464
   423
slouken@464
   424
  if (DFBToSDLPixelFormat (format, vformat))
slouken@167
   425
    {
slouken@167
   426
      SDL_SetError ("Unsupported pixelformat");
slouken@464
   427
      goto error;
slouken@167
   428
    }
slouken@167
   429
slouken@167
   430
  /* Enumerate the available fullscreen modes */
slouken@167
   431
  ret = dfb->EnumVideoModes (dfb, EnumModesCallback, this);
slouken@167
   432
  if (ret)
slouken@167
   433
    {
slouken@167
   434
      SetDirectFBerror ("dfb->EnumVideoModes", ret);
slouken@464
   435
      goto error;
slouken@167
   436
    }
slouken@464
   437
slouken@464
   438
  HIDDEN->modelist = calloc (HIDDEN->nummodes + 1, sizeof(SDL_Rect *));
slouken@464
   439
  if (!HIDDEN->modelist)
slouken@167
   440
    {
slouken@464
   441
      SDL_OutOfMemory();
slouken@464
   442
      goto error;
slouken@167
   443
    }
slouken@167
   444
slouken@464
   445
  for (i = 0, rect = enumlist; rect; ++i, rect = rect->next )
slouken@464
   446
    {
slouken@464
   447
      HIDDEN->modelist[i] = &rect->r;
slouken@464
   448
    }
slouken@464
   449
slouken@464
   450
  HIDDEN->modelist[i] = NULL;
slouken@464
   451
slouken@464
   452
slouken@167
   453
  /* Query card capabilities to get the video memory size */
slouken@167
   454
  dfb->GetCardCapabilities (dfb, &caps);
slouken@167
   455
slouken@167
   456
  this->info.wm_available = 1;
slouken@167
   457
  this->info.hw_available = 1;
slouken@167
   458
  this->info.blit_hw      = 1;
slouken@167
   459
  this->info.blit_hw_CC   = 1;
slouken@167
   460
  this->info.blit_hw_A    = 1;
slouken@167
   461
  this->info.blit_fill    = 1;
slouken@167
   462
  this->info.video_mem    = caps.video_memory / 1024;
slouken@167
   463
slouken@167
   464
  HIDDEN->initialized = 1;
slouken@167
   465
  HIDDEN->dfb         = dfb;
slouken@167
   466
  HIDDEN->layer       = layer;
slouken@464
   467
  HIDDEN->eventbuffer = events;
slouken@167
   468
slouken@167
   469
  return 0;
slouken@464
   470
slouken@464
   471
 error:
slouken@464
   472
  if (events)
slouken@464
   473
    events->Release (events);
slouken@464
   474
  
slouken@464
   475
  if (layer)
slouken@464
   476
    layer->Release (layer);
slouken@464
   477
slouken@464
   478
  if (dfb)
slouken@464
   479
    dfb->Release (dfb);
slouken@464
   480
slouken@464
   481
  return -1;
slouken@167
   482
}
slouken@167
   483
slouken@167
   484
static SDL_Rect **DirectFB_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
slouken@167
   485
{
slouken@167
   486
  if (flags & SDL_FULLSCREEN)
slouken@464
   487
    return HIDDEN->modelist;
slouken@167
   488
  else
slouken@167
   489
    if (SDLToDFBPixelFormat (format) != DSPF_UNKNOWN)
slouken@167
   490
      return (SDL_Rect**) -1;
slouken@167
   491
slouken@167
   492
  return NULL;
slouken@167
   493
}
slouken@167
   494
slouken@464
   495
static SDL_Surface *DirectFB_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
slouken@167
   496
{
slouken@167
   497
  DFBResult             ret;
slouken@167
   498
  DFBSurfaceDescription dsc;
slouken@167
   499
  DFBSurfacePixelFormat pixelformat;
slouken@167
   500
slouken@167
   501
  fprintf (stderr, "SDL DirectFB_SetVideoMode: %dx%d@%d, flags: 0x%08x\n",
slouken@167
   502
           width, height, bpp, flags);
slouken@167
   503
slouken@167
   504
  flags |= SDL_FULLSCREEN;
slouken@167
   505
slouken@167
   506
  /* Release previous primary surface */
slouken@167
   507
  if (current->hwdata && current->hwdata->surface)
slouken@167
   508
    {
slouken@167
   509
      current->hwdata->surface->Release (current->hwdata->surface);
slouken@167
   510
      current->hwdata->surface = NULL;
slouken@167
   511
    }
slouken@167
   512
  else if (!current->hwdata)
slouken@167
   513
    {
slouken@167
   514
      /* Allocate the hardware acceleration data */
slouken@464
   515
      current->hwdata = (struct private_hwdata *) calloc (1, sizeof(*current->hwdata));
slouken@167
   516
      if (!current->hwdata)
slouken@167
   517
        {
slouken@167
   518
          SDL_OutOfMemory();
slouken@167
   519
          return NULL;
slouken@167
   520
	}
slouken@167
   521
    }
slouken@167
   522
slouken@167
   523
  /* Set cooperative level depending on flag SDL_FULLSCREEN */
slouken@167
   524
  if (flags & SDL_FULLSCREEN)
slouken@167
   525
    {
slouken@167
   526
      ret = HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_FULLSCREEN);
slouken@167
   527
      if (ret)
slouken@167
   528
        {
slouken@167
   529
          DirectFBError ("dfb->SetCooperativeLevel", ret);
slouken@167
   530
          flags &= ~SDL_FULLSCREEN;
slouken@167
   531
        }
slouken@167
   532
    }
slouken@167
   533
  else
slouken@167
   534
    HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
slouken@167
   535
slouken@167
   536
  /* Set video mode */
slouken@167
   537
  ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
slouken@167
   538
  if (ret)
slouken@167
   539
    {
slouken@167
   540
      if (flags & SDL_FULLSCREEN)
slouken@167
   541
        {
slouken@167
   542
          flags &= ~SDL_FULLSCREEN;
slouken@167
   543
          HIDDEN->dfb->SetCooperativeLevel (HIDDEN->dfb, DFSCL_NORMAL);
slouken@167
   544
          ret = HIDDEN->dfb->SetVideoMode (HIDDEN->dfb, width, height, bpp);
slouken@167
   545
        }
slouken@167
   546
slouken@167
   547
      if (ret)
slouken@167
   548
        {
slouken@167
   549
          SetDirectFBerror ("dfb->SetVideoMode", ret);
slouken@167
   550
          return NULL;
slouken@167
   551
        }
slouken@167
   552
    }
slouken@167
   553
slouken@167
   554
  /* Create primary surface */
slouken@464
   555
  dsc.flags       = DSDESC_CAPS | DSDESC_PIXELFORMAT;
slouken@464
   556
  dsc.caps        = DSCAPS_PRIMARY | ((flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0);
slouken@464
   557
  dsc.pixelformat = GetFormatForBpp (bpp, HIDDEN->layer);
slouken@167
   558
slouken@167
   559
  ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &current->hwdata->surface);
slouken@167
   560
  if (ret && (flags & SDL_DOUBLEBUF))
slouken@167
   561
    {
slouken@167
   562
      /* Try without double buffering */
slouken@167
   563
      dsc.caps &= ~DSCAPS_FLIPPING;
slouken@167
   564
      ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &current->hwdata->surface);
slouken@167
   565
    }
slouken@167
   566
  if (ret)
slouken@167
   567
    {
slouken@167
   568
      SetDirectFBerror ("dfb->CreateSurface", ret);
slouken@167
   569
      current->hwdata->surface = NULL;
slouken@167
   570
      return NULL;
slouken@167
   571
    }
slouken@167
   572
slouken@167
   573
  current->w     = width;
slouken@167
   574
  current->h     = height;
slouken@167
   575
  current->flags = SDL_HWSURFACE | SDL_PREALLOC;
slouken@167
   576
slouken@167
   577
  if (flags & SDL_FULLSCREEN)
slouken@167
   578
    {
slouken@167
   579
      current->flags |= SDL_FULLSCREEN;
slouken@167
   580
      this->UpdateRects = DirectFB_DirectUpdate;
slouken@167
   581
    }
slouken@167
   582
  else
slouken@167
   583
    this->UpdateRects = DirectFB_WindowedUpdate;
slouken@167
   584
slouken@167
   585
  if (dsc.caps & DSCAPS_FLIPPING)
slouken@167
   586
    current->flags |= SDL_DOUBLEBUF;
slouken@167
   587
slouken@167
   588
  current->hwdata->surface->GetPixelFormat (current->hwdata->surface, &pixelformat);
slouken@167
   589
  DFBToSDLPixelFormat (pixelformat, current->format);
slouken@167
   590
slouken@167
   591
  return current;
slouken@167
   592
}
slouken@167
   593
slouken@167
   594
static int DirectFB_AllocHWSurface(_THIS, SDL_Surface *surface)
slouken@167
   595
{
slouken@167
   596
  DFBResult             ret;
slouken@167
   597
  DFBSurfaceDescription dsc;
slouken@167
   598
slouken@167
   599
  /*  fprintf(stderr, "SDL: DirectFB_AllocHWSurface (%dx%d@%d, flags: 0x%08x)\n",
slouken@167
   600
      surface->w, surface->h, surface->format->BitsPerPixel, surface->flags);*/
slouken@167
   601
slouken@167
   602
  if (surface->w < 8 || surface->h < 8)
slouken@167
   603
    return -1;
slouken@167
   604
slouken@167
   605
  /* fill surface description */
slouken@167
   606
  dsc.flags  = DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
slouken@167
   607
  dsc.width  = surface->w;
slouken@167
   608
  dsc.height = surface->h;
slouken@464
   609
  dsc.caps   = (surface->flags & SDL_DOUBLEBUF) ? DSCAPS_FLIPPING : 0;
slouken@167
   610
slouken@167
   611
  /* find the right pixelformat */
slouken@167
   612
  dsc.pixelformat = SDLToDFBPixelFormat (surface->format);
slouken@167
   613
  if (dsc.pixelformat == DSPF_UNKNOWN)
slouken@167
   614
    return -1;
slouken@167
   615
slouken@167
   616
  /* Allocate the hardware acceleration data */
slouken@464
   617
  surface->hwdata = (struct private_hwdata *) calloc (1, sizeof(*surface->hwdata));
slouken@167
   618
  if (surface->hwdata == NULL)
slouken@167
   619
    {
slouken@167
   620
      SDL_OutOfMemory();
slouken@167
   621
      return -1;
slouken@167
   622
    }
slouken@167
   623
slouken@167
   624
  /* Create the surface */
slouken@167
   625
  ret = HIDDEN->dfb->CreateSurface (HIDDEN->dfb, &dsc, &surface->hwdata->surface);
slouken@167
   626
  if (ret)
slouken@167
   627
    {
slouken@167
   628
      SetDirectFBerror ("dfb->CreateSurface", ret);
slouken@167
   629
      free (surface->hwdata);
slouken@167
   630
      surface->hwdata = NULL;
slouken@167
   631
      return -1;
slouken@167
   632
    }
slouken@167
   633
slouken@167
   634
  surface->flags |= SDL_HWSURFACE | SDL_PREALLOC;
slouken@167
   635
slouken@167
   636
  return 0;
slouken@167
   637
}
slouken@167
   638
slouken@167
   639
static void DirectFB_FreeHWSurface(_THIS, SDL_Surface *surface)
slouken@167
   640
{
slouken@167
   641
  if (surface->hwdata && HIDDEN->initialized)
slouken@167
   642
    {
slouken@167
   643
      surface->hwdata->surface->Release (surface->hwdata->surface);
slouken@167
   644
      free (surface->hwdata);
slouken@167
   645
      surface->hwdata = NULL;
slouken@167
   646
    }
slouken@167
   647
}
slouken@167
   648
slouken@167
   649
static int DirectFB_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dst)
slouken@167
   650
{
slouken@167
   651
  /*  fprintf(stderr, "SDL: DirectFB_CheckHWBlit (src->hwdata: %p, dst->hwdata: %p)\n",
slouken@167
   652
      src->hwdata, dst->hwdata);*/
slouken@167
   653
slouken@167
   654
  if (!src->hwdata || !dst->hwdata)
slouken@167
   655
    return 0;
slouken@167
   656
slouken@167
   657
  src->flags |= SDL_HWACCEL;
slouken@167
   658
  src->map->hw_blit = DirectFB_HWAccelBlit;
slouken@167
   659
slouken@167
   660
  return 1;
slouken@167
   661
}
slouken@167
   662
slouken@167
   663
static int DirectFB_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
slouken@167
   664
                                SDL_Surface *dst, SDL_Rect *dstrect)
slouken@167
   665
{
slouken@464
   666
  DFBSurfaceBlittingFlags flags = DSBLIT_NOFX;
slouken@167
   667
slouken@464
   668
  DFBRectangle sr = { srcrect->x, srcrect->y, srcrect->w, srcrect->h };
slouken@464
   669
  DFBRectangle dr = { dstrect->x, dstrect->y, dstrect->w, dstrect->h };
slouken@167
   670
slouken@464
   671
  IDirectFBSurface *surface = dst->hwdata->surface;
slouken@167
   672
slouken@167
   673
  if (src->flags & SDL_SRCCOLORKEY)
slouken@167
   674
    {
slouken@167
   675
      flags |= DSBLIT_SRC_COLORKEY;
slouken@219
   676
      DirectFB_SetHWColorKey (NULL, src, src->format->colorkey);
slouken@167
   677
    }
slouken@167
   678
slouken@167
   679
  if (src->flags & SDL_SRCALPHA)
slouken@167
   680
    {
slouken@167
   681
      flags |= DSBLIT_BLEND_COLORALPHA;
slouken@167
   682
      surface->SetColor (surface, 0xff, 0xff, 0xff, src->format->alpha);
slouken@167
   683
    }
slouken@167
   684
slouken@167
   685
  surface->SetBlittingFlags (surface, flags);
slouken@167
   686
slouken@167
   687
  if (sr.w == dr.w && sr.h == dr.h)
slouken@167
   688
    surface->Blit (surface, src->hwdata->surface, &sr, dr.x, dr.y);
slouken@167
   689
  else
slouken@167
   690
    surface->StretchBlit (surface, src->hwdata->surface, &sr, &dr);
slouken@167
   691
slouken@167
   692
  return 0;
slouken@167
   693
}
slouken@167
   694
slouken@167
   695
static int DirectFB_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color)
slouken@167
   696
{
slouken@167
   697
  SDL_PixelFormat  *fmt     = dst->format;
slouken@167
   698
  IDirectFBSurface *surface = dst->hwdata->surface;
slouken@167
   699
slouken@167
   700
  /* ugly */
slouken@167
   701
  surface->SetColor (surface,
slouken@167
   702
                     (color & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
slouken@167
   703
                     (color & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
slouken@167
   704
                     (color & fmt->Bmask) << (fmt->Bloss - fmt->Bshift), 0xFF);
slouken@167
   705
  surface->FillRectangle (surface, dstrect->x, dstrect->y, dstrect->w, dstrect->h);
slouken@167
   706
slouken@167
   707
  return 0;
slouken@167
   708
}
slouken@167
   709
slouken@219
   710
static int DirectFB_SetHWColorKey(_THIS, SDL_Surface *src, Uint32 key)
slouken@167
   711
{
slouken@219
   712
  SDL_PixelFormat  *fmt     = src->format;
slouken@219
   713
  IDirectFBSurface *surface = src->hwdata->surface;
slouken@219
   714
slouken@219
   715
  /* ugly */
slouken@219
   716
  surface->SetSrcColorKey (surface,
slouken@219
   717
                           (key & fmt->Rmask) >> (fmt->Rshift - fmt->Rloss),
slouken@219
   718
                           (key & fmt->Gmask) >> (fmt->Gshift - fmt->Gloss),
slouken@219
   719
                           (key & fmt->Bmask) << (fmt->Bloss - fmt->Bshift));
slouken@219
   720
slouken@167
   721
  return 0;
slouken@167
   722
}
slouken@167
   723
slouken@167
   724
static int DirectFB_SetHWAlpha(_THIS, SDL_Surface *surface, Uint8 alpha)
slouken@167
   725
{
slouken@167
   726
  return 0;
slouken@167
   727
}
slouken@167
   728
slouken@167
   729
static int DirectFB_FlipHWSurface(_THIS, SDL_Surface *surface)
slouken@167
   730
{
slouken@167
   731
  return surface->hwdata->surface->Flip (surface->hwdata->surface, NULL, DSFLIP_WAITFORSYNC);
slouken@167
   732
}
slouken@167
   733
slouken@167
   734
static int DirectFB_LockHWSurface(_THIS, SDL_Surface *surface)
slouken@167
   735
{
slouken@167
   736
  DFBResult  ret;
slouken@167
   737
  void      *data;
slouken@167
   738
  int        pitch;
slouken@167
   739
slouken@167
   740
  ret = surface->hwdata->surface->Lock (surface->hwdata->surface,
slouken@167
   741
                                        DSLF_WRITE, &data, &pitch);
slouken@167
   742
  if (ret)
slouken@167
   743
    {
slouken@167
   744
      SetDirectFBerror ("surface->Lock", ret);
slouken@167
   745
      return -1;
slouken@167
   746
    }
slouken@167
   747
slouken@167
   748
  surface->pixels = data;
slouken@167
   749
  surface->pitch  = pitch;
slouken@167
   750
slouken@167
   751
  return 0;
slouken@167
   752
}
slouken@167
   753
slouken@167
   754
static void DirectFB_UnlockHWSurface(_THIS, SDL_Surface *surface)
slouken@167
   755
{
slouken@167
   756
  surface->hwdata->surface->Unlock (surface->hwdata->surface);
slouken@167
   757
  surface->pixels = NULL;
slouken@167
   758
}
slouken@167
   759
slouken@167
   760
static void DirectFB_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@167
   761
{
slouken@167
   762
}
slouken@167
   763
slouken@167
   764
static void DirectFB_WindowedUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@167
   765
{
slouken@286
   766
  DFBRegion         region;
slouken@286
   767
  int               i;
slouken@286
   768
  int               region_valid = 0;
slouken@167
   769
  IDirectFBSurface *surface = this->screen->hwdata->surface;
slouken@167
   770
slouken@286
   771
  for (i=0; i<numrects; ++i)
slouken@167
   772
    {
slouken@167
   773
      int x2, y2;
slouken@167
   774
slouken@286
   775
      if ( ! rects[i].w ) /* Clipped? */
slouken@286
   776
        continue;
slouken@167
   777
slouken@286
   778
      x2 = rects[i].x + rects[i].w - 1;
slouken@286
   779
      y2 = rects[i].y + rects[i].h - 1;
slouken@167
   780
slouken@286
   781
      if (region_valid)
slouken@286
   782
        {
slouken@286
   783
          if (rects[i].x < region.x1)
slouken@286
   784
            region.x1 = rects[i].x;
slouken@167
   785
slouken@286
   786
          if (rects[i].y < region.y1)
slouken@286
   787
            region.y1 = rects[i].y;
slouken@167
   788
slouken@286
   789
          if (x2 > region.x2)
slouken@286
   790
            region.x2 = x2;
slouken@167
   791
slouken@286
   792
          if (y2 > region.y2)
slouken@286
   793
            region.y2 = y2;
slouken@286
   794
        }
slouken@286
   795
      else
slouken@286
   796
        {
slouken@286
   797
            region.x1 = rects[i].x;
slouken@286
   798
            region.y1 = rects[i].y;
slouken@286
   799
            region.x2 = x2;
slouken@286
   800
            region.y2 = y2;
slouken@286
   801
slouken@286
   802
            region_valid = 1;
slouken@286
   803
        }
slouken@167
   804
    }
slouken@167
   805
slouken@286
   806
  if (region_valid)
slouken@286
   807
    surface->Flip (surface, &region, DSFLIP_WAITFORSYNC);
slouken@167
   808
}
slouken@167
   809
slouken@167
   810
int DirectFB_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
slouken@167
   811
{
slouken@167
   812
  fprintf(stderr, "SDL: Unimplemented DirectFB_SetColors!\n");
slouken@464
   813
  return -1;
slouken@167
   814
}
slouken@167
   815
	
slouken@167
   816
void DirectFB_VideoQuit(_THIS)
slouken@167
   817
{
slouken@464
   818
  struct DirectFBEnumRect *rect = enumlist;
slouken@167
   819
slouken@464
   820
  if (HIDDEN->eventbuffer)
slouken@464
   821
    {
slouken@464
   822
      HIDDEN->eventbuffer->Release (HIDDEN->eventbuffer);
slouken@464
   823
      HIDDEN->eventbuffer = NULL;
slouken@464
   824
    }
slouken@167
   825
slouken@464
   826
  if (HIDDEN->layer)
slouken@167
   827
    {
slouken@464
   828
      HIDDEN->layer->Release (HIDDEN->layer);
slouken@464
   829
      HIDDEN->layer = NULL;
slouken@167
   830
    }
slouken@167
   831
slouken@464
   832
  if (HIDDEN->dfb)
slouken@464
   833
    {
slouken@464
   834
      HIDDEN->dfb->Release (HIDDEN->dfb);
slouken@464
   835
      HIDDEN->dfb = NULL;
slouken@464
   836
    }
slouken@464
   837
slouken@464
   838
  /* Free video mode list */
slouken@464
   839
  if (HIDDEN->modelist)
slouken@464
   840
    {
slouken@464
   841
      free (HIDDEN->modelist);
slouken@464
   842
      HIDDEN->modelist = NULL;
slouken@464
   843
    }
slouken@464
   844
slouken@464
   845
  /* Free mode enumeration list */
slouken@464
   846
  while (rect)
slouken@464
   847
    {
slouken@464
   848
      struct DirectFBEnumRect *next = rect->next;
slouken@464
   849
      free (rect);
slouken@464
   850
      rect = next;
slouken@464
   851
    }
slouken@464
   852
  enumlist = NULL;
slouken@464
   853
slouken@167
   854
  HIDDEN->initialized = 0;
slouken@167
   855
}
slouken@167
   856
slouken@167
   857
void DirectFB_FinalQuit(void) 
slouken@167
   858
{
slouken@167
   859
}