⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hidjoystick.cpp

📁 Wxpython Implemented on Windows CE, Source code
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/////////////////////////////////////////////////////////////////////////////
// Name:        src/mac/corefoundation/joystick.cpp
// Purpose:     wxJoystick class
// Author:      Ryan Norton
// Modified by:
// Created:     2/13/2005
// RCS-ID:      $Id: hidjoystick.cpp,v 1.12 2006/05/04 07:56:44 ABX Exp $
// Copyright:   (c) Ryan Norton
// Licence:     wxWindows licence
/////////////////////////////////////////////////////////////////////////////

//===========================================================================
//  DECLARATIONS
//===========================================================================

//---------------------------------------------------------------------------
// Pre-compiled header stuff
//---------------------------------------------------------------------------

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

//---------------------------------------------------------------------------
// Guard
//---------------------------------------------------------------------------

//we only support HID on OSX (DARWIN), since it requires DARWIN...
#if wxUSE_JOYSTICK && defined(__DARWIN__)

//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------

#ifndef WX_PRECOMP
    #include "wx/log.h"
    #include "wx/event.h"   //joystick wxEvents
    #include "wx/window.h"  //for wxWindow to "capture" joystick
#endif

#include "wx/joystick.h"    //...
#include "wx/thread.h"      //wxThread for polling thread/ wxCriticalSection

//private headers
#include "wx/mac/corefoundation/hid.h" //private mac hid stuff

//mac headers
#include <CoreServices/CoreServices.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <unistd.h>

//---------------------------------------------------------------------------
// Definitions/Enumerations
//---------------------------------------------------------------------------

#define wxJS_MAX_AXES       10 /*max number of axes*/
#define wxJS_MAX_BUTTONS    40 /*max number of buttons*/

enum
{
    //These are positions within the cookie array
    //in wxHIDJoystick that the cookies that store the axis' are
    wxJS_AXIS_X = 40,
    wxJS_AXIS_Y,
    wxJS_AXIS_Z,
    wxJS_AXIS_RUDDER,
    wxJS_AXIS_U,
    wxJS_AXIS_V,
};

//---------------------------------------------------------------------------
// wxHIDJoystick
//---------------------------------------------------------------------------
class wxHIDJoystick : public wxHIDDevice
{
public:
    wxHIDJoystick();
    ~wxHIDJoystick();

    bool Create(int nWhich);
    virtual void BuildCookies(CFArrayRef Array);
    void MakeCookies(CFArrayRef Array);
    IOHIDElementCookie* GetCookies();
    IOHIDQueueInterface** GetQueue();

    int  m_nXMax, m_nYMax, m_nZMax, m_nRudderMax, m_nUMax, m_nVMax,
         m_nXMin, m_nYMin, m_nZMin, m_nRudderMin, m_nUMin, m_nVMin;

    friend class wxJoystick;
};

//---------------------------------------------------------------------------
// wxJoystickThread
//---------------------------------------------------------------------------
class wxJoystickThread : public wxThread
{
public:
    wxJoystickThread(wxHIDJoystick* hid, int joystick);
    void* Entry();
    static void HIDCallback(void* target, IOReturn res, void* context, void* sender);

private:
    wxHIDJoystick*       m_hid;
    int       m_joystick;
    wxPoint   m_lastposition;
    int       m_axe[wxJS_MAX_AXES];
    int       m_buttons;
    wxWindow* m_catchwin;
    int       m_polling;

    friend class wxJoystick;
};

