src/video/ps2gs/SDL_gsvideo.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 07 Dec 2008 22:37:40 +0000
changeset 2853 6258fa7cd300
parent 2735 204be4fc2726
child 2859 99210400e8b9
permissions -rw-r--r--
Fixed picking blit function when RLE fails
     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 /* Framebuffer console based SDL video driver implementation.
    25 */
    26 
    27 #include <fcntl.h>
    28 #include <unistd.h>
    29 #include <sys/ioctl.h>
    30 #include <sys/mman.h>
    31 
    32 #include "SDL_video.h"
    33 #include "SDL_mouse.h"
    34 #include "../SDL_sysvideo.h"
    35 #include "../SDL_pixels_c.h"
    36 #include "../../events/SDL_events_c.h"
    37 #include "../SDL_cursor_c.h"
    38 #include "SDL_gsvideo.h"
    39 #include "SDL_gsmouse_c.h"
    40 #include "SDL_gsevents_c.h"
    41 #include "SDL_gsyuv_c.h"
    42 
    43 
    44 /* Initialization/Query functions */
    45 static int GS_VideoInit(_THIS, SDL_PixelFormat * vformat);
    46 static SDL_Rect **GS_ListModes(_THIS, SDL_PixelFormat * format, Uint32 flags);
    47 static SDL_Surface *GS_SetVideoMode(_THIS, SDL_Surface * current, int width,
    48                                     int height, int bpp, Uint32 flags);
    49 static int GS_SetColors(_THIS, int firstcolor, int ncolors,
    50                         SDL_Color * colors);
    51 static void GS_VideoQuit(_THIS);
    52 
    53 /* Hardware surface functions */
    54 static int GS_AllocHWSurface(_THIS, SDL_Surface * surface);
    55 static int GS_LockHWSurface(_THIS, SDL_Surface * surface);
    56 static void GS_UnlockHWSurface(_THIS, SDL_Surface * surface);
    57 static void GS_FreeHWSurface(_THIS, SDL_Surface * surface);
    58 
    59 /* GS driver bootstrap functions */
    60 
    61 static int
    62 GS_Available(void)
    63 {
    64     int console, memory;
    65 
    66     console = open(PS2_DEV_GS, O_RDWR, 0);
    67     if (console >= 0) {
    68         close(console);
    69     }
    70     memory = open(PS2_DEV_MEM, O_RDWR, 0);
    71     if (memory >= 0) {
    72         close(memory);
    73     }
    74     return ((console >= 0) && (memory >= 0));
    75 }
    76 
    77 static void
    78 GS_DeleteDevice(SDL_VideoDevice * device)
    79 {
    80     SDL_free(device->hidden);
    81     SDL_free(device);
    82 }
    83 
    84 static SDL_VideoDevice *
    85 GS_CreateDevice(int devindex)
    86 {
    87     SDL_VideoDevice *this;
    88 
    89     /* Initialize all variables that we clean on shutdown */
    90     this = (SDL_VideoDevice *) SDL_malloc(sizeof(SDL_VideoDevice));
    91     if (this) {
    92         SDL_memset(this, 0, (sizeof *this));
    93         this->hidden = (struct SDL_PrivateVideoData *)
    94             SDL_malloc((sizeof *this->hidden));
    95     }
    96     if ((this == NULL) || (this->hidden == NULL)) {
    97         SDL_OutOfMemory();
    98         if (this) {
    99             SDL_free(this);
   100         }
   101         return (0);
   102     }
   103     SDL_memset(this->hidden, 0, (sizeof *this->hidden));
   104     mouse_fd = -1;
   105     keyboard_fd = -1;
   106 
   107     /* Set the function pointers */
   108     this->VideoInit = GS_VideoInit;
   109     this->ListModes = GS_ListModes;
   110     this->SetVideoMode = GS_SetVideoMode;
   111     this->CreateYUVOverlay = GS_CreateYUVOverlay;
   112     this->SetColors = GS_SetColors;
   113     this->UpdateRects = NULL;
   114     this->VideoQuit = GS_VideoQuit;
   115     this->AllocHWSurface = GS_AllocHWSurface;
   116     this->CheckHWBlit = NULL;
   117     this->FillHWRect = NULL;
   118     this->SetHWColorKey = NULL;
   119     this->SetHWAlpha = NULL;
   120     this->LockHWSurface = GS_LockHWSurface;
   121     this->UnlockHWSurface = GS_UnlockHWSurface;
   122     this->FlipHWSurface = NULL;
   123     this->FreeHWSurface = GS_FreeHWSurface;
   124     this->SetIcon = NULL;
   125     this->SetCaption = NULL;
   126     this->GetWMInfo = NULL;
   127     this->FreeWMCursor = GS_FreeWMCursor;
   128     this->CreateWMCursor = GS_CreateWMCursor;
   129     this->ShowWMCursor = GS_ShowWMCursor;
   130     this->MoveWMCursor = GS_MoveWMCursor;
   131     this->InitOSKeymap = GS_InitOSKeymap;
   132     this->PumpEvents = GS_PumpEvents;
   133 
   134     this->free = GS_DeleteDevice;
   135 
   136     return this;
   137 }
   138 
   139 VideoBootStrap PS2GS_bootstrap = {
   140     "ps2gs", "PlayStation 2 Graphics Synthesizer",
   141     GS_Available, GS_CreateDevice
   142 };
   143 
   144 /* These are the pixel formats for the 32, 24, and 16 bit video modes */
   145 static struct
   146 {
   147     int bpp;
   148     Uint32 r;
   149     Uint32 g;
   150     Uint32 b;
   151 } GS_pixelmasks[] = {
   152     {
   153         32, 0x000000FF,         /* RGB little-endian */
   154     0x0000FF00, 0x00FF0000}, {
   155         24, 0x000000FF,         /* RGB little-endian */
   156     0x0000FF00, 0x00FF0000}, {
   157         16, 0x0000001f,         /* RGB little-endian */
   158 0x000003e0, 0x00007c00},};
   159 
   160 /* This is a mapping from SDL bytes-per-pixel to GS pixel format */
   161 static int GS_formatmap[] = {
   162     -1,                         /* 0 bpp, not a legal value */
   163     -1,                         /* 8 bpp, not supported (yet?) */
   164     PS2_GS_PSMCT16,             /* 16 bpp */
   165     PS2_GS_PSMCT24,             /* 24 bpp */
   166     PS2_GS_PSMCT32              /* 32 bpp */
   167 };
   168 
   169 static unsigned long long head_tags[] __attribute__ ((aligned(16))) = {
   170     4 | (1LL << 60),            /* GIFtag */
   171         0x0e,                   /* A+D */
   172         0,                      /* 2 */
   173         PS2_GS_BITBLTBUF, 0,    /* 4 */
   174         PS2_GS_TRXPOS, 0,       /* 6 */
   175         PS2_GS_TRXREG, 0,       /* 8 */
   176 PS2_GS_TRXDIR};
   177 
   178 #define MAXIMG		(32767 * 16)
   179 #define MAXTAGS		8
   180 
   181 static inline int
   182 loadimage_nonblock(int fd, struct ps2_image *image, int size,
   183                    unsigned long long *hm, unsigned long long *im)
   184 {
   185     struct ps2_plist plist;
   186     struct ps2_packet packet[1 + MAXTAGS * 2];
   187     int isize;
   188     int pnum, it, eop;
   189     char *data;
   190 
   191     /* initialize the variables */
   192     data = (char *) image->ptr;
   193     pnum = it = eop = 0;
   194     plist.packet = packet;
   195 
   196     /* make BITBLT packet */
   197     packet[pnum].ptr = hm;
   198     packet[pnum].len = sizeof(head_tags);
   199     pnum++;
   200     hm[2] = ((unsigned long long) image->fbp << 32) |
   201         ((unsigned long long) image->fbw << 48) |
   202         ((unsigned long long) image->psm << 56);
   203     hm[4] = ((unsigned long long) image->x << 32) |
   204         ((unsigned long long) image->y << 48);
   205     hm[6] = (unsigned long long) image->w |
   206         ((unsigned long long) image->h << 32);
   207 
   208     /* make image mode tags */
   209     while (!eop) {
   210         isize = size > MAXIMG ? MAXIMG : size;
   211         size -= isize;
   212         eop = (size == 0);
   213 
   214         packet[pnum].ptr = &im[it];
   215         packet[pnum].len = sizeof(unsigned long long) * 2;
   216         pnum++;
   217         im[it++] = (isize >> 4) | (eop ? (1 << 15) : 0) | (2LL << 58);
   218         im[it++] = 0;
   219 
   220         packet[pnum].ptr = (void *) data;
   221         packet[pnum].len = isize;
   222         pnum++;
   223         data += isize;
   224     }
   225     plist.num = pnum;
   226 
   227     return ioctl(fd, PS2IOC_SENDL, &plist);
   228 }
   229 
   230 static unsigned long long tex_tags[] __attribute__ ((aligned(16))) = {
   231     3 | (1LL << 60),            /* GIFtag */
   232         0x0e,                   /* A+D */
   233         0,                      /* 2 */
   234 PS2_GS_TEX0_1, (1 << 5) + (1 << 6), PS2_GS_TEX1_1, 0, PS2_GS_TEXFLUSH};
   235 
   236 static unsigned long long scale_tags[] __attribute__ ((aligned(16))) = {
   237     5 | (1LL << 60),            /* GIFtag */
   238         0x0e,                   /* A+D */
   239         6 + (1 << 4) + (1 << 8), PS2_GS_PRIM, ((unsigned long long) 0 * 16) + (((unsigned long long) 0 * 16) << 16), PS2_GS_UV, ((unsigned long long) 0 * 16) + (((unsigned long long) 0 * 16) << 16), PS2_GS_XYZ2, 0,  /* 8 */
   240         PS2_GS_UV, 0,           /* 10 */
   241 PS2_GS_XYZ2};
   242 
   243 
   244 int
   245 scaleimage_nonblock(int fd, unsigned long long *tm, unsigned long long *sm)
   246 {
   247     struct ps2_plist plist;
   248     struct ps2_packet packet[2];
   249 
   250     /* initialize the variables */
   251     plist.num = 2;
   252     plist.packet = packet;
   253 
   254     packet[0].ptr = tm;
   255     packet[0].len = sizeof(tex_tags);
   256     packet[1].ptr = sm;
   257     packet[1].len = sizeof(scale_tags);
   258 
   259     return ioctl(fd, PS2IOC_SENDL, &plist);
   260 }
   261 
   262 static int
   263 power_of_2(int value)
   264 {
   265     int shift;
   266 
   267     for (shift = 0; (1 << shift) < value; ++shift) {
   268         /* Keep looking */ ;
   269     }
   270     return (shift);
   271 }
   272 
   273 static int
   274 GS_VideoInit(_THIS, SDL_PixelFormat * vformat)
   275 {
   276     struct ps2_screeninfo vinfo;
   277 
   278     /* Initialize the library */
   279     console_fd = open(PS2_DEV_GS, O_RDWR, 0);
   280     if (console_fd < 0) {
   281         SDL_SetError("Unable to open %s", PS2_DEV_GS);
   282         return (-1);
   283     }
   284     memory_fd = open(PS2_DEV_MEM, O_RDWR, 0);
   285     if (memory_fd < 0) {
   286         close(console_fd);
   287         console_fd = -1;
   288         SDL_SetError("Unable to open %s", PS2_DEV_MEM);
   289         return (-1);
   290     }
   291 
   292     if (ioctl(console_fd, PS2IOC_GSCREENINFO, &vinfo) < 0) {
   293         close(memory_fd);
   294         close(console_fd);
   295         console_fd = -1;
   296         SDL_SetError("Couldn't get console pixel format");
   297         return (-1);
   298     }
   299 
   300     /* Determine the current screen size */
   301     this->info.current_w = vinfo.w;
   302     this->info.current_h = vinfo.h;
   303 
   304     /* Determine the current screen depth */
   305     switch (vinfo.psm) {
   306         /* Supported pixel formats */
   307     case PS2_GS_PSMCT32:
   308     case PS2_GS_PSMCT24:
   309     case PS2_GS_PSMCT16:
   310         break;
   311     default:
   312         GS_VideoQuit(this);
   313         SDL_SetError("Unknown console pixel format: %d", vinfo.psm);
   314         return (-1);
   315     }
   316     vformat->BitsPerPixel = GS_pixelmasks[vinfo.psm].bpp;
   317     vformat->Rmask = GS_pixelmasks[vinfo.psm].r;
   318     vformat->Gmask = GS_pixelmasks[vinfo.psm].g;
   319     vformat->Bmask = GS_pixelmasks[vinfo.psm].b;
   320     saved_vinfo = vinfo;
   321 
   322     /* Enable mouse and keyboard support */
   323     if (GS_OpenKeyboard(this) < 0) {
   324         GS_VideoQuit(this);
   325         SDL_SetError("Unable to open keyboard");
   326         return (-1);
   327     }
   328     if (GS_OpenMouse(this) < 0) {
   329         const char *sdl_nomouse;
   330 
   331         sdl_nomouse = SDL_getenv("SDL_NOMOUSE");
   332         if (!sdl_nomouse) {
   333             GS_VideoQuit(this);
   334             SDL_SetError("Unable to open mouse");
   335             return (-1);
   336         }
   337     }
   338 
   339     /* We're done! */
   340     return (0);
   341 }
   342 
   343 static SDL_Rect **
   344 GS_ListModes(_THIS, SDL_PixelFormat * format, Uint32 flags)
   345 {
   346     static SDL_Rect GS_vesa_mode_list[] = {
   347         {0, 0, 1280, 1024},
   348         {0, 0, 1024, 768},
   349         {0, 0, 800, 600},
   350         {0, 0, 640, 480}
   351     };
   352     static SDL_Rect *GS_vesa_modes[] = {
   353         &GS_vesa_mode_list[0],
   354         &GS_vesa_mode_list[1],
   355         &GS_vesa_mode_list[2],
   356         &GS_vesa_mode_list[3],
   357         NULL
   358     };
   359     static SDL_Rect GS_tvout_stretch;
   360     static SDL_Rect GS_tvout_mode;
   361     static SDL_Rect *GS_tvout_modes[3];
   362     SDL_Rect **modes = NULL;
   363 
   364     switch (format->BitsPerPixel) {
   365     case 16:
   366     case 24:
   367     case 32:
   368         if (saved_vinfo.mode == PS2_GS_VESA) {
   369             modes = GS_vesa_modes;
   370         } else {
   371             int i, j = 0;
   372 
   373 // FIXME - what's wrong with the stretch code at 16 bpp?
   374             if (format->BitsPerPixel != 32)
   375                 break;
   376             /* Add a mode that we could possibly stretch to */
   377             for (i = 0; GS_vesa_modes[i]; ++i) {
   378                 if ((GS_vesa_modes[i]->w == saved_vinfo.w) &&
   379                     (GS_vesa_modes[i]->h != saved_vinfo.h)) {
   380                     GS_tvout_stretch.w = GS_vesa_modes[i]->w;
   381                     GS_tvout_stretch.h = GS_vesa_modes[i]->h;
   382                     GS_tvout_modes[j++] = &GS_tvout_stretch;
   383                     break;
   384                 }
   385             }
   386             /* Add the current TV video mode */
   387             GS_tvout_mode.w = saved_vinfo.w;
   388             GS_tvout_mode.h = saved_vinfo.h;
   389             GS_tvout_modes[j++] = &GS_tvout_mode;
   390             GS_tvout_modes[j++] = NULL;
   391 
   392             /* Return the created list of modes */
   393             modes = GS_tvout_modes;
   394         }
   395         break;
   396     default:
   397         break;
   398     }
   399     return (modes);
   400 }
   401 
   402 /* Various screen update functions available */
   403 static void GS_DMAFullUpdate(_THIS, int numrects, SDL_Rect * rects);
   404 
   405 static SDL_Surface *
   406 GS_SetVideoMode(_THIS, SDL_Surface * current,
   407                 int width, int height, int bpp, Uint32 flags)
   408 {
   409     struct ps2_screeninfo vinfo;
   410 
   411     /* Set the terminal into graphics mode */
   412     if (GS_EnterGraphicsMode(this) < 0) {
   413         return (NULL);
   414     }
   415 
   416     /* Set the video mode and get the final screen format */
   417     if (ioctl(console_fd, PS2IOC_GSCREENINFO, &vinfo) < 0) {
   418         SDL_SetError("Couldn't get console screen info");
   419         return (NULL);
   420     }
   421     if ((vinfo.w != width) || (vinfo.h != height) ||
   422         (GS_pixelmasks[vinfo.psm].bpp != bpp)) {
   423         /* If we're not in VESA mode, we have to scale resolution */
   424         if (saved_vinfo.mode == PS2_GS_VESA) {
   425             switch (width) {
   426             case 640:
   427                 vinfo.res = PS2_GS_640x480;
   428                 break;
   429             case 800:
   430                 vinfo.res = PS2_GS_800x600;
   431                 break;
   432             case 1024:
   433                 vinfo.res = PS2_GS_1024x768;
   434                 break;
   435             case 1280:
   436                 vinfo.res = PS2_GS_1280x1024;
   437                 break;
   438             default:
   439                 SDL_SetError("Unsupported resolution: %dx%d\n",
   440                              width, height);
   441                 return (NULL);
   442             }
   443             vinfo.res |= (PS2_GS_75Hz << 8);
   444             vinfo.w = width;
   445             vinfo.h = height;
   446         }
   447         vinfo.fbp = 0;
   448         vinfo.psm = GS_formatmap[bpp / 8];
   449         if (vinfo.psm < 0) {
   450             SDL_SetError("Unsupported depth: %d bpp\n", bpp);
   451             return (NULL);
   452         }
   453         if (ioctl(console_fd, PS2IOC_SSCREENINFO, &vinfo) < 0) {
   454             SDL_SetError("Couldn't set console screen info");
   455             return (NULL);
   456         }
   457 
   458         /* Unmap the previous DMA buffer */
   459         if (mapped_mem) {
   460             munmap(mapped_mem, mapped_len);
   461             mapped_mem = NULL;
   462         }
   463     }
   464     if (!SDL_ReallocFormat(current, GS_pixelmasks[vinfo.psm].bpp,
   465                            GS_pixelmasks[vinfo.psm].r,
   466                            GS_pixelmasks[vinfo.psm].g,
   467                            GS_pixelmasks[vinfo.psm].b, 0)) {
   468         return (NULL);
   469     }
   470 
   471     /* Set up the new mode framebuffer */
   472     current->flags = SDL_FULLSCREEN;
   473     current->w = width;
   474     current->h = height;
   475     current->pitch = SDL_CalculatePitch(current);
   476 
   477     /* Memory map the DMA area for block memory transfer */
   478     if (!mapped_mem) {
   479         pixels_len = height * current->pitch;
   480         mapped_len = pixels_len +
   481             /* Screen update DMA command area */
   482             sizeof(head_tags) + ((2 * MAXTAGS) * 16);
   483         if (saved_vinfo.mode != PS2_GS_VESA) {
   484             mapped_len += sizeof(tex_tags) + sizeof(scale_tags);
   485         }
   486         mapped_mem = mmap(0, mapped_len, PROT_READ | PROT_WRITE,
   487                           MAP_SHARED, memory_fd, 0);
   488         if (mapped_mem == MAP_FAILED) {
   489             SDL_SetError("Unable to map %d bytes for DMA", mapped_len);
   490             mapped_mem = NULL;
   491             return (NULL);
   492         }
   493 
   494         /* Set up the entire screen for DMA transfer */
   495         screen_image.ptr = mapped_mem;
   496         screen_image.fbp = 0;
   497         screen_image.fbw = (vinfo.w + 63) / 64;
   498         screen_image.psm = vinfo.psm;
   499         screen_image.x = 0;
   500         if (vinfo.h == height) {
   501             screen_image.y = 0;
   502         } else {
   503             /* Put image offscreen and scale to screen height */
   504             screen_image.y = vinfo.h;
   505         }
   506         screen_image.w = current->w;
   507         screen_image.h = current->h;
   508 
   509         /* get screen image data size (qword aligned) */
   510         screen_image_size = (screen_image.w * screen_image.h);
   511         switch (screen_image.psm) {
   512         case PS2_GS_PSMCT32:
   513             screen_image_size *= 4;
   514             break;
   515         case PS2_GS_PSMCT24:
   516             screen_image_size *= 3;
   517             break;
   518         case PS2_GS_PSMCT16:
   519             screen_image_size *= 2;
   520             break;
   521         }
   522         screen_image_size = (screen_image_size + 15) & ~15;
   523 
   524         /* Set up the memory for screen update DMA commands */
   525         head_tags_mem = (unsigned long long *) (mapped_mem + pixels_len);
   526         image_tags_mem = (unsigned long long *)
   527             ((caddr_t) head_tags_mem + sizeof(head_tags));
   528         SDL_memcpy(head_tags_mem, head_tags, sizeof(head_tags));
   529         if (saved_vinfo.mode != PS2_GS_VESA) {
   530             tex_tags_mem = (unsigned long long *)
   531                 ((caddr_t) image_tags_mem + ((2 * MAXTAGS) * 16));
   532             scale_tags_mem = (unsigned long long *)
   533                 ((caddr_t) tex_tags_mem + sizeof(tex_tags));
   534             SDL_memcpy(tex_tags_mem, tex_tags, sizeof(tex_tags));
   535             tex_tags_mem[2] =
   536                 (vinfo.h * vinfo.w) / 64 +
   537                 ((unsigned long long) screen_image.fbw << 14) +
   538                 ((unsigned long long) screen_image.psm << 20) +
   539                 ((unsigned long long) power_of_2(screen_image.w) << 26) +
   540                 ((unsigned long long) power_of_2(screen_image.h) << 30) +
   541                 ((unsigned long long) 1 << 34) +
   542                 ((unsigned long long) 1 << 35);
   543             SDL_memcpy(scale_tags_mem, scale_tags, sizeof(scale_tags));
   544             scale_tags_mem[8] =
   545                 ((unsigned long long) screen_image.w * 16) +
   546                 (((unsigned long long) screen_image.h * 16) << 16);
   547             scale_tags_mem[10] =
   548                 ((unsigned long long) vinfo.w * 16) +
   549                 (((unsigned long long) vinfo.h * 16) << 16);
   550         }
   551     }
   552     current->pixels = NULL;
   553     if (SDL_getenv("SDL_FULLSCREEN_UPDATE")) {
   554         /* Correct semantics */
   555         current->flags |= SDL_ASYNCBLIT;
   556     } else {
   557         /* We lie here - the screen memory isn't really the visible
   558            display memory and still requires an update, but this
   559            has the desired effect for most applications.
   560          */
   561         current->flags |= SDL_HWSURFACE;
   562     }
   563 
   564     /* Set the update rectangle function */
   565     this->UpdateRects = GS_DMAFullUpdate;
   566 
   567     /* We're done */
   568     return (current);
   569 }
   570 
   571 /* We don't support hardware surfaces yet */
   572 static int
   573 GS_AllocHWSurface(_THIS, SDL_Surface * surface)
   574 {
   575     return (-1);
   576 }
   577 
   578 static void
   579 GS_FreeHWSurface(_THIS, SDL_Surface * surface)
   580 {
   581     return;
   582 }
   583 
   584 static int
   585 GS_LockHWSurface(_THIS, SDL_Surface * surface)
   586 {
   587     if (surface == this->screen) {
   588         /* Since mouse motion affects 'pixels', lock it */
   589         SDL_LockCursor();
   590 
   591         /* Make sure any pending DMA has completed */
   592         if (dma_pending) {
   593             ioctl(console_fd, PS2IOC_SENDQCT, 1);
   594             dma_pending = 0;
   595         }
   596 
   597         /* If the cursor is drawn on the DMA area, remove it */
   598         if (cursor_drawn) {
   599             surface->pixels = mapped_mem + surface->offset;
   600             SDL_EraseCursorNoLock(this->screen);
   601             cursor_drawn = 0;
   602         }
   603 
   604         /* Set the surface pixels to the base of the DMA area */
   605         surface->pixels = mapped_mem;
   606 
   607         /* We're finished! */
   608         SDL_UnlockCursor();
   609     }
   610     return (0);
   611 }
   612 
   613 static void
   614 GS_UnlockHWSurface(_THIS, SDL_Surface * surface)
   615 {
   616     if (surface == this->screen) {
   617         /* Since mouse motion affects 'pixels', lock it */
   618         SDL_LockCursor();
   619         surface->pixels = NULL;
   620         SDL_UnlockCursor();
   621     }
   622 }
   623 
   624 static void
   625 GS_DMAFullUpdate(_THIS, int numrects, SDL_Rect * rects)
   626 {
   627     /* Lock so we aren't interrupted by a mouse update */
   628     SDL_LockCursor();
   629 
   630     /* Make sure any pending DMA has completed */
   631     if (dma_pending) {
   632         ioctl(console_fd, PS2IOC_SENDQCT, 1);
   633         dma_pending = 0;
   634     }
   635 
   636     /* If the mouse is visible, draw it on the DMA area */
   637     if ((SDL_cursorstate & CURSOR_VISIBLE) && !cursor_drawn) {
   638         this->screen->pixels = mapped_mem + this->screen->offset;
   639         SDL_DrawCursorNoLock(this->screen);
   640         this->screen->pixels = NULL;
   641         cursor_drawn = 1;
   642     }
   643 
   644     /* Put the image onto the screen */
   645     loadimage_nonblock(console_fd,
   646                        &screen_image, screen_image_size,
   647                        head_tags_mem, image_tags_mem);
   648     if (screen_image.y > 0) {
   649         /* Need to scale offscreen image to TV output */
   650         ioctl(console_fd, PS2IOC_SENDQCT, 1);
   651         dma_pending = 0;
   652         scaleimage_nonblock(console_fd, tex_tags_mem, scale_tags_mem);
   653     } else {
   654         dma_pending = 1;
   655     }
   656 
   657     /* We're finished! */
   658     SDL_UnlockCursor();
   659 }
   660 
   661 static int
   662 GS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color * colors)
   663 {
   664     return (0);
   665 }
   666 
   667 static void
   668 GS_VideoQuit(_THIS)
   669 {
   670     /* Close console and input file descriptors */
   671     if (console_fd > 0) {
   672         /* Unmap the video framebuffer */
   673         if (mapped_mem) {
   674             /* Unmap the video framebuffer */
   675             munmap(mapped_mem, mapped_len);
   676             mapped_mem = NULL;
   677         }
   678         close(memory_fd);
   679 
   680         /* Restore the original video mode */
   681         if (GS_InGraphicsMode(this)) {
   682             ioctl(console_fd, PS2IOC_SSCREENINFO, &saved_vinfo);
   683         }
   684 
   685         /* We're all done with the graphics device */
   686         close(console_fd);
   687         console_fd = -1;
   688     }
   689     GS_CloseMouse(this);
   690     GS_CloseKeyboard(this);
   691 }
   692 
   693 /* vi: set ts=4 sw=4 expandtab: */