mouse: add MouseHelper class
authorGiWoong Kim <giwoong.kim@samsung.com>
Wed, 16 Dec 2015 07:59:48 +0000 (16:59 +0900)
committerSeokYeon Hwang <syeon.hwang@samsung.com>
Fri, 18 Dec 2015 11:21:37 +0000 (20:21 +0900)
- extract input related codes from Display class
- clean up some lines

Change-Id: I90e9db61dbfffd1ee85aa578a5de9225921d7b55
Signed-off-by: GiWoong Kim <giwoong.kim@samsung.com>
tizen/src/ui/displaybase.cpp
tizen/src/ui/displaybase.h
tizen/src/ui/input/Makefile.objs
tizen/src/ui/input/mousehelper.cpp [new file with mode: 0644]
tizen/src/ui/input/mousehelper.h [new file with mode: 0644]

index cbf9ea40ba692d4eb6fabeae59a1484f5507b8c7..4dfa9329e43782a073f2c85ceda22347cf297369 100644 (file)
  */
 
 #include "displaybase.h"
-#include "resource/ui_strings.h"
 #include "mainwindow.h"
+#include "resource/ui_strings.h"
 
 extern "C" {
 #include "util/ui_operations.h"
 
 void qt5_graphic_hw_invalidate(void);
 void req_set_sensor_accel_angle(int angle);
-void maru_tablet_event(int event_type, int x, int y, int btn, int btn_status);
 }
 
 uint32_t qt5_window_width = 0;
@@ -57,28 +56,22 @@ DisplayBase::DisplayBase(DisplayType *displayForm, QSize resolution, qreal scale
     this->offGuide = NULL;
     this->offGuideShown = false;
 
-    this->isTouch = is_touchscreen_enabled();
+    this->isTsEnabled = is_touchscreen_enabled();
 
     /* touch screen */
     this->tsHelper = new TouchScreenHelper(this);
+    win->getMainView()->getKbdHelper()->setMtTracker(tsHelper->getMtTracker());
 
     /* mouse */
-    this->prevX = 0;
-    this->prevY = 0;
-    this->lastMouseTime.tv_sec = 0;
-    this->mouseStatus = MOUSE_LEAVE;
+    this->mouseHelper = new MouseHelper(this, resolution);
 
-    win->getMainView()->getKbdHelper()->setMtTracker(tsHelper->getMtTracker());
-
-    loadGuideImg();
+    loadOffGuideImg();
 
+    req_set_sensor_accel_angle(rotateAngle); /* update sensor */
     updateGeometry();
-
-    /* update sensor */
-    req_set_sensor_accel_angle(rotateAngle);
 }
 
-void DisplayBase::loadGuideImg()
+void DisplayBase::loadOffGuideImg()
 {
     QString offImage = QDir(QCoreApplication::applicationDirPath() +
         QDir::separator() + SDK_EMULATOR_IMAGES_PATH +
@@ -142,12 +135,10 @@ void DisplayBase::switchForm(DisplayType *displayForm)
     rect = displayForm->getRect();
     maskImage = displayForm->getMask();
 
-    /* update sensor */
-    req_set_sensor_accel_angle(rotateAngle);
-
+    req_set_sensor_accel_angle(rotateAngle); /* update sensor */
     updateGeometry();
-    update();
 
+    update();
     widget->repaint();
 }
 
@@ -158,8 +149,8 @@ void DisplayBase::scaleForm(qreal scaleFactor)
     this->scaleFactor = scaleFactor;
 
     updateGeometry();
-    update();
 
+    update();
     widget->repaint();
 }
 
@@ -249,15 +240,20 @@ TouchScreenHelper *DisplayBase::getTouchScreenHelper()
     return tsHelper;
 }
 
