src/video/directfb/SDL_DirectFB_mouse.c
author Bob Pendleton <bob@pendleton.com>
Fri, 09 Jan 2009 20:43:30 +0000
changeset 3011 8f4ed5ec2b06
parent 2998 d364ee9b9c15
child 3013 8cc00819c8d6
permissions -rw-r--r--
I ran a global "make indent" it modified the following files.
slouken@2226
     1
/*
slouken@2226
     2
    SDL - Simple DirectMedia Layer
slouken@2859
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@2226
     4
slouken@2226
     5
    This library is free software; you can redistribute it and/or
slouken@2226
     6
    modify it under the terms of the GNU Lesser General Public
slouken@2226
     7
    License as published by the Free Software Foundation; either
slouken@2226
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@2226
     9
slouken@2226
    10
    This library is distributed in the hope that it will be useful,
slouken@2226
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@2226
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@2226
    13
    Lesser General Public License for more details.
slouken@2226
    14
slouken@2226
    15
    You should have received a copy of the GNU Lesser General Public
slouken@2226
    16
    License along with this library; if not, write to the Free Software
slouken@2226
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@2226
    18
slouken@2226
    19
    Sam Lantinga
slouken@2226
    20
    slouken@libsdl.org
slouken@2226
    21
*/
slouken@2226
    22
#include "SDL_config.h"
slouken@2226
    23
slouken@2226
    24
#include "SDL_DirectFB_video.h"
slouken@2226
    25
slouken@2226
    26
#include "../SDL_sysvideo.h"
slouken@2226
    27
#include "../../events/SDL_mouse_c.h"
slouken@2226
    28
slouken@2226
    29
static SDL_Cursor *DirectFB_CreateCursor(SDL_Surface * surface, int hot_x,
slouken@2226
    30
                                         int hot_y);
slouken@2226
    31
static int DirectFB_ShowCursor(SDL_Cursor * cursor);
slouken@2226
    32
static void DirectFB_MoveCursor(SDL_Cursor * cursor);
slouken@2226
    33
static void DirectFB_FreeCursor(SDL_Cursor * cursor);
slouken@2226
    34
static void DirectFB_WarpMouse(SDL_Mouse * mouse, SDL_WindowID windowID,
slouken@2226
    35
                               int x, int y);
slouken@2226
    36
static void DirectFB_FreeMouse(SDL_Mouse * mouse);
slouken@2226
    37
slouken@2860
    38
static int id_mask;
slouken@2860
    39
slouken@2860
    40
static DFBEnumerationResult
slouken@2860
    41
EnumMice(DFBInputDeviceID device_id,
slouken@2860
    42
         DFBInputDeviceDescription desc, void *callbackdata)
slouken@2860
    43
{
slouken@2860
    44
    DFB_DeviceData *devdata = callbackdata;
slouken@2860
    45
slouken@2860
    46
    if ((desc.type & DIDTF_MOUSE) && (device_id & id_mask)) {
slouken@2860
    47
        SDL_Mouse mouse;
slouken@2860
    48
slouken@2860
    49
        SDL_zero(mouse);
slouken@2940
    50
        mouse.id = device_id;
slouken@2860
    51
        mouse.CreateCursor = DirectFB_CreateCursor;
slouken@2860
    52
        mouse.ShowCursor = DirectFB_ShowCursor;
slouken@2860
    53
        mouse.MoveCursor = DirectFB_MoveCursor;
slouken@2860
    54
        mouse.FreeCursor = DirectFB_FreeCursor;
slouken@2860
    55
        mouse.WarpMouse = DirectFB_WarpMouse;
slouken@2860
    56
        mouse.FreeMouse = DirectFB_FreeMouse;
slouken@2860
    57
        mouse.cursor_shown = 1;
slouken@2860
    58
slouken@2940
    59
        SDL_AddMouse(&mouse, desc.name, 0, 0, 1);
slouken@2940
    60
        devdata->mouse_id[devdata->num_mice++] = device_id;
slouken@2860
    61
    }
slouken@2860
    62
    return DFENUM_OK;
slouken@2860
    63
}
slouken@2860
    64
slouken@2226
    65
void
slouken@2226
    66
