Changeset 45:72d5105fcb72 in xplra


Ignore:
Timestamp:
02/16/13 09:02:30 (12 years ago)
Author:
István Váradi <ivaradi@…>
Branch:
default
Phase:
public
Message:

Implemented hotkey handling

Files:
2 added
6 edited

Legend:

Unmodified
Added
Removed
  • misc/client.py

    r39 r45  
    443443            print >> sys.stderr, "Error code:", result
    444444
     445    def do_register_hotkeys(self, args):
     446        words = self._splitArgs(args)
     447        if not words:
     448            print >> sys.stderr, "Missing parameters"
     449            return False
     450
     451        self._writeU8(0x51)
     452        self._writeU32(len(words))
     453        for word in words:
     454            self._writeU16(int(word, 16))
     455        self._flush()
     456
     457        result = self._readU8()
     458        if result!=0:
     459            print >> sys.stderr, "Error code:", result
     460
     461    def do_query_hotkeys(self, args):
     462        self._writeU8(0x52)
     463        self._flush()
     464
     465        result = self._readU8()
     466        if result==0:
     467            length = self._readU32()
     468            values = []
     469            for i in range(0, length):
     470                values.append(self._readU8())
     471            print values
     472        else:
     473            print >> sys.stderr, "Error code:", result
     474
     475    def do_unregister_hotkeys(self, args):
     476        self._writeU8(0x53)
     477        self._flush()
     478
     479        result = self._readU8()
     480        if result!=0:
     481            print >> sys.stderr, "Error code:", result
     482
    445483    def _writeU8(self, x):
    446484        """Write the given value as an unsigned, 8-bit value."""
    447485        self._stream.write(struct.pack("B", x))
     486
     487    def _writeU16(self, x):
     488        """Write the given value as an unsigned, 16-bit value."""
     489        self._stream.write(struct.pack("H", x))
    448490
    449491    def _writeS32(self, x):
  • src/plugin/src/xplra/Globals.h

    r44 r45  
    3333#include "MessageWindow.h"
    3434#include "Menu.h"
     35#include "HotkeyHandler.h"
    3536
    3637//------------------------------------------------------------------------------
     
    6768    Menu menu;
    6869
     70    /**
     71     * The hotkey handler.
     72     */
     73    HotkeyHandler hotkeyHandler;
     74
    6975public:
    7076    /**
     
    8288     */
    8389    MessageWindow& getMessageWindow();
     90
     91    /**
     92     * Get the hotkey handler.
     93     */
     94    HotkeyHandler& getHotkeyHandler();
    8495};
    8596
    8697//------------------------------------------------------------------------------
    8798// Inline definitions
     99//------------------------------------------------------------------------------
     100
     101inline void Globals::getVersions(int& xplaneV, int& xplmV) const
     102{
     103    xplaneV = xplaneVersion;
     104    xplmV = xplmVersion;
     105}
     106
    88107//------------------------------------------------------------------------------
    89108
     
    95114//------------------------------------------------------------------------------
    96115
    97 inline void Globals::getVersions(int& xplaneV, int& xplmV) const
     116inline HotkeyHandler& Globals::getHotkeyHandler()
    98117{
    99     xplaneV = xplaneVersion;
    100     xplmV = xplmVersion;
     118    return hotkeyHandler;
    101119}
    102120
  • src/plugin/src/xplra/Makefile.am

    r44 r45  
    2121        MessageWindow.cc                \
    2222        MessageRequest.cc               \
    23         Menu.cc
     23        Menu.cc                         \
     24        HotkeyHandler.cc
    2425
    2526noinst_HEADERS= \
     
    4142        MessageWindow.h                 \
    4243        MessageRequest.h                \
    43         Menu.h
     44        Menu.h                          \
     45        HotkeyHandler.h
    4446
    4547if TARGET_API_POSIX
  • src/plugin/src/xplra/Protocol.h

    r39 r45  
    107107
    108108    /**
     109     * Command: register a set of hotkeys for the client. Old hotkeys,
     110     * if any, are forgotten.
     111     *
     112     * The command is followed by the following data:
     113     * - The number of the hotkeys defined (U32).
     114     * - The code of the hotkeys (U16*number of the hotkeys).
     115     *   The lower byte is the same as the X-Plane virtual key code
     116     *   (the same as the ASCII code for numbers and upper-case letters),
     117     *   the upper one is a logical OR of the HOTKEY_MODIFIER_XXX
     118     *   codes.
     119     *
     120     * The reply consists of a result code only. It may fail with an
     121     * invalid length code if the number of hotkeys is too large.
     122     */
     123    static const uint8_t COMMAND_REGISTER_HOTKEYS = 0x51;
     124
     125    /**
     126     * Command: query the hotkeys.
     127     *
     128     * The reply consists of a result code, followed by the following
     129     * data, if the result code is RESULT_OK:
     130     * - The number of hotkeys defined (U32).
     131     * - An array of U8 values each being 0 or 1 depending on whether
     132     *   the corresponding hotkey has been pressed since the last
     133     *   query (or the registration, whichever is later). The value at
     134     *   index i corresponds to the hotkey code at index i in the
     135     *   array passed with COMMAND_REGISTER_HOTKEYS.
     136     *
     137     * If not hotkey has been registered, the number of hotkeys is
     138     * returned as 0.
     139     */
     140    static const uint8_t COMMAND_QUERY_HOTKEYS = 0x52;
     141
     142    /**
     143     * Command: unregister the previously registered hotkeys.
     144     *
     145     * The reply is a result code.
     146     */
     147    static const uint8_t COMMAND_UNREGISTER_HOTKEYS = 0x53;
     148
     149    /**
    109150     * Data type: int
    110151     */
     
    185226     */
    186227    static const uint8_t RESULT_OTHER_ERROR = 0xff;
     228
     229    /**
     230     * Hotkey modifier: shift
     231     */
     232    static const uint16_t HOTKEY_MODIFIER_SHIFT = 0x0100;
     233
     234    /**
     235     * Hotkey modifier: control
     236     */
     237    static const uint16_t HOTKEY_MODIFIER_CONTROL = 0x0200;
    187238
    188239    /**
     
    201252     */
    202253    static const float MAX_MESSAGE_DURATION = 5*60;
     254
     255    /**
     256     * The maximal number of hotkeys that can be registered for a
     257     * client.
     258     */
     259    static const size_t MAX_HOTKEY_COUNT = 128;
    203260
    204261    /**
  • src/plugin/src/xplra/ServerThread.cc

    r44 r45  
    7676//------------------------------------------------------------------------------
    7777
     78inline bool ServerThread::destroyHotkeys()
     79{
     80    if (hotkeys!=0) {
     81        listenThread.getGlobals().getHotkeyHandler().unregisterHotkeys(hotkeys);
     82        delete hotkeys; hotkeys = 0;
     83        return true;
     84    } else {
     85        return false;
     86    }
     87}
     88
     89//------------------------------------------------------------------------------
     90
    7891ServerThread::ServerThread(ListenThread& listenThread,
    7992                           RequestQueue& requestQueue, LocalAcceptor& acceptor) :
     
    8497    stream(*bufferedStream),
    8598    nextGetMultiRequestID(1),
    86     nextSetMultiRequestID(1)
     99    nextSetMultiRequestID(1),
     100    hotkeys(0)
    87101{
    88102    instancesMutex.lock();
     
    159173        } else if (command==Protocol::COMMAND_SHOW_MESSAGE) {
    160174            if (!handleShowMessage()) break;
     175        } else if (command==Protocol::COMMAND_REGISTER_HOTKEYS) {
     176            if (!handleRegisterHotkeys()) break;
     177        } else if (command==Protocol::COMMAND_QUERY_HOTKEYS) {
     178            if (!handleQueryHotkeys()) break;
     179        } else if (command==Protocol::COMMAND_UNREGISTER_HOTKEYS) {
     180            if (!handleUnregisterHotkeys()) break;
    161181        } else {
    162182            stream.writeU8(Protocol::RESULT_INVALID_COMMAND);
     
    164184        stream.flush();
    165185    }
     186
     187    destroyHotkeys();
    166188
    167189    Util::debug("hu.varadiistvan.xplra.ServerThread[%p]::run: quitting\n", this);
     
    446468//------------------------------------------------------------------------------
    447469
     470bool ServerThread::handleRegisterHotkeys()
     471{
     472    size_t numHotkeys = stream.readU32();
     473    if (!stream) return false;
     474
     475    if (numHotkeys>Protocol::MAX_HOTKEY_COUNT) {
     476        stream.writeU8(Protocol::RESULT_INVALID_LENGTH);
     477    } else {
     478        HotkeyHandler::Hotkeys* newHotkeys =
     479            new HotkeyHandler::Hotkeys(numHotkeys, stream);
     480        if (newHotkeys->isValid()) {
     481            destroyHotkeys();
     482            hotkeys = newHotkeys;
     483            listenThread.getGlobals().getHotkeyHandler().registerHotkeys(hotkeys);
     484            stream.writeU8(Protocol::RESULT_OK);
     485        } else {
     486            delete newHotkeys;
     487            return false;
     488        }
     489    }
     490
     491    return true;
     492}
     493
     494//------------------------------------------------------------------------------
     495
     496bool ServerThread::handleQueryHotkeys()
     497{
     498    stream.writeU8(Protocol::RESULT_OK);
     499    if (hotkeys==0) {
     500        stream.writeU32(0);
     501    } else {
     502        hotkeys->writePressed(stream);
     503    }
     504    return true;
     505}
     506
     507//------------------------------------------------------------------------------
     508
     509bool ServerThread::handleUnregisterHotkeys()
     510{
     511    destroyHotkeys();
     512    stream.writeU8(Protocol::RESULT_OK);
     513    return true;
     514}
     515
     516//------------------------------------------------------------------------------
     517
    448518// Local Variables:
    449519// mode: C++
  • src/plugin/src/xplra/ServerThread.h

    r39 r45  
    3131//------------------------------------------------------------------------------
    3232
     33#include "HotkeyHandler.h"
     34
    3335#include <hu/varadiistvan/scpl/Thread.h>
    3436
     
    141143    setMultiRequests_t setMultiRequests;
    142144
     145    /**
     146     * The set of hotkeys currently being handled.
     147     */
     148    HotkeyHandler::Hotkeys* hotkeys;
     149
    143150public:
    144151    /**
     
    255262     */
    256263    bool handleShowMessage();
     264
     265    /**
     266     * Handle the COMMAND_REGISTER_HOTKEYS command.
     267     *
     268     * @return true if we can continue, false if the thread should
     269     * quit.
     270     */
     271    bool handleRegisterHotkeys();
     272
     273    /**
     274     * Handle the COMMAND_QUERY_HOTKEYS command.
     275     *
     276     * @return true if we can continue, false if the thread should
     277     * quit.
     278     */
     279    bool handleQueryHotkeys();
     280
     281    /**
     282     * Handle the COMMAND_UNREGISTER_HOTKEYS command.
     283     *
     284     * @return true if we can continue, false if the thread should
     285     * quit.
     286     */
     287    bool handleUnregisterHotkeys();
     288
     289private:
     290    /**
     291     * Destroy the current set of hotkeys, if any.
     292     *
     293     * @return whether there were any hotkeys or not.
     294     */
     295    bool destroyHotkeys();
    257296};
    258297
Note: See TracChangeset for help on using the changeset viewer.