/*
 * Copyright (C) 2018 Tianjin KYLIN Information Technology 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, 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 <http://www.gnu.org/licenses/>.
 *
**/
#include "grab-x11.h"
#include <QApplication>
#include <X11/Xlib.h>
#include <xcb/xcb.h>

class XServerGraber
{
public:
    XServerGraber() {
        xcb_connection_t *connection = nullptr;
        if (auto *x11Application = qApp->nativeInterface<QNativeInterface::QX11Application>()) {
            connection = x11Application->connection();
        }
        xcb_grab_server(connection);
    }
    ~XServerGraber() {
        xcb_connection_t *connection = nullptr;
        if (auto *x11Application = qApp->nativeInterface<QNativeInterface::QX11Application>()) {
            connection = x11Application->connection();
        }
        xcb_ungrab_server(connection);
        xcb_flush(connection);
    }
};

static bool grabKeyboard()
{
    Display *display = nullptr;
    Window window = DefaultRootWindow(display);
    if (auto *x11Application = qApp->nativeInterface<QNativeInterface::QX11Application>()) {
        display = x11Application->display();
    }
    int rv = XGrabKeyboard(display, window,
                           True, GrabModeAsync, GrabModeAsync, CurrentTime);
    return (rv == GrabSuccess);
}

static bool grabMouse()
{
#define GRABEVENTS ButtonPressMask | ButtonReleaseMask | PointerMotionMask | \
                   EnterWindowMask | LeaveWindowMask | KeyPressMask | KeyReleaseMask
    Display *display = nullptr;
    Window window = DefaultRootWindow(display);
    if (auto *x11Application = qApp->nativeInterface<QNativeInterface::QX11Application>()) {
        display = x11Application->display();
    }
    int rv = XGrabPointer(display, window,
                          True, GRABEVENTS, GrabModeAsync, GrabModeAsync, None,
                          None, CurrentTime);
#undef GRABEVENTS

    return (rv == GrabSuccess);
}

bool establishGrab()
{
    Display *display = nullptr;
    if (auto *x11Application = qApp->nativeInterface<QNativeInterface::QX11Application>()) {
        display = x11Application->display();
    }
    XSync(display, False);
    XServerGraber xserverGraber;

    Q_UNUSED(xserverGraber);

    if (!grabKeyboard()) {
        return false;
    }

    //不抢占鼠标事件
//    if(!grabMouse()) {
//        XUngrabKeyboard(QX11Info::display(), CurrentTime);
//        XFlush(QX11Info::display());
//        return false;
//    }

    return true;
}

bool closeGrab()
{
    Display *display = nullptr;
    if (auto *x11Application = qApp->nativeInterface<QNativeInterface::QX11Application>()) {
        display = x11Application->display();
    }
    XSync(display, False);
    XServerGraber xserverGraber;

    Q_UNUSED(xserverGraber);

    XUngrabKeyboard(display, CurrentTime);
    XUngrabPointer(display, CurrentTime);
    XFlush(display);
    return true;
}