DirectFB_InitMouse(_THIS)
slouken@2226
    67
{
slouken@2226
    68
    SDL_DFB_DEVICEDATA(_this);
slouken@2226
    69
slouken@2860
    70
    devdata->num_mice = 0;
slouken@2998
    71
    if (devdata->use_linux_input) {
slouken@2860
    72
        /* try non-core devices first */
slouken@2860
    73
        id_mask = 0xF0;
slouken@2860
    74
        devdata->dfb->EnumInputDevices(devdata->dfb, EnumMice, devdata);
slouken@2860
    75
        if (devdata->num_mice == 0) {
slouken@2860
    76
            /* try core devices */
slouken@2860
    77
            id_mask = 0x0F;
slouken@2860
    78
            devdata->dfb->EnumInputDevices(devdata->dfb, EnumMice, devdata);
slouken@2860
    79
        }
slouken@2860
    80
    }
slouken@2860
    81
    if (devdata->num_mice == 0) {
slouken@2860
    82
        SDL_Mouse mouse;
slouken@2860
    83
slouken@2860
    84
        SDL_zero(mouse);
slouken@2860
    85
        mouse.CreateCursor = DirectFB_CreateCursor;
slouken@2860
    86
        mouse.ShowCursor = DirectFB_ShowCursor;
slouken@2860
    87
        mouse.MoveCursor = DirectFB_MoveCursor;
slouken@2860
    88
        mouse.FreeCursor = DirectFB_FreeCursor;
slouken@2860
    89
        mouse.WarpMouse = DirectFB_WarpMouse;
slouken@2860
    90
        mouse.FreeMouse = DirectFB_FreeMouse;
slouken@2860
    91
        mouse.cursor_shown = 1;
slouken@2860
    92
slouken@2940
    93
        SDL_AddMouse(&mouse, "Mouse", 0, 0, 1);
slouken@2860
    94
        devdata->num_mice = 1;
slouken@2860
    95
    }
slouken@2226
    96
}
slouken@2226
    97
slouken@2226
    98
void
slouken@2226
    99
DirectFB_QuitMouse(_THIS)
slouken@2226
   100
{
slouken@2226
   101
    SDL_DFB_DEVICEDATA(_this);
slouken@2226
   102
slouken@2998
   103
    if (devdata->use_linux_input) {
slouken@2860
   104
        SDL_MouseQuit();
slouken@2860
   105
    } else {
slouken@2860
   106
        SDL_DelMouse(0);
slouken@2860
   107
    }
slouken@2226
   108
}
slouken@2226
   109
slouken@2226
   110
/* Create a cursor from a surface */
slouken@2226
   111
static SDL_Cursor *
slouken@2226
   112
DirectFB_CreateCursor(SDL_Surface * surface, int hot_x, int hot_y)
slouken@2226
   113
{
slouken@2226
   114
    SDL_VideoDevice *dev = SDL_GetVideoDevice();
slouken@2226
   115
slouken@2226
   116
    SDL_DFB_DEVICEDATA(dev);
slouken@2226
   117
    DFB_CursorData *curdata;
slouken@2226
   118
    DFBResult ret;
slouken@2226
   119
    DFBSurfaceDescription dsc;
slouken@2226
   120
    SDL_Cursor *cursor;
slouken@2226
   121
    Uint32 *dest;
slouken@2226
   122
    Uint32 *p;
slouken@2226
   123
    int pitch, i;
slouken@2226
   124
slouken@2226
   125
    SDL_DFB_CALLOC(cursor, 1, sizeof(*cursor));
slouken@2226
   126
    SDL_DFB_CALLOC(curdata, 1, sizeof(*curdata));
slouken@2226
   127
slouken@2226
   128
    dsc.flags =
slouken@2226
   129
        DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | DSDESC_CAPS;
slouken@2721
   130
    dsc.caps = DSCAPS_VIDEOONLY;
slouken@2226
   131
    dsc.width = surface->w;
slouken@2226
   132
    dsc.height = surface->h;
slouken@2226
   133
    dsc.pixelformat = DSPF_ARGB;
slouken@2226
   134
bob@3011
   135
    SDL_DFB_CHECKERR(devdata->
bob@3011
   136
                     dfb->CreateSurface(devdata->dfb, &dsc, &curdata->surf));
slouken@2226
   137
    curdata->hotx = hot_x;
slouken@2226
   138
    curdata->hoty = hot_y;
slouken@2226
   139
    cursor->driverdata = curdata;
slouken@2226
   140
bob@3011
   141
    SDL_DFB_CHECKERR(curdata->
bob@3011
   142
                     surf->Lock(curdata->surf, DSLF_WRITE, (void *) &dest,
bob@3011
   143
                                &pitch));
slouken@2226
   144
slouken@2737
   145
    /* Relies on the fact that this is only called with ARGB surface. */
slouken@2226
   146
    p = surface->pixels;
slouken@2737
   147
    for (i = 0; i < surface->h; i++)
slouken@2737
   148
        memcpy((char *) dest + i * pitch, (char *) p + i * surface->pitch,
slouken@2737
   149
               4 * surface->w);
slouken@2737
   150
slouken@2226
   151
    curdata->surf->Unlock(curdata->surf);
slouken@2226
   152
    return cursor;
slouken@2226
   153
  error:
slouken@2226
   154
    return NULL;
slouken@2226
   155
}
slouken@2226
   156
slouken@2226
   157
/* Show the specified cursor, or hide if cursor is NULL */
slouken@2226
   158
static int
slouken@2226
   159
