src/video/wayland/SDL_waylandtouch.c
changeset 8082 5b83ad3f01ac
child 8104 2e4f1bd21196
equal deleted inserted replaced
8081:402b8c313246 8082:5b83ad3f01ac
       
     1 /*
       
     2   Simple DirectMedia Layer
       
     3   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
       
     4 
       
     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.
       
     8 
       
     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:
       
    12 
       
    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.
       
    20 */
       
    21 
       
    22 /* Contributed by Thomas Perl <thomas.perl@jollamobile.com> */
       
    23 
       
    24 #include "SDL_config.h"
       
    25 
       
    26 #ifdef SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH
       
    27 
       
    28 #include "SDL_waylandtouch.h"
       
    29 #include "SDL_log.h"
       
    30 #include "../../events/SDL_touch_c.h"
       
    31 
       
    32 struct SDL_WaylandTouch {
       
    33     struct qt_touch_extension *touch_extension;
       
    34 };
       
    35 
       
    36 
       
    37 /**
       
    38  * Qt TouchPointState
       
    39  * adapted from qtbase/src/corelib/global/qnamespace.h
       
    40  **/
       
    41 enum QtWaylandTouchPointState {
       
    42     QtWaylandTouchPointPressed    = 0x01,
       
    43     QtWaylandTouchPointMoved      = 0x02,
       
    44     /*
       
    45     Never sent by the server:
       
    46     QtWaylandTouchPointStationary = 0x04,
       
    47     */
       
    48     QtWaylandTouchPointReleased   = 0x08,
       
    49 };
       
    50 
       
    51 static void
       
    52 touch_handle_touch(void *data,
       
    53         struct qt_touch_extension *qt_touch_extension,
       
    54         uint32_t time,
       
    55         uint32_t id,
       
    56         uint32_t state,
       
    57         int32_t x,
       
    58         int32_t y,
       
    59         int32_t normalized_x,
       
    60         int32_t normalized_y,
       
    61         int32_t width,
       
    62         int32_t height,
       
    63         uint32_t pressure,
       
    64         int32_t velocity_x,
       
    65         int32_t velocity_y,
       
    66         uint32_t flags,
       
    67         struct wl_array *rawdata)
       
    68 {
       
    69     /**
       
    70      * Event is assembled in QtWayland in TouchExtensionGlobal::postTouchEvent
       
    71      * (src/compositor/wayland_wrapper/qwltouch.cpp)
       
    72      **/
       
    73 
       
    74     float FIXED_TO_FLOAT = 1. / 10000.;
       
    75     float xf = FIXED_TO_FLOAT * x;
       
    76     float yf = FIXED_TO_FLOAT * y;
       
    77 
       
    78     float PRESSURE_TO_FLOAT = 1. / 255.;
       
    79     float pressuref = PRESSURE_TO_FLOAT * pressure;
       
    80 
       
    81     uint32_t touchState = state & 0xFFFF;
       
    82     /*
       
    83     Other fields that are sent by the server (qwltouch.cpp),
       
    84     but not used at the moment can be decoded in this way:
       
    85 
       
    86     uint32_t sentPointCount = state >> 16;
       
    87     uint32_t touchFlags = flags & 0xFFFF;
       
    88     uint32_t capabilities = flags >> 16;
       
    89     */
       
    90 
       
    91     SDL_TouchID deviceId = 0;
       
    92     if (!SDL_GetTouch(deviceId)) {
       
    93         if (SDL_AddTouch(deviceId, "qt_touch_extension") < 0) {
       
    94              SDL_Log("error: can't add touch %s, %d", __FILE__, __LINE__);
       
    95         }
       
    96     }
       
    97 
       
    98     switch (touchState) {
       
    99         case QtWaylandTouchPointPressed:
       
   100         case QtWaylandTouchPointReleased:
       
   101             SDL_SendTouch(deviceId, (SDL_FingerID)id,
       
   102                     (touchState == QtWaylandTouchPointPressed) ? SDL_TRUE : SDL_FALSE,
       
   103                     xf, yf, pressuref);
       
   104             break;
       
   105         case QtWaylandTouchPointMoved:
       
   106             SDL_SendTouchMotion(deviceId, (SDL_FingerID)id, xf, yf, pressuref);
       
   107             break;
       
   108         default:
       
   109             /* Should not happen */
       
   110             break;
       
   111     }
       
   112 }
       
   113 
       
   114 static void
       
   115 touch_handle_configure(void *data,
       
   116         struct qt_touch_extension *qt_touch_extension,
       
   117         uint32_t flags)
       
   118 {
       
   119 }
       
   120 
       
   121 /* wayland-qt-touch-extension.c BEGINS */
       
   122 
       
   123 static const struct qt_touch_extension_listener touch_listener = {
       
   124     touch_handle_touch,
       
   125     touch_handle_configure,
       
   126 };
       
   127 
       
   128 static const struct wl_interface *qt_touch_extension_types[] = {
       
   129     NULL,
       
   130     NULL,
       
   131     NULL,
       
   132     NULL,
       
   133     NULL,
       
   134     NULL,
       
   135     NULL,
       
   136     NULL,
       
   137     NULL,
       
   138     NULL,
       
   139     NULL,
       
   140     NULL,
       
   141     NULL,
       
   142     NULL,
       
   143 };
       
   144 
       
   145 static const struct wl_message qt_touch_extension_requests[] = {
       
   146     { "dummy", "", qt_touch_extension_types + 0 },
       
   147 };
       
   148 
       
   149 static const struct wl_message qt_touch_extension_events[] = {
       
   150     { "touch", "uuuiiiiiiuiiua", qt_touch_extension_types + 0 },
       
   151     { "configure", "u", qt_touch_extension_types + 0 },
       
   152 };
       
   153 
       
   154 WL_EXPORT const struct wl_interface qt_touch_extension_interface = {
       
   155     "qt_touch_extension", 1,
       
   156     1, qt_touch_extension_requests,
       
   157     2, qt_touch_extension_events,
       
   158 };
       
   159 
       
   160 /* wayland-qt-touch-extension.c ENDS */
       
   161 
       
   162 /* wayland-qt-windowmanager.c BEGINS */
       
   163 static const struct wl_interface *qt_windowmanager_types[] = {
       
   164     NULL,
       
   165     NULL,
       
   166 };
       
   167 
       
   168 static const struct wl_message qt_windowmanager_requests[] = {
       
   169     { "open_url", "us", qt_windowmanager_types + 0 },
       
   170 };
       
   171 
       
   172 static const struct wl_message qt_windowmanager_events[] = {
       
   173     { "hints", "i", qt_windowmanager_types + 0 },
       
   174     { "quit", "", qt_windowmanager_types + 0 },
       
   175 };
       
   176 
       
   177 WL_EXPORT const struct wl_interface qt_windowmanager_interface = {
       
   178     "qt_windowmanager", 1,
       
   179     1, qt_windowmanager_requests,
       
   180     2, qt_windowmanager_events,
       
   181 };
       
   182 /* wayland-qt-windowmanager.c ENDS */
       
   183 
       
   184 /* wayland-qt-surface-extension.c BEGINS */
       
   185 extern const struct wl_interface qt_extended_surface_interface;
       
   186 extern const struct wl_interface wl_surface_interface;
       
   187 
       
   188 static const struct wl_interface *qt_surface_extension_types[] = {
       
   189     NULL,
       
   190     NULL,
       
   191     &qt_extended_surface_interface,
       
   192     &wl_surface_interface,
       
   193 };
       
   194 
       
   195 static const struct wl_message qt_surface_extension_requests[] = {
       
   196     { "get_extended_surface", "no", qt_surface_extension_types + 2 },
       
   197 };
       
   198 
       
   199 WL_EXPORT const struct wl_interface qt_surface_extension_interface = {
       
   200     "qt_surface_extension", 1,
       
   201     1, qt_surface_extension_requests,
       
   202     0, NULL,
       
   203 };
       
   204 
       
   205 static const struct wl_message qt_extended_surface_requests[] = {
       
   206     { "update_generic_property", "sa", qt_surface_extension_types + 0 },
       
   207     { "set_content_orientation", "i", qt_surface_extension_types + 0 },
       
   208     { "set_window_flags", "i", qt_surface_extension_types + 0 },
       
   209 };
       
   210 
       
   211 static const struct wl_message qt_extended_surface_events[] = {
       
   212     { "onscreen_visibility", "i", qt_surface_extension_types + 0 },
       
   213     { "set_generic_property", "sa", qt_surface_extension_types + 0 },
       
   214     { "close", "", qt_surface_extension_types + 0 },
       
   215 };
       
   216 
       
   217 WL_EXPORT const struct wl_interface qt_extended_surface_interface = {
       
   218     "qt_extended_surface", 1,
       
   219     3, qt_extended_surface_requests,
       
   220     3, qt_extended_surface_events,
       
   221 };
       
   222 
       
   223 /* wayland-qt-surface-extension.c ENDS */
       
   224 
       
   225 void
       
   226 Wayland_touch_create(SDL_VideoData *data, uint32_t id)
       
   227 {
       
   228     if (data->touch) {
       
   229         Wayland_touch_destroy(data);
       
   230     }
       
   231 
       
   232     data->touch = malloc(sizeof(struct SDL_WaylandTouch));
       
   233 
       
   234     struct SDL_WaylandTouch *touch = data->touch;
       
   235     touch->touch_extension = wl_registry_bind(data->registry, id, &qt_touch_extension_interface, 1);
       
   236     qt_touch_extension_add_listener(touch->touch_extension, &touch_listener, data);
       
   237 }
       
   238 
       
   239 void
       
   240 Wayland_touch_destroy(SDL_VideoData *data)
       
   241 {
       
   242     if (data->touch) {
       
   243         struct SDL_WaylandTouch *touch = data->touch;
       
   244         if (touch->touch_extension) {
       
   245             qt_touch_extension_destroy(touch->touch_extension);
       
   246         }
       
   247 
       
   248         free(data->touch);
       
   249         data->touch = NULL;
       
   250     }
       
   251 }
       
   252 
       
   253 #endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */