2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
4 Copyright 2015 Samsung Electronics co., Ltd. All Rights Reserved.
6 This software is provided 'as-is', without any express or implied
7 warranty. In no event will the authors be held liable for any damages
8 arising from the use of this software.
10 Permission is granted to anyone to use this software for any purpose,
11 including commercial applications, and to alter it and redistribute it
12 freely, subject to the following restrictions:
14 1. The origin of this software must not be misrepresented; you must not
15 claim that you wrote the original software. If you use this software
16 in a product, an acknowledgment in the product documentation would be
17 appreciated but is not required.
18 2. Altered source versions must be plainly marked as such, and must not be
19 misrepresented as being the original software.
20 3. This notice may not be removed or altered from any source distribution.
23 #include "../../SDL_internal.h"
34 #include "../../events/SDL_mouse_c.h"
37 #include "SDL_timer.h"
38 #include "SDL_tizenindicator.h"
39 #include "SDL_tizenmouse.h"
40 #include "SDL_tizentouch.h"
42 #include "SDL_tizenvideo.h"
45 struct wl_buffer *buffer;
46 struct wl_surface *surface;
47 SDL_WindowData *win_data;
52 /* Either a preloaded cursor, or one we created ourselves */
53 struct wl_cursor *cursor;
58 wayland_create_tmp_file(off_t size)
60 static const char template[] = "/sdl-shared-XXXXXX";
62 char tmp_path[PATH_MAX];
65 xdg_path = SDL_getenv("XDG_RUNTIME_DIR");
71 SDL_strlcpy(tmp_path, xdg_path, PATH_MAX);
72 SDL_strlcat(tmp_path, template, PATH_MAX);
74 fd = mkostemp(tmp_path, O_CLOEXEC);
78 if (ftruncate(fd, size) < 0) {
87 mouse_buffer_release(void *data, struct wl_buffer *buffer)
91 static const struct wl_buffer_listener mouse_buffer_listener = {
96 create_buffer_from_shm(Tizen_CursorData *d, int width, int height, uint32_t format)
98 struct wl_shm_pool *shm_pool;
100 int stride = width * 4;
101 int size = stride * height;
105 shm_fd = wayland_create_tmp_file(size);
108 fprintf(stderr, "creating mouse cursor buffer failed!\n");
112 d->shm_data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
113 if (d->shm_data == MAP_FAILED) {
115 fprintf (stderr, "mmap () failed\n");
120 shm_pool = wl_shm_create_pool(ecore_wl_shm_get(), shm_fd, size);
121 d->buffer = wl_shm_pool_create_buffer(shm_pool, 0, width, height, stride, format);
122 wl_buffer_add_listener(d->buffer, &mouse_buffer_listener, d);
124 wl_shm_pool_destroy (shm_pool);
131 Tizen_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
135 cursor = calloc(1, sizeof (*cursor));
137 SDL_VideoDevice *vd = SDL_GetVideoDevice ();
138 SDL_Window* window = vd->windows;
140 Tizen_CursorData *data = calloc (1, sizeof (Tizen_CursorData));
141 data->win_data = window->driverdata;
142 cursor->driverdata = (void *) data;
144 /* Assume ARGB8888 */
145 SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
146 SDL_assert(surface->pitch == surface->w * 4);
148 /* Allocate shared memory buffer for this cursor */
149 if (create_buffer_from_shm (data,
152 WL_SHM_FORMAT_XRGB8888) < 0)
154 free (cursor->driverdata);
159 SDL_memcpy(data->shm_data,
161 surface->h * surface->pitch);
163 data->surface = wl_compositor_create_surface(ecore_wl_compositor_get());
167 data->w = surface->w;
168 data->h = surface->h;
176 Tizen_FreeCursor(SDL_Cursor *cursor)
183 d = cursor->driverdata;
185 /* Probably not a cursor we own */
189 if (d->buffer && !d->cursor)
190 wl_buffer_destroy(d->buffer);
193 wl_surface_destroy(d->surface);
195 /* Not sure what's meant to happen to shm_data */
196 free (cursor->driverdata);
201 Tizen_ShowCursor(SDL_Cursor *cursor)
205 Tizen_CursorData *data = cursor->driverdata;
206 SDL_WindowData *win_data = data->win_data;
207 ecore_wl_window_buffer_attach(win_data->window, data->buffer, 0,0);
208 ecore_wl_input_pointer_set(ecore_wl_input_get(), data->surface, data->hot_x, data->hot_y);
212 ecore_wl_input_pointer_set(ecore_wl_input_get(), NULL, 0, 0);
219 Tizen_InitMouse(void)
221 SDL_Mouse *mouse = SDL_GetMouse();
223 mouse->CreateCursor = Tizen_CreateCursor;
224 mouse->FreeCursor = Tizen_FreeCursor;
228 Tizen_FiniMouse(void)
230 /* This effectively assumes that nobody else touches SDL_Mouse which is effectively a singleton */
231 SDL_Mouse *mouse = SDL_GetMouse();
233 /* Free the current cursor if not the same pointer as the default cursor */
234 if (mouse->def_cursor != mouse->cur_cursor)
235 Tizen_FreeCursor (mouse->cur_cursor);
237 Tizen_FreeCursor (mouse->def_cursor);
238 mouse->def_cursor = NULL;
239 mouse->cur_cursor = NULL;
241 mouse->CreateCursor = NULL;
242 mouse->CreateSystemCursor = NULL;
243 mouse->ShowCursor = NULL;
244 mouse->FreeCursor = NULL;
245 mouse->WarpMouse = NULL;
246 mouse->SetRelativeMouseMode = NULL;
249 void _tizen_get_mouseXY(SDL_Window* window, int x, int y, int* retX, int* retY)
251 SDL_WindowData *wind = window->driverdata;
252 if(!wind->support_pre_rotation || wind->rotation==0)
259 switch (wind->rotation)
274 _tizen_cb_event_mousedown_change(void *data, int type, void *event)
276 SDL_VideoDevice *_this = SDL_GetVideoDevice();
278 if (!event) return ECORE_CALLBACK_PASS_ON;
280 Ecore_Event_Mouse_Button *e = event;
283 SDL_WindowData *wind;
285 ew = ecore_wl_window_find(e->window);
286 window = Tizen_FindWindow(_this, ew);
287 wind = window->driverdata;
290 _tizen_get_mouseXY(window, (int)e->x, (int)e->y, &x, &y);
291 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "mouse down (%d x %d)", x, y);
292 if(Tizen_isTouchIndicator(window, wind->rotation, x, y))
294 ModelMatrixTranslateInit(window, wind->rotation);
295 wind->indicator_show = SDL_TRUE;
296 wind->last_indicator_showtime = SDL_GetTicks();
299 SDL_SendMouseMotion(window, 0, 0, x, y);
300 SDL_SendMouseButton(window, 0, SDL_PRESSED, SDL_BUTTON_LEFT);
302 Tizen_OnTouch(_this, 1, e->multi.device, ACTION_POINTER_DOWN, x, y, 1.0f);
304 return ECORE_CALLBACK_PASS_ON;
308 _tizen_cb_event_mouseup_change(void *data, int type, void *event)
310 SDL_VideoDevice *_this = SDL_GetVideoDevice();
311 if (!event) return ECORE_CALLBACK_PASS_ON;
313 Ecore_Event_Mouse_Button *e = event;
317 ew = ecore_wl_window_find(e->window);
318 window = Tizen_FindWindow(_this, ew);
321 _tizen_get_mouseXY(window, (int)e->x, (int)e->y, &x, &y);
322 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "mouse up (%d x %d)", x, y);
323 SDL_SendMouseMotion(window, 0, 0, x, y);
324 SDL_SendMouseButton(window, 0, SDL_RELEASED, SDL_BUTTON_LEFT);
326 Tizen_OnTouch(_this,1,e->multi.device,ACTION_POINTER_UP, x, y, 1.0f);
328 return ECORE_CALLBACK_PASS_ON;
332 _tizen_cb_event_mousemove_change(void *data, int type, void *event)
334 SDL_VideoDevice *_this = SDL_GetVideoDevice();
336 if (!event) return ECORE_CALLBACK_PASS_ON;
338 Ecore_Event_Mouse_Move *e = event;
342 ew = ecore_wl_window_find(e->window);
343 window = Tizen_FindWindow(_this, ew);
346 _tizen_get_mouseXY(window, (int)e->x, (int)e->y, &x, &y);
347 //SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "mouse move (%d x %d)",e->x,e->y);
348 SDL_SendMouseMotion(window, 0, 0, x, y);
350 Tizen_OnTouch(_this,1,e->multi.device,ACTION_POINTER_MOVE, x, y, 1.0f);
352 return ECORE_CALLBACK_PASS_ON;
356 _tizen_cb_event_mouse_in(void *data, int type EINA_UNUSED, void *event)
358 SDL_VideoDevice *_this = SDL_GetVideoDevice();
359 Ecore_Wl_Event_Mouse_In *ev;
364 ew = ecore_wl_window_find(ev->window);
365 window = Tizen_FindWindow(_this, ew);
367 SDL_SetMouseFocus(window);
368 return ECORE_CALLBACK_PASS_ON;
372 _tizen_cb_event_mouse_out(void *data, int type EINA_UNUSED, void *event)
374 SDL_SetMouseFocus(NULL);
375 return ECORE_CALLBACK_PASS_ON;