VirtualBox

Ignore:
Timestamp:
Mar 29, 2016 7:23:26 PM (9 years ago)
Author:
vboxsync
Message:

FE/Qt: ​​​bugref:8308: Implementing passive event handler (part 01): Initial implementation, not used for now.

Location:
trunk/src/VBox/Frontends/VirtualBox
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VirtualBox/Makefile.kmk

    r60227 r60250  
    532532        src/globals/UIActionPool.cpp \
    533533        src/globals/UIDesktopWidgetWatchdog.cpp \
     534    src/globals/UIMainEventListener.cpp \
    534535        src/globals/UIThreadPool.cpp \
    535536        src/medium/UIMediumEnumerator.cpp \
     
    542543        src/runtime/UIStatusBarEditorWindow.cpp \
    543544        src/selector/UIActionPoolSelector.cpp \
    544         src/selector/UIVirtualBoxEventHandler.cpp \
     545    src/selector/UIVirtualBoxEventHandler.cpp \
    545546        src/selector/UIVMDesktop.cpp \
    546547        src/settings/machine/UIMachineSettingsStorage.cpp \
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/UIMainEventListener.cpp

    r58947 r60250  
    55
    66/*
    7  * Copyright (C) 2010-2015 Oracle Corporation
     7 * Copyright (C) 2010-2016 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    2020#else  /* !VBOX_WITH_PRECOMPILED_HEADERS */
    2121
     22/* Qt includes: */
     23# include <QThread>
     24
    2225/* GUI includes: */
    2326# include "UIMainEventListener.h"
     
    2730# include "COMEnums.h"
    2831# include "CEvent.h"
     32# include "CEventSource.h"
     33# include "CEventListener.h"
    2934# include "CVBoxSVCAvailabilityChangedEvent.h"
    3035# include "CVirtualBoxErrorInfo.h"
     
    5459#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
    5560
     61
     62/** Private QThread extension allowing to listen for Main events in separate thread.
     63  * This thread listens for a Main events infinitely unless creator calls for #setShutdown. */
     64class UIMainEventListeningThread : public QThread
     65{
     66    Q_OBJECT;
     67
     68public:
     69
     70    /** Constructs Main events listener thread redirecting events from @a source to @a listener. */
     71    UIMainEventListeningThread(const CEventSource &source, const CEventListener &listener);
     72    /** Destructs Main events listener thread. */
     73    ~UIMainEventListeningThread();
     74
     75protected:
     76
     77    /** Contains the thread excution body. */
     78    virtual void run() /* override */;
     79
     80    /** Returns whether the thread asked to shutdown prematurely. */
     81    bool isShutdown() const;
     82    /** Defines whether the thread asked to @a fShutdown prematurely. */
     83    void setShutdown(bool fShutdown);
     84
     85private:
     86
     87    /** Holds the Main event source reference. */
     88    CEventSource m_source;
     89    /** Holds the Main event listener reference. */
     90    CEventListener m_listener;
     91
     92    /** Holds the mutex instance which protects thread access. */
     93    mutable QMutex m_mutex;
     94    /** Holds whether the thread asked to shutdown prematurely. */
     95    bool m_fShutdown;
     96};
     97
     98
     99/*********************************************************************************************************************************
     100*   Class UIMainEventListeningThread implementation.                                                                             *
     101*********************************************************************************************************************************/
     102
     103UIMainEventListeningThread::UIMainEventListeningThread(const CEventSource &source, const CEventListener &listener)
     104    : m_source(source)
     105    , m_listener(listener)
     106    , m_fShutdown(false)
     107{
     108}
     109
     110UIMainEventListeningThread::~UIMainEventListeningThread()
     111{
     112    /* Make a request to shutdown: */
     113    setShutdown(true);
     114
     115    /* And wait 30 seconds for run() to finish: */
     116    wait(30000);
     117}
     118
     119void UIMainEventListeningThread::run()
     120{
     121    /* Initialize COM: */
     122    COMBase::InitializeCOM(false);
     123
     124    /* Copy source wrapper to this thread: */
     125    CEventSource source = m_source;
     126    /* Copy listener wrapper to this thread: */
     127    CEventListener listener = m_listener;
     128
     129    /* While we are not in shutdown: */
     130    while (!isShutdown())
     131    {
     132        /* Fetch the event from the queue: */
     133        CEvent event = source.GetEvent(listener, 50);
     134        if (!event.isNull())
     135        {
     136            /* Process the event and tell the listener: */
     137            listener.HandleEvent(event);
     138            if (event.GetWaitable())
     139                source.EventProcessed(listener, event);
     140        }
     141    }
     142
     143    /* Cleanup COM: */
     144    COMBase::CleanupCOM();
     145}
     146
     147bool UIMainEventListeningThread::isShutdown() const
     148{
     149    m_mutex.lock();
     150    bool fShutdown = m_fShutdown;
     151    m_mutex.unlock();
     152    return fShutdown;
     153}
     154
     155void UIMainEventListeningThread::setShutdown(bool fShutdown)
     156{
     157    m_mutex.lock();
     158    m_fShutdown = fShutdown;
     159    m_mutex.unlock();
     160}
     161
     162
     163/*********************************************************************************************************************************
     164*   Class UIMainEventListener implementation.                                                                                    *
     165*********************************************************************************************************************************/
     166
    56167UIMainEventListener::UIMainEventListener()
    57168{
     
    67178}
    68179
     180void UIMainEventListener::registerSource(const CEventSource &source, const CEventListener &listener)
     181{
     182    /* Create thread for passed source: */
     183    m_threads << new UIMainEventListeningThread(source, listener);
     184    /* And start it: */
     185    m_threads.last()->start();
     186}
     187
     188void UIMainEventListener::unregisterSources()
     189{
     190    /* Wipe out the threads: */
     191    qDeleteAll(m_threads);
     192}
     193
    69194STDMETHODIMP UIMainEventListener::HandleEvent(VBoxEventType_T /* type */, IEvent *pEvent)
    70195{
     
    283408}
    284409
     410#include "UIMainEventListener.moc"
     411
  • trunk/src/VBox/Frontends/VirtualBox/src/globals/UIMainEventListener.h

    r58947 r60250  
    55
    66/*
    7  * Copyright (C) 2010-2015 Oracle Corporation
     7 * Copyright (C) 2010-2016 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    2121/* Qt includes: */
    2222#include <QObject>
     23#include <QList>
    2324
    2425/* COM includes: */
     
    3132/* Other VBox includes: */
    3233#include <VBox/com/listeners.h>
     34
     35/* Forward declarations: */
     36class UIMainEventListeningThread;
     37class CEventListener;
     38class CEventSource;
    3339
    3440
     
    116122public:
    117123
    118     /** Constructor. */
     124    /** Constructs main event listener. */
    119125    UIMainEventListener();
    120126
     
    124130    void uninit() {}
    125131
     132    /** Registers event @a source for passive event @a listener. */
     133    void registerSource(const CEventSource &source, const CEventListener &listener);
     134    /** Unregisters event sources. */
     135    void unregisterSources();
     136
    126137    /** Main event handler routine. */
    127138    STDMETHOD(HandleEvent)(VBoxEventType_T enmType, IEvent *pEvent);
     139
     140    /** Holds the list of threads handling passive event listening. */
     141    QList<UIMainEventListeningThread*> m_threads;
    128142};
    129143
     
    132146
    133147#endif /* !___UIMainEventListener_h___ */
     148
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette