src/video/wscons/SDL_wsconsvideo.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 27 Aug 2008 15:10:03 +0000
changeset 2735 204be4fc2726
parent 1895 c121d94672cb
child 2859 99210400e8b9
permissions -rw-r--r--
Final merge of Google Summer of Code 2008 work...

Port SDL 1.3 to the Nintendo DS
by Darren Alton, mentored by Sam Lantinga
icculus@1187
     1
/*
icculus@1187
     2
    SDL - Simple DirectMedia Layer
slouken@1312
     3
    Copyright (C) 1997-2006 Sam Lantinga
icculus@1187
     4
icculus@1187
     5
    This library is free software; you can redistribute it and/or
slouken@1312
     6
    modify it under the terms of the GNU Lesser General Public
icculus@1187
     7
    License as published by the Free Software Foundation; either
slouken@1312
     8
    version 2.1 of the License, or (at your option) any later version.
icculus@1187
     9
icculus@1187
    10
    This library is distributed in the hope that it will be useful,
icculus@1187
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
icculus@1187
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1312
    13
    Lesser General Public License for more details.
icculus@1187
    14
slouken@1312
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1312
    16
    License along with this library; if not, write to the Free Software
slouken@1312
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
icculus@1187
    18
icculus@1187
    19
    Sam Lantinga
icculus@1187
    20
    slouken@libsdl.org
icculus@1187
    21
*/
slouken@1402
    22
#include "SDL_config.h"
icculus@1187
    23
icculus@1187
    24
#include <sys/time.h>
icculus@1187
    25
#include <sys/mman.h>
icculus@1187
    26
#include <sys/ioctl.h>
icculus@1187
    27
#include <dev/wscons/wsdisplay_usl_io.h>
icculus@1187
    28
#include <fcntl.h>
icculus@1187
    29
#include <unistd.h>
icculus@1187
    30
#include <errno.h>
icculus@1187
    31
icculus@1187
    32
#include "SDL_video.h"
icculus@1187
    33
#include "SDL_mouse.h"
slouken@1361
    34
#include "../SDL_sysvideo.h"
slouken@1361
    35
#include "../SDL_pixels_c.h"
slouken@1361
    36
#include "../../events/SDL_events_c.h"
icculus@1187
    37
icculus@1187
    38
#include "SDL_wsconsvideo.h"
icculus@1187
    39
#include "SDL_wsconsevents_c.h"
icculus@1187
    40
#include "SDL_wsconsmouse_c.h"
icculus@1187
    41
icculus@1187
    42
#define WSCONSVID_DRIVER_NAME "wscons"
slouken@1895
    43
enum
slouken@1895
    44
{
slouken@1895
    45
    WSCONS_ROTATE_NONE = 0,
slouken@1895
    46
    WSCONS_ROTATE_CCW = 90,
slouken@1895
    47
    WSCONS_ROTATE_UD = 180,
slouken@1895
    48
    WSCONS_ROTATE_CW = 270
icculus@1187
    49
};
icculus@1187
    50
icculus@1187
    51
#define min(a,b) ((a)<(b)?(a):(b))
icculus@1187
    52
icculus@1187
    53
/* Initialization/Query functions */
slouken@1895
    54
static int WSCONS_VideoInit(_THIS, SDL_PixelFormat * vformat);
slouken@1895
    55
static SDL_Rect **WSCONS_ListModes(_THIS, SDL_PixelFormat * format,
slouken@1895
    56
                                   Uint32 flags);
slouken@1895
    57
static SDL_Surface *WSCONS_SetVideoMode(_THIS, SDL_Surface * current,
slouken@1895
    58
                                        int width, int height, int bpp,
slouken@1895
    59
                                        Uint32 flags);
slouken@1895
    60
static int WSCONS_SetColors(_THIS, int firstcolor, int ncolors,
slouken@1895
    61
                            SDL_Color * colors);
icculus@1187
    62
static void WSCONS_VideoQuit(_THIS);
icculus@1187
    63
icculus@1187
    64
/* Hardware surface functions */
slouken@1895
    65
static int WSCONS_AllocHWSurface(_THIS, SDL_Surface * surface);
slouken@1895
    66
static int WSCONS_LockHWSurface(_THIS, SDL_Surface * surface);
slouken@1895
    67
static void WSCONS_UnlockHWSurface(_THIS, SDL_Surface * surface);
slouken@1895
    68
static void WSCONS_FreeHWSurface(_THIS, SDL_Surface * surface);
icculus@1187
    69
icculus@1187
    70
/* etc. */
icculus@1187
    71
static WSCONS_bitBlit WSCONS_blit16;
icculus@1187
    72
static WSCONS_bitBlit WSCONS_blit16blocked;
slouken@1895
    73
static void WSCONS_UpdateRects(_THIS, int numrects, SDL_Rect * rects);
icculus@1187
    74
slouken@1895
    75
void
slouken@1895
    76
WSCONS_ReportError(char *fmt, ...)
icculus@1187
    77
{
slouken@1895
    78
    char message[200];
slouken@1895
    79
    va_list vaArgs;
icculus@1187
    80
slouken@1895
    81
    message[199] = '\0';
slouken@1895
    82
slouken@1895
    83
    va_start(vaArgs, fmt);
slouken@1895
    84
    vsnprintf(message, 199, fmt, vaArgs);
slouken@1895
    85
    va_end(vaArgs);
slouken@1895
    86
slouken@1895
    87
    SDL_SetError(message);
slouken@1895
    88
    fprintf(stderr, "WSCONS error: %s\n", message);
icculus@1187
    89
}
icculus@1187
    90
icculus@1187
    91
/* WSCONS driver bootstrap functions */
icculus@1187
    92
slouken@1895
    93
static int
slouken@1895
    94
WSCONS_Available(void)
icculus@1187
    95
{
slouken@1895
    96
    return 1;
icculus@1187
    97
}
icculus@1187
    98
slouken@1895
    99
static void
slouken@1895
   100
WSCONS_DeleteDevice(SDL_VideoDevice * device)
icculus@1187
   101
{
slouken@1895
   102
    SDL_free(device->hidden);
slouken@1895
   103
    SDL_free(device);
icculus@1187
   104
}
icculus@1187
   105
slouken@1895
   106
static SDL_VideoDevice *
slouken@1895
   107
WSCONS_CreateDevice(int devindex)
icculus@1187
   108
{
slouken@1895
   109
    SDL_VideoDevice *device;
slouken@1895
   110
slouken@1895
   111
    /* Initialize all variables that we clean on shutdown */
slouken@1895
   112
    device = (SDL_VideoDevice *) SDL_malloc(sizeof(SDL_VideoDevice));
slouken@1895
   113
    if (device == NULL) {
slouken@1895
   114
        SDL_OutOfMemory();
slouken@1895
   115
        return 0;
slouken@1895
   116
    }
slouken@1895
   117
    SDL_memset(device, 0, (sizeof *device));
slouken@1895
   118
    device->hidden =
slouken@1895
   119
        (struct SDL_PrivateVideoData *) SDL_malloc((sizeof *device->hidden));
slouken@1895
   120
    if (device->hidden == NULL) {
slouken@1895
   121
        SDL_OutOfMemory();
slouken@1895
   122
        SDL_free(device);
slouken@1895
   123
        return (0);
slouken@1895
   124
    }
slouken@1895
   125
    SDL_memset(device->hidden, 0, (sizeof *device->hidden));
slouken@1895
   126
    device->hidden->fd = -1;
slouken@1895
   127
slouken@1895
   128
    /* Set the function pointers */
slouken@1895
   129
    device->VideoInit = WSCONS_VideoInit;
slouken@1895
   130
    device->ListModes = WSCONS_ListModes;
slouken@1895
   131
    device->SetVideoMode = WSCONS_SetVideoMode;
slouken@1895
   132
    device->SetColors = WSCONS_SetColors;
slouken@1895
   133
    device->UpdateRects = WSCONS_UpdateRects;
slouken@1895
   134
    device->VideoQuit = WSCONS_VideoQuit;
slouken@1895
   135
    device->AllocHWSurface = WSCONS_AllocHWSurface;
slouken@1895
   136
    device->LockHWSurface = WSCONS_LockHWSurface;
slouken@1895
   137
    device->UnlockHWSurface = WSCONS_UnlockHWSurface;
slouken@1895
   138
    device->FreeHWSurface = WSCONS_FreeHWSurface;
slouken@1895
   139
    device->InitOSKeymap = WSCONS_InitOSKeymap;
slouken@1895
   140
    device->PumpEvents = WSCONS_PumpEvents;
slouken@1895
   141
    device->free = WSCONS_DeleteDevice;
slouken@1895
   142
slouken@1895
   143
    return device;
icculus@1187
   144
}
icculus@1187
   145
icculus@1187
   146
VideoBootStrap WSCONS_bootstrap = {
slouken@1895
   147
    WSCONSVID_DRIVER_NAME,
slouken@1895
   148
    "SDL wscons video driver",
slouken@1895
   149
    WSCONS_Available,
slouken@1895
   150
    WSCONS_CreateDevice
icculus@1187
   151
};
icculus@1187
   152
icculus@1187
   153
#define WSCONSDEV_FORMAT "/dev/ttyC%01x"
icculus@1187
   154
slouken@1895
   155
int
slouken@1895
   156
WSCONS_VideoInit(_THIS, SDL_PixelFormat * vformat)
icculus@1187
   157
{
slouken@1895
   158
    char devnamebuf[30];
slouken@1895
   159
    char *devname;
slouken@1895
   160
    char *rotation;
slouken@1895
   161
    int wstype;
slouken@1895
   162
    int wsmode = WSDISPLAYIO_MODE_DUMBFB;
slouken@1895
   163
    size_t len, mapsize;
slouken@1895
   164
    int pagemask;
slouken@1895
   165
    int width, height;
slouken@1895
   166
slouken@1895
   167
    devname = SDL_getenv("SDL_WSCONSDEV");
slouken@1895
   168
    if (devname == NULL) {
slouken@1895
   169
        int activeVT;
slouken@1895
   170
        if (ioctl(STDIN_FILENO, VT_GETACTIVE, &activeVT) == -1) {
slouken@1895
   171
            WSCONS_ReportError("Unable to determine active terminal: %s",
slouken@1895
   172
                               strerror(errno));
slouken@1895
   173
            return -1;
slouken@1895
   174
        }
slouken@1895
   175
        SDL_snprintf(devnamebuf, sizeof(devnamebuf), WSCONSDEV_FORMAT,
slouken@1895
   176
                     activeVT - 1);
slouken@1895
   177
        devname = devnamebuf;
icculus@1187
   178
    }
icculus@1187
   179
slouken@1895
   180
    private->fd = open(devname, O_RDWR | O_NONBLOCK, 0);
slouken@1895
   181
    if (private->fd == -1) {
slouken@1895
   182
        WSCONS_ReportError("open %s: %s", devname, strerror(errno));
slouken@1895
   183
        return -1;
slouken@1895
   184
    }
slouken@1895
   185
    if (ioctl(private->fd, WSDISPLAYIO_GINFO, &private->info) == -1) {
slouken@1895
   186
        WSCONS_ReportError("ioctl WSDISPLAY_GINFO: %s", strerror(errno));
slouken@1895
   187
        return -1;
slouken@1895
   188
    }
slouken@1895
   189
    if (ioctl(private->fd, WSDISPLAYIO_GTYPE, &wstype) == -1) {
slouken@1895
   190
        WSCONS_ReportError("ioctl WSDISPLAY_GTYPE: %s", strerror(errno));
slouken@1895
   191
        return -1;
slouken@1895
   192
    }
slouken@1895
   193
    if (ioctl(private->fd, WSDISPLAYIO_LINEBYTES, &private->physlinebytes) ==
slouken@1895
   194
        -1) {
slouken@1895
   195
        WSCONS_ReportError("ioctl WSDISPLAYIO_LINEBYTES: %s",
slouken@1895
   196
                           strerror(errno));
slouken@1895
   197
        return -1;
slouken@1895
   198
    }
slouken@1895
   199
    if (private->info.depth > 8) {
slouken@1895
   200
        if (wstype == WSDISPLAY_TYPE_SUN24 ||
slouken@1895
   201
            wstype == WSDISPLAY_TYPE_SUNCG12 ||
slouken@1895
   202
            wstype == WSDISPLAY_TYPE_SUNCG14 ||
slouken@1895
   203
            wstype == WSDISPLAY_TYPE_SUNTCX ||
slouken@1895
   204
            wstype == WSDISPLAY_TYPE_SUNFFB) {
slouken@1895
   205
            private->redMask = 0x0000ff;
slouken@1895
   206
            private->greenMask = 0x00ff00;
slouken@1895
   207
            private->blueMask = 0xff0000;
slouken@1383
   208
#ifdef WSDISPLAY_TYPE_PXALCD
slouken@1895
   209
        } else if (wstype == WSDISPLAY_TYPE_PXALCD) {
slouken@1895
   210
            private->redMask = 0x1f << 11;
slouken@1895
   211
            private->greenMask = 0x3f << 5;
slouken@1895
   212
            private->blueMask = 0x1f;
slouken@1383
   213
#endif
slouken@1895
   214
        } else {
slouken@1895
   215
            WSCONS_ReportError("Unknown video hardware");
slouken@1895
   216
            return -1;
slouken@1895
   217
        }
icculus@1187
   218
    } else {
slouken@1895
   219
        WSCONS_ReportError("Displays with 8 bpp or less are not supported");
slouken@1895
   220
        return -1;
icculus@1187
   221
    }
slouken@1895
   222
slouken@1895
   223
    private->rotate = WSCONS_ROTATE_NONE;
slouken@1895
   224
    rotation = SDL_getenv("SDL_VIDEO_WSCONS_ROTATION");
slouken@1895
   225
    if (rotation != NULL) {
slouken@1895
   226
        if (SDL_strlen(rotation) == 0) {
slouken@1895
   227
            private->shadowFB = 0;
slouken@1895
   228
            private->rotate = WSCONS_ROTATE_NONE;
slouken@1895
   229
            printf("Not rotating, no shadow\n");
slouken@1895
   230
        } else if (!SDL_strcmp(rotation, "NONE")) {
slouken@1895
   231
            private->shadowFB = 1;
slouken@1895
   232
            private->rotate = WSCONS_ROTATE_NONE;
slouken@1895
   233
            printf("Not rotating, but still using shadow\n");
slouken@1895
   234
        } else if (!SDL_strcmp(rotation, "CW")) {
slouken@1895
   235
            private->shadowFB = 1;
slouken@1895
   236
            private->rotate = WSCONS_ROTATE_CW;
slouken@1895
   237
            printf("Rotating screen clockwise\n");
slouken@1895
   238
        } else if (!SDL_strcmp(rotation, "CCW")) {
slouken@1895
   239
            private->shadowFB = 1;
slouken@1895
   240
            private->rotate = WSCONS_ROTATE_CCW;
slouken@1895
   241
            printf("Rotating screen counter clockwise\n");
slouken@1895
   242
        } else if (!SDL_strcmp(rotation, "UD")) {
slouken@1895
   243
            private->shadowFB = 1;
slouken@1895
   244
            private->rotate = WSCONS_ROTATE_UD;
slouken@1895
   245
            printf("Rotating screen upside down\n");
slouken@1895
   246
        } else {
slouken@1895
   247
            WSCONS_ReportError("\"%s\" is not a valid value for "
slouken@1895
   248
                               "SDL_VIDEO_WSCONS_ROTATION", rotation);
slouken@1895
   249
            return -1;
slouken@1895
   250
        }
icculus@1187
   251
    }
icculus@1187
   252
slouken@1895
   253
    switch (private->info.depth) {
icculus@1187
   254
    case 1:
icculus@1187
   255
    case 4:
icculus@1187
   256
    case 8:
slouken@1895
   257
        len = private->physlinebytes * private->info.height;
slouken@1895
   258
        break;
icculus@1187
   259
    case 16:
slouken@1895
   260
        if (private->physlinebytes == private->info.width) {
slouken@1895
   261
            len = private->info.width * private->info.height * sizeof(short);
slouken@1895
   262
        } else {
slouken@1895
   263
            len = private->physlinebytes * private->info.height;
slouken@1895
   264
        }
slouken@1895
   265
        if (private->rotate == WSCONS_ROTATE_NONE ||
slouken@1895
   266
            private->rotate == WSCONS_ROTATE_UD) {
slouken@1895
   267
            private->blitFunc = WSCONS_blit16;
slouken@1895
   268
        } else {
slouken@1895
   269
            private->blitFunc = WSCONS_blit16blocked;
slouken@1895
   270
        }
slouken@1895
   271
        break;
icculus@1187
   272
    case 32:
slouken@1895
   273
        if (private->physlinebytes == private->info.width) {
slouken@1895
   274
            len = private->info.width * private->info.height * sizeof(int);
slouken@1895
   275
        } else {
slouken@1895
   276
            len = private->physlinebytes * private->info.height;
slouken@1895
   277
        }
slouken@1895
   278
        break;
icculus@1187
   279
    default:
slouken@1895
   280
        WSCONS_ReportError("unsupported depth %d", private->info.depth);
slouken@1895
   281
        return -1;
slouken@1895
   282
    }
icculus@1187
   283
slouken@1895
   284
    if (private->shadowFB && private->blitFunc == NULL) {
slouken@1895
   285
        WSCONS_ReportError
slouken@1895
   286
            ("Using software buffer, but no blitter function is "
slouken@1895
   287
             "available for this %d bpp.", private->info.depth);
slouken@1895
   288
        return -1;
slouken@1895
   289
    }
icculus@1187
   290
slouken@1895
   291
    if (ioctl(private->fd, WSDISPLAYIO_SMODE, &wsmode) == -1) {
slouken@1895
   292
        WSCONS_ReportError("ioctl SMODE");
slouken@1895
   293
        return -1;
slouken@1895
   294
    }
icculus@1187
   295
slouken@1895
   296
    pagemask = getpagesize() - 1;
slouken@1895
   297
    mapsize = ((int) len + pagemask) & ~pagemask;
slouken@1895
   298
    private->physmem = (Uint8 *) mmap(NULL, mapsize,
slouken@1895
   299
                                      PROT_READ | PROT_WRITE, MAP_SHARED,
slouken@1895
   300
                                      private->fd, (off_t) 0);
slouken@1895
   301
    if (private->physmem == (Uint8 *) MAP_FAILED) {
slouken@1895
   302
        private->physmem = NULL;
slouken@1895
   303
        WSCONS_ReportError("mmap: %s", strerror(errno));
slouken@1895
   304
        return -1;
slouken@1895
   305
    }
slouken@1895
   306
    private->fbmem_len = len;
icculus@1187
   307
slouken@1895
   308
    if (private->rotate == WSCONS_ROTATE_CW ||
slouken@1895
   309
        private->rotate == WSCONS_ROTATE_CCW) {
slouken@1895
   310
        width = private->info.height;
slouken@1895
   311
        height = private->info.width;
slouken@1895
   312
    } else {
slouken@1895
   313
        width = private->info.width;
slouken@1895
   314
        height = private->info.height;
slouken@1895
   315
    }
icculus@1187
   316
slouken@1895
   317
    this->info.current_w = width;
slouken@1895
   318
    this->info.current_h = height;
slouken@1545
   319
slouken@1895
   320
    if (private->shadowFB) {
slouken@1895
   321
        private->shadowmem = (Uint8 *) SDL_malloc(len);
slouken@1895
   322
        if (private->shadowmem == NULL) {
slouken@1895
   323
            WSCONS_ReportError("No memory for shadow");
slouken@1895
   324
            return -1;
slouken@1895
   325
        }
slouken@1895
   326
        private->fbstart = private->shadowmem;
slouken@1895
   327
        private->fblinebytes = width * ((private->info.depth + 7) / 8);
slouken@1895
   328
    } else {
slouken@1895
   329
        private->fbstart = private->physmem;
slouken@1895
   330
        private->fblinebytes = private->physlinebytes;
icculus@1187
   331
    }
icculus@1187
   332
slouken@1895
   333
    private->SDL_modelist[0] = (SDL_Rect *) SDL_malloc(sizeof(SDL_Rect));
slouken@1895
   334
    private->SDL_modelist[0]->w = width;
slouken@1895
   335
    private->SDL_modelist[0]->h = height;
slouken@1895
   336
slouken@1895
   337
    vformat->BitsPerPixel = private->info.depth;
slouken@1895
   338
    vformat->BytesPerPixel = private->info.depth / 8;
slouken@1895
   339
slouken@1895
   340
    if (WSCONS_InitKeyboard(this) == -1) {
slouken@1895
   341
        return -1;
slouken@1895
   342
    }
slouken@1895
   343
slouken@1895
   344
    return 0;
icculus@1187
   345
}
icculus@1187
   346
