src/video/ggi/SDL_ggivideo.c
changeset 1981 3f21778e7433
parent 1980 0e7becb566ee
child 1982 3b4ce57c6215
equal deleted inserted replaced
1980:0e7becb566ee 1981:3f21778e7433
     1 /*
       
     2     SDL - Simple DirectMedia Layer
       
     3     Copyright (C) 1997-2006 Sam Lantinga
       
     4 
       
     5     This library is free software; you can redistribute it and/or
       
     6     modify it under the terms of the GNU Lesser General Public
       
     7     License as published by the Free Software Foundation; either
       
     8     version 2.1 of the License, or (at your option) any later version.
       
     9 
       
    10     This library is distributed in the hope that it will be useful,
       
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13     Lesser General Public License for more details.
       
    14 
       
    15     You should have received a copy of the GNU Lesser General Public
       
    16     License along with this library; if not, write to the Free Software
       
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
       
    18 
       
    19     Sam Lantinga
       
    20     slouken@libsdl.org
       
    21 */
       
    22 #include "SDL_config.h"
       
    23 
       
    24 /* GGI-based SDL video driver implementation.
       
    25 */
       
    26 
       
    27 #include <fcntl.h>
       
    28 #include <unistd.h>
       
    29 #include <sys/mman.h>
       
    30 
       
    31 #include <ggi/ggi.h>
       
    32 #include <ggi/gii.h>
       
    33 
       
    34 #include "SDL_video.h"
       
    35 #include "SDL_mouse.h"
       
    36 #include "../SDL_sysvideo.h"
       
    37 #include "../SDL_pixels_c.h"
       
    38 #include "../../events/SDL_events_c.h"
       
    39 #include "SDL_ggivideo.h"
       
    40 #include "SDL_ggimouse_c.h"
       
    41 #include "SDL_ggievents_c.h"
       
    42 
       
    43 
       
    44 struct private_hwdata
       
    45 {
       
    46     ggi_visual_t vis;
       
    47 };
       
    48 
       
    49 ggi_visual_t VIS;
       
    50 
       
    51 /* Initialization/Query functions */
       
    52 static int GGI_VideoInit(_THIS, SDL_PixelFormat * vformat);
       
    53 static SDL_Rect **GGI_ListModes(_THIS, SDL_PixelFormat * format,
       
    54                                 Uint32 flags);
       
    55 static SDL_Surface *GGI_SetVideoMode(_THIS, SDL_Surface * current, int width,
       
    56                                      int height, int bpp, Uint32 flags);
       
    57 static int GGI_SetColors(_THIS, int firstcolor, int ncolors,
       
    58                          SDL_Color * colors);
       
    59 static void GGI_VideoQuit(_THIS);
       
    60 
       
    61 /* Hardware surface functions */
       
    62 static int GGI_AllocHWSurface(_THIS, SDL_Surface * surface);
       
    63 static int GGI_LockHWSurface(_THIS, SDL_Surface * surface);
       
    64 static void GGI_UnlockHWSurface(_THIS, SDL_Surface * surface);
       
    65 static void GGI_FreeHWSurface(_THIS, SDL_Surface * surface);
       
    66 
       
    67 /* GGI driver bootstrap functions */
       
    68 
       
    69 static int
       
    70 GGI_Available(void)
       
    71 {
       
    72     ggi_visual_t *vis;
       
    73 
       
    74     vis = NULL;
       
    75     if (ggiInit() == 0) {
       
    76         vis = ggiOpen(NULL);
       
    77         if (vis != NULL) {
       
    78             ggiClose(vis);
       
    79         }
       
    80     }
       
    81     return (vis != NULL);
       
    82 }
       
    83 
       
    84 static void
       
    85 GGI_DeleteDevice(SDL_VideoDevice * device)
       
    86 {
       
    87     SDL_free(device->hidden);
       
    88     SDL_free(device);
       
    89 }
       
    90 
       
    91 static SDL_VideoDevice *
       
    92 GGI_CreateDevice(int devindex)
       
    93 {
       
    94     SDL_VideoDevice *device;
       
    95 
       
    96     /* Initialize all variables that we clean on shutdown */
       
    97     device = (SDL_VideoDevice *) SDL_malloc(sizeof(SDL_VideoDevice));
       
    98     if (device) {
       
    99         SDL_memset(device, 0, (sizeof *device));
       
   100         device->hidden = (struct SDL_PrivateVideoData *)
       
   101             SDL_malloc((sizeof *device->hidden));
       
   102     }
       
   103     if ((device == NULL) || (device->hidden == NULL)) {
       
   104         SDL_OutOfMemory();
       
   105         if (device) {
       
   106             SDL_free(device);
       
   107         }
       
   108         return (0);
       
   109     }
       
   110     SDL_memset(device->hidden, 0, (sizeof *device->hidden));
       
   111 
       
   112     /* Set the function pointers */
       
   113     device->VideoInit = GGI_VideoInit;
       
   114     device->ListModes = GGI_ListModes;
       
   115     device->SetVideoMode = GGI_SetVideoMode;
       
   116     device->SetColors = GGI_SetColors;
       
   117     device->UpdateRects = NULL;
       
   118     device->VideoQuit = GGI_VideoQuit;
       
   119     device->AllocHWSurface = GGI_AllocHWSurface;
       
   120     device->CheckHWBlit = NULL;
       
   121     device->FillHWRect = NULL;
       
   122     device->SetHWColorKey = NULL;
       
   123     device->SetHWAlpha = NULL;
       
   124     device->LockHWSurface = GGI_LockHWSurface;
       
   125     device->UnlockHWSurface = GGI_UnlockHWSurface;
       
   126     device->FlipHWSurface = NULL;
       
   127     device->FreeHWSurface = GGI_FreeHWSurface;
       
   128     device->SetCaption = NULL;
       
   129     device->SetIcon = NULL;
       
   130     device->IconifyWindow = NULL;
       
   131     device->GrabInput = NULL;
       
   132     device->GetWMInfo = NULL;
       
   133     device->InitOSKeymap = GGI_InitOSKeymap;
       
   134     device->PumpEvents = GGI_PumpEvents;
       
   135 
       
   136     device->free = GGI_DeleteDevice;
       
   137 
       
   138     return device;
       
   139 }
       
   140 
       
   141 VideoBootStrap GGI_bootstrap = {
       
   142     "ggi", "General Graphics Interface (GGI)",
       
   143     GGI_Available, GGI_CreateDevice
       
   144 };
       
   145 
       
   146 
       
   147 static SDL_Rect video_mode;
       
   148 static SDL_Rect *SDL_modelist[4] = { NULL, NULL, NULL, NULL };
       
   149 
       
   150 int
       
   151 GGI_VideoInit(_THIS, SDL_PixelFormat * vformat)
       
   152 {
       
   153     ggi_mode mode = {
       
   154         1,
       
   155         {GGI_AUTO, GGI_AUTO},
       
   156         {GGI_AUTO, GGI_AUTO},
       
   157         {0, 0},
       
   158         GT_AUTO,
       
   159         {GGI_AUTO, GGI_AUTO}
       
   160     };
       
   161     struct private_hwdata *priv;
       
   162     ggi_color pal[256], map[256];
       
   163     const ggi_directbuffer *db;
       
   164     int err, num_bufs;
       
   165     ggi_pixel white, black;
       
   166 
       
   167     priv = SDL_malloc(sizeof(struct private_hwdata));
       
   168     if (priv == NULL) {
       
   169         SDL_SetError("Unhandled GGI mode type!\n");
       
   170         GGI_VideoQuit(NULL);
       
   171     }
       
   172 
       
   173     if (ggiInit() != 0) {
       
   174         SDL_SetError("Unable to initialize GGI!\n");
       
   175         GGI_VideoQuit(NULL);
       
   176     }
       
   177 
       
   178     VIS = ggiOpen(NULL);
       
   179     if (VIS == NULL) {
       
   180         SDL_SetError("Unable to open default GGI visual!\n");
       
   181         ggiExit();
       
   182         GGI_VideoQuit(NULL);
       
   183     }
       
   184 
       
   185     ggiSetFlags(VIS, GGIFLAG_ASYNC);
       
   186 
       
   187     /* Validate mode, autodetecting any GGI_AUTO or GT_AUTO fields */
       
   188     ggiCheckMode(VIS, &mode);
       
   189 
       
   190     /* At this point we should have a valid mode - try to set it */
       
   191     err = ggiSetMode(VIS, &mode);
       
   192 
       
   193     /* If we couldn't set _any_ modes, something is very wrong */
       
   194     if (err) {
       
   195         SDL_SetError("Can't set a mode!\n");
       
   196         ggiClose(VIS);
       
   197         ggiExit();
       
   198         GGI_VideoQuit(NULL);
       
   199     }
       
   200 
       
   201     /* Determine the current screen size */
       
   202     this->info.current_w = mode.virt.x;
       
   203     this->info.current_h = mode.virt.y;
       
   204 
       
   205     /* Set a palette for palletized modes */
       
   206     if (GT_SCHEME(mode.graphtype) == GT_PALETTE) {
       
   207         ggiSetColorfulPalette(VIS);
       
   208         ggiGetPalette(VIS, 0, 1 << vformat->BitsPerPixel, pal);
       
   209     }
       
   210 
       
   211     /* Now we try to get the DirectBuffer info, which determines whether
       
   212      * SDL can access hardware surfaces directly. */
       
   213 
       
   214     num_bufs = ggiDBGetNumBuffers(VIS);
       
   215 
       
   216     if (num_bufs > 0) {
       
   217         db = ggiDBGetBuffer(VIS, 0);    /* Only handle one DB for now */
       
   218 
       
   219         vformat->BitsPerPixel = db->buffer.plb.pixelformat->depth;
       
   220 
       
   221         vformat->Rmask = db->buffer.plb.pixelformat->red_mask;
       
   222         vformat->Gmask = db->buffer.plb.pixelformat->green_mask;
       
   223         vformat->Bmask = db->buffer.plb.pixelformat->blue_mask;
       
   224 
       
   225         /* Fill in our hardware acceleration capabilities */
       
   226 
       
   227         this->info.wm_available = 0;
       
   228         this->info.hw_available = 1;
       
   229         this->info.video_mem = db->buffer.plb.stride * mode.virt.y;
       
   230     }
       
   231 
       
   232     video_mode.x = 0;
       
   233     video_mode.y = 0;
       
   234     video_mode.w = mode.virt.x;
       
   235     video_mode.h = mode.virt.y;
       
   236     SDL_modelist[((vformat->BitsPerPixel + 7) / 8) - 1] = &video_mode;
       
   237 
       
   238     /* We're done! */
       
   239     return (0);
       
   240 }
       
   241 
       
   242 static SDL_Rect **
       
   243 GGI_ListModes(_THIS, SDL_PixelFormat * format, Uint32 flags)
       
   244 {
       
   245     return (&SDL_modelist[((format->BitsPerPixel + 7) / 8) - 1]);
       
   246 }
       
   247 
       
   248 /* Various screen update functions available */
       
   249 static void GGI_DirectUpdate(_THIS, int numrects, SDL_Rect * rects);
       
   250 
       
   251 SDL_Surface *
       
   252 GGI_SetVideoMode(_THIS, SDL_Surface * current, int width, int height,
       
   253                  int bpp, Uint32 flags)
       
   254 {
       
   255     ggi_mode mode = {
       
   256         1,
       
   257         {GGI_AUTO, GGI_AUTO},
       
   258         {GGI_AUTO, GGI_AUTO},
       
   259         {0, 0},
       
   260         GT_AUTO,
       
   261         {GGI_AUTO, GGI_AUTO}
       
   262     };
       
   263     const ggi_directbuffer *db;
       
   264     ggi_color pal[256];
       
   265     int err;
       
   266 
       
   267     fprintf(stderr, "GGI_SetVideoMode()\n");
       
   268 
       
   269     mode.visible.x = mode.virt.x = width;
       
   270     mode.visible.y = mode.virt.y = height;
       
   271 
       
   272     /* Translate requested SDL bit depth into a GGI mode */
       
   273     switch (bpp) {
       
   274     case 1:
       
   275         mode.graphtype = GT_1BIT;
       
   276         break;
       
   277     case 2:
       
   278         mode.graphtype = GT_2BIT;
       
   279         break;
       
   280     case 4:
       
   281         mode.graphtype = GT_4BIT;
       
   282         break;
       
   283     case 8:
       
   284         mode.graphtype = GT_8BIT;
       
   285         break;
       
   286     case 15:
       
   287         mode.graphtype = GT_15BIT;
       
   288         break;
       
   289     case 16:
       
   290         mode.graphtype = GT_16BIT;
       
   291         break;
       
   292     case 24:
       
   293         mode.graphtype = GT_24BIT;
       
   294         break;
       
   295     case 32:
       
   296         mode.graphtype = GT_32BIT;
       
   297         break;
       
   298     default:
       
   299         SDL_SetError("Unknown SDL bit depth, using GT_AUTO....\n");
       
   300         mode.graphtype = GT_AUTO;
       
   301     }
       
   302 
       
   303     /* Validate mode, autodetecting any GGI_AUTO or GT_AUTO fields */
       
   304     ggiCheckMode(VIS, &mode);
       
   305 
       
   306     /* At this point we should have a valid mode - try to set it */
       
   307     err = ggiSetMode(VIS, &mode);
       
   308 
       
   309     /* If we couldn't set _any_ modes, something is very wrong */
       
   310     if (err) {
       
   311         SDL_SetError("Can't set a mode!\n");
       
   312         ggiClose(VIS);
       
   313         ggiExit();
       
   314         GGI_VideoQuit(NULL);
       
   315     }
       
   316 
       
   317     /* Set a palette for palletized modes */
       
   318     if (GT_SCHEME(mode.graphtype) == GT_PALETTE) {
       
   319         ggiSetColorfulPalette(VIS);
       
   320         ggiGetPalette(VIS, 0, 1 << bpp, pal);
       
   321     }
       
   322 
       
   323     db = ggiDBGetBuffer(VIS, 0);
       
   324 
       
   325     /* Set up the new mode framebuffer */
       
   326     current->flags = (SDL_FULLSCREEN | SDL_HWSURFACE);
       
   327     current->w = mode.virt.x;
       
   328     current->h = mode.virt.y;
       
   329     current->pitch = db->buffer.plb.stride;
       
   330     current->pixels = db->read;
       
   331 
       
   332     /* Set the blit function */
       
   333     this->UpdateRects = GGI_DirectUpdate;
       
   334 
       
   335     /* We're done */
       
   336     return (current);
       
   337 }
       
   338 
       
   339 static int
       
   340 GGI_AllocHWSurface(_THIS, SDL_Surface * surface)
       
   341 {
       
   342     return (-1);
       
   343 }
       
   344 static void
       
   345 GGI_FreeHWSurface(_THIS, SDL_Surface * surface)
       
   346 {
       
   347     return;
       
   348 }
       
   349 static int
       
   350 GGI_LockHWSurface(_THIS, SDL_Surface * surface)
       
   351 {
       
   352     return (0);
       
   353 }
       
   354 static void
       
   355 GGI_UnlockHWSurface(_THIS, SDL_Surface * surface)
       
   356 {
       
   357     return;
       
   358 }
       
   359 
       
   360 static void
       
   361 GGI_DirectUpdate(_THIS, int numrects, SDL_Rect * rects)
       
   362 {
       
   363     int i;
       
   364 
       
   365 /*	ggiFlush(VIS); */
       
   366 
       
   367     for (i = 0; i < numrects; i++) {
       
   368         ggiFlushRegion(VIS, rects[i].x, rects[i].y, rects[i].w, rects[i].h);
       
   369     }
       
   370     return;
       
   371 }
       
   372 
       
   373 int
       
   374 GGI_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color * colors)
       
   375 {
       
   376     int i;
       
   377     ggi_color pal[256];
       
   378 
       
   379     /* Set up the colormap */
       
   380     for (i = 0; i < ncolors; i++) {
       
   381         pal[i].r = (colors[i].r << 8) | colors[i].r;
       
   382         pal[i].g = (colors[i].g << 8) | colors[i].g;
       
   383         pal[i].b = (colors[i].b << 8) | colors[i].b;
       
   384     }
       
   385 
       
   386     ggiSetPalette(VIS, firstcolor, ncolors, pal);
       
   387 
       
   388     return 1;
       
   389 }
       
   390 
       
   391 void
       
   392 GGI_VideoQuit(_THIS)
       
   393 {
       
   394 }
       
   395 void
       
   396 GGI_FinalQuit(void)
       
   397 {
       
   398 }
       
   399 
       
   400 /* vi: set ts=4 sw=4 expandtab: */