From bfa4ea1b72c633ec1ec9c361e1ad67d611c1a101 Mon Sep 17 00:00:00 2001 From: trialuser02 Date: Mon, 11 Feb 2013 12:11:48 +0000 Subject: global hotkey manager: prepare for windows support git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@3227 90c681e8-e032-0410-971d-27865f9a5e38 --- src/plugins/General/hotkey/hotkey.pro | 122 +++++----- src/plugins/General/hotkey/hotkeymanager_win.cpp | 280 +++++++++++++++++++++++ src/plugins/General/hotkey/hotkeymanager_x11.cpp | 3 + src/plugins/General/hotkey/settingsdialog.cpp | 10 +- 4 files changed, 353 insertions(+), 62 deletions(-) create mode 100644 src/plugins/General/hotkey/hotkeymanager_win.cpp (limited to 'src/plugins/General/hotkey') diff --git a/src/plugins/General/hotkey/hotkey.pro b/src/plugins/General/hotkey/hotkey.pro index 3e2443f6f..7ff1554b2 100644 --- a/src/plugins/General/hotkey/hotkey.pro +++ b/src/plugins/General/hotkey/hotkey.pro @@ -1,54 +1,68 @@ -include(../../plugins.pri) - -CONFIG += release \ -warn_on \ -plugin \ - lib \ - link_pkgconfig - -PKGCONFIG += x11 -DEFINES += HAVE_XKBLIB_H - -TARGET = $$PLUGINS_PREFIX/General/hotkey -QMAKE_CLEAN = $$PLUGINS_PREFIX/General/libhotkey.so - -TEMPLATE = lib -QMAKE_LIBDIR += ../../../../lib - -TRANSLATIONS = translations/hotkey_plugin_cs.ts \ - translations/hotkey_plugin_de.ts \ - translations/hotkey_plugin_zh_CN.ts \ - translations/hotkey_plugin_zh_TW.ts \ - translations/hotkey_plugin_ru.ts \ - translations/hotkey_plugin_pl.ts \ - translations/hotkey_plugin_uk_UA.ts \ - translations/hotkey_plugin_it.ts \ - translations/hotkey_plugin_tr.ts \ - translations/hotkey_plugin_lt.ts \ - translations/hotkey_plugin_nl.ts \ - translations/hotkey_plugin_ja.ts \ - translations/hotkey_plugin_es.ts -RESOURCES = translations/translations.qrc - -isEmpty(LIB_DIR){ - LIB_DIR = /lib -} -target.path = $$LIB_DIR/qmmp/General -INSTALLS += target - -HEADERS += hotkeyfactory.h \ - hotkeymanager.h \ - settingsdialog.h \ - hotkeydialog.h - -SOURCES += hotkeyfactory.cpp \ - settingsdialog.cpp \ - hotkeydialog.cpp \ - hotkeymanager_x11.cpp - -INCLUDEPATH += ../../../../src - -LIBS += -lqmmpui -lqmmp - -FORMS += settingsdialog.ui \ - hotkeydialog.ui +include(../../plugins.pri) + +CONFIG += release \ +warn_on \ +plugin \ + lib \ + link_pkgconfig + + +TARGET = $$PLUGINS_PREFIX/General/hotkey + + +TEMPLATE = lib +QMAKE_LIBDIR += ../../../../lib + +TRANSLATIONS = translations/hotkey_plugin_cs.ts \ + translations/hotkey_plugin_de.ts \ + translations/hotkey_plugin_zh_CN.ts \ + translations/hotkey_plugin_zh_TW.ts \ + translations/hotkey_plugin_ru.ts \ + translations/hotkey_plugin_pl.ts \ + translations/hotkey_plugin_uk_UA.ts \ + translations/hotkey_plugin_it.ts \ + translations/hotkey_plugin_tr.ts \ + translations/hotkey_plugin_lt.ts \ + translations/hotkey_plugin_nl.ts \ + translations/hotkey_plugin_ja.ts \ + translations/hotkey_plugin_es.ts +RESOURCES = translations/translations.qrc + +target.path = $$LIB_DIR/qmmp/General +INSTALLS += target + +HEADERS += hotkeyfactory.h \ + hotkeymanager.h \ + settingsdialog.h \ + hotkeydialog.h + +SOURCES += hotkeyfactory.cpp \ + settingsdialog.cpp \ + hotkeydialog.cpp \ + hotkeymanager_x11.cpp \ + hotkeymanager_win.cpp + +FORMS += settingsdialog.ui \ + hotkeydialog.ui + + +INCLUDEPATH += ../../../../src + +unix { + isEmpty(LIB_DIR){ + LIB_DIR = /lib + } + target.path = $$LIB_DIR/qmmp/General + INSTALLS += target + QMAKE_LIBDIR += ../../../../lib + QMAKE_CLEAN = $$PLUGINS_PREFIX/General/libhotkey.so + PKGCONFIG += x11 + DEFINES += HAVE_XKBLIB_H + LIBS += -lqmmpui -lqmmp +} + +win32 { + QMAKE_LIBDIR += ../../../../bin + LIBS += -lqmmpui0 -lqmmp0 +} + diff --git a/src/plugins/General/hotkey/hotkeymanager_win.cpp b/src/plugins/General/hotkey/hotkeymanager_win.cpp new file mode 100644 index 000000000..cb9c5b3f0 --- /dev/null +++ b/src/plugins/General/hotkey/hotkeymanager_win.cpp @@ -0,0 +1,280 @@ +/*************************************************************************** + * Copyright (C) 2013 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * 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. * + ***************************************************************************/ + +#include +#ifdef Q_OS_WIN +#include +#include +#include +#include +#include +#include +#include +#define _WIN32_WINNT 0x0500 +#include +#include +#include +#include +#include +#include +#include +#include "hotkeymanager.h" + +quint32 Hotkey::defaultKey() +{ + return defaultKey(action); +} + +quint32 Hotkey::defaultKey(int act) +{ + //default key bindings + QMap keyMap; + keyMap[PLAY] = 0; + keyMap[STOP] = VK_MEDIA_STOP; + keyMap[PAUSE] = 0; + keyMap[PLAY_PAUSE] = VK_MEDIA_PLAY_PAUSE; + keyMap[NEXT] = VK_MEDIA_NEXT_TRACK; + keyMap[PREVIOUS] = VK_MEDIA_PREV_TRACK; + keyMap[SHOW_HIDE] = 0; + keyMap[VOLUME_UP] = VK_VOLUME_UP; + keyMap[VOLUME_DOWN] = VK_VOLUME_DOWN; + keyMap[FORWARD] = 0; + keyMap[REWIND] = 0; + keyMap[JUMP_TO_TRACK] = 0; + return keyMap[act]; +} + +WPARAM nextId132 = 1; + + +class KeyFilterWidget : public QWidget +{ +public: + KeyFilterWidget() + {} + + ~KeyFilterWidget() + { + + } + +bool winEvent(MSG* m, long* result) +{ + if (m->message == WM_HOTKEY) + { + qDebug("HOT KEY PRESSED!!!"); + return true; + } + return QWidget::winEvent(m, result); +} +}; + + +HotkeyManager::HotkeyManager(QObject *parent) : QObject(parent) +{ + QCoreApplication::instance()->installEventFilter(this); + KeyFilterWidget *w = new KeyFilterWidget; + + //qDebug("+++%d++++", int(WId)); + QSettings settings(Qmmp::configFile(), QSettings::IniFormat); //load settings + settings.beginGroup("Hotkey"); + for (int i = Hotkey::PLAY; i <= Hotkey::JUMP_TO_TRACK; ++i) + { + qDebug("----"); + quint32 key = settings.value(QString("key_%1").arg(i), Hotkey::defaultKey(i)).toUInt(); + quint32 mod = settings.value(QString("modifiers_%1").arg(i), 0).toUInt(); + + if (key) + { + + Hotkey *hotkey = new Hotkey; + hotkey->action = i; + hotkey->key = key; + //hotkey->code = keycodeToKeysym(hotkey->key); + /*if(!hotkey->code) + continue;*/ + + qDebug("1"); + if(RegisterHotKey(w->winId(), MOD_CONTROL^hotkey->key, MOD_CONTROL, hotkey->key)) + { + qDebug("registered"); + nextId132++; + } + else + { + qDebug("failed"); + } + qDebug("2"); + + //hotkey->mod = mod | mask_mod; + m_grabbedKeys << hotkey; + } + } + settings.endGroup(); +} + +HotkeyManager::~HotkeyManager() +{ + /*foreach(Hotkey *key, m_grabbedKeys) + { + if(key->code) + XUngrabKey(QX11Info::display(), key->code, key->mod, QX11Info::appRootWindow()); + } + while (!m_grabbedKeys.isEmpty()) + delete m_grabbedKeys.takeFirst ();*/ +} + +const QString HotkeyManager::getKeyString(quint32 key, quint32 modifiers) +{ + qDebug("%ux++%ux", key, modifiers); + QString strModList[] = { "Control", "Shift", "Alt"}; + quint32 modList[] = { HOTKEYF_CONTROL, HOTKEYF_SHIFT, HOTKEYF_ALT}; + + MOD_CONTROL, MOD_ALT, HOTKEYF_EXT; + + QString keyStr; + for (int j = 0; j < 3; j++) + { + if (modifiers & modList[j]) + keyStr.append(strModList[j] + "+"); + } + + LONG lScan = MapVirtualKey(key, 0) << 16; + + int nBufferLen = 64; + std::wstring str; + int nLen; + do + { + nBufferLen *= 2; + str.resize(nBufferLen); + nLen = ::GetKeyNameTextW(lScan, &str[0], nBufferLen); + } + while (nLen == nBufferLen); + + keyStr += QString::fromWCharArray(str.c_str()); + + return keyStr; +} + +bool HotkeyManager::eventFilter(QObject* o, QEvent* e) +{ + if (e->type() == QEvent::KeyPress) + { + qDebug("KEY EVENT!!"); + } + + //receive events from active and root windows only + /*if (e->type() == QEvent::KeyPress && (o == qApp->desktop () || o == qApp->activeWindow ())) + { + QKeyEvent* k = static_cast(e); + quint32 key = keycodeToKeysym(k->nativeScanCode()); + quint32 mod = k->nativeModifiers (); + SoundCore *core = SoundCore::instance(); + MediaPlayer *player = MediaPlayer::instance(); + foreach(Hotkey *hotkey, m_grabbedKeys) + { + if (hotkey->key != key || hotkey->mod != mod) + continue; + qDebug("HotkeyManager: [%s] pressed", qPrintable(getKeyString(key, mod))); + + switch (hotkey->action) + { + case Hotkey::PLAY: + player->play(); + break; + case Hotkey::STOP: + player->stop(); + break; + case Hotkey::PAUSE: + core->pause(); + break; + case Hotkey::PLAY_PAUSE: + if (core->state() == Qmmp::Stopped) + MediaPlayer::instance()->play(); + else if (core->state() != Qmmp::FatalError) + core->pause(); + break; + case Hotkey::NEXT: + player->next(); + break; + case Hotkey::PREVIOUS: + player->previous(); + break; + case Hotkey::SHOW_HIDE: + UiHelper::instance()->toggleVisibility(); + break; + case Hotkey::VOLUME_UP: + case Hotkey::VOLUME_DOWN: + { + int volume = qMax(core->leftVolume(), core->rightVolume()); + int balance = 0; + int left = core->leftVolume(); + int right = core->rightVolume(); + if (left || right) + balance = (right - left)*100/volume; + if(hotkey->action == Hotkey::VOLUME_UP) + volume = qMin (100, volume + 5); + else + volume = qMax (0, volume - 5); + core->setVolume(volume-qMax(balance,0)*volume/100, + volume+qMin(balance,0)*volume/100); + } + break; + case Hotkey::FORWARD: + core->seek(core->elapsed() + 5000); + break; + case Hotkey::REWIND: + core->seek(qMax(qint64(0), core->elapsed() - 5000)); + break; + case Hotkey::JUMP_TO_TRACK: + UiHelper::instance()->jumpToTrack(); + break; + + } + qApp->processEvents(); + } + }*/ + return QObject::eventFilter(o, e); +} +long HotkeyManager::m_alt_mask = 0; +long HotkeyManager::m_meta_mask = 0; +long HotkeyManager::m_super_mask = 0; +long HotkeyManager::m_hyper_mask = 0; +long HotkeyManager::m_numlock_mask = 0; +bool HotkeyManager::m_haveMods = false; + + +void HotkeyManager::ensureModifiers(){} + +QList HotkeyManager::ignModifiersList() +{ + ensureModifiers(); + QList ret; + ret << 0; + return ret; +} + +quint32 HotkeyManager::keycodeToKeysym(quint32 keycode) +{ + return MapVirtualKey(keycode, 1); +} +#endif + diff --git a/src/plugins/General/hotkey/hotkeymanager_x11.cpp b/src/plugins/General/hotkey/hotkeymanager_x11.cpp index 5f96617ea..c5bf0e7ca 100644 --- a/src/plugins/General/hotkey/hotkeymanager_x11.cpp +++ b/src/plugins/General/hotkey/hotkeymanager_x11.cpp @@ -20,6 +20,8 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ +#include +#ifdef Q_WS_X11 #include #include #include @@ -321,3 +323,4 @@ quint32 HotkeyManager::keycodeToKeysym(quint32 keycode) return XKeycodeToKeysym(QX11Info::display(), keycode,0); #endif } +#endif diff --git a/src/plugins/General/hotkey/settingsdialog.cpp b/src/plugins/General/hotkey/settingsdialog.cpp index f858d13aa..1faa503d6 100644 --- a/src/plugins/General/hotkey/settingsdialog.cpp +++ b/src/plugins/General/hotkey/settingsdialog.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2009-2012 by Ilya Kotov * + * Copyright (C) 2009-2013 by Ilya Kotov * * forkotov02@hotmail.ru * * * * This program is free software; you can redistribute it and/or modify * @@ -20,12 +20,6 @@ #include #include - -extern "C" -{ -#include -} - #include #include "hotkeydialog.h" #include "settingsdialog.h" @@ -61,7 +55,7 @@ SettingsDialog::SettingsDialog(QWidget *parent) hotkey->key = settings.value(QString("key_%1").arg(i), hotkey->defaultKey()).toUInt(); hotkey->mod = settings.value(QString("modifiers_%1").arg(i), 0).toUInt(); ui.tableWidget->setItem(j,1, new QTableWidgetItem(HotkeyManager::getKeyString(hotkey->key, - hotkey->mod), i)); + hotkey->mod), i)); m_hotkeys << hotkey; } settings.endGroup(); -- cgit v1.2.3-13-gbd6f