Skip to content

Latest commit

 

History

History
303 lines (245 loc) · 8.17 KB

SDL_mirmouse.c

File metadata and controls

303 lines (245 loc) · 8.17 KB
 
1
2
/*
Simple DirectMedia Layer
Jan 2, 2017
Jan 2, 2017
3
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/*
Contributed by Brandon Schaefer, <brandon.schaefer@canonical.com>
*/
#include "../../SDL_internal.h"
#if SDL_VIDEO_DRIVER_MIR
#include "../../events/SDL_mouse_c.h"
Feb 21, 2016
Feb 21, 2016
31
#include "../SDL_sysvideo.h"
32
33
34
35
#include "SDL_assert.h"
#include "SDL_mirdyn.h"
Feb 21, 2016
Feb 21, 2016
36
37
38
39
40
41
42
43
#include "SDL_mirvideo.h"
#include "SDL_mirmouse.h"
#include "SDL_mirwindow.h"
typedef struct
{
MirCursorConfiguration* conf;
MirBufferStream* stream;
Feb 27, 2017
Feb 27, 2017
44
char const* name;
Feb 21, 2016
Feb 21, 2016
45
46
} MIR_Cursor;
47
48
49
50
51
52
53
static SDL_Cursor*
MIR_CreateDefaultCursor()
{
SDL_Cursor* cursor;
cursor = SDL_calloc(1, sizeof(SDL_Cursor));
if (cursor) {
Feb 21, 2016
Feb 21, 2016
54
55
56
57
58
MIR_Cursor* mir_cursor = SDL_calloc(1, sizeof(MIR_Cursor));
if (mir_cursor) {
mir_cursor->conf = NULL;
mir_cursor->stream = NULL;
Feb 27, 2017
Feb 27, 2017
59
mir_cursor->name = NULL;
Feb 21, 2016
Feb 21, 2016
60
61
62
63
cursor->driverdata = mir_cursor;
}
else {
SDL_OutOfMemory();
Feb 22, 2016
Feb 22, 2016
64
65
SDL_free(cursor);
cursor = NULL;
Feb 21, 2016
Feb 21, 2016
66
}
67
68
69
70
71
72
73
74
}
else {
SDL_OutOfMemory();
}
return cursor;
}
Feb 21, 2016
Feb 21, 2016
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
static void
CopySurfacePixelsToMirStream(SDL_Surface* surface, MirBufferStream* stream)
{
char* dest, *pixels;
int i, s_w, s_h, r_stride, p_stride, bytes_per_pixel, bytes_per_row;
MirGraphicsRegion region;
MIR_mir_buffer_stream_get_graphics_region(stream, &region);
s_w = surface->w;
s_h = surface->h;
bytes_per_pixel = surface->format->BytesPerPixel;
bytes_per_row = bytes_per_pixel * s_w;
dest = region.vaddr;
pixels = (char*)surface->pixels;
r_stride = region.stride;
p_stride = surface->pitch;
for (i = 0; i < s_h; i++)
{
Feb 24, 2016
Feb 24, 2016
98
SDL_memcpy(dest, pixels, bytes_per_row);
Feb 21, 2016
Feb 21, 2016
99
100
101
102
103
dest += r_stride;
pixels += p_stride;
}
}
Feb 21, 2016
Feb 21, 2016
105
MIR_CreateCursor(SDL_Surface* surface, int hot_x, int hot_y)
Feb 21, 2016
Feb 21, 2016
107
108
109
110
111
112
113
114
MirCursorConfiguration* conf;
MirBufferStream* stream;
int s_w = surface->w;
int s_h = surface->h;
MIR_Data* mir_data = (MIR_Data*)SDL_GetVideoDevice()->driverdata;
SDL_Cursor* cursor = MIR_CreateDefaultCursor();
Feb 22, 2016
Feb 22, 2016
115
116
117
118
119
120
121
MIR_Cursor* mir_cursor;
if (!cursor) {
return NULL;
}
mir_cursor = (MIR_Cursor*)cursor->driverdata;
Feb 21, 2016
Feb 21, 2016
122
123
124
125
126
127
128
129
130
131
132
133
134
135
stream = MIR_mir_connection_create_buffer_stream_sync(mir_data->connection,
s_w, s_h, mir_data->pixel_format,
mir_buffer_usage_software);
conf = MIR_mir_cursor_configuration_from_buffer_stream(stream, hot_x, hot_y);
CopySurfacePixelsToMirStream(surface, stream);
MIR_mir_buffer_stream_swap_buffers_sync(stream);
mir_cursor->conf = conf;
mir_cursor->stream = stream;
return cursor;
136
137
138
139
140
}
static SDL_Cursor*
MIR_CreateSystemCursor(SDL_SystemCursor id)
{
Feb 21, 2016
Feb 21, 2016
141
char const* cursor_name = NULL;
May 6, 2017
May 6, 2017
142
SDL_Cursor* cursor;
Mar 4, 2017
Mar 4, 2017
143
MIR_Cursor* mir_cursor;
Feb 21, 2016
Feb 21, 2016
144
145
146
switch(id) {
case SDL_SYSTEM_CURSOR_ARROW:
Feb 21, 2016
Feb 21, 2016
147
cursor_name = MIR_mir_arrow_cursor_name;
148
149
break;
case SDL_SYSTEM_CURSOR_IBEAM:
Feb 21, 2016
Feb 21, 2016
150
cursor_name = MIR_mir_caret_cursor_name;
151
152
break;
case SDL_SYSTEM_CURSOR_WAIT:
Feb 21, 2016
Feb 21, 2016
153
cursor_name = MIR_mir_busy_cursor_name;
154
155
break;
case SDL_SYSTEM_CURSOR_CROSSHAIR:
Feb 21, 2016
Feb 21, 2016
156
157
/* Unsupported */
cursor_name = MIR_mir_arrow_cursor_name;
158
159
break;
case SDL_SYSTEM_CURSOR_WAITARROW:
Feb 21, 2016
Feb 21, 2016
160
cursor_name = MIR_mir_busy_cursor_name;
161
162
break;
case SDL_SYSTEM_CURSOR_SIZENWSE:
Feb 21, 2016
Feb 21, 2016
163
cursor_name = MIR_mir_omnidirectional_resize_cursor_name;
164
165
break;
case SDL_SYSTEM_CURSOR_SIZENESW:
Feb 21, 2016
Feb 21, 2016
166
cursor_name = MIR_mir_omnidirectional_resize_cursor_name;
167
168
break;
case SDL_SYSTEM_CURSOR_SIZEWE:
Feb 21, 2016
Feb 21, 2016
169
cursor_name = MIR_mir_horizontal_resize_cursor_name;
170
171
break;
case SDL_SYSTEM_CURSOR_SIZENS:
Feb 21, 2016
Feb 21, 2016
172
cursor_name = MIR_mir_vertical_resize_cursor_name;
173
174
break;
case SDL_SYSTEM_CURSOR_SIZEALL:
Feb 21, 2016
Feb 21, 2016
175
cursor_name = MIR_mir_omnidirectional_resize_cursor_name;
176
177
break;
case SDL_SYSTEM_CURSOR_NO:
Feb 21, 2016
Feb 21, 2016
178
179
/* Unsupported */
cursor_name = MIR_mir_closed_hand_cursor_name;
180
181
break;
case SDL_SYSTEM_CURSOR_HAND:
Feb 21, 2016
Feb 21, 2016
182
cursor_name = MIR_mir_open_hand_cursor_name;
183
184
185
186
187
188
break;
default:
SDL_assert(0);
return NULL;
}
May 6, 2017
May 6, 2017
189
190
191
192
193
194
cursor = MIR_CreateDefaultCursor();
if (!cursor) {
return NULL;
}
mir_cursor = (MIR_Cursor*)cursor->driverdata;
Feb 27, 2017
Feb 27, 2017
195
mir_cursor->name = cursor_name;
Feb 21, 2016
Feb 21, 2016
196
197
return cursor;
198
199
200
201
202
}
static void
MIR_FreeCursor(SDL_Cursor* cursor)
{
Feb 21, 2016
Feb 21, 2016
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
if (cursor) {
if (cursor->driverdata) {
MIR_Cursor* mir_cursor = (MIR_Cursor*)cursor->driverdata;
if (mir_cursor->conf)
MIR_mir_cursor_configuration_destroy(mir_cursor->conf);
if (mir_cursor->stream)
MIR_mir_buffer_stream_release_sync(mir_cursor->stream);
SDL_free(mir_cursor);
}
SDL_free(cursor);
}
218
219
220
221
222
}
static int
MIR_ShowCursor(SDL_Cursor* cursor)
{
Feb 21, 2016
Feb 21, 2016
223
224
225
226
MIR_Data* mir_data = (MIR_Data*)SDL_GetVideoDevice()->driverdata;
MIR_Window* mir_window = mir_data->current_window;
if (cursor && cursor->driverdata) {
Feb 27, 2017
Feb 27, 2017
227
if (mir_window && MIR_mir_window_is_valid(mir_window->window)) {
Feb 21, 2016
Feb 21, 2016
228
229
MIR_Cursor* mir_cursor = (MIR_Cursor*)cursor->driverdata;
Feb 27, 2017
Feb 27, 2017
230
231
232
233
234
235
236
if (mir_cursor->name != NULL) {
MirWindowSpec* spec = MIR_mir_create_window_spec(mir_data->connection);
MIR_mir_window_spec_set_cursor_name(spec, mir_cursor->name);
MIR_mir_window_apply_spec(mir_window->window, spec);
MIR_mir_window_spec_release(spec);
}
Feb 21, 2016
Feb 21, 2016
237
if (mir_cursor->conf) {
Feb 27, 2017
Feb 27, 2017
238
MIR_mir_window_configure_cursor(mir_window->window, mir_cursor->conf);
Feb 21, 2016
Feb 21, 2016
239
240
241
}
}
}
Feb 27, 2017
Feb 27, 2017
242
243
else if(mir_window && MIR_mir_window_is_valid(mir_window->window)) {
MIR_mir_window_configure_cursor(mir_window->window, NULL);
Feb 21, 2016
Feb 21, 2016
244
}
Feb 27, 2017
Feb 27, 2017
245
246
247
248
249
250
251
252
253
254
return 0;
}
static void
MIR_WarpMouse(SDL_Window* window, int x, int y)
{
SDL_Unsupported();
}
Jul 18, 2015
Jul 18, 2015
255
static int
256
257
MIR_WarpMouseGlobal(int x, int y)
{
Jul 18, 2015
Jul 18, 2015
258
return SDL_Unsupported();
259
260
261
262
263
}
static int
MIR_SetRelativeMouseMode(SDL_bool enabled)
{
Jun 7, 2016
Jun 7, 2016
264
return 0;
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
}
/* TODO Actually implement the cursor, need to wait for mir support */
void
MIR_InitMouse()
{
SDL_Mouse* mouse = SDL_GetMouse();
mouse->CreateCursor = MIR_CreateCursor;
mouse->ShowCursor = MIR_ShowCursor;
mouse->FreeCursor = MIR_FreeCursor;
mouse->WarpMouse = MIR_WarpMouse;
mouse->WarpMouseGlobal = MIR_WarpMouseGlobal;
mouse->CreateSystemCursor = MIR_CreateSystemCursor;
mouse->SetRelativeMouseMode = MIR_SetRelativeMouseMode;
SDL_SetDefaultCursor(MIR_CreateDefaultCursor());
}
void
MIR_FiniMouse()
{
SDL_Mouse* mouse = SDL_GetMouse();
MIR_FreeCursor(mouse->def_cursor);
mouse->def_cursor = NULL;
mouse->CreateCursor = NULL;
mouse->ShowCursor = NULL;
mouse->FreeCursor = NULL;
mouse->WarpMouse = NULL;
mouse->CreateSystemCursor = NULL;
mouse->SetRelativeMouseMode = NULL;
}
#endif /* SDL_VIDEO_DRIVER_MIR */
/* vi: set ts=4 sw=4 expandtab: */