src/video/haiku/SDL_BWin.h
author Ryan C. Gordon <icculus@icculus.org>
Thu, 29 Sep 2016 22:52:41 -0400
changeset 10385 ad12658bc7ae
parent 9998 f67cf37e9cd4
child 10388 0190965b0ec5
permissions -rw-r--r--
Added SDL_SetWindowResizable(). (thanks, Ethan!)
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:
nathanheisey@5916
   340
        case B_UNMAPPED_KEY_DOWN:      /* modifier keys are unmapped */
slouken@7191
   341
            if (msg->FindInt32("key", &key) == B_OK) {
slouken@7191
   342
                _KeyEvent((SDL_Scancode)key, SDL_PRESSED);
slouken@7191
   343
            }
slouken@7191
   344
            break;
nathanheisey@5916
   345
nathanheisey@5916
   346
        case B_KEY_UP:
nathanheisey@5916
   347
        case B_UNMAPPED_KEY_UP:        /* modifier keys are unmapped */
slouken@7191
   348
            if (msg->FindInt32("key", &key) == B_OK) {
slouken@7191
   349
                _KeyEvent(key, SDL_RELEASED);
nathanheisey@5916
   350
            }
nathanheisey@5916
   351
            break;
slouken@7191
   352
nathanheisey@5916
   353
        default:
nathanheisey@5916
   354
            /* move it after switch{} so it's always handled
icculus@7981
   355
               that way we keep Haiku features like:
nathanheisey@5916
   356
               - CTRL+Q to close window (and other shortcuts)
nathanheisey@5916
   357
               - PrintScreen to make screenshot into /boot/home
nathanheisey@5916
   358
               - etc.. */
gabomdq@7678
   359
            /* BDirectWindow::DispatchMessage(msg, target); */
nathanheisey@5916
   360
            break;
nathanheisey@5916
   361
        }
nathanheisey@5925
   362
nathanheisey@5916
   363
        BDirectWindow::DispatchMessage(msg, target);
slouken@1895
   364
    }
slouken@7191
   365
nathanheisey@5916
   366
    /* Handle command messages */
nathanheisey@5916
   367
    virtual void MessageReceived(BMessage* message) {
slouken@7191
   368
        switch (message->what) {
slouken@7191
   369
            /* Handle commands from SDL */
slouken@7191
   370
            case BWIN_SET_TITLE:
slouken@7191
   371
                _SetTitle(message);
slouken@7191
   372
                break;
slouken@7191
   373
            case BWIN_MOVE_WINDOW:
slouken@7191
   374
                _MoveTo(message);
slouken@7191
   375
                break;
slouken@7191
   376
            case BWIN_RESIZE_WINDOW:
slouken@7191
   377
                _ResizeTo(message);
slouken@7191
   378
                break;
slouken@7191
   379
            case BWIN_SET_BORDERED:
slouken@7191
   380
                _SetBordered(message);
slouken@7191
   381
                break;
icculus@10385
   382
            case BWIN_SET_RESIZABLE:
icculus@10385
   383
                _SetResizable(message);
icculus@10385
   384
                break;
slouken@7191
   385
            case BWIN_SHOW_WINDOW:
slouken@7191
   386
                Show();
slouken@7191
   387
                break;
slouken@7191
   388
            case BWIN_HIDE_WINDOW:
slouken@7191
   389
                Hide();
slouken@7191
   390
                break;
slouken@7191
   391
            case BWIN_MAXIMIZE_WINDOW:
slouken@7191
   392
                BWindow::Zoom();
slouken@7191
   393
                break;
slouken@7191
   394
            case BWIN_MINIMIZE_WINDOW:
slouken@7191
   395
                Minimize(true);
slouken@7191
   396
                break;
slouken@7191
   397
            case BWIN_RESTORE_WINDOW:
slouken@7191
   398
                _Restore();
slouken@7191
   399
                break;
slouken@7191
   400
            case BWIN_FULLSCREEN:
slouken@7191
   401
                _SetFullScreen(message);
slouken@7191
   402
                break;
slouken@7191
   403
            default:
slouken@7191
   404
                /* Perform normal message handling */
slouken@7191
   405
                BDirectWindow::MessageReceived(message);
slouken@7191
   406
                break;
slouken@7191
   407
        }
nathanheisey@5916
   408
nathanheisey@5916
   409
    }
nathanheisey@5916
   410
slouken@7191
   411
slouken@7191
   412
slouken@7191
   413
    /* Accessor methods */
slouken@7191
   414
    bool IsShown() { return _shown; }
slouken@7191
   415
    int32 GetID() { return _id; }
slouken@7191
   416
    uint32 GetRowBytes() { return _row_bytes; }
slouken@7191
   417
    int32 GetFbX() { return _bounds.left; }
slouken@7191
   418
    int32 GetFbY() { return _bounds.top; }
