src/video/haiku/SDL_BWin.h
author Sam Lantinga <slouken@libsdl.org>
Sun, 02 Feb 2014 00:53:27 -0800
changeset 8149 681eb46b8ac4
parent 8093 b43765095a6f
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Fixed bug 2374 - Update copyright for 2014...

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