//===========================================================================
//  IMPLEMENTATION
//===========================================================================

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// wxGetIntFromCFDictionary
//
// Helper function that gets a integer from a dictionary key
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void wxGetIntFromCFDictionary(CFTypeRef cfDict, CFStringRef key, int* pOut)
{
        CFNumberGetValue(
          (CFNumberRef) CFDictionaryGetValue((CFDictionaryRef) cfDict,
                                              key),
                        kCFNumberIntType, pOut);
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//
// wxJoystick
//
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

IMPLEMENT_DYNAMIC_CLASS(wxJoystick, wxObject)

//---------------------------------------------------------------------------
// wxJoystick Constructor
//
// 1) Initializes member variables
// 2) Attempts to create the native HID joystick implementation - if none
//    could be found (no joysticks, etc.) then it sets it to NULL
//---------------------------------------------------------------------------
wxJoystick::wxJoystick(int joystick)
    : m_joystick(joystick),
      m_thread(NULL)
{
    m_hid = new wxHIDJoystick();

    if (m_hid->Create(m_joystick+1)) //wxHIDDevice is 1-based while this is 0
    {
        m_thread = new wxJoystickThread(m_hid, m_joystick);
        m_thread->Create();
        m_thread->Run();
    }
    else
    {
        delete m_hid;
        m_hid = NULL;
    }
}

//---------------------------------------------------------------------------
// wxJoystick Destructor
//
// Releases the capture of the thread, deletes it, and deletes
// the native implementation.
//---------------------------------------------------------------------------
wxJoystick::~wxJoystick()
{
    ReleaseCapture();
    if (m_thread)
        m_thread->Delete();  // It's detached so it will delete itself

    if (m_hid)
        delete m_hid;
}

//---------------------------------------------------------------------------
// wxJoystick::Get[XXX]Position
//
// Returns the value of an axis that was polled from the thread. In the
// case of GetPosition returns the X and Y values in a wxPoint
//---------------------------------------------------------------------------
wxPoint wxJoystick::GetPosition() const
{
    wxPoint pos(wxDefaultPosition);
    if (m_thread) pos = m_thread->m_lastposition;
    return pos;
}
int wxJoystick::GetZPosition() const
{
    if (m_thread)
        return m_thread->m_axe[wxJS_AXIS_Z];
    return 0;
}
int wxJoystick::GetRudderPosition() const
{
    if (m_thread)
        return m_thread->m_axe[wxJS_AXIS_RUDDER];
    return 0;
}
int wxJoystick::GetUPosition() const
{
    if (m_thread)
        return m_thread->m_axe[wxJS_AXIS_U];
    return 0;
}
int wxJoystick::GetVPosition() const
{
    if (m_thread)
        return m_thread->m_axe[wxJS_AXIS_V];
    return 0;
}

//---------------------------------------------------------------------------
// wxJoystick::GetButtonState
//
// Returns the state of the buttons in a bitmask as dictated by the
// wx manual (the real work takes place in the thread, as always)
//---------------------------------------------------------------------------
int wxJoystick::GetButtonState() const
{
    if (m_thread)
        return m_thread->m_buttons;
    return 0;
}

//---------------------------------------------------------------------------
// wxJoystick::IsOk
//
// Returns whether the joystick initialized successfully - in this case
// if the native implementation doesn't exist (in constructor)
//---------------------------------------------------------------------------
bool wxJoystick::IsOk() const
{
    return m_hid != NULL;
}

//---------------------------------------------------------------------------
// wxJoystick::Get[XXX](Id/Name)
//
// Simple accessors to the native HID implementation
//---------------------------------------------------------------------------
int wxJoystick::GetManufacturerId() const
{
    return m_hid->m_nManufacturerId;
}

int wxJoystick::GetProductId() const
{
    return m_hid->m_nProductId;
}

wxString wxJoystick::GetProductName() const
{
    return m_hid->m_szProductName;
}

//---------------------------------------------------------------------------
// wxJoystick::GetNumberButtons
// wxJoystick::GetNumberAxes
//
// Queries the joystick for an active number of buttons/axes.
//
// In the native HID implementation, the cookies:
// 0-40     are the buttons of the joystick
// 40-50    are the axes of the joystick
//
// These just query the native HID implementation as above.
//---------------------------------------------------------------------------
int wxJoystick::GetNumberButtons() const
{
    int nCount = 0;

    for(int nIndex = 0; nIndex < 40; ++nIndex)
    {
        if(m_hid->HasElement(nIndex))
            ++nCount;
    }

    return nCount;
}
int wxJoystick::GetNumberAxes() const
{
    int nCount = 0;

    for(int nIndex = 40; nIndex < 50; ++nIndex)
    {
        if(m_hid->HasElement(nIndex))
            ++nCount;
    }

    return nCount;
}

//---------------------------------------------------------------------------
// wxJoystick::GetNumberJoysticks
//
// Gets the number of joysticks on the system. In HID that
// is all devices with the kHIDUsage_GD_Joystick or kHIDUsage_GD_GamePad
// identifiers.
//---------------------------------------------------------------------------
int wxJoystick::GetNumberJoysticks()
{
    return
        wxHIDDevice::GetCount(kHIDPage_GenericDesktop, kHIDUsage_GD_Joystick) +
        wxHIDDevice::GetCount(kHIDPage_GenericDesktop, kHIDUsage_GD_GamePad);
}

//---------------------------------------------------------------------------
// wxJoystick::SetCapture
//
// Stops sending events from the thread to the window set in
// SetCapture and stops polling the joystick
//---------------------------------------------------------------------------
bool wxJoystick::SetCapture(wxWindow* win, int pollingFreq)
{
    if (m_thread)
    {
        m_thread->m_catchwin = win;
        m_thread->m_polling = pollingFreq;
        return true;
    }
    return false;
}

//---------------------------------------------------------------------------
// wxJoystick::ReleaseCapture
//
// Stops sending events from the thread to the window set in
// SetCapture and stops polling the joystick
//---------------------------------------------------------------------------
bool wxJoystick::ReleaseCapture()
{
    if (m_thread)
    {
        m_thread->m_catchwin = NULL;
        m_thread->m_polling = 0;
        return true;
    }
    return false;
}

//---------------------------------------------------------------------------
// wxJoystick::Get[XXX]
//
// Gets the minimum and maximum values for each axis, returning 0 if the
// axis doesn't exist.
//---------------------------------------------------------------------------
int wxJoystick::GetXMin() const
{
    return m_hid->m_nXMin;
}

int wxJoystick::GetYMin() const
{
    return m_hid->m_nYMin;
}

int wxJoystick::GetZMin() const
{
    return m_hid->m_nZMin;
}

int wxJoystick::GetRudderMin() const
{
    return m_hid->m_nRudderMin;
}

int wxJoystick::GetUMin() const
{
    return m_hid->m_nUMin;
}

int wxJoystick::GetVMin() const
{
    return m_hid->m_nVMin;
}

int wxJoystick::GetXMax() const
{
    return m_hid->m_nXMax;
}

int wxJoystick::GetYMax() const
{
    return m_hid->m_nYMax;
}

int wxJoystick::GetZMax() const
{
    return m_hid->m_nZMax;
}

int wxJoystick::GetRudderMax() const
{
    return m_hid->m_nRudderMax;
}

int wxJoystick::GetUMax() const
{
    return m_hid->m_nUMax;
}

int wxJoystick::GetVMax() const
{
    return m_hid->m_nVMax;
}

//---------------------------------------------------------------------------
// wxJoystick::Get[XXX]
//
// Min/Max values for buttons, axes, etc.. Polling in this case is just
// what the linux port has.
//---------------------------------------------------------------------------
int wxJoystick::GetMaxButtons() const
{
    return wxJS_MAX_BUTTONS;
}

int wxJoystick::GetMaxAxes() const
{
    return wxJS_MAX_AXES;
}

int wxJoystick::GetPollingMin() const
{
    return 10;
}

int wxJoystick::GetPollingMax() const
{
    return 1000;
}

//---------------------------------------------------------------------------
// wxJoystick::Has[XXX]
//
// Just queries the native hid implementation if the cookie was found
// when enumerating the cookies of the joystick device
//---------------------------------------------------------------------------
bool wxJoystick::HasZ() const
{
    return m_hid->HasElement(wxJS_AXIS_Z);
}

bool wxJoystick::HasRudder() const
{
    return m_hid->HasElement(wxJS_AXIS_RUDDER);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -