src/video/haiku/SDL_BWin.h
author Sam Lantinga <slouken@libsdl.org>
Fri, 09 Dec 2016 01:47:43 -0800
changeset 10690 23a825f341e6
parent 10555 476a717c10cf
child 10737 3406a0f8b041
permissions -rw-r--r--
Fixed bug 3513 - SDL_GL_SwapWindow does not return error status

Return an error code from SDL_GL_SwapWindow(), like the other SDL APIs.
slouken@0
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
slouken@0
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@0
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@0
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@0
    20
*/
slouken@0
    21
slouken@0
    22
#ifndef _SDL_BWin_h
slouken@0
    23
#define _SDL_BWin_h
slouken@0
    24
nathanheisey@5916
    25
#ifdef __cplusplus
nathanheisey@5916
    26
extern "C" {
nathanheisey@5916
    27
#endif
nathanheisey@5916
    28
icculus@8093
    29
#include "../../SDL_internal.h"
nathanheisey@5916
    30
#include "SDL.h"
nathanheisey@5916
    31
#include "SDL_syswm.h"
nathanheisey@5931
    32
#include "SDL_bframebuffer.h"
nathanheisey@5916
    33
nathanheisey@5916
    34
#ifdef __cplusplus
nathanheisey@5916
    35
}
nathanheisey@5916
    36
#endif
slouken@1367
    37
slouken@0
    38
#include <stdio.h>
slouken@0
    39
#include <AppKit.h>
slouken@0
    40
#include <InterfaceKit.h>
slouken@0
    41
#include <be/game/DirectWindow.h>
slouken@1361
    42
#if SDL_VIDEO_OPENGL
slouken@0
    43
#include <be/opengl/GLView.h>
slouken@0
    44
#endif
slouken@0
    45
#include "SDL_events.h"
icculus@7981
    46
#include "../../main/haiku/SDL_BApp.h"
slouken@0
    47
nathanheisey@5931
    48
nathanheisey@5916
    49
enum WinCommands {
slouken@7191
    50
    BWIN_MOVE_WINDOW,
slouken@7191
    51
    BWIN_RESIZE_WINDOW,
slouken@7191
    52
    BWIN_SHOW_WINDOW,
slouken@7191
    53
    BWIN_HIDE_WINDOW,
slouken@7191
    54
    BWIN_MAXIMIZE_WINDOW,
slouken@7191
    55
    BWIN_MINIMIZE_WINDOW,
slouken@7191
    56
    BWIN_RESTORE_WINDOW,
slouken@7191
    57
    BWIN_SET_TITLE,
slouken@7191
    58
    BWIN_SET_BORDERED,
icculus@10385
    59
    BWIN_SET_RESIZABLE,
slouken@7191
    60
    BWIN_FULLSCREEN
slouken@0
    61
};
slouken@0
    62
nathanheisey@5916
    63
slouken@1895
    64
class SDL_BWin:public BDirectWindow
slouken@0
    65
{
slouken@1895
    66
  public:
slouken@7191
    67
    /* Constructor/Destructor */
icculus@6421
    68
    SDL_BWin(BRect bounds, window_look look, uint32 flags)
icculus@6421
    69
        : BDirectWindow(bounds, "Untitled", look, B_NORMAL_WINDOW_FEEL, flags)
slouken@1895
    70
    {
nathanheisey@5925
    71
        _last_buttons = 0;
nathanheisey@5923
    72
slouken@1361
    73
#if SDL_VIDEO_OPENGL
nathanheisey@5925
    74
        _SDL_GLView = NULL;
slouken@0
    75
#endif
nathanheisey@5916
    76
        _shown = false;
nathanheisey@5925
    77
        _inhibit_resize = false;
nathanheisey@5925
    78
        _mouse_focused = false;
nathanheisey@5925
    79
        _prev_frame = NULL;
nathanheisey@5925
    80
nathanheisey@5922
    81
        /* Handle framebuffer stuff */
nathanheisey@5925
    82
        _connected = _connection_disabled = false;
nathanheisey@5929
    83
        _buffer_created = _buffer_dirty = false;
nathanheisey@5930
    84
        _trash_window_buffer = false;
nathanheisey@5925
    85
        _buffer_locker = new BLocker();
nathanheisey@5933
    86
        _bitmap = NULL;
icculus@7984
    87
        _clips = NULL;
icculus@7984
    88
nathanheisey@5936
    89
#ifdef DRAWTHREAD
nathanheisey@5929
    90
        _draw_thread_id = spawn_thread(BE_DrawThread, "drawing_thread",
slouken@7191
    91
                            B_NORMAL_PRIORITY, (void*) this);
nathanheisey@5929
    92
        resume_thread(_draw_thread_id);
nathanheisey@5936
    93
#endif
slouken@1895
    94
    }
slouken@907
    95
slouken@1895
    96
    virtual ~ SDL_BWin()
slouken@1895
    97
    {
slouken@1895
    98
        Lock();
nathanheisey@5925
    99
        _connection_disabled = true;
nathanheisey@5929
   100
        int32 result;
slouken@7191
   101
slouken@1361
   102
#if SDL_VIDEO_OPENGL
nathanheisey@5925
   103
        if (_SDL_GLView) {
nathanheisey@5925
   104
            _SDL_GLView->UnlockGL();
slouken@7191
   105
            RemoveChild(_SDL_GLView);   /* Why was this outside the if
slouken@7191
   106
                                            statement before? */
slouken@1895
   107
        }
slouken@7191
   108
slouken@7191
   109
#endif
slouken@1895
   110
        Unlock();
slouken@1361
   111
#if SDL_VIDEO_OPENGL
nathanheisey@5925
   112
        if (_SDL_GLView) {
nathanheisey@5925
   113
            delete _SDL_GLView;
slouken@1895
   114
        }
slouken@0
   115
#endif
slouken@7191
   116
nathanheisey@5922
   117
        /* Clean up framebuffer stuff */
nathanheisey@5925
   118
        _buffer_locker->Lock();
nathanheisey@5936
   119
#ifdef DRAWTHREAD
nathanheisey@5929
   120
        wait_for_thread(_draw_thread_id, &result);
nathanheisey@5936
   121
#endif
nathanheisey@5923
   122
        free(_clips);
nathanheisey@5925
   123
        delete _buffer_locker;
slouken@1895
   124
    }
slouken@7191
   125
nathanheisey@5923
   126
nathanheisey@5931
   127
    /* * * * * OpenGL functionality * * * * */
nathanheisey@5923
   128
#if SDL_VIDEO_OPENGL
nathanheisey@5931
   129
    virtual BGLView *CreateGLView(Uint32 gl_flags) {
slouken@1895
   130
        Lock();
nathanheisey@5931
   131
        if (_SDL_GLView == NULL) {
nathanheisey@5931
   132
            _SDL_GLView = new BGLView(Bounds(), "SDL GLView",
nathanheisey@5931
   133
                                     B_FOLLOW_ALL_SIDES,
nathanheisey@5931
   134
                                     (B_WILL_DRAW | B_FRAME_EVENTS),
nathanheisey@5931
   135
                                     gl_flags);
slouken@1895
   136
        }
nathanheisey@5931
   137
        AddChild(_SDL_GLView);
nathanheisey@5937
   138
        _SDL_GLView->EnableDirectMode(true);
slouken@7191
   139
        _SDL_GLView->LockGL();  /* "New" GLViews are created */
slouken@1895
   140
        Unlock();
nathanheisey@5931
   141
        return (_SDL_GLView);
slouken@1895
   142
    }
slouken@7191
   143
nathanheisey@5931
   144
    virtual void RemoveGLView() {
slouken@7191
   145
        Lock();
slouken@7191
   146
        if(_SDL_GLView) {
slouken@7191
   147
            _SDL_GLView->UnlockGL();
slouken@7191
   148
            RemoveChild(_SDL_GLView);
slouken@7191
   149
        }
slouken@7191
   150
        Unlock();
nathanheisey@5931
   151
    }
slouken@7191
   152
nathanheisey@5931
   153
    virtual void SwapBuffers(void) {
slouken@7191
   154
        _SDL_GLView->UnlockGL();
slouken@7191
   155
        _SDL_GLView->LockGL();
slouken@7191
   156
        _SDL_GLView->SwapBuffers();
slouken@7191
   157
    }
nathanheisey@5923
   158
#endif
slouken@7191
   159
nathanheisey@5921
   160
    /* * * * * Framebuffering* * * * */
nathanheisey@5921
   161
    virtual void DirectConnected(direct_buffer_info *info) {
slouken@7191
   162
        if(!_connected && _connection_disabled) {
slouken@7191
   163
            return;
slouken@7191
   164
        }
nathanheisey@5930
   165
slouken@7191
   166
        /* Determine if the pixel buffer is usable after this update */
slouken@7191
   167
        _trash_window_buffer =      _trash_window_buffer
slouken@7191
   168
                                || ((info->buffer_state & B_BUFFER_RESIZED)
slouken@7191
   169
                                || (info->buffer_state & B_BUFFER_RESET)
slouken@7191
   170
                                || (info->driver_state == B_MODE_CHANGED));
slouken@7191
   171
        LockBuffer();
nathanheisey@5930
   172
slouken@7191
   173
        switch(info->buffer_state & B_DIRECT_MODE_MASK) {
slouken@7191
   174
        case B_DIRECT_START:
slouken@7191
   175
            _connected = true;
nathanheisey@5922
   176
slouken@7191
   177
        case B_DIRECT_MODIFY:
slouken@7191
   178
            if(_clips) {
slouken@7191
   179
                free(_clips);
slouken@7191
   180
                _clips = NULL;
slouken@7191
   181
            }
nathanheisey@5922
   182
slouken@7191
   183
            _num_clips = info->clip_list_count;
slouken@7191
   184
            _clips = (clipping_rect *)malloc(_num_clips*sizeof(clipping_rect));
slouken@7191
   185
            if(_clips) {
slouken@7191
   186
                memcpy(_clips, info->clip_list,
slouken@7191
   187
                    _num_clips*sizeof(clipping_rect));
slouken@7191
   188
slouken@7191
   189
                _bits = (uint8*) info->bits;
slouken@7191
   190
                _row_bytes = info->bytes_per_row;
slouken@7191
   191
                _bounds = info->window_bounds;
slouken@7191
   192
                _bytes_per_px = info->bits_per_pixel / 8;
slouken@7191
   193
                _buffer_dirty = true;
slouken@7191
   194
            }
slouken@7191
   195
            break;
slouken@7191
   196
slouken@7191
   197
        case B_DIRECT_STOP:
slouken@7191
   198
            _connected = false;
slouken@7191
   199
            break;
slouken@7191
   200
        }
nathanheisey@5931
   201
#if SDL_VIDEO_OPENGL
slouken@7191
   202
        if(_SDL_GLView) {
slouken@7191
   203
            _SDL_GLView->DirectConnected(info);
slouken@7191
   204
        }
nathanheisey@5931
   205
#endif
slouken@7191
   206
slouken@7191
   207
slouken@7191
   208
        /* Call the base object directconnected */
slouken@7191
   209
        BDirectWindow::DirectConnected(info);
slouken@7191
   210
slouken@7191
   211
        UnlockBuffer();
slouken@7191
   212
nathanheisey@5921
   213
    }
slouken@7191
   214
slouken@7191
   215
slouken@7191
   216
slouken@7191
   217
nathanheisey@5916
   218
    /* * * * * Event sending * * * * */
nathanheisey@5916
   219
    /* Hook functions */
nathanheisey@5916
   220
    virtual void FrameMoved(BPoint origin) {
slouken@7191
   221
        /* Post a message to the BApp so that it can handle the window event */
slouken@7191
   222
        BMessage msg(BAPP_WINDOW_MOVED);
slouken@7191
   223
        msg.AddInt32("window-x", (int)origin.x);
slouken@7191
   224
        msg.AddInt32("window-y", (int)origin.y);
slouken@7191
   225
        _PostWindowEvent(msg);
nathanheisey@5939
   226
slouken@7191
   227
        /* Perform normal hook operations */
slouken@7191
   228
        BDirectWindow::FrameMoved(origin);
nathanheisey@5916
   229
    }
nathanheisey@5916
   230
slouken@7191
   231
    virtual void FrameResized(float width, float height) {
slouken@7191
   232
        /* Post a message to the BApp so that it can handle the window event */
slouken@7191
   233
        BMessage msg(BAPP_WINDOW_RESIZED);
nathanheisey@5925
   234
slouken@7191
   235
        msg.AddInt32("window-w", (int)width + 1);
slouken@7191
   236
        msg.AddInt32("window-h", (int)height + 1);
slouken@7191
   237
        _PostWindowEvent(msg);
slouken@7191
   238
slouken@7191
   239
        /* Perform normal hook operations */
slouken@7191
   240
        BDirectWindow::FrameResized(width, height);
nathanheisey@5916
   241
    }
slouken@7191
   242
slouken@7191
   243
    virtual bool QuitRequested() {
slouken@7191
   244
        BMessage msg(BAPP_WINDOW_CLOSE_REQUESTED);
slouken@7191
   245
        _PostWindowEvent(msg);
slouken@7191
   246
slouken@7191
   247
        /* We won't allow a quit unless asked by DestroyWindow() */
slouken@7191
   248
        return false;
slouken@7191
   249
    }
slouken@7191
   250
nathanheisey@5916
   251
    virtual void WindowActivated(bool active) {
slouken@7191
   252
        BMessage msg(BAPP_KEYBOARD_FOCUS);  /* Mouse focus sold separately */
slouken@7191
   253
        _PostWindowEvent(msg);
nathanheisey@5916
   254
    }
slouken@7191
   255
nathanheisey@5916
   256
    virtual void Zoom(BPoint origin,
slouken@7191
   257
                float width,
slouken@7191
   258
                float height) {
slouken@7191
   259
        BMessage msg(BAPP_MAXIMIZE);    /* Closest thing to maximization Haiku has */
slouken@7191
   260
        _PostWindowEvent(msg);
nathanheisey@5916
   261
slouken@7191
   262
        /* Before the window zooms, record its size */
slouken@7191
   263
        if( !_prev_frame )
slouken@7191
   264
            _prev_frame = new BRect(Frame());
slouken@7191
   265
slouken@7191
   266
        /* Perform normal hook operations */
slouken@7191
   267
        BDirectWindow::Zoom(origin, width, height);
nathanheisey@5916
   268
    }
slouken@7191
   269
nathanheisey@5916
   270
    /* Member functions */
nathanheisey@5916
   271
    virtual void Show() {
slouken@7191
   272
        while(IsHidden()) {
slouken@7191
   273
            BDirectWindow::Show();
slouken@7191
   274
        }
slouken@7191
   275
        _shown = true;
nathanheisey@5925
   276
slouken@7191
   277
        BMessage msg(BAPP_SHOW);
slouken@7191
   278
        _PostWindowEvent(msg);
nathanheisey@5916
   279
    }
slouken@7191
   280
nathanheisey@5916
   281
    virtual void Hide() {
slouken@7191
   282
        BDirectWindow::Hide();
slouken@7191
   283
        _shown = false;
nathanheisey@5925
   284
slouken@7191
   285
        BMessage msg(BAPP_HIDE);
slouken@7191
   286
        _PostWindowEvent(msg);
nathanheisey@5916
   287
    }
nathanheisey@5916
   288
nathanheisey@5916
   289
    virtual void Minimize(bool minimize) {
slouken@7191
   290
        BDirectWindow::Minimize(minimize);
slouken@7191
   291
        int32 minState = (minimize ? BAPP_MINIMIZE : BAPP_RESTORE);
slouken@7191
   292
slouken@7191
   293
        BMessage msg(minState);
slouken@7191
   294
        _PostWindowEvent(msg);
nathanheisey@5916
   295
    }
slouken@7191
   296
slouken@7191
   297
nathanheisey@5916
   298
    /* BView message interruption */
nathanheisey@5916
   299
    virtual void DispatchMessage(BMessage * msg, BHandler * target)
slouken@1895
   300
    {
slouken@7191
   301
        BPoint where;   /* Used by mouse moved */
slouken@7191
   302
        int32 buttons;  /* Used for mouse button events */
slouken@7191
   303
        int32 key;      /* Used for key events */
slouken@7191
   304
nathanheisey@5916
   305
        switch (msg->what) {
nathanheisey@5916
   306
        case B_MOUSE_MOVED:
nathanheisey@5916
   307
            int32 transit;
nathanheisey@5916
   308
            if (msg->FindPoint("where", &where) == B_OK
nathanheisey@5916
   309
                && msg->FindInt32("be:transit", &transit) == B_OK) {
slouken@7191
   310
                _MouseMotionEvent(where, transit);
nathanheisey@5916
   311
            }
slouken@7191
   312
nathanheisey@5916
   313
            /* FIXME: Apparently a button press/release event might be dropped
nathanheisey@5916
   314
               if made before before a different button is released.  Does
nathanheisey@5916
   315
               B_MOUSE_MOVED have the data needed to check if a mouse button
nathanheisey@5916
   316
               state has changed? */
nathanheisey@5916
   317
            if (msg->FindInt32("buttons", &buttons) == B_OK) {
slouken@7191
   318
                _MouseButtonEvent(buttons);
nathanheisey@5916
   319
            }
nathanheisey@5916
   320
            break;
nathanheisey@5916
   321
nathanheisey@5916
   322
        case B_MOUSE_DOWN:
nathanheisey@5916
   323
        case B_MOUSE_UP:
nathanheisey@5916
   324
            /* _MouseButtonEvent() detects any and all buttons that may have
nathanheisey@5916
   325
               changed state, as well as that button's new state */
nathanheisey@5916
   326
            if (msg->FindInt32("buttons", &buttons) == B_OK) {
slouken@7191
   327
                _MouseButtonEvent(buttons);
nathanheisey@5916
   328
            }
nathanheisey@5916
   329
            break;
nathanheisey@5916
   330
nathanheisey@5916
   331
        case B_MOUSE_WHEEL_CHANGED:
slouken@7191
   332
            float x, y;
slouken@7191
   333
            if (msg->FindFloat("be:wheel_delta_x", &x) == B_OK
slouken@7191
   334
                && msg->FindFloat("be:wheel_delta_y", &y) == B_OK) {
slouken@7191
   335
                    _MouseWheelEvent((int)x, (int)y);
slouken@7191
   336
            }
slouken@7191
   337
            break;
nathanheisey@5916
   338
nathanheisey@5916
   339
        case B_KEY_DOWN:
slouken@10555
   340
            {
slouken@10555
   341
                int32 i = 0;
slouken@10555
   342
                int8 byte;
slouken@10555
   343
                int8 bytes[4] = { 0, 0, 0, 0 };
slouken@10555
   344
                while (i < 4 && msg->FindInt8("byte", i, &byte) == B_OK) {
slouken@10555
   345
                    bytes[i] = byte;
slouken@10555
   346
                    i++;
slouken@10555
   347
                }
slouken@10555
   348
                if (msg->FindInt32("key", &key) == B_OK) {
slouken@10555
   349
                    _KeyEvent((SDL_Scancode)key, &bytes[0], i, SDL_PRESSED);
slouken@10555
   350
                }
slouken@10555
   351
            }
slouken@10555
   352
            break;
slouken@10555
   353
            
nathanheisey@5916
   354
        case B_UNMAPPED_KEY_DOWN:      /* modifier keys are unmapped */
slouken@7191
   355
            if (msg->FindInt32("key", &key) == B_OK) {
slouken@10555
   356
                _KeyEvent((SDL_Scancode)key, NULL, 0, SDL_PRESSED);
slouken@7191
   357
            }
slouken@7191
   358
            break;
nathanheisey@5916
   359
nathanheisey@5916
   360
        case B_KEY_UP:
nathanheisey@5916
   361
        case B_UNMAPPED_KEY_UP:        /* modifier keys are unmapped */
slouken@7191
   362
            if (msg->FindInt32("key", &key) == B_OK) {
slouken@10555
   363
                _KeyEvent(key, NULL, 0, SDL_RELEASED);
nathanheisey@5916
   364
            }
nathanheisey@5916
   365
            break;
slouken@7191
   366
nathanheisey@5916
   367
        default:
nathanheisey@5916
   368
            /* move it after switch{} so it's always handled
icculus@7981
   369
               that way we keep Haiku features like:
nathanheisey@5916
   370
               - CTRL+Q to close window (and other shortcuts)
nathanheisey@5916
   371
               - PrintScreen to make screenshot into /boot/home
nathanheisey@5916
   372
               - etc.. */
gabomdq@7678
   373
            /* BDirectWindow::DispatchMessage(msg, target); */
nathanheisey@5916
   374
            break;
nathanheisey@5916
   375
        }
nathanheisey@5925
   376
nathanheisey@5916
   377
        BDirectWindow::DispatchMessage(msg, target);
slouken@1895
   378
    }
slouken@7191
   379
nathanheisey@5916
   380
    /* Handle command messages */
nathanheisey@5916
   381
    virtual void MessageReceived(BMessage* message) {
slouken@7191
   382
        switch (message->what) {
slouken@7191
   383
            /* Handle commands from SDL */
slouken@7191
   384
            case BWIN_SET_TITLE:
slouken@7191
   385
                _SetTitle(message);
slouken@7191
   386
                break;
slouken@7191
   387
            case BWIN_MOVE_WINDOW:
slouken@7191
   388
                _MoveTo(message);
slouken@7191
   389
                break;
slouken@7191
   390
            case BWIN_RESIZE_WINDOW:
slouken@7191
   391
                _ResizeTo(message);
slouken@7191
   392
                break;
slouken@7191
   393
            case BWIN_SET_BORDERED:
slouken@7191
   394
                _SetBordered(message);
slouken@7191
   395
                break;
icculus@10385
   396
            case BWIN_SET_RESIZABLE:
icculus@10385
   397
                _SetResizable(message);
icculus@10385
   398
                break;
slouken@7191
   399
            case BWIN_SHOW_WINDOW:
slouken@7191
   400
                Show();
slouken@7191
   401
                break;
slouken@7191
   402
            case BWIN_HIDE_WINDOW:
slouken@7191
   403
                Hide();
slouken@7191
   404
                break;
slouken@7191
   405
            case BWIN_MAXIMIZE_WINDOW:
slouken@7191
   406
                BWindow::Zoom();
slouken@7191
   407
                break;
slouken@7191
   408
            case BWIN_MINIMIZE_WINDOW:
slouken@7191
   409
                Minimize(true);
slouken@7191
   410
                break;
slouken@7191
   411
            case BWIN_RESTORE_WINDOW:
slouken@7191
   412
                _Restore();
slouken@7191
   413
                break;
slouken@7191
   414
            case BWIN_FULLSCREEN:
slouken@7191
   415
                _SetFullScreen(message);
slouken@7191
   416
                break;
slouken@7191
   417
            default:
slouken@7191
   418
                /* Perform normal message handling */
slouken@7191
   419
                BDirectWindow::MessageReceived(message);
slouken@7191
   420
                break;
slouken@7191
   421
        }
nathanheisey@5916
   422
nathanheisey@5916
   423
    }
nathanheisey@5916
   424
slouken@7191
   425
slouken@7191
   426
slouken@7191
   427
    /* Accessor methods */
slouken@7191
   428
    bool IsShown() { return _shown; }
slouken@7191
   429
    int32 GetID() { return _id; }
slouken@7191
   430
    uint32 GetRowBytes() { return _row_bytes; }
slouken@7191
   431
    int32 GetFbX() { return _bounds.left; }
slouken@7191
   432
    int32 GetFbY() { return _bounds.top; }
slouken@7191
   433
    bool ConnectionEnabled() { return !_connection_disabled; }
slouken@7191
   434
    bool Connected() { return _connected; }
slouken@7191
   435
    clipping_rect *GetClips() { return _clips; }
slouken@7191
   436
    int32 GetNumClips() { return _num_clips; }
slouken@7191
   437
    uint8* GetBufferPx() { return _bits; }
slouken@7191
   438
    int32 GetBytesPerPx() { return _bytes_per_px; }
slouken@7191
   439
    bool CanTrashWindowBuffer() { return _trash_window_buffer; }
slouken@7191
   440
    bool BufferExists() { return _buffer_created; }
slouken@7191
   441
    bool BufferIsDirty() { return _buffer_dirty; }
slouken@7191
   442
    BBitmap *GetBitmap() { return _bitmap; }
nathanheisey@5934
   443
#if SDL_VIDEO_OPENGL
slouken@7191
   444
    BGLView *GetGLView() { return _SDL_GLView; }
nathanheisey@5934
   445
#endif
slouken@7191
   446
slouken@7191
   447
    /* Setter methods */
slouken@7191
   448
    void SetID(int32 id) { _id = id; }
slouken@7191
   449
    void SetBufferExists(bool bufferExists) { _buffer_created = bufferExists; }
slouken@7191
   450
    void LockBuffer() { _buffer_locker->Lock(); }
slouken@7191
   451
    void UnlockBuffer() { _buffer_locker->Unlock(); }
slouken@7191
   452
    void SetBufferDirty(bool bufferDirty) { _buffer_dirty = bufferDirty; }
slouken@7191
   453
    void SetTrashBuffer(bool trash) { _trash_window_buffer = trash;     }
slouken@7191
   454
    void SetBitmap(BBitmap *bitmap) { _bitmap = bitmap; }
slouken@7191
   455
slouken@7191
   456
nathanheisey@5916
   457
private:
slouken@7191
   458
    /* Event redirection */
nathanheisey@5916
   459
    void _MouseMotionEvent(BPoint &where, int32 transit) {
slouken@7191
   460
        if(transit == B_EXITED_VIEW) {
slouken@7191
   461
            /* Change mouse focus */
slouken@7191
   462
            if(_mouse_focused) {
slouken@7191
   463
                _MouseFocusEvent(false);
slouken@7191
   464
            }
slouken@7191
   465
        } else {
slouken@7191
   466
            /* Change mouse focus */
slouken@7191
   467
            if (!_mouse_focused) {
slouken@7191
   468
                _MouseFocusEvent(true);
slouken@7191
   469
            }
slouken@7191
   470
            BMessage msg(BAPP_MOUSE_MOVED);
slouken@7191
   471
            msg.AddInt32("x", (int)where.x);
slouken@7191
   472
            msg.AddInt32("y", (int)where.y);
slouken@7191
   473
slouken@7191
   474
            _PostWindowEvent(msg);
slouken@7191
   475
        }
slouken@1895
   476
    }
slouken@7191
   477
nathanheisey@5916
   478
    void _MouseFocusEvent(bool focusGained) {
slouken@7191
   479
        _mouse_focused = focusGained;
slouken@7191
   480
        BMessage msg(BAPP_MOUSE_FOCUS);
slouken@7191
   481
        msg.AddBool("focusGained", focusGained);
slouken@7191
   482
        _PostWindowEvent(msg);
slouken@7191
   483
gabomdq@7678
   484
/* FIXME: Why were these here?
gabomdq@7663
   485
 if false: be_app->SetCursor(B_HAND_CURSOR);
gabomdq@7677
   486
 if true:  SDL_SetCursor(NULL); */
slouken@1895
   487
    }
slouken@7191
   488
nathanheisey@5916
   489
    void _MouseButtonEvent(int32 buttons) {
slouken@7191
   490
        int32 buttonStateChange = buttons ^ _last_buttons;
slouken@7191
   491
slouken@7191
   492
        /* Make sure at least one button has changed state */
slouken@7191
   493
        if( !(buttonStateChange) ) {
slouken@7191
   494
            return;
slouken@7191
   495
        }
slouken@7191
   496
slouken@7191
   497
        /* Add any mouse button events */
slouken@7191
   498
        if(buttonStateChange & B_PRIMARY_MOUSE_BUTTON) {
slouken@7191
   499
            _SendMouseButton(SDL_BUTTON_LEFT, buttons &
slouken@7191
   500
                B_PRIMARY_MOUSE_BUTTON);
slouken@7191
   501
        }
slouken@7191
   502
        if(buttonStateChange & B_SECONDARY_MOUSE_BUTTON) {
slouken@7191
   503
            _SendMouseButton(SDL_BUTTON_RIGHT, buttons &
slouken@7191
   504
                B_PRIMARY_MOUSE_BUTTON);
slouken@7191
   505
        }
slouken@7191
   506
        if(buttonStateChange & B_TERTIARY_MOUSE_BUTTON) {
slouken@7191
   507
            _SendMouseButton(SDL_BUTTON_MIDDLE, buttons &
slouken@7191
   508
                B_PRIMARY_MOUSE_BUTTON);
slouken@7191
   509
        }
slouken@7191
   510
slouken@7191
   511
        _last_buttons = buttons;
slouken@1895
   512
    }
slouken@7191
   513
nathanheisey@5916
   514
    void _SendMouseButton(int32 button, int32 state) {
slouken@7191
   515
        BMessage msg(BAPP_MOUSE_BUTTON);
slouken@7191
   516
        msg.AddInt32("button-id", button);
slouken@7191
   517
        msg.AddInt32("button-state", state);
slouken@7191
   518
        _PostWindowEvent(msg);
nathanheisey@5916
   519
    }
slouken@7191
   520
nathanheisey@5916
   521
    void _MouseWheelEvent(int32 x, int32 y) {
slouken@7191
   522
        /* Create a message to pass along to the BeApp thread */
slouken@7191
   523
        BMessage msg(BAPP_MOUSE_WHEEL);
slouken@7191
   524
        msg.AddInt32("xticks", x);
slouken@7191
   525
        msg.AddInt32("yticks", y);
slouken@7191
   526
        _PostWindowEvent(msg);
nathanheisey@5916
   527
    }
slouken@7191
   528
slouken@10555
   529
    void _KeyEvent(int32 keyCode, const int8 *keyUtf8, const ssize_t & len, int32 keyState) {
slouken@7191
   530
        /* Create a message to pass along to the BeApp thread */
slouken@7191
   531
        BMessage msg(BAPP_KEY);
slouken@7191
   532
        msg.AddInt32("key-state", keyState);
slouken@7191
   533
        msg.AddInt32("key-scancode", keyCode);
slouken@10555
   534
        if (keyUtf8 != NULL) {
slouken@10555
   535
        	msg.AddData("key-utf8", B_INT8_TYPE, (const void*)keyUtf8, len);
slouken@10555
   536
        }
slouken@7191
   537
        be_app->PostMessage(&msg);
nathanheisey@5916
   538
    }
slouken@7191
   539
nathanheisey@5916
   540
    void _RepaintEvent() {
slouken@7191
   541
        /* Force a repaint: Call the SDL exposed event */
slouken@7191
   542
        BMessage msg(BAPP_REPAINT);
slouken@7191
   543
        _PostWindowEvent(msg);
nathanheisey@5916
   544
    }
nathanheisey@5916
   545
    void _PostWindowEvent(BMessage &msg) {
slouken@7191
   546
        msg.AddInt32("window-id", _id);
slouken@7191
   547
        be_app->PostMessage(&msg);
nathanheisey@5916
   548
    }
slouken@7191
   549
slouken@7191
   550
    /* Command methods (functions called upon by SDL) */
nathanheisey@5916
   551
    void _SetTitle(BMessage *msg) {
slouken@7191
   552
        const char *title;
slouken@7191
   553
        if(
slouken@7191
   554
            msg->FindString("window-title", &title) != B_OK
slouken@7191
   555
        ) {
slouken@7191
   556
            return;
slouken@7191
   557
        }
slouken@7191
   558
        SetTitle(title);
nathanheisey@5916
   559
    }
slouken@7191
   560
nathanheisey@5916
   561
    void _MoveTo(BMessage *msg) {
slouken@7191
   562
        int32 x, y;
slouken@7191
   563
        if(
slouken@7191
   564
            msg->FindInt32("window-x", &x) != B_OK ||
slouken@7191
   565
            msg->FindInt32("window-y", &y) != B_OK
slouken@7191
   566
        ) {
slouken@7191
   567
            return;
slouken@7191
   568
        }
slouken@7191
   569
        MoveTo(x, y);
nathanheisey@5916
   570
    }
slouken@7191
   571
nathanheisey@5916
   572
    void _ResizeTo(BMessage *msg) {
slouken@7191
   573
        int32 w, h;
slouken@7191
   574
        if(
slouken@7191
   575
            msg->FindInt32("window-w", &w) != B_OK ||
slouken@7191
   576
            msg->FindInt32("window-h", &h) != B_OK
slouken@7191
   577
        ) {
slouken@7191
   578
            return;
slouken@7191
   579
        }
slouken@7191
   580
        ResizeTo(w, h);
nathanheisey@5916
   581
    }
icculus@6422
   582
icculus@6422
   583
    void _SetBordered(BMessage *msg) {
slouken@7191
   584
        bool bEnabled;
slouken@7191
   585
        if(msg->FindBool("window-border", &bEnabled) != B_OK) {
slouken@7191
   586
            return;
slouken@7191
   587
        }
slouken@7191
   588
        SetLook(bEnabled ? B_BORDERED_WINDOW_LOOK : B_NO_BORDER_WINDOW_LOOK);
icculus@6422
   589
    }
icculus@6422
   590
icculus@10385
   591
    void _SetResizable(BMessage *msg) {
icculus@10385
   592
        bool bEnabled;
icculus@10385
   593
        if(msg->FindBool("window-resizable", &bEnabled) != B_OK) {
icculus@10385
   594
            return;
icculus@10385
   595
        }
icculus@10385
   596
        if (bEnabled) {
icculus@10388
   597
            SetFlags(Flags() & ~(B_NOT_RESIZABLE | B_NOT_ZOOMABLE));
icculus@10385
   598
        } else {
icculus@10388
   599
            SetFlags(Flags() | (B_NOT_RESIZABLE | B_NOT_ZOOMABLE));
icculus@10385
   600
        }
icculus@10385
   601
    }
icculus@10385
   602
nathanheisey@5916
   603
    void _Restore() {
slouken@7191
   604
        if(IsMinimized()) {
slouken@7191
   605
            Minimize(false);
slouken@7191
   606
        } else if(IsHidden()) {
slouken@7191
   607
            Show();
slouken@7191
   608
        } else if(_prev_frame != NULL) {    /* Zoomed */
slouken@7191
   609
            MoveTo(_prev_frame->left, _prev_frame->top);
slouken@7191
   610
            ResizeTo(_prev_frame->Width(), _prev_frame->Height());
slouken@7191
   611
        }
slouken@1895
   612
    }
slouken@0
   613
nathanheisey@5916
   614
    void _SetFullScreen(BMessage *msg) {
slouken@7191
   615
        bool fullscreen;
slouken@7191
   616
        if(
slouken@7191
   617
            msg->FindBool("fullscreen", &fullscreen) != B_OK
slouken@7191
   618
        ) {
slouken@7191
   619
            return;
slouken@7191
   620
        }
slouken@7191
   621
        SetFullScreen(fullscreen);
slouken@1895
   622
    }
slouken@7191
   623
nathanheisey@5916
   624
    /* Members */
slouken@1361
   625
#if SDL_VIDEO_OPENGL
nathanheisey@5925
   626
    BGLView * _SDL_GLView;
slouken@0
   627
#endif
slouken@7191
   628
nathanheisey@5925
   629
    int32 _last_buttons;
slouken@7191
   630
    int32 _id;  /* Window id used by SDL_BApp */
slouken@7191
   631
    bool  _mouse_focused;       /* Does this window have mouse focus? */
nathanheisey@5925
   632
    bool  _shown;
nathanheisey@5925
   633
    bool  _inhibit_resize;
slouken@7191
   634
slouken@7191
   635
    BRect *_prev_frame; /* Previous position and size of the window */
slouken@7191
   636
nathanheisey@5922
   637
    /* Framebuffer members */
slouken@7191
   638
    bool            _connected,
slouken@7191
   639
                    _connection_disabled,
slouken@7191
   640
                    _buffer_created,
slouken@7191
   641
                    _buffer_dirty,
slouken@7191
   642
                    _trash_window_buffer;
slouken@7191
   643
    uint8          *_bits;
slouken@7191
   644
    uint32          _row_bytes;
slouken@7191
   645
    clipping_rect   _bounds;
slouken@7191
   646
    BLocker        *_buffer_locker;
nathanheisey@5925
   647
    clipping_rect  *_clips;
slouken@7191
   648
    int32           _num_clips;
slouken@7191
   649
    int32           _bytes_per_px;
slouken@7191
   650
    thread_id       _draw_thread_id;
slouken@7191
   651
slouken@7191
   652
    BBitmap        *_bitmap;
slouken@0
   653
};
slouken@0
   654
nathanheisey@5939
   655
nathanheisey@5939
   656
/* FIXME:
nathanheisey@5939
   657
 * An explanation of framebuffer flags.
nathanheisey@5939
   658
 *
nathanheisey@5939
   659
 * _connected -           Original variable used to let the drawing thread know
nathanheisey@5939
   660
 *                         when changes are being made to the other framebuffer
nathanheisey@5939
   661
 *                         members.
nathanheisey@5939
   662
 * _connection_disabled - Used to signal to the drawing thread that the window
nathanheisey@5939
   663
 *                         is closing, and the thread should exit.
nathanheisey@5939
   664
 * _buffer_created -      True if the current buffer is valid
nathanheisey@5939
   665
 * _buffer_dirty -        True if the window should be redrawn.
nathanheisey@5939
   666
 * _trash_window_buffer - True if the window buffer needs to be trashed partway
nathanheisey@5939
   667
 *                         through a draw cycle.  Occurs when the previous
nathanheisey@5939
   668
 *                         buffer provided by DirectConnected() is invalidated.
nathanheisey@5939
   669
 */
nathanheisey@5916
   670
#endif
slouken@10690
   671
slouken@10690
   672
/* vi: set ts=4 sw=4 expandtab: */