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"
29 #include "../../events/SDL_mouse_c.h"
31 #include "SDL_tizenmouse.h"
33 #include "SDL_tizentouch.h"
42 struct wl_buffer *buffer;
43 struct wl_surface *surface;
44 SDL_WindowData *win_data;
49 /* Either a preloaded cursor, or one we created ourselves */
50 struct wl_cursor *cursor;
55 wayland_create_tmp_file(off_t size)
57 static const char template[] = "/sdl-shared-XXXXXX";
59 char tmp_path[PATH_MAX];
62 xdg_path = SDL_getenv("XDG_RUNTIME_DIR");
68 SDL_strlcpy(tmp_path, xdg_path, PATH_MAX);
69 SDL_strlcat(tmp_path, template, PATH_MAX);
71 fd = mkostemp(tmp_path, O_CLOEXEC);
75 if (ftruncate(fd, size) < 0) {
84 mouse_buffer_release(void *data, struct wl_buffer *buffer)
88 static const struct wl_buffer_listener mouse_buffer_listener = {
93 create_buffer_from_shm(Tizen_CursorData *d, int width, int height, uint32_t format)
95 //SDL_VideoDevice *vd = SDL_GetVideoDevice();
96 //SDL_VideoData *data = (SDL_VideoData *) vd->driverdata;
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 fprintf(stderr, "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 fprintf (stderr, "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
233 * 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;
250 _tizen_cb_event_mousedown_change(void *data, int type, void *event)
252 SDL_VideoDevice *_this = SDL_GetVideoDevice();
254 if (!event) return ECORE_CALLBACK_PASS_ON;
256 Ecore_Event_Mouse_Button *e = event;
257 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "mouse down (%d x %d)",e->x,e->y);
258 if(_this->current_glwin->indicator_type == 0)
260 if(e->x > _this->current_glwin->x && e->x < _this->current_glwin->x + _this->current_glwin->w &&
261 e->y > _this->current_glwin->y && e->y < _this->current_glwin->y + 52)
263 _this->current_glwin->indicator_show = SDL_TRUE;
264 _this->current_glwin->last_indicator_showtime = SDL_GetTicks();
267 else if(_this->current_glwin->indicator_type == 1)
269 if(e->x > _this->current_glwin->x && e->x < _this->current_glwin->x + 52 &&
270 e->y > _this->current_glwin->y && e->y < _this->current_glwin->y + _this->current_glwin->h)
272 _this->current_glwin->indicator_show = SDL_TRUE;
273 _this->current_glwin->last_indicator_showtime = SDL_GetTicks();
276 else if(_this->current_glwin->indicator_type == 2)
278 if(e->x > _this->current_glwin->x && e->x < _this->current_glwin->x + _this->current_glwin->w &&
279 e->y > _this->current_glwin->y + _this->current_glwin->h -52 && e->y < _this->current_glwin->y + _this->current_glwin->h)
281 _this->current_glwin->indicator_show = SDL_TRUE;
282 _this->current_glwin->last_indicator_showtime = SDL_GetTicks();
285 else if(_this->current_glwin->indicator_type == 3)
287 if(e->x > _this->current_glwin->x + _this->current_glwin->w -52 && e->x < _this->current_glwin->x + _this->current_glwin->w &&
288 e->y > _this->current_glwin->y && e->y < _this->current_glwin->y + _this->current_glwin->h)
290 _this->current_glwin->indicator_show = SDL_TRUE;
291 _this->current_glwin->last_indicator_showtime = SDL_GetTicks();
295 SDL_SendMouseMotion(_this->current_glwin, 0, 0, e->x, e->y);
296 SDL_SendMouseButton(_this->current_glwin, 0, SDL_PRESSED, SDL_BUTTON_LEFT);
298 Tizen_OnTouch(_this,1,e->multi.device,ACTION_POINTER_DOWN,e->x,e->y,1.0f);
300 return ECORE_CALLBACK_PASS_ON;
304 _tizen_cb_event_mouseup_change(void *data, int type, void *event)
306 SDL_VideoDevice *_this = SDL_GetVideoDevice();
307 if (!event) return ECORE_CALLBACK_PASS_ON;
309 Ecore_Event_Mouse_Button *e = event;
310 SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "mouse up (%d x %d)",e->x,e->y);
311 SDL_SendMouseMotion(_this->current_glwin, 0, 0, e->x, e->y);
312 SDL_SendMouseButton(_this->current_glwin, 0, SDL_RELEASED, SDL_BUTTON_LEFT);
314 Tizen_OnTouch(_this,1,e->multi.device,ACTION_POINTER_UP,e->x,e->y,1.0f);
316 return ECORE_CALLBACK_PASS_ON;
320 _tizen_cb_event_mousemove_change(void *data, int type, void *event)
322 SDL_VideoDevice *_this = SDL_GetVideoDevice();
324 if (!event) return ECORE_CALLBACK_PASS_ON;
326 Ecore_Event_Mouse_Move *e = event;
327 //SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "mouse move (%d x %d)",e->x,e->y);
328 SDL_SendMouseMotion(_this->current_glwin, 0, 0, e->x, e->y);
330 Tizen_OnTouch(_this,1,e->multi.device,ACTION_POINTER_MOVE,e->x,e->y,1.0f);
332 return ECORE_CALLBACK_PASS_ON;