2011-12-22 15:19:25 +00:00
|
|
|
/*
|
|
|
|
* \brief Input event structure
|
|
|
|
* \author Norman Feske
|
|
|
|
* \date 2006-08-16
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2017-02-20 12:23:52 +00:00
|
|
|
* Copyright (C) 2006-2017 Genode Labs GmbH
|
2011-12-22 15:19:25 +00:00
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
2017-02-20 12:23:52 +00:00
|
|
|
* under the terms of the GNU Affero General Public License version 3.
|
2011-12-22 15:19:25 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _INCLUDE__INPUT__EVENT_H_
|
|
|
|
#define _INCLUDE__INPUT__EVENT_H_
|
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
#include <base/output.h>
|
2013-09-06 12:43:12 +00:00
|
|
|
#include <input/keycodes.h>
|
2018-04-20 12:27:45 +00:00
|
|
|
#include <util/geometry.h>
|
|
|
|
#include <util/utf8.h>
|
2013-09-06 12:43:12 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
namespace Input {
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
typedef Genode::Codepoint Codepoint;
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
struct Touch_id { int value; };
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
/*
|
|
|
|
* Event attributes
|
|
|
|
*/
|
|
|
|
struct Press { Keycode key; };
|
|
|
|
struct Press_char { Keycode key; Codepoint codepoint; };
|
|
|
|
struct Release { Keycode key; };
|
|
|
|
struct Wheel { int x, y; };
|
|
|
|
struct Focus_enter { };
|
|
|
|
struct Focus_leave { };
|
|
|
|
struct Hover_leave { };
|
|
|
|
struct Absolute_motion { int x, y; };
|
|
|
|
struct Relative_motion { int x, y; };
|
|
|
|
struct Touch { Touch_id id; float x, y; };
|
|
|
|
struct Touch_release { Touch_id id; };
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
class Event;
|
|
|
|
}
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
class Input::Event
|
|
|
|
{
|
|
|
|
private:
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
enum Type { INVALID, PRESS, RELEASE, REL_MOTION, ABS_MOTION, WHEEL,
|
|
|
|
FOCUS_ENTER, FOCUS_LEAVE, HOVER_LEAVE, TOUCH, TOUCH_RELEASE };
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
Type _type = INVALID;
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
struct Attr
|
|
|
|
{
|
|
|
|
union {
|
|
|
|
Press_char press;
|
|
|
|
Release release;
|
|
|
|
Wheel wheel;
|
|
|
|
Absolute_motion abs_motion;
|
|
|
|
Relative_motion rel_motion;
|
|
|
|
Touch touch;
|
|
|
|
Touch_release touch_release;
|
|
|
|
};
|
|
|
|
} _attr { };
|
|
|
|
|
|
|
|
static bool _valid(Keycode key) { return key > KEY_RESERVED && key < KEY_MAX; }
|
2015-03-04 20:12:14 +00:00
|
|
|
|
|
|
|
/**
|
2018-04-20 12:27:45 +00:00
|
|
|
* Return point representation of attribute 'a'
|
2015-03-04 20:12:14 +00:00
|
|
|
*/
|
2018-04-20 12:27:45 +00:00
|
|
|
template <typename R, typename T>
|
|
|
|
static Genode::Point<R> _xy(T const &a) { return Genode::Point<R>(a.x, a.y); }
|
2017-01-30 14:50:04 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
public:
|
2017-01-30 14:50:04 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default constructor creates an invalid event
|
2018-04-20 12:27:45 +00:00
|
|
|
*
|
|
|
|
* This constructor can be used for creating an array of events.
|
2017-01-30 14:50:04 +00:00
|
|
|
*/
|
|
|
|
Event() { }
|
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
/*
|
|
|
|
* Constructors for creating input events of various types
|
|
|
|
*
|
|
|
|
* These constructors can be used implicitly for the given attribute
|
|
|
|
* types for assignments or for passing an event as return value.
|
2017-01-30 14:50:04 +00:00
|
|
|
*/
|
2018-04-20 12:27:45 +00:00
|
|
|
Event(Press_char arg) : _type(PRESS) { _attr.press = arg; }
|
|
|
|
Event(Press arg) : Event(Press_char{arg.key, Codepoint{INVALID}}) { }
|
|
|
|
Event(Release arg) : _type(RELEASE) { _attr.release = arg; }
|
|
|
|
Event(Relative_motion arg) : _type(REL_MOTION) { _attr.rel_motion = arg; }
|
|
|
|
Event(Absolute_motion arg) : _type(ABS_MOTION) { _attr.abs_motion = arg; }
|
|
|
|
Event(Wheel arg) : _type(WHEEL) { _attr.wheel = arg; }
|
|
|
|
Event(Focus_enter) : _type(FOCUS_ENTER) { }
|
|
|
|
Event(Focus_leave) : _type(FOCUS_LEAVE) { }
|
|
|
|
Event(Hover_leave) : _type(HOVER_LEAVE) { }
|
|
|
|
Event(Touch arg) : _type(TOUCH) { _attr.touch = arg; }
|
|
|
|
Event(Touch_release arg) : _type(TOUCH_RELEASE) { _attr.touch_release = arg; }
|
|
|
|
|
|
|
|
|
|
|
|
/************************************
|
|
|
|
** Methods for handling the event **
|
|
|
|
************************************/
|
|
|
|
|
|
|
|
bool valid() const { return _type != INVALID; }
|
|
|
|
bool press() const { return _type == PRESS; }
|
|
|
|
bool release() const { return _type == RELEASE; }
|
|
|
|
bool absolute_motion() const { return _type == ABS_MOTION; }
|
|
|
|
bool relative_motion() const { return _type == REL_MOTION; }
|
|
|
|
bool wheel() const { return _type == WHEEL; }
|
|
|
|
bool focus_enter() const { return _type == FOCUS_ENTER; }
|
|
|
|
bool focus_leave() const { return _type == FOCUS_LEAVE; }
|
|
|
|
bool hover_leave() const { return _type == HOVER_LEAVE; }
|
|
|
|
bool touch() const { return _type == TOUCH; }
|
|
|
|
bool touch_release() const { return _type == TOUCH_RELEASE; }
|
|
|
|
|
|
|
|
bool key_press(Keycode key) const
|
|
|
|
{
|
|
|
|
return press() && _attr.press.key == key;
|
|
|
|
}
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
bool key_release(Keycode key) const
|
|
|
|
{
|
|
|
|
return release() && _attr.release.key == key;
|
|
|
|
}
|
2017-01-30 14:50:04 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
template <typename FN>
|
|
|
|
void handle_press(FN const &fn) const
|
2017-11-14 16:21:04 +00:00
|
|
|
{
|
2018-04-20 12:27:45 +00:00
|
|
|
if (press() && _valid(_attr.press.key))
|
|
|
|
fn(_attr.press.key, _attr.press.codepoint);
|
|
|
|
}
|
2017-11-14 16:21:04 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
template <typename FN>
|
|
|
|
void handle_repeat(FN const &fn) const
|
|
|
|
{
|
|
|
|
if (key_press(KEY_UNKNOWN) && _attr.press.codepoint.value)
|
|
|
|
fn(_attr.press.codepoint);
|
2017-11-14 16:21:04 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
template <typename FN>
|
|
|
|
void handle_release(FN const &fn) const
|
|
|
|
{
|
|
|
|
if (release() && _valid(_attr.release.key))
|
|
|
|
fn(_attr.release.key);
|
|
|
|
}
|
2015-03-04 20:12:14 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
template <typename FN>
|
|
|
|
void handle_relative_motion(FN const &fn) const
|
|
|
|
{
|
|
|
|
if (relative_motion())
|
|
|
|
fn(_attr.rel_motion.x, _attr.rel_motion.y);
|
|
|
|
}
|
2017-01-30 14:50:04 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
template <typename FN>
|
|
|
|
void handle_absolute_motion(FN const &fn) const
|
2015-03-04 20:12:14 +00:00
|
|
|
{
|
2018-04-20 12:27:45 +00:00
|
|
|
if (absolute_motion())
|
|
|
|
fn(_attr.abs_motion.x, _attr.abs_motion.y);
|
2015-03-04 20:12:14 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
template <typename FN>
|
|
|
|
void handle_wheel(FN const &fn) const
|
|
|
|
{
|
|
|
|
if (wheel())
|
|
|
|
fn(_attr.wheel.x, _attr.wheel.y);
|
|
|
|
}
|
2016-05-11 16:21:47 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
template <typename FN>
|
|
|
|
void handle_touch(FN const &fn) const
|
|
|
|
{
|
|
|
|
if (touch())
|
|
|
|
fn(_attr.touch.id, _attr.touch.x, _attr.touch.y);
|
|
|
|
}
|
2015-03-09 11:42:37 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
template <typename FN>
|
|
|
|
void handle_touch_release(FN const &fn) const
|
2015-03-09 11:42:37 +00:00
|
|
|
{
|
2018-04-20 12:27:45 +00:00
|
|
|
if (touch_release())
|
|
|
|
fn(_attr.touch_release.id);
|
2015-03-09 11:42:37 +00:00
|
|
|
}
|
2018-04-20 12:27:45 +00:00
|
|
|
|
|
|
|
inline void print(Genode::Output &out) const;
|
2015-03-04 20:12:14 +00:00
|
|
|
};
|
2011-12-22 15:19:25 +00:00
|
|
|
|
2018-04-20 12:27:45 +00:00
|
|
|
|
|
|
|
void Input::Event::print(Genode::Output &out) const
|
|
|
|
{
|
|
|
|
using Genode::print;
|
|
|
|
switch (_type) {
|
|
|
|
case INVALID: print(out, "INVALID"); break;
|
|
|
|
case PRESS: print(out, "PRESS ", key_name(_attr.press.key),
|
|
|
|
" ", _attr.press.codepoint.value); break;
|
|
|
|
case RELEASE: print(out, "RELEASE ", key_name(_attr.release.key)); break;
|
|
|
|
case REL_MOTION: print(out, "REL_MOTION ", _xy<int>(_attr.rel_motion)); break;
|
|
|
|
case ABS_MOTION: print(out, "ABS_MOTION ", _xy<int>(_attr.abs_motion)); break;
|
|
|
|
case WHEEL: print(out, "WHEEL ", _xy<int>(_attr.wheel)); break;
|
|
|
|
case FOCUS_ENTER: print(out, "FOCUS_ENTER"); break;
|
|
|
|
case FOCUS_LEAVE: print(out, "FOCUS_LEAVE"); break;
|
|
|
|
case HOVER_LEAVE: print(out, "HOVER_LEAVE"); break;
|
|
|
|
case TOUCH_RELEASE: print(out, "TOUCH_RELEASE ", _attr.touch.id.value); break;
|
|
|
|
case TOUCH: print(out, "TOUCH ", _attr.touch.id.value, " ",
|
|
|
|
_xy<float>(_attr.touch)); break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-12-22 15:19:25 +00:00
|
|
|
#endif /* _INCLUDE__INPUT__EVENT_H_ */
|