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