slouken@1895
   347
SDL_Rect **
slouken@1895
   348
WSCONS_ListModes(_THIS, SDL_PixelFormat * format, Uint32 flags)
icculus@1187
   349
{
slouken@1895
   350
    if (format->BitsPerPixel == private->info.depth) {
slouken@1895
   351
        return private->SDL_modelist;
slouken@1895
   352
    } else {
slouken@1895
   353
        return NULL;
slouken@1895
   354
    }
icculus@1187
   355
}
icculus@1187
   356
slouken@1895
   357
SDL_Surface *
slouken@1895
   358
WSCONS_SetVideoMode(_THIS, SDL_Surface * current,
slouken@1895
   359
                    int width, int height, int bpp, Uint32 flags)
icculus@1187
   360
{
slouken@1895
   361
    if (width != private->SDL_modelist[0]->w ||
slouken@1895
   362
        height != private->SDL_modelist[0]->h) {
slouken@1895
   363
        WSCONS_ReportError("Requested video mode %dx%d not supported.",
slouken@1895
   364
                           width, height);
slouken@1895
   365
        return NULL;
slouken@1895
   366
    }
slouken@1895
   367
    if (bpp != private->info.depth) {
slouken@1895
   368
        WSCONS_ReportError("Requested video depth %d bpp not supported.",
slouken@1895
   369
                           bpp);
slouken@1895
   370
        return NULL;
slouken@1895
   371
    }
icculus@1187
   372
slouken@1895
   373
    if (!SDL_ReallocFormat(current,
slouken@1895
   374
                           bpp,
slouken@1895
   375
                           private->redMask,
slouken@1895
   376
                           private->greenMask, private->blueMask, 0)) {
slouken@1895
   377
        WSCONS_ReportError("Couldn't allocate new pixel format");
slouken@1895
   378
        return NULL;
slouken@1895
   379
    }
icculus@1187
   380
slouken@1895
   381
    current->flags &= SDL_FULLSCREEN;
slouken@1895
   382
    if (private->shadowFB) {
slouken@1895
   383
        current->flags |= SDL_SWSURFACE;
slouken@1895
   384
    } else {
slouken@1895
   385
        current->flags |= SDL_HWSURFACE;
slouken@1895
   386
    }
slouken@1895
   387
    current->w = width;
slouken@1895
   388
    current->h = height;
slouken@1895
   389
    current->pitch = private->fblinebytes;
slouken@1895
   390
    current->pixels = private->fbstart;
icculus@1187
   391
slouken@1895
   392
    SDL_memset(private->fbstart, 0, private->fbmem_len);
icculus@1187
   393
slouken@1895
   394
    return current;
icculus@1187
   395
}
icculus@1187
   396