slouken@7191
   419
    bool ConnectionEnabled() { return !_connection_disabled; }
slouken@7191
   420
    bool Connected() { return _connected; }
slouken@7191
   421
    clipping_rect *GetClips() { return _clips; }
slouken@7191
   422
    int32 GetNumClips() { return _num_clips; }
slouken@7191
   423
    uint8* GetBufferPx() { return _bits; }
slouken@7191
   424
    int32 GetBytesPerPx() { return _bytes_per_px; }
slouken@7191
   425
    bool CanTrashWindowBuffer() { return _trash_window_buffer; }
slouken@7191
   426
    bool BufferExists() { return _buffer_created; }
slouken@7191
   427
    bool BufferIsDirty() { return _buffer_dirty; }
slouken@7191
   428
    BBitmap *GetBitmap() { return _bitmap; }
nathanheisey@5934
   429
#if SDL_VIDEO_OPENGL
slouken@7191
   430
    BGLView *GetGLView() { return _SDL_GLView; }
nathanheisey@5934
   431
#endif
slouken@7191
   432
slouken@7191
   433
    /* Setter methods */
slouken@7191
   434
    void SetID(int32 id) { _id = id; }
slouken@7191
   435
    void SetBufferExists(bool bufferExists) { _buffer_created = bufferExists; }
slouken@7191
   436
    void LockBuffer() { _buffer_locker->Lock(); }
slouken@7191
   437
    void UnlockBuffer() { _buffer_locker->Unlock(); }
slouken@7191
   438
    void SetBufferDirty(bool bufferDirty) { _buffer_dirty = bufferDirty; }
slouken@7191
   439
    void SetTrashBuffer(bool trash) { _trash_window_buffer = trash;     }
slouken@7191
   440
    void SetBitmap(BBitmap *bitmap) { _bitmap = bitmap; }
slouken@7191
   441
slouken@7191
   442
nathanheisey@5916
   443
private:
slouken@7191
   444
    /* Event redirection */
nathanheisey@5916
   445
    void _MouseMotionEvent(BPoint &where, int32 transit) {
slouken@7191
   446
        if(transit == B_EXITED_VIEW) {
slouken@7191
   447
            /* Change mouse focus */
slouken@7191
   448
            if(_mouse_focused) {
slouken@7191
   449
                _MouseFocusEvent(false);
slouken@7191
   450
            }
slouken@7191
   451
        } else {
slouken@7191
   452
            /* Change mouse focus */
slouken@7191
   453
            if (!_mouse_focused) {
slouken@7191
   454
                _MouseFocusEvent(true);
slouken@7191
   455
            }
slouken@7191
   456
            BMessage msg(BAPP_MOUSE_MOVED);
slouken@7191
   457
            msg.AddInt32("x", (int)where.x);
slouken@7191
   458
            msg.AddInt32("y", (int)where.y);
slouken@7191
   459
slouken@7191
   460
            _PostWindowEvent(msg);
slouken@7191
   461
        }
slouken@1895
   462
    }
slouken@7191
   463
nathanheisey@5916
   464
    void _MouseFocusEvent(bool focusGained) {
slouken@7191
   465
        _mouse_focused = focusGained;
slouken@7191
   466
        BMessage msg(BAPP_MOUSE_FOCUS);
slouken@7191
   467
        msg.AddBool("focusGained", focusGained);
slouken@7191
   468
        _PostWindowEvent(msg);
slouken@7191
   469
gabomdq@7678
   470
/* FIXME: Why were these here?
gabomdq@7663
   471
 if false: be_app->SetCursor(B_HAND_CURSOR);
gabomdq@7677
   472
 if true:  SDL_SetCursor(NULL); */
slouken@1895
   473
    }
slouken@7191
   474
nathanheisey@5916
   475
    void _MouseButtonEvent(int32 buttons) {
slouken@7191
   476
        int32 buttonStateChange = buttons ^ _last_buttons;
slouken@7191
   477
slouken@7191
   478
        /* Make sure at least one button has changed state */
slouken@7191
   479
        if( !(buttonStateChange) ) {
slouken@7191
   480
            return;
slouken@7191
   481
        }
slouken@7191
   482
slouken@7191
   483
        /* Add any mouse button events */
slouken@7191
   484
        if(buttonStateChange & B_PRIMARY_MOUSE_BUTTON) {
slouken@7191
   485
            _SendMouseButton(SDL_BUTTON_LEFT, buttons &
slouken@7191
   486
                B_PRIMARY_MOUSE_BUTTON);
slouken@7191
   487
        }
slouken@7191
   488
        if(buttonStateChange & B_SECONDARY_MOUSE_BUTTON) {
slouken@7191
   489
            _SendMouseButton(SDL_BUTTON_RIGHT, buttons &
slouken@7191
   490
                B_PRIMARY_MOUSE_BUTTON);
slouken@7191
   491
        }
slouken@7191
   492
        if(buttonStateChange & B_TERTIARY_MOUSE_BUTTON) {
slouken@7191
   493
            _SendMouseButton(SDL_BUTTON_MIDDLE, buttons &
slouken@7191
   494
                B_PRIMARY_MOUSE_BUTTON);
slouken@7191
   495
        }
slouken@7191
   496
slouken@7191
   497
        _last_buttons = buttons;
slouken@1895
   498
    }
