1 2 3 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
/*
* Copyright (C) 2023 Igalia S.L.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WPEEvent.h"
#include <variant>
#include <wtf/FastMalloc.h>
#include <wtf/StdLibExtras.h>
#include <wtf/glib/GRefPtr.h>
struct WPEEventPointerButton {
WPEModifiers modifiers { static_cast<WPEModifiers>(0) };
unsigned button { 0 };
unsigned pressCount { 0 };
double x { 0 };
double y { 0 };
};
struct WPEEventPointerMove {
WPEModifiers modifiers { static_cast<WPEModifiers>(0) };
double x { 0 };
double y { 0 };
double deltaX { 0 };
double deltaY { 0 };
};
struct WPEEventScroll {
WPEModifiers modifiers { static_cast<WPEModifiers>(0) };
double deltaX { 0 };
double deltaY { 0 };
double x { 0 };
double y { 0 };
bool hasPreciseDeltas { false };
bool isStop { false };
};
struct WPEEventKeyboard {
WPEModifiers modifiers { static_cast<WPEModifiers>(0) };
unsigned keycode;
unsigned keyval;
};
struct WPEEventTouch {
WPEModifiers modifiers { static_cast<WPEModifiers>(0) };
uint32_t sequenceID { 0 };
double x { 0 };
double y { 0 };
};
/**
* WPEEvent: (ref-func wpe_event_ref) (unref-func wpe_event_unref)
*
*/
struct _WPEEvent {
WTF_MAKE_STRUCT_FAST_ALLOCATED;
GRefPtr<WPEView> view;
WPEEventType type { WPE_EVENT_NONE };
WPEInputSource source { WPE_INPUT_SOURCE_MOUSE };
guint32 time { 0 };
std::variant<WPEEventPointerButton, WPEEventPointerMove, WPEEventScroll, WPEEventKeyboard, WPEEventTouch> variant;
int referenceCount { 1 };
};
G_DEFINE_BOXED_TYPE(WPEEvent, wpe_event, wpe_event_ref, wpe_event_unref)
/**
* wpe_event_ref:
* @event: a #WPEEvent
*
* Atomically increments the reference count of @event by one.
*
* This function is MT-safe and may be called from any thread.
*
* Returns: The passed in #WPEEvent
*/
WPEEvent* wpe_event_ref(WPEEvent* event)
{
g_atomic_int_inc(&event->referenceCount);
return event;
}
/**
* wpe_event_unref:
* @event: a #WPEEvent
*
* Atomically decrements the reference count of @event by one.
*
* If the reference count drops to 0, all memory allocated by the #WPEEvent is
* released. This function is MT-safe and may be called from any thread.
*/
void wpe_event_unref(WPEEvent* event)
{
if (g_atomic_int_dec_and_test(&event->referenceCount))
delete event;
}
/**
* wpe_event_get_event_type:
* @event: a #WPEEvent
*
* Get the #WPEEventType of @event
*
* Returns: a #WPEEventType
*/
WPEEventType wpe_event_get_event_type(WPEEvent* event)
{
g_return_val_if_fail(event, WPE_EVENT_NONE);
return event->type;
}
/**
* wpe_event_get_view:
* @event: a #WPEEvent
*
* Get the #WPEView associated to @event
*
* Returns: (transfer none): a #WPEView
*/
WPEView* wpe_event_get_view(WPEEvent* event)
{
g_return_val_if_fail(event, nullptr);
return event->view.get();
}
/**
* wpe_event_get_input_source:
* @event: a #WPEEvent
*
* Get the device input source of @event
*
* Returns: a #WPEInputSource
*/
WPEInputSource wpe_event_get_input_source(WPEEvent* event)
{
g_return_val_if_fail(event, WPE_INPUT_SOURCE_MOUSE);
return event->source;
}
/**
* wpe_event_get_time:
* @event: a #WPEEvent
*
* Get the time stamp of @event
*
* Returns: a time stamp or 0
*/
guint32 wpe_event_get_time(WPEEvent* event)
{
g_return_val_if_fail(event, 0);
return event->time;
}
/**
* wpe_event_get_modifiers:
* @event: a #WPEEvent
*
* Get the modifiers of @event. If the #WPEEvent doesn't have
* modifiers state 0 is returned.
*
* Returns: a #WPEModifiers
*/
WPEModifiers wpe_event_get_modifiers(WPEEvent* event)
{
g_return_val_if_fail(event, static_cast<WPEModifiers>(0));
return WTF::switchOn(event->variant,
[](const WPEEventPointerButton& button) { return button.modifiers; },
[](const WPEEventPointerMove& move) { return move.modifiers; },
[](const WPEEventScroll& scroll) { return scroll.modifiers; },
[](const WPEEventKeyboard& keyboard) { return keyboard.modifiers; },
[](const WPEEventTouch& touch) { return touch.modifiers; },
[](const auto&) { return static_cast<WPEModifiers>(0); }
);
}
/**
* wpe_event_get_position:
* @event: a #WPEEvent
* @x: (out): location to store x coordinate
* @y: (out): location to store y coordinate
*
* Get the position of @event. If the #WPEEvent doesn't have
* a position, %FALSE is returned.
*
* Returns: %TRUE if position is returned in @x and @y,
* or %FALSE if @event doesn't have a positon
*/
gboolean wpe_event_get_position(WPEEvent* event, double* x, double* y)
{
g_return_val_if_fail(event, FALSE);
return WTF::switchOn(event->variant,
[&x, &y](const WPEEventPointerButton& button) -> gboolean {
if (x)
*x = button.x;
if (y)
*y = button.y;
return TRUE;
},
[&x, &y](const WPEEventPointerMove& move) -> gboolean {
if (x)
*x = move.x;
if (y)
*y = move.y;
return TRUE;
},
[&x, &y](const WPEEventScroll& scroll) -> gboolean {
if (x)
*x = scroll.x;
if (y)
*y = scroll.y;
return TRUE;
},
[&x, &y](const WPEEventTouch& touch) -> gboolean {
if (x)
*x = touch.x;
if (y)
*y = touch.y;
return TRUE;
},
[](const auto&) -> gboolean { return FALSE; }
);
}
/**
* wpe_event_pointer_button_new:
* @type: a #WPEEventType (%WPE_EVENT_POINTER_DOWN or %WPE_EVENT_POINTER_UP)
* @view: a #WPEView
* @source: a #WPEInputSource
* @time: the event timestamp
* @modifiers: a #WPEModifiers
* @button: the button number
* @x: the x coordinate of the pointer
* @y: the y coordinate of the pointer
* @press_count: the number of button presses
*
* Create a #WPEEvent for a pointer button press or release.
*
* Returns: (transfer full): a new allocated #WPEEvent.
*/
WPEEvent* wpe_event_pointer_button_new(WPEEventType type, WPEView* view, WPEInputSource source, guint32 time, WPEModifiers modifiers, guint button, double x, double y, guint pressCount)
{
g_return_val_if_fail(type == WPE_EVENT_POINTER_DOWN || type == WPE_EVENT_POINTER_UP, nullptr);
g_return_val_if_fail(WPE_IS_VIEW(view), nullptr);
g_return_val_if_fail(!pressCount || type == WPE_EVENT_POINTER_DOWN, nullptr);
return new _WPEEvent { view, type, source, time, WPEEventPointerButton { modifiers, button, pressCount, x, y }, 1 };
}
/**
* wpe_event_pointer_button_get_button:
* @event: a #WPEEvent
*
* Get the button number of @event.
* Note that @event must be a pointer button event (%WPE_EVENT_POINTER_DOWN or %WPE_EVENT_POINTER_UP).
*
* Returns: the button of @event
*/
guint wpe_event_pointer_button_get_button(WPEEvent* event)
{
g_return_val_if_fail(event, 0);
g_return_val_if_fail(event->type == WPE_EVENT_POINTER_DOWN || event->type == WPE_EVENT_POINTER_UP, 0);
return std::get<WPEEventPointerButton>(event->variant).button;
}
/**
* wpe_event_pointer_button_get_press_count:
* @event: a #WPEEvent
*
* Get the numbbr of button presses for @event
* Note that @event must be a pointer button press event (%WPE_EVENT_POINTER_DOWN).
*
* Returns: the press count of @event
*/
guint wpe_event_pointer_button_get_press_count(WPEEvent* event)
{
g_return_val_if_fail(event, 0);
g_return_val_if_fail(event->type == WPE_EVENT_POINTER_DOWN, 0);
return std::get<WPEEventPointerButton>(event->variant).pressCount;
}
/**
* wpe_event_pointer_move_new:
* @type: a #WPEEventType (%WPE_EVENT_POINTER_MOVE, %WPE_EVENT_POINTER_ENTER or %WPE_EVENT_POINTER_LEAVE)
* @view: a #WPEView
* @source: a #WPEInputSource
* @time: the event timestamp
* @modifiers: a #WPEModifiers
* @x: the x coordinate of the pointer
* @y: the y coordinate of the pointer
* @delta_x: the x coordainte movement delta
* @delta_y: the y coordinate movement delta
*
* Create a #WPEEvent for a pointer move.
*
* Returns: (transfer full): a new allocated #WPEEvent.
*/
WPEEvent* wpe_event_pointer_move_new(WPEEventType type, WPEView* view, WPEInputSource source, guint32 time, WPEModifiers modifiers, double x, double y, double deltaX, double deltaY)
{
g_return_val_if_fail(type == WPE_EVENT_POINTER_MOVE || type == WPE_EVENT_POINTER_ENTER || type == WPE_EVENT_POINTER_LEAVE, nullptr);
g_return_val_if_fail(WPE_IS_VIEW(view), nullptr);
return new _WPEEvent { view, type, source, time, WPEEventPointerMove { modifiers, x, y, deltaX, deltaY }, 1 };
}
/**
* wpe_event_pointer_move_get_delta:
* @event: a #WPEEvent
* @delta_x: (out): location to store the x coordinate delta
* @delta_y: (out): location to store the y coordinate delta
*
* Get the movement delta of @event
* Note that @event must be a pointer move event (%WPE_EVENT_POINTER_MOVE, %WPE_EVENT_POINTER_ENTER or %WPE_EVENT_POINTER_LEAVE)
*/
void wpe_event_pointer_move_get_delta(WPEEvent* event, double* deltaX, double* deltaY)
{
g_return_if_fail(event);
g_return_if_fail(event->type == WPE_EVENT_POINTER_MOVE || event->type == WPE_EVENT_POINTER_ENTER || event->type == WPE_EVENT_POINTER_LEAVE);
const auto& move = std::get<WPEEventPointerMove>(event->variant);
if (deltaX)
*deltaX = move.deltaX;
if (deltaY)
*deltaY = move.deltaY;
}
/**
* wpe_event_scroll_new:
* @view: a #WPEView
* @source: a #WPEInputSource
* @time: the event timestamp
* @modifiers: a #WPEModifiers
* @delta_x: delta on the X axis
* @delta_y: delta on the Y axis
* @precise_deltas: whether it has precise deltas
* @is_stop: whether it's a stop event
* @x: the x coordinate of the pointer, or 0 if not a pointing device
* @y: the y coordinate of the pointer, or 0 if not a pointing device
*
* Create a #WPEEvent for a scroll.
*
* Returns: (transfer full): a new allocated #WPEEvent.
*/
WPEEvent* wpe_event_scroll_new(WPEView* view, WPEInputSource source, guint32 time, WPEModifiers modifiers, double deltaX, double deltaY, gboolean preciseDeltas, gboolean isStop, double x, double y)
{
g_return_val_if_fail(WPE_IS_VIEW(view), nullptr);
return new _WPEEvent { view, WPE_EVENT_SCROLL, source, time, WPEEventScroll { modifiers, deltaX, deltaY, x, y, !!preciseDeltas, !!isStop }, 1 };
}
/**
* wpe_event_scroll_get_deltas:
* @event: a #WPEEvent
* @delta_x: (out): location to store the x coordinate delta
* @delta_y: (out): location to store the y coordinate delta
*
* Get the scroll deltas of @event
* Note that @event must be a scroll event (%WPE_EVENT_SCROLL)
*/
void wpe_event_scroll_get_deltas(WPEEvent* event, double* deltaX, double* deltaY)
{
g_return_if_fail(event);
g_return_if_fail(event->type == WPE_EVENT_SCROLL);
const auto& scroll = std::get<WPEEventScroll>(event->variant);
if (deltaX)
*deltaX = scroll.deltaX;
if (deltaY)
*deltaY = scroll.deltaY;
}
/**
* wpe_event_scroll_has_precise_deltas:
* @event: a #WPEEvent
*
* Get whether @event has precise scroll deltas.
* Note that @event must be a scroll event (%WPE_EVENT_SCROLL)
*
* Returns: %TRUE if @event has precise scroll deltas, or %FALSE otherwise
*/
gboolean wpe_event_scroll_has_precise_deltas(WPEEvent* event)
{
g_return_val_if_fail(event, FALSE);
g_return_val_if_fail(event->type == WPE_EVENT_SCROLL, FALSE);
return std::get<WPEEventScroll>(event->variant).hasPreciseDeltas;
}
/**
* wpe_event_scroll_is_stop:
* @event: a #WPEEvent
*
* Get whether @event is a stop scroll event
*
* Returns: %TRUE if @event is a stop scroll event, or %FALSE otherwise
*/
gboolean wpe_event_scroll_is_stop(WPEEvent* event)
{
g_return_val_if_fail(event, FALSE);
g_return_val_if_fail(event->type == WPE_EVENT_SCROLL, FALSE);
return std::get<WPEEventScroll>(event->variant).isStop;
}
/**
* wpe_event_keyboard_new:
* @type: a #WPEEventType (%WPE_EVENT_KEYBOARD_KEY_DOWN or %WPE_EVENT_KEYBOARD_KEY_UP)
* @view: a #WPEView
* @source: a #WPEInputSource
* @time: the event timestamp
* @modifiers: a #WPEModifiers
* @keycode: the hardware key code
* @keyval: the translated key value
*
* Create a #WPEEvent for a keyboard key press or release
*
* Returns: (transfer full): a new allocated #WPEEvent.
*/
WPEEvent* wpe_event_keyboard_new(WPEEventType type, WPEView* view, WPEInputSource source, guint32 time, WPEModifiers modifiers, guint keycode, guint keyval)
{
g_return_val_if_fail(type == WPE_EVENT_KEYBOARD_KEY_DOWN || type == WPE_EVENT_KEYBOARD_KEY_UP, nullptr);
g_return_val_if_fail(WPE_IS_VIEW(view), nullptr);
return new _WPEEvent { view, type, source, time, WPEEventKeyboard { modifiers, keycode, keyval }, 1 };
}
/**
* wpe_event_keyboard_get_keycode:
* @event: a #WPEEvent
*
* Get the hardware key code of @event
* Note that @event must be a keyboard event (%WPE_EVENT_KEYBOARD_KEY_DOWN or %WPE_EVENT_KEYBOARD_KEY_UP)
*
* Returns: the hardware key code of @event
*/
guint wpe_event_keyboard_get_keycode(WPEEvent* event)
{
g_return_val_if_fail(event, 0);
g_return_val_if_fail(event->type == WPE_EVENT_KEYBOARD_KEY_DOWN || event->type == WPE_EVENT_KEYBOARD_KEY_UP, 0);
return std::get<WPEEventKeyboard>(event->variant).keycode;
}
/**
* wpe_event_keyboard_get_keyval:
* @event: a #WPEEvent
*
* Get the translated key value of @event
* Note that @event must be a keyboard event (%WPE_EVENT_KEYBOARD_KEY_DOWN or %WPE_EVENT_KEYBOARD_KEY_UP)
*
* Returns: the translated key value of @event
*/
guint wpe_event_keyboard_get_keyval(WPEEvent* event)
{
g_return_val_if_fail(event, 0);
g_return_val_if_fail(event->type == WPE_EVENT_KEYBOARD_KEY_DOWN || event->type == WPE_EVENT_KEYBOARD_KEY_UP, 0);
return std::get<WPEEventKeyboard>(event->variant).keyval;
}
/**
* wpe_event_touch_new:
* @type: a #WPEEventType (%WPE_EVENT_TOUCH_DOWN, %WPE_EVENT_TOUCH_UP, %WPE_EVENT_TOUCH_MOVER or %WPE_EVENT_TOUCH_CANCEL)
* @view: a #WPEView
* @source: a #WPEInputSource
* @time: the event timestamp
* @modifiers: a #WPEModifiers
* @sequence_id: the sequence event identifier
* @x: the x coordinate of the touch
* @y: the y coordinate of the touch
*
* Create a #WPEEvent for a touch
*
* Returns: (transfer full): a new allocated #WPEEvent.
*/
WPEEvent* wpe_event_touch_new(WPEEventType type, WPEView* view, WPEInputSource source, guint32 time, WPEModifiers modifiers, guint32 sequenceID, double x, double y)
{
g_return_val_if_fail(type == WPE_EVENT_TOUCH_DOWN || type == WPE_EVENT_TOUCH_UP || type == WPE_EVENT_TOUCH_MOVE || type == WPE_EVENT_TOUCH_CANCEL, nullptr);
g_return_val_if_fail(WPE_IS_VIEW(view), nullptr);
return new _WPEEvent { view, type, source, time, WPEEventTouch { modifiers, sequenceID, x, y }, 1 };
}
/**
* wpe_event_touch_get_sequence_id:
* @event: a #WPEEvent
*
* Get the sequence identifier of @event.
* Note that @event must be a touch event (%WPE_EVENT_TOUCH_DOWN, %WPE_EVENT_TOUCH_UP, %WPE_EVENT_TOUCH_MOVER or %WPE_EVENT_TOUCH_CANCEL)
*
* Returns: the event sequence identifier
*/
guint32 wpe_event_touch_get_sequence_id(WPEEvent* event)
{
g_return_val_if_fail(event, 0);
g_return_val_if_fail(event->type == WPE_EVENT_TOUCH_DOWN || event->type == WPE_EVENT_TOUCH_UP || event->type == WPE_EVENT_TOUCH_MOVE || event->type == WPE_EVENT_TOUCH_CANCEL, 0);
return std::get<WPEEventTouch>(event->variant).sequenceID;
}
|