<commit_before>/***************************************************************************
 *   Copyright (c) 2018 <NAME> <tsadowski[at]gmx.net>            *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  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 Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************/

#include <FCConfig.h>
#include "SpaceballEvent.h"

#include <QMainWindow>

#include "GuiNativeEventLinuxX11.h"

#include "GuiApplicationNativeEventAware.h"
#include <Base/Console.h>

#include <QX11Info>
#include <spnav.h>

#if QT_VERSION >= 0x050000
  #include "GuiRawInputEventFilter.h"
  #undef Bool
  #undef CursorShape
  #undef Expose
  #undef KeyPress
  #undef KeyRelease
  #undef FocusIn
  #undef FocusOut
  #undef FontChange
  #undef None
  #undef Status
  #undef Unsorted
  #undef False
  #undef True
  #undef Complex
#endif // #if QT_VERSION >= 0x050000

Gui::GuiNativeEvent::GuiNativeEvent(Gui::GUIApplicationNativeEventAware *app)
: GuiAbstractNativeEvent(app)
{
}

Gui::GuiNativeEvent::~GuiNativeEvent()
{
    if (spnav_close())
        Base::Console().Log("Couldn't disconnect from spacenav daemon\n");
    else
        Base::Console().Log("Disconnected from spacenav daemon\n");
}

void Gui::GuiNativeEvent::initSpaceball(QMainWindow *window)
{
#if QT_VERSION >= 0x050200
    if (!QX11Info::isPlatformX11()) {
        Base::Console().Log("Application is not running on X11\n");
        return;
    }
#endif
    if (spnav_x11_open(QX11Info::display(), window->winId()) == -1) {
        Base::Console().Log("Couldn't connect to spacenav daemon\n");
    } else {
        Base::Console().Log("Connected to spacenav daemon\n");
        mainApp->setSpaceballPresent(true);

#if QT_VERSION >= 0x050000
        mainApp->installNativeEventFilter(new Gui::RawInputEventFilter(&xcbEventFilter));
#endif // #if QT_VERSION >= 0x050000
    }
}

#if QT_VERSION >= 0x050000

bool Gui::GuiNativeEvent::xcbEventFilter(void *xcb_void, long* result)
{
    Q_UNUSED(result);
    auto inst(dynamic_cast<Gui::GUIApplicationNativeEventAware *>(QApplication::instance()));
    if (!inst)
      return false;<commit_msg>Gui: [skip ci] make log messages distinguishable to see if X11 or polling API if spacenav is used<commit_after>/***************************************************************************
 *   Copyright (c) 2018 <NAME> <tsadowski[at]gmx.net>            *
 *                                                                         *
 *   This file is part of the FreeCAD CAx development system.              *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License as published by the Free Software Foundation; either          *
 *   version 2 of the License, or (at your option) any later version.      *
 *                                                                         *
 *   This library  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 Library General Public License for more details.                  *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; see the file COPYING.LIB. If not,    *
 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
 *   Suite 330, Boston, MA  02111-1307, USA                                *
 *                                                                         *
 ***************************************************************************/

#include <FCConfig.h>
#include "SpaceballEvent.h"

#include <QMainWindow>

#include "GuiNativeEventLinuxX11.h"

#include "GuiApplicationNativeEventAware.h"
#include <Base/Console.h>

#include <QX11Info>
#include <spnav.h>

#if QT_VERSION >= 0x050000
  #include "GuiRawInputEventFilter.h"
  #undef Bool
  #undef CursorShape
  #undef Expose
  #undef KeyPress
  #undef KeyRelease
  #undef FocusIn
  #undef FocusOut
  #undef FontChange
  #undef None
  #undef Status
  #undef Unsorted
  #undef False
  #undef True
  #undef Complex
#endif // #if QT_VERSION >= 0x050000

Gui::GuiNativeEvent::GuiNativeEvent(Gui::GUIApplicationNativeEventAware *app)
: GuiAbstractNativeEvent(app)
{
}

Gui::GuiNativeEvent::~GuiNativeEvent()
{
    if (spnav_close())
        Base::Console().Log("Couldn't disconnect from spacenav daemon\n");
    else
        Base::Console().Log("Disconnected from spacenav daemon\n");
}

void Gui::GuiNativeEvent::initSpaceball(QMainWindow *window)
{
#if QT_VERSION >= 0x050200
    if (!QX11Info::isPlatformX11()) {
        Base::Console().Log("Application is not running on X11\n");
        return;
    }
#endif
    if (spnav_x11_open(QX11Info::display(), window->winId()) == -1) {
        Base::Console().Log("Couldn't connect to spacenav daemon on X11\n");
    } else {
        Base::Console().Log("Connected to spacenav daemon on X11\n");
        mainApp->setSpaceballPresent(true);

#if QT_VERSION >= 0x050000
        mainApp->installNativeEventFilter(new Gui::RawInputEventFilter(&xcbEventFilter));
#endif // #if QT_VERSION >= 0x050000
    }
}

#if QT_VERSION >= 0x050000

bool Gui::GuiNativeEvent::xcbEventFilter(void *xcb_void, long* result)
{
    Q_UNUSED(result);
    auto inst(dynamic_cast<Gui::GUIApplicationNativeEventAware *>(QApplication::instance()));
    if (!inst)
      return false;
{'fraction_non_alphanumeric': 0.1351623096811261, 'fraction_numerical': 0.022694627980465384, 'mean_word_length': 2.244641192917055, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 32, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2758791', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1826, 'n_words': 542}