slouken@1895
   397
static int
slouken@1895
   398
WSCONS_AllocHWSurface(_THIS, SDL_Surface * surface)
icculus@1187
   399
{
slouken@1895
   400
    return -1;
icculus@1187
   401
}
slouken@2735
   402
slouken@1895
   403
static void
slouken@1895
   404
WSCONS_FreeHWSurface(_THIS, SDL_Surface * surface)
icculus@1187
   405
{
icculus@1187
   406
}
icculus@1187
   407
slouken@1895
   408
static int
slouken@1895
   409
WSCONS_LockHWSurface(_THIS, SDL_Surface * surface)
icculus@1187
   410
{
slouken@1895
   411
    return 0;
icculus@1187
   412
}
icculus@1187
   413
slouken@1895
   414
static void
slouken@1895
   415
WSCONS_UnlockHWSurface(_THIS, SDL_Surface * surface)
icculus@1187
   416
{
icculus@1187
   417
}
icculus@1187
   418
slouken@1895
   419
static void
slouken@1895
   420
WSCONS_blit16(Uint8 * byte_src_pos,
slouken@1895
   421
              int srcRightDelta,
slouken@1895
   422
              int srcDownDelta,
slouken@1895
   423
              Uint8 * byte_dst_pos, int dst_linebytes, int width, int height)
