src/video/emscripten/SDL_emscriptenmouse.c
author Sam Lantinga
Fri, 27 Jan 2017 21:23:27 -0800
changeset 10861 71d8f9afb690
parent 10737 3406a0f8b041
child 10897 19203215ca8e
permissions -rw-r--r--
Fixed bug 3569 - GL_UpdateViewport leaves PROJECTION matrix selected

Tom Seddon

GL_ActivateRenderer may call GL_UpdateViewport, which leaves the GL_PROJECTION matrix selected. But after GL_ResetState, the GL_MODELVIEW matrix is selected, suggesting that's the intended default state.

It seems at least like these should be consistent. Presumably GL_UpdateViewport should be doing a glMatrixMode(GL_MODELVIEW) before it finishes.
icculus@9278
     1
/*
icculus@9278
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
icculus@9278
     4
icculus@9278
     5
  This software is provided 'as-is', without any express or implied
icculus@9278
     6
  warranty.  In no event will the authors be held liable for any damages
icculus@9278
     7
  arising from the use of this software.
icculus@9278
     8
icculus@9278
     9
  Permission is granted to anyone to use this software for any purpose,
icculus@9278
    10
  including commercial applications, and to alter it and redistribute it
icculus@9278
    11
  freely, subject to the following restrictions:
icculus@9278
    12
icculus@9278
    13
  1. The origin of this software must not be misrepresented; you must not
icculus@9278
    14
     claim that you wrote the original software. If you use this software
icculus@9278
    15
     in a product, an acknowledgment in the product documentation would be
icculus@9278
    16
     appreciated but is not required.
icculus@9278
    17
  2. Altered source versions must be plainly marked as such, and must not be
icculus@9278
    18
     misrepresented as being the original software.
icculus@9278
    19
  3. This notice may not be removed or altered from any source distribution.
icculus@9278
    20
*/
icculus@9278
    21
icculus@9278
    22
icculus@9278
    23
#include "../../SDL_internal.h"
icculus@9278
    24
icculus@9278
    25
#if SDL_VIDEO_DRIVER_EMSCRIPTEN
icculus@9278
    26
icculus@9278
    27
#include <emscripten/emscripten.h>
icculus@9278
    28
#include <emscripten/html5.h>
icculus@9278
    29
icculus@9278
    30
#include "SDL_emscriptenmouse.h"
icculus@9278
    31
icculus@9278
    32
#include "../../events/SDL_mouse_c.h"
icculus@9278
    33
#include "SDL_assert.h"
icculus@9278
    34
icculus@9278
    35
icculus@9278
    36
static SDL_Cursor*
icculus@9278
    37
Emscripten_CreateDefaultCursor()
icculus@9278
    38
{
icculus@9278
    39
    SDL_Cursor* cursor;
icculus@9278
    40
    Emscripten_CursorData *curdata;
icculus@9278
    41
icculus@9278
    42
    cursor = SDL_calloc(1, sizeof(SDL_Cursor));
icculus@9278
    43
    if (cursor) {
icculus@9278
    44
        curdata = (Emscripten_CursorData *) SDL_calloc(1, sizeof(*curdata));
philipp@9298
    45
        if (!curdata) {
philipp@9298
    46
            SDL_OutOfMemory();
philipp@9298
    47
            SDL_free(cursor);
philipp@9298
    48
            return NULL;
philipp@9298
    49
        }
icculus@9278
    50
icculus@9278
    51
        curdata->system_cursor = "default";
icculus@9278
    52
        cursor->driverdata = curdata;
icculus@9278
    53
    }
icculus@9278
    54
    else {
icculus@9278
    55
        SDL_OutOfMemory();
icculus@9278
    56
    }
icculus@9278
    57
icculus@9278
    58
    return cursor;
icculus@9278
    59
}
icculus@9278
    60
philipp@10115
    61
/*
icculus@9278
    62
static SDL_Cursor*
icculus@9278
    63
Emscripten_CreateCursor(SDL_Surface* sruface, int hot_x, int hot_y)
icculus@9278
    64
{
icculus@9278
    65
    return Emscripten_CreateDefaultCursor();
icculus@9278
    66
}
philipp@10115
    67
*/
icculus@9278
    68
icculus@9278
    69
static SDL_Cursor*
icculus@9278
    70