+MouseHelper *DisplayBase::getMouseHelper()
+{
+    return mouseHelper;
+}
+
 void DisplayBase::handleMousePress(QMouseEvent *event)
 {
     if (event->button() == Qt::LeftButton) {
         isDragging = true;
 
-        if (isTouch) {
+        if (isTsEnabled == true) {
             tsHelper->mousePressed(event, getGuestPos(event->pos()));
         } else {
-            do_mouse_event(1, PS2_PRESS, 0, 0, 0, 0, 0);
+            mouseHelper->mousePressed(event);
         }
     }
 }
@@ -265,21 +261,21 @@ void DisplayBase::handleMousePress(QMouseEvent *event)
 void DisplayBase::handleMouseRelease(QMouseEvent *event)
 {
     if (event->button() == Qt::LeftButton) {
-        if (isDragging) {
+        if (isDragging == true) {
             isDragging = false;
         }
 
-        if (isTouch) {
+        if (isTsEnabled == true) {
             tsHelper->mouseReleased(event, getGuestPos(event->pos()));
         } else {
-            do_mouse_event(1, PS2_RELEASE, 0, 0, 0, 0, 0);
+            mouseHelper->mouseReleased(event);
         }
     }
 }
 
 void DisplayBase::handleMouseMove(QMouseEvent *event)
 {
-    if (isTouch) { /* touch device */
+    if (isTsEnabled == true) { /* touch device */
         if (isDragging == true) {
             int hostPosX = event->x();
             int hostPosY = event->y();
@@ -311,105 +307,40 @@ void DisplayBase::handleMouseMove(QMouseEvent *event)
                 tsHelper->mouseMoved(event, getGuestPos(event->pos()));
             }
         }
-    } else {
-        int event_type = MOUSE_MOVE;
-        int clientX = event->x();
-        int clientY = event->y();
+    } else { /* mouse device */
+        const int hostPosX = event->x();
+        const int hostPosY = event->y();
 
-        if (clientX < 0 || clientX > widget->width() ||
-                    clientY < 0 || clientY > widget->height()) {
+        if (hostPosX < 0 || hostPosX > widget->width() ||
+            hostPosY < 0 || hostPosY > widget->height()) {
             if (isDragging == true) {
+                qDebug("drag out: auto release");
+
                 isDragging = false;
-                qDebug("auto released...");
-                do_mouse_event(1, PS2_RELEASE, 0, 0, 0, 0, 0);
+                mouseHelper->mouseReleased(event);
                 return;
-            } else {
-                /* do nothing */
             }
         }
-        sendMouseEvent(event_type, clientX, clientY);
-    }
-}
 
-void DisplayBase::handleMouseInit(QMouseEvent *event)
-{
-    do_mouse_event(1, MOUSE_MOVE, -widget->width()/scaleFactor,
-            -widget->height()/scaleFactor,
-            -widget->width()/scaleFactor,
-            -widget->height()/scaleFactor, 0);
-    prevX = 0;
-    prevY = 0;
+        mouseHelper->mouseMoved(event,
+            event->pos(), getGuestPos(event->pos()));
+    }
 }
 
 void DisplayBase::handleMouseEnter(QEvent *event)
 {
-    if (!isTouch) {
-        if (mouseStatus == MOUSE_LEAVE) {
-            mouseStatus = MOUSE_ENTER;
-        }
+    if (isTsEnabled == false) {
+        mouseHelper->mouseEnter(event);
     }
 }
 
 void DisplayBase::handleMouseLeave(QEvent *event)
 {
-    if (!isTouch) {
-        mouseStatus = MOUSE_LEAVE;
+    if (isTsEnabled == false) {
+        mouseHelper->mouseLeave(event);
     }
 }
 
-void DisplayBase::sendMouseEvent(int eventType, int clientX, int clientY)
-{
-    int xx = clientX / scaleFactor;
-    int yy = clientY / scaleFactor;
-    int absX = xx;
-    int absY = yy;
-
-    /* TODO: x*cos(rad)-y*sin(rad) */
-    switch(rotateAngle) {
-    case 90: /* Reverse Landscape */
-        absX = yy;
-        absY = resolution.height() - xx;
-        break;
-    case 180: /* Reverse Portrait */
-        absX = resolution.width() - xx;
-        absY = resolution.height() - yy;
-        break;
-    case 270: /* Landscape */
-        absX = resolution.width() - yy;
-        absY = xx;
-        break;
-    case 0:
-    default:
-        break;
-    }
-
-    /* Because the TV usb mouse pointer is moved to center after a couple of seconds.
-     * Also the mouse pointer can be entered before emulator skin is initialized.
-     */
-    struct timeval tv;
-    gettimeofday(&tv, NULL);
-    if (tv.tv_sec - lastMouseTime.tv_sec > CURSOR_RESET_TIME || mouseStatus != MOUSE_ENTERED) {
-        maru_tablet_event(INPUT_MOVE, absX, absY, 0, 0);
-        prevX = absX;
-        prevY = absY;
-        mouseStatus = MOUSE_ENTERED;
-    }
-    gettimeofday(&lastMouseTime, NULL);
-
-    prevX = (prevX > resolution.width()) ? resolution.width() : prevX;
-    prevX = (prevX < 0) ? 0 : prevX;
-
-    prevY = (prevY > resolution.height()) ? resolution.height() : prevY;
-    prevY = (prevY < 0) ? 0 : prevY;
-
-    int relX = absX - prevX;
-    int relY = absY - prevY;
-
-    do_mouse_event(1, MOUSE_MOVE, clientX, clientY, relX, relY, 0);
-    prevX = absX;
-    prevY = absY;
-}
-
 DisplayBase::~DisplayBase()
 {
     qDebug("destroy display");
@@ -419,4 +350,7 @@ DisplayBase::~DisplayBase()
     if (tsHelper != NULL) {
         delete tsHelper;
     }
+    if (mouseHelper != NULL) {
+        delete mouseHelper;
+    }
 }
index 6440105e9ec6b680217a8fbb8fe0893e9d0e078d..fdaa8d3e5bfa7961c112d7f9bfef5f4746658695 100644 (file)
 #include <QLabel>
 #include <QImage>
 #include <QPixmap>
-#include <sys/time.h>
 
 #include "layout/displaytype.h"
 #include "input/touchscreenhelper.h"
+#include "input/mousehelper.h"
 
 class MainWindow;
 
-enum {
-    TOUCH_PRESS = 1,
-    TOUCH_RELEASE = 2,
-    PS2_MOVE = 6,
-    PS2_PRESS = 8,
-    PS2_RELEASE = 9,
-};
-
-enum {
-    MOUSE_LEAVE = 0,
-    MOUSE_ENTER = 1,
-    MOUSE_ENTERED = 2,
-};
-
-/* keep it consistent with emulator-kernel tablet definition */
-enum {
-    INPUT_MOVE = 1,
-    INPUT_BTN = 2,
-};
-
-#define CURSOR_RESET_TIME 6    /* TV spec (sec) */
-
 class DisplayBase
 {
 public:
@@ -81,6 +59,7 @@ public:
 
     QPoint getGuestPos(QPoint hostPos);
     TouchScreenHelper *getTouchScreenHelper();
+    MouseHelper *getMouseHelper();
 
 protected:
     DisplayBase(DisplayType *displayForm, QSize resolution, qreal scaleFactor, QWidget *w);
@@ -92,7 +71,6 @@ protected:
     void handleMousePress(QMouseEvent *event);
     void handleMouseRelease(QMouseEvent *event);
     void handleMouseMove(QMouseEvent *event);
-    void handleMouseInit(QMouseEvent *event);
 
     void handleMouseEnter(QEvent *event);
     void handleMouseLeave(QEvent *event);
@@ -102,23 +80,18 @@ protected:
     bool isDragging;
 
 private:
-    void loadGuideImg();
-
-    void sendMouseEvent(int eventType, int clientX, int clientY);
+    void loadOffGuideImg();
 
     QSize resolution;
-    bool isTouch;
-
-    int prevX;
-    int prevY;
-    int mouseStatus;
-    struct timeval lastMouseTime;
+    bool isTsEnabled;
 
     MainWindow *win;
     QWidget *widget; /* child */
     QRect rect;
     QPixmap maskImage;
+
     TouchScreenHelper *tsHelper;
+    MouseHelper *mouseHelper;
 
     QLabel *offGuide;
     QPixmap offGuideImg;
index 5316f1cb3baea24055e63fda76f108283491885e..186c3d738f52fb08555aadc975e7019b3cf4d47d 100644 (file)
@@ -1,5 +1,6 @@
 obj-$(CONFIG_QT) += touchscreenhelper.o
 obj-$(CONFIG_QT) += multitouchtracker.o
+obj-$(CONFIG_QT) += mousehelper.o
 obj-$(CONFIG_QT) += keyboardhelper.o
 obj-$(CONFIG_QT) += keyboardshortcut.o moc_keyboardshortcut.o
 
diff --git a/tizen/src/ui/input/mousehelper.cpp b/tizen/src/ui/input/mousehelper.cpp
new file mode 100644 (file)
index 0000000..297fbd7
--- /dev/null
@@ -0,0 +1,130 @@
+/*
+ * Qt mouse helper
+ *
+ * Copyright (C) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * GiWoong Kim <giwoong.kim@samsung.com>
+ * SeokYeon Hwang <syeon.hwang@samsung.com>
+ * Sangho Park <sangho.p@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+#include <QWidget>
+#include <QMouseEvent>
+
+#include "mousehelper.h"
+
+extern "C" {
+#include "emul_state.h"
+#include "util/ui_operations.h"
+
+void maru_tablet_event(int event_type, int x, int y, int btn, int btn_status);
+}
+
+#define CURSOR_RESET_TIME (6) /* TV spec (sec) */
+
+MouseHelper::MouseHelper(void *parent, QSize resolution)
+{
+    this->parent = parent;
+    this->resolution = resolution;
+
+    this->prevX = 0;
+    this->prevY = 0;
+    this->mouseStatus = MOUSE_LEAVE;
+    this->lastMouseTime.tv_sec = 0;
+}
+
+void *MouseHelper::getParent()
+{
+    return parent;
+}
+
+void MouseHelper::mousePressed(QMouseEvent *event)
+{
+    Q_UNUSED(event);
+
+    do_mouse_event(1, PS2_PRESS, 0, 0, 0, 0, 0);
+}
+
+void MouseHelper::mouseReleased(QMouseEvent *event)
+{
+    Q_UNUSED(event);
+
+    do_mouse_event(1, PS2_RELEASE, 0, 0, 0, 0, 0);
+}
+
+void MouseHelper::mouseMoved(QMouseEvent *event,
+    QPoint hostPos, QPoint guestPos)
+{
+    const int absX = guestPos.x();
+    const int absY = guestPos.y();
+
+    /* Because the TV usb mouse pointer is moved to center after a couple of seconds.
+     * Also the mouse pointer can be entered before emulator skin is initialized.
+     */
+
+    gettimeofday(&tv, NULL);
+    if (tv.tv_sec - lastMouseTime.tv_sec > CURSOR_RESET_TIME ||
+        mouseStatus != MOUSE_ENTERED)
+    {
+        maru_tablet_event(INPUT_MOVE, absX, absY, 0, 0);
+        prevX = absX;
+        prevY = absY;
+        mouseStatus = MOUSE_ENTERED;
+    }
+    gettimeofday(&lastMouseTime, NULL);
+
+    prevX = qMin(resolution.width(), prevX);
+    prevX = qMax(0, prevX);
+    prevY = qMin(resolution.height(), prevY);
+    prevY = qMax(0, prevY);
+
+    /* relative positioning */
+    const int relX = absX - prevX;
+    const int relY = absY - prevY;
+
+    do_mouse_event(1, MOUSE_MOVE, hostPos.x(), hostPos.y(), relX, relY, 0);
+
+    prevX = absX;
+    prevY = absY;
+}
+
+void MouseHelper::mouseEnter(QEvent *event)
+{
+    Q_UNUSED(event);
+
+    if (mouseStatus == MOUSE_LEAVE) {
+        mouseStatus = MOUSE_ENTER;
+    }
+}
+
+void MouseHelper::mouseLeave(QEvent *event)
+{
+    Q_UNUSED(event);
+
+    mouseStatus = MOUSE_LEAVE;
+}
+
+MouseHelper::~MouseHelper()
+{
+    qDebug("destroy mouse helper");
+}
diff --git a/tizen/src/ui/input/mousehelper.h b/tizen/src/ui/input/mousehelper.h
new file mode 100644 (file)
index 0000000..3bcb127
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Qt mouse helper
+ *
+ * Copyright (C) 2015 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact:
+ * GiWoong Kim <giwoong.kim@samsung.com>
+ * SeokYeon Hwang <syeon.hwang@samsung.com>
+ * Sangho Park <sangho.p@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+#ifndef MOUSEHELPER_H
+#define MOUSEHELPER_H
+
+#include <sys/time.h>
+
+enum {
+    PS2_MOVE = 6,
+    PS2_PRESS = 8,
+    PS2_RELEASE = 9,
+};
+
+enum {
+    MOUSE_LEAVE = 0,
+    MOUSE_ENTER = 1,
+    MOUSE_ENTERED = 2,
+};
+
+/* keep it consistent with emulator-kernel tablet definition */
+enum {
+    INPUT_MOVE = 1,
+    INPUT_BTN = 2,
+};
+
+class MouseHelper
+{
+public:
+    MouseHelper(void *parent, QSize resolution);
+    ~MouseHelper();
+
+    void *getParent();
+
+    void mousePressed(QMouseEvent *event);
+    void mouseReleased(QMouseEvent *event);
+    void mouseMoved(QMouseEvent *event, QPoint hostPos, QPoint guestPos);
+
+    void mouseEnter(QEvent *event);
+    void mouseLeave(QEvent *event);
+
+private:
+    void *parent;
+    QSize resolution;
+
+    int prevX;
+    int prevY;
+    int mouseStatus;
+    struct timeval tv;
+    struct timeval lastMouseTime;
+};
+
+#endif // MOUSEHELPER_H