icculus@1187
   424
{
slouken@1895
   425
    int w;
slouken@1895
   426
    Uint16 *src_pos = (Uint16 *) byte_src_pos;
slouken@1895
   427
    Uint16 *dst_pos = (Uint16 *) byte_dst_pos;
icculus@1187
   428
slouken@1895
   429
    while (height) {
slouken@1895
   430
        Uint16 *src = src_pos;
slouken@1895
   431
        Uint16 *dst = dst_pos;
slouken@1895
   432
        for (w = width; w != 0; w--) {
slouken@1895
   433
            *dst = *src;
slouken@1895
   434
            src += srcRightDelta;
slouken@1895
   435
            dst++;
slouken@1895
   436
        }
slouken@1895
   437
        dst_pos = (Uint16 *) ((Uint8 *) dst_pos + dst_linebytes);
slouken@1895
   438
        src_pos += srcDownDelta;
slouken@1895
   439
        height--;
icculus@1187
   440
    }
icculus@1187
   441
}
icculus@1187
   442
icculus@1187
   443
#define BLOCKSIZE_W 32
icculus@1187
   444
#define BLOCKSIZE_H 32
icculus@1187
   445
slouken@1895
   446
static void
slouken@1895
   447
WSCONS_blit16blocked(Uint8 * byte_src_pos,
slouken@1895
   448
                     int srcRightDelta,
slouken@1895
   449
                     int srcDownDelta,
slouken@1895
   450
                     Uint8 * byte_dst_pos,
slouken@1895
   451
                     int dst_linebytes, int width, int height)
