2 Simple DirectMedia Layer
3 Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
23 Contributed by Brandon Schaefer, <brandon.schaefer@canonical.com>
26 #include "../../SDL_internal.h"
28 #if SDL_VIDEO_DRIVER_MIR
30 #include "../../events/SDL_mouse_c.h"
31 #include "../SDL_sysvideo.h"
32 #include "SDL_assert.h"
34 #include "SDL_mirdyn.h"
36 #include "SDL_mirvideo.h"
37 #include "SDL_mirmouse.h"
38 #include "SDL_mirwindow.h"
42 MirCursorConfiguration* conf;
43 MirBufferStream* stream;
48 MIR_CreateDefaultCursor()
52 cursor = SDL_calloc(1, sizeof(SDL_Cursor));
55 MIR_Cursor* mir_cursor = SDL_calloc(1, sizeof(MIR_Cursor));
57 mir_cursor->conf = NULL;
58 mir_cursor->stream = NULL;
59 mir_cursor->name = NULL;
60 cursor->driverdata = mir_cursor;
76 CopySurfacePixelsToMirStream(SDL_Surface* surface, MirBufferStream* stream)
79 int i, s_w, s_h, r_stride, p_stride, bytes_per_pixel, bytes_per_row;
81 MirGraphicsRegion region;
82 MIR_mir_buffer_stream_get_graphics_region(stream, ®ion);
87 bytes_per_pixel = surface->format->BytesPerPixel;
88 bytes_per_row = bytes_per_pixel * s_w;
91 pixels = (char*)surface->pixels;
93 r_stride = region.stride;
94 p_stride = surface->pitch;
96 for (i = 0; i < s_h; i++)
98 SDL_memcpy(dest, pixels, bytes_per_row);
105 MIR_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y)
107 MirCursorConfiguration* conf;
108 MirBufferStream* stream;
110 int s_w = surface->w;
111 int s_h = surface->h;
113 MIR_Data* mir_data = (MIR_Data*)SDL_GetVideoDevice()->driverdata;
114 SDL_Cursor* cursor = MIR_CreateDefaultCursor();
115 MIR_Cursor* mir_cursor;
121 mir_cursor = (MIR_Cursor*)cursor->driverdata;
123 stream = MIR_mir_connection_create_buffer_stream_sync(mir_data->connection,
124 s_w, s_h, mir_data->pixel_format,
125 mir_buffer_usage_software);
127 conf = MIR_mir_cursor_configuration_from_buffer_stream(stream, hot_x, hot_y);
129 CopySurfacePixelsToMirStream(surface, stream);
130 MIR_mir_buffer_stream_swap_buffers_sync(stream);
132 mir_cursor->conf = conf;
133 mir_cursor->stream = stream;
139 MIR_CreateSystemCursor(SDL_SystemCursor id)
141 char const* cursor_name = NULL;
143 MIR_Cursor* mir_cursor;
146 case SDL_SYSTEM_CURSOR_ARROW:
147 cursor_name = MIR_mir_arrow_cursor_name;
149 case SDL_SYSTEM_CURSOR_IBEAM:
150 cursor_name = MIR_mir_caret_cursor_name;
152 case SDL_SYSTEM_CURSOR_WAIT:
153 cursor_name = MIR_mir_busy_cursor_name;
155 case SDL_SYSTEM_CURSOR_CROSSHAIR:
157 cursor_name = MIR_mir_arrow_cursor_name;
159 case SDL_SYSTEM_CURSOR_WAITARROW:
160 cursor_name = MIR_mir_busy_cursor_name;
162 case SDL_SYSTEM_CURSOR_SIZENWSE:
163 cursor_name = MIR_mir_omnidirectional_resize_cursor_name;
165 case SDL_SYSTEM_CURSOR_SIZENESW:
166 cursor_name = MIR_mir_omnidirectional_resize_cursor_name;
168 case SDL_SYSTEM_CURSOR_SIZEWE:
169 cursor_name = MIR_mir_horizontal_resize_cursor_name;
171 case SDL_SYSTEM_CURSOR_SIZENS:
172 cursor_name = MIR_mir_vertical_resize_cursor_name;
174 case SDL_SYSTEM_CURSOR_SIZEALL:
175 cursor_name = MIR_mir_omnidirectional_resize_cursor_name;
177 case SDL_SYSTEM_CURSOR_NO:
179 cursor_name = MIR_mir_closed_hand_cursor_name;
181 case SDL_SYSTEM_CURSOR_HAND:
182 cursor_name = MIR_mir_open_hand_cursor_name;
189 cursor = MIR_CreateDefaultCursor();
194 mir_cursor = (MIR_Cursor*)cursor->driverdata;
195 mir_cursor->name = cursor_name;
201 MIR_FreeCursor(SDL_Cursor* cursor)
205 if (cursor->driverdata) {
206 MIR_Cursor* mir_cursor = (MIR_Cursor*)cursor->driverdata;
208 if (mir_cursor->conf)
209 MIR_mir_cursor_configuration_destroy(mir_cursor->conf);
210 if (mir_cursor->stream)
211 MIR_mir_buffer_stream_release_sync(mir_cursor->stream);
213 SDL_free(mir_cursor);
221 MIR_ShowCursor(SDL_Cursor* cursor)
223 MIR_Data* mir_data = (MIR_Data*)SDL_GetVideoDevice()->driverdata;
224 MIR_Window* mir_window = mir_data->current_window;
226 if (cursor && cursor->driverdata) {
227 if (mir_window && MIR_mir_window_is_valid(mir_window->window)) {
228 MIR_Cursor* mir_cursor = (MIR_Cursor*)cursor->driverdata;
230 if (mir_cursor->name != NULL) {
231 MirWindowSpec* spec = MIR_mir_create_window_spec(mir_data->connection);
232 MIR_mir_window_spec_set_cursor_name(spec, mir_cursor->name);
233 MIR_mir_window_apply_spec(mir_window->window, spec);
234 MIR_mir_window_spec_release(spec);
237 if (mir_cursor->conf) {
238 MIR_mir_window_configure_cursor(mir_window->window, mir_cursor->conf);
242 else if(mir_window && MIR_mir_window_is_valid(mir_window->window)) {
243 MIR_mir_window_configure_cursor(mir_window->window, NULL);
250 MIR_WarpMouse(SDL_Window* window, int x, int y)
256 MIR_WarpMouseGlobal(int x, int y)
258 return SDL_Unsupported();
262 MIR_SetRelativeMouseMode(SDL_bool enabled)
267 /* TODO Actually implement the cursor, need to wait for mir support */
271 SDL_Mouse* mouse = SDL_GetMouse();
273 mouse->CreateCursor = MIR_CreateCursor;
274 mouse->ShowCursor = MIR_ShowCursor;
275 mouse->FreeCursor = MIR_FreeCursor;
276 mouse->WarpMouse = MIR_WarpMouse;
277 mouse->WarpMouseGlobal = MIR_WarpMouseGlobal;
278 mouse->CreateSystemCursor = MIR_CreateSystemCursor;
279 mouse->SetRelativeMouseMode = MIR_SetRelativeMouseMode;
281 SDL_SetDefaultCursor(MIR_CreateDefaultCursor());
289 #endif /* SDL_VIDEO_DRIVER_MIR */
291 /* vi: set ts=4 sw=4 expandtab: */