view src/video/bwindow/SDL_sysvideo.cc @ 4532:dc4a3de60d3a

Logitech G15 on Ubuntu 10.04: code = 1, sym = 0xFF1B (Escape) scancode = 41 (Escape) code = 2, sym = 0x31 (1) scancode = 30 (1) code = 3, sym = 0x32 (2) scancode = 31 (2) code = 4, sym = 0x33 (3) scancode = 32 (3) code = 5, sym = 0x34 (4) scancode = 33 (4) code = 6, sym = 0x35 (5) scancode = 34 (5) code = 7, sym = 0x36 (6) scancode = 35 (6) code = 8, sym = 0x37 (7) scancode = 36 (7) code = 9, sym = 0x38 (8) scancode = 37 (8) code = 10, sym = 0x39 (9) scancode = 38 (9) code = 11, sym = 0x30 (0) scancode = 39 (0) code = 12, sym = 0x2D (minus) scancode = 45 (-) code = 13, sym = 0x3D (equal) scancode = 46 (=) code = 14, sym = 0xFF08 (BackSpace) scancode = 42 (Backspace) code = 15, sym = 0xFF09 (Tab) scancode = 43 (Tab) code = 16, sym = 0x71 (q) scancode = 20 (Q) code = 17, sym = 0x77 (w) scancode = 26 (W) code = 18, sym = 0x65 (e) scancode = 8 (E) code = 19, sym = 0x72 (r) scancode = 21 (R) code = 20, sym = 0x74 (t) scancode = 23 (T) code = 21, sym = 0x79 (y) scancode = 28 (Y) code = 22, sym = 0x75 (u) scancode = 24 (U) code = 23, sym = 0x69 (i) scancode = 12 (I) code = 24, sym = 0x6F (o) scancode = 18 (O) code = 25, sym = 0x70 (p) scancode = 19 (P) code = 26, sym = 0x5B (bracketleft) scancode = 47 ([) code = 27, sym = 0x5D (bracketright) scancode = 48 (]) code = 28, sym = 0xFF0D (Return) scancode = 40 (Return) code = 29, sym = 0xFFE3 (Control_L) scancode = 224 (Left Ctrl) code = 30, sym = 0x61 (a) scancode = 4 (A) code = 31, sym = 0x73 (s) scancode = 22 (S) code = 32, sym = 0x64 (d) scancode = 7 (D) code = 33, sym = 0x66 (f) scancode = 9 (F) code = 34, sym = 0x67 (g) scancode = 10 (G) code = 35, sym = 0x68 (h) scancode = 11 (H) code = 36, sym = 0x6A (j) scancode = 13 (J) code = 37, sym = 0x6B (k) scancode = 14 (K) code = 38, sym = 0x6C (l) scancode = 15 (L) code = 39, sym = 0x3B (semicolon) scancode = 51 (;) code = 40, sym = 0x27 (apostrophe) scancode = 52 (') code = 41, sym = 0x60 (grave) scancode = 53 (`) code = 42, sym = 0xFFE1 (Shift_L) scancode = 225 (Left Shift) code = 43, sym = 0x5C (backslash) scancode = 49 (\) code = 44, sym = 0x7A (z) scancode = 29 (Z) code = 45, sym = 0x78 (x) scancode = 27 (X) code = 46, sym = 0x63 (c) scancode = 6 (C) code = 47, sym = 0x76 (v) scancode = 25 (V) code = 48, sym = 0x62 (b) scancode = 5 (B) code = 49, sym = 0x6E (n) scancode = 17 (N) code = 50, sym = 0x6D (m) scancode = 16 (M) code = 51, sym = 0x2C (comma) scancode = 54 (,) code = 52, sym = 0x2E (period) scancode = 55 (.) code = 53, sym = 0x2F (slash) scancode = 56 (/) code = 54, sym = 0xFFE2 (Shift_R) scancode = 229 (Right Shift) code = 55, sym = 0xFFAA (KP_Multiply) scancode = 85 (Keypad *) code = 56, sym = 0xFFE9 (Alt_L) scancode = 226 (Left Alt) code = 57, sym = 0x20 (space) scancode = 44 (Space) code = 58, sym = 0xFFE5 (Caps_Lock) scancode = 57 (CapsLock) code = 59, sym = 0xFFBE (F1) scancode = 58 (F1) code = 60, sym = 0xFFBF (F2) scancode = 59 (F2) code = 61, sym = 0xFFC0 (F3) scancode = 60 (F3) code = 62, sym = 0xFFC1 (F4) scancode = 61 (F4) code = 63, sym = 0xFFC2 (F5) scancode = 62 (F5) code = 64, sym = 0xFFC3 (F6) scancode = 63 (F6) code = 65, sym = 0xFFC4 (F7) scancode = 64 (F7) code = 66, sym = 0xFFC5 (F8) scancode = 65 (F8) code = 67, sym = 0xFFC6 (F9) scancode = 66 (F9) code = 68, sym = 0xFFC7 (F10) scancode = 67 (F10) code = 69, sym = 0xFF7F (Num_Lock) scancode = 83 (Numlock) code = 70, sym = 0xFF14 (Scroll_Lock) scancode = 71 (ScrollLock) code = 71, sym = 0xFF95 (KP_Home) scancode = 95 (Keypad 7) code = 72, sym = 0xFF97 (KP_Up) scancode = 96 (Keypad 8) code = 73, sym = 0xFF9A (KP_Prior) scancode = 97 (Keypad 9) code = 74, sym = 0xFFAD (KP_Subtract) scancode = 86 (Keypad -) code = 75, sym = 0xFF96 (KP_Left) scancode = 92 (Keypad 4) code = 76, sym = 0xFF9D (KP_Begin) scancode = 93 (Keypad 5) code = 77, sym = 0xFF98 (KP_Right) scancode = 94 (Keypad 6) code = 78, sym = 0xFFAB (KP_Add) scancode = 87 (Keypad +) code = 79, sym = 0xFF9C (KP_End) scancode = 89 (Keypad 1) code = 80, sym = 0xFF99 (KP_Down) scancode = 90 (Keypad 2) code = 81, sym = 0xFF9B (KP_Next) scancode = 91 (Keypad 3) code = 82, sym = 0xFF9E (KP_Insert) scancode = 98 (Keypad 0) code = 83, sym = 0xFF9F (KP_Delete) scancode = 99 (Keypad .) code = 84, sym = 0xFE03 (ISO_Level3_Shift) scancode = 0 () code = 86, sym = 0x3C (less) scancode not found code = 87, sym = 0xFFC8 (F11) scancode = 68 (F11) code = 88, sym = 0xFFC9 (F12) scancode = 69 (F12) code = 90, sym = 0xFF26 (Katakana) scancode = 0 () code = 91, sym = 0xFF25 (Hiragana) scancode = 0 () code = 92, sym = 0xFF23 (Henkan_Mode) scancode = 0 () code = 93, sym = 0xFF27 (Hiragana_Katakana) scancode = 0 () code = 94, sym = 0xFF22 (Muhenkan) scancode = 0 () code = 96, sym = 0xFF8D (KP_Enter) scancode = 88 (Keypad Enter) code = 97, sym = 0xFFE4 (Control_R) scancode = 228 (Right Ctrl) code = 98, sym = 0xFFAF (KP_Divide) scancode = 84 (Keypad /) code = 99, sym = 0xFF61 (Print) scancode = 70 (PrintScreen) code = 100, sym = 0xFFEA (Alt_R) scancode = 230 (Right Alt) code = 101, sym = 0xFF0A (Linefeed) scancode = 0 () code = 102, sym = 0xFF50 (Home) scancode = 74 (Home) code = 103, sym = 0xFF52 (Up) scancode = 82 (Up) code = 104, sym = 0xFF55 (Prior) scancode = 75 (PageUp) code = 105, sym = 0xFF51 (Left) scancode = 80 (Left) code = 106, sym = 0xFF53 (Right) scancode = 79 (Right) code = 107, sym = 0xFF57 (End) scancode = 77 (End) code = 108, sym = 0xFF54 (Down) scancode = 81 (Down) code = 109, sym = 0xFF56 (Next) scancode = 78 (PageDown) code = 110, sym = 0xFF63 (Insert) scancode = 73 (Insert) code = 111, sym = 0xFFFF (Delete) scancode = 76 (Delete) code = 113, sym = 0x1008FF12 (XF86AudioMute) scancode = 0 () code = 114, sym = 0x1008FF11 (XF86AudioLowerVolume) scancode = 0 () code = 115, sym = 0x1008FF13 (XF86AudioRaiseVolume) scancode = 0 () code = 116, sym = 0x1008FF2A (XF86PowerOff) scancode = 0 () code = 117, sym = 0xFFBD (KP_Equal) scancode = 103 (Keypad =) code = 118, sym = 0xB1 (plusminus) scancode not found code = 119, sym = 0xFF13 (Pause) scancode = 72 (Pause) code = 120, sym = 0x1008FF4A (XF86LaunchA) scancode = 0 () code = 121, sym = 0xFFAE (KP_Decimal) scancode = 99 (Keypad .) code = 122, sym = 0xFF31 (Hangul) scancode = 0 () code = 123, sym = 0xFF34 (Hangul_Hanja) scancode = 0 () code = 125, sym = 0xFFEB (Super_L) scancode = 227 (Left GUI) code = 126, sym = 0xFFEC (Super_R) scancode = 231 (Right GUI) code = 127, sym = 0xFF67 (Menu) scancode = 118 (Menu) code = 128, sym = 0xFF69 (Cancel) scancode = 120 (Stop) code = 129, sym = 0xFF66 (Redo) scancode = 121 (Again) code = 130, sym = 0x1005FF70 (SunProps) scancode = 0 () code = 131, sym = 0xFF65 (Undo) scancode = 122 (Undo) code = 132, sym = 0x1005FF71 (SunFront) scancode = 0 () code = 133, sym = 0x1008FF57 (XF86Copy) scancode = 0 () code = 134, sym = 0x1005FF73 (SunOpen) scancode = 0 () code = 135, sym = 0x1008FF6D (XF86Paste) scancode = 0 () code = 136, sym = 0xFF68 (Find) scancode = 126 (Find) code = 137, sym = 0x1008FF58 (XF86Cut) scancode = 0 () code = 138, sym = 0xFF6A (Help) scancode = 117 (Help) code = 139, sym = 0x1008FF65 (XF86MenuKB) scancode = 0 () code = 140, sym = 0x1008FF1D (XF86Calculator) scancode = 0 () code = 142, sym = 0x1008FF2F (XF86Sleep) scancode = 0 () code = 143, sym = 0x1008FF2B (XF86WakeUp) scancode = 0 () code = 144, sym = 0x1008FF5D (XF86Explorer) scancode = 0 () code = 145, sym = 0x1008FF7B (XF86Send) scancode = 0 () code = 147, sym = 0x1008FF8A (XF86Xfer) scancode = 0 () code = 148, sym = 0x1008FF41 (XF86Launch1) scancode = 0 () code = 149, sym = 0x1008FF42 (XF86Launch2) scancode = 0 () code = 150, sym = 0x1008FF2E (XF86WWW) scancode = 0 () code = 151, sym = 0x1008FF5A (XF86DOS) scancode = 0 () code = 152, sym = 0x1008FF2D (XF86ScreenSaver) scancode = 0 () code = 154, sym = 0x1008FF74 (XF86RotateWindows) scancode = 0 () code = 155, sym = 0x1008FF19 (XF86Mail) scancode = 0 () code = 156, sym = 0x1008FF30 (XF86Favorites) scancode = 0 () code = 157, sym = 0x1008FF33 (XF86MyComputer) scancode = 0 () code = 158, sym = 0x1008FF26 (XF86Back) scancode = 0 () code = 159, sym = 0x1008FF27 (XF86Forward) scancode = 0 () code = 161, sym = 0x1008FF2C (XF86Eject) scancode = 0 () code = 162, sym = 0x1008FF2C (XF86Eject) scancode = 0 () code = 163, sym = 0x1008FF17 (XF86AudioNext) scancode = 0 () code = 164, sym = 0x1008FF14 (XF86AudioPlay) scancode = 0 () code = 165, sym = 0x1008FF16 (XF86AudioPrev) scancode = 0 () code = 166, sym = 0x1008FF15 (XF86AudioStop) scancode = 0 () code = 167, sym = 0x1008FF1C (XF86AudioRecord) scancode = 0 () code = 168, sym = 0x1008FF3E (XF86AudioRewind) scancode = 0 () code = 169, sym = 0x1008FF6E (XF86Phone) scancode = 0 () code = 171, sym = 0x1008FF81 (XF86Tools) scancode = 0 () code = 172, sym = 0x1008FF18 (XF86HomePage) scancode = 0 () code = 173, sym = 0x1008FF73 (XF86Reload) scancode = 0 () code = 174, sym = 0x1008FF56 (XF86Close) scancode = 0 () code = 177, sym = 0x1008FF78 (XF86ScrollUp) scancode = 0 () code = 178, sym = 0x1008FF79 (XF86ScrollDown) scancode = 0 () code = 179, sym = 0x28 (parenleft) scancode not found code = 180, sym = 0x29 (parenright) scancode not found code = 181, sym = 0x1008FF68 (XF86New) scancode = 0 () code = 182, sym = 0xFF66 (Redo) scancode = 121 (Again) code = 183, sym = 0x1008FF81 (XF86Tools) scancode = 0 () code = 184, sym = 0x1008FF45 (XF86Launch5) scancode = 0 () code = 185, sym = 0x1008FF65 (XF86MenuKB) scancode = 0 () code = 192, sym = 0x1008FFA9 (XF86TouchpadToggle) scancode = 0 () code = 195, sym = 0xFF7E (Mode_switch) scancode = 257 (ModeSwitch) code = 200, sym = 0x1008FF14 (XF86AudioPlay) scancode = 0 () code = 201, sym = 0x1008FF31 (XF86AudioPause) scancode = 0 () code = 202, sym = 0x1008FF43 (XF86Launch3) scancode = 0 () code = 203, sym = 0x1008FF44 (XF86Launch4) scancode = 0 () code = 204, sym = 0x1008FF4B (XF86LaunchB) scancode = 0 () code = 205, sym = 0x1008FFA7 (XF86Suspend) scancode = 0 () code = 206, sym = 0x1008FF56 (XF86Close) scancode = 0 () code = 207, sym = 0x1008FF14 (XF86AudioPlay) scancode = 0 () code = 208, sym = 0x1008FF97 (XF86AudioForward) scancode = 0 () code = 210, sym = 0xFF61 (Print) scancode = 70 (PrintScreen) code = 212, sym = 0x1008FF8F (XF86WebCam) scancode = 0 () code = 215, sym = 0x1008FF19 (XF86Mail) scancode = 0 () code = 217, sym = 0x1008FF1B (XF86Search) scancode = 0 () code = 219, sym = 0x1008FF3C (XF86Finance) scancode = 0 () code = 221, sym = 0x1008FF36 (XF86Shop) scancode = 0 () code = 223, sym = 0xFF69 (Cancel) scancode = 120 (Stop) code = 224, sym = 0x1008FF03 (XF86MonBrightnessDown) scancode = 0 () code = 225, sym = 0x1008FF02 (XF86MonBrightnessUp) scancode = 0 () code = 226, sym = 0x1008FF32 (XF86AudioMedia) scancode = 0 () code = 227, sym = 0x1008FF59 (XF86Display) scancode = 0 () code = 228, sym = 0x1008FF04 (XF86KbdLightOnOff) scancode = 0 () code = 229, sym = 0x1008FF06 (XF86KbdBrightnessDown) scancode = 0 () code = 230, sym = 0x1008FF05 (XF86KbdBrightnessUp) scancode = 0 () code = 231, sym = 0x1008FF7B (XF86Send) scancode = 0 () code = 232, sym = 0x1008FF72 (XF86Reply) scancode = 0 () code = 233, sym = 0x1008FF90 (XF86MailForward) scancode = 0 () code = 234, sym = 0x1008FF77 (XF86Save) scancode = 0 () code = 235, sym = 0x1008FF5B (XF86Documents) scancode = 0 () code = 236, sym = 0x1008FF93 (XF86Battery) scancode = 0 () code = 237, sym = 0x1008FF94 (XF86Bluetooth) scancode = 0 () code = 238, sym = 0x1008FF95 (XF86WLAN) scancode = 0 ()
author Sam Lantinga <slouken@libsdl.org>
date Sun, 18 Jul 2010 00:40:17 -0700
parents 8cc00819c8d6
children 3a8a452b49f0
line wrap: on
line source

/*
    SDL - Simple DirectMedia Layer
    Copyright (C) 1997-2009 Sam Lantinga

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Sam Lantinga
    slouken@libsdl.org
*/
#include "SDL_config.h"

/* BWindow based framebuffer implementation */

#include <unistd.h>

#include "SDL_BWin.h"
#include "SDL_timer.h"

extern "C"
{

#include "../SDL_sysvideo.h"
#include "../../events/SDL_events_c.h"
#include "SDL_sysevents_c.h"
#include "SDL_sysmouse_c.h"
#include "SDL_syswm_c.h"
#include "SDL_lowvideo.h"
#include "../SDL_yuvfuncs.h"
#include "SDL_sysyuv.h"
#include "../blank_cursor.h"

#define BEOS_HIDDEN_SIZE	32      /* starting hidden window size */

/* Initialization/Query functions */
    static int BE_VideoInit(_THIS, SDL_PixelFormat * vformat);
    static SDL_Rect **BE_ListModes(_THIS, SDL_PixelFormat * format,
                                   Uint32 flags);
    static SDL_Surface *BE_SetVideoMode(_THIS, SDL_Surface * current,
                                        int width, int height, int bpp,
                                        Uint32 flags);
    static void BE_UpdateMouse(_THIS);
    static int BE_SetColors(_THIS, int firstcolor, int ncolors,
                            SDL_Color * colors);
    static void BE_VideoQuit(_THIS);

/* Hardware surface functions */
    static int BE_AllocHWSurface(_THIS, SDL_Surface * surface);
    static int BE_LockHWSurface(_THIS, SDL_Surface * surface);
    static void BE_UnlockHWSurface(_THIS, SDL_Surface * surface);
    static void BE_FreeHWSurface(_THIS, SDL_Surface * surface);

    static int BE_ToggleFullScreen(_THIS, int fullscreen);
    static SDL_Overlay *BE_CreateYUVOverlay(_THIS, int width, int height,
                                            Uint32 format,
                                            SDL_Surface * display);

/* OpenGL functions */
#if SDL_VIDEO_OPENGL
    static int BE_GL_LoadLibrary(_THIS, const char *path);
    static void *BE_GL_GetProcAddress(_THIS, const char *proc);
    static int BE_GL_GetAttribute(_THIS, SDL_GLattr attrib, int *value);
    static int BE_GL_MakeCurrent(_THIS);
    static void BE_GL_SwapBuffers(_THIS);
#endif

/* FB driver bootstrap functions */

    static int BE_Available(void)
    {
        return (1);
    }

    static void BE_DeleteDevice(SDL_VideoDevice * device)
    {
        SDL_free(device->hidden);
        SDL_free(device);
    }

    static SDL_VideoDevice *BE_CreateDevice(int devindex)
    {
        SDL_VideoDevice *device;

        /* Initialize all variables that we clean on shutdown */
        device = (SDL_VideoDevice *) SDL_malloc(sizeof(SDL_VideoDevice));
        if (device) {
            SDL_memset(device, 0, (sizeof *device));
            device->hidden = (struct SDL_PrivateVideoData *)
                SDL_malloc((sizeof *device->hidden));
        }
        if ((device == NULL) || (device->hidden == NULL)) {
            SDL_OutOfMemory();
            if (device) {
                SDL_free(device);
            }
            return (0);
        }
        SDL_memset(device->hidden, 0, (sizeof *device->hidden));

        /* Set the function pointers */
        /* Initialization/Query functions */
        device->VideoInit = BE_VideoInit;
        device->ListModes = BE_ListModes;
        device->SetVideoMode = BE_SetVideoMode;
        device->ToggleFullScreen = BE_ToggleFullScreen;
        device->UpdateMouse = BE_UpdateMouse;
        device->CreateYUVOverlay = BE_CreateYUVOverlay;
        device->SetColors = BE_SetColors;
        device->UpdateRects = NULL;
        device->VideoQuit = BE_VideoQuit;
        /* Hardware acceleration functions */
        device->AllocHWSurface = BE_AllocHWSurface;
        device->CheckHWBlit = NULL;
        device->FillHWRect = NULL;
        device->SetHWColorKey = NULL;
        device->SetHWAlpha = NULL;
        device->LockHWSurface = BE_LockHWSurface;
        device->UnlockHWSurface = BE_UnlockHWSurface;
        device->FlipHWSurface = NULL;
        device->FreeHWSurface = BE_FreeHWSurface;
        /* Gamma support */
#if SDL_VIDEO_OPENGL
        /* OpenGL support */
        device->GL_LoadLibrary = BE_GL_LoadLibrary;
        device->GL_GetProcAddress = BE_GL_GetProcAddress;
        device->GL_GetAttribute = BE_GL_GetAttribute;
        device->GL_MakeCurrent = BE_GL_MakeCurrent;
        device->GL_SwapBuffers = BE_GL_SwapBuffers;
#endif
        /* Window manager functions */
        device->SetCaption = BE_SetWMCaption;
        device->SetIcon = NULL;
        device->IconifyWindow = BE_IconifyWindow;
        device->GrabInput = NULL;
        device->GetWMInfo = BE_GetWMInfo;
        /* Cursor manager functions */
        device->FreeWMCursor = BE_FreeWMCursor;
        device->CreateWMCursor = BE_CreateWMCursor;
        device->ShowWMCursor = BE_ShowWMCursor;
        device->WarpWMCursor = BE_WarpWMCursor;
        device->MoveWMCursor = NULL;
        device->CheckMouseMode = NULL;
        /* Event manager functions */
        device->InitOSKeymap = BE_InitOSKeymap;
        device->PumpEvents = BE_PumpEvents;

        device->free = BE_DeleteDevice;

        /* Set the driver flags */
        device->handles_any_size = 1;

        return device;
    }

    VideoBootStrap BWINDOW_bootstrap = {
        "bwindow", "BDirectWindow graphics",
        BE_Available, BE_CreateDevice
    };

    static inline int ColorSpaceToBitsPerPixel(uint32 colorspace)
    {
        int bitsperpixel;

        bitsperpixel = 0;
        switch (colorspace) {
        case B_CMAP8:
            bitsperpixel = 8;
            break;
        case B_RGB15:
        case B_RGBA15:
        case B_RGB15_BIG:
        case B_RGBA15_BIG:
            bitsperpixel = 15;
            break;
        case B_RGB16:
        case B_RGB16_BIG:
            bitsperpixel = 16;
            break;
        case B_RGB32:
        case B_RGBA32:
        case B_RGB32_BIG:
        case B_RGBA32_BIG:
            bitsperpixel = 32;
            break;
        default:
            break;
        }
        return (bitsperpixel);
    }

/* Function to sort the display_list in bscreen */
    static int CompareModes(const void *A, const void *B)
    {
        const display_mode *a = (display_mode *) A;
        const display_mode *b = (display_mode *) B;

        if (a->space == b->space) {
            return ((b->virtual_width * b->virtual_height) -
                    (a->virtual_width * a->virtual_height));
        } else {
            return (ColorSpaceToBitsPerPixel(b->space) -
                    ColorSpaceToBitsPerPixel(a->space));
        }
    }

/* Yes, this isn't the fastest it could be, but it works nicely */
    static int BE_AddMode(_THIS, int index, unsigned int w, unsigned int h)
    {
        SDL_Rect *mode;
        int i;
        int next_mode;

        /* Check to see if we already have this mode */
        if (SDL_nummodes[index] > 0) {
            for (i = SDL_nummodes[index] - 1; i >= 0; --i) {
                mode = SDL_modelist[index][i];
                if ((mode->w == w) && (mode->h == h)) {
#ifdef BWINDOW_DEBUG
                    fprintf(stderr,
                            "We already have mode %dx%d at %d bytes per pixel\n",
                            w, h, index + 1);
#endif
                    return (0);
                }
            }
        }

        /* Set up the new video mode rectangle */
        mode = (SDL_Rect *) SDL_malloc(sizeof *mode);
        if (mode == NULL) {
            SDL_OutOfMemory();
            return (-1);
        }
        mode->x = 0;
        mode->y = 0;
        mode->w = w;
        mode->h = h;
#ifdef BWINDOW_DEBUG
        fprintf(stderr, "Adding mode %dx%d at %d bytes per pixel\n", w, h,
                index + 1);
#endif

        /* Allocate the new list of modes, and fill in the new mode */
        next_mode = SDL_nummodes[index];
        SDL_modelist[index] = (SDL_Rect **)
            SDL_realloc(SDL_modelist[index],
                        (1 + next_mode + 1) * sizeof(SDL_Rect *));
        if (SDL_modelist[index] == NULL) {
            SDL_OutOfMemory();
            SDL_nummodes[index] = 0;
            SDL_free(mode);
            return (-1);
        }
        SDL_modelist[index][next_mode] = mode;
        SDL_modelist[index][next_mode + 1] = NULL;
        SDL_nummodes[index]++;

        return (0);
    }

    int BE_VideoInit(_THIS, SDL_PixelFormat * vformat)
    {
        display_mode *modes;
        uint32 i, nmodes;
        int bpp;
        BRect bounds;

        /* Initialize the Be Application for appserver interaction */
        if (SDL_InitBeApp() < 0) {
            return (-1);
        }

        /* It is important that this be created after SDL_InitBeApp() */
        BScreen bscreen;

        /* Save the current display mode */
        bscreen.GetMode(&saved_mode);
        _this->info.current_w = saved_mode.virtual_width;
        _this->info.current_h = saved_mode.virtual_height;

        /* Determine the screen depth */
        vformat->BitsPerPixel =
            ColorSpaceToBitsPerPixel(bscreen.ColorSpace());
        if (vformat->BitsPerPixel == 0) {
            SDL_SetError("Unknown BScreen colorspace: 0x%x",
                         bscreen.ColorSpace());
            return (-1);
        }

        /* Get the video modes we can switch to in fullscreen mode */
        bscreen.GetModeList(&modes, &nmodes);
        SDL_qsort(modes, nmodes, sizeof *modes, CompareModes);
        for (i = 0; i < nmodes; ++i) {
            bpp = ColorSpaceToBitsPerPixel(modes[i].space);
            //if ( bpp != 0 ) { // There are bugs in changing colorspace
            if (modes[i].space == saved_mode.space) {
                BE_AddMode(_this, ((bpp + 7) / 8) - 1,
                           modes[i].virtual_width, modes[i].virtual_height);
            }
        }

        /* Create the window and view */
        bounds.top = 0;
        bounds.left = 0;
        bounds.right = BEOS_HIDDEN_SIZE;
        bounds.bottom = BEOS_HIDDEN_SIZE;
        SDL_Win = new SDL_BWin(bounds);

#if SDL_VIDEO_OPENGL
        /* testgl application doesn't load library, just tries to load symbols */
        /* is it correct? if so we have to load library here */
        BE_GL_LoadLibrary(_this, NULL);
#endif

        /* Create the clear cursor */
        SDL_BlankCursor = BE_CreateWMCursor(_this, blank_cdata, blank_cmask,
                                            BLANK_CWIDTH, BLANK_CHEIGHT,
                                            BLANK_CHOTX, BLANK_CHOTY);

        /* Fill in some window manager capabilities */
        _this->info.wm_available = 1;

        /* We're done! */
        return (0);
    }

/* We support any dimension at our bit-depth */
    SDL_Rect **BE_ListModes(_THIS, SDL_PixelFormat * format, Uint32 flags)
    {
        SDL_Rect **modes;

        modes = ((SDL_Rect **) 0);
        if ((flags & SDL_FULLSCREEN) == SDL_FULLSCREEN) {
            modes = SDL_modelist[((format->BitsPerPixel + 7) / 8) - 1];
        } else {
            if (format->BitsPerPixel == _this->screen->format->BitsPerPixel) {
                modes = ((SDL_Rect **) - 1);
            }
        }
        return (modes);
    }

/* Various screen update functions available */
    static void BE_NormalUpdate(_THIS, int numrects, SDL_Rect * rects);


/* Find the closest display mode for fullscreen */
    static bool BE_FindClosestFSMode(_THIS, int width, int height, int bpp,
                                     display_mode * mode)
    {
        BScreen bscreen;
        uint32 i, nmodes;
        SDL_Rect **modes;
        display_mode *dmodes;
        display_mode current;
        float current_refresh;
        bscreen.GetMode(&current);
        current_refresh = (1000 * current.timing.pixel_clock) /
            (current.timing.h_total * current.timing.v_total);

        modes = SDL_modelist[((bpp + 7) / 8) - 1];

        bool exactmatch = false;
        for (uint32 x = 0; modes[x]; x++) {
            if (modes[x]->w == width && modes[x]->h == height) {
                exactmatch = true;
                i = x;
                break;
            }
        }
        if (!exactmatch) {
            for (i = 0; modes[i] && (modes[i]->w > width) &&
                 (modes[i]->h > height); ++i) {
                /* still looking */
            }
            if (!modes[i] || (modes[i]->w < width) || (modes[i]->h < height)) {
                /* We went too far */
                --i;
            }
        }
        width = modes[i]->w;
        height = modes[i]->h;
        bscreen.GetModeList(&dmodes, &nmodes);
        for (i = 0; i < nmodes; ++i) {
            if ((bpp == ColorSpaceToBitsPerPixel(dmodes[i].space)) &&
                (width == dmodes[i].virtual_width) &&
                (height == dmodes[i].virtual_height)) {
                break;
            }
        }
        if (i != nmodes) {
            *mode = dmodes[i];
            if ((mode->virtual_width <= current.virtual_width) &&
                (mode->virtual_height <= current.virtual_height)) {
                float new_refresh = (1000 * mode->timing.pixel_clock) /
                    (mode->timing.h_total * mode->timing.v_total);
                if (new_refresh < current_refresh) {
                    mode->timing.pixel_clock =
                        (uint32) ((mode->timing.h_total *
                                   mode->timing.v_total) *
                                  current_refresh / 1000);
                }
            }
            return true;
        } else {
            return false;
        }
    }

    static int BE_SetFullScreen(_THIS, SDL_Surface * screen, int fullscreen)
    {
        int was_fullscreen;
        bool needs_unlock;
        BScreen bscreen;
        BRect bounds;
        display_mode mode;
        int width, height, bpp;

        /* Set the fullscreen mode */
        was_fullscreen = SDL_Win->IsFullScreen();
        SDL_Win->SetFullScreen(fullscreen);
        fullscreen = SDL_Win->IsFullScreen();

        width = screen->w;
        height = screen->h;

        /* Set the appropriate video mode */
        if (fullscreen) {
            bpp = screen->format->BitsPerPixel;
            bscreen.GetMode(&mode);
            if ((bpp != ColorSpaceToBitsPerPixel(mode.space)) ||
                (width != mode.virtual_width) ||
                (height != mode.virtual_height)) {
                if (BE_FindClosestFSMode(_this, width, height, bpp, &mode)) {
                    bscreen.SetMode(&mode);
                    /* This simply stops the next resize event from being
                     * sent to the SDL handler.
                     */
                    SDL_Win->InhibitResize();
                } else {
                    fullscreen = 0;
                    SDL_Win->SetFullScreen(fullscreen);
                }
            }
        }
        if (was_fullscreen && !fullscreen) {
            bscreen.SetMode(&saved_mode);
        }

        if (SDL_Win->Lock()) {
            int xoff, yoff;
            if (SDL_Win->Shown()) {
                needs_unlock = 1;
                SDL_Win->Hide();
            } else {
                needs_unlock = 0;
            }
            /* This resizes the window and view area, but inhibits resizing
             * of the BBitmap due to the InhibitResize call above. Thus the
             * bitmap (pixel data) never changes.
             */
            SDL_Win->ResizeTo(width, height);
            bounds = bscreen.Frame();
            /* Calculate offsets - used either to center window
             * (windowed mode) or to set drawing offsets (fullscreen mode)
             */
            xoff = (bounds.IntegerWidth() - width) / 2;
            yoff = (bounds.IntegerHeight() - height) / 2;
            if (fullscreen) {
                /* Set offset for drawing */
                SDL_Win->SetXYOffset(xoff, yoff);
            } else {
                /* Center window and reset the drawing offset */
                SDL_Win->SetXYOffset(0, 0);
            }
            if (!needs_unlock || was_fullscreen) {
                /* Center the window the first time */
                SDL_Win->MoveTo(xoff > 0 ? (float) xoff : 0.0f,
                                yoff > 0 ? (float) yoff : 0.0f);
            }
            SDL_Win->Show();

            /* Unlock the window manually after the first Show() */
            if (needs_unlock) {
                SDL_Win->Unlock();
            }
        }

        /* Set the fullscreen flag in the screen surface */
        if (fullscreen) {
            screen->flags |= SDL_FULLSCREEN;
        } else {
            screen->flags &= ~SDL_FULLSCREEN;
        }
        return (1);
    }

    static int BE_ToggleFullScreen(_THIS, int fullscreen)
    {
        return BE_SetFullScreen(_this, _this->screen, fullscreen);
    }

/* FIXME: check return values and cleanup here */
    SDL_Surface *BE_SetVideoMode(_THIS, SDL_Surface * current,
                                 int width, int height, int bpp, Uint32 flags)
    {
        BScreen bscreen;
        BBitmap *bbitmap;
        BRect bounds;
        Uint32 gl_flags = 0;

        /* Only RGB works on r5 currently */
        gl_flags = BGL_RGB;
        if (_this->gl_config.double_buffer)
            gl_flags |= BGL_DOUBLE;
        else
            gl_flags |= BGL_SINGLE;
        if (_this->gl_config.alpha_size > 0 || bpp == 32)
            gl_flags |= BGL_ALPHA;
        if (_this->gl_config.depth_size > 0)
            gl_flags |= BGL_DEPTH;
        if (_this->gl_config.stencil_size > 0)
            gl_flags |= BGL_STENCIL;
        if (_this->gl_config.accum_red_size > 0
            || _this->gl_config.accum_green_size > 0
            || _this->gl_config.accum_blue_size > 0
            || _this->gl_config.accum_alpha_size > 0)
            gl_flags |= BGL_ACCUM;

        /* Create the view for this window, using found flags */
        if (SDL_Win->CreateView(flags, gl_flags) < 0) {
            return (NULL);
        }

        current->flags = 0;     /* Clear flags */
        current->w = width;
        current->h = height;
        SDL_Win->SetType(B_TITLED_WINDOW);
        if (flags & SDL_NOFRAME) {
            current->flags |= SDL_NOFRAME;
            SDL_Win->SetLook(B_NO_BORDER_WINDOW_LOOK);
        } else {
            if ((flags & SDL_RESIZABLE) && !(flags & SDL_INTERNALOPENGL)) {
                current->flags |= SDL_RESIZABLE;
                /* We don't want opaque resizing (TM). :-) */
                SDL_Win->SetFlags(B_OUTLINE_RESIZE);
            } else {
                SDL_Win->SetFlags(B_NOT_RESIZABLE | B_NOT_ZOOMABLE);
            }
        }

        if (flags & SDL_INTERNALOPENGL) {
            current->flags |= SDL_INTERNALOPENGL;
            current->pitch = 0;
            current->pixels = NULL;
            _this->UpdateRects = NULL;
        } else {
            /* Create the BBitmap framebuffer */
            bounds.top = 0;
            bounds.left = 0;
            bounds.right = width - 1;
            bounds.bottom = height - 1;
            bbitmap = new BBitmap(bounds, bscreen.ColorSpace());
            if (!bbitmap->IsValid()) {
                SDL_SetError("Couldn't create screen bitmap");
                delete bbitmap;
                return (NULL);
            }
            current->pitch = bbitmap->BytesPerRow();
            current->pixels = (void *) bbitmap->Bits();
            SDL_Win->SetBitmap(bbitmap);
            _this->UpdateRects = BE_NormalUpdate;
        }

        /* Set the correct fullscreen mode */
        BE_SetFullScreen(_this, current, flags & SDL_FULLSCREEN ? 1 : 0);

        /* We're done */
        return (current);
    }

/* Update the current mouse state and position */
    void BE_UpdateMouse(_THIS)
    {
        BPoint point;
        uint32 buttons;

        if (SDL_Win->Lock()) {
            /* Get new input state, if still active */
            if (SDL_Win->IsActive()) {
                (SDL_Win->View())->GetMouse(&point, &buttons, true);
            } else {
                point.x = -1;
                point.y = -1;
            }
            SDL_Win->Unlock();

            if ((point.x >= 0) && (point.x < SDL_VideoSurface->w) &&
                (point.y >= 0) && (point.y < SDL_VideoSurface->h)) {
                SDL_PrivateAppActive(1, SDL_APPMOUSEFOCUS);
                SDL_PrivateMouseMotion(0, 0,
                                       (Sint16) point.x, (Sint16) point.y);
            } else {
                SDL_PrivateAppActive(0, SDL_APPMOUSEFOCUS);
            }
        }
    }

/* We don't actually allow hardware surfaces other than the main one */
    static int BE_AllocHWSurface(_THIS, SDL_Surface * surface)
    {
        return (-1);
    }
    static void BE_FreeHWSurface(_THIS, SDL_Surface * surface)
    {
        return;
    }
    static int BE_LockHWSurface(_THIS, SDL_Surface * surface)
    {
        return (0);
    }
    static void BE_UnlockHWSurface(_THIS, SDL_Surface * surface)
    {
        return;
    }

    static void BE_NormalUpdate(_THIS, int numrects, SDL_Rect * rects)
    {
        if (SDL_Win->BeginDraw()) {
            int i;

            for (i = 0; i < numrects; ++i) {
                BRect rect;

                rect.top = rects[i].y;
                rect.left = rects[i].x;
                rect.bottom = rect.top + rects[i].h - 1;
                rect.right = rect.left + rects[i].w - 1;
                SDL_Win->DrawAsync(rect);
            }
            SDL_Win->EndDraw();
        }
    }

#if SDL_VIDEO_OPENGL
/* Passing a NULL path means load pointers from the application */
    int BE_GL_LoadLibrary(_THIS, const char *path)
    {
        if (path == NULL) {
            if (_this->gl_config.dll_handle == NULL) {
                image_info info;
                int32 cookie = 0;
                while (get_next_image_info(0, &cookie, &info) == B_OK) {
                    void *location = NULL;
                    if (get_image_symbol
                        ((image_id) cookie, "glBegin",
                         B_SYMBOL_TYPE_ANY, &location) == B_OK) {
                        _this->gl_config.dll_handle = (void *) cookie;
                        _this->gl_config.driver_loaded = 1;
                        SDL_strlcpy(_this->gl_config.driver_path,
                                    "libGL.so",
                                    SDL_arraysize(_this->
                                                  gl_config.driver_path));
                    }
                }
            }
        } else {
            /*
               FIXME None of BeOS libGL.so implementations have exported functions 
               to load BGLView, which should be reloaded from new lib.
               So for now just "load" linked libGL.so :(
             */
            if (_this->gl_config.dll_handle == NULL) {
                return BE_GL_LoadLibrary(_this, NULL);
            }

            /* Unload old first */
            /*if (_this->gl_config.dll_handle != NULL) { */
            /* Do not try to unload application itself (if LoadLibrary was called before with NULL ;) */
            /*      image_info info;
               if (get_image_info((image_id)_this->gl_config.dll_handle, &info) == B_OK) {
               if (info.type != B_APP_IMAGE) {
               unload_add_on((image_id)_this->gl_config.dll_handle);
               }
               }

               }

               if ((_this->gl_config.dll_handle = (void*)load_add_on(path)) != (void*)B_ERROR) {
               _this->gl_config.driver_loaded = 1;
               SDL_strlcpy(_this->gl_config.driver_path, path, SDL_arraysize(_this->gl_config.driver_path));
               } */
        }

        if (_this->gl_config.dll_handle != NULL) {
            return 0;
        } else {
            _this->gl_config.dll_handle = NULL;
            _this->gl_config.driver_loaded = 0;
            *_this->gl_config.driver_path = '\0';
            return -1;
        }
    }

    void *BE_GL_GetProcAddress(_THIS, const char *proc)
    {
        if (_this->gl_config.dll_handle != NULL) {
            void *location = NULL;
            status_t err;
            if ((err =
                 get_image_symbol((image_id) _this->gl_config.dll_handle,
                                  proc, B_SYMBOL_TYPE_ANY,
                                  &location)) == B_OK) {
                return location;
            } else {
                SDL_SetError("Couldn't find OpenGL symbol");
                return NULL;
            }
        } else {
            SDL_SetError("OpenGL library not loaded");
            return NULL;
        }
    }

    int BE_GL_GetAttribute(_THIS, SDL_GLattr attrib, int *value)
    {
        /*
           FIXME? Right now BE_GL_GetAttribute shouldn't be called between glBegin() and glEnd() - it doesn't use "cached" values
         */
        switch (attrib) {
        case SDL_GL_RED_SIZE:
            glGetIntegerv(GL_RED_BITS, (GLint *) value);
            break;
        case SDL_GL_GREEN_SIZE:
            glGetIntegerv(GL_GREEN_BITS, (GLint *) value);
            break;
        case SDL_GL_BLUE_SIZE:
            glGetIntegerv(GL_BLUE_BITS, (GLint *) value);
            break;
        case SDL_GL_ALPHA_SIZE:
            glGetIntegerv(GL_ALPHA_BITS, (GLint *) value);
            break;
        case SDL_GL_DOUBLEBUFFER:
            glGetBooleanv(GL_DOUBLEBUFFER, (GLboolean *) value);
            break;
        case SDL_GL_BUFFER_SIZE:
            int v;
            glGetIntegerv(GL_RED_BITS, (GLint *) & v);
            *value = v;
            glGetIntegerv(GL_GREEN_BITS, (GLint *) & v);
            *value += v;
            glGetIntegerv(GL_BLUE_BITS, (GLint *) & v);
            *value += v;
            glGetIntegerv(GL_ALPHA_BITS, (GLint *) & v);
            *value += v;
            break;
        case SDL_GL_DEPTH_SIZE:
            glGetIntegerv(GL_DEPTH_BITS, (GLint *) value);      /* Mesa creates 16 only? r5 always 32 */
            break;
        case SDL_GL_STENCIL_SIZE:
            glGetIntegerv(GL_STENCIL_BITS, (GLint *) value);
            break;
        case SDL_GL_ACCUM_RED_SIZE:
            glGetIntegerv(GL_ACCUM_RED_BITS, (GLint *) value);
            break;
        case SDL_GL_ACCUM_GREEN_SIZE:
            glGetIntegerv(GL_ACCUM_GREEN_BITS, (GLint *) value);
            break;
        case SDL_GL_ACCUM_BLUE_SIZE:
            glGetIntegerv(GL_ACCUM_BLUE_BITS, (GLint *) value);
            break;
        case SDL_GL_ACCUM_ALPHA_SIZE:
            glGetIntegerv(GL_ACCUM_ALPHA_BITS, (GLint *) value);
            break;
        case SDL_GL_STEREO:
        case SDL_GL_MULTISAMPLEBUFFERS:
        case SDL_GL_MULTISAMPLESAMPLES:
        default:
            *value = 0;
            return (-1);
        }
        return 0;
    }

    int BE_GL_MakeCurrent(_THIS)
    {
        /* FIXME: should we glview->unlock and then glview->lock()? */
        return 0;
    }

    void BE_GL_SwapBuffers(_THIS)
    {
        SDL_Win->SwapBuffers();
    }
#endif

/* Is the system palette settable? */
    int BE_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color * colors)
    {
        int i;
        SDL_Palette *palette;
        const color_map *cmap = BScreen().ColorMap();

        /* Get the screen colormap */
        palette = _this->screen->format->palette;
        for (i = 0; i < 256; ++i) {
            palette->colors[i].r = cmap->color_list[i].red;
            palette->colors[i].g = cmap->color_list[i].green;
            palette->colors[i].b = cmap->color_list[i].blue;
        }
        return (0);
    }

    void BE_VideoQuit(_THIS)
    {
        int i, j;

        SDL_Win->Quit();
        SDL_Win = NULL;

        if (SDL_BlankCursor != NULL) {
            BE_FreeWMCursor(_this, SDL_BlankCursor);
            SDL_BlankCursor = NULL;
        }
        for (i = 0; i < NUM_MODELISTS; ++i) {
            if (SDL_modelist[i]) {
                for (j = 0; SDL_modelist[i][j]; ++j) {
                    SDL_free(SDL_modelist[i][j]);
                }
                SDL_free(SDL_modelist[i]);
                SDL_modelist[i] = NULL;
            }
        }
        /* Restore the original video mode */
        if (_this->screen) {
            if ((_this->screen->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN) {
                BScreen bscreen;
                bscreen.SetMode(&saved_mode);
            }
            _this->screen->pixels = NULL;
        }
#if SDL_VIDEO_OPENGL
        if (_this->gl_config.dll_handle != NULL)
            unload_add_on((image_id) _this->gl_config.dll_handle);
#endif

        SDL_QuitBeApp();
    }

};                              /* Extern C */

/* vi: set ts=4 sw=4 expandtab: */