slouken@7191
   499
nathanheisey@5916
   500
    void _SendMouseButton(int32 button, int32 state) {
slouken@7191
   501
        BMessage msg(BAPP_MOUSE_BUTTON);
slouken@7191
   502
        msg.AddInt32("button-id", button);
slouken@7191
   503
        msg.AddInt32("button-state", state);
slouken@7191
   504
        _PostWindowEvent(msg);
nathanheisey@5916
   505
    }
slouken@7191
   506
nathanheisey@5916
   507
    void _MouseWheelEvent(int32 x, int32 y) {
slouken@7191
   508
        /* Create a message to pass along to the BeApp thread */
slouken@7191
   509
        BMessage msg(BAPP_MOUSE_WHEEL);
slouken@7191
   510
        msg.AddInt32("xticks", x);
slouken@7191
   511
        msg.AddInt32("yticks", y);
slouken@7191
   512
        _PostWindowEvent(msg);
nathanheisey@5916
   513
    }
slouken@7191
   514
nathanheisey@5916
   515
    void _KeyEvent(int32 keyCode, int32 keyState) {
slouken@7191
   516
        /* Create a message to pass along to the BeApp thread */
slouken@7191
   517
        BMessage msg(BAPP_KEY);
slouken@7191
   518
        msg.AddInt32("key-state", keyState);
slouken@7191
   519
        msg.AddInt32("key-scancode", keyCode);
slouken@7191
   520
        be_app->PostMessage(&msg);
slouken@7191
   521
        /* Apparently SDL only uses the scancode */
nathanheisey@5916
   522
    }
slouken@7191
   523
nathanheisey@5916
   524
    void _RepaintEvent() {
slouken@7191
   525
        /* Force a repaint: Call the SDL exposed event */
slouken@7191
   526
        BMessage msg(BAPP_REPAINT);
slouken@7191
   527
        _PostWindowEvent(msg);
nathanheisey@5916
   528
    }
nathanheisey@5916
   529
    void _PostWindowEvent(BMessage &msg) {
slouken@7191
   530
        msg.AddInt32("window-id", _id);
slouken@7191
   531
        be_app->PostMessage(&msg);
nathanheisey@5916
   532
    }
slouken@7191
   533
slouken@7191
   534
    /* Command methods (functions called upon by SDL) */
nathanheisey@5916
   535
    void _SetTitle(BMessage *msg) {
slouken@7191
   536
        const char *title;
slouken@7191
   537
        if(
slouken@7191
   538
            msg->FindString("window-title", &title) != B_OK
slouken@7191
   539
        ) {
slouken@7191
   540
            return;
slouken@7191
   541
        }
slouken@7191
   542
        SetTitle(title);
nathanheisey@5916
   543
    }
slouken@7191
   544
nathanheisey@5916
   545
    void _MoveTo(BMessage *msg) {
slouken@7191
   546
        int32 x, y;
slouken@7191
   547
        if(
slouken@7191
   548
            msg->FindInt32("window-x", &x) != B_OK ||
slouken@7191
   549
            msg->FindInt32("window-y", &y) != B_OK
slouken@7191
   550
        ) {
slouken@7191
   551
            return;
slouken@7191
   552
        }
slouken@7191
   553
        MoveTo(x, y);
nathanheisey@5916
   554
    }
slouken@7191
   555
nathanheisey@5916
   556
    void _ResizeTo(BMessage *msg) {
slouken@7191
   557
        int32 w, h;
slouken@7191
   558
        if(
slouken@7191
   559
            msg->FindInt32("window-w", &w) != B_OK ||
slouken@7191
   560
            msg->FindInt32("window-h", &h) != B_OK
slouken@7191
   561
        ) {
slouken@7191
   562
            return;
slouken@7191
   563
        }
slouken@7191
   564
        ResizeTo(w, h);
nathanheisey@5916
   565
    }
icculus@6422
   566
icculus@6422
   567
    void _SetBordered(BMessage *msg) {
slouken@7191
   568
        bool bEnabled;
slouken@7191
   569
        if(msg->FindBool("window-border", &bEnabled) != B_OK) {
slouken@7191
   570
            return;
slouken@7191
   571
        }
slouken@7191
   572
        SetLook(bEnabled ? B_BORDERED_WINDOW_LOOK : B_NO_BORDER_WINDOW_LOOK);
icculus@6422
   573
    }
