Ticket #10069: mythinputmanager-2009-09-29.diff

File mythinputmanager-2009-09-29.diff, 17.2 KB (added by Xavier Hervy <xavier.hervy@…>, 8 years ago)
  • mythtv/libs/libmythbase/mythdirs.cpp

    diff --git a/mythtv/libs/libmythbase/mythdirs.cpp b/mythtv/libs/libmythbase/mythdirs.cpp
    index 9e43148..305a4e1 100644
    a b static QString themedir = QString::null; 
    1616static QString pluginsdir = QString::null;
    1717static QString translationsdir = QString::null;
    1818static QString filtersdir = QString::null;
     19static QString inputsdir = QString::null;
     20
    1921
    2022void InitializeMythDirs(void)
    2123{
    void InitializeMythDirs(void) 
    6971    pluginsdir = libdir + "plugins/";
    7072    translationsdir = sharedir + "i18n/";
    7173    filtersdir = libdir + "filters/";
     74    inputsdir = libdir + "inputevents/";
    7275}
    7376
    7477QString GetInstallPrefix(void) { return installprefix; }
    QString GetThemesParentDir(void) { return themedir; } 
    7982QString GetPluginsDir(void) { return pluginsdir; }
    8083QString GetTranslationsDir(void) { return translationsdir; }
    8184QString GetFiltersDir(void) { return filtersdir; }
     85QString GetInputsDir(void) { return inputsdir; }
    8286
    8387// These defines provide portability for different
    8488// plugin file names.
  • mythtv/libs/libmythbase/mythdirs.h

    diff --git a/mythtv/libs/libmythbase/mythdirs.h b/mythtv/libs/libmythbase/mythdirs.h
    index 34b1047..855b69d 100644
    a b  
    1414 MBASE_PUBLIC  QString GetPluginsDir(void);
    1515 MBASE_PUBLIC  QString GetTranslationsDir(void);
    1616 MBASE_PUBLIC  QString GetFiltersDir(void);
     17 MBASE_PUBLIC  QString GetInputsDir(void);
    1718
    1819 MBASE_PUBLIC  QString GetPluginsNameFilter(void);
    1920 MBASE_PUBLIC  QString FindPluginName(const QString &plugname);
  • mythtv/libs/libmythui/libmythui.pro

    diff --git a/mythtv/libs/libmythui/libmythui.pro b/mythtv/libs/libmythui/libmythui.pro
    index 32b839e..92f3965 100644
    a b HEADERS += mythgenerictree.h mythuibuttontree.h mythuiutils.h 
    3030HEADERS += mythvirtualkeyboard.h mythuishape.h mythuiguidegrid.h
    3131HEADERS += mythrender_base.h mythfontmanager.h mythuieditbar.h
    3232HEADERS += mythdisplay.h mythuivideo.h mythudplistener.h
    33 HEADERS += mythuiexp.h
     33HEADERS += mythuiexp.h mythinputmanager.h mythinputinterface.h
    3434
    3535SOURCES  = mythmainwindow.cpp mythpainter.cpp mythimage.cpp mythrect.cpp
    3636SOURCES += myththemebase.cpp  mythpainter_qimage.cpp mythpainter_yuva.cpp
    SOURCES += mythgenerictree.cpp mythuibuttontree.cpp mythuiutils.cpp 
    4848SOURCES += mythvirtualkeyboard.cpp mythuishape.cpp mythuiguidegrid.cpp
    4949SOURCES += mythfontmanager.cpp mythuieditbar.cpp
    5050SOURCES += mythdisplay.cpp mythuivideo.cpp mythudplistener.cpp
     51SOURCES += mythinputmanager.cpp
    5152
    5253
    5354inc.path = $${PREFIX}/include/mythtv/libmythui/
  • new file mythtv/libs/libmythui/mythinputinterface.h

    diff --git a/mythtv/libs/libmythui/mythinputinterface.h b/mythtv/libs/libmythui/mythinputinterface.h
    new file mode 100755
    index 0000000..4ea9b7a
    - +  
     1#ifndef MYTHINPUTINTERFACE_H
     2#define MYTHINPUTINTERFACE_H
     3
     4#include <QtPlugin>
     5#include <QKeyEvent>
     6
     7/**
     8 * \file
     9 * This is the only file a plugin need to include, a plugin should normally not be link with libmythui
     10 */
     11
     12class MythInputInterface;
     13
     14/**
     15 * \brief Interface accessible from a input plugin.
     16 */
     17class MythInputManagerInterface : public QObject
     18{
     19    Q_OBJECT
     20  public:
     21    MythInputManagerInterface():QObject(){};
     22
     23    /**
     24     * \brief an event need to be send to MythMainWindow
     25     */
     26    virtual void onKeyPressed(const QKeyEvent &keyEvent)=0;
     27
     28
     29    /**
     30     * \brief get a string setting
     31     */
     32    virtual QString getSetting(const MythInputInterface * input, const QString &key)=0;
     33    /**
     34     * \brief get a string setting with a default value
     35     */
     36    virtual QString getSetting(const MythInputInterface * input, const QString &key, const QString &defaultval)=0;
     37    /**
     38     * \brief save a string setting
     39     */
     40    virtual bool saveSetting(const MythInputInterface * input, const QString &key, const QString &newValue)=0;
     41
     42    /**
     43     * \brief get a integer setting
     44     */
     45    virtual int getNumSetting(const MythInputInterface * input, const QString &key)=0;
     46    /**
     47     * \brief get a integer setting with a default value
     48     */
     49    virtual int getNumSetting(const MythInputInterface * input, const QString &key, int defaultval)=0;
     50    /**
     51     * \brief save an integer setting
     52     */
     53    virtual bool saveNumSetting(const MythInputInterface * input, const QString &key, int newValue)=0;
     54
     55    /**
     56     * \brief get a float setting
     57     */
     58    virtual double getFloatSetting(const MythInputInterface * input, const QString &key)=0;
     59    /**
     60     * \brief get a float setting with a default value
     61     */
     62    virtual double getFloatSetting(const MythInputInterface * input, const QString &key, double defaultval)=0;
     63    /**
     64     * \brief save an float setting
     65     */
     66    virtual bool saveFloatSetting(const MythInputInterface * input, const QString &key, double newValue)=0;
     67
     68  public slots:
     69    /* helper methods so a plugin can log using myth without to link with myth*/
     70    virtual void logDebug(const QString &msg)=0;
     71    virtual void logInfo(const QString &msg)=0;
     72    virtual void logWarning(const QString &msg)=0;
     73    virtual void logError(const QString &msg)=0;
     74};
     75
     76/**
     77 * \defgroup MythUI_Input_Plugin Input plugins
     78 * \ingroup MythUI_Input
     79 * \brief Access to a particular user input device.
     80 *
     81 * Read a device and produce events QKeyEvent and send them to the input manager
     82 */
     83
     84/**
     85 * \interface MythInputInterface
     86 * \brief This is the interface an input plugin should implement.
     87 * \ingroup MythUI_Input
     88 */
     89class MythInputInterface
     90{
     91
     92  public:
     93    virtual ~MythInputInterface(){};
     94
     95    /**
     96     * \brief start the input plugin.
     97     */
     98    virtual void start()=0;
     99
     100    /**
     101     * \brief stop the input plugin.
     102     *
     103     * When a input is stopped it should stopped calling onKeyPressed.
     104     * Any socket or thread should be stopped too.
     105     */
     106    virtual void stop()=0;
     107
     108    /**
     109     * \brief name of the plugin
     110     */
     111    virtual const QString name() const=0;
     112
     113    /**
     114     * \brief human readable description
     115     */
     116    virtual const QString description() const=0;
     117
     118    /**
     119     * \brief set the input manager
     120     * The input manager is where the QKeyEvent need to be sent using \fn MythInputManagerInterface::onKeyPressed
     121     */
     122    virtual void setInputManager(MythInputManagerInterface * inputManager)=0;
     123
     124};
     125
     126Q_DECLARE_INTERFACE(MythInputInterface, "org.mythtv.MythInputInterface/1.0");
     127
     128#endif // MYTHINPUTINTERFACE_H
     129
  • new file mythtv/libs/libmythui/mythinputmanager.cpp

    diff --git a/mythtv/libs/libmythui/mythinputmanager.cpp b/mythtv/libs/libmythui/mythinputmanager.cpp
    new file mode 100755
    index 0000000..38cbfa4
    - +  
     1#include "mythinputmanager.h"
     2
     3
     4//Qt
     5#include <QPluginLoader>
     6#include <QString>
     7#include <QCoreApplication>
     8#include <QDir>
     9//Myth
     10#include "mythinputinterface.h"
     11#include "mythlogging.h"
     12#include "mythdb.h"
     13#include "mythdirs.h"
     14
     15
     16
     17
     18
     19/**
     20 * \class MythInputManager
     21 * \brief This class provide a single entry point to access the different Input available.
     22 *
     23 * The Input Manager is a singleton class which allow to start() and stop() all
     24 * the inputs in one go.
     25 * We can also reload the configuration at any point by calling the restart() method.
     26 * \ingroup MythUI_Input
     27 */
     28
     29MythInputManager * MythInputManager::m_mythInputManager = NULL;
     30
     31MythInputManager::MythInputManager(QObject * mainWindow)
     32    :MythInputManagerInterface(),
     33    m_mainWindow(mainWindow),
     34    m_lockedInputs(false)
     35{
     36    QDir FiltDir(GetInputsDir());
     37
     38    FiltDir.setFilter(QDir::Files | QDir::Readable);
     39    if (FiltDir.exists())
     40    {
     41        QStringList LibList = FiltDir.entryList();
     42        for (QStringList::iterator i = LibList.begin(); i != LibList.end();
     43             ++i)
     44        {
     45            QString path = FiltDir.filePath(*i);
     46            if (path.length() <= 1)
     47                continue;
     48
     49            LOG(VB_GUI, LOG_INFO,
     50                QString("MythInputManager: Loading input plugin '%1'").arg(path));
     51
     52            if (!loadPlugin(path))
     53            {
     54                LOG(VB_GUI, LOG_ERR,
     55                    QString("MythInputManager: Failed to load input plugin: %1").arg(path));
     56            }
     57        }
     58    }
     59    else
     60        LOG(VB_GUI, LOG_ERR,
     61            "MythInputManager: plugin directory '" + FiltDir.absolutePath() + "' doesn't exist?");
     62}
     63
     64const QString MythInputManager::getSettingKey(const MythInputInterface * input, const QString &key)const
     65{
     66    //to ensure that to plugin does not override other plugin settings
     67    if (input==NULL)
     68    {
     69        LOG(VB_GUI, LOG_ERR,QString("MythInputManager::getSettingKey: an input parameter is NULL")); 
     70        return QString("");
     71    }
     72    return QString("inputmanager.%1.%2").arg(input->name()).arg(key);
     73}
     74
     75
     76void MythInputManager::lockInputDevices(bool locked)
     77{
     78    m_lockedInputs=locked;
     79}
     80
     81bool MythInputManager::loadPlugin(QString fileName)
     82{
     83    QPluginLoader pluginLoader(fileName);
     84    QObject *plugin = pluginLoader.instance();
     85    MythInputInterface * inputPlugin;
     86    if (plugin)
     87    {
     88        inputPlugin = qobject_cast<MythInputInterface *>(plugin);
     89        if (inputPlugin)
     90        {
     91            m_inputs[inputPlugin->name()]=inputPlugin;
     92            m_inputs[inputPlugin->name()]->setInputManager(this);
     93            LOG(VB_GUI, LOG_INFO, QString("MythInputManager::loadPlugin(%1): success").arg(inputPlugin->name()));
     94            return true;
     95        }
     96        else
     97        {
     98            LOG(VB_GUI, LOG_ERR, QString("MythInputManager::loadPlugin(%1): wrong interface").arg(fileName));
     99        }
     100    }
     101    else
     102    {
     103        LOG(VB_GUI, LOG_ERR, pluginLoader.errorString ());
     104        LOG(VB_GUI, LOG_ERR, QString("MythInputManager::loadPlugin(%1): can not load").arg(fileName));
     105    }
     106    return false;
     107}
     108
     109MythInputManager::~MythInputManager()
     110{
     111    stop();
     112}
     113
     114/**
     115 * \brief start all the inputs.
     116 *
     117 * The input manager will start the input manager which previously register to it.
     118 */
     119void MythInputManager::start()
     120{
     121    LOG(VB_GUI, LOG_ERR, "MythInputManager::start()");
     122    MythInputInterface *input;
     123    foreach (input , m_inputs)
     124            input->start();
     125}
     126
     127/**
     128 * \brief stop all the inputs
     129 */
     130void MythInputManager::stop()
     131{
     132    LOG(VB_GUI, LOG_ERR, "MythInputManager::stop()");
     133     MythInputInterface *input;
     134     foreach (input , m_inputs)
     135             input->stop();
     136}
     137
     138/* The following method are the implementation of MythInputManagerInterface */
     139
     140QString MythInputManager::getSetting(const MythInputInterface * input, const QString &key)
     141{
     142    return GetMythDB()->GetSettingOnHost(getSettingKey(input,key),
     143        GetMythDB()->GetHostName());
     144}
     145QString MythInputManager::getSetting(const MythInputInterface * input, const QString &key, const QString &defaultval)
     146{
     147    return GetMythDB()->GetSettingOnHost(getSettingKey(input,key),
     148        GetMythDB()->GetHostName(),defaultval);
     149}
     150
     151bool MythInputManager::saveSetting(const MythInputInterface * input, const QString &key, const QString &newValue)
     152{
     153    return GetMythDB()->SaveSettingOnHost(getSettingKey(input,key),newValue,
     154            GetMythDB()->GetHostName());
     155}
     156
     157int MythInputManager::getNumSetting(const MythInputInterface * input, const QString &key)
     158{
     159    return GetMythDB()->GetNumSettingOnHost(getSettingKey(input,key),
     160        GetMythDB()->GetHostName());
     161}
     162int MythInputManager::getNumSetting(const MythInputInterface * input, const QString &key, int defaultval)
     163{
     164    return GetMythDB()->GetNumSettingOnHost(getSettingKey(input,key),
     165        GetMythDB()->GetHostName(), defaultval);
     166}
     167
     168bool MythInputManager::saveNumSetting(const MythInputInterface * input, const QString &key, int newValue)
     169{
     170    QString val = QString::number(newValue);
     171    return saveSetting(input,key,val);
     172}
     173
     174double MythInputManager::getFloatSetting(const MythInputInterface * input, const QString &key)
     175{
     176    return GetMythDB()->GetFloatSettingOnHost(getSettingKey(input,key),
     177        GetMythDB()->GetHostName());
     178}
     179
     180double MythInputManager::getFloatSetting(const MythInputInterface * input, const QString &key, double defaultval)
     181{
     182    return GetMythDB()->GetFloatSettingOnHost(getSettingKey(input,key),
     183        GetMythDB()->GetHostName(), defaultval);
     184}
     185
     186bool MythInputManager::saveFloatSetting(const MythInputInterface * input, const QString &key, double newValue)
     187{
     188    QString val = QString::number(newValue);
     189    return saveSetting(input,key,val);
     190}
     191
     192void MythInputManager::onKeyPressed(const QKeyEvent &keyEvent)
     193{
     194    LOG(VB_GUI, LOG_ERR, "MythInputManager::onKeyPressed: receive keyPress");
     195    if (!m_lockedInputs)
     196    {
     197        QKeyEvent * event = new QKeyEvent(keyEvent);
     198        QCoreApplication::postEvent(m_mainWindow, event);
     199    }
     200}
     201
     202void MythInputManager::logDebug(const QString &msg)
     203{
     204    LOG(VB_GUI, LOG_DEBUG, QString("MythInputManager: %1").arg(msg));
     205}
     206
     207void MythInputManager::logInfo(const QString &msg)
     208{
     209    LOG(VB_GUI, LOG_INFO, QString("MythInputManager: %1").arg(msg));
     210}
     211
     212void MythInputManager::logWarning(const QString &msg)
     213{
     214    LOG(VB_GUI, LOG_WARNING, QString("MythInputManager: %1").arg(msg));
     215}
     216
     217void MythInputManager::logError(const QString &msg)
     218{
     219    LOG(VB_GUI, LOG_ERR, QString("MythInputManager: %1").arg(msg));
     220}
     221
     222
     223
     224
     225
     226
  • new file mythtv/libs/libmythui/mythinputmanager.h

    diff --git a/mythtv/libs/libmythui/mythinputmanager.h b/mythtv/libs/libmythui/mythinputmanager.h
    new file mode 100755
    index 0000000..9201784
    - +  
     1#ifndef MYTHINPUTMANAGER_H
     2#define MYTHINPUTMANAGER_H
     3
     4#include <QObject>
     5#include <QMap>
     6#include <QHash>
     7#include <QString>
     8#include <QMutex>
     9#include <QMutexLocker>
     10#include <QKeyEvent>
     11#include "mythinputinterface.h"
     12
     13class MythMainWindow;
     14
     15
     16class MythInputManager : public MythInputManagerInterface
     17{
     18  public:
     19    MythInputManager(QObject * mainWindow);
     20    ~MythInputManager();
     21
     22    void start();
     23    void restart();
     24    void stop();
     25
     26    const MythInputInterface * getInputByName(const QString &inputName);
     27    QList<const MythInputInterface*> inputs();
     28
     29    void lockInputDevices(bool locked);
     30
     31    /* MythInputManagerInterface*/
     32    QString getSetting(const MythInputInterface * input, const QString &key);
     33    QString getSetting(const MythInputInterface * input, const QString &key, const QString &defaultval);
     34    bool saveSetting(const MythInputInterface * input, const QString &key, const QString &newValue);
     35
     36    int getNumSetting(const MythInputInterface * input, const QString &key);
     37    int getNumSetting(const MythInputInterface * input, const QString &key, int defaultval);
     38    bool saveNumSetting(const MythInputInterface * input, const QString &key, int newValue);
     39
     40    double getFloatSetting(const MythInputInterface * input, const QString &key);
     41    double getFloatSetting(const MythInputInterface * input, const QString &key, double defaultval);
     42    bool saveFloatSetting(const MythInputInterface * input, const QString &key, double newValue);
     43
     44    void onKeyPressed(const QKeyEvent &keyEvent);
     45
     46    void logDebug(const QString &);
     47    void logInfo(const QString &);
     48    void logWarning(const QString &);
     49    void logError(const QString &);
     50    /* MythInputManagerInterface end */
     51  private:
     52    const QString getSettingKey(const MythInputInterface * input, const QString &key)const;
     53    bool loadPlugin(QString filename);
     54    static MythInputManager * m_mythInputManager;
     55
     56    QObject *m_mainWindow;
     57    QMap<QString, MythInputInterface*> m_inputs;
     58
     59    bool m_lockedInputs;
     60
     61};
     62
     63
     64#endif // MYTHINPUTMANAGER_H
  • mythtv/libs/libmythui/mythmainwindow.cpp

    diff --git a/mythtv/libs/libmythui/mythmainwindow.cpp b/mythtv/libs/libmythui/mythmainwindow.cpp
    index e69e4fd..fb3ae51 100644
    a b using namespace std; 
    5252#include "lircevent.h"
    5353#include "mythudplistener.h"
    5454#include "mythrender_base.h"
     55#include "mythinputmanager.h"
    5556
    5657#ifdef USING_APPLEREMOTE
    5758#include "AppleRemoteListener.h"
    class MythMainWindowPrivate 
    173174
    174175        m_udpListener(NULL),
    175176
    176         m_pendingUpdate(false)
     177        m_pendingUpdate(false),
     178        m_inputManager(NULL)
    177179    {
     180
    178181    }
    179182
    180183    int TranslateKeyNum(QKeyEvent *e);
    class MythMainWindowPrivate 
    256259    MythUDPListener *m_udpListener;
    257260
    258261    bool m_pendingUpdate;
     262    MythInputManager * m_inputManager;
    259263};
    260264
    261265// Make keynum in QKeyEvent be equivalent to what's in QKeySequence
    MythMainWindow::MythMainWindow(const bool useDB) 
    423427    d->lircThread = NULL;
    424428    StartLIRC();
    425429
     430    d->m_inputManager = new MythInputManager(this);
     431    d->m_inputManager->start();
     432
     433
    426434#ifdef USE_JOYSTICK_MENU
    427435    d->ignore_joystick_keys = false;
    428436
    MythMainWindow::~MythMainWindow() 
    495503        d->lircThread = NULL;
    496504    }
    497505#endif
     506    if (d->m_inputManager)
     507    {
     508        delete d->m_inputManager;
     509    }
     510
    498511
    499512#ifdef USE_JOYSTICK_MENU
    500513    if (d->joystickThread)
    void MythMainWindow::LockInputDevices( bool locked ) 
    24322445    else
    24332446        LOG(VB_GENERAL, LOG_INFO, "Unlocking input devices");
    24342447
     2448    d->m_inputManager->lockInputDevices(locked);
     2449
    24352450#ifdef USE_LIRC
    24362451    d->ignore_lirc_keys = locked;
    24372452#endif