DirectFB_ShowCursor(SDL_Cursor * cursor)
slouken@2226
   160
{
slouken@2226
   161
    SDL_DFB_CURSORDATA(cursor);
slouken@2226
   162
    DFBResult ret;
slouken@2226
   163
    SDL_WindowID wid;
slouken@2226
   164
slouken@2226
   165
    wid = SDL_GetFocusWindow();
slouken@2737
   166
    if (wid < 0)
slouken@2226
   167
        return -1;
slouken@2226
   168
    else {
slouken@2226
   169
        SDL_Window *window = SDL_GetWindowFromID(wid);
slouken@2226
   170
        SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@2259
   171
slouken@2737
   172
        if (display) {
slouken@2737
   173
            DFB_DisplayData *dispdata =
slouken@2737
   174
                (DFB_DisplayData *) display->driverdata;
slouken@2737
   175
            DFB_WindowData *windata = (DFB_WindowData *) window->driverdata;
slouken@2737
   176
slouken@2737
   177
            if (cursor)
bob@3011
   178
                SDL_DFB_CHECKERR(windata->
bob@3011
   179
                                 window->SetCursorShape(windata->window,
bob@3011
   180
                                                        curdata->surf,
bob@3011
   181
                                                        curdata->hotx,
bob@3011
   182
                                                        curdata->hoty));
slouken@2737
   183
slouken@2737
   184
            /* fprintf(stdout, "Cursor is %s\n", cursor ? "on" : "off"); */
bob@3011
   185
            SDL_DFB_CHECKERR(dispdata->
bob@3011
   186
                             layer->SetCooperativeLevel(dispdata->layer,
bob@3011
   187
                                                        DLSCL_ADMINISTRATIVE));
bob@3011
   188
            SDL_DFB_CHECKERR(dispdata->
bob@3011
   189
                             layer->SetCursorOpacity(dispdata->layer,
bob@3011
   190
                                                     cursor ? 0xC0 : 0x00));
bob@3011
   191
            SDL_DFB_CHECKERR(dispdata->
bob@3011
   192
                             layer->SetCooperativeLevel(dispdata->layer,
bob@3011
   193
                                                        DLSCL_SHARED));
slouken@2737
   194
        }
slouken@2226
   195
    }
slouken@2226
   196
slouken@2226
   197
    return 0;
slouken@2226
   198
  error:
slouken@2226
   199
    return -1;
slouken@2226
   200
}
slouken@2226
   201
slouken@2226
   202
/* This is called when a mouse motion event occurs */
slouken@2226
   203
static void
slouken@2226
   204
DirectFB_MoveCursor(SDL_Cursor * cursor)
slouken@2226
   205
{
slouken@2737
   206
slouken@2226
   207
}
slouken@2226
   208
slouken@2226
   209
/* Free a window manager cursor */
slouken@2226
   210
static void
slouken@2226
   211
DirectFB_FreeCursor(SDL_Cursor * cursor)
slouken@2226
   212
{
slouken@2226
   213
    SDL_DFB_CURSORDATA(cursor);
slouken@2226
   214
slouken@2226
   215
    SDL_DFB_RELEASE(curdata->surf);
slouken@2226
   216
    SDL_DFB_FREE(cursor->driverdata);
slouken@2226
   217
    SDL_DFB_FREE(cursor);
slouken@2226
   218
}
slouken@2226
   219
slouken@2226
   220
/* Warp the mouse to (x,y) */
slouken@2226
   221
static void
slouken@2226
   222
DirectFB_WarpMouse(SDL_Mouse * mouse, SDL_WindowID windowID, int x, int y)
slouken@2226
   223
{
slouken@2226
   224
    SDL_Window *window = SDL_GetWindowFromID(windowID);
slouken@2226
   225
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@2226
   226
    DFB_DisplayData *dispdata = (DFB_DisplayData *) display->driverdata;
slouken@2226
   227
    DFB_WindowData *windata = (DFB_WindowData *) window->driverdata;
slouken@2226
   228
    DFBResult ret;
slouken@2226
   229
    int cx, cy;
slouken@2226
   230
slouken@2226
   231
    SDL_DFB_CHECKERR(windata->window->GetPosition(windata->window, &cx, &cy));
bob@3011
   232
    SDL_DFB_CHECKERR(dispdata->
bob@3011
   233
                     layer->WarpCursor(dispdata->layer, cx + x, cy + y));
slouken@2226
   234
slouken@2226
   235
  error:
slouken@2226
   236
    return;
slouken@2226
   237
}
slouken@2226
   238
slouken@2226
   239
/* Free the mouse when it's time */
slouken@2226
   240
static void
slouken@2226
   241
DirectFB_FreeMouse(SDL_Mouse * mouse)
slouken@2226
   242
{
slouken@2737
   243
    /* nothing yet */
slouken@2226
   244
}
slouken@2226
   245
slouken@2226
   246
/* vi: set ts=4 sw=4 expandtab: */