icculus@1187
   452
{
slouken@1895
   453
    int w;
slouken@1895
   454
    Uint16 *src_pos = (Uint16 *) byte_src_pos;
slouken@1895
   455
    Uint16 *dst_pos = (Uint16 *) byte_dst_pos;
icculus@1187
   456
slouken@1895
   457
    while (height > 0) {
slouken@1895
   458
        Uint16 *src = src_pos;
slouken@1895
   459
        Uint16 *dst = dst_pos;
slouken@1895
   460
        for (w = width; w > 0; w -= BLOCKSIZE_W) {
slouken@1895
   461
            WSCONS_blit16((Uint8 *) src,
slouken@1895
   462
                          srcRightDelta,
slouken@1895
   463
                          srcDownDelta,
slouken@1895
   464
                          (Uint8 *) dst,
slouken@1895
   465
                          dst_linebytes,
slouken@1895
   466
                          min(w, BLOCKSIZE_W), min(height, BLOCKSIZE_H));
slouken@1895
   467
            src += srcRightDelta * BLOCKSIZE_W;
slouken@1895
   468
            dst += BLOCKSIZE_W;
slouken@1895
   469
        }
slouken@1895
   470
        dst_pos =
slouken@1895
   471
            (Uint16 *) ((Uint8 *) dst_pos + dst_linebytes * BLOCKSIZE_H);
slouken@1895
   472
        src_pos += srcDownDelta * BLOCKSIZE_H;
slouken@1895
   473
        height -= BLOCKSIZE_H;
icculus@1187
   474
    }
icculus@1187
   475
}
icculus@1187
   476
slouken@1895
   477
static void
slouken@1895
   478
WSCONS_UpdateRects(_THIS, int numrects, SDL_Rect * rects)
icculus@1187
   479
{
slouken@1895
   480
    int width = private->SDL_modelist[0]->w;
slouken@1895
   481
    int height = private->SDL_modelist[0]->h;
slouken@1895
   482
    int bytesPerPixel = (private->info.depth + 7) / 8;
slouken@1895
   483
    int i;
icculus@1187
   484
slouken@1895
   485
    if (!private->shadowFB) {
slouken@1895
   486
        return;
icculus@1187
   487
    }
icculus@1187
   488
slouken@1895
   489
    if (private->info.depth != 16) {
slouken@1895
   490
        WSCONS_ReportError("Shadow copy only implemented for 16 bpp");
slouken@1895
   491
        return;
icculus@1187
   492
    }
icculus@1187
   493
slouken@1895
   494
    for (i = 0; i < numrects; i++) {
slouken@1895
   495
        int x1, y1, x2, y2;
slouken@1895
   496
        int scr_x1, scr_y1, scr_x2, scr_y2;
slouken@1895
   497
        int sha_x1, sha_y1;
slouken@1895
   498
        int shadowRightDelta;   /* Address change when moving right in dest */
slouken@1895
   499
        int shadowDownDelta;    /* Address change when moving down in dest */
slouken@1895
   500
        Uint8 *src_start;
slouken@1895
   501
        Uint8 *dst_start;
icculus@1187
   502
slouken@1895
   503
        x1 = rects[i].x;
slouken@1895
   504
        y1 = rects[i].y;
slouken@1895
   505
        x2 = x1 + rects[i].w;
slouken@1895
   506
        y2 = y1 + rects[i].h;
slouken@1895
   507
slouken@1895
   508
        if (x1 < 0) {
slouken@1895
   509
            x1 = 0;
slouken@1895
   510
        } else if (x1 > width) {
slouken@1895
   511
            x1 = width;
slouken@1895
   512
        }
slouken@1895
   513
        if (x2 < 0) {
slouken@1895
   514
            x2 = 0;
slouken@1895
   515
        } else if (x2 > width) {
slouken@1895
   516
            x2 = width;
slouken@1895
   517
        }
slouken@1895
   518
        if (y1 < 0) {
slouken@1895
   519
            y1 = 0;
slouken@1895
   520
        } else if (y1 > height) {
slouken@1895
   521
            y1 = height;
slouken@1895
   522
        }
slouken@1895
   523
        if (y2 < 0) {
slouken@1895
   524
            y2 = 0;
slouken@1895
   525
        } else if (y2 > height) {
slouken@1895
   526
            y2 = height;
slouken@1895
   527
        }
slouken@1895
   528
        if (x2 <= x1 || y2 <= y1) {
slouken@1895
   529
            continue;
slouken@1895
   530
        }
slouken@1895
   531
slouken@1895
   532
        switch (private->rotate) {
slouken@1895
   533
        case WSCONS_ROTATE_NONE:
slouken@1895
   534
            sha_x1 = scr_x1 = x1;
slouken@1895
   535
            sha_y1 = scr_y1 = y1;
slouken@1895
   536
            scr_x2 = x2;
slouken@1895
   537
            scr_y2 = y2;
slouken@1895
   538
            shadowRightDelta = 1;
slouken@1895
   539
            shadowDownDelta = width;
slouken@1895
   540
            break;
slouken@1895
   541
        case WSCONS_ROTATE_CCW:
slouken@1895
   542
            scr_x1 = y1;
slouken@1895
   543
            scr_y1 = width - x2;
slouken@1895
   544
            scr_x2 = y2;
slouken@1895
   545
            scr_y2 = width - x1;
slouken@1895
   546
            sha_x1 = x2 - 1;
slouken@1895
   547
            sha_y1 = y1;
slouken@1895
   548
            shadowRightDelta = width;
slouken@1895
   549
            shadowDownDelta = -1;
slouken@1895
   550
            break;
slouken@1895
   551
        case WSCONS_ROTATE_UD:
slouken@1895
   552
            scr_x1 = width - x2;
slouken@1895
   553
            scr_y1 = height - y2;
slouken@1895
   554
            scr_x2 = width - x1;
slouken@1895
   555
            scr_y2 = height - y1;
slouken@1895
   556
            sha_x1 = x2 - 1;
slouken@1895
   557
            sha_y1 = y2 - 1;
slouken@1895
   558
            shadowRightDelta = -1;
slouken@1895
   559
            shadowDownDelta = -width;
slouken@1895
   560
            break;
slouken@1895
   561
        case WSCONS_ROTATE_CW:
slouken@1895
   562
            scr_x1 = height - y2;
slouken@1895
   563
            scr_y1 = x1;
slouken@1895
   564
            scr_x2 = height - y1;
slouken@1895
   565
            scr_y2 = x2;
slouken@1895
   566
            sha_x1 = x1;
slouken@1895
   567
            sha_y1 = y2 - 1;
slouken@1895
   568
            shadowRightDelta = -width;
slouken@1895
   569
            shadowDownDelta = 1;
slouken@1895
   570
            break;
slouken@1895
   571
        default:
slouken@1895
   572
            WSCONS_ReportError("Unknown rotation");
slouken@1895
   573
            return;
slouken@1895
   574
        }
slouken@1895
   575
slouken@1895
   576
        src_start =
slouken@1895
   577
            private->shadowmem + (sha_y1 * width + sha_x1) * bytesPerPixel;
slouken@1895
   578
        dst_start =
slouken@1895
   579
            private->physmem + scr_y1 * private->physlinebytes +
slouken@1895
   580
            scr_x1 * bytesPerPixel;
slouken@1895
   581
slouken@1895
   582
        private->blitFunc(src_start,
slouken@1895
   583
                          shadowRightDelta,
slouken@1895
   584
                          shadowDownDelta,
slouken@1895
   585
                          dst_start,
slouken@1895
   586
                          private->physlinebytes,
slouken@1895
   587
                          scr_x2 - scr_x1, scr_y2 - scr_y1);
slouken@1895
   588
    }
icculus@1187
   589
}
icculus@1187
   590
