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#include "SDL_config.h"
239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/time.h>
259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/mman.h>
269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <sys/ioctl.h>
279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <dev/wscons/wsdisplay_usl_io.h>
289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <fcntl.h>
299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <unistd.h>
309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include <errno.h>
319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_video.h"
339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_mouse.h"
349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_sysvideo.h"
359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../SDL_pixels_c.h"
369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "../../events/SDL_events_c.h"
379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_wsconsvideo.h"
399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_wsconsevents_c.h"
409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#include "SDL_wsconsmouse_c.h"
419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define WSCONSVID_DRIVER_NAME "wscons"
439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallenum {
449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WSCONS_ROTATE_NONE = 0,
459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WSCONS_ROTATE_CCW = 90,
469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WSCONS_ROTATE_UD = 180,
479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WSCONS_ROTATE_CW = 270
489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define min(a,b) ((a)<(b)?(a):(b))
519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Initialization/Query functions */
539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int WSCONS_VideoInit(_THIS, SDL_PixelFormat *vformat);
549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Rect **WSCONS_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_Surface *WSCONS_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int WSCONS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void WSCONS_VideoQuit(_THIS);
589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* Hardware surface functions */
609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int WSCONS_AllocHWSurface(_THIS, SDL_Surface *surface);
619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int WSCONS_LockHWSurface(_THIS, SDL_Surface *surface);
629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void WSCONS_UnlockHWSurface(_THIS, SDL_Surface *surface);
639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void WSCONS_FreeHWSurface(_THIS, SDL_Surface *surface);
649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* etc. */
669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic WSCONS_bitBlit WSCONS_blit16;
679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic WSCONS_bitBlit WSCONS_blit16blocked;
689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void WSCONS_UpdateRects(_THIS, int numrects, SDL_Rect *rects);
699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid WSCONS_ReportError(char *fmt, ...)
719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  char message[200];
739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  va_list vaArgs;
749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  message[199] = '\0';
769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  va_start(vaArgs, fmt);
789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vsnprintf(message, 199, fmt, vaArgs);
799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  va_end(vaArgs);
809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_SetError(message);
829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  fprintf(stderr, "WSCONS error: %s\n", message);
839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/* WSCONS driver bootstrap functions */
869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int WSCONS_Available(void)
889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 1;
909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void WSCONS_DeleteDevice(SDL_VideoDevice *device)
939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_free(device->hidden);
959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_free(device);
969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic SDL_VideoDevice *WSCONS_CreateDevice(int devindex)
999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_VideoDevice *device;
1019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Initialize all variables that we clean on shutdown */
1039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
1049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (device == NULL) {
1059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_OutOfMemory();
1069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return 0;
1079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
1089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_memset(device, 0, (sizeof *device));
1099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->hidden =
1109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    (struct SDL_PrivateVideoData *)SDL_malloc((sizeof *device->hidden));
1119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (device->hidden == NULL) {
1129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_OutOfMemory();
1139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(device);
1149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return(0);
1159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
1169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_memset(device->hidden, 0, (sizeof *device->hidden));
1179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->hidden->fd = -1;
1189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  /* Set the function pointers */
1209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->VideoInit = WSCONS_VideoInit;
1219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->ListModes = WSCONS_ListModes;
1229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->SetVideoMode = WSCONS_SetVideoMode;
1239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->SetColors = WSCONS_SetColors;
1249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->UpdateRects = WSCONS_UpdateRects;
1259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->VideoQuit = WSCONS_VideoQuit;
1269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->AllocHWSurface = WSCONS_AllocHWSurface;
1279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->LockHWSurface = WSCONS_LockHWSurface;
1289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->UnlockHWSurface = WSCONS_UnlockHWSurface;
1299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->FreeHWSurface = WSCONS_FreeHWSurface;
1309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->InitOSKeymap = WSCONS_InitOSKeymap;
1319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->PumpEvents = WSCONS_PumpEvents;
1329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  device->free = WSCONS_DeleteDevice;
1339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return device;
1359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
1369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallVideoBootStrap WSCONS_bootstrap = {
1389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WSCONSVID_DRIVER_NAME,
1399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  "SDL wscons video driver",
1409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WSCONS_Available,
1419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WSCONS_CreateDevice
1429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall};
1439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define WSCONSDEV_FORMAT "/dev/ttyC%01x"
1459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint WSCONS_VideoInit(_THIS, SDL_PixelFormat *vformat)
1479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
1489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  char devnamebuf[30];
1499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  char *devname;
1509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  char *rotation;
1519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int wstype;
1529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int wsmode = WSDISPLAYIO_MODE_DUMBFB;
1539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  size_t len, mapsize;
1549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int pagemask;
1559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int width, height;
1569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  devname = SDL_getenv("SDL_WSCONSDEV");
1589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (devname == NULL) {
1599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int activeVT;
1609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (ioctl(STDIN_FILENO, VT_GETACTIVE, &activeVT) == -1) {
1619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WSCONS_ReportError("Unable to determine active terminal: %s",
1629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 strerror(errno));
1639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return -1;
1649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
1659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_snprintf(devnamebuf, sizeof(devnamebuf), WSCONSDEV_FORMAT, activeVT - 1);
1669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    devname = devnamebuf;
1679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
1689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
1699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  private->fd = open(devname, O_RDWR | O_NONBLOCK, 0);
1709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (private->fd == -1) {
1719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WSCONS_ReportError("open %s: %s", devname, strerror(errno));
1729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
1739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
1749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ioctl(private->fd, WSDISPLAYIO_GINFO, &private->info) == -1) {
1759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WSCONS_ReportError("ioctl WSDISPLAY_GINFO: %s", strerror(errno));
1769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
1779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
1789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ioctl(private->fd, WSDISPLAYIO_GTYPE, &wstype) == -1) {
1799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WSCONS_ReportError("ioctl WSDISPLAY_GTYPE: %s", strerror(errno));
1809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
1819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
1829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ioctl(private->fd, WSDISPLAYIO_LINEBYTES, &private->physlinebytes) == -1) {
1839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WSCONS_ReportError("ioctl WSDISPLAYIO_LINEBYTES: %s", strerror(errno));
1849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
1859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
1869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (private->info.depth > 8) {
1879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (wstype == WSDISPLAY_TYPE_SUN24 ||
1889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	wstype == WSDISPLAY_TYPE_SUNCG12 ||
1899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	wstype == WSDISPLAY_TYPE_SUNCG14 ||
1909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	wstype == WSDISPLAY_TYPE_SUNTCX ||
1919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	wstype == WSDISPLAY_TYPE_SUNFFB) {
1929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->redMask = 0x0000ff;
1939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->greenMask = 0x00ff00;
1949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->blueMask = 0xff0000;
1959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#ifdef WSDISPLAY_TYPE_PXALCD
1969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else if (wstype == WSDISPLAY_TYPE_PXALCD) {
1979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->redMask = 0x1f << 11;
1989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->greenMask = 0x3f << 5;
1999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->blueMask = 0x1f;
2009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#endif
2019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
2029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WSCONS_ReportError("Unknown video hardware");
2039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return -1;
2049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else {
2069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WSCONS_ReportError("Displays with 8 bpp or less are not supported");
2079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
2089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  private->rotate = WSCONS_ROTATE_NONE;
2119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  rotation = SDL_getenv("SDL_VIDEO_WSCONS_ROTATION");
2129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (rotation != NULL) {
2139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (SDL_strlen(rotation) == 0) {
2149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->shadowFB = 0;
2159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->rotate = WSCONS_ROTATE_NONE;
2169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("Not rotating, no shadow\n");
2179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else if (!SDL_strcmp(rotation, "NONE")) {
2189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->shadowFB = 1;
2199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->rotate = WSCONS_ROTATE_NONE;
2209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("Not rotating, but still using shadow\n");
2219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else if (!SDL_strcmp(rotation, "CW")) {
2229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->shadowFB = 1;
2239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->rotate = WSCONS_ROTATE_CW;
2249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("Rotating screen clockwise\n");
2259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else if (!SDL_strcmp(rotation, "CCW")) {
2269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->shadowFB = 1;
2279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->rotate = WSCONS_ROTATE_CCW;
2289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("Rotating screen counter clockwise\n");
2299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else if (!SDL_strcmp(rotation, "UD")) {
2309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->shadowFB = 1;
2319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->rotate = WSCONS_ROTATE_UD;
2329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      printf("Rotating screen upside down\n");
2339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else {
2349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WSCONS_ReportError("\"%s\" is not a valid value for "
2359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 "SDL_VIDEO_WSCONS_ROTATION", rotation);
2369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return -1;
2379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
2389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  switch (private->info.depth) {
2419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case 1:
2429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case 4:
2439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case 8:
2449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      len = private->physlinebytes * private->info.height;
2459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
2469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case 16:
2479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (private->physlinebytes == private->info.width) {
2489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	len = private->info.width * private->info.height * sizeof(short);
2499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      } else {
2509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	len = private->physlinebytes * private->info.height;
2519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
2529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (private->rotate == WSCONS_ROTATE_NONE ||
2539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	  private->rotate == WSCONS_ROTATE_UD) {
2549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	private->blitFunc = WSCONS_blit16;
2559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      } else {
2569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	private->blitFunc = WSCONS_blit16blocked;
2579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
2589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
2599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    case 32:
2609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      if (private->physlinebytes == private->info.width) {
2619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	len = private->info.width * private->info.height * sizeof(int);
2629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      } else {
2639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	len = private->physlinebytes * private->info.height;
2649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      }
2659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      break;
2669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    default:
2679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WSCONS_ReportError("unsupported depth %d", private->info.depth);
2689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return -1;
2699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (private->shadowFB && private->blitFunc == NULL) {
2729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WSCONS_ReportError("Using software buffer, but no blitter function is "
2739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		       "available for this %d bpp.", private->info.depth);
2749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
2759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ioctl(private->fd, WSDISPLAYIO_SMODE, &wsmode) == -1) {
2789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WSCONS_ReportError("ioctl SMODE");
2799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
2809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  pagemask = getpagesize() - 1;
2839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  mapsize = ((int)len + pagemask) & ~pagemask;
2849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  private->physmem = (Uint8 *)mmap(NULL, mapsize,
2859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				   PROT_READ | PROT_WRITE, MAP_SHARED,
2869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				   private->fd, (off_t)0);
2879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (private->physmem == (Uint8 *)MAP_FAILED) {
2889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    private->physmem = NULL;
2899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WSCONS_ReportError("mmap: %s", strerror(errno));
2909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
2919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
2929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  private->fbmem_len = len;
2939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
2949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (private->rotate == WSCONS_ROTATE_CW ||
2959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      private->rotate == WSCONS_ROTATE_CCW) {
2969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    width = private->info.height;
2979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    height = private->info.width;
2989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else {
2999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    width = private->info.width;
3009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    height = private->info.height;
3019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->info.current_w = width;
3049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  this->info.current_h = height;
3059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (private->shadowFB) {
3079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    private->shadowmem = (Uint8 *)SDL_malloc(len);
3089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (private->shadowmem == NULL) {
3099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WSCONS_ReportError("No memory for shadow");
3109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      return -1;
3119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
3129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    private->fbstart = private->shadowmem;
3139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    private->fblinebytes = width * ((private->info.depth + 7) / 8);
3149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else {
3159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    private->fbstart = private->physmem;
3169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    private->fblinebytes = private->physlinebytes;
3179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  private->SDL_modelist[0] = (SDL_Rect *)SDL_malloc(sizeof(SDL_Rect));
3209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  private->SDL_modelist[0]->w = width;
3219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  private->SDL_modelist[0]->h = height;
3229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->BitsPerPixel = private->info.depth;
3249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  vformat->BytesPerPixel = private->info.depth / 8;
3259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (WSCONS_InitKeyboard(this) == -1) {
3279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return -1;
3289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 0;
3319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Rect **WSCONS_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
3349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (format->BitsPerPixel == private->info.depth) {
3369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return private->SDL_modelist;
3379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else {
3389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return NULL;
3399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse HallSDL_Surface *WSCONS_SetVideoMode(_THIS, SDL_Surface *current,
3439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				 int width, int height, int bpp, Uint32 flags)
3449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (width != private->SDL_modelist[0]->w ||
3469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      height != private->SDL_modelist[0]->h) {
3479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WSCONS_ReportError("Requested video mode %dx%d not supported.",
3489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		       width, height);
3499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return NULL;
3509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (bpp != private->info.depth) {
3529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WSCONS_ReportError("Requested video depth %d bpp not supported.", bpp);
3539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return NULL;
3549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!SDL_ReallocFormat(current,
3579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 bpp,
3589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 private->redMask,
3599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 private->greenMask,
3609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 private->blueMask,
3619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			 0)) {
3629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WSCONS_ReportError("Couldn't allocate new pixel format");
3639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return NULL;
3649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  current->flags &= SDL_FULLSCREEN;
3679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (private->shadowFB) {
3689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    current->flags |= SDL_SWSURFACE;
3699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  } else {
3709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    current->flags |= SDL_HWSURFACE;
3719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
3729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  current->w = width;
3739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  current->h = height;
3749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  current->pitch = private->fblinebytes;
3759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  current->pixels = private->fbstart;
3769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  SDL_memset(private->fbstart, 0, private->fbmem_len);
3789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return current;
3809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int WSCONS_AllocHWSurface(_THIS, SDL_Surface *surface)
3839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return -1;
3859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void WSCONS_FreeHWSurface(_THIS, SDL_Surface *surface)
3879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic int WSCONS_LockHWSurface(_THIS, SDL_Surface *surface)
3919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 0;
3939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void WSCONS_UnlockHWSurface(_THIS, SDL_Surface *surface)
3969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
3979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
3989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
3999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void WSCONS_blit16(Uint8 *byte_src_pos,
4009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  int srcRightDelta,
4019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  int srcDownDelta,
4029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  Uint8 *byte_dst_pos,
4039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  int dst_linebytes,
4049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  int width,
4059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall			  int height)
4069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int w;
4089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  Uint16 *src_pos = (Uint16 *)byte_src_pos;
4099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  Uint16 *dst_pos = (Uint16 *)byte_dst_pos;
4109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  while (height) {
4129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint16 *src = src_pos;
4139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint16 *dst = dst_pos;
4149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (w = width; w != 0; w--) {
4159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      *dst = *src;
4169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      src += srcRightDelta;
4179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      dst++;
4189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dst_pos = (Uint16 *)((Uint8 *)dst_pos + dst_linebytes);
4209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    src_pos += srcDownDelta;
4219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    height--;
4229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
4239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define BLOCKSIZE_W 32
4269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall#define BLOCKSIZE_H 32
4279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void WSCONS_blit16blocked(Uint8 *byte_src_pos,
4299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				 int srcRightDelta,
4309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				 int srcDownDelta,
4319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				 Uint8 *byte_dst_pos,
4329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				 int dst_linebytes,
4339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				 int width,
4349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall				 int height)
4359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int w;
4379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  Uint16 *src_pos = (Uint16 *)byte_src_pos;
4389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  Uint16 *dst_pos = (Uint16 *)byte_dst_pos;
4399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  while (height > 0) {
4419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint16 *src = src_pos;
4429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint16 *dst = dst_pos;
4439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    for (w = width; w > 0; w -= BLOCKSIZE_W) {
4449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      WSCONS_blit16((Uint8 *)src,
4459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    srcRightDelta,
4469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    srcDownDelta,
4479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    (Uint8 *)dst,
4489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    dst_linebytes,
4499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    min(w, BLOCKSIZE_W),
4509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		    min(height, BLOCKSIZE_H));
4519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      src += srcRightDelta * BLOCKSIZE_W;
4529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      dst += BLOCKSIZE_W;
4539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dst_pos = (Uint16 *)((Uint8 *)dst_pos + dst_linebytes * BLOCKSIZE_H);
4559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    src_pos += srcDownDelta * BLOCKSIZE_H;
4569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    height -= BLOCKSIZE_H;
4579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
4589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
4599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallstatic void WSCONS_UpdateRects(_THIS, int numrects, SDL_Rect *rects)
4619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
4629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int width = private->SDL_modelist[0]->w;
4639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int height = private->SDL_modelist[0]->h;
4649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int bytesPerPixel = (private->info.depth + 7) / 8;
4659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int i;
4669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (!private->shadowFB) {
4689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return;
4699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
4709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (private->info.depth != 16) {
4729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WSCONS_ReportError("Shadow copy only implemented for 16 bpp");
4739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    return;
4749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
4759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  for (i = 0; i < numrects; i++) {
4779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int x1, y1, x2, y2;
4789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int scr_x1, scr_y1, scr_x2, scr_y2;
4799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int sha_x1, sha_y1;
4809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int shadowRightDelta;  /* Address change when moving right in dest */
4819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    int shadowDownDelta;   /* Address change when moving down in dest */
4829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint8 *src_start;
4839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    Uint8 *dst_start;
4849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    x1 = rects[i].x;
4869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    y1 = rects[i].y;
4879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    x2 = x1 + rects[i].w;
4889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    y2 = y1 + rects[i].h;
4899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
4909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (x1 < 0) {
4919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      x1 = 0;
4929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else if (x1 > width) {
4939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      x1 = width;
4949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
4959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (x2 < 0) {
4969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      x2 = 0;
4979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else if (x2 > width) {
4989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      x2 = width;
4999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (y1 < 0) {
5019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      y1 = 0;
5029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else if (y1 > height) {
5039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      y1 = height;
5049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (y2 < 0) {
5069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      y2 = 0;
5079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    } else if (y2 > height) {
5089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      y2 = height;
5099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5109682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    if (x2 <= x1 || y2 <= y1) {
5119682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      continue;
5129682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5139682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5149682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    switch (private->rotate) {
5159682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case WSCONS_ROTATE_NONE:
5169682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sha_x1 = scr_x1 = x1;
5179682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sha_y1 = scr_y1 = y1;
5189682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scr_x2 = x2;
5199682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scr_y2 = y2;
5209682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shadowRightDelta = 1;
5219682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shadowDownDelta = width;
5229682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	break;
5239682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case WSCONS_ROTATE_CCW:
5249682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scr_x1 = y1;
5259682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scr_y1 = width - x2;
5269682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scr_x2 = y2;
5279682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scr_y2 = width - x1;
5289682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sha_x1 = x2 - 1;
5299682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sha_y1 = y1;
5309682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shadowRightDelta = width;
5319682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shadowDownDelta = -1;
5329682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	break;
5339682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case WSCONS_ROTATE_UD:
5349682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scr_x1 = width - x2;
5359682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scr_y1 = height - y2;
5369682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scr_x2 = width - x1;
5379682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scr_y2 = height - y1;
5389682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sha_x1 = x2 - 1;
5399682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sha_y1 = y2 - 1;
5409682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shadowRightDelta = -1;
5419682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shadowDownDelta = -width;
5429682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	break;
5439682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      case WSCONS_ROTATE_CW:
5449682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scr_x1 = height - y2;
5459682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scr_y1 = x1;
5469682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scr_x2 = height - y1;
5479682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	scr_y2 = x2;
5489682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sha_x1 = x1;
5499682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	sha_y1 = y2 - 1;
5509682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shadowRightDelta = -width;
5519682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	shadowDownDelta = 1;
5529682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	break;
5539682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      default:
5549682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	WSCONS_ReportError("Unknown rotation");
5559682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall	return;
5569682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    }
5579682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5589682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    src_start = private->shadowmem + (sha_y1 * width + sha_x1) * bytesPerPixel;
5599682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    dst_start = private->physmem + scr_y1 * private->physlinebytes +
5609682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall      scr_x1 * bytesPerPixel;
5619682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5629682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    private->blitFunc(src_start,
5639682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      shadowRightDelta,
5649682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      shadowDownDelta,
5659682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      dst_start,
5669682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      private->physlinebytes,
5679682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      scr_x2 - scr_x1,
5689682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall		      scr_y2 - scr_y1);
5699682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
5709682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5719682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5729682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallint WSCONS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
5739682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5749682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  return 0;
5759682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
5769682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5779682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall/*
5789682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * Note: If we are terminated, this could be called in the middle of
5799682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall * another SDL video routine -- notably UpdateRects.
5809682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall */
5819682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hallvoid WSCONS_VideoQuit(_THIS)
5829682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall{
5839682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  int mode = WSDISPLAYIO_MODE_EMUL;
5849682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5859682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (private->shadowmem != NULL) {
5869682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(private->shadowmem);
5879682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    private->shadowmem = NULL;
5889682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
5899682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  private->fbstart = NULL;
5909682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (this->screen != NULL) {
5919682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    this->screen->pixels = NULL;
5929682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
5939682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5949682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (private->SDL_modelist[0] != NULL) {
5959682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    SDL_free(private->SDL_modelist[0]);
5969682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    private->SDL_modelist[0] = NULL;
5979682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
5989682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
5999682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (ioctl(private->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
6009682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    WSCONS_ReportError("ioctl SMODE");
6019682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
6029682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6039682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  WSCONS_ReleaseKeyboard(this);
6049682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall
6059682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  if (private->fd != -1) {
6069682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    close(private->fd);
6079682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall    private->fd = -1;
6089682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall  }
6099682c8870b8ff5e4ac2e4c70b759f791c6f38c1fJesse Hall}
610