src/video/mir/SDL_mirmouse.c
changeset 10089 25fda20d0173
parent 9998 f67cf37e9cd4
child 10091 9fccd3acad30
equal deleted inserted replaced
10088:33bee97dbcbb 10089:25fda20d0173
    25 
    25 
    26 #include "../../SDL_internal.h"
    26 #include "../../SDL_internal.h"
    27 
    27 
    28 #if SDL_VIDEO_DRIVER_MIR
    28 #if SDL_VIDEO_DRIVER_MIR
    29 
    29 
       
    30 #include "../../events/SDL_mouse_c.h"
       
    31 #include "../SDL_sysvideo.h"
       
    32 #include "SDL_assert.h"
       
    33 
       
    34 #include "SDL_mirdyn.h"
       
    35 
       
    36 #include "SDL_mirvideo.h"
    30 #include "SDL_mirmouse.h"
    37 #include "SDL_mirmouse.h"
    31 
    38 #include "SDL_mirwindow.h"
    32 #include "../../events/SDL_mouse_c.h"
    39 
    33 #include "SDL_assert.h"
    40 typedef struct
    34 
    41 {
    35 #include "SDL_mirdyn.h"
    42     MirCursorConfiguration* conf;
       
    43     MirBufferStream*        stream;
       
    44 } MIR_Cursor;
    36 
    45 
    37 static SDL_Cursor*
    46 static SDL_Cursor*
    38 MIR_CreateDefaultCursor()
    47 MIR_CreateDefaultCursor()
    39 {
    48 {
    40     SDL_Cursor* cursor;
    49     SDL_Cursor* cursor;
    41 
    50 
    42     cursor = SDL_calloc(1, sizeof(SDL_Cursor));
    51     cursor = SDL_calloc(1, sizeof(SDL_Cursor));
    43     if (cursor) {
    52     if (cursor) {
       
    53 
       
    54         MIR_Cursor* mir_cursor = SDL_calloc(1, sizeof(MIR_Cursor));
       
    55         if (mir_cursor) {
       
    56             mir_cursor->conf   = NULL;
       
    57             mir_cursor->stream = NULL;
       
    58             cursor->driverdata = mir_cursor;
       
    59         }
       
    60         else {
       
    61             SDL_OutOfMemory();
       
    62         }
    44     }
    63     }
    45     else {
    64     else {
    46         SDL_OutOfMemory();
    65         SDL_OutOfMemory();
    47     }
    66     }
    48 
    67 
    49     return cursor;
    68     return cursor;
    50 }
    69 }
    51 
    70 
       
    71 static void
       
    72 CopySurfacePixelsToMirStream(SDL_Surface* surface, MirBufferStream* stream)
       
    73 {
       
    74     char* dest, *pixels;
       
    75     int i, s_w, s_h, r_stride, p_stride, bytes_per_pixel, bytes_per_row;
       
    76 
       
    77     MirGraphicsRegion region;
       
    78     MIR_mir_buffer_stream_get_graphics_region(stream, &region);
       
    79 
       
    80     s_w = surface->w;
       
    81     s_h = surface->h;
       
    82 
       
    83     bytes_per_pixel = surface->format->BytesPerPixel;
       
    84     bytes_per_row   = bytes_per_pixel * s_w;
       
    85 
       
    86     dest = region.vaddr;
       
    87     pixels = (char*)surface->pixels;
       
    88 
       
    89     r_stride = region.stride;
       
    90     p_stride = surface->pitch;
       
    91 
       
    92     for (i = 0; i < s_h; i++)
       
    93     {
       
    94         memcpy(dest, pixels, bytes_per_row);
       
    95         dest   += r_stride;
       
    96         pixels += p_stride;
       
    97     }
       
    98 }
       
    99 
    52 static SDL_Cursor*
   100 static SDL_Cursor*
    53 MIR_CreateCursor(SDL_Surface* sruface, int hot_x, int hot_y)
   101 MIR_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y)
    54 {
   102 {
    55     return MIR_CreateDefaultCursor();
   103     MirCursorConfiguration* conf;
       
   104     MirBufferStream*        stream;
       
   105 
       
   106     int s_w = surface->w;
       
   107     int s_h = surface->h;
       
   108 
       
   109     MIR_Data* mir_data     = (MIR_Data*)SDL_GetVideoDevice()->driverdata;
       
   110     SDL_Cursor* cursor     = MIR_CreateDefaultCursor();
       
   111     MIR_Cursor* mir_cursor = (MIR_Cursor*)cursor->driverdata;
       
   112 
       
   113     stream = MIR_mir_connection_create_buffer_stream_sync(mir_data->connection,
       
   114                                                           s_w, s_h, mir_data->pixel_format,
       
   115                                                           mir_buffer_usage_software);
       
   116 
       
   117     conf = MIR_mir_cursor_configuration_from_buffer_stream(stream, hot_x, hot_y);
       
   118 
       
   119     CopySurfacePixelsToMirStream(surface, stream);
       
   120     MIR_mir_buffer_stream_swap_buffers_sync(stream);
       
   121 
       
   122     mir_cursor->conf   = conf;
       
   123     mir_cursor->stream = stream;
       
   124 
       
   125     return cursor;
    56 }
   126 }
    57 
   127 
    58 static SDL_Cursor*
   128 static SDL_Cursor*
    59 MIR_CreateSystemCursor(SDL_SystemCursor id)
   129 MIR_CreateSystemCursor(SDL_SystemCursor id)
    60 {
   130 {
       
   131     char const* cursor_name = NULL;
       
   132     MirCursorConfiguration* conf;
       
   133     SDL_Cursor* cursor = MIR_CreateDefaultCursor();
       
   134 
    61     switch(id) {
   135     switch(id) {
    62         case SDL_SYSTEM_CURSOR_ARROW:
   136         case SDL_SYSTEM_CURSOR_ARROW:
       
   137             cursor_name = MIR_mir_arrow_cursor_name;
    63             break;
   138             break;
    64         case SDL_SYSTEM_CURSOR_IBEAM:
   139         case SDL_SYSTEM_CURSOR_IBEAM:
       
   140             cursor_name = MIR_mir_caret_cursor_name;
    65             break;
   141             break;
    66         case SDL_SYSTEM_CURSOR_WAIT:
   142         case SDL_SYSTEM_CURSOR_WAIT:
       
   143             cursor_name = MIR_mir_busy_cursor_name;
    67             break;
   144             break;
    68         case SDL_SYSTEM_CURSOR_CROSSHAIR:
   145         case SDL_SYSTEM_CURSOR_CROSSHAIR:
       
   146             /* Unsupported */
       
   147             cursor_name = MIR_mir_arrow_cursor_name;
    69             break;
   148             break;
    70         case SDL_SYSTEM_CURSOR_WAITARROW:
   149         case SDL_SYSTEM_CURSOR_WAITARROW:
       
   150             cursor_name = MIR_mir_busy_cursor_name;
    71             break;
   151             break;
    72         case SDL_SYSTEM_CURSOR_SIZENWSE:
   152         case SDL_SYSTEM_CURSOR_SIZENWSE:
       
   153             cursor_name = MIR_mir_omnidirectional_resize_cursor_name;
    73             break;
   154             break;
    74         case SDL_SYSTEM_CURSOR_SIZENESW:
   155         case SDL_SYSTEM_CURSOR_SIZENESW:
       
   156             cursor_name = MIR_mir_omnidirectional_resize_cursor_name;
    75             break;
   157             break;
    76         case SDL_SYSTEM_CURSOR_SIZEWE:
   158         case SDL_SYSTEM_CURSOR_SIZEWE:
       
   159             cursor_name = MIR_mir_horizontal_resize_cursor_name;
    77             break;
   160             break;
    78         case SDL_SYSTEM_CURSOR_SIZENS:
   161         case SDL_SYSTEM_CURSOR_SIZENS:
       
   162             cursor_name = MIR_mir_vertical_resize_cursor_name;
    79             break;
   163             break;
    80         case SDL_SYSTEM_CURSOR_SIZEALL:
   164         case SDL_SYSTEM_CURSOR_SIZEALL:
       
   165             cursor_name = MIR_mir_omnidirectional_resize_cursor_name;
    81             break;
   166             break;
    82         case SDL_SYSTEM_CURSOR_NO:
   167         case SDL_SYSTEM_CURSOR_NO:
       
   168             /* Unsupported */
       
   169             cursor_name = MIR_mir_closed_hand_cursor_name;
    83             break;
   170             break;
    84         case SDL_SYSTEM_CURSOR_HAND:
   171         case SDL_SYSTEM_CURSOR_HAND:
       
   172             cursor_name = MIR_mir_open_hand_cursor_name;
    85             break;
   173             break;
    86         default:
   174         default:
    87             SDL_assert(0);
   175             SDL_assert(0);
    88             return NULL;
   176             return NULL;
    89     }
   177     }
    90 
   178 
    91     return MIR_CreateDefaultCursor();
   179     conf = MIR_mir_cursor_configuration_from_name(cursor_name);
       
   180 
       
   181     cursor->driverdata = conf;
       
   182 
       
   183     return cursor;
    92 }
   184 }
    93 
   185 
    94 static void
   186 static void
    95 MIR_FreeCursor(SDL_Cursor* cursor)
   187 MIR_FreeCursor(SDL_Cursor* cursor)
    96 {
   188 {
    97     if (cursor)
   189     if (cursor) {
    98       SDL_free(cursor);
   190 
       
   191         if (cursor->driverdata) {
       
   192             MIR_Cursor* mir_cursor = (MIR_Cursor*)cursor->driverdata;
       
   193 
       
   194             if (mir_cursor->conf)
       
   195                 MIR_mir_cursor_configuration_destroy(mir_cursor->conf);
       
   196             if (mir_cursor->stream)
       
   197                 MIR_mir_buffer_stream_release_sync(mir_cursor->stream);
       
   198 
       
   199             SDL_free(mir_cursor);
       
   200         }
       
   201 
       
   202         SDL_free(cursor);
       
   203     }
    99 }
   204 }
   100 
   205 
   101 static int
   206 static int
   102 MIR_ShowCursor(SDL_Cursor* cursor)
   207 MIR_ShowCursor(SDL_Cursor* cursor)
   103 {
   208 {
       
   209     MIR_Data* mir_data      = (MIR_Data*)SDL_GetVideoDevice()->driverdata;
       
   210     MIR_Window* mir_window  = mir_data->current_window;
       
   211 
       
   212     if (cursor && cursor->driverdata) {
       
   213         if (mir_window && MIR_mir_surface_is_valid(mir_window->surface)) {
       
   214             MIR_Cursor* mir_cursor = (MIR_Cursor*)cursor->driverdata;
       
   215 
       
   216             if (mir_cursor->conf) {
       
   217                 MIR_mir_wait_for(MIR_mir_surface_configure_cursor(mir_window->surface, mir_cursor->conf));
       
   218             }
       
   219         }
       
   220     }
       
   221     else if(mir_window && MIR_mir_surface_is_valid(mir_window->surface)) {
       
   222         MIR_mir_wait_for(MIR_mir_surface_configure_cursor(mir_window->surface, NULL));
       
   223     }
       
   224     
   104     return 0;
   225     return 0;
   105 }
   226 }
   106 
   227 
   107 static void
   228 static void
   108 MIR_WarpMouse(SDL_Window* window, int x, int y)
   229 MIR_WarpMouse(SDL_Window* window, int x, int y)