Emscripten_CreateSystemCursor(SDL_SystemCursor id)
icculus@9278
    71
{
icculus@9278
    72
    SDL_Cursor *cursor;
icculus@9278
    73
    Emscripten_CursorData *curdata;
icculus@9278
    74
    const char *cursor_name = NULL;
icculus@9278
    75
icculus@9278
    76
    switch(id) {
icculus@9278
    77
        case SDL_SYSTEM_CURSOR_ARROW:
icculus@9278
    78
            cursor_name = "default";
icculus@9278
    79
            break;
icculus@9278
    80
        case SDL_SYSTEM_CURSOR_IBEAM:
icculus@9278
    81
            cursor_name = "text";
icculus@9278
    82
            break;
icculus@9278
    83
        case SDL_SYSTEM_CURSOR_WAIT:
icculus@9278
    84
            cursor_name = "wait";
icculus@9278
    85
            break;
icculus@9278
    86
        case SDL_SYSTEM_CURSOR_CROSSHAIR:
icculus@9278
    87
            cursor_name = "crosshair";
icculus@9278
    88
            break;
icculus@9278
    89
        case SDL_SYSTEM_CURSOR_WAITARROW:
icculus@9278
    90
            cursor_name = "progress";
icculus@9278
    91
            break;
icculus@9278
    92
        case SDL_SYSTEM_CURSOR_SIZENWSE:
icculus@9278
    93
            cursor_name = "nwse-resize";
icculus@9278
    94
            break;
icculus@9278
    95
        case SDL_SYSTEM_CURSOR_SIZENESW:
icculus@9278
    96
            cursor_name = "nesw-resize";
icculus@9278
    97
            break;
icculus@9278
    98
        case SDL_SYSTEM_CURSOR_SIZEWE:
icculus@9278
    99
            cursor_name = "ew-resize";
icculus@9278
   100
            break;
icculus@9278
   101
        case SDL_SYSTEM_CURSOR_SIZENS:
icculus@9278
   102
            cursor_name = "ns-resize";
icculus@9278
   103
            break;
icculus@9278
   104
        case SDL_SYSTEM_CURSOR_SIZEALL:
icculus@9278
   105
            break;
icculus@9278
   106
        case SDL_SYSTEM_CURSOR_NO:
icculus@9278
   107
            cursor_name = "not-allowed";
icculus@9278
   108
            break;
icculus@9278
   109
        case SDL_SYSTEM_CURSOR_HAND:
icculus@9278
   110
            cursor_name = "pointer";
icculus@9278
   111
            break;
icculus@9278
   112
        default:
icculus@9278
   113
            SDL_assert(0);
icculus@9278
   114
            return NULL;
icculus@9278
   115
    }
icculus@9278
   116
icculus@9278
   117
    cursor = (SDL_Cursor *) SDL_calloc(1, sizeof(*cursor));
philipp@9298
   118
    if (!cursor) {
philipp@9298
   119
        SDL_OutOfMemory();
philipp@9298
   120
        return NULL;
philipp@9298
   121
    }
icculus@9278
   122
    curdata = (Emscripten_CursorData *) SDL_calloc(1, sizeof(*curdata));
philipp@9298
   123
    if (!curdata) {
philipp@9298
   124
        SDL_OutOfMemory();
philipp@9298
   125
        SDL_free(cursor);
philipp@9298
   126
        return NULL;
philipp@9298
   127
    }
icculus@9278
   128
icculus@9278
   129
    curdata->system_cursor = cursor_name;
icculus@9278
   130
    cursor->driverdata = curdata;
icculus@9278
   131
icculus@9278
   132
    return cursor;
icculus@9278
   133
}
icculus@9278
   134
icculus@9278
   135
static void
icculus@9278
   136
Emscripten_FreeCursor(SDL_Cursor* cursor)
icculus@9278
   137
{
icculus@9278
   138
    Emscripten_CursorData *curdata;
icculus@9278
   139
    if (cursor) {
icculus@9278
   140
        curdata = (Emscripten_CursorData *) cursor->driverdata;
icculus@9278
   141
icculus@9278
   142
        if (curdata != NULL) {
icculus@9278
   143
            SDL_free(cursor->driverdata);
icculus@9278
   144
        }
icculus@9278
   145
icculus@9278
   146
        SDL_free(cursor);
icculus@9278
   147
    }
icculus@9278
   148
}
icculus@9278
   149
icculus@9278
   150
static int
icculus@9278
   151
