/*
 * Copyright (C) 2020, KylinSoft Co., Ltd.
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */
#ifndef XEVENTMONITOR_H
#define XEVENTMONITOR_H

#include <QObject>

#include <QThread>
#include <QtCore/qnativeinterface.h>
#include <X11/Xlib.h>
#include <X11/extensions/record.h>
#include <X11/Xlibint.h>
#include <X11/keysym.h>
#include <X11/XKBlib.h>
#undef Always
#undef min
#undef max
// Virtual button codes that are not defined by X11.
#define Button1			1
#define Button2			2
#define Button3			3
#define WheelUp			4
#define WheelDown		5
#define WheelLeft		6
#define WheelRight		7
#define XButton1		8
#define XButton2		9

#ifdef False
#undef False
#endif

#ifdef True
#undef True
#endif

#ifdef Bool
#undef Bool
#endif

#ifdef None
#undef None
#endif

#ifdef Status
#undef Status
#endif

class xEventMonitor : public QThread
{
    Q_OBJECT

public:
    xEventMonitor(QObject *parent = 0);
    void freeXres();
Q_SIGNALS:
    void mousePress(int x, int y);
    void mouseRelease(int x, int y);

protected:
    bool isWheelEvent(int detail);
    static void callback(XPointer trash, XRecordInterceptData* data);
    void handleRecordEvent(XRecordInterceptData *);
    void run();

private:
    bool isOpenDisplay = false;
    XRecordContext m_context;
    Display *m_displayDatalink = nullptr;
};

#endif // XEVENTMONITOR_H
