Simplify and modernize QWaylandInputDevice

Use enum values and nullptr instead of using magic ints directly. Also get rid
of ifdefs in constructors by using in-class member initializers.

Change-Id: I037bec7070296a4a8cb46ebe85104caf5a08fb77
Reviewed-by: Pier Luigi Fiorini <pierluigi.fiorini@liri.io>
This commit is contained in:
Johan Klokkhammer Helsing 2017-12-04 16:26:50 +01:00 committed by Johan Helsing
parent dd13698a73
commit 7b40685ad2
2 changed files with 38 additions and 67 deletions

View File

@ -80,13 +80,6 @@ namespace QtWaylandClient {
QWaylandInputDevice::Keyboard::Keyboard(QWaylandInputDevice *p) QWaylandInputDevice::Keyboard::Keyboard(QWaylandInputDevice *p)
: mParent(p) : mParent(p)
, mFocus(0)
#if QT_CONFIG(xkbcommon_evdev)
, mXkbContext(0)
, mXkbMap(0)
, mXkbState(0)
#endif
, mNativeModifiers(0)
{ {
connect(&mRepeatTimer, SIGNAL(timeout()), this, SLOT(repeatKey())); connect(&mRepeatTimer, SIGNAL(timeout()), this, SLOT(repeatKey()));
} }
@ -168,7 +161,7 @@ QWaylandInputDevice::Keyboard::~Keyboard()
releaseKeyMap(); releaseKeyMap();
#endif #endif
if (mFocus) if (mFocus)
QWindowSystemInterface::handleWindowActivated(0); QWindowSystemInterface::handleWindowActivated(nullptr);
if (mParent->mVersion >= 3) if (mParent->mVersion >= 3)
wl_keyboard_release(object()); wl_keyboard_release(object());
else else
@ -182,16 +175,6 @@ void QWaylandInputDevice::Keyboard::stopRepeat()
QWaylandInputDevice::Pointer::Pointer(QWaylandInputDevice *p) QWaylandInputDevice::Pointer::Pointer(QWaylandInputDevice *p)
: mParent(p) : mParent(p)
, mFocus(0)
, mEnterSerial(0)
#if QT_CONFIG(cursor)
, mCursorSerial(0)
#endif
, mButtons(0)
#if QT_CONFIG(cursor)
, mCursorBuffer(nullptr)
, mCursorShape(Qt::BitmapCursor)
#endif
{ {
} }
@ -205,7 +188,6 @@ QWaylandInputDevice::Pointer::~Pointer()
QWaylandInputDevice::Touch::Touch(QWaylandInputDevice *p) QWaylandInputDevice::Touch::Touch(QWaylandInputDevice *p)
: mParent(p) : mParent(p)
, mFocus(0)
{ {
} }
@ -223,17 +205,6 @@ QWaylandInputDevice::QWaylandInputDevice(QWaylandDisplay *display, int version,
, mQDisplay(display) , mQDisplay(display)
, mDisplay(display->wl_display()) , mDisplay(display->wl_display())
, mVersion(qMin(version, 4)) , mVersion(qMin(version, 4))
, mCaps(0)
#if QT_CONFIG(wayland_datadevice)
, mDataDevice(0)
#endif
, mKeyboard(0)
, mPointer(0)
, mTouch(0)
, mTextInput(0)
, mTime(0)
, mSerial(0)
, mTouchDevice(0)
{ {
#if QT_CONFIG(wayland_datadevice) #if QT_CONFIG(wayland_datadevice)
if (mQDisplay->dndSelectionHandler()) { if (mQDisplay->dndSelectionHandler()) {
@ -262,7 +233,7 @@ void QWaylandInputDevice::seat_capabilities(uint32_t caps)
mKeyboard->init(get_keyboard()); mKeyboard->init(get_keyboard());
} else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && mKeyboard) { } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && mKeyboard) {
delete mKeyboard; delete mKeyboard;
mKeyboard = 0; mKeyboard = nullptr;
} }
if (caps & WL_SEAT_CAPABILITY_POINTER && !mPointer) { if (caps & WL_SEAT_CAPABILITY_POINTER && !mPointer) {
@ -271,7 +242,7 @@ void QWaylandInputDevice::seat_capabilities(uint32_t caps)
pointerSurface = mQDisplay->createSurface(this); pointerSurface = mQDisplay->createSurface(this);
} else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && mPointer) { } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && mPointer) {
delete mPointer; delete mPointer;
mPointer = 0; mPointer = nullptr;
} }
if (caps & WL_SEAT_CAPABILITY_TOUCH && !mTouch) { if (caps & WL_SEAT_CAPABILITY_TOUCH && !mTouch) {
@ -286,7 +257,7 @@ void QWaylandInputDevice::seat_capabilities(uint32_t caps)
} }
} else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && mTouch) { } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && mTouch) {
delete mTouch; delete mTouch;
mTouch = 0; mTouch = nullptr;
} }
} }
@ -350,17 +321,17 @@ void QWaylandInputDevice::removeMouseButtonFromState(Qt::MouseButton button)
QWaylandWindow *QWaylandInputDevice::pointerFocus() const QWaylandWindow *QWaylandInputDevice::pointerFocus() const
{ {
return mPointer ? mPointer->mFocus : 0; return mPointer ? mPointer->mFocus : nullptr;
} }
QWaylandWindow *QWaylandInputDevice::keyboardFocus() const QWaylandWindow *QWaylandInputDevice::keyboardFocus() const
{ {
return mKeyboard ? mKeyboard->mFocus : 0; return mKeyboard ? mKeyboard->mFocus : nullptr;
} }
QWaylandWindow *QWaylandInputDevice::touchFocus() const QWaylandWindow *QWaylandInputDevice::touchFocus() const
{ {
return mTouch ? mTouch->mFocus : 0; return mTouch ? mTouch->mFocus : nullptr;
} }
Qt::KeyboardModifiers QWaylandInputDevice::modifiers() const Qt::KeyboardModifiers QWaylandInputDevice::modifiers() const
@ -440,7 +411,7 @@ void QWaylandInputDevice::setCursor(struct wl_buffer *buffer, const QPoint &hotS
/* Hide cursor */ /* Hide cursor */
if (!buffer) if (!buffer)
{ {
mPointer->set_cursor(mPointer->mEnterSerial, NULL, 0, 0); mPointer->set_cursor(mPointer->mEnterSerial, nullptr, 0, 0);
return; return;
} }
@ -463,7 +434,7 @@ class EnterEvent : public QWaylandPointerEvent
{ {
public: public:
EnterEvent(const QPointF &l, const QPointF &g) EnterEvent(const QPointF &l, const QPointF &g)
: QWaylandPointerEvent(QWaylandPointerEvent::Enter, 0, l, g, 0, Qt::NoModifier) : QWaylandPointerEvent(QWaylandPointerEvent::Enter, 0, l, g, nullptr, Qt::NoModifier)
{} {}
}; };
@ -503,7 +474,7 @@ void QWaylandInputDevice::Pointer::pointer_leave(uint32_t time, struct wl_surfac
QWaylandWindow *window = QWaylandWindow::fromWlSurface(surface); QWaylandWindow *window = QWaylandWindow::fromWlSurface(surface);
window->handleMouseLeave(mParent); window->handleMouseLeave(mParent);
} }
mFocus = 0; mFocus = nullptr;
mButtons = Qt::NoButton; mButtons = Qt::NoButton;
mParent->mTime = time; mParent->mTime = time;
@ -522,7 +493,7 @@ void QWaylandInputDevice::Pointer::pointer_motion(uint32_t time, wl_fixed_t surf
{ {
QWaylandWindow *window = mFocus; QWaylandWindow *window = mFocus;
if (window == NULL) { if (!window) {
// We destroyed the pointer focus surface, but the server // We destroyed the pointer focus surface, but the server
// didn't get the message yet. // didn't get the message yet.
return; return;
@ -624,7 +595,7 @@ void QWaylandInputDevice::Pointer::pointer_axis(uint32_t time, uint32_t axis, in
QPoint pixelDelta; QPoint pixelDelta;
QPoint angleDelta; QPoint angleDelta;
if (window == NULL) { if (!window) {
// We destroyed the pointer focus surface, but the server // We destroyed the pointer focus surface, but the server
// didn't get the message yet. // didn't get the message yet.
return; return;
@ -653,7 +624,7 @@ void QWaylandInputDevice::Keyboard::keyboard_keymap(uint32_t format, int32_t fd,
return; return;
} }
char *map_str = (char *)mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); char *map_str = static_cast<char *>(mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0));
if (map_str == MAP_FAILED) { if (map_str == MAP_FAILED) {
close(fd); close(fd);
return; return;
@ -664,8 +635,8 @@ void QWaylandInputDevice::Keyboard::keyboard_keymap(uint32_t format, int32_t fd,
releaseComposeState(); releaseComposeState();
releaseKeyMap(); releaseKeyMap();
mXkbContext = xkb_context_new(xkb_context_flags(0)); mXkbContext = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
mXkbMap = xkb_map_new_from_string(mXkbContext, map_str, XKB_KEYMAP_FORMAT_TEXT_V1, (xkb_keymap_compile_flags)0); mXkbMap = xkb_map_new_from_string(mXkbContext, map_str, XKB_KEYMAP_FORMAT_TEXT_V1, XKB_KEYMAP_COMPILE_NO_FLAGS);
munmap(map_str, size); munmap(map_str, size);
close(fd); close(fd);
@ -704,7 +675,7 @@ void QWaylandInputDevice::Keyboard::keyboard_leave(uint32_t time, struct wl_surf
window->unfocus(); window->unfocus();
} }
mFocus = NULL; mFocus = nullptr;
mParent->mQDisplay->handleKeyboardFocusChanged(mParent); mParent->mQDisplay->handleKeyboardFocusChanged(mParent);
@ -735,7 +706,7 @@ void QWaylandInputDevice::Keyboard::keyboard_key(uint32_t serial, uint32_t time,
{ {
QWaylandWindow *window = mFocus; QWaylandWindow *window = mFocus;
uint32_t code = key + 8; uint32_t code = key + 8;
bool isDown = state != 0; bool isDown = state != WL_KEYBOARD_KEY_STATE_RELEASED;
QEvent::Type type = isDown ? QEvent::KeyPress : QEvent::KeyRelease; QEvent::Type type = isDown ? QEvent::KeyPress : QEvent::KeyRelease;
QString text; QString text;
int qtkey = key + 8; // qt-compositor substracts 8 for some reason int qtkey = key + 8; // qt-compositor substracts 8 for some reason
@ -874,7 +845,7 @@ void QWaylandInputDevice::Touch::touch_up(uint32_t serial, uint32_t time, int32_
{ {
Q_UNUSED(serial); Q_UNUSED(serial);
Q_UNUSED(time); Q_UNUSED(time);
mFocus = 0; mFocus = nullptr;
mParent->handleTouchPoint(id, 0, 0, Qt::TouchPointReleased); mParent->handleTouchPoint(id, 0, 0, Qt::TouchPointReleased);
// As of Weston 1.5.90 there is no touch_frame after the last touch_up // As of Weston 1.5.90 there is no touch_frame after the last touch_up
@ -900,7 +871,7 @@ void QWaylandInputDevice::Touch::touch_cancel()
if (touchExt) if (touchExt)
touchExt->touchCanceled(); touchExt->touchCanceled();
QWindowSystemInterface::handleTouchCancelEvent(0, mParent->mTouchDevice); QWindowSystemInterface::handleTouchCancelEvent(nullptr, mParent->mTouchDevice);
} }
void QWaylandInputDevice::handleTouchPoint(int id, double x, double y, Qt::TouchPointState state) void QWaylandInputDevice::handleTouchPoint(int id, double x, double y, Qt::TouchPointState state)
@ -985,7 +956,7 @@ void QWaylandInputDevice::Touch::touch_frame()
return; return;
} }
QWindow *window = mFocus ? mFocus->window() : 0; QWindow *window = mFocus ? mFocus->window() : nullptr;
if (mFocus) { if (mFocus) {
const QWindowSystemInterface::TouchPoint &tp = mTouchPoints.last(); const QWindowSystemInterface::TouchPoint &tp = mTouchPoints.last();

View File

@ -147,27 +147,27 @@ private:
struct wl_display *mDisplay; struct wl_display *mDisplay;
int mVersion; int mVersion;
uint32_t mCaps; uint32_t mCaps = 0;
struct wl_surface *pointerSurface; struct wl_surface *pointerSurface;
#if QT_CONFIG(wayland_datadevice) #if QT_CONFIG(wayland_datadevice)
QWaylandDataDevice *mDataDevice; QWaylandDataDevice *mDataDevice = nullptr;
#endif #endif
Keyboard *mKeyboard; Keyboard *mKeyboard = nullptr;
Pointer *mPointer; Pointer *mPointer = nullptr;
Touch *mTouch; Touch *mTouch = nullptr;
QWaylandTextInput *mTextInput; QWaylandTextInput *mTextInput = nullptr;
uint32_t mTime; uint32_t mTime = 0;
uint32_t mSerial; uint32_t mSerial = 0;
void seat_capabilities(uint32_t caps) override; void seat_capabilities(uint32_t caps) override;
void handleTouchPoint(int id, double x, double y, Qt::TouchPointState state); void handleTouchPoint(int id, double x, double y, Qt::TouchPointState state);
QTouchDevice *mTouchDevice; QTouchDevice *mTouchDevice = nullptr;
QSharedPointer<QWaylandBuffer> mPixmapCursor; QSharedPointer<QWaylandBuffer> mPixmapCursor;
@ -210,13 +210,13 @@ public:
QWaylandInputDevice *mParent; QWaylandInputDevice *mParent;
QPointer<QWaylandWindow> mFocus; QPointer<QWaylandWindow> mFocus;
#if QT_CONFIG(xkbcommon_evdev) #if QT_CONFIG(xkbcommon_evdev)
xkb_context *mXkbContext; xkb_context *mXkbContext = nullptr;
xkb_keymap *mXkbMap; xkb_keymap *mXkbMap = nullptr;
xkb_state *mXkbState; xkb_state *mXkbState = nullptr;
xkb_compose_table *mXkbComposeTable = nullptr; xkb_compose_table *mXkbComposeTable = nullptr;
xkb_compose_state *mXkbComposeState = nullptr; xkb_compose_state *mXkbComposeState = nullptr;
#endif #endif
uint32_t mNativeModifiers; uint32_t mNativeModifiers = 0;
int mRepeatKey; int mRepeatKey;
uint32_t mRepeatCode; uint32_t mRepeatCode;
@ -264,16 +264,16 @@ public:
QWaylandInputDevice *mParent; QWaylandInputDevice *mParent;
QPointer<QWaylandWindow> mFocus; QPointer<QWaylandWindow> mFocus;
uint32_t mEnterSerial; uint32_t mEnterSerial = 0;
#if QT_CONFIG(cursor) #if QT_CONFIG(cursor)
uint32_t mCursorSerial; uint32_t mCursorSerial = 0;
#endif #endif
QPointF mSurfacePos; QPointF mSurfacePos;
QPointF mGlobalPos; QPointF mGlobalPos;
Qt::MouseButtons mButtons; Qt::MouseButtons mButtons = Qt::NoButton;
#if QT_CONFIG(cursor) #if QT_CONFIG(cursor)
wl_buffer *mCursorBuffer; wl_buffer *mCursorBuffer = nullptr;
Qt::CursorShape mCursorShape; Qt::CursorShape mCursorShape = Qt::BitmapCursor;
#endif #endif
}; };