icculus@6422
   574
icculus@10385
   575
    void _SetResizable(BMessage *msg) {
icculus@10385
   576
        bool bEnabled;
icculus@10385
   577
        if(msg->FindBool("window-resizable", &bEnabled) != B_OK) {
icculus@10385
   578
            return;
icculus@10385
   579
        }
icculus@10385
   580
        if (bEnabled) {
icculus@10385
   581
            SetFlags(GetFlags() & ~(B_NOT_RESIZABLE | B_NOT_ZOOMABLE));
icculus@10385
   582
        } else {
icculus@10385
   583
            SetFlags(GetFlags() | (B_NOT_RESIZABLE | B_NOT_ZOOMABLE));
icculus@10385
   584
        }
icculus@10385
   585
    }
icculus@10385
   586
nathanheisey@5916
   587
    void _Restore() {
slouken@7191
   588
        if(IsMinimized()) {
slouken@7191
   589
            Minimize(false);
slouken@7191
   590
        } else if(IsHidden()) {
slouken@7191
   591
            Show();
slouken@7191
   592
        } else if(_prev_frame != NULL) {    /* Zoomed */
slouken@7191
   593
            MoveTo(_prev_frame->left, _prev_frame->top);
slouken@7191
   594
            ResizeTo(_prev_frame->Width(), _prev_frame->Height());
slouken@7191
   595
        }
slouken@1895
   596
    }
slouken@0
   597
nathanheisey@5916
   598
    void _SetFullScreen(BMessage *msg) {
slouken@7191
   599
        bool fullscreen;
slouken@7191
   600
        if(
slouken@7191
   601
            msg->FindBool("fullscreen", &fullscreen) != B_OK
slouken@7191
   602
        ) {
slouken@7191
   603
            return;
slouken@7191
   604
        }
slouken@7191
   605
        SetFullScreen(fullscreen);
slouken@1895
   606
    }
slouken@7191
   607
nathanheisey@5916
   608
    /* Members */
slouken@1361
   609
#if SDL_VIDEO_OPENGL
nathanheisey@5925
   610
    BGLView * _SDL_GLView;
slouken@0
   611
#endif
slouken@7191
   612
nathanheisey@5925
   613
    int32 _last_buttons;
slouken@7191
   614
    int32 _id;  /* Window id used by SDL_BApp */
slouken@7191
   615
    bool  _mouse_focused;       /* Does this window have mouse focus? */
nathanheisey@5925
   616
    bool  _shown;
nathanheisey@5925
   617
    bool  _inhibit_resize;
slouken@7191
   618
slouken@7191
   619
    BRect *_prev_frame; /* Previous position and size of the window */
slouken@7191
   620
nathanheisey@5922
   621
    /* Framebuffer members */
slouken@7191
   622
    bool            _connected,
slouken@7191
   623
                    _connection_disabled,
slouken@7191
   624
                    _buffer_created,
slouken@7191
   625
                    _buffer_dirty,
slouken@7191
   626
                    _trash_window_buffer;
slouken@7191
   627
    uint8          *_bits;
slouken@7191
   628
    uint32          _row_bytes;
slouken@7191
   629
    clipping_rect   _bounds;
slouken@7191
   630
    BLocker        *_buffer_locker;
nathanheisey@5925
   631
    clipping_rect  *_clips;
slouken@7191
   632
    int32           _num_clips;
slouken@7191
   633
    int32           _bytes_per_px;
slouken@7191
   634
    thread_id       _draw_thread_id;
slouken@7191
   635
slouken@7191
   636
    BBitmap        *_bitmap;
slouken@0
   637
};
slouken@0
   638
nathanheisey@5939
   639
nathanheisey@5939
   640
/* FIXME:
nathanheisey@5939
   641
 * An explanation of framebuffer flags.
nathanheisey@5939
   642
 *
nathanheisey@5939
   643
 * _connected -           Original variable used to let the drawing thread know
nathanheisey@5939
   644
 *                         when changes are being made to the other framebuffer
nathanheisey@5939
   645
 *                         members.
nathanheisey@5939
   646
 * _connection_disabled - Used to signal to the drawing thread that the window
nathanheisey@5939
   647
 *                         is closing, and the thread should exit.
nathanheisey@5939
   648
 * _buffer_created -      True if the current buffer is valid
nathanheisey@5939
   649
 * _buffer_dirty -        True if the window should be redrawn.
nathanheisey@5939
   650
 * _trash_window_buffer - True if the window buffer needs to be trashed partway
nathanheisey@5939
   651
 *                         through a draw cycle.  Occurs when the previous
nathanheisey@5939
   652
 *                         buffer provided by DirectConnected() is invalidated.
nathanheisey@5939
   653
 */
nathanheisey@5916
   654
#endif