src/events/SDL_mouse.c
changeset 5371 fc3d3d580777
parent 5370 cb219a294ebf
child 5376 183ec2d4485c
equal deleted inserted replaced
5370:cb219a294ebf 5371:fc3d3d580777
    27 #include "SDL_events_c.h"
    27 #include "SDL_events_c.h"
    28 #include "default_cursor.h"
    28 #include "default_cursor.h"
    29 #include "../video/SDL_sysvideo.h"
    29 #include "../video/SDL_sysvideo.h"
    30 
    30 
    31 
    31 
    32 /* Global mouse information */
    32 /* The mouse state */
    33 
       
    34 typedef struct SDL_Mouse SDL_Mouse;
       
    35 
       
    36 struct SDL_Mouse
       
    37 {
       
    38     /* Create a cursor from a surface */
       
    39     SDL_Cursor *(*CreateCursor) (SDL_Surface * surface, int hot_x, int hot_y);
       
    40 
       
    41     /* Show the specified cursor, or hide if cursor is NULL */
       
    42     int (*ShowCursor) (SDL_Cursor * cursor);
       
    43 
       
    44     /* This is called when a mouse motion event occurs */
       
    45     void (*MoveCursor) (SDL_Cursor * cursor);
       
    46 
       
    47     /* Free a window manager cursor */
       
    48     void (*FreeCursor) (SDL_Cursor * cursor);
       
    49 
       
    50     /* Warp the mouse to (x,y) */
       
    51     void (*WarpMouse) (SDL_Mouse * mouse, SDL_Window * window, int x, int y);
       
    52 
       
    53     /* Data common to all mice */
       
    54     SDL_Window *focus;
       
    55     int x;
       
    56     int y;
       
    57     int xdelta;
       
    58     int ydelta;
       
    59     int last_x, last_y;         /* the last reported x and y coordinates */
       
    60     Uint8 buttonstate;
       
    61     SDL_bool relative_mode;
       
    62 
       
    63     SDL_Cursor *cursors;
       
    64     SDL_Cursor *def_cursor;
       
    65     SDL_Cursor *cur_cursor;
       
    66     SDL_bool cursor_shown;
       
    67 };
       
    68 
       
    69 static SDL_Mouse SDL_mouse;
    33 static SDL_Mouse SDL_mouse;
    70 
    34 
    71 
    35 
    72 /* Public functions */
    36 /* Public functions */
    73 int
    37 int
    74 SDL_MouseInit(void)
    38 SDL_MouseInit(void)
    75 {
    39 {
    76     return (0);
    40     return (0);
       
    41 }
       
    42 
       
    43 SDL_Mouse *
       
    44 SDL_GetMouse(void)
       
    45 {
       
    46     return &SDL_mouse;
    77 }
    47 }
    78 
    48 
    79 void
    49 void
    80 SDL_ResetMouse(void)
    50 SDL_ResetMouse(void)
    81 {
    51 {
    83 }
    53 }
    84 
    54 
    85 SDL_Window *
    55 SDL_Window *
    86 SDL_GetMouseFocus(void)
    56 SDL_GetMouseFocus(void)
    87 {
    57 {
    88     SDL_Mouse *mouse = &SDL_mouse;
    58     SDL_Mouse *mouse = SDL_GetMouse();
    89 
    59 
    90     return mouse->focus;
    60     return mouse->focus;
    91 }
    61 }
    92 
    62 
    93 void
    63 void
    94 SDL_SetMouseFocus(SDL_Window * window)
    64 SDL_SetMouseFocus(SDL_Window * window)
    95 {
    65 {
    96     SDL_Mouse *mouse = &SDL_mouse;
    66     SDL_Mouse *mouse = SDL_GetMouse();
    97 
    67 
    98     if (mouse->focus == window) {
    68     if (mouse->focus == window) {
    99         return;
    69         return;
   100     }
    70     }
   101 
    71 
   112 }
    82 }
   113 
    83 
   114 int
    84 int
   115 SDL_SendMouseMotion(SDL_Window * window, int relative, int x, int y)
    85 SDL_SendMouseMotion(SDL_Window * window, int relative, int x, int y)
   116 {
    86 {
   117     SDL_Mouse *mouse = &SDL_mouse;
    87     SDL_Mouse *mouse = SDL_GetMouse();
   118     int posted;
    88     int posted;
   119     int xrel;
    89     int xrel;
   120     int yrel;
    90     int yrel;
   121     int x_max = 0, y_max = 0;
    91     int x_max = 0, y_max = 0;
   122 
    92 
   202 }
   172 }
   203 
   173 
   204 int
   174 int
   205 SDL_SendMouseButton(SDL_Window * window, Uint8 state, Uint8 button)
   175 SDL_SendMouseButton(SDL_Window * window, Uint8 state, Uint8 button)
   206 {
   176 {
   207     SDL_Mouse *mouse = &SDL_mouse;
   177     SDL_Mouse *mouse = SDL_GetMouse();
   208     int posted;
   178     int posted;
   209     Uint32 type;
   179     Uint32 type;
   210 
   180 
   211     if (window) {
   181     if (window) {
   212         SDL_SetMouseFocus(window);
   182         SDL_SetMouseFocus(window);
   251 }
   221 }
   252 
   222 
   253 int
   223 int
   254 SDL_SendMouseWheel(SDL_Window * window, int x, int y)
   224 SDL_SendMouseWheel(SDL_Window * window, int x, int y)
   255 {
   225 {
   256     SDL_Mouse *mouse = &SDL_mouse;
   226     SDL_Mouse *mouse = SDL_GetMouse();
   257     int posted;
   227     int posted;
   258 
   228 
   259     if (window) {
   229     if (window) {
   260         SDL_SetMouseFocus(window);
   230         SDL_SetMouseFocus(window);
   261     }
   231     }
   283 }
   253 }
   284 
   254 
   285 Uint8
   255 Uint8
   286 SDL_GetMouseState(int *x, int *y)
   256 SDL_GetMouseState(int *x, int *y)
   287 {
   257 {
   288     SDL_Mouse *mouse = &SDL_mouse;
   258     SDL_Mouse *mouse = SDL_GetMouse();
   289 
   259 
   290     if (x) {
   260     if (x) {
   291         *x = mouse->x;
   261         *x = mouse->x;
   292     }
   262     }
   293     if (y) {
   263     if (y) {
   297 }
   267 }
   298 
   268 
   299 Uint8
   269 Uint8
   300 SDL_GetRelativeMouseState(int *x, int *y)
   270 SDL_GetRelativeMouseState(int *x, int *y)
   301 {
   271 {
   302     SDL_Mouse *mouse = &SDL_mouse;
   272     SDL_Mouse *mouse = SDL_GetMouse();
   303 
   273 
   304     if (x) {
   274     if (x) {
   305         *x = mouse->xdelta;
   275         *x = mouse->xdelta;
   306     }
   276     }
   307     if (y) {
   277     if (y) {
   313 }
   283 }
   314 
   284 
   315 void
   285 void
   316 SDL_WarpMouseInWindow(SDL_Window * window, int x, int y)
   286 SDL_WarpMouseInWindow(SDL_Window * window, int x, int y)
   317 {
   287 {
   318     SDL_Mouse *mouse = &SDL_mouse;
   288     SDL_Mouse *mouse = SDL_GetMouse();
   319 
   289 
   320     if (mouse->WarpMouse) {
   290     if (mouse->WarpMouse) {
   321         mouse->WarpMouse(mouse, window, x, y);
   291         mouse->WarpMouse(window, x, y);
   322     } else {
   292     } else {
   323         SDL_SendMouseMotion(window, 0, x, y);
   293         SDL_SendMouseMotion(window, 0, x, y);
   324     }
   294     }
   325 }
   295 }
   326 
   296 
   327 int
   297 int
   328 SDL_SetRelativeMouseMode(SDL_bool enabled)
   298 SDL_SetRelativeMouseMode(SDL_bool enabled)
   329 {
   299 {
   330     SDL_Mouse *mouse = &SDL_mouse;
   300     SDL_Mouse *mouse = SDL_GetMouse();
   331 
   301 
   332     /* Flush pending mouse motion */
   302     /* Flush pending mouse motion */
   333     SDL_FlushEvent(SDL_MOUSEMOTION);
   303     SDL_FlushEvent(SDL_MOUSEMOTION);
   334 
   304 
   335     /* Set the relative mode */
   305     /* Set the relative mode */
   347 }
   317 }
   348 
   318 
   349 SDL_bool
   319 SDL_bool
   350 SDL_GetRelativeMouseMode()
   320 SDL_GetRelativeMouseMode()
   351 {
   321 {
   352     SDL_Mouse *mouse = &SDL_mouse;
   322     SDL_Mouse *mouse = SDL_GetMouse();
   353 
   323 
   354     return mouse->relative_mode;
   324     return mouse->relative_mode;
   355 }
   325 }
   356 
   326 
   357 SDL_Cursor *
   327 SDL_Cursor *
   358 SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
   328 SDL_CreateCursor(const Uint8 * data, const Uint8 * mask,
   359                  int w, int h, int hot_x, int hot_y)
   329                  int w, int h, int hot_x, int hot_y)
   360 {
   330 {
   361     SDL_Mouse *mouse = &SDL_mouse;
   331     SDL_Mouse *mouse = SDL_GetMouse();
   362     SDL_Surface *surface;
   332     SDL_Surface *surface;
   363     SDL_Cursor *cursor;
   333     SDL_Cursor *cursor;
   364     int x, y;
   334     int x, y;
   365     Uint32 *pixel;
   335     Uint32 *pixel;
   366     Uint8 datab = 0, maskb = 0;
   336     Uint8 datab = 0, maskb = 0;
   422    the video mode and when the SDL window gains the mouse focus.
   392    the video mode and when the SDL window gains the mouse focus.
   423  */
   393  */
   424 void
   394 void
   425 SDL_SetCursor(SDL_Cursor * cursor)
   395 SDL_SetCursor(SDL_Cursor * cursor)
   426 {
   396 {
   427     SDL_Mouse *mouse = &SDL_mouse;
   397     SDL_Mouse *mouse = SDL_GetMouse();
   428 
   398 
   429     /* Set the new cursor */
   399     /* Set the new cursor */
   430     if (cursor) {
   400     if (cursor) {
   431         /* Make sure the cursor is still valid for this mouse */
   401         /* Make sure the cursor is still valid for this mouse */
   432         SDL_Cursor *found;
   402         SDL_Cursor *found;
   456 }
   426 }
   457 
   427 
   458 SDL_Cursor *
   428 SDL_Cursor *
   459 SDL_GetCursor(void)
   429 SDL_GetCursor(void)
   460 {
   430 {
   461     SDL_Mouse *mouse = &SDL_mouse;
   431     SDL_Mouse *mouse = SDL_GetMouse();
   462 
   432 
   463     if (!mouse) {
   433     if (!mouse) {
   464         return NULL;
   434         return NULL;
   465     }
   435     }
   466     return mouse->cur_cursor;
   436     return mouse->cur_cursor;
   467 }
   437 }
   468 
   438 
   469 void
   439 void
   470 SDL_FreeCursor(SDL_Cursor * cursor)
   440 SDL_FreeCursor(SDL_Cursor * cursor)
   471 {
   441 {
   472     SDL_Mouse *mouse = &SDL_mouse;
   442     SDL_Mouse *mouse = SDL_GetMouse();
   473     SDL_Cursor *curr, *prev;
   443     SDL_Cursor *curr, *prev;
   474 
   444 
   475     if (!cursor) {
   445     if (!cursor) {
   476         return;
   446         return;
   477     }
   447     }
   501 }
   471 }
   502 
   472 
   503 int
   473 int
   504 SDL_ShowCursor(int toggle)
   474 SDL_ShowCursor(int toggle)
   505 {
   475 {
   506     SDL_Mouse *mouse = &SDL_mouse;
   476     SDL_Mouse *mouse = SDL_GetMouse();
   507     SDL_bool shown;
   477     SDL_bool shown;
   508 
   478 
   509     if (!mouse) {
   479     if (!mouse) {
   510         return 0;
   480         return 0;
   511     }
   481     }