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_tizenmouse.h"
39 #include "SDL_tizentouch.h"
41 #include "SDL_tizenvideo.h"
44 struct wl_buffer *buffer;
45 struct wl_surface *surface;
46 SDL_WindowData *win_data;
51 /* Either a preloaded cursor, or one we created ourselves */
52 struct wl_cursor *cursor;
57 wayland_create_tmp_file(off_t size)
59 static const char template[] = "/sdl-shared-XXXXXX";
61 char tmp_path[PATH_MAX];
64 xdg_path = SDL_getenv("XDG_RUNTIME_DIR");
70 SDL_strlcpy(tmp_path, xdg_path, PATH_MAX);
71 SDL_strlcat(tmp_path, template, PATH_MAX);
73 fd = mkostemp(tmp_path, O_CLOEXEC);
77 if (ftruncate(fd, size) < 0) {
86 mouse_buffer_release(void *data, struct wl_buffer *buffer)
90 static const struct wl_buffer_listener mouse_buffer_listener = {
95 create_buffer_from_shm(Tizen_CursorData *d, int width, int height, uint32_t format)
97 struct wl_shm_pool *shm_pool;
99 int stride = width * 4;
100 int size = stride * height;
104 shm_fd = wayland_create_tmp_file(size);
107 SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "creating mouse cursor buffer failed!\n");
111 d->shm_data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, shm_fd, 0);
112 if (d->shm_data == MAP_FAILED) {
114 SDL_LogError(SDL_LOG_CATEGORY_ASSERT, "mmap () failed\n");
119 shm_pool = wl_shm_create_pool(ecore_wl_shm_get(), shm_fd, size);
120 d->buffer = wl_shm_pool_create_buffer(shm_pool, 0, width, height, stride, format);
121 wl_buffer_add_listener(d->buffer, &mouse_buffer_listener, d);
123 wl_shm_pool_destroy (shm_pool);
130 Tizen_CreateCursor(SDL_Surface *surface, int hot_x, int hot_y)
134 cursor = calloc(1, sizeof (*cursor));
136 SDL_VideoDevice *vd = SDL_GetVideoDevice ();
137 SDL_Window* window = vd->windows;
139 Tizen_CursorData *data = calloc (1, sizeof (Tizen_CursorData));
140 data->win_data = window->driverdata;
141 cursor->driverdata = (void *) data;
143 /* Assume ARGB8888 */
144 SDL_assert(surface->format->format == SDL_PIXELFORMAT_ARGB8888);
145 SDL_assert(surface->pitch == surface->w * 4);
147 /* Allocate shared memory buffer for this cursor */
148 if (create_buffer_from_shm (data,
151 WL_SHM_FORMAT_XRGB8888) < 0)
153 free (cursor->driverdata);
158 SDL_memcpy(data->shm_data,
160 surface->h * surface->pitch);
162 data->surface = wl_compositor_create_surface(ecore_wl_compositor_get());
166 data->w = surface->w;
167 data->h = surface->h;
175 Tizen_FreeCursor(SDL_Cursor *cursor)
182 d = cursor->driverdata;
184 /* Probably not a cursor we own */
188 if (d->buffer && !d->cursor)
189 wl_buffer_destroy(d->buffer);
192 wl_surface_destroy(d->surface);
194 /* Not sure what's meant to happen to shm_data */
195 free (cursor->driverdata);
200 Tizen_ShowCursor(SDL_Cursor *cursor)
204 Tizen_CursorData *data = cursor->driverdata;
205 SDL_WindowData *win_data = data->win_data;
206 ecore_wl_window_buffer_attach(win_data->window, data->buffer, 0,0);
207 ecore_wl_input_pointer_set(ecore_wl_input_get(), data->surface, data->hot_x, data->hot_y);
211 ecore_wl_input_pointer_set(ecore_wl_input_get(), NULL, 0, 0);
218 Tizen_InitMouse(void)
220 SDL_Mouse *mouse = SDL_GetMouse();
222 mouse->CreateCursor = Tizen_CreateCursor;
223 mouse->FreeCursor = Tizen_FreeCursor;
227 Tizen_FiniMouse(void)
229 /* This effectively assumes that nobody else touches SDL_Mouse which is effectively a singleton */
230 SDL_Mouse *mouse = SDL_GetMouse();
232 /* Free the current cursor if not the same pointer as the default cursor */
233 if (mouse->def_cursor != mouse->cur_cursor)
234 Tizen_FreeCursor (mouse->cur_cursor);
236 Tizen_FreeCursor (mouse->def_cursor);
237 mouse->def_cursor = NULL;
238 mouse->cur_cursor = NULL;
240 mouse->CreateCursor = NULL;
241 mouse->CreateSystemCursor = NULL;
242 mouse->ShowCursor = NULL;
243 mouse->FreeCursor = NULL;
244 mouse->WarpMouse = NULL;
245 mouse->SetRelativeMouseMode = NULL;
248 void _tizen_get_mouseXY(SDL_Window* window, int x, int y, int* retX, int* retY)
250 SDL_WindowData *wind = window->driverdata;
251 if((wind->output_rotation + wind->rotation) % 360 == 0)
258 switch (wind->rotation)
273 _tizen_cb_event_mousedown_change(void *data, int type, void *event)
275 SDL_VideoDevice *_this = SDL_GetVideoDevice();
277 if (!event) return ECORE_CALLBACK_PASS_ON;
279 Ecore_Event_Mouse_Button *e = event;
283 ew = ecore_wl_window_find(e->window);
284 window = Tizen_FindWindow(_this, ew);
287 _tizen_get_mouseXY(window, (int)e->x, (int)e->y, &x, &y);
289 SDL_VideoData* videoData = SDL_GetVideoDevice()->driverdata;
290 SDL_WindowData *wind = window->driverdata;
291 if(x >= 0 && x <= window->w && y >= 0 && y <= videoData->indicator_height)
293 if(!videoData->indicator_visible && wind->support_indicator)
295 videoData->indicator_parent_id = wind->id;
296 _tizen_ecore_ipc_client_send(OP_INDICATOR_SHOW, wind->rotation, wind->g_res_id, 0);
300 SDL_SendMouseMotion(window, 0, 0, x, y);
301 SDL_SendMouseButton(window, 0, SDL_PRESSED, SDL_BUTTON_LEFT);
303 Tizen_OnTouch(_this, 1, e->multi.device, ACTION_POINTER_DOWN, x, y, 1.0f);
305 return ECORE_CALLBACK_PASS_ON;
309 _tizen_cb_event_mouseup_change(void *data, int type, void *event)
311 SDL_VideoDevice *_this = SDL_GetVideoDevice();
312 if (!event) return ECORE_CALLBACK_PASS_ON;
314 Ecore_Event_Mouse_Button *e = event;
318 ew = ecore_wl_window_find(e->window);
319 window = Tizen_FindWindow(_this, ew);
321 SDL_VideoData* videoData = SDL_GetVideoDevice()->driverdata;
322 SDL_WindowData *wind = window->driverdata;
324 if(videoData->indicator_on && wind->support_indicator && videoData->indicator_visible)
326 _tizen_quickpanel_on(window->driverdata);
330 _tizen_get_mouseXY(window, (int)e->x, (int)e->y, &x, &y);
331 SDL_SendMouseMotion(window, 0, 0, x, y);
332 SDL_SendMouseButton(window, 0, SDL_RELEASED, SDL_BUTTON_LEFT);
334 Tizen_OnTouch(_this,1,e->multi.device,ACTION_POINTER_UP, x, y, 1.0f);
336 return ECORE_CALLBACK_PASS_ON;
340 _tizen_cb_event_mousemove_change(void *data, int type, void *event)
342 SDL_VideoDevice *_this = SDL_GetVideoDevice();
344 if (!event) return ECORE_CALLBACK_PASS_ON;
346 Ecore_Event_Mouse_Move *e = event;
350 ew = ecore_wl_window_find(e->window);
351 window = Tizen_FindWindow(_this, ew);
354 _tizen_get_mouseXY(window, (int)e->x, (int)e->y, &x, &y);
355 SDL_SendMouseMotion(window, 0, 0, x, y);
357 Tizen_OnTouch(_this,1,e->multi.device,ACTION_POINTER_MOVE, x, y, 1.0f);
359 return ECORE_CALLBACK_PASS_ON;
363 _tizen_cb_event_mouse_in(void *data, int type EINA_UNUSED, void *event)
365 SDL_VideoDevice *_this = SDL_GetVideoDevice();
366 Ecore_Wl_Event_Mouse_In *ev;
371 ew = ecore_wl_window_find(ev->window);
372 window = Tizen_FindWindow(_this, ew);
374 SDL_SetMouseFocus(window);
375 return ECORE_CALLBACK_PASS_ON;
379 _tizen_cb_event_mouse_out(void *data, int type EINA_UNUSED, void *event)
381 SDL_SetMouseFocus(NULL);
382 return ECORE_CALLBACK_PASS_ON;