slouken@1895
   591
int
slouken@1895
   592
WSCONS_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color * colors)
icculus@1187
   593
{
slouken@1895
   594
    return 0;
icculus@1187
   595
}
icculus@1187
   596
icculus@1187
   597
/*
icculus@1187
   598
 * Note: If we are terminated, this could be called in the middle of
icculus@1187
   599
 * another SDL video routine -- notably UpdateRects.
icculus@1187
   600
 */
slouken@1895
   601
void
slouken@1895
   602
WSCONS_VideoQuit(_THIS)
icculus@1187
   603
{
slouken@1895
   604
    int mode = WSDISPLAYIO_MODE_EMUL;
icculus@1187
   605
slouken@1895
   606
    if (private->shadowmem != NULL) {
slouken@1895
   607
        SDL_free(private->shadowmem);
slouken@1895
   608
        private->shadowmem = NULL;
slouken@1895
   609
    }
slouken@1895
   610
    private->fbstart = NULL;
slouken@1895
   611
    if (this->screen != NULL) {
slouken@1895
   612
        this->screen->pixels = NULL;
slouken@1895
   613
    }
icculus@1187
   614
slouken@1895
   615
    if (private->SDL_modelist[0] != NULL) {
slouken@1895
   616
        SDL_free(private->SDL_modelist[0]);
slouken@1895
   617
        private->SDL_modelist[0] = NULL;
slouken@1895
   618
    }
icculus@1187
   619
slouken@1895
   620
    if (ioctl(private->fd, WSDISPLAYIO_SMODE, &mode) == -1) {
slouken@1895
   621
        WSCONS_ReportError("ioctl SMODE");
slouken@1895
   622
    }
icculus@1187
   623
slouken@1895
   624
    WSCONS_ReleaseKeyboard(this);
icculus@1187
   625
slouken@1895
   626
    if (private->fd != -1) {
slouken@1895
   627
        close(private->fd);
slouken@1895
   628
        private->fd = -1;
slouken@1895
   629
    }
icculus@1187
   630
}
slouken@1895
   631
slouken@1895
   632
/* vi: set ts=4 sw=4 expandtab: */