Emscripten_ShowCursor(SDL_Cursor* cursor)
icculus@9278
   152
{
icculus@9278
   153
    Emscripten_CursorData *curdata;
icculus@9278
   154
    if (SDL_GetMouseFocus() != NULL) {
icculus@9278
   155
        if(cursor && cursor->driverdata) {
icculus@9278
   156
            curdata = (Emscripten_CursorData *) cursor->driverdata;
icculus@9278
   157
icculus@9278
   158
            if(curdata->system_cursor) {
icculus@9278
   159
                EM_ASM_INT({
icculus@9278
   160
                    if (Module['canvas']) {
icculus@9278
   161
                        Module['canvas'].style['cursor'] = Module['Pointer_stringify']($0);
icculus@9278
   162
                    }
icculus@9278
   163
                    return 0;
icculus@9278
   164
                }, curdata->system_cursor);
icculus@9278
   165
            }
icculus@9278
   166
        }
icculus@9278
   167
        else {
icculus@9278
   168
            EM_ASM(
icculus@9278
   169
                if (Module['canvas']) {
icculus@9278
   170
                    Module['canvas'].style['cursor'] = 'none';
icculus@9278
   171
                }
icculus@9278
   172
            );
icculus@9278
   173
        }
icculus@9278
   174
    }
icculus@9278
   175
    return 0;
icculus@9278
   176
}
icculus@9278
   177
icculus@9278
   178
static void
icculus@9278
   179
Emscripten_WarpMouse(SDL_Window* window, int x, int y)
icculus@9278
   180
{
icculus@9278
   181
    SDL_Unsupported();
icculus@9278
   182
}
icculus@9278
   183
icculus@9278
   184
static int
icculus@9278
   185
Emscripten_SetRelativeMouseMode(SDL_bool enabled)
icculus@9278
   186
{
icculus@9278
   187
    /* TODO: pointer lock isn't actually enabled yet */
icculus@9278
   188
    if(enabled) {
icculus@9278
   189
        if(emscripten_request_pointerlock(NULL, 1) >= EMSCRIPTEN_RESULT_SUCCESS) {
icculus@9278
   190
            return 0;
icculus@9278
   191
        }
icculus@9278
   192
    } else {
icculus@9278
   193
        if(emscripten_exit_pointerlock() >= EMSCRIPTEN_RESULT_SUCCESS) {
icculus@9278
   194
            return 0;
icculus@9278
   195
        }
icculus@9278
   196
    }
icculus@9278
   197
    return -1;
icculus@9278
   198
}
icculus@9278
   199
icculus@9278
   200
void
icculus@9278
   201
Emscripten_InitMouse()
icculus@9278
   202
{
icculus@9278
   203
    SDL_Mouse* mouse = SDL_GetMouse();
icculus@9278
   204
philipp@10115
   205
/*
icculus@9278
   206
    mouse->CreateCursor         = Emscripten_CreateCursor;
philipp@10115
   207
*/ 
icculus@9278
   208
    mouse->ShowCursor           = Emscripten_ShowCursor;
icculus@9278
   209
    mouse->FreeCursor           = Emscripten_FreeCursor;
icculus@9278
   210
    mouse->WarpMouse            = Emscripten_WarpMouse;
icculus@9278
   211
    mouse->CreateSystemCursor   = Emscripten_CreateSystemCursor;
icculus@9278
   212
    mouse->SetRelativeMouseMode = Emscripten_SetRelativeMouseMode;
icculus@9278
   213
icculus@9278
   214
    SDL_SetDefaultCursor(Emscripten_CreateDefaultCursor());
icculus@9278
   215
}
icculus@9278
   216
icculus@9278
   217
void
icculus@9278
   218
Emscripten_FiniMouse()
icculus@9278
   219
{
icculus@9278
   220
    SDL_Mouse* mouse = SDL_GetMouse();
icculus@9278
   221
icculus@9278
   222
    Emscripten_FreeCursor(mouse->def_cursor);
icculus@9278
   223
    mouse->def_cursor = NULL;
icculus@9278
   224
icculus@9278
   225
    mouse->CreateCursor         = NULL;
icculus@9278
   226
    mouse->ShowCursor           = NULL;
icculus@9278
   227
    mouse->FreeCursor           = NULL;
icculus@9278
   228
    mouse->WarpMouse            = NULL;
icculus@9278
   229
    mouse->CreateSystemCursor   = NULL;
icculus@9278
   230
    mouse->SetRelativeMouseMode = NULL;
icculus@9278
   231
}
icculus@9278
   232
icculus@9278
   233
#endif /* SDL_VIDEO_DRIVER_EMSCRIPTEN */
icculus@9278
   234
icculus@9278
   235
/* vi: set ts=4 sw=4 expandtab: */
icculus@9278
   236