mirror of
https://github.com/AxioDL/boo.git
synced 2025-12-10 05:57:41 +00:00
Merge branch 'master' of https://github.com/RetroView/libBoo into ds3
This commit is contained in:
1
lib/inputdev/CafeProPad.cpp
Normal file
1
lib/inputdev/CafeProPad.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include "boo/inputdev/CafeProPad.hpp"
|
||||
73
lib/inputdev/DeviceBase.cpp
Normal file
73
lib/inputdev/DeviceBase.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#include "boo/inputdev/DeviceBase.hpp"
|
||||
#include "boo/inputdev/DeviceToken.hpp"
|
||||
#include "IHIDDevice.hpp"
|
||||
#include <cstdarg>
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
DeviceBase::DeviceBase(DeviceToken* token)
|
||||
: m_token(token), m_hidDev(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
DeviceBase::~DeviceBase()
|
||||
{
|
||||
delete m_hidDev;
|
||||
}
|
||||
|
||||
void DeviceBase::_deviceDisconnected()
|
||||
{
|
||||
deviceDisconnected();
|
||||
m_token = NULL;
|
||||
if (m_hidDev)
|
||||
{
|
||||
m_hidDev->_deviceDisconnected();
|
||||
delete m_hidDev;
|
||||
m_hidDev = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void DeviceBase::closeDevice()
|
||||
{
|
||||
if (m_token)
|
||||
m_token->_deviceClose();
|
||||
}
|
||||
|
||||
void DeviceBase::deviceError(const char* error, ...)
|
||||
{
|
||||
va_list vl;
|
||||
va_start(vl, error);
|
||||
vfprintf(stderr, error, vl);
|
||||
va_end(vl);
|
||||
}
|
||||
|
||||
bool DeviceBase::sendUSBInterruptTransfer(const uint8_t* data, size_t length)
|
||||
{
|
||||
if (m_hidDev)
|
||||
return m_hidDev->_sendUSBInterruptTransfer(data, length);
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t DeviceBase::receiveUSBInterruptTransfer(uint8_t* data, size_t length)
|
||||
{
|
||||
if (m_hidDev)
|
||||
return m_hidDev->_receiveUSBInterruptTransfer(data, length);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DeviceBase::sendHIDReport(const uint8_t* data, size_t length, uint16_t message)
|
||||
{
|
||||
if (m_hidDev)
|
||||
return m_hidDev->_sendHIDReport(data, length, message);
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t DeviceBase::receiveReport(uint8_t* data, size_t length, uint16_t message)
|
||||
{
|
||||
if (m_hidDev)
|
||||
return m_hidDev->_recieveReport(data, length, message);
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
78
lib/inputdev/DeviceSignature.cpp
Normal file
78
lib/inputdev/DeviceSignature.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
#include "boo/inputdev/DeviceSignature.hpp"
|
||||
#include "boo/inputdev/DeviceToken.hpp"
|
||||
#include "boo/inputdev/GenericPad.hpp"
|
||||
#include "IHIDDevice.hpp"
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
extern const DeviceSignature BOO_DEVICE_SIGS[];
|
||||
|
||||
|
||||
bool DeviceSignature::DeviceMatchToken(const DeviceToken& token, const TDeviceSignatureSet& sigSet)
|
||||
{
|
||||
if (token.getDeviceType() == DeviceToken::DEVTYPE_GENERICHID)
|
||||
return true;
|
||||
for (const DeviceSignature* sig : sigSet)
|
||||
{
|
||||
if (sig->m_vid == token.getVendorId() && sig->m_pid == token.getProductId())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
IHIDDevice* IHIDDeviceNew(DeviceToken& token, DeviceBase& devImp);
|
||||
DeviceBase* DeviceSignature::DeviceNew(DeviceToken& token)
|
||||
{
|
||||
DeviceBase* retval = NULL;
|
||||
|
||||
/* Early-return for generic HID devices */
|
||||
if (token.getDeviceType() == DeviceToken::DEVTYPE_GENERICHID)
|
||||
{
|
||||
retval = new GenericPad(&token);
|
||||
if (!retval)
|
||||
return NULL;
|
||||
|
||||
IHIDDevice* newDev = IHIDDeviceNew(token, *retval);
|
||||
if (!newDev)
|
||||
{
|
||||
delete retval;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return retval;
|
||||
}
|
||||
|
||||
/* Otherwise perform signature-matching to find the appropriate device-factory */
|
||||
const DeviceSignature* foundSig = NULL;
|
||||
const DeviceSignature* sigIter = BOO_DEVICE_SIGS;
|
||||
unsigned targetVid = token.getVendorId();
|
||||
unsigned targetPid = token.getProductId();
|
||||
while (sigIter->m_name)
|
||||
{
|
||||
if (sigIter->m_vid == targetVid && sigIter->m_pid == targetPid)
|
||||
{
|
||||
foundSig = sigIter;
|
||||
break;
|
||||
}
|
||||
++sigIter;
|
||||
}
|
||||
if (!foundSig)
|
||||
return NULL;
|
||||
|
||||
retval = foundSig->m_factory(&token);
|
||||
if (!retval)
|
||||
return NULL;
|
||||
|
||||
IHIDDevice* newDev = IHIDDeviceNew(token, *retval);
|
||||
if (!newDev)
|
||||
{
|
||||
delete retval;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return retval;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
123
lib/inputdev/DolphinSmashAdapter.cpp
Normal file
123
lib/inputdev/DolphinSmashAdapter.cpp
Normal file
@@ -0,0 +1,123 @@
|
||||
#include "boo/inputdev/DolphinSmashAdapter.hpp"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
/*
|
||||
* Reference: https://github.com/ToadKing/wii-u-gc-adapter/blob/master/wii-u-gc-adapter.c
|
||||
*/
|
||||
|
||||
DolphinSmashAdapter::DolphinSmashAdapter(DeviceToken* token)
|
||||
: DeviceBase(token),
|
||||
m_callback(NULL),
|
||||
m_knownControllers(0),
|
||||
m_rumbleRequest(0),
|
||||
m_rumbleState(0)
|
||||
{
|
||||
}
|
||||
|
||||
DolphinSmashAdapter::~DolphinSmashAdapter()
|
||||
{
|
||||
}
|
||||
|
||||
static inline EDolphinControllerType parseType(unsigned char status)
|
||||
{
|
||||
unsigned char type = status & (DOL_TYPE_NORMAL | DOL_TYPE_WAVEBIRD);
|
||||
switch (type)
|
||||
{
|
||||
case DOL_TYPE_NORMAL:
|
||||
case DOL_TYPE_WAVEBIRD:
|
||||
return (EDolphinControllerType)type;
|
||||
default:
|
||||
return DOL_TYPE_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
static inline EDolphinControllerType
|
||||
parseState(DolphinControllerState* stateOut, uint8_t* payload, bool& rumble)
|
||||
{
|
||||
memset(stateOut, 0, sizeof(DolphinControllerState));
|
||||
unsigned char status = payload[0];
|
||||
EDolphinControllerType type = parseType(status);
|
||||
|
||||
rumble = ((status & 0x04) != 0) ? true : false;
|
||||
|
||||
stateOut->m_btns = (uint16_t)payload[1] << 8 | (uint16_t)payload[2];
|
||||
|
||||
stateOut->m_leftStick[0] = payload[3];
|
||||
stateOut->m_leftStick[1] = payload[4];
|
||||
stateOut->m_rightStick[0] = payload[5];
|
||||
stateOut->m_rightStick[1] = payload[6];
|
||||
stateOut->m_analogTriggers[0] = payload[7];
|
||||
stateOut->m_analogTriggers[1] = payload[8];
|
||||
|
||||
return type;
|
||||
}
|
||||
|
||||
void DolphinSmashAdapter::initialCycle()
|
||||
{
|
||||
uint8_t handshakePayload[] = {0x13};
|
||||
sendUSBInterruptTransfer(handshakePayload, sizeof(handshakePayload));
|
||||
}
|
||||
|
||||
void DolphinSmashAdapter::transferCycle()
|
||||
{
|
||||
uint8_t payload[37];
|
||||
size_t recvSz = receiveUSBInterruptTransfer(payload, sizeof(payload));
|
||||
if (recvSz != 37 || payload[0] != 0x21)
|
||||
return;
|
||||
//printf("RECEIVED DATA %zu %02X\n", recvSz, payload[0]);
|
||||
|
||||
if (!m_callback)
|
||||
return;
|
||||
|
||||
/* Parse controller states */
|
||||
uint8_t* controller = &payload[1];
|
||||
uint8_t rumbleMask = 0;
|
||||
for (int i=0 ; i<4 ; i++, controller += 9)
|
||||
{
|
||||
DolphinControllerState state;
|
||||
bool rumble = false;
|
||||
EDolphinControllerType type = parseState(&state, controller, rumble);
|
||||
if (type && !(m_knownControllers & 1<<i))
|
||||
{
|
||||
m_knownControllers |= 1<<i;
|
||||
m_callback->controllerConnected(i, type);
|
||||
}
|
||||
else if (!type && (m_knownControllers & 1<<i))
|
||||
{
|
||||
m_knownControllers &= ~(1<<i);
|
||||
m_callback->controllerDisconnected(i, type);
|
||||
}
|
||||
if (m_knownControllers & 1<<i)
|
||||
m_callback->controllerUpdate(i, type, state);
|
||||
rumbleMask |= rumble ? 1<<i : 0;
|
||||
}
|
||||
|
||||
/* Send rumble message (if needed) */
|
||||
uint8_t rumbleReq = m_rumbleRequest & rumbleMask;
|
||||
if (rumbleReq != m_rumbleState)
|
||||
{
|
||||
uint8_t rumbleMessage[5] = {0x11};
|
||||
for (int i=0 ; i<4 ; ++i)
|
||||
rumbleMessage[i+1] = (rumbleReq & 1<<i);
|
||||
|
||||
sendUSBInterruptTransfer(rumbleMessage, sizeof(rumbleMessage));
|
||||
m_rumbleState = rumbleReq;
|
||||
}
|
||||
}
|
||||
|
||||
void DolphinSmashAdapter::finalCycle()
|
||||
{
|
||||
uint8_t rumbleMessage[5] = {0x11, 0, 0, 0, 0};
|
||||
sendUSBInterruptTransfer(rumbleMessage, sizeof(rumbleMessage));
|
||||
}
|
||||
|
||||
void DolphinSmashAdapter::deviceDisconnected()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
138
lib/inputdev/DualshockPad.cpp
Normal file
138
lib/inputdev/DualshockPad.cpp
Normal file
@@ -0,0 +1,138 @@
|
||||
#include "boo/inputdev/DualshockPad.hpp"
|
||||
#include <math.h>
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <endian.h>
|
||||
#include <memory.h>
|
||||
|
||||
#define RAD_TO_DEG (180.0/M_PI)
|
||||
|
||||
void hexdump(void *ptr, int buflen) {
|
||||
unsigned char *buf = (unsigned char*)ptr;
|
||||
int i, j;
|
||||
for (i=0; i<buflen; i+=16) {
|
||||
printf("%06x: ", i);
|
||||
for (j=0; j<16; j++)
|
||||
if (i+j < buflen)
|
||||
printf("%02x ", buf[i+j]);
|
||||
else
|
||||
printf(" ");
|
||||
printf(" ");
|
||||
for (j=0; j<16; j++)
|
||||
if (i+j < buflen)
|
||||
printf("%c", isprint(buf[i+j]) ? buf[i+j] : '.');
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
namespace boo
|
||||
{
|
||||
static const uint8_t defaultReport[35] = {
|
||||
0x01, 0xff, 0x00, 0xff, 0x00,
|
||||
0xff, 0x80, 0x00, 0x00, 0x00,
|
||||
0xff, 0x27, 0x10, 0x00, 0x32,
|
||||
0xff, 0x27, 0x10, 0x00, 0x32,
|
||||
0xff, 0x27, 0x10, 0x00, 0x32,
|
||||
0xff, 0x27, 0x10, 0x00, 0x32,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00
|
||||
};
|
||||
|
||||
DualshockPad::DualshockPad(DeviceToken* token)
|
||||
: DeviceBase(token),
|
||||
m_callback(nullptr),
|
||||
m_rumbleRequest(0),
|
||||
m_rumbleState(0)
|
||||
{
|
||||
memcpy(m_report.buf, defaultReport, 35);
|
||||
}
|
||||
|
||||
DualshockPad::~DualshockPad()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void DualshockPad::deviceDisconnected()
|
||||
{
|
||||
if (m_callback)
|
||||
m_callback->controllerDisconnected();
|
||||
}
|
||||
|
||||
void DualshockPad::initialCycle()
|
||||
{
|
||||
uint8_t setupCommand[4] = {0x42, 0x0c, 0x00, 0x00}; //Tells controller to start sending changes on in pipe
|
||||
if (!sendHIDReport(setupCommand, sizeof(setupCommand), 0x03F4))
|
||||
{
|
||||
deviceError("Unable to send complete packet! Request size %x\n", sizeof(setupCommand));
|
||||
return;
|
||||
}
|
||||
uint8_t btAddr[8];
|
||||
receiveReport(btAddr, sizeof(btAddr), 0x03F5);
|
||||
for (int i = 0; i < 6; i++)
|
||||
m_btAddress[5 - i] = btAddr[i + 2]; // Copy into buffer reversed, so it is LSB first
|
||||
}
|
||||
|
||||
void DualshockPad::transferCycle()
|
||||
{
|
||||
DualshockControllerState state;
|
||||
size_t recvSz = receiveUSBInterruptTransfer((uint8_t*)&state, 49);
|
||||
if (recvSz != 49)
|
||||
return;
|
||||
|
||||
for (int i = 0; i < 3; i++)
|
||||
state.m_accelerometer[i] = be16toh(state.m_accelerometer[i]);
|
||||
|
||||
state.m_gyrometerZ = be16toh(state.m_gyrometerZ);
|
||||
if (m_callback)
|
||||
m_callback->controllerUpdate(state);
|
||||
|
||||
if (m_rumbleRequest != m_rumbleState)
|
||||
{
|
||||
if (m_rumbleRequest & DS3_MOTOR_LEFT)
|
||||
{
|
||||
m_report.rumble.leftDuration = m_rumbleDuration[0];
|
||||
m_report.rumble.leftForce = m_rumbleIntensity[0];
|
||||
}
|
||||
else
|
||||
{
|
||||
m_report.rumble.leftDuration = 0;
|
||||
m_report.rumble.leftForce = 0;
|
||||
}
|
||||
|
||||
if (m_rumbleRequest & DS3_MOTOR_RIGHT)
|
||||
{
|
||||
m_report.rumble.rightDuration = m_rumbleDuration[0];
|
||||
m_report.rumble.rightOn = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_report.rumble.rightDuration = 0;
|
||||
m_report.rumble.rightOn = false;
|
||||
}
|
||||
sendHIDReport(m_report.buf, sizeof(m_report), 0x0201);
|
||||
m_rumbleState = m_rumbleRequest;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (state.m_reserved5[8] & 0x80)
|
||||
m_rumbleRequest &= ~DS3_MOTOR_RIGHT;
|
||||
if (state.m_reserved5[7] & 0x01)
|
||||
m_rumbleRequest &= ~DS3_MOTOR_LEFT;
|
||||
m_rumbleState = m_rumbleRequest;
|
||||
const double zeroG = 511.5; // 1.65/3.3*1023 (1,65V);
|
||||
float accXval = -((double)state.m_accelerometer[0] - zeroG);
|
||||
float accYval = -((double)state.m_accelerometer[1] - zeroG);
|
||||
float accZval = -((double)state.m_accelerometer[2] - zeroG);
|
||||
state.accPitch = (atan2(accYval, accZval) + M_PI) * RAD_TO_DEG;
|
||||
state.accYaw = (atan2(accXval, accZval) + M_PI) * RAD_TO_DEG;
|
||||
state.gyroZ = (state.m_gyrometerZ / 1023.f);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void DualshockPad::finalCycle()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
} // boo
|
||||
23
lib/inputdev/GenericPad.cpp
Normal file
23
lib/inputdev/GenericPad.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "boo/inputdev/GenericPad.hpp"
|
||||
#include "boo/inputdev/DeviceToken.hpp"
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
GenericPad::GenericPad(DeviceToken* token)
|
||||
: DeviceBase(token)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
GenericPad::~GenericPad()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void GenericPad::deviceDisconnected()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
256
lib/inputdev/HIDDeviceIOKit.cpp
Normal file
256
lib/inputdev/HIDDeviceIOKit.cpp
Normal file
@@ -0,0 +1,256 @@
|
||||
#include "IHIDDevice.hpp"
|
||||
#include "inputdev/CDeviceToken.hpp"
|
||||
#include "inputdev/CDeviceBase.hpp"
|
||||
#include <IOKit/hid/IOHIDLib.h>
|
||||
#include <IOKit/usb/IOUSBLib.h>
|
||||
#include <IOKit/IOCFPlugIn.h>
|
||||
#include <thread>
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
class CHIDDeviceIOKit final : public IHIDDevice
|
||||
{
|
||||
CDeviceToken& m_token;
|
||||
CDeviceBase& m_devImp;
|
||||
|
||||
IOUSBInterfaceInterface** m_usbIntf = NULL;
|
||||
uint8_t m_usbIntfInPipe = 0;
|
||||
uint8_t m_usbIntfOutPipe = 0;
|
||||
bool m_runningTransferLoop = false;
|
||||
|
||||
const std::string& m_devPath;
|
||||
std::mutex m_initMutex;
|
||||
std::condition_variable m_initCond;
|
||||
std::thread* m_thread;
|
||||
|
||||
bool _sendUSBInterruptTransfer(const uint8_t* data, size_t length)
|
||||
{
|
||||
if (m_usbIntf)
|
||||
{
|
||||
IOReturn res = (*m_usbIntf)->WritePipe(m_usbIntf, m_usbIntfOutPipe, (void*)data, length);
|
||||
return res == kIOReturnSuccess;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t _receiveUSBInterruptTransfer(uint8_t* data, size_t length)
|
||||
{
|
||||
if (m_usbIntf)
|
||||
{
|
||||
UInt32 readSize = length;
|
||||
IOReturn res = (*m_usbIntf)->ReadPipe(m_usbIntf, m_usbIntfInPipe, data, &readSize);
|
||||
if (res != kIOReturnSuccess)
|
||||
return 0;
|
||||
return readSize;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void _threadProcUSBLL(CHIDDeviceIOKit* device)
|
||||
{
|
||||
char thrName[128];
|
||||
snprintf(thrName, 128, "%s Transfer Thread", device->m_token.getProductName().c_str());
|
||||
pthread_setname_np(thrName);
|
||||
char errStr[256];
|
||||
std::unique_lock<std::mutex> lk(device->m_initMutex);
|
||||
|
||||
/* Get the HID element's parent (USB interrupt transfer-interface) */
|
||||
io_iterator_t devIter;
|
||||
io_registry_entry_t devEntry = IORegistryEntryFromPath(kIOMasterPortDefault, device->m_devPath.c_str());
|
||||
IORegistryEntryGetChildIterator(devEntry, kIOServicePlane, &devIter);
|
||||
io_object_t obj, interfaceEntry = 0;
|
||||
while ((obj = IOIteratorNext(devIter)))
|
||||
{
|
||||
if (IOObjectConformsTo(obj, kIOUSBInterfaceClassName))
|
||||
interfaceEntry = obj;
|
||||
else
|
||||
IOObjectRelease(obj);
|
||||
}
|
||||
if (!interfaceEntry)
|
||||
{
|
||||
snprintf(errStr, 256, "Unable to find interface for %s@%s\n",
|
||||
device->m_token.getProductName().c_str(),
|
||||
device->m_devPath.c_str());
|
||||
device->m_devImp.deviceError(errStr);
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
return;
|
||||
}
|
||||
|
||||
/* IOKit Plugin COM interface (WTF Apple???) */
|
||||
IOCFPlugInInterface **iodev;
|
||||
SInt32 score;
|
||||
IOReturn err;
|
||||
err = IOCreatePlugInInterfaceForService(interfaceEntry,
|
||||
kIOUSBInterfaceUserClientTypeID,
|
||||
kIOCFPlugInInterfaceID,
|
||||
&iodev,
|
||||
&score);
|
||||
IOObjectRelease(interfaceEntry);
|
||||
if (err)
|
||||
{
|
||||
snprintf(errStr, 256, "Unable to open %s@%s\n",
|
||||
device->m_token.getProductName().c_str(), device->m_devPath.c_str());
|
||||
device->m_devImp.deviceError(errStr);
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
return;
|
||||
}
|
||||
|
||||
/* USB interface function-pointer table */
|
||||
IOUSBInterfaceInterface** intf = NULL;
|
||||
err = (*iodev)->QueryInterface(iodev,
|
||||
CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID),
|
||||
(LPVOID*)&intf);
|
||||
if (err)
|
||||
{
|
||||
snprintf(errStr, 256, "Unable to open %s@%s\n",
|
||||
device->m_token.getProductName().c_str(), device->m_devPath.c_str());
|
||||
device->m_devImp.deviceError(errStr);
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
IODestroyPlugInInterface(iodev);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Obtain exclusive lock on device */
|
||||
device->m_usbIntf = intf;
|
||||
err = (*intf)->USBInterfaceOpen(intf);
|
||||
if (err != kIOReturnSuccess)
|
||||
{
|
||||
if (err == kIOReturnExclusiveAccess)
|
||||
{
|
||||
snprintf(errStr, 256, "Unable to open %s@%s: someone else using it\n",
|
||||
device->m_token.getProductName().c_str(), device->m_devPath.c_str());
|
||||
device->m_devImp.deviceError(errStr);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(errStr, 256, "Unable to open %s@%s\n",
|
||||
device->m_token.getProductName().c_str(), device->m_devPath.c_str());
|
||||
device->m_devImp.deviceError(errStr);
|
||||
}
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
(*intf)->Release(intf);
|
||||
IODestroyPlugInInterface(iodev);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Determine pipe indices for interrupt I/O */
|
||||
UInt8 numEndpoints = 0;
|
||||
err = (*intf)->GetNumEndpoints(intf, &numEndpoints);
|
||||
for (int i=1 ; i<numEndpoints+1 ; ++i)
|
||||
{
|
||||
UInt8 dir, num, tType, interval;
|
||||
UInt16 mPacketSz;
|
||||
err = (*intf)->GetPipeProperties(intf, i, &dir, &num, &tType, &mPacketSz, &interval);
|
||||
if (tType == kUSBInterrupt)
|
||||
{
|
||||
if (dir == kUSBIn)
|
||||
device->m_usbIntfInPipe = num;
|
||||
else if (dir == kUSBOut)
|
||||
device->m_usbIntfOutPipe = num;
|
||||
}
|
||||
}
|
||||
|
||||
/* Return control to main thread */
|
||||
device->m_runningTransferLoop = true;
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
|
||||
/* Start transfer loop */
|
||||
device->m_devImp.initialCycle();
|
||||
while (device->m_runningTransferLoop)
|
||||
device->m_devImp.transferCycle();
|
||||
device->m_devImp.finalCycle();
|
||||
|
||||
/* Cleanup */
|
||||
(*intf)->USBInterfaceClose(intf);
|
||||
(*intf)->Release(intf);
|
||||
IODestroyPlugInInterface(iodev);
|
||||
device->m_usbIntf = NULL;
|
||||
|
||||
}
|
||||
|
||||
static void _threadProcBTLL(CHIDDeviceIOKit* device)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(device->m_initMutex);
|
||||
|
||||
/* Return control to main thread */
|
||||
device->m_runningTransferLoop = true;
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
|
||||
/* Start transfer loop */
|
||||
device->m_devImp.initialCycle();
|
||||
while (device->m_runningTransferLoop)
|
||||
device->m_devImp.transferCycle();
|
||||
device->m_devImp.finalCycle();
|
||||
|
||||
}
|
||||
|
||||
static void _threadProcHID(CHIDDeviceIOKit* device)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(device->m_initMutex);
|
||||
|
||||
/* Return control to main thread */
|
||||
device->m_runningTransferLoop = true;
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
|
||||
/* Start transfer loop */
|
||||
device->m_devImp.initialCycle();
|
||||
while (device->m_runningTransferLoop)
|
||||
device->m_devImp.transferCycle();
|
||||
device->m_devImp.finalCycle();
|
||||
}
|
||||
|
||||
void _deviceDisconnected()
|
||||
{
|
||||
m_runningTransferLoop = false;
|
||||
}
|
||||
|
||||
bool _sendHIDReport(const uint8_t* data, size_t length, uint16_t message)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
CHIDDeviceIOKit(CDeviceToken& token, CDeviceBase& devImp)
|
||||
: m_token(token),
|
||||
m_devImp(devImp),
|
||||
m_devPath(token.getDevicePath())
|
||||
{
|
||||
devImp.m_hidDev = this;
|
||||
std::unique_lock<std::mutex> lk(m_initMutex);
|
||||
CDeviceToken::TDeviceType dType = token.getDeviceType();
|
||||
if (dType == CDeviceToken::DEVTYPE_USB)
|
||||
m_thread = new std::thread(_threadProcUSBLL, this);
|
||||
else if (dType == CDeviceToken::DEVTYPE_BLUETOOTH)
|
||||
m_thread = new std::thread(_threadProcBTLL, this);
|
||||
else if (dType == CDeviceToken::DEVTYPE_GENERICHID)
|
||||
m_thread = new std::thread(_threadProcHID, this);
|
||||
else
|
||||
throw std::runtime_error("invalid token supplied to device constructor");
|
||||
m_initCond.wait(lk);
|
||||
}
|
||||
|
||||
~CHIDDeviceIOKit()
|
||||
{
|
||||
m_runningTransferLoop = false;
|
||||
m_thread->join();
|
||||
delete m_thread;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
IHIDDevice* IHIDDeviceNew(CDeviceToken& token, CDeviceBase& devImp)
|
||||
{
|
||||
return new CHIDDeviceIOKit(token, devImp);
|
||||
}
|
||||
|
||||
}
|
||||
275
lib/inputdev/HIDDeviceUdev.cpp
Normal file
275
lib/inputdev/HIDDeviceUdev.cpp
Normal file
@@ -0,0 +1,275 @@
|
||||
#include "IHIDDevice.hpp"
|
||||
#include "boo/inputdev/DeviceToken.hpp"
|
||||
#include "boo/inputdev/DeviceBase.hpp"
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <libudev.h>
|
||||
#include <stropts.h>
|
||||
#include <linux/usb/ch9.h>
|
||||
#include <linux/usbdevice_fs.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
udev* GetUdev();
|
||||
|
||||
/*
|
||||
* Reference: http://tali.admingilde.org/linux-docbook/usb/ch07s06.html
|
||||
*/
|
||||
|
||||
class HIDDeviceUdev final : public IHIDDevice
|
||||
{
|
||||
DeviceToken& m_token;
|
||||
DeviceBase& m_devImp;
|
||||
|
||||
int m_devFd = 0;
|
||||
unsigned m_usbIntfInPipe = 0;
|
||||
unsigned m_usbIntfOutPipe = 0;
|
||||
bool m_runningTransferLoop = false;
|
||||
|
||||
const std::string& m_devPath;
|
||||
std::mutex m_initMutex;
|
||||
std::condition_variable m_initCond;
|
||||
std::thread* m_thread;
|
||||
|
||||
bool _sendUSBInterruptTransfer(const uint8_t* data, size_t length)
|
||||
{
|
||||
if (m_devFd)
|
||||
{
|
||||
usbdevfs_bulktransfer xfer =
|
||||
{
|
||||
m_usbIntfOutPipe | USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
|
||||
(unsigned)length,
|
||||
0,
|
||||
(void*)data
|
||||
};
|
||||
int ret = ioctl(m_devFd, USBDEVFS_BULK, &xfer);
|
||||
if (ret != (int)length)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t _receiveUSBInterruptTransfer(uint8_t* data, size_t length)
|
||||
{
|
||||
if (m_devFd)
|
||||
{
|
||||
usbdevfs_bulktransfer xfer =
|
||||
{
|
||||
m_usbIntfInPipe | USB_DIR_IN,
|
||||
(unsigned)length,
|
||||
0,
|
||||
data
|
||||
};
|
||||
return ioctl(m_devFd, USBDEVFS_BULK, &xfer);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void _threadProcUSBLL(HIDDeviceUdev* device)
|
||||
{
|
||||
unsigned i;
|
||||
char errStr[256];
|
||||
std::unique_lock<std::mutex> lk(device->m_initMutex);
|
||||
udev_device* udevDev = udev_device_new_from_syspath(GetUdev(), device->m_devPath.c_str());
|
||||
|
||||
/* Get device file */
|
||||
const char* dp = udev_device_get_devnode(udevDev);
|
||||
device->m_devFd = open(dp, O_RDWR);
|
||||
if (device->m_devFd < 0)
|
||||
{
|
||||
snprintf(errStr, 256, "Unable to open %s@%s: %s\n",
|
||||
device->m_token.getProductName().c_str(), dp, strerror(errno));
|
||||
device->m_devImp.deviceError(errStr);
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
udev_device_unref(udevDev);
|
||||
return;
|
||||
}
|
||||
usb_device_descriptor devDesc = {};
|
||||
read(device->m_devFd, &devDesc, 1);
|
||||
read(device->m_devFd, &devDesc.bDescriptorType, devDesc.bLength-1);
|
||||
if (devDesc.bNumConfigurations)
|
||||
{
|
||||
usb_config_descriptor confDesc = {};
|
||||
read(device->m_devFd, &confDesc, 1);
|
||||
read(device->m_devFd, &confDesc.bDescriptorType, confDesc.bLength-1);
|
||||
if (confDesc.bNumInterfaces)
|
||||
{
|
||||
usb_interface_descriptor intfDesc = {};
|
||||
read(device->m_devFd, &intfDesc, 1);
|
||||
read(device->m_devFd, &intfDesc.bDescriptorType, intfDesc.bLength-1);
|
||||
for (i=0 ; i<intfDesc.bNumEndpoints+1 ; ++i)
|
||||
{
|
||||
usb_endpoint_descriptor endpDesc = {};
|
||||
read(device->m_devFd, &endpDesc, 1);
|
||||
read(device->m_devFd, &endpDesc.bDescriptorType, endpDesc.bLength-1);
|
||||
if ((endpDesc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) == USB_ENDPOINT_XFER_INT)
|
||||
{
|
||||
if ((endpDesc.bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_IN)
|
||||
device->m_usbIntfInPipe = endpDesc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
|
||||
else if ((endpDesc.bEndpointAddress & USB_ENDPOINT_DIR_MASK) == USB_DIR_OUT)
|
||||
device->m_usbIntfOutPipe = endpDesc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Request that kernel disconnects existing driver */
|
||||
usbdevfs_ioctl disconnectReq = {
|
||||
0,
|
||||
USBDEVFS_DISCONNECT,
|
||||
NULL
|
||||
};
|
||||
ioctl(device->m_devFd, USBDEVFS_IOCTL, &disconnectReq);
|
||||
|
||||
/* Return control to main thread */
|
||||
device->m_runningTransferLoop = true;
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
|
||||
/* Start transfer loop */
|
||||
device->m_devImp.initialCycle();
|
||||
while (device->m_runningTransferLoop)
|
||||
device->m_devImp.transferCycle();
|
||||
device->m_devImp.finalCycle();
|
||||
|
||||
/* Cleanup */
|
||||
close(device->m_devFd);
|
||||
device->m_devFd = 0;
|
||||
udev_device_unref(udevDev);
|
||||
|
||||
}
|
||||
|
||||
static void _threadProcBTLL(HIDDeviceUdev* device)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(device->m_initMutex);
|
||||
udev_device* udevDev = udev_device_new_from_syspath(GetUdev(), device->m_devPath.c_str());
|
||||
|
||||
/* Return control to main thread */
|
||||
device->m_runningTransferLoop = true;
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
|
||||
/* Start transfer loop */
|
||||
device->m_devImp.initialCycle();
|
||||
while (device->m_runningTransferLoop)
|
||||
device->m_devImp.transferCycle();
|
||||
device->m_devImp.finalCycle();
|
||||
|
||||
udev_device_unref(udevDev);
|
||||
}
|
||||
|
||||
static void _threadProcHID(HIDDeviceUdev* device)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(device->m_initMutex);
|
||||
udev_device* udevDev = udev_device_new_from_syspath(GetUdev(), device->m_devPath.c_str());
|
||||
|
||||
/* Return control to main thread */
|
||||
device->m_runningTransferLoop = true;
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
|
||||
/* Start transfer loop */
|
||||
device->m_devImp.initialCycle();
|
||||
while (device->m_runningTransferLoop)
|
||||
device->m_devImp.transferCycle();
|
||||
device->m_devImp.finalCycle();
|
||||
|
||||
udev_device_unref(udevDev);
|
||||
}
|
||||
|
||||
void _deviceDisconnected()
|
||||
{
|
||||
m_runningTransferLoop = false;
|
||||
}
|
||||
|
||||
bool _sendHIDReport(const uint8_t* data, size_t length, uint16_t message)
|
||||
{
|
||||
if (m_devFd)
|
||||
{
|
||||
usbdevfs_ctrltransfer xfer =
|
||||
{
|
||||
USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
|
||||
0x09, // HID_SET_REPORT
|
||||
message,
|
||||
0,
|
||||
(uint16_t)length,
|
||||
0,
|
||||
(void*)data
|
||||
};
|
||||
int ret = ioctl(m_devFd, USBDEVFS_CONTROL, &xfer);
|
||||
if (ret != (int)length)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t _recieveReport(const uint8_t *data, size_t length, uint16_t message)
|
||||
{
|
||||
if (m_devFd)
|
||||
{
|
||||
usbdevfs_ctrltransfer xfer =
|
||||
{
|
||||
USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
|
||||
0x01, // HID_GET_REPORT
|
||||
message,
|
||||
0,
|
||||
(uint16_t)length,
|
||||
0,
|
||||
(void*)data
|
||||
};
|
||||
return ioctl(m_devFd, USBDEVFS_CONTROL, &xfer);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
HIDDeviceUdev(DeviceToken& token, DeviceBase& devImp)
|
||||
: m_token(token),
|
||||
m_devImp(devImp),
|
||||
m_devPath(token.getDevicePath())
|
||||
{
|
||||
devImp.m_hidDev = this;
|
||||
std::unique_lock<std::mutex> lk(m_initMutex);
|
||||
DeviceToken::TDeviceType dType = token.getDeviceType();
|
||||
if (dType == DeviceToken::DEVTYPE_USB)
|
||||
m_thread = new std::thread(_threadProcUSBLL, this);
|
||||
else if (dType == DeviceToken::DEVTYPE_BLUETOOTH)
|
||||
m_thread = new std::thread(_threadProcBTLL, this);
|
||||
else if (dType == DeviceToken::DEVTYPE_GENERICHID)
|
||||
m_thread = new std::thread(_threadProcHID, this);
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "invalid token supplied to device constructor");
|
||||
abort();
|
||||
}
|
||||
m_initCond.wait(lk);
|
||||
}
|
||||
|
||||
~HIDDeviceUdev()
|
||||
{
|
||||
m_runningTransferLoop = false;
|
||||
m_thread->join();
|
||||
delete m_thread;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
IHIDDevice* IHIDDeviceNew(DeviceToken& token, DeviceBase& devImp)
|
||||
{
|
||||
return new HIDDeviceUdev(token, devImp);
|
||||
}
|
||||
|
||||
}
|
||||
224
lib/inputdev/HIDDeviceWinUSB.cpp
Normal file
224
lib/inputdev/HIDDeviceWinUSB.cpp
Normal file
@@ -0,0 +1,224 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1 /* STFU MSVC */
|
||||
#include "IHIDDevice.hpp"
|
||||
#include "inputdev/CDeviceToken.hpp"
|
||||
#include "inputdev/CDeviceBase.hpp"
|
||||
#include <thread>
|
||||
#include <mutex>
|
||||
#include <condition_variable>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#define _WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <winusb.h>
|
||||
#include <usb100.h>
|
||||
#include <Winusbio.h>
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
class CHIDDeviceWinUSB final : public IHIDDevice
|
||||
{
|
||||
CDeviceToken& m_token;
|
||||
CDeviceBase& m_devImp;
|
||||
|
||||
HANDLE m_devHandle = 0;
|
||||
WINUSB_INTERFACE_HANDLE m_usbHandle = NULL;
|
||||
unsigned m_usbIntfInPipe = 0;
|
||||
unsigned m_usbIntfOutPipe = 0;
|
||||
bool m_runningTransferLoop = false;
|
||||
|
||||
const std::string& m_devPath;
|
||||
std::mutex m_initMutex;
|
||||
std::condition_variable m_initCond;
|
||||
std::thread* m_thread;
|
||||
|
||||
bool _sendUSBInterruptTransfer(const uint8_t* data, size_t length)
|
||||
{
|
||||
if (m_usbHandle)
|
||||
{
|
||||
ULONG lengthTransferred = 0;
|
||||
if (!WinUsb_WritePipe(m_usbHandle, m_usbIntfOutPipe, (PUCHAR)data,
|
||||
(ULONG)length, &lengthTransferred, NULL) ||
|
||||
lengthTransferred != length)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
size_t _receiveUSBInterruptTransfer(uint8_t* data, size_t length)
|
||||
{
|
||||
if (m_usbHandle)
|
||||
{
|
||||
ULONG lengthTransferred = 0;
|
||||
if (!WinUsb_ReadPipe(m_usbHandle, m_usbIntfInPipe, (PUCHAR)data,
|
||||
(ULONG)length, &lengthTransferred, NULL))
|
||||
return 0;
|
||||
return lengthTransferred;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void _threadProcUSBLL(CHIDDeviceWinUSB* device)
|
||||
{
|
||||
unsigned i;
|
||||
char errStr[256];
|
||||
std::unique_lock<std::mutex> lk(device->m_initMutex);
|
||||
|
||||
/* POSIX.. who needs it?? -MS */
|
||||
device->m_devHandle = CreateFileA(device->m_devPath.c_str(),
|
||||
GENERIC_WRITE | GENERIC_READ,
|
||||
FILE_SHARE_WRITE | FILE_SHARE_READ,
|
||||
NULL,
|
||||
OPEN_EXISTING,
|
||||
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED,
|
||||
NULL);
|
||||
if (INVALID_HANDLE_VALUE == device->m_devHandle)
|
||||
{
|
||||
_snprintf(errStr, 256, "Unable to open %s@%s: %d\n",
|
||||
device->m_token.getProductName().c_str(),
|
||||
device->m_devPath.c_str(), GetLastError());
|
||||
device->m_devImp.deviceError(errStr);
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!WinUsb_Initialize(device->m_devHandle, &device->m_usbHandle))
|
||||
{
|
||||
_snprintf(errStr, 256, "Unable to open %s@%s: %d\n",
|
||||
device->m_token.getProductName().c_str(),
|
||||
device->m_devPath.c_str(), GetLastError());
|
||||
device->m_devImp.deviceError(errStr);
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
CloseHandle(device->m_devHandle);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Enumerate device pipes */
|
||||
USB_INTERFACE_DESCRIPTOR ifDesc = {0};
|
||||
if (!WinUsb_QueryInterfaceSettings(device->m_usbHandle, 0, &ifDesc))
|
||||
{
|
||||
_snprintf(errStr, 256, "Unable to open %s@%s: %d\n",
|
||||
device->m_token.getProductName().c_str(),
|
||||
device->m_devPath.c_str(), GetLastError());
|
||||
device->m_devImp.deviceError(errStr);
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
CloseHandle(device->m_devHandle);
|
||||
return;
|
||||
}
|
||||
for (i=0 ; i<ifDesc.bNumEndpoints ; ++i)
|
||||
{
|
||||
WINUSB_PIPE_INFORMATION pipeDesc;
|
||||
WinUsb_QueryPipe(device->m_usbHandle, 0, i, &pipeDesc);
|
||||
if (pipeDesc.PipeType == UsbdPipeTypeInterrupt)
|
||||
{
|
||||
if (USB_ENDPOINT_DIRECTION_IN(pipeDesc.PipeId))
|
||||
device->m_usbIntfInPipe = pipeDesc.PipeId;
|
||||
else
|
||||
device->m_usbIntfOutPipe = pipeDesc.PipeId;
|
||||
}
|
||||
}
|
||||
|
||||
/* Return control to main thread */
|
||||
device->m_runningTransferLoop = true;
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
|
||||
/* Start transfer loop */
|
||||
device->m_devImp.initialCycle();
|
||||
while (device->m_runningTransferLoop)
|
||||
device->m_devImp.transferCycle();
|
||||
device->m_devImp.finalCycle();
|
||||
|
||||
/* Cleanup */
|
||||
WinUsb_Free(device->m_usbHandle);
|
||||
CloseHandle(device->m_devHandle);
|
||||
device->m_devHandle = 0;
|
||||
|
||||
}
|
||||
|
||||
static void _threadProcBTLL(CHIDDeviceWinUSB* device)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(device->m_initMutex);
|
||||
|
||||
/* Return control to main thread */
|
||||
device->m_runningTransferLoop = true;
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
|
||||
/* Start transfer loop */
|
||||
device->m_devImp.initialCycle();
|
||||
while (device->m_runningTransferLoop)
|
||||
device->m_devImp.transferCycle();
|
||||
device->m_devImp.finalCycle();
|
||||
|
||||
}
|
||||
|
||||
static void _threadProcHID(CHIDDeviceWinUSB* device)
|
||||
{
|
||||
std::unique_lock<std::mutex> lk(device->m_initMutex);
|
||||
|
||||
/* Return control to main thread */
|
||||
device->m_runningTransferLoop = true;
|
||||
lk.unlock();
|
||||
device->m_initCond.notify_one();
|
||||
|
||||
/* Start transfer loop */
|
||||
device->m_devImp.initialCycle();
|
||||
while (device->m_runningTransferLoop)
|
||||
device->m_devImp.transferCycle();
|
||||
device->m_devImp.finalCycle();
|
||||
|
||||
}
|
||||
|
||||
void _deviceDisconnected()
|
||||
{
|
||||
m_runningTransferLoop = false;
|
||||
}
|
||||
|
||||
bool _sendHIDReport(const uint8_t* data, size_t length, uint16_t message)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
CHIDDeviceWinUSB(CDeviceToken& token, CDeviceBase& devImp)
|
||||
: m_token(token),
|
||||
m_devImp(devImp),
|
||||
m_devPath(token.getDevicePath())
|
||||
{
|
||||
devImp.m_hidDev = this;
|
||||
std::unique_lock<std::mutex> lk(m_initMutex);
|
||||
CDeviceToken::TDeviceType dType = token.getDeviceType();
|
||||
if (dType == CDeviceToken::DEVTYPE_USB)
|
||||
m_thread = new std::thread(_threadProcUSBLL, this);
|
||||
else if (dType == CDeviceToken::DEVTYPE_BLUETOOTH)
|
||||
m_thread = new std::thread(_threadProcBTLL, this);
|
||||
else if (dType == CDeviceToken::DEVTYPE_GENERICHID)
|
||||
m_thread = new std::thread(_threadProcHID, this);
|
||||
else
|
||||
throw std::runtime_error("invalid token supplied to device constructor");
|
||||
m_initCond.wait(lk);
|
||||
}
|
||||
|
||||
~CHIDDeviceWinUSB()
|
||||
{
|
||||
m_runningTransferLoop = false;
|
||||
m_thread->join();
|
||||
delete m_thread;
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
IHIDDevice* IHIDDeviceNew(CDeviceToken& token, CDeviceBase& devImp)
|
||||
{
|
||||
return new CHIDDeviceWinUSB(token, devImp);
|
||||
}
|
||||
|
||||
}
|
||||
217
lib/inputdev/HIDListenerIOKit.cpp
Normal file
217
lib/inputdev/HIDListenerIOKit.cpp
Normal file
@@ -0,0 +1,217 @@
|
||||
#include "IHIDListener.hpp"
|
||||
#include "inputdev/CDeviceFinder.hpp"
|
||||
#include <CoreFoundation/CoreFoundation.h>
|
||||
#include <IOKit/hid/IOHIDLib.h>
|
||||
#include <IOKit/IOKitLib.h>
|
||||
#include <IOKit/usb/IOUSBLib.h>
|
||||
#include <IOKit/IOCFPlugIn.h>
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
/*
|
||||
* Reference: http://oroboro.com/usb-serial-number-osx/
|
||||
*/
|
||||
|
||||
static bool getUSBStringDescriptor(IOUSBDeviceInterface182** usbDevice, UInt8 idx, char* out)
|
||||
{
|
||||
UInt16 buffer[128];
|
||||
|
||||
// wow... we're actually forced to make hard coded bus requests. Its like
|
||||
// hard disk programming in the 80's!
|
||||
IOUSBDevRequest request;
|
||||
|
||||
request.bmRequestType = USBmakebmRequestType(kUSBIn,
|
||||
kUSBStandard,
|
||||
kUSBDevice);
|
||||
request.bRequest = kUSBRqGetDescriptor;
|
||||
request.wValue = (kUSBStringDesc << 8) | idx;
|
||||
request.wIndex = 0x409; // english
|
||||
request.wLength = sizeof(buffer);
|
||||
request.pData = buffer;
|
||||
|
||||
kern_return_t err = (*usbDevice)->DeviceRequest(usbDevice, &request);
|
||||
if (err != 0)
|
||||
{
|
||||
// the request failed... fairly uncommon for the USB disk driver, but not
|
||||
// so uncommon for other devices. This can also be less reliable if your
|
||||
// disk is mounted through an external USB hub. At this level we actually
|
||||
// have to worry about hardware issues like this.
|
||||
return false;
|
||||
}
|
||||
|
||||
// we're mallocing this string just as an example. But you probably will want
|
||||
// to do something smarter, like pre-allocated buffers in the info class, or
|
||||
// use a string class.
|
||||
unsigned count = (request.wLenDone - 1) / 2;
|
||||
unsigned i;
|
||||
for (i=0 ; i<count ; ++i)
|
||||
out[i] = buffer[i+1];
|
||||
out[i] = '\0';
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
class CHIDListenerIOKit final : public IHIDListener
|
||||
{
|
||||
CDeviceFinder& m_finder;
|
||||
|
||||
CFRunLoopRef m_listenerRunLoop;
|
||||
IONotificationPortRef m_llPort;
|
||||
io_iterator_t m_llAddNotif, m_llRemoveNotif;
|
||||
bool m_scanningEnabled;
|
||||
|
||||
static void devicesConnectedUSBLL(CHIDListenerIOKit* listener,
|
||||
io_iterator_t iterator)
|
||||
{
|
||||
io_object_t obj;
|
||||
while ((obj = IOIteratorNext(iterator)))
|
||||
{
|
||||
io_string_t devPath;
|
||||
if (IORegistryEntryGetPath(obj, kIOServicePlane, devPath) != 0)
|
||||
continue;
|
||||
|
||||
if (!listener->m_scanningEnabled ||
|
||||
listener->m_finder._hasToken(devPath))
|
||||
{
|
||||
IOObjectRelease(obj);
|
||||
continue;
|
||||
}
|
||||
|
||||
IOCFPlugInInterface** devServ;
|
||||
SInt32 score;
|
||||
IOReturn err;
|
||||
err = IOCreatePlugInInterfaceForService(obj, kIOUSBDeviceUserClientTypeID, kIOCFPlugInInterfaceID, &devServ, &score);
|
||||
if (err != kIOReturnSuccess)
|
||||
throw std::runtime_error("unable to open IOKit plugin interface");
|
||||
|
||||
IOUSBDeviceInterface182 **dev;
|
||||
err = (*devServ)->QueryInterface(devServ,
|
||||
CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID182),
|
||||
(LPVOID*)&dev);
|
||||
if (err != kIOReturnSuccess)
|
||||
throw std::runtime_error("unable to open IOKit device interface");
|
||||
|
||||
UInt16 vid, pid;
|
||||
(*dev)->GetDeviceVendor(dev, &vid);
|
||||
(*dev)->GetDeviceProduct(dev, &pid);
|
||||
|
||||
UInt8 vstridx, pstridx;
|
||||
(*dev)->USBGetManufacturerStringIndex(dev, &vstridx);
|
||||
(*dev)->USBGetProductStringIndex(dev, &pstridx);
|
||||
|
||||
char vstr[128] = {0};
|
||||
char pstr[128] = {0};
|
||||
getUSBStringDescriptor(dev, vstridx, vstr);
|
||||
getUSBStringDescriptor(dev, pstridx, pstr);
|
||||
|
||||
if (!listener->m_finder._insertToken(CDeviceToken(CDeviceToken::DEVTYPE_USB,
|
||||
vid, pid, vstr, pstr, devPath)))
|
||||
{
|
||||
/* Matched-insertion failed; see if generic HID interface is available */
|
||||
/* TODO: Do */
|
||||
}
|
||||
|
||||
//printf("ADDED %08X %s\n", obj, devPath);
|
||||
(*dev)->Release(dev);
|
||||
IODestroyPlugInInterface(devServ);
|
||||
IOObjectRelease(obj);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static void devicesDisconnectedUSBLL(CHIDListenerIOKit* listener,
|
||||
io_iterator_t iterator)
|
||||
{
|
||||
if (CFRunLoopGetCurrent() != listener->m_listenerRunLoop)
|
||||
{
|
||||
CFRunLoopPerformBlock(listener->m_listenerRunLoop, kCFRunLoopDefaultMode, ^{
|
||||
devicesDisconnectedUSBLL(listener, iterator);
|
||||
});
|
||||
CFRunLoopWakeUp(listener->m_listenerRunLoop);
|
||||
return;
|
||||
}
|
||||
io_object_t obj;
|
||||
while ((obj = IOIteratorNext(iterator)))
|
||||
{
|
||||
io_string_t devPath;
|
||||
if (IORegistryEntryGetPath(obj, kIOServicePlane, devPath) != 0)
|
||||
continue;
|
||||
listener->m_finder._removeToken(devPath);
|
||||
//printf("REMOVED %08X %s\n", obj, devPath);
|
||||
IOObjectRelease(obj);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
CHIDListenerIOKit(CDeviceFinder& finder)
|
||||
: m_finder(finder)
|
||||
{
|
||||
|
||||
/* Register Low-Level USB Matcher */
|
||||
m_listenerRunLoop = CFRunLoopGetCurrent();
|
||||
m_llPort = IONotificationPortCreate(kIOMasterPortDefault);
|
||||
CFRunLoopSourceRef rlSrc = IONotificationPortGetRunLoopSource(m_llPort);
|
||||
CFRunLoopAddSource(m_listenerRunLoop, rlSrc, kCFRunLoopDefaultMode);
|
||||
|
||||
CFMutableDictionaryRef matchDict = IOServiceMatching(kIOUSBDeviceClassName);
|
||||
CFRetain(matchDict);
|
||||
|
||||
m_scanningEnabled = true;
|
||||
kern_return_t llRet =
|
||||
IOServiceAddMatchingNotification(m_llPort, kIOMatchedNotification, matchDict,
|
||||
(IOServiceMatchingCallback)devicesConnectedUSBLL, this, &m_llAddNotif);
|
||||
if (llRet == kIOReturnSuccess)
|
||||
devicesConnectedUSBLL(this, m_llAddNotif);
|
||||
|
||||
llRet =
|
||||
IOServiceAddMatchingNotification(m_llPort, kIOTerminatedNotification, matchDict,
|
||||
(IOServiceMatchingCallback)devicesDisconnectedUSBLL, this, &m_llRemoveNotif);
|
||||
if (llRet == kIOReturnSuccess)
|
||||
devicesDisconnectedUSBLL(this, m_llRemoveNotif);
|
||||
|
||||
m_scanningEnabled = false;
|
||||
|
||||
}
|
||||
|
||||
~CHIDListenerIOKit()
|
||||
{
|
||||
CFRunLoopRemoveSource(m_listenerRunLoop, IONotificationPortGetRunLoopSource(m_llPort), kCFRunLoopDefaultMode);
|
||||
IOObjectRelease(m_llAddNotif);
|
||||
IOObjectRelease(m_llRemoveNotif);
|
||||
IONotificationPortDestroy(m_llPort);
|
||||
}
|
||||
|
||||
/* Automatic device scanning */
|
||||
bool startScanning()
|
||||
{
|
||||
m_scanningEnabled = true;
|
||||
return true;
|
||||
}
|
||||
bool stopScanning()
|
||||
{
|
||||
m_scanningEnabled = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Manual device scanning */
|
||||
bool scanNow()
|
||||
{
|
||||
io_iterator_t iter;
|
||||
if (IOServiceGetMatchingServices(kIOMasterPortDefault,
|
||||
IOServiceMatching(kIOUSBDeviceClassName), &iter) == kIOReturnSuccess)
|
||||
{
|
||||
devicesConnectedUSBLL(this, iter);
|
||||
IOObjectRelease(iter);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
IHIDListener* IHIDListenerNew(CDeviceFinder& finder)
|
||||
{
|
||||
return new CHIDListenerIOKit(finder);
|
||||
}
|
||||
|
||||
}
|
||||
222
lib/inputdev/HIDListenerUdev.cpp
Normal file
222
lib/inputdev/HIDListenerUdev.cpp
Normal file
@@ -0,0 +1,222 @@
|
||||
#include "boo/inputdev/IHIDListener.hpp"
|
||||
#include "boo/inputdev/DeviceFinder.hpp"
|
||||
#include <libudev.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <signal.h>
|
||||
#include <thread>
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
static udev* UDEV_INST = NULL;
|
||||
udev* GetUdev()
|
||||
{
|
||||
if (!UDEV_INST)
|
||||
UDEV_INST = udev_new();
|
||||
return UDEV_INST;
|
||||
}
|
||||
|
||||
class HIDListenerUdev final : public IHIDListener
|
||||
{
|
||||
DeviceFinder& m_finder;
|
||||
|
||||
udev_monitor* m_udevMon;
|
||||
std::thread* m_udevThread;
|
||||
bool m_udevRunning;
|
||||
bool m_scanningEnabled;
|
||||
|
||||
static void deviceConnected(HIDListenerUdev* listener,
|
||||
udev_device* device)
|
||||
{
|
||||
if (!listener->m_scanningEnabled)
|
||||
return;
|
||||
|
||||
/* Filter to USB/BT */
|
||||
const char* dt = udev_device_get_devtype(device);
|
||||
DeviceToken::TDeviceType type;
|
||||
if (!strcmp(dt, "usb_device"))
|
||||
type = DeviceToken::DEVTYPE_USB;
|
||||
else if (!strcmp(dt, "bluetooth_device"))
|
||||
type = DeviceToken::DEVTYPE_BLUETOOTH;
|
||||
else
|
||||
return;
|
||||
|
||||
/* Prevent redundant registration */
|
||||
const char* devPath = udev_device_get_syspath(device);
|
||||
if (listener->m_finder._hasToken(devPath))
|
||||
return;
|
||||
|
||||
int vid = 0, pid = 0;
|
||||
udev_list_entry* attrs = udev_device_get_properties_list_entry(device);
|
||||
#if 0
|
||||
udev_list_entry* att = NULL;
|
||||
udev_list_entry_foreach(att, attrs)
|
||||
{
|
||||
const char* name = udev_list_entry_get_name(att);
|
||||
const char* val = udev_list_entry_get_value(att);
|
||||
fprintf(stderr, "%s %s\n", name, val);
|
||||
}
|
||||
fprintf(stderr, "\n\n");
|
||||
#endif
|
||||
|
||||
udev_list_entry* vide = udev_list_entry_get_by_name(attrs, "ID_VENDOR_ID");
|
||||
if (vide)
|
||||
vid = strtol(udev_list_entry_get_value(vide), NULL, 16);
|
||||
|
||||
udev_list_entry* pide = udev_list_entry_get_by_name(attrs, "ID_MODEL_ID");
|
||||
if (pide)
|
||||
pid = strtol(udev_list_entry_get_value(pide), NULL, 16);
|
||||
|
||||
const char* manuf = NULL;
|
||||
udev_list_entry* manufe = udev_list_entry_get_by_name(attrs, "ID_VENDOR");
|
||||
if (manufe)
|
||||
manuf = udev_list_entry_get_value(manufe);
|
||||
|
||||
const char* product = NULL;
|
||||
udev_list_entry* producte = udev_list_entry_get_by_name(attrs, "ID_MODEL");
|
||||
if (producte)
|
||||
product = udev_list_entry_get_value(producte);
|
||||
|
||||
if (!listener->m_finder._insertToken(DeviceToken(type, vid, pid, manuf, product, devPath)))
|
||||
{
|
||||
/* Matched-insertion failed; see if generic HID interface is available */
|
||||
udev_list_entry* devInterfaces = NULL;
|
||||
if (type == DeviceToken::DEVTYPE_USB)
|
||||
devInterfaces = udev_list_entry_get_by_name(attrs, "ID_USB_INTERFACES");
|
||||
else if (type == DeviceToken::DEVTYPE_BLUETOOTH)
|
||||
devInterfaces = udev_list_entry_get_by_name(attrs, "ID_BLUETOOTH_INTERFACES");
|
||||
if (devInterfaces)
|
||||
{
|
||||
const char* interfacesStr = udev_list_entry_get_value(devInterfaces);
|
||||
if (strstr(interfacesStr, ":030104") || /* HID / GenericDesktop / Joystick */
|
||||
strstr(interfacesStr, ":030105") || /* HID / GenericDesktop / Gamepad */
|
||||
strstr(interfacesStr, ":090000")) /* HID / Sony / Dualshock */
|
||||
{
|
||||
udev_enumerate* hidEnum = udev_enumerate_new(UDEV_INST);
|
||||
udev_enumerate_add_match_parent(hidEnum, device);
|
||||
udev_enumerate_add_match_subsystem(hidEnum, "hid");
|
||||
udev_enumerate_scan_devices(hidEnum);
|
||||
udev_list_entry* hidEnt = udev_enumerate_get_list_entry(hidEnum);
|
||||
if (hidEnt)
|
||||
{
|
||||
const char* hidPath = udev_list_entry_get_name(hidEnt);
|
||||
if (!listener->m_finder._hasToken(hidPath))
|
||||
listener->m_finder._insertToken(DeviceToken(DeviceToken::DEVTYPE_GENERICHID,
|
||||
vid, pid, manuf, product, hidPath));
|
||||
}
|
||||
udev_enumerate_unref(hidEnum);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void deviceDisconnected(HIDListenerUdev* listener,
|
||||
udev_device* device)
|
||||
{
|
||||
const char* devPath = udev_device_get_syspath(device);
|
||||
listener->m_finder._removeToken(devPath);
|
||||
}
|
||||
|
||||
static void _udevProc(HIDListenerUdev* listener)
|
||||
{
|
||||
udev_monitor_enable_receiving(listener->m_udevMon);
|
||||
int fd = udev_monitor_get_fd(listener->m_udevMon);
|
||||
while (listener->m_udevRunning)
|
||||
{
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(fd, &fds);
|
||||
select(fd+1, &fds, NULL, NULL, NULL);
|
||||
udev_device* dev = udev_monitor_receive_device(listener->m_udevMon);
|
||||
if (dev)
|
||||
{
|
||||
const char* action = udev_device_get_action(dev);
|
||||
if (!strcmp(action, "add"))
|
||||
deviceConnected(listener, dev);
|
||||
else if (!strcmp(action, "remove"))
|
||||
deviceDisconnected(listener, dev);
|
||||
udev_device_unref(dev);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
HIDListenerUdev(DeviceFinder& finder)
|
||||
: m_finder(finder)
|
||||
{
|
||||
|
||||
/* Setup hotplug events */
|
||||
m_udevMon = udev_monitor_new_from_netlink(GetUdev(), "udev");
|
||||
if (!m_udevMon)
|
||||
{
|
||||
fprintf(stderr, "unable to init udev_monitor");
|
||||
abort();
|
||||
}
|
||||
udev_monitor_filter_add_match_subsystem_devtype(m_udevMon, "usb", "usb_device");
|
||||
udev_monitor_filter_add_match_subsystem_devtype(m_udevMon, "bluetooth", "bluetooth_device");
|
||||
udev_monitor_filter_update(m_udevMon);
|
||||
|
||||
/* Initial HID Device Add */
|
||||
m_scanningEnabled = true;
|
||||
scanNow();
|
||||
m_scanningEnabled = false;
|
||||
|
||||
/* Start hotplug thread */
|
||||
m_udevRunning = true;
|
||||
m_udevThread = new std::thread(_udevProc, this);
|
||||
|
||||
}
|
||||
|
||||
~HIDListenerUdev()
|
||||
{
|
||||
m_udevRunning = false;
|
||||
pthread_kill(m_udevThread->native_handle(), SIGINT);
|
||||
m_udevThread->join();
|
||||
delete m_udevThread;
|
||||
udev_monitor_unref(m_udevMon);
|
||||
}
|
||||
|
||||
/* Automatic device scanning */
|
||||
bool startScanning()
|
||||
{
|
||||
m_scanningEnabled = true;
|
||||
return true;
|
||||
}
|
||||
bool stopScanning()
|
||||
{
|
||||
m_scanningEnabled = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Manual device scanning */
|
||||
bool scanNow()
|
||||
{
|
||||
udev_enumerate* uenum = udev_enumerate_new(GetUdev());
|
||||
udev_enumerate_add_match_subsystem(uenum, "usb");
|
||||
udev_enumerate_add_match_property(uenum, "DEVTYPE", "usb_device");
|
||||
udev_enumerate_add_match_subsystem(uenum, "bluetooth");
|
||||
udev_enumerate_add_match_property(uenum, "DEVTYPE", "bluetooth_device");
|
||||
udev_enumerate_scan_devices(uenum);
|
||||
udev_list_entry* uenumList = udev_enumerate_get_list_entry(uenum);
|
||||
udev_list_entry* uenumItem;
|
||||
udev_list_entry_foreach(uenumItem, uenumList)
|
||||
{
|
||||
const char* devPath = udev_list_entry_get_name(uenumItem);
|
||||
udev_device* dev = udev_device_new_from_syspath(UDEV_INST, devPath);
|
||||
if (dev)
|
||||
deviceConnected(this, dev);
|
||||
udev_device_unref(dev);
|
||||
}
|
||||
udev_enumerate_unref(uenum);
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
IHIDListener* IHIDListenerNew(DeviceFinder& finder)
|
||||
{
|
||||
return new HIDListenerUdev(finder);
|
||||
}
|
||||
|
||||
}
|
||||
209
lib/inputdev/HIDListenerWinUSB.cpp
Normal file
209
lib/inputdev/HIDListenerWinUSB.cpp
Normal file
@@ -0,0 +1,209 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1 /* STFU MSVC */
|
||||
#include "inputdev/IHIDListener.hpp"
|
||||
#include "inputdev/CDeviceFinder.hpp"
|
||||
#include <string.h>
|
||||
#include <thread>
|
||||
|
||||
#define _WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
|
||||
#include <initguid.h>
|
||||
#include <SetupAPI.h>
|
||||
#include <Cfgmgr32.h>
|
||||
#include <Usbiodef.h>
|
||||
#include <Devpkey.h>
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
class CHIDListenerWinUSB final : public IHIDListener
|
||||
{
|
||||
CDeviceFinder& m_finder;
|
||||
|
||||
bool m_scanningEnabled;
|
||||
|
||||
/*
|
||||
* Reference: https://github.com/pbatard/libwdi/blob/master/libwdi/libwdi.c
|
||||
*/
|
||||
|
||||
void _pollDevices(const char* pathFilter)
|
||||
{
|
||||
|
||||
/* Don't ask */
|
||||
static const LPCSTR arPrefix[3] = {"VID_", "PID_", "MI_"};
|
||||
unsigned i, j;
|
||||
CONFIGRET r;
|
||||
ULONG devpropType;
|
||||
DWORD reg_type;
|
||||
HDEVINFO hDevInfo = 0;
|
||||
SP_DEVINFO_DATA DeviceInfoData = {0};
|
||||
DeviceInfoData.cbSize = sizeof(DeviceInfoData);
|
||||
SP_DEVICE_INTERFACE_DATA DeviceInterfaceData = {0};
|
||||
DeviceInterfaceData.cbSize = sizeof(DeviceInterfaceData);
|
||||
union {
|
||||
SP_DEVICE_INTERFACE_DETAIL_DATA_A wtf;
|
||||
CHAR alloc[2048];
|
||||
} DeviceInterfaceDetailData; /* Stack allocation should be fine for this */
|
||||
DeviceInterfaceDetailData.wtf.cbSize = sizeof(DeviceInterfaceDetailData);
|
||||
CHAR szDeviceInstanceID[MAX_DEVICE_ID_LEN];
|
||||
LPSTR pszToken, pszNextToken;
|
||||
CHAR szVid[MAX_DEVICE_ID_LEN], szPid[MAX_DEVICE_ID_LEN], szMi[MAX_DEVICE_ID_LEN];
|
||||
|
||||
/* List all connected USB devices */
|
||||
hDevInfo = SetupDiGetClassDevs(NULL, 0, 0, DIGCF_PRESENT | DIGCF_ALLCLASSES | DIGCF_DEVICEINTERFACE);
|
||||
if (hDevInfo == INVALID_HANDLE_VALUE)
|
||||
return;
|
||||
|
||||
for (i=0 ; ; ++i)
|
||||
{
|
||||
|
||||
if (!SetupDiEnumDeviceInterfaces(hDevInfo,
|
||||
NULL,
|
||||
&GUID_DEVINTERFACE_USB_DEVICE,
|
||||
i,
|
||||
&DeviceInterfaceData))
|
||||
break;
|
||||
|
||||
DeviceInterfaceDetailData.wtf.cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
|
||||
if (!SetupDiGetDeviceInterfaceDetailA(hDevInfo,
|
||||
&DeviceInterfaceData,
|
||||
&DeviceInterfaceDetailData.wtf,
|
||||
sizeof(DeviceInterfaceDetailData),
|
||||
NULL,
|
||||
&DeviceInfoData))
|
||||
continue;
|
||||
|
||||
r = CM_Get_Device_IDA(DeviceInfoData.DevInst, szDeviceInstanceID , MAX_PATH, 0);
|
||||
if (r != CR_SUCCESS)
|
||||
continue;
|
||||
|
||||
/* Retreive the device description as reported by the device itself */
|
||||
pszToken = strtok_s(szDeviceInstanceID , "\\#&", &pszNextToken);
|
||||
szVid[0] = '\0';
|
||||
szPid[0] = '\0';
|
||||
szMi[0] = '\0';
|
||||
while (pszToken != NULL)
|
||||
{
|
||||
for (j=0 ; j<3 ; ++j)
|
||||
{
|
||||
if (strncmp(pszToken, arPrefix[j], 4) == 0)
|
||||
{
|
||||
switch (j)
|
||||
{
|
||||
case 0:
|
||||
strcpy_s(szVid, MAX_DEVICE_ID_LEN, pszToken);
|
||||
break;
|
||||
case 1:
|
||||
strcpy_s(szPid, MAX_DEVICE_ID_LEN, pszToken);
|
||||
break;
|
||||
case 2:
|
||||
strcpy_s(szMi, MAX_DEVICE_ID_LEN, pszToken);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
pszToken = strtok_s(NULL, "\\#&", &pszNextToken);
|
||||
}
|
||||
|
||||
if (!szVid[0] || !szPid[0])
|
||||
continue;
|
||||
|
||||
unsigned vid = strtol(szVid+4, NULL, 16);
|
||||
unsigned pid = strtol(szPid+4, NULL, 16);
|
||||
|
||||
WCHAR productW[1024] = {0};
|
||||
CHAR product[1024] = {0};
|
||||
DWORD productSz = 0;
|
||||
if (!SetupDiGetDevicePropertyW(hDevInfo, &DeviceInfoData, &DEVPKEY_Device_BusReportedDeviceDesc,
|
||||
&devpropType, (BYTE*)productW, 1024, &productSz, 0)) {
|
||||
/* fallback to SPDRP_DEVICEDESC (USB hubs still use it) */
|
||||
SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_DEVICEDESC,
|
||||
®_type, (BYTE*)productW, 1024, &productSz);
|
||||
}
|
||||
wcstombs(product, productW, productSz / 2);
|
||||
|
||||
WCHAR manufW[1024] = L"Someone"; /* Windows Vista and earlier will use this as the vendor */
|
||||
CHAR manuf[1024] = {0};
|
||||
DWORD manufSz = 0;
|
||||
SetupDiGetDevicePropertyW(hDevInfo, &DeviceInfoData, &DEVPKEY_Device_Manufacturer,
|
||||
&devpropType, (BYTE*)manufW, 1024, &manufSz, 0);
|
||||
wcstombs(manuf, manufW, manufSz / 2);
|
||||
|
||||
/* Store as a shouting string (to keep hash-lookups consistent) */
|
||||
CharUpperA(DeviceInterfaceDetailData.wtf.DevicePath);
|
||||
|
||||
/* Filter to specific device (provided by hotplug event) */
|
||||
if (pathFilter && strcmp(pathFilter, DeviceInterfaceDetailData.wtf.DevicePath))
|
||||
continue;
|
||||
|
||||
/* Whew!! that's a single device enumerated!! */
|
||||
if (!m_finder._hasToken(DeviceInterfaceDetailData.wtf.DevicePath))
|
||||
m_finder._insertToken(CDeviceToken(CDeviceToken::DEVTYPE_USB,
|
||||
vid, pid, manuf, product,
|
||||
DeviceInterfaceDetailData.wtf.DevicePath));
|
||||
|
||||
}
|
||||
|
||||
SetupDiDestroyDeviceInfoList(hDevInfo);
|
||||
|
||||
}
|
||||
|
||||
public:
|
||||
CHIDListenerWinUSB(CDeviceFinder& finder)
|
||||
: m_finder(finder)
|
||||
{
|
||||
/* Initial HID Device Add */
|
||||
_pollDevices(NULL);
|
||||
}
|
||||
|
||||
~CHIDListenerWinUSB()
|
||||
{}
|
||||
|
||||
/* Automatic device scanning */
|
||||
bool startScanning()
|
||||
{
|
||||
m_scanningEnabled = true;
|
||||
return true;
|
||||
}
|
||||
bool stopScanning()
|
||||
{
|
||||
m_scanningEnabled = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Manual device scanning */
|
||||
bool scanNow()
|
||||
{
|
||||
_pollDevices(NULL);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _extDevConnect(const char* path)
|
||||
{
|
||||
char upperPath[1024];
|
||||
strcpy_s(upperPath, 1024, path);
|
||||
CharUpperA(upperPath);
|
||||
if (m_scanningEnabled && !m_finder._hasToken(upperPath))
|
||||
_pollDevices(upperPath);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool _extDevDisconnect(const char* path)
|
||||
{
|
||||
char upperPath[1024];
|
||||
strcpy_s(upperPath, 1024, path);
|
||||
CharUpperA(upperPath);
|
||||
m_finder._removeToken(upperPath);
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
IHIDListener* IHIDListenerNew(CDeviceFinder& finder)
|
||||
{
|
||||
return new CHIDListenerWinUSB(finder);
|
||||
}
|
||||
|
||||
}
|
||||
23
lib/inputdev/IHIDDevice.hpp
Normal file
23
lib/inputdev/IHIDDevice.hpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#ifndef IHIDDEVICE_HPP
|
||||
#define IHIDDEVICE_HPP
|
||||
|
||||
#include "boo/inputdev/DeviceToken.hpp"
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
class IHIDDevice
|
||||
{
|
||||
friend class DeviceBase;
|
||||
virtual void _deviceDisconnected()=0;
|
||||
virtual bool _sendUSBInterruptTransfer(const uint8_t* data, size_t length)=0;
|
||||
virtual size_t _receiveUSBInterruptTransfer(uint8_t* data, size_t length)=0;
|
||||
virtual bool _sendHIDReport(const uint8_t* data, size_t length, uint16_t message)=0;
|
||||
virtual size_t _recieveReport(const uint8_t* data, size_t length, uint16_t message){}
|
||||
public:
|
||||
inline virtual ~IHIDDevice() {}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // IHIDDEVICE_HPP
|
||||
1
lib/inputdev/RevolutionPad.cpp
Normal file
1
lib/inputdev/RevolutionPad.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include "boo/inputdev/RevolutionPad.hpp"
|
||||
182
lib/mac/ApplicationCocoa.mm
Normal file
182
lib/mac/ApplicationCocoa.mm
Normal file
@@ -0,0 +1,182 @@
|
||||
#include <AppKit/AppKit.h>
|
||||
|
||||
#include "IApplication.hpp"
|
||||
|
||||
@interface AppDelegate : NSObject <NSApplicationDelegate>
|
||||
{
|
||||
boo::IApplicationCallback* callback;
|
||||
@public
|
||||
NSPanel* aboutPanel;
|
||||
}
|
||||
- (id)initWithCallback:(boo::IApplicationCallback*)cb;
|
||||
@end
|
||||
|
||||
@implementation AppDelegate
|
||||
- (id)initWithCallback:(boo::IApplicationCallback*)cb
|
||||
{
|
||||
self = [super init];
|
||||
callback = cb;
|
||||
return self;
|
||||
}
|
||||
- (void)applicationDidFinishLaunching:(NSNotification*)notification
|
||||
{
|
||||
(void)notification;
|
||||
callback->appLaunched(boo::IApplicationInstance());
|
||||
}
|
||||
- (void)applicationWillTerminate:(NSNotification*)notification
|
||||
{
|
||||
(void)notification;
|
||||
callback->appQuitting(boo::IApplicationInstance());
|
||||
}
|
||||
- (BOOL)application:(NSApplication*)sender openFile:(NSString*)filename
|
||||
{
|
||||
(void)sender;
|
||||
return callback->appFileOpen(boo::IApplicationInstance(), [filename UTF8String]);
|
||||
}
|
||||
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication*)sender {
|
||||
(void)sender;
|
||||
return YES;
|
||||
}
|
||||
- (IBAction)aboutApp:(id)sender
|
||||
{
|
||||
(void)sender;
|
||||
NSRect screenFrame = [[aboutPanel screen] frame];
|
||||
CGFloat xPos = NSWidth(screenFrame)/2 - 300/2;
|
||||
CGFloat yPos = NSHeight(screenFrame)/2 - 220/2;
|
||||
NSRect aboutCr = NSMakeRect(xPos, yPos, 300, 220);
|
||||
[aboutPanel setFrame:aboutCr display:NO];
|
||||
[aboutPanel makeKeyAndOrderFront:self];
|
||||
}
|
||||
- (IBAction)toggleFs:(id)sender
|
||||
{
|
||||
(void)sender;
|
||||
[[NSApp keyWindow] toggleFullScreen:nil];
|
||||
}
|
||||
- (IBAction)quitApp:(id)sender
|
||||
{
|
||||
(void)sender;
|
||||
[NSApp terminate:nil];
|
||||
}
|
||||
@end
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
IWindow* _CWindowCocoaNew(const std::string& title);
|
||||
|
||||
class CApplicationCocoa final : public IApplication
|
||||
{
|
||||
IApplicationCallback& m_callback;
|
||||
const std::string m_friendlyName;
|
||||
const std::string m_pname;
|
||||
const std::vector<std::string> m_args;
|
||||
|
||||
NSPanel* aboutPanel;
|
||||
|
||||
void _deletedWindow(IWindow* window)
|
||||
{
|
||||
(void)window;
|
||||
}
|
||||
|
||||
public:
|
||||
CApplicationCocoa(IApplicationCallback& callback,
|
||||
const std::string& friendlyName,
|
||||
const std::string& pname,
|
||||
const std::vector<std::string>& args)
|
||||
: m_callback(callback),
|
||||
m_friendlyName(friendlyName),
|
||||
m_pname(pname),
|
||||
m_args(args)
|
||||
{}
|
||||
|
||||
EPlatformType getPlatformType() const
|
||||
{
|
||||
return PLAT_COCOA;
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
@autoreleasepool
|
||||
{
|
||||
NSApplication* app = [NSApplication sharedApplication];
|
||||
[app setActivationPolicy:NSApplicationActivationPolicyRegular];
|
||||
|
||||
/* Delegate (OS X callbacks) */
|
||||
AppDelegate* appDelegate = [[AppDelegate alloc] initWithCallback:&m_callback];
|
||||
[app setDelegate:appDelegate];
|
||||
|
||||
/* App menu */
|
||||
NSMenu* appMenu = [[NSMenu alloc] initWithTitle:@"main"];
|
||||
NSMenu* rwkMenu = [[NSMenu alloc] initWithTitle:[[NSString stringWithUTF8String:m_friendlyName.c_str()] autorelease]];
|
||||
[rwkMenu addItemWithTitle:[[NSString stringWithFormat:@"About %s", m_friendlyName.c_str()] autorelease]
|
||||
action:@selector(aboutApp:)
|
||||
keyEquivalent:@""];
|
||||
NSMenuItem* fsItem = [rwkMenu addItemWithTitle:@"Toggle Full Screen"
|
||||
action:@selector(toggleFs:)
|
||||
keyEquivalent:@"f"];
|
||||
[fsItem setKeyEquivalentModifierMask:NSCommandKeyMask];
|
||||
[rwkMenu addItem:[NSMenuItem separatorItem]];
|
||||
NSMenuItem* quit_item = [rwkMenu addItemWithTitle:[[NSString stringWithFormat:@"Quit %s", m_friendlyName.c_str()] autorelease]
|
||||
action:@selector(quitApp:)
|
||||
keyEquivalent:@"q"];
|
||||
[quit_item setKeyEquivalentModifierMask:NSCommandKeyMask];
|
||||
[[appMenu addItemWithTitle:[[NSString stringWithUTF8String:m_friendlyName.c_str()] autorelease]
|
||||
action:nil keyEquivalent:@""] setSubmenu:rwkMenu];
|
||||
[[NSApplication sharedApplication] setMainMenu:appMenu];
|
||||
|
||||
/* About panel */
|
||||
NSRect aboutCr = NSMakeRect(0, 0, 300, 220);
|
||||
aboutPanel = [[NSPanel alloc] initWithContentRect:aboutCr
|
||||
styleMask:NSUtilityWindowMask|NSTitledWindowMask|NSClosableWindowMask
|
||||
backing:NSBackingStoreBuffered defer:YES];
|
||||
[aboutPanel setTitle:[[NSString stringWithFormat:@"About %s", m_friendlyName.c_str()] autorelease]];
|
||||
NSText* aboutText = [[NSText alloc] initWithFrame:aboutCr];
|
||||
[aboutText setEditable:NO];
|
||||
[aboutText setAlignment:NSCenterTextAlignment];
|
||||
[aboutText setString:@"\nRWK Authors\n\nJackoalan\nAntidote\n"];
|
||||
[aboutPanel setContentView:aboutText];
|
||||
appDelegate->aboutPanel = aboutPanel;
|
||||
|
||||
[app run];
|
||||
}
|
||||
}
|
||||
|
||||
void quit()
|
||||
{
|
||||
[NSApp terminate:nil];
|
||||
}
|
||||
|
||||
const std::string& getProcessName() const
|
||||
{
|
||||
return m_pname;
|
||||
}
|
||||
|
||||
const std::vector<std::string>& getArgs() const
|
||||
{
|
||||
return m_args;
|
||||
}
|
||||
|
||||
IWindow* newWindow(const std::string& title)
|
||||
{
|
||||
return _CWindowCocoaNew(title);
|
||||
}
|
||||
};
|
||||
|
||||
IApplication* APP = NULL;
|
||||
IApplication* IApplicationBootstrap(IApplication::EPlatformType platform,
|
||||
IApplicationCallback& cb,
|
||||
const std::string& friendlyName,
|
||||
const std::string& pname,
|
||||
const std::vector<std::string>& args)
|
||||
{
|
||||
if (!APP)
|
||||
{
|
||||
if (platform != IApplication::PLAT_COCOA &&
|
||||
platform != IApplication::PLAT_AUTO)
|
||||
return NULL;
|
||||
APP = new CApplicationCocoa(cb, friendlyName, pname, args);
|
||||
}
|
||||
return APP;
|
||||
}
|
||||
|
||||
}
|
||||
0
lib/mac/GLViewCocoa.mm
Normal file
0
lib/mac/GLViewCocoa.mm
Normal file
639
lib/mac/GraphicsContextCocoa.mm
Normal file
639
lib/mac/GraphicsContextCocoa.mm
Normal file
@@ -0,0 +1,639 @@
|
||||
#import <AppKit/AppKit.h>
|
||||
#include <OpenGL/OpenGL.h>
|
||||
#include <OpenGL/gl3.h>
|
||||
#include "windowsys/IGraphicsContext.hpp"
|
||||
#include "windowsys/IWindow.hpp"
|
||||
|
||||
/* AppKit applies OpenGL much differently than other platforms
|
||||
* the NSOpenGLView class composes together all necessary
|
||||
* OGL context members and provides the necessary event hooks
|
||||
* for KB/Mouse/Touch events
|
||||
*/
|
||||
|
||||
static const NSOpenGLPixelFormatAttribute PF_RGBA8_ATTRS[] =
|
||||
{
|
||||
NSOpenGLPFAAccelerated,
|
||||
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
||||
NSOpenGLPFADoubleBuffer,
|
||||
NSOpenGLPFAColorSize, 24,
|
||||
NSOpenGLPFAAlphaSize, 8,
|
||||
};
|
||||
|
||||
static const NSOpenGLPixelFormatAttribute PF_RGBA8_Z24_ATTRS[] =
|
||||
{
|
||||
NSOpenGLPFAAccelerated,
|
||||
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
||||
NSOpenGLPFADoubleBuffer,
|
||||
NSOpenGLPFAColorSize, 24,
|
||||
NSOpenGLPFAAlphaSize, 8,
|
||||
NSOpenGLPFADepthSize, 24,
|
||||
};
|
||||
|
||||
static const NSOpenGLPixelFormatAttribute PF_RGBAF32_ATTRS[] =
|
||||
{
|
||||
NSOpenGLPFAAccelerated,
|
||||
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
||||
NSOpenGLPFADoubleBuffer,
|
||||
NSOpenGLPFAColorFloat,
|
||||
NSOpenGLPFAColorSize, 96,
|
||||
NSOpenGLPFAAlphaSize, 32,
|
||||
};
|
||||
|
||||
static const NSOpenGLPixelFormatAttribute PF_RGBAF32_Z24_ATTRS[] =
|
||||
{
|
||||
NSOpenGLPFAAccelerated,
|
||||
NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion3_2Core,
|
||||
NSOpenGLPFADoubleBuffer,
|
||||
NSOpenGLPFAColorFloat,
|
||||
NSOpenGLPFAColorSize, 96,
|
||||
NSOpenGLPFAAlphaSize, 32,
|
||||
NSOpenGLPFADepthSize, 24,
|
||||
};
|
||||
|
||||
static const NSOpenGLPixelFormatAttribute* PF_TABLE[] =
|
||||
{
|
||||
NULL,
|
||||
PF_RGBA8_ATTRS,
|
||||
PF_RGBA8_Z24_ATTRS,
|
||||
PF_RGBAF32_ATTRS,
|
||||
PF_RGBAF32_Z24_ATTRS
|
||||
};
|
||||
|
||||
namespace boo {class CGraphicsContextCocoa;}
|
||||
@interface CGraphicsContextCocoaInternal : NSOpenGLView
|
||||
{
|
||||
NSUInteger lastModifiers;
|
||||
boo::CGraphicsContextCocoa* booContext;
|
||||
}
|
||||
- (id)initWithBooContext:(boo::CGraphicsContextCocoa*)bctx;
|
||||
@end
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
class CGraphicsContextCocoa final : public IGraphicsContext
|
||||
{
|
||||
|
||||
EGraphicsAPI m_api;
|
||||
EPixelFormat m_pf;
|
||||
IWindow* m_parentWindow;
|
||||
CGraphicsContextCocoaInternal* m_nsContext;
|
||||
NSOpenGLContext* m_nsShareContext;
|
||||
|
||||
public:
|
||||
IWindowCallback* m_callback;
|
||||
|
||||
CGraphicsContextCocoa(EGraphicsAPI api, IWindow* parentWindow)
|
||||
: m_api(api),
|
||||
m_pf(PF_RGBA8),
|
||||
m_parentWindow(parentWindow),
|
||||
m_nsContext(NULL),
|
||||
m_nsShareContext(NULL),
|
||||
m_callback(NULL)
|
||||
{}
|
||||
|
||||
~CGraphicsContextCocoa()
|
||||
{
|
||||
[m_nsContext release];
|
||||
[m_nsShareContext release];
|
||||
}
|
||||
|
||||
void _setCallback(IWindowCallback* cb)
|
||||
{
|
||||
m_callback = cb;
|
||||
}
|
||||
|
||||
EGraphicsAPI getAPI() const
|
||||
{
|
||||
return m_api;
|
||||
}
|
||||
|
||||
EPixelFormat getPixelFormat() const
|
||||
{
|
||||
return m_pf;
|
||||
}
|
||||
|
||||
void setPixelFormat(EPixelFormat pf)
|
||||
{
|
||||
if (pf > PF_RGBAF32_Z24)
|
||||
return;
|
||||
m_pf = pf;
|
||||
}
|
||||
|
||||
void initializeContext()
|
||||
{
|
||||
if (m_nsShareContext)
|
||||
return;
|
||||
m_nsContext = [[CGraphicsContextCocoaInternal alloc] initWithBooContext:this];
|
||||
[(NSWindow*)m_parentWindow->getPlatformHandle() setContentView:m_nsContext];
|
||||
}
|
||||
|
||||
IGraphicsContext* makeShareContext() const
|
||||
{
|
||||
NSOpenGLContext* nsctx;
|
||||
if (m_nsContext)
|
||||
{
|
||||
nsctx = [[NSOpenGLContext alloc] initWithFormat:[m_nsContext pixelFormat]
|
||||
shareContext:[m_nsContext openGLContext]];
|
||||
}
|
||||
else if (m_nsShareContext)
|
||||
{
|
||||
nsctx = [[NSOpenGLContext alloc] initWithFormat:[m_nsShareContext pixelFormat]
|
||||
shareContext:m_nsShareContext];
|
||||
}
|
||||
else
|
||||
return NULL;
|
||||
if (!nsctx)
|
||||
return NULL;
|
||||
CGraphicsContextCocoa* newCtx = new CGraphicsContextCocoa(m_api, NULL);
|
||||
newCtx->m_nsShareContext = nsctx;
|
||||
return newCtx;
|
||||
}
|
||||
|
||||
void makeCurrent()
|
||||
{
|
||||
if (m_nsContext)
|
||||
[[m_nsContext openGLContext] makeCurrentContext];
|
||||
else if (m_nsShareContext)
|
||||
[m_nsShareContext makeCurrentContext];
|
||||
}
|
||||
|
||||
void clearCurrent()
|
||||
{
|
||||
[NSOpenGLContext clearCurrentContext];
|
||||
}
|
||||
|
||||
void swapBuffer()
|
||||
{
|
||||
[[m_nsContext openGLContext] flushBuffer];
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
IGraphicsContext* _CGraphicsContextCocoaNew(IGraphicsContext::EGraphicsAPI api,
|
||||
IWindow* parentWindow)
|
||||
{
|
||||
if (api != IGraphicsContext::API_OPENGL_3_3 && api != IGraphicsContext::API_OPENGL_4_2)
|
||||
return NULL;
|
||||
|
||||
/* Create temporary context to query GL version */
|
||||
NSOpenGLPixelFormat* nspf = [[NSOpenGLPixelFormat alloc] initWithAttributes:PF_RGBA8_ATTRS];
|
||||
if (!nspf)
|
||||
return NULL;
|
||||
NSOpenGLContext* nsctx = [[NSOpenGLContext alloc] initWithFormat:nspf shareContext:nil];
|
||||
[nspf release];
|
||||
if (!nsctx)
|
||||
return NULL;
|
||||
[nsctx makeCurrentContext];
|
||||
const char* glVersion = (char*)glGetString(GL_VERSION);
|
||||
unsigned major = 0;
|
||||
unsigned minor = 0;
|
||||
if (glVersion)
|
||||
{
|
||||
major = glVersion[0] - '0';
|
||||
minor = glVersion[2] - '0';
|
||||
}
|
||||
[NSOpenGLContext clearCurrentContext];
|
||||
[nsctx release];
|
||||
if (!glVersion)
|
||||
return NULL;
|
||||
|
||||
if (major > 4 || (major == 4 && minor >= 2))
|
||||
api = IGraphicsContext::API_OPENGL_4_2;
|
||||
else if (major == 3 && minor >= 3)
|
||||
if (api == IGraphicsContext::API_OPENGL_4_2)
|
||||
return NULL;
|
||||
|
||||
return new CGraphicsContextCocoa(api, parentWindow);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@implementation CGraphicsContextCocoaInternal
|
||||
- (id)initWithBooContext:(boo::CGraphicsContextCocoa*)bctx
|
||||
{
|
||||
lastModifiers = 0;
|
||||
booContext = bctx;
|
||||
boo::IGraphicsContext::EPixelFormat pf = bctx->getPixelFormat();
|
||||
NSOpenGLPixelFormat* nspf = [[NSOpenGLPixelFormat alloc] initWithAttributes:PF_TABLE[pf]];
|
||||
self = [self initWithFrame:NSMakeRect(0, 0, 100, 100) pixelFormat:nspf];
|
||||
[nspf release];
|
||||
return self;
|
||||
}
|
||||
|
||||
- (BOOL)acceptsTouchEvents
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
|
||||
static inline boo::IWindowCallback::EModifierKey getMod(NSEventModifierFlags flags)
|
||||
{
|
||||
int ret = boo::IWindowCallback::MKEY_NONE;
|
||||
if (flags & NSControlKeyMask)
|
||||
ret |= boo::IWindowCallback::MKEY_CTRL;
|
||||
if (flags & NSAlternateKeyMask)
|
||||
ret |= boo::IWindowCallback::MKEY_ALT;
|
||||
if (flags & NSShiftKeyMask)
|
||||
ret |= boo::IWindowCallback::MKEY_SHIFT;
|
||||
if (flags & NSCommandKeyMask)
|
||||
ret |= boo::IWindowCallback::MKEY_COMMAND;
|
||||
return static_cast<boo::IWindowCallback::EModifierKey>(ret);
|
||||
}
|
||||
|
||||
static inline boo::IWindowCallback::EMouseButton getButton(NSEvent* event)
|
||||
{
|
||||
NSInteger buttonNumber = event.buttonNumber;
|
||||
if (buttonNumber == 3)
|
||||
return boo::IWindowCallback::BUTTON_MIDDLE;
|
||||
else if (buttonNumber == 4)
|
||||
return boo::IWindowCallback::BUTTON_AUX1;
|
||||
else if (buttonNumber == 5)
|
||||
return boo::IWindowCallback::BUTTON_AUX2;
|
||||
return boo::IWindowCallback::BUTTON_NONE;
|
||||
}
|
||||
|
||||
- (void)mouseDown:(NSEvent*)theEvent
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
NSPoint liw = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
||||
float pixelFactor = [[self window] backingScaleFactor];
|
||||
NSRect frame = [self frame];
|
||||
boo::IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
|
||||
{(unsigned)liw.x, (unsigned)liw.y},
|
||||
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
|
||||
};
|
||||
booContext->m_callback->mouseDown(coord, boo::IWindowCallback::BUTTON_PRIMARY,
|
||||
getMod([theEvent modifierFlags]));
|
||||
}
|
||||
|
||||
- (void)mouseUp:(NSEvent*)theEvent
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
NSPoint liw = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
||||
float pixelFactor = [[self window] backingScaleFactor];
|
||||
NSRect frame = [self frame];
|
||||
boo::IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
|
||||
{(unsigned)liw.x, (unsigned)liw.y},
|
||||
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
|
||||
};
|
||||
booContext->m_callback->mouseUp(coord, boo::IWindowCallback::BUTTON_PRIMARY,
|
||||
getMod([theEvent modifierFlags]));
|
||||
}
|
||||
|
||||
- (void)rightMouseDown:(NSEvent*)theEvent
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
NSPoint liw = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
||||
float pixelFactor = [[self window] backingScaleFactor];
|
||||
NSRect frame = [self frame];
|
||||
boo::IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
|
||||
{(unsigned)liw.x, (unsigned)liw.y},
|
||||
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
|
||||
};
|
||||
booContext->m_callback->mouseDown(coord, boo::IWindowCallback::BUTTON_SECONDARY,
|
||||
getMod([theEvent modifierFlags]));
|
||||
}
|
||||
|
||||
- (void)rightMouseUp:(NSEvent*)theEvent
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
NSPoint liw = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
||||
float pixelFactor = [[self window] backingScaleFactor];
|
||||
NSRect frame = [self frame];
|
||||
boo::IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
|
||||
{(unsigned)liw.x, (unsigned)liw.y},
|
||||
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
|
||||
};
|
||||
booContext->m_callback->mouseUp(coord, boo::IWindowCallback::BUTTON_SECONDARY,
|
||||
getMod([theEvent modifierFlags]));
|
||||
}
|
||||
|
||||
- (void)otherMouseDown:(NSEvent*)theEvent
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
boo::IWindowCallback::EMouseButton button = getButton(theEvent);
|
||||
if (!button)
|
||||
return;
|
||||
NSPoint liw = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
||||
float pixelFactor = [[self window] backingScaleFactor];
|
||||
NSRect frame = [self frame];
|
||||
boo::IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
|
||||
{(unsigned)liw.x, (unsigned)liw.y},
|
||||
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
|
||||
};
|
||||
booContext->m_callback->mouseDown(coord, button, getMod([theEvent modifierFlags]));
|
||||
}
|
||||
|
||||
- (void)otherMouseUp:(NSEvent*)theEvent
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
boo::IWindowCallback::EMouseButton button = getButton(theEvent);
|
||||
if (!button)
|
||||
return;
|
||||
NSPoint liw = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
||||
float pixelFactor = [[self window] backingScaleFactor];
|
||||
NSRect frame = [self frame];
|
||||
boo::IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
|
||||
{(unsigned)liw.x, (unsigned)liw.y},
|
||||
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
|
||||
};
|
||||
booContext->m_callback->mouseUp(coord, button, getMod([theEvent modifierFlags]));
|
||||
}
|
||||
|
||||
- (void)mouseMoved:(NSEvent*)theEvent
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
NSPoint liw = [self convertPoint:[theEvent locationInWindow] fromView:nil];
|
||||
float pixelFactor = [[self window] backingScaleFactor];
|
||||
NSRect frame = [self frame];
|
||||
boo::IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{(unsigned)(liw.x * pixelFactor), (unsigned)(liw.y * pixelFactor)},
|
||||
{(unsigned)liw.x, (unsigned)liw.y},
|
||||
{(float)(liw.x / frame.size.width), (float)(liw.y / frame.size.height)}
|
||||
};
|
||||
booContext->m_callback->mouseMove(coord);
|
||||
}
|
||||
- (void)mouseDragged:(NSEvent*)theEvent
|
||||
{
|
||||
[self mouseMoved:theEvent];
|
||||
}
|
||||
- (void)rightMouseDragged:(NSEvent*)theEvent
|
||||
{
|
||||
[self mouseMoved:theEvent];
|
||||
}
|
||||
- (void)otherMouseDragged:(NSEvent*)theEvent
|
||||
{
|
||||
[self mouseMoved:theEvent];
|
||||
}
|
||||
|
||||
- (void)scrollWheel:(NSEvent*)theEvent
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
boo::IWindowCallback::SScrollDelta scroll =
|
||||
{
|
||||
{(float)[theEvent scrollingDeltaX], (float)[theEvent scrollingDeltaY]},
|
||||
(bool)[theEvent hasPreciseScrollingDeltas]
|
||||
};
|
||||
booContext->m_callback->scroll(scroll);
|
||||
}
|
||||
|
||||
- (void)touchesBeganWithEvent:(NSEvent*)event
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
for (NSTouch* touch in [event touchesMatchingPhase:NSTouchPhaseBegan inView:nil])
|
||||
{
|
||||
NSPoint pos = touch.normalizedPosition;
|
||||
boo::IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{(float)pos.x, (float)pos.y}
|
||||
};
|
||||
booContext->m_callback->touchDown(coord, (uintptr_t)touch.identity);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)touchesEndedWithEvent:(NSEvent*)event
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
for (NSTouch* touch in [event touchesMatchingPhase:NSTouchPhaseEnded inView:nil])
|
||||
{
|
||||
NSPoint pos = touch.normalizedPosition;
|
||||
boo::IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{(float)pos.x, (float)pos.y}
|
||||
};
|
||||
booContext->m_callback->touchUp(coord, (uintptr_t)touch.identity);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)touchesMovedWithEvent:(NSEvent*)event
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
for (NSTouch* touch in [event touchesMatchingPhase:NSTouchPhaseMoved inView:nil])
|
||||
{
|
||||
NSPoint pos = touch.normalizedPosition;
|
||||
boo::IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{(float)pos.x, (float)pos.y}
|
||||
};
|
||||
booContext->m_callback->touchMove(coord, (uintptr_t)touch.identity);
|
||||
}
|
||||
}
|
||||
|
||||
- (void)touchesCancelledWithEvent:(NSEvent*)event
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
for (NSTouch* touch in [event touchesMatchingPhase:NSTouchPhaseCancelled inView:nil])
|
||||
{
|
||||
NSPoint pos = touch.normalizedPosition;
|
||||
boo::IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{0, 0},
|
||||
{0, 0},
|
||||
{(float)pos.x, (float)pos.y}
|
||||
};
|
||||
booContext->m_callback->touchUp(coord, (uintptr_t)touch.identity);
|
||||
}
|
||||
}
|
||||
|
||||
/* keycodes for keys that are independent of keyboard layout*/
|
||||
enum
|
||||
{
|
||||
kVK_Return = 0x24,
|
||||
kVK_Tab = 0x30,
|
||||
kVK_Space = 0x31,
|
||||
kVK_Delete = 0x33,
|
||||
kVK_Escape = 0x35,
|
||||
kVK_Command = 0x37,
|
||||
kVK_Shift = 0x38,
|
||||
kVK_CapsLock = 0x39,
|
||||
kVK_Option = 0x3A,
|
||||
kVK_Control = 0x3B,
|
||||
kVK_RightShift = 0x3C,
|
||||
kVK_RightOption = 0x3D,
|
||||
kVK_RightControl = 0x3E,
|
||||
kVK_Function = 0x3F,
|
||||
kVK_F17 = 0x40,
|
||||
kVK_VolumeUp = 0x48,
|
||||
kVK_VolumeDown = 0x49,
|
||||
kVK_Mute = 0x4A,
|
||||
kVK_F18 = 0x4F,
|
||||
kVK_F19 = 0x50,
|
||||
kVK_F20 = 0x5A,
|
||||
kVK_F5 = 0x60,
|
||||
kVK_F6 = 0x61,
|
||||
kVK_F7 = 0x62,
|
||||
kVK_F3 = 0x63,
|
||||
kVK_F8 = 0x64,
|
||||
kVK_F9 = 0x65,
|
||||
kVK_F11 = 0x67,
|
||||
kVK_F13 = 0x69,
|
||||
kVK_F16 = 0x6A,
|
||||
kVK_F14 = 0x6B,
|
||||
kVK_F10 = 0x6D,
|
||||
kVK_F12 = 0x6F,
|
||||
kVK_F15 = 0x71,
|
||||
kVK_Help = 0x72,
|
||||
kVK_Home = 0x73,
|
||||
kVK_PageUp = 0x74,
|
||||
kVK_ForwardDelete = 0x75,
|
||||
kVK_F4 = 0x76,
|
||||
kVK_End = 0x77,
|
||||
kVK_F2 = 0x78,
|
||||
kVK_PageDown = 0x79,
|
||||
kVK_F1 = 0x7A,
|
||||
kVK_LeftArrow = 0x7B,
|
||||
kVK_RightArrow = 0x7C,
|
||||
kVK_DownArrow = 0x7D,
|
||||
kVK_UpArrow = 0x7E
|
||||
};
|
||||
static boo::IWindowCallback::ESpecialKey translateKeycode(short code)
|
||||
{
|
||||
switch (code) {
|
||||
case kVK_F1:
|
||||
return boo::IWindowCallback::KEY_F1;
|
||||
case kVK_F2:
|
||||
return boo::IWindowCallback::KEY_F2;
|
||||
case kVK_F3:
|
||||
return boo::IWindowCallback::KEY_F3;
|
||||
case kVK_F4:
|
||||
return boo::IWindowCallback::KEY_F4;
|
||||
case kVK_F5:
|
||||
return boo::IWindowCallback::KEY_F5;
|
||||
case kVK_F6:
|
||||
return boo::IWindowCallback::KEY_F6;
|
||||
case kVK_F7:
|
||||
return boo::IWindowCallback::KEY_F7;
|
||||
case kVK_F8:
|
||||
return boo::IWindowCallback::KEY_F8;
|
||||
case kVK_F9:
|
||||
return boo::IWindowCallback::KEY_F9;
|
||||
case kVK_F10:
|
||||
return boo::IWindowCallback::KEY_F10;
|
||||
case kVK_F11:
|
||||
return boo::IWindowCallback::KEY_F11;
|
||||
case kVK_F12:
|
||||
return boo::IWindowCallback::KEY_F12;
|
||||
case kVK_Escape:
|
||||
return boo::IWindowCallback::KEY_ESC;
|
||||
case kVK_Return:
|
||||
return boo::IWindowCallback::KEY_ENTER;
|
||||
case kVK_Delete:
|
||||
return boo::IWindowCallback::KEY_BACKSPACE;
|
||||
case kVK_ForwardDelete:
|
||||
return boo::IWindowCallback::KEY_DELETE;
|
||||
case kVK_Home:
|
||||
return boo::IWindowCallback::KEY_HOME;
|
||||
case kVK_End:
|
||||
return boo::IWindowCallback::KEY_END;
|
||||
case kVK_PageUp:
|
||||
return boo::IWindowCallback::KEY_PGUP;
|
||||
case kVK_PageDown:
|
||||
return boo::IWindowCallback::KEY_PGDOWN;
|
||||
case kVK_LeftArrow:
|
||||
return boo::IWindowCallback::KEY_LEFT;
|
||||
case kVK_RightArrow:
|
||||
return boo::IWindowCallback::KEY_RIGHT;
|
||||
case kVK_UpArrow:
|
||||
return boo::IWindowCallback::KEY_UP;
|
||||
case kVK_DownArrow:
|
||||
return boo::IWindowCallback::KEY_DOWN;
|
||||
default:
|
||||
return boo::IWindowCallback::KEY_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
- (void)keyDown:(NSEvent*)theEvent
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
NSString* chars = theEvent.characters;
|
||||
if ([chars length] == 0)
|
||||
booContext->m_callback->specialKeyDown(translateKeycode(theEvent.keyCode),
|
||||
getMod(theEvent.modifierFlags),
|
||||
theEvent.isARepeat);
|
||||
else
|
||||
booContext->m_callback->charKeyDown([chars characterAtIndex:0],
|
||||
getMod(theEvent.modifierFlags),
|
||||
theEvent.isARepeat);
|
||||
}
|
||||
|
||||
- (void)keyUp:(NSEvent*)theEvent
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
NSString* chars = theEvent.characters;
|
||||
if ([chars length] == 0)
|
||||
booContext->m_callback->specialKeyUp(translateKeycode(theEvent.keyCode),
|
||||
getMod(theEvent.modifierFlags));
|
||||
else
|
||||
booContext->m_callback->charKeyUp([chars characterAtIndex:0],
|
||||
getMod(theEvent.modifierFlags));
|
||||
}
|
||||
|
||||
- (void)flagsChanged:(NSEvent*)theEvent
|
||||
{
|
||||
if (!booContext->m_callback)
|
||||
return;
|
||||
NSUInteger modFlags = theEvent.modifierFlags;
|
||||
bool isRepeat = theEvent.isARepeat;
|
||||
if (modFlags != lastModifiers)
|
||||
{
|
||||
NSUInteger changedFlags = modFlags ^ lastModifiers;
|
||||
|
||||
NSUInteger downFlags = changedFlags & modFlags;
|
||||
if (downFlags & NSControlKeyMask)
|
||||
booContext->m_callback->modKeyDown(boo::IWindowCallback::MKEY_CTRL, isRepeat);
|
||||
if (downFlags & NSAlternateKeyMask)
|
||||
booContext->m_callback->modKeyDown(boo::IWindowCallback::MKEY_ALT, isRepeat);
|
||||
if (downFlags & NSShiftKeyMask)
|
||||
booContext->m_callback->modKeyDown(boo::IWindowCallback::MKEY_SHIFT, isRepeat);
|
||||
if (downFlags & NSCommandKeyMask)
|
||||
booContext->m_callback->modKeyDown(boo::IWindowCallback::MKEY_COMMAND, isRepeat);
|
||||
|
||||
NSUInteger upFlags = changedFlags & ~modFlags;
|
||||
if (upFlags & NSControlKeyMask)
|
||||
booContext->m_callback->modKeyUp(boo::IWindowCallback::MKEY_CTRL);
|
||||
if (upFlags & NSAlternateKeyMask)
|
||||
booContext->m_callback->modKeyUp(boo::IWindowCallback::MKEY_ALT);
|
||||
if (upFlags & NSShiftKeyMask)
|
||||
booContext->m_callback->modKeyUp(boo::IWindowCallback::MKEY_SHIFT);
|
||||
if (upFlags & NSCommandKeyMask)
|
||||
booContext->m_callback->modKeyUp(boo::IWindowCallback::MKEY_COMMAND);
|
||||
|
||||
lastModifiers = modFlags;
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
180
lib/mac/WindowCocoa.mm
Normal file
180
lib/mac/WindowCocoa.mm
Normal file
@@ -0,0 +1,180 @@
|
||||
#import <AppKit/AppKit.h>
|
||||
#include "IApplication.hpp"
|
||||
#include "windowsys/IWindow.hpp"
|
||||
#include "windowsys/IGraphicsContext.hpp"
|
||||
|
||||
namespace boo {class CWindowCocoa;}
|
||||
@interface CWindowCocoaInternal : NSWindow
|
||||
{
|
||||
boo::CWindowCocoa* booWindow;
|
||||
}
|
||||
- (id)initWithBooWindow:(boo::CWindowCocoa*)bw title:(const std::string&)title;
|
||||
- (void)setFrameDefault;
|
||||
- (NSRect)genFrameDefault;
|
||||
@end
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
IGraphicsContext* _CGraphicsContextCocoaNew(IGraphicsContext::EGraphicsAPI api,
|
||||
IWindow* parentWindow);
|
||||
|
||||
class CWindowCocoa final : public IWindow
|
||||
{
|
||||
|
||||
CWindowCocoaInternal* m_nsWindow;
|
||||
IGraphicsContext* m_gfxCtx;
|
||||
|
||||
public:
|
||||
|
||||
CWindowCocoa(const std::string& title)
|
||||
{
|
||||
m_nsWindow = [[CWindowCocoaInternal alloc] initWithBooWindow:this title:title];
|
||||
m_gfxCtx = _CGraphicsContextCocoaNew(IGraphicsContext::API_OPENGL_3_3, this);
|
||||
m_gfxCtx->initializeContext();
|
||||
}
|
||||
|
||||
void _clearWindow()
|
||||
{
|
||||
m_nsWindow = NULL;
|
||||
}
|
||||
|
||||
~CWindowCocoa()
|
||||
{
|
||||
[m_nsWindow orderOut:nil];
|
||||
[m_nsWindow release];
|
||||
delete m_gfxCtx;
|
||||
IApplicationInstance()->_deletedWindow(this);
|
||||
}
|
||||
|
||||
void setCallback(IWindowCallback* cb)
|
||||
{
|
||||
m_gfxCtx->_setCallback(cb);
|
||||
}
|
||||
|
||||
void showWindow()
|
||||
{
|
||||
[m_nsWindow makeKeyAndOrderFront:nil];
|
||||
}
|
||||
|
||||
void hideWindow()
|
||||
{
|
||||
[m_nsWindow orderOut:nil];
|
||||
}
|
||||
|
||||
std::string getTitle()
|
||||
{
|
||||
return [[m_nsWindow title] UTF8String];
|
||||
}
|
||||
|
||||
void setTitle(const std::string& title)
|
||||
{
|
||||
[m_nsWindow setTitle:[[NSString stringWithUTF8String:title.c_str()] autorelease]];
|
||||
}
|
||||
|
||||
void setWindowFrameDefault()
|
||||
{
|
||||
NSScreen* mainScreen = [NSScreen mainScreen];
|
||||
NSRect scrFrame = mainScreen.frame;
|
||||
float x_off = scrFrame.size.width / 3.0;
|
||||
float y_off = scrFrame.size.height / 3.0;
|
||||
[m_nsWindow setFrame:NSMakeRect(x_off, y_off, x_off * 2.0, y_off * 2.0) display:NO];
|
||||
}
|
||||
|
||||
void getWindowFrame(float& xOut, float& yOut, float& wOut, float& hOut) const
|
||||
{
|
||||
NSRect wFrame = m_nsWindow.frame;
|
||||
xOut = wFrame.origin.x;
|
||||
yOut = wFrame.origin.y;
|
||||
wOut = wFrame.size.width;
|
||||
hOut = wFrame.size.height;
|
||||
}
|
||||
|
||||
void setWindowFrame(float x, float y, float w, float h)
|
||||
{
|
||||
NSRect wFrame = NSMakeRect(x, y, w, h);
|
||||
[m_nsWindow setFrame:wFrame display:NO];
|
||||
}
|
||||
|
||||
float getVirtualPixelFactor() const
|
||||
{
|
||||
return [m_nsWindow backingScaleFactor];
|
||||
}
|
||||
|
||||
bool isFullscreen() const
|
||||
{
|
||||
return ([m_nsWindow styleMask] & NSFullScreenWindowMask) == NSFullScreenWindowMask;
|
||||
}
|
||||
|
||||
void setFullscreen(bool fs)
|
||||
{
|
||||
if ((fs && !isFullscreen()) || (!fs && isFullscreen()))
|
||||
[m_nsWindow toggleFullScreen:nil];
|
||||
}
|
||||
|
||||
ETouchType getTouchType() const
|
||||
{
|
||||
return TOUCH_TRACKPAD;
|
||||
}
|
||||
|
||||
uintptr_t getPlatformHandle() const
|
||||
{
|
||||
return (uintptr_t)m_nsWindow;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
IWindow* _CWindowCocoaNew(const std::string& title)
|
||||
{
|
||||
return new CWindowCocoa(title);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@implementation CWindowCocoaInternal
|
||||
- (id)initWithBooWindow:(boo::CWindowCocoa *)bw title:(const std::string&)title
|
||||
{
|
||||
self = [self initWithContentRect:[self genFrameDefault]
|
||||
styleMask:NSTitledWindowMask|
|
||||
NSClosableWindowMask|
|
||||
NSMiniaturizableWindowMask|
|
||||
NSResizableWindowMask
|
||||
backing:NSBackingStoreBuffered
|
||||
defer:YES];
|
||||
self.title = [[NSString stringWithUTF8String:title.c_str()] autorelease];
|
||||
booWindow = bw;
|
||||
return self;
|
||||
}
|
||||
- (void)setFrameDefault
|
||||
{
|
||||
[self setFrame:[self genFrameDefault] display:NO];
|
||||
}
|
||||
- (NSRect)genFrameDefault
|
||||
{
|
||||
NSScreen* mainScreen = [NSScreen mainScreen];
|
||||
NSRect scrFrame = mainScreen.frame;
|
||||
float width = scrFrame.size.width * 2.0 / 3.0;
|
||||
float height = scrFrame.size.height * 2.0 / 3.0;
|
||||
return NSMakeRect((scrFrame.size.width - width) / 2.0,
|
||||
(scrFrame.size.height - height) / 2.0,
|
||||
width, height);
|
||||
}
|
||||
- (void)close
|
||||
{
|
||||
booWindow->_clearWindow();
|
||||
[super close];
|
||||
}
|
||||
- (BOOL)acceptsFirstResponder
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
- (BOOL)acceptsMouseMovedEvents
|
||||
{
|
||||
return YES;
|
||||
}
|
||||
- (NSWindowCollectionBehavior)collectionBehavior
|
||||
{
|
||||
return NSWindowCollectionBehaviorFullScreenPrimary;
|
||||
}
|
||||
@end
|
||||
|
||||
166
lib/win/ApplicationWin32.cpp
Normal file
166
lib/win/ApplicationWin32.cpp
Normal file
@@ -0,0 +1,166 @@
|
||||
#define _CRT_SECURE_NO_WARNINGS 1 /* STFU MSVC */
|
||||
#define _WIN32_LEAN_AND_MEAN 1
|
||||
#include <windows.h>
|
||||
#include <initguid.h>
|
||||
#include <Usbiodef.h>
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "IRunLoop.hpp"
|
||||
#include "inputdev/CDeviceFinder.hpp"
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
IWindow* _CWindowWin32New(const std::string& title);
|
||||
|
||||
class CApplicationWin32 final : public IApplication
|
||||
{
|
||||
const IApplicationCallback& m_callback;
|
||||
const std::string m_friendlyName;
|
||||
const std::string m_pname;
|
||||
const std::vector<std::string> m_args;
|
||||
std::unordered_map<HWND, IWindow*> m_allWindows;
|
||||
bool m_singleInstance;
|
||||
|
||||
void _deletedWindow(IWindow* window)
|
||||
{
|
||||
m_allWindows.erase(window);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
CApplicationWin32(const IApplicationCallback& callback,
|
||||
const std::string& friendlyName,
|
||||
const std::string& pname,
|
||||
const std::vector<std::string>& args,
|
||||
bool singleInstance)
|
||||
: m_callback(callback),
|
||||
m_friendlyName(friendlyName),
|
||||
m_pname(pname),
|
||||
m_args(args),
|
||||
m_singleInstance(singleInstance)
|
||||
{}
|
||||
|
||||
EPlatformType getPlatformType() const
|
||||
{
|
||||
return PLAT_WIN32;
|
||||
}
|
||||
|
||||
LRESULT winHwndHandler(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
/* Lookup boo window instance */
|
||||
IWindow* window = m_allWindows[hwnd];
|
||||
switch (uMsg)
|
||||
{
|
||||
case WM_CREATE:
|
||||
return 0;
|
||||
|
||||
case WM_DEVICECHANGE:
|
||||
return CDeviceFinder::winDevChangedHandler(wParam, lParam);
|
||||
|
||||
default:
|
||||
return DefWindowProc(hwnd, uMsg, wParam, lParam);
|
||||
}
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
/* Pump messages */
|
||||
MSG msg = {0};
|
||||
while (GetMessage(&msg, NULL, 0, 0))
|
||||
{
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
}
|
||||
}
|
||||
|
||||
const std::string& getProcessName() const
|
||||
{
|
||||
return m_pname;
|
||||
}
|
||||
|
||||
const std::vector<std::string>& getArgs() const
|
||||
{
|
||||
return m_args;
|
||||
}
|
||||
|
||||
IWindow* newWindow(const std::string& title)
|
||||
{
|
||||
IWindow* window = _CWindowWin32New(title);
|
||||
HWND hwnd = window->getPlatformHandle();
|
||||
m_allWindows[hwnd] = window;
|
||||
}
|
||||
};
|
||||
|
||||
IApplication* APP = NULL;
|
||||
IApplication* IApplicationBootstrap(IApplication::EPlatformType platform,
|
||||
IApplicationCallback& cb,
|
||||
const std::string& friendlyName,
|
||||
const std::string& pname,
|
||||
const std::vector<std::string>& args,
|
||||
bool singleInstance)
|
||||
{
|
||||
if (!APP)
|
||||
{
|
||||
if (platform != IApplication::PLAT_WIN32 &&
|
||||
platform != IApplication::PLAT_AUTO)
|
||||
return NULL;
|
||||
APP = new CApplicationWin32(cb, friendlyName, pname, args, singleInstance);
|
||||
}
|
||||
return APP;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static const DEV_BROADCAST_DEVICEINTERFACE_A HOTPLUG_CONF =
|
||||
{
|
||||
sizeof(DEV_BROADCAST_DEVICEINTERFACE_A),
|
||||
DBT_DEVTYP_DEVICEINTERFACE,
|
||||
0,
|
||||
GUID_DEVINTERFACE_USB_DEVICE
|
||||
};
|
||||
static bool HOTPLUG_REGISTERED = false;
|
||||
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
|
||||
{
|
||||
if (!HOTPLUG_REGISTERED && hwnd == WM_CREATE)
|
||||
{
|
||||
/* Register hotplug notification with windows */
|
||||
RegisterDeviceNotificationA(hwnd, (LPVOID)&HOTPLUG_CONF, DEVICE_NOTIFY_WINDOW_HANDLE);
|
||||
HOTPLUG_REGISTERED = true;
|
||||
}
|
||||
return IRunLoopInstance()->winHwndHandler(hwnd, uMsg, wParam, lParam);
|
||||
}
|
||||
|
||||
int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPCWSTR lpCmdLine, int)
|
||||
{
|
||||
#if DEBUG
|
||||
/* Debug console */
|
||||
AllocConsole();
|
||||
freopen("CONOUT$", "w", stdout);
|
||||
#endif
|
||||
|
||||
/* One class for *all* boo windows */
|
||||
WNDCLASS wndClass =
|
||||
{
|
||||
0,
|
||||
WindowProc,
|
||||
0,
|
||||
0,
|
||||
hInstance,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
L"BooWindow"
|
||||
};
|
||||
|
||||
RegisterClassW(&wndClass);
|
||||
|
||||
int argc = 0;
|
||||
LPWSTR* argv = CommandLineToArgvW(lpCmdLine, &argc);
|
||||
|
||||
/* Call into the 'proper' entry point */
|
||||
return main(argc, argv);
|
||||
|
||||
}
|
||||
83
lib/win/GraphicsContextWin32.cpp
Normal file
83
lib/win/GraphicsContextWin32.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
#include "windowsys/IGraphicsContext.hpp"
|
||||
#include "windowsys/IWindow.hpp"
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
class CGraphicsContextWin32 final : public IGraphicsContext
|
||||
{
|
||||
|
||||
EGraphicsAPI m_api;
|
||||
EPixelFormat m_pf;
|
||||
IWindow* m_parentWindow;
|
||||
|
||||
public:
|
||||
IWindowCallback* m_callback;
|
||||
|
||||
CGraphicsContextWin32(EGraphicsAPI api, IWindow* parentWindow)
|
||||
: m_api(api),
|
||||
m_pf(PF_RGBA8),
|
||||
m_parentWindow(parentWindow)
|
||||
{}
|
||||
|
||||
~CGraphicsContextWin32()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void _setCallback(IWindowCallback* cb)
|
||||
{
|
||||
m_callback = cb;
|
||||
}
|
||||
|
||||
EGraphicsAPI getAPI() const
|
||||
{
|
||||
return m_api;
|
||||
}
|
||||
|
||||
EPixelFormat getPixelFormat() const
|
||||
{
|
||||
return m_pf;
|
||||
}
|
||||
|
||||
void setPixelFormat(EPixelFormat pf)
|
||||
{
|
||||
if (pf > PF_RGBAF32_Z24)
|
||||
return;
|
||||
m_pf = pf;
|
||||
}
|
||||
|
||||
void initializeContext()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
IGraphicsContext* makeShareContext() const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void makeCurrent()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void clearCurrent()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void swapBuffer()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
IGraphicsContext* _CGraphicsContextWin32New(IGraphicsContext::EGraphicsAPI api,
|
||||
IWindow* parentWindow)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
96
lib/win/WindowWin32.cpp
Normal file
96
lib/win/WindowWin32.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include "windowsys/IWindow.hpp"
|
||||
#include "windowsys/IGraphicsContext.hpp"
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
IGraphicsContext* _CGraphicsContextWin32New(IGraphicsContext::EGraphicsAPI api,
|
||||
IWindow* parentWindow);
|
||||
|
||||
class CWindowWin32 final : public IWindow
|
||||
{
|
||||
|
||||
HWND m_hwnd;
|
||||
|
||||
public:
|
||||
|
||||
CWindowWin32(const std::string& title)
|
||||
{
|
||||
m_hwnd = CreateWindowW(L"BooWindow", L"BooTest", WS_OVERLAPPEDWINDOW,
|
||||
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
|
||||
NULL, NULL, hInstance, NULL);
|
||||
}
|
||||
|
||||
~CWindowWin32()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void setCallback(IWindowCallback* cb)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void showWindow()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void hideWindow()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string getTitle()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void setTitle(const std::string& title)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void setWindowFrameDefault()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void getWindowFrame(float& xOut, float& yOut, float& wOut, float& hOut) const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void setWindowFrame(float x, float y, float w, float h)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
float getVirtualPixelFactor() const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool isFullscreen() const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void setFullscreen(bool fs)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ETouchType getTouchType() const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
IWindow* _CWindowWin32New(const std::string& title)
|
||||
{
|
||||
return new CWindowWin32(title);
|
||||
}
|
||||
|
||||
}
|
||||
71
lib/x11/ApplicationUnix.cpp
Normal file
71
lib/x11/ApplicationUnix.cpp
Normal file
@@ -0,0 +1,71 @@
|
||||
/* Meta-implementation for dynamically-constructing user's preferred
|
||||
* platform interface
|
||||
*/
|
||||
|
||||
#define APPLICATION_UNIX_CPP
|
||||
#include "ApplicationXCB.hpp"
|
||||
#include "ApplicationWayland.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <dbus/dbus.h>
|
||||
#include <stdio.h>
|
||||
|
||||
DBusConnection* registerDBus(const char* appName, bool& isFirst)
|
||||
{
|
||||
isFirst = true;
|
||||
DBusError err = {};
|
||||
dbus_error_init(&err);
|
||||
|
||||
/* connect to the bus and check for errors */
|
||||
DBusConnection* conn = dbus_bus_get(DBUS_BUS_SESSION, &err);
|
||||
if (dbus_error_is_set(&err))
|
||||
{
|
||||
fprintf(stderr, "DBus Connection Error (%s)\n", err.message);
|
||||
dbus_error_free(&err);
|
||||
}
|
||||
if (NULL == conn)
|
||||
return NULL;
|
||||
|
||||
/* request our name on the bus and check for errors */
|
||||
char busName[256];
|
||||
snprintf(busName, 256, "boo.%s.unique", appName);
|
||||
int ret = dbus_bus_request_name(conn, busName, DBUS_NAME_FLAG_DO_NOT_QUEUE , &err);
|
||||
if (dbus_error_is_set(&err))
|
||||
{
|
||||
fprintf(stderr, "DBus Name Error (%s)\n", err.message);
|
||||
dbus_error_free(&err);
|
||||
dbus_connection_close(conn);
|
||||
return NULL;
|
||||
}
|
||||
if (DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER != ret)
|
||||
isFirst = false;
|
||||
|
||||
return conn;
|
||||
|
||||
}
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
IApplication* APP = NULL;
|
||||
std::unique_ptr<IApplication> ApplicationBootstrap(IApplication::EPlatformType platform,
|
||||
IApplicationCallback& cb,
|
||||
const std::string& uniqueName,
|
||||
const std::string& friendlyName,
|
||||
const std::string& pname,
|
||||
const std::vector<std::string>& args,
|
||||
bool singleInstance)
|
||||
{
|
||||
if (APP)
|
||||
return std::unique_ptr<IApplication>();
|
||||
if (platform == IApplication::PLAT_WAYLAND)
|
||||
APP = new ApplicationWayland(cb, uniqueName, friendlyName, pname, args, singleInstance);
|
||||
else if (platform == IApplication::PLAT_XCB ||
|
||||
platform == IApplication::PLAT_AUTO)
|
||||
APP = new ApplicationXCB(cb, uniqueName, friendlyName, pname, args, singleInstance);
|
||||
else
|
||||
return std::unique_ptr<IApplication>();
|
||||
return std::unique_ptr<IApplication>(APP);
|
||||
}
|
||||
|
||||
}
|
||||
85
lib/x11/ApplicationWayland.hpp
Normal file
85
lib/x11/ApplicationWayland.hpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#ifndef APPLICATION_UNIX_CPP
|
||||
#error This file may only be included from CApplicationUnix.cpp
|
||||
#endif
|
||||
|
||||
#include "boo/IApplication.hpp"
|
||||
|
||||
#include <dbus/dbus.h>
|
||||
DBusConnection* registerDBus(const char* appName, bool& isFirst);
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
IWindow* _CWindowWaylandNew(const std::string& title);
|
||||
|
||||
class ApplicationWayland final : public IApplication
|
||||
{
|
||||
IApplicationCallback& m_callback;
|
||||
const std::string m_uniqueName;
|
||||
const std::string m_friendlyName;
|
||||
const std::string m_pname;
|
||||
const std::vector<std::string> m_args;
|
||||
bool m_singleInstance;
|
||||
|
||||
void _deletedWindow(IWindow* window)
|
||||
{
|
||||
(void)window;
|
||||
}
|
||||
|
||||
public:
|
||||
ApplicationWayland(IApplicationCallback& callback,
|
||||
const std::string& uniqueName,
|
||||
const std::string& friendlyName,
|
||||
const std::string& pname,
|
||||
const std::vector<std::string>& args,
|
||||
bool singleInstance)
|
||||
: m_callback(callback),
|
||||
m_uniqueName(uniqueName),
|
||||
m_friendlyName(friendlyName),
|
||||
m_pname(pname),
|
||||
m_args(args),
|
||||
m_singleInstance(singleInstance)
|
||||
{}
|
||||
|
||||
EPlatformType getPlatformType() const
|
||||
{
|
||||
return PLAT_WAYLAND;
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void quit()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
const std::string& getUniqueName() const
|
||||
{
|
||||
return m_uniqueName;
|
||||
}
|
||||
|
||||
const std::string& getFriendlyName() const
|
||||
{
|
||||
return m_friendlyName;
|
||||
}
|
||||
|
||||
const std::string& getProcessName() const
|
||||
{
|
||||
return m_pname;
|
||||
}
|
||||
|
||||
const std::vector<std::string>& getArgs() const
|
||||
{
|
||||
return m_args;
|
||||
}
|
||||
|
||||
IWindow* newWindow(const std::string& title)
|
||||
{
|
||||
return _CWindowWaylandNew(title);
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
334
lib/x11/ApplicationXCB.hpp
Normal file
334
lib/x11/ApplicationXCB.hpp
Normal file
@@ -0,0 +1,334 @@
|
||||
#ifndef APPLICATION_UNIX_CPP
|
||||
#error This file may only be included from CApplicationUnix.cpp
|
||||
#endif
|
||||
|
||||
#include "boo/IApplication.hpp"
|
||||
|
||||
#define explicit explicit_c
|
||||
#include <xcb/xcb.h>
|
||||
#include <xcb/xcb_event.h>
|
||||
#include <xkbcommon/xkbcommon-x11.h>
|
||||
#include <xcb/xkb.h>
|
||||
#include <xcb/xinput.h>
|
||||
#undef explicit
|
||||
|
||||
#include <dbus/dbus.h>
|
||||
DBusConnection* registerDBus(const char* appName, bool& isFirst);
|
||||
|
||||
#include <sys/param.h>
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
int XINPUT_OPCODE = 0;
|
||||
|
||||
static xcb_window_t getWindowOfEvent(xcb_generic_event_t* event, bool& windowEvent)
|
||||
{
|
||||
switch (XCB_EVENT_RESPONSE_TYPE(event))
|
||||
{
|
||||
case XCB_CLIENT_MESSAGE:
|
||||
{
|
||||
xcb_client_message_event_t* ev = (xcb_client_message_event_t*)event;
|
||||
windowEvent = true;
|
||||
return ev->window;
|
||||
}
|
||||
case XCB_EXPOSE:
|
||||
{
|
||||
xcb_expose_event_t* ev = (xcb_expose_event_t*)event;
|
||||
windowEvent = true;
|
||||
return ev->window;
|
||||
}
|
||||
case XCB_CONFIGURE_NOTIFY:
|
||||
{
|
||||
xcb_configure_notify_event_t* ev = (xcb_configure_notify_event_t*)event;
|
||||
windowEvent = true;
|
||||
return ev->window;
|
||||
}
|
||||
case XCB_KEY_PRESS:
|
||||
{
|
||||
xcb_key_press_event_t* ev = (xcb_key_press_event_t*)event;
|
||||
windowEvent = true;
|
||||
return ev->event;
|
||||
}
|
||||
case XCB_KEY_RELEASE:
|
||||
{
|
||||
xcb_key_release_event_t* ev = (xcb_key_release_event_t*)event;
|
||||
windowEvent = true;
|
||||
return ev->event;
|
||||
}
|
||||
case XCB_BUTTON_PRESS:
|
||||
{
|
||||
xcb_button_press_event_t* ev = (xcb_button_press_event_t*)event;
|
||||
windowEvent = true;
|
||||
return ev->event;
|
||||
}
|
||||
case XCB_BUTTON_RELEASE:
|
||||
{
|
||||
xcb_button_release_event_t* ev = (xcb_button_release_event_t*)event;
|
||||
windowEvent = true;
|
||||
return ev->event;
|
||||
}
|
||||
case XCB_MOTION_NOTIFY:
|
||||
{
|
||||
xcb_motion_notify_event_t* ev = (xcb_motion_notify_event_t*)event;
|
||||
windowEvent = true;
|
||||
return ev->event;
|
||||
}
|
||||
case XCB_GE_GENERIC:
|
||||
{
|
||||
xcb_ge_event_t* gev = (xcb_ge_event_t*)event;
|
||||
if (gev->pad0 == XINPUT_OPCODE)
|
||||
{
|
||||
switch (gev->event_type)
|
||||
{
|
||||
case XCB_INPUT_MOTION:
|
||||
{
|
||||
xcb_input_motion_event_t* ev = (xcb_input_motion_event_t*)event;
|
||||
windowEvent = true;
|
||||
return ev->event;
|
||||
}
|
||||
case XCB_INPUT_TOUCH_BEGIN:
|
||||
{
|
||||
xcb_input_touch_begin_event_t* ev = (xcb_input_touch_begin_event_t*)event;
|
||||
windowEvent = true;
|
||||
return ev->event;
|
||||
}
|
||||
case XCB_INPUT_TOUCH_UPDATE:
|
||||
{
|
||||
xcb_input_touch_update_event_t* ev = (xcb_input_touch_update_event_t*)event;
|
||||
windowEvent = true;
|
||||
return ev->event;
|
||||
}
|
||||
case XCB_INPUT_TOUCH_END:
|
||||
{
|
||||
xcb_input_touch_end_event_t* ev = (xcb_input_touch_end_event_t*)event;
|
||||
windowEvent = true;
|
||||
return ev->event;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
windowEvent = false;
|
||||
return 0;
|
||||
}
|
||||
|
||||
IWindow* _CWindowXCBNew(const std::string& title, xcb_connection_t* conn);
|
||||
|
||||
class ApplicationXCB final : public IApplication
|
||||
{
|
||||
IApplicationCallback& m_callback;
|
||||
const std::string m_uniqueName;
|
||||
const std::string m_friendlyName;
|
||||
const std::string m_pname;
|
||||
const std::vector<std::string> m_args;
|
||||
|
||||
/* DBus single-instance */
|
||||
bool m_singleInstance;
|
||||
DBusConnection* m_dbus = NULL;
|
||||
|
||||
/* All windows */
|
||||
std::unordered_map<xcb_window_t, IWindow*> m_windows;
|
||||
|
||||
xcb_connection_t* m_xcbConn = NULL;
|
||||
bool m_running;
|
||||
|
||||
void _deletedWindow(IWindow* window)
|
||||
{
|
||||
m_windows.erase((xcb_window_t)window->getPlatformHandle());
|
||||
}
|
||||
|
||||
public:
|
||||
ApplicationXCB(IApplicationCallback& callback,
|
||||
const std::string& uniqueName,
|
||||
const std::string& friendlyName,
|
||||
const std::string& pname,
|
||||
const std::vector<std::string>& args,
|
||||
bool singleInstance)
|
||||
: m_callback(callback),
|
||||
m_uniqueName(uniqueName),
|
||||
m_friendlyName(friendlyName),
|
||||
m_pname(pname),
|
||||
m_args(args),
|
||||
m_singleInstance(singleInstance)
|
||||
{
|
||||
/* DBus single instance registration */
|
||||
bool isFirst;
|
||||
m_dbus = registerDBus(uniqueName.c_str(), isFirst);
|
||||
if (m_singleInstance)
|
||||
{
|
||||
if (!isFirst)
|
||||
{
|
||||
/* This is a duplicate instance, send signal and return */
|
||||
if (args.size())
|
||||
{
|
||||
/* create a signal & check for errors */
|
||||
DBusMessage*
|
||||
msg = dbus_message_new_signal("/boo/signal/FileHandler",
|
||||
"boo.signal.FileHandling",
|
||||
"Open");
|
||||
|
||||
/* append arguments onto signal */
|
||||
DBusMessageIter argsIter;
|
||||
dbus_message_iter_init_append(msg, &argsIter);
|
||||
for (const std::string& arg : args)
|
||||
{
|
||||
const char* sigvalue = arg.c_str();
|
||||
dbus_message_iter_append_basic(&argsIter, DBUS_TYPE_STRING, &sigvalue);
|
||||
}
|
||||
|
||||
/* send the message and flush the connection */
|
||||
dbus_uint32_t serial;
|
||||
dbus_connection_send(m_dbus, msg, &serial);
|
||||
dbus_connection_flush(m_dbus);
|
||||
dbus_message_unref(msg);
|
||||
}
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* This is the first instance, register for signal */
|
||||
// add a rule for which messages we want to see
|
||||
DBusError err = {};
|
||||
dbus_bus_add_match(m_dbus, "type='signal',interface='boo.signal.FileHandling'", &err);
|
||||
dbus_connection_flush(m_dbus);
|
||||
}
|
||||
}
|
||||
|
||||
/* Open X connection */
|
||||
m_xcbConn = xcb_connect(NULL, NULL);
|
||||
|
||||
/* The xkb extension requests that the X server does not
|
||||
* send repeated keydown events when a key is held */
|
||||
xkb_x11_setup_xkb_extension(m_xcbConn,
|
||||
XKB_X11_MIN_MAJOR_XKB_VERSION,
|
||||
XKB_X11_MIN_MINOR_XKB_VERSION,
|
||||
XKB_X11_SETUP_XKB_EXTENSION_NO_FLAGS,
|
||||
NULL, NULL, NULL, NULL);
|
||||
xcb_xkb_per_client_flags(m_xcbConn, XCB_XKB_ID_USE_CORE_KBD,
|
||||
XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT,
|
||||
XCB_XKB_PER_CLIENT_FLAG_DETECTABLE_AUTO_REPEAT, 0, 0, 0);
|
||||
|
||||
/* Xinput major opcode */
|
||||
const xcb_query_extension_reply_t* xiReply =
|
||||
xcb_get_extension_data(m_xcbConn, &xcb_input_id);
|
||||
if (xiReply)
|
||||
XINPUT_OPCODE = xiReply->major_opcode;
|
||||
|
||||
}
|
||||
|
||||
~ApplicationXCB()
|
||||
{
|
||||
xcb_disconnect(m_xcbConn);
|
||||
}
|
||||
|
||||
EPlatformType getPlatformType() const
|
||||
{
|
||||
return PLAT_XCB;
|
||||
}
|
||||
|
||||
void run()
|
||||
{
|
||||
if (!m_xcbConn)
|
||||
return;
|
||||
|
||||
xcb_generic_event_t* event;
|
||||
m_running = true;
|
||||
m_callback.appLaunched(this);
|
||||
xcb_flush(m_xcbConn);
|
||||
|
||||
int xcbFd = xcb_get_file_descriptor(m_xcbConn);
|
||||
int dbusFd;
|
||||
dbus_connection_get_unix_fd(m_dbus, &dbusFd);
|
||||
int maxFd = MAX(xcbFd, dbusFd);
|
||||
|
||||
while (m_running)
|
||||
{
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(xcbFd, &fds);
|
||||
FD_SET(dbusFd, &fds);
|
||||
select(maxFd+1, &fds, NULL, NULL, NULL);
|
||||
|
||||
if (FD_ISSET(xcbFd, &fds))
|
||||
{
|
||||
event = xcb_poll_for_event(m_xcbConn);
|
||||
if (!event)
|
||||
break;
|
||||
|
||||
bool windowEvent;
|
||||
xcb_window_t evWindow = getWindowOfEvent(event, windowEvent);
|
||||
//fprintf(stderr, "EVENT %d\n", XCB_EVENT_RESPONSE_TYPE(event));
|
||||
if (windowEvent)
|
||||
{
|
||||
auto window = m_windows.find(evWindow);
|
||||
if (window != m_windows.end())
|
||||
window->second->_incomingEvent(event);
|
||||
}
|
||||
free(event);
|
||||
}
|
||||
|
||||
if (FD_ISSET(dbusFd, &fds))
|
||||
{
|
||||
DBusMessage* msg;
|
||||
dbus_connection_read_write(m_dbus, 0);
|
||||
while ((msg = dbus_connection_pop_message(m_dbus)))
|
||||
{
|
||||
/* check if the message is a signal from the correct interface and with the correct name */
|
||||
if (dbus_message_is_signal(msg, "boo.signal.FileHandling", "Open"))
|
||||
{
|
||||
/* read the parameters */
|
||||
std::vector<std::string> paths;
|
||||
DBusMessageIter iter;
|
||||
dbus_message_iter_init(msg, &iter);
|
||||
while (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INVALID)
|
||||
{
|
||||
const char* argVal;
|
||||
dbus_message_iter_get_basic(&iter, &argVal);
|
||||
paths.push_back(argVal);
|
||||
dbus_message_iter_next(&iter);
|
||||
}
|
||||
m_callback.appFilesOpen(this, paths);
|
||||
}
|
||||
dbus_message_unref(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
m_callback.appQuitting(this);
|
||||
}
|
||||
|
||||
void quit()
|
||||
{
|
||||
m_running = false;
|
||||
}
|
||||
|
||||
const std::string& getUniqueName() const
|
||||
{
|
||||
return m_uniqueName;
|
||||
}
|
||||
|
||||
const std::string& getFriendlyName() const
|
||||
{
|
||||
return m_friendlyName;
|
||||
}
|
||||
|
||||
const std::string& getProcessName() const
|
||||
{
|
||||
return m_pname;
|
||||
}
|
||||
|
||||
const std::vector<std::string>& getArgs() const
|
||||
{
|
||||
return m_args;
|
||||
}
|
||||
|
||||
IWindow* newWindow(const std::string& title)
|
||||
{
|
||||
IWindow* newWindow = _CWindowXCBNew(title, m_xcbConn);
|
||||
m_windows[(xcb_window_t)newWindow->getPlatformHandle()] = newWindow;
|
||||
return newWindow;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
63
lib/x11/GraphicsContextWayland.cpp
Normal file
63
lib/x11/GraphicsContextWayland.cpp
Normal file
@@ -0,0 +1,63 @@
|
||||
#include "boo/IGraphicsContext.hpp"
|
||||
#include "boo/IWindow.hpp"
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
struct GraphicsContextWayland : IGraphicsContext
|
||||
{
|
||||
|
||||
EGraphicsAPI m_api;
|
||||
EPixelFormat m_pf;
|
||||
IWindow* m_parentWindow;
|
||||
|
||||
public:
|
||||
IWindowCallback* m_callback;
|
||||
|
||||
GraphicsContextWayland(EGraphicsAPI api, IWindow* parentWindow)
|
||||
: m_api(api),
|
||||
m_pf(PF_RGBA8),
|
||||
m_parentWindow(parentWindow)
|
||||
{}
|
||||
|
||||
~GraphicsContextWayland()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void _setCallback(IWindowCallback* cb)
|
||||
{
|
||||
m_callback = cb;
|
||||
}
|
||||
|
||||
EGraphicsAPI getAPI() const
|
||||
{
|
||||
return m_api;
|
||||
}
|
||||
|
||||
EPixelFormat getPixelFormat() const
|
||||
{
|
||||
return m_pf;
|
||||
}
|
||||
|
||||
void setPixelFormat(EPixelFormat pf)
|
||||
{
|
||||
if (pf > PF_RGBAF32_Z24)
|
||||
return;
|
||||
m_pf = pf;
|
||||
}
|
||||
|
||||
void initializeContext()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
IGraphicsContext* _GraphicsContextWaylandNew(IGraphicsContext::EGraphicsAPI api,
|
||||
IWindow* parentWindow)
|
||||
{
|
||||
return new GraphicsContextWayland(api, parentWindow);
|
||||
}
|
||||
|
||||
}
|
||||
153
lib/x11/GraphicsContextXCB.cpp
Normal file
153
lib/x11/GraphicsContextXCB.cpp
Normal file
@@ -0,0 +1,153 @@
|
||||
#include "boo/IGraphicsContext.hpp"
|
||||
#include "boo/IWindow.hpp"
|
||||
|
||||
#include <xcb/xcb.h>
|
||||
#include <xcb/glx.h>
|
||||
#include <GL/glx.h>
|
||||
#include <GL/glcorearb.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <thread>
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
struct GraphicsContextXCB : IGraphicsContext
|
||||
{
|
||||
|
||||
EGraphicsAPI m_api;
|
||||
EPixelFormat m_pf;
|
||||
IWindow* m_parentWindow;
|
||||
xcb_connection_t* m_xcbConn;
|
||||
|
||||
xcb_glx_fbconfig_t m_fbconfig = 0;
|
||||
xcb_visualid_t m_visualid = 0;
|
||||
xcb_glx_window_t m_glxWindow = 0;
|
||||
xcb_glx_context_t m_glxCtx = 0;
|
||||
xcb_glx_context_tag_t m_glxCtxTag = 0;
|
||||
|
||||
std::thread* m_commandThread = NULL;
|
||||
|
||||
public:
|
||||
IWindowCallback* m_callback;
|
||||
|
||||
GraphicsContextXCB(EGraphicsAPI api, IWindow* parentWindow, xcb_connection_t* conn, uint32_t& visualIdOut)
|
||||
: m_api(api),
|
||||
m_pf(PF_RGBA8),
|
||||
m_parentWindow(parentWindow),
|
||||
m_xcbConn(conn)
|
||||
{
|
||||
|
||||
/* WTF freedesktop?? Fix this awful API and your nonexistant docs */
|
||||
xcb_glx_get_fb_configs_reply_t* fbconfigs =
|
||||
xcb_glx_get_fb_configs_reply(m_xcbConn, xcb_glx_get_fb_configs(m_xcbConn, 0), NULL);
|
||||
struct conf_prop
|
||||
{
|
||||
uint32_t key;
|
||||
uint32_t val;
|
||||
}* props = (struct conf_prop*)xcb_glx_get_fb_configs_property_list(fbconfigs);
|
||||
|
||||
for (uint32_t i=0 ; i<fbconfigs->num_FB_configs ; ++i)
|
||||
{
|
||||
struct conf_prop* configProps = &props[fbconfigs->num_properties * i];
|
||||
uint32_t fbconfId, visualId, depthSize, colorSize, doubleBuffer;
|
||||
for (uint32_t j=0 ; j<fbconfigs->num_properties ; ++j)
|
||||
{
|
||||
struct conf_prop* prop = &configProps[j];
|
||||
if (prop->key == GLX_FBCONFIG_ID)
|
||||
fbconfId = prop->val;
|
||||
if (prop->key == GLX_VISUAL_ID)
|
||||
visualId = prop->val;
|
||||
else if (prop->key == GLX_DEPTH_SIZE)
|
||||
depthSize = prop->val;
|
||||
else if (prop->key == GLX_BUFFER_SIZE)
|
||||
colorSize = prop->val;
|
||||
else if (prop->key == GLX_DOUBLEBUFFER)
|
||||
doubleBuffer = prop->val;
|
||||
}
|
||||
|
||||
/* Double-buffer only */
|
||||
if (!doubleBuffer)
|
||||
continue;
|
||||
|
||||
if (m_pf == PF_RGBA8 && colorSize >= 32)
|
||||
{
|
||||
m_fbconfig = fbconfId;
|
||||
m_visualid = visualId;
|
||||
break;
|
||||
}
|
||||
else if (m_pf == PF_RGBA8_Z24 && colorSize >= 32 && depthSize >= 24)
|
||||
{
|
||||
m_fbconfig = fbconfId;
|
||||
m_visualid = visualId;
|
||||
break;
|
||||
}
|
||||
else if (m_pf == PF_RGBAF32 && colorSize >= 128)
|
||||
{
|
||||
m_fbconfig = fbconfId;
|
||||
m_visualid = visualId;
|
||||
break;
|
||||
}
|
||||
else if (m_pf == PF_RGBAF32_Z24 && colorSize >= 128 && depthSize >= 24)
|
||||
{
|
||||
m_fbconfig = fbconfId;
|
||||
m_visualid = visualId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(fbconfigs);
|
||||
|
||||
if (!m_fbconfig)
|
||||
{
|
||||
fprintf(stderr, "unable to find suitable pixel format");
|
||||
return;
|
||||
}
|
||||
|
||||
visualIdOut = m_visualid;
|
||||
}
|
||||
|
||||
~GraphicsContextXCB()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void _setCallback(IWindowCallback* cb)
|
||||
{
|
||||
m_callback = cb;
|
||||
}
|
||||
|
||||
EGraphicsAPI getAPI() const
|
||||
{
|
||||
return m_api;
|
||||
}
|
||||
|
||||
EPixelFormat getPixelFormat() const
|
||||
{
|
||||
return m_pf;
|
||||
}
|
||||
|
||||
void setPixelFormat(EPixelFormat pf)
|
||||
{
|
||||
if (pf > PF_RGBAF32_Z24)
|
||||
return;
|
||||
m_pf = pf;
|
||||
}
|
||||
|
||||
void initializeContext()
|
||||
{
|
||||
m_glxWindow = xcb_generate_id(m_xcbConn);
|
||||
xcb_glx_create_window(m_xcbConn, 0, m_fbconfig,
|
||||
m_parentWindow->getPlatformHandle(),
|
||||
m_glxWindow, 0, NULL);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
IGraphicsContext* _GraphicsContextXCBNew(IGraphicsContext::EGraphicsAPI api,
|
||||
IWindow* parentWindow, xcb_connection_t* conn,
|
||||
uint32_t& visualIdOut)
|
||||
{
|
||||
return new GraphicsContextXCB(api, parentWindow, conn, visualIdOut);
|
||||
}
|
||||
|
||||
}
|
||||
94
lib/x11/WindowWayland.cpp
Normal file
94
lib/x11/WindowWayland.cpp
Normal file
@@ -0,0 +1,94 @@
|
||||
#include "boo/IWindow.hpp"
|
||||
#include "boo/IGraphicsContext.hpp"
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
IGraphicsContext* _CGraphicsContextWaylandNew(IGraphicsContext::EGraphicsAPI api,
|
||||
IWindow* parentWindow);
|
||||
|
||||
struct WindowWayland : IWindow
|
||||
{
|
||||
WindowWayland(const std::string& title)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
~WindowWayland()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void setCallback(IWindowCallback* cb)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void showWindow()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void hideWindow()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string getTitle()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void setTitle(const std::string& title)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void setWindowFrameDefault()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void getWindowFrame(float& xOut, float& yOut, float& wOut, float& hOut) const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void setWindowFrame(float x, float y, float w, float h)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
float getVirtualPixelFactor() const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool isFullscreen() const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void setFullscreen(bool fs)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
uintptr_t getPlatformHandle() const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ETouchType getTouchType() const
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
IWindow* _CWindowWaylandNew(const std::string& title)
|
||||
{
|
||||
return new WindowWayland(title);
|
||||
}
|
||||
|
||||
}
|
||||
770
lib/x11/WindowXCB.cpp
Normal file
770
lib/x11/WindowXCB.cpp
Normal file
@@ -0,0 +1,770 @@
|
||||
#include "boo/IWindow.hpp"
|
||||
#include "boo/IGraphicsContext.hpp"
|
||||
#include "boo/IApplication.hpp"
|
||||
|
||||
#include <xcb/xcb.h>
|
||||
#include <xcb/xcb_event.h>
|
||||
#include <xcb/xproto.h>
|
||||
#include <xcb/xcb_keysyms.h>
|
||||
#include <xkbcommon/xkbcommon.h>
|
||||
#include <xcb/xinput.h>
|
||||
#include <limits.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#define XK_MISCELLANY
|
||||
#define XK_XKB_KEYS
|
||||
#define XK_LATIN1
|
||||
#include <X11/keysymdef.h>
|
||||
|
||||
#define REF_DPMM 3.7824 /* 96 DPI */
|
||||
#define FS_ATOM "_NET_WM_STATE_FULLSCREEN"
|
||||
|
||||
namespace boo
|
||||
{
|
||||
|
||||
extern int XINPUT_OPCODE;
|
||||
|
||||
static inline double fp3232val(xcb_input_fp3232_t* val)
|
||||
{
|
||||
return val->integral + val->frac / (double)UINT_MAX;
|
||||
}
|
||||
|
||||
static uint32_t translateKeysym(xcb_keysym_t sym, int& specialSym, int& modifierSym)
|
||||
{
|
||||
specialSym = IWindowCallback::KEY_NONE;
|
||||
modifierSym = IWindowCallback::MKEY_NONE;
|
||||
if (sym >= XK_F1 && sym <= XK_F12)
|
||||
specialSym = IWindowCallback::KEY_F1 + sym - XK_F1;
|
||||
else if (sym == XK_Escape)
|
||||
specialSym = IWindowCallback::KEY_ESC;
|
||||
else if (sym == XK_Return)
|
||||
specialSym = IWindowCallback::KEY_ENTER;
|
||||
else if (sym == XK_BackSpace)
|
||||
specialSym = IWindowCallback::KEY_BACKSPACE;
|
||||
else if (sym == XK_Insert)
|
||||
specialSym = IWindowCallback::KEY_INSERT;
|
||||
else if (sym == XK_Delete)
|
||||
specialSym = IWindowCallback::KEY_DELETE;
|
||||
else if (sym == XK_Home)
|
||||
specialSym = IWindowCallback::KEY_HOME;
|
||||
else if (sym == XK_End)
|
||||
specialSym = IWindowCallback::KEY_END;
|
||||
else if (sym == XK_Page_Up)
|
||||
specialSym = IWindowCallback::KEY_PGUP;
|
||||
else if (sym == XK_Page_Down)
|
||||
specialSym = IWindowCallback::KEY_PGDOWN;
|
||||
else if (sym == XK_Left)
|
||||
specialSym = IWindowCallback::KEY_LEFT;
|
||||
else if (sym == XK_Right)
|
||||
specialSym = IWindowCallback::KEY_RIGHT;
|
||||
else if (sym == XK_Up)
|
||||
specialSym = IWindowCallback::KEY_UP;
|
||||
else if (sym == XK_Down)
|
||||
specialSym = IWindowCallback::KEY_DOWN;
|
||||
else if (sym == XK_Shift_L || sym == XK_Shift_R)
|
||||
modifierSym = IWindowCallback::MKEY_SHIFT;
|
||||
else if (sym == XK_Control_L || sym == XK_Control_R)
|
||||
modifierSym = IWindowCallback::MKEY_CTRL;
|
||||
else if (sym == XK_Alt_L || sym == XK_Alt_R)
|
||||
modifierSym = IWindowCallback::MKEY_ALT;
|
||||
else
|
||||
return xkb_keysym_to_utf32(sym);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int translateModifiers(unsigned state)
|
||||
{
|
||||
int retval = 0;
|
||||
if (state & XCB_MOD_MASK_SHIFT)
|
||||
retval |= IWindowCallback::MKEY_SHIFT;
|
||||
if (state & XCB_MOD_MASK_CONTROL)
|
||||
retval |= IWindowCallback::MKEY_CTRL;
|
||||
if (state & XCB_MOD_MASK_1)
|
||||
retval |= IWindowCallback::MKEY_ALT;
|
||||
return retval;
|
||||
}
|
||||
|
||||
static int translateButton(unsigned detail)
|
||||
{
|
||||
int retval = 0;
|
||||
if (detail == 1)
|
||||
retval = IWindowCallback::BUTTON_PRIMARY;
|
||||
else if (detail == 3)
|
||||
retval = IWindowCallback::BUTTON_SECONDARY;
|
||||
else if (detail == 2)
|
||||
retval = IWindowCallback::BUTTON_MIDDLE;
|
||||
else if (detail == 8)
|
||||
retval = IWindowCallback::BUTTON_AUX1;
|
||||
else if (detail == 9)
|
||||
retval =
|
||||
IWindowCallback::BUTTON_AUX2;
|
||||
return retval;
|
||||
}
|
||||
|
||||
#define INTERN_ATOM(var, conn, name, if_exists) \
|
||||
do {\
|
||||
xcb_intern_atom_cookie_t cookie = \
|
||||
xcb_intern_atom(conn, if_exists, sizeof(#name), #name); \
|
||||
xcb_intern_atom_reply_t* reply = \
|
||||
xcb_intern_atom_reply(conn, cookie, NULL); \
|
||||
var = reply->atom; \
|
||||
free(reply); \
|
||||
} while(0)
|
||||
|
||||
struct XCBAtoms
|
||||
{
|
||||
xcb_atom_t m_wmProtocols = 0;
|
||||
xcb_atom_t m_wmDeleteWindow = 0;
|
||||
xcb_atom_t m_netwmState = 0;
|
||||
xcb_atom_t m_netwmStateFullscreen = 0;
|
||||
xcb_atom_t m_netwmStateAdd = 0;
|
||||
xcb_atom_t m_netwmStateRemove = 0;
|
||||
xcb_key_symbols_t* m_keySyms = NULL;
|
||||
XCBAtoms(xcb_connection_t* conn)
|
||||
{
|
||||
INTERN_ATOM(m_wmProtocols, conn, WM_PROTOCOLS, 1);
|
||||
INTERN_ATOM(m_wmDeleteWindow, conn, WM_DELETE_WINDOW, 1);
|
||||
INTERN_ATOM(m_netwmState, conn, _NET_WM_STATE, 0);
|
||||
INTERN_ATOM(m_netwmStateFullscreen, conn, _NET_WM_STATE_FULLSCREEN, 0);
|
||||
INTERN_ATOM(m_netwmStateAdd, conn, _NET_WM_STATE_ADD, 0);
|
||||
INTERN_ATOM(m_netwmStateRemove, conn, _NET_WM_STATE_REMOVE, 0);
|
||||
m_keySyms = xcb_key_symbols_alloc(conn);
|
||||
}
|
||||
};
|
||||
static XCBAtoms* S_ATOMS = NULL;
|
||||
|
||||
static void genFrameDefault(xcb_screen_t* screen, int* xOut, int* yOut, int* wOut, int* hOut)
|
||||
{
|
||||
float width = screen->width_in_pixels * 2.0 / 3.0;
|
||||
float height = screen->height_in_pixels * 2.0 / 3.0;
|
||||
*xOut = (screen->width_in_pixels - width) / 2.0;
|
||||
*yOut = (screen->height_in_pixels - height) / 2.0;
|
||||
*wOut = width;
|
||||
*hOut = height;
|
||||
}
|
||||
|
||||
IGraphicsContext* _GraphicsContextXCBNew(IGraphicsContext::EGraphicsAPI api,
|
||||
IWindow* parentWindow, xcb_connection_t* conn,
|
||||
uint32_t& visualIdOut);
|
||||
|
||||
struct WindowXCB : IWindow
|
||||
{
|
||||
xcb_connection_t* m_xcbConn;
|
||||
xcb_window_t m_windowId;
|
||||
IGraphicsContext* m_gfxCtx;
|
||||
IWindowCallback* m_callback;
|
||||
|
||||
/* Last known input device id (0xffff if not yet set) */
|
||||
xcb_input_device_id_t m_lastInputID = 0xffff;
|
||||
ETouchType m_touchType = TOUCH_NONE;
|
||||
|
||||
/* Scroll valuators */
|
||||
int m_hScrollValuator = -1;
|
||||
int m_vScrollValuator = -1;
|
||||
double m_hScrollLast = 0.0;
|
||||
double m_vScrollLast = 0.0;
|
||||
|
||||
/* Cached window rectangle (to avoid repeated X queries) */
|
||||
int m_wx, m_wy, m_ww, m_wh;
|
||||
float m_pixelFactor;
|
||||
|
||||
public:
|
||||
|
||||
WindowXCB(const std::string& title, xcb_connection_t* conn)
|
||||
: m_xcbConn(conn), m_callback(NULL)
|
||||
{
|
||||
if (!S_ATOMS)
|
||||
S_ATOMS = new XCBAtoms(conn);
|
||||
|
||||
/* Default screen */
|
||||
xcb_screen_t* screen = xcb_setup_roots_iterator(xcb_get_setup(m_xcbConn)).data;
|
||||
m_pixelFactor = screen->width_in_pixels / (float)screen->width_in_millimeters / REF_DPMM;
|
||||
|
||||
/* Construct graphics context */
|
||||
uint32_t visualId;
|
||||
m_gfxCtx = _GraphicsContextXCBNew(IGraphicsContext::API_OPENGL_3_3, this, m_xcbConn, visualId);
|
||||
|
||||
/* Create colormap */
|
||||
xcb_colormap_t colormap = xcb_generate_id(m_xcbConn);
|
||||
xcb_create_colormap(m_xcbConn, XCB_COLORMAP_ALLOC_NONE,
|
||||
colormap, screen->root, visualId);
|
||||
|
||||
/* Create window */
|
||||
int x, y, w, h;
|
||||
genFrameDefault(screen, &x, &y, &w, &h);
|
||||
uint32_t valueMasks[] =
|
||||
{
|
||||
XCB_NONE,
|
||||
XCB_EVENT_MASK_KEY_PRESS | XCB_EVENT_MASK_KEY_RELEASE |
|
||||
XCB_EVENT_MASK_BUTTON_PRESS | XCB_EVENT_MASK_BUTTON_RELEASE |
|
||||
XCB_EVENT_MASK_POINTER_MOTION | XCB_EVENT_MASK_EXPOSURE |
|
||||
XCB_EVENT_MASK_STRUCTURE_NOTIFY,
|
||||
colormap,
|
||||
XCB_NONE
|
||||
};
|
||||
m_windowId = xcb_generate_id(conn);
|
||||
xcb_create_window(m_xcbConn, XCB_COPY_FROM_PARENT, m_windowId, screen->root,
|
||||
x, y, w, h, 10,
|
||||
XCB_WINDOW_CLASS_INPUT_OUTPUT, visualId,
|
||||
XCB_CW_BORDER_PIXEL | XCB_CW_EVENT_MASK | XCB_CW_COLORMAP,
|
||||
valueMasks);
|
||||
|
||||
/* The XInput 2.1 extension enables per-pixel smooth scrolling trackpads */
|
||||
xcb_generic_error_t* xiErr = NULL;
|
||||
xcb_input_xi_query_version_reply_t* xiReply =
|
||||
xcb_input_xi_query_version_reply(m_xcbConn,
|
||||
xcb_input_xi_query_version(m_xcbConn, 2, 1), &xiErr);
|
||||
if (!xiErr)
|
||||
{
|
||||
struct
|
||||
{
|
||||
xcb_input_event_mask_t mask;
|
||||
uint32_t maskVal;
|
||||
} masks =
|
||||
{
|
||||
{XCB_INPUT_DEVICE_ALL_MASTER, 1},
|
||||
XCB_INPUT_XI_EVENT_MASK_MOTION |
|
||||
XCB_INPUT_XI_EVENT_MASK_TOUCH_BEGIN |
|
||||
XCB_INPUT_XI_EVENT_MASK_TOUCH_UPDATE |
|
||||
XCB_INPUT_XI_EVENT_MASK_TOUCH_END
|
||||
};
|
||||
xcb_input_xi_select_events(m_xcbConn, m_windowId, 1, &masks.mask);
|
||||
}
|
||||
free(xiReply);
|
||||
|
||||
/* Register netwm extension atom for window closing */
|
||||
#if 0
|
||||
xcb_change_property(m_xcbConn, XCB_PROP_MODE_REPLACE, m_windowId, S_ATOMS->m_wmProtocols,
|
||||
XCB_ATOM_ATOM, 32, 1, &S_ATOMS->m_wmDeleteWindow);
|
||||
const xcb_atom_t wm_protocols[1] = {
|
||||
S_ATOMS->m_wmDeleteWindow,
|
||||
};
|
||||
xcb_change_property(m_xcbConn, XCB_PROP_MODE_REPLACE, m_windowId,
|
||||
S_ATOMS->m_wmProtocols, 4,
|
||||
32, 1, wm_protocols);
|
||||
#endif
|
||||
|
||||
/* Set the title of the window */
|
||||
const char* c_title = title.c_str();
|
||||
xcb_change_property(m_xcbConn, XCB_PROP_MODE_REPLACE, m_windowId,
|
||||
XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8,
|
||||
strlen(c_title), c_title);
|
||||
|
||||
/* Set the title of the window icon */
|
||||
xcb_change_property(m_xcbConn, XCB_PROP_MODE_REPLACE, m_windowId,
|
||||
XCB_ATOM_WM_ICON_NAME, XCB_ATOM_STRING, 8,
|
||||
strlen(c_title), c_title);
|
||||
|
||||
/* Initialize context */
|
||||
xcb_map_window(m_xcbConn, m_windowId);
|
||||
xcb_flush(m_xcbConn);
|
||||
|
||||
m_gfxCtx->initializeContext();
|
||||
}
|
||||
|
||||
~WindowXCB()
|
||||
{
|
||||
APP->_deletedWindow(this);
|
||||
}
|
||||
|
||||
void setCallback(IWindowCallback* cb)
|
||||
{
|
||||
m_callback = cb;
|
||||
}
|
||||
|
||||
void showWindow()
|
||||
{
|
||||
xcb_map_window(m_xcbConn, m_windowId);
|
||||
xcb_flush(m_xcbConn);
|
||||
}
|
||||
|
||||
void hideWindow()
|
||||
{
|
||||
xcb_unmap_window(m_xcbConn, m_windowId);
|
||||
xcb_flush(m_xcbConn);
|
||||
}
|
||||
|
||||
std::string getTitle()
|
||||
{
|
||||
xcb_get_property_cookie_t cookie =
|
||||
xcb_get_property(m_xcbConn, 0, m_windowId, XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 0, 64);
|
||||
xcb_get_property_reply_t* reply =
|
||||
xcb_get_property_reply(m_xcbConn, cookie, NULL);
|
||||
std::string retval((const char*)xcb_get_property_value(reply));
|
||||
free(reply);
|
||||
return retval;
|
||||
}
|
||||
|
||||
void setTitle(const std::string& title)
|
||||
{
|
||||
const char* c_title = title.c_str();
|
||||
xcb_change_property(m_xcbConn, XCB_PROP_MODE_REPLACE, m_windowId,
|
||||
XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8,
|
||||
strlen(c_title), c_title);
|
||||
}
|
||||
|
||||
void setWindowFrameDefault()
|
||||
{
|
||||
int x, y, w, h;
|
||||
xcb_screen_t* screen = xcb_setup_roots_iterator(xcb_get_setup(m_xcbConn)).data;
|
||||
genFrameDefault(screen, &x, &y, &w, &h);
|
||||
uint32_t values[] = {(uint32_t)x, (uint32_t)y, (uint32_t)w, (uint32_t)h};
|
||||
xcb_configure_window(m_xcbConn, m_windowId,
|
||||
XCB_CONFIG_WINDOW_X |
|
||||
XCB_CONFIG_WINDOW_Y |
|
||||
XCB_CONFIG_WINDOW_WIDTH |
|
||||
XCB_CONFIG_WINDOW_HEIGHT,
|
||||
values);
|
||||
}
|
||||
|
||||
void getWindowFrame(float& xOut, float& yOut, float& wOut, float& hOut) const
|
||||
{
|
||||
xOut = m_wx;
|
||||
yOut = m_wy;
|
||||
wOut = m_ww;
|
||||
hOut = m_wh;
|
||||
}
|
||||
|
||||
void setWindowFrame(float x, float y, float w, float h)
|
||||
{
|
||||
uint32_t values[] = {(uint32_t)x, (uint32_t)y, (uint32_t)w, (uint32_t)h};
|
||||
xcb_configure_window(m_xcbConn, m_windowId,
|
||||
XCB_CONFIG_WINDOW_X |
|
||||
XCB_CONFIG_WINDOW_Y |
|
||||
XCB_CONFIG_WINDOW_WIDTH |
|
||||
XCB_CONFIG_WINDOW_HEIGHT,
|
||||
values);
|
||||
}
|
||||
|
||||
float getVirtualPixelFactor() const
|
||||
{
|
||||
return m_pixelFactor;
|
||||
}
|
||||
|
||||
bool isFullscreen() const
|
||||
{
|
||||
xcb_get_property_cookie_t cookie =
|
||||
xcb_get_property(m_xcbConn, 0, m_windowId, S_ATOMS->m_netwmState, XCB_ATOM_ATOM, 0, 32);
|
||||
xcb_get_property_reply_t* reply =
|
||||
xcb_get_property_reply(m_xcbConn, cookie, NULL);
|
||||
char* props = (char*)xcb_get_property_value(reply);
|
||||
char fullscreen = false;
|
||||
for (unsigned i=0 ; i<reply->length/4 ; ++i)
|
||||
{
|
||||
if ((xcb_atom_t)props[i] == S_ATOMS->m_netwmStateFullscreen)
|
||||
{
|
||||
fullscreen = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(reply);
|
||||
return fullscreen;
|
||||
}
|
||||
|
||||
void setFullscreen(bool fs)
|
||||
{
|
||||
xcb_client_message_event_t fsEvent =
|
||||
{
|
||||
XCB_CLIENT_MESSAGE,
|
||||
32,
|
||||
0,
|
||||
m_windowId,
|
||||
S_ATOMS->m_netwmState,
|
||||
{}
|
||||
};
|
||||
fsEvent.data.data32[0] = fs ? S_ATOMS->m_netwmStateAdd : S_ATOMS->m_netwmStateRemove;
|
||||
fsEvent.data.data32[1] = S_ATOMS->m_netwmStateFullscreen;
|
||||
xcb_send_event(m_xcbConn, 0, m_windowId,
|
||||
XCB_EVENT_MASK_STRUCTURE_NOTIFY |
|
||||
XCB_EVENT_MASK_SUBSTRUCTURE_REDIRECT,
|
||||
(const char*)&fsEvent);
|
||||
}
|
||||
|
||||
uintptr_t getPlatformHandle() const
|
||||
{
|
||||
return (uintptr_t)m_windowId;
|
||||
}
|
||||
|
||||
void _pointingDeviceChanged(xcb_input_device_id_t deviceId)
|
||||
{
|
||||
xcb_input_xi_query_device_reply_t* reply =
|
||||
xcb_input_xi_query_device_reply(m_xcbConn, xcb_input_xi_query_device(m_xcbConn, deviceId), NULL);
|
||||
|
||||
xcb_input_xi_device_info_iterator_t infoIter = xcb_input_xi_query_device_infos_iterator(reply);
|
||||
while (infoIter.rem)
|
||||
{
|
||||
/* First iterate classes for scrollables */
|
||||
xcb_input_device_class_iterator_t classIter =
|
||||
xcb_input_xi_device_info_classes_iterator(infoIter.data);
|
||||
int hScroll = -1;
|
||||
int vScroll = -1;
|
||||
m_hScrollLast = 0.0;
|
||||
m_vScrollLast = 0.0;
|
||||
m_hScrollValuator = -1;
|
||||
m_vScrollValuator = -1;
|
||||
while (classIter.rem)
|
||||
{
|
||||
if (classIter.data->type == XCB_INPUT_DEVICE_CLASS_TYPE_SCROLL)
|
||||
{
|
||||
xcb_input_scroll_class_t* scrollClass = (xcb_input_scroll_class_t*)classIter.data;
|
||||
if (scrollClass->scroll_type == XCB_INPUT_SCROLL_TYPE_VERTICAL)
|
||||
vScroll = scrollClass->number;
|
||||
else if (scrollClass->scroll_type == XCB_INPUT_SCROLL_TYPE_HORIZONTAL)
|
||||
hScroll = scrollClass->number;
|
||||
}
|
||||
xcb_input_device_class_next(&classIter);
|
||||
}
|
||||
|
||||
/* Next iterate for touch and scroll valuators */
|
||||
classIter = xcb_input_xi_device_info_classes_iterator(infoIter.data);
|
||||
while (classIter.rem)
|
||||
{
|
||||
if (classIter.data->type == XCB_INPUT_DEVICE_CLASS_TYPE_VALUATOR)
|
||||
{
|
||||
xcb_input_valuator_class_t* valClass = (xcb_input_valuator_class_t*)classIter.data;
|
||||
if (valClass->number == vScroll)
|
||||
{
|
||||
m_vScrollLast = fp3232val(&valClass->value);
|
||||
m_vScrollValuator = vScroll;
|
||||
}
|
||||
else if (valClass->number == hScroll)
|
||||
{
|
||||
m_hScrollLast = fp3232val(&valClass->value);
|
||||
m_hScrollValuator = hScroll;
|
||||
}
|
||||
}
|
||||
else if (classIter.data->type == XCB_INPUT_DEVICE_CLASS_TYPE_TOUCH)
|
||||
{
|
||||
xcb_input_touch_class_t* touchClass = (xcb_input_touch_class_t*)classIter.data;
|
||||
if (touchClass->mode == XCB_INPUT_TOUCH_MODE_DIRECT)
|
||||
m_touchType = TOUCH_DISPLAY;
|
||||
else if (touchClass->mode == XCB_INPUT_TOUCH_MODE_DEPENDENT)
|
||||
m_touchType = TOUCH_TRACKPAD;
|
||||
else
|
||||
m_touchType = TOUCH_NONE;
|
||||
}
|
||||
xcb_input_device_class_next(&classIter);
|
||||
}
|
||||
xcb_input_xi_device_info_next(&infoIter);
|
||||
}
|
||||
|
||||
free(reply);
|
||||
m_lastInputID = deviceId;
|
||||
}
|
||||
|
||||
void _incomingEvent(void* e)
|
||||
{
|
||||
xcb_generic_event_t* event = (xcb_generic_event_t*)e;
|
||||
switch (XCB_EVENT_RESPONSE_TYPE(event))
|
||||
{
|
||||
case XCB_EXPOSE:
|
||||
{
|
||||
xcb_expose_event_t* ev = (xcb_expose_event_t*)event;
|
||||
m_wx = ev->x;
|
||||
m_wy = ev->y;
|
||||
m_ww = ev->width;
|
||||
m_wh = ev->height;
|
||||
return;
|
||||
}
|
||||
case XCB_CONFIGURE_NOTIFY:
|
||||
{
|
||||
xcb_configure_notify_event_t* ev = (xcb_configure_notify_event_t*)event;
|
||||
if (ev->width && ev->height)
|
||||
{
|
||||
m_wx = ev->x;
|
||||
m_wy = ev->y;
|
||||
m_ww = ev->width;
|
||||
m_wh = ev->height;
|
||||
}
|
||||
return;
|
||||
}
|
||||
case XCB_KEY_PRESS:
|
||||
{
|
||||
xcb_key_press_event_t* ev = (xcb_key_press_event_t*)event;
|
||||
if (m_callback)
|
||||
{
|
||||
int specialKey;
|
||||
int modifierKey;
|
||||
wchar_t charCode = translateKeysym(xcb_key_press_lookup_keysym(S_ATOMS->m_keySyms, ev, 0),
|
||||
specialKey, modifierKey);
|
||||
int modifierMask = translateModifiers(ev->state);
|
||||
if (charCode)
|
||||
m_callback->charKeyDown(charCode,
|
||||
(IWindowCallback::EModifierKey)modifierMask, false);
|
||||
else if (specialKey)
|
||||
m_callback->specialKeyDown((IWindowCallback::ESpecialKey)specialKey,
|
||||
(IWindowCallback::EModifierKey)modifierMask, false);
|
||||
else if (modifierKey)
|
||||
m_callback->modKeyDown((IWindowCallback::EModifierKey)modifierKey, false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
case XCB_KEY_RELEASE:
|
||||
{
|
||||
xcb_key_release_event_t* ev = (xcb_key_release_event_t*)event;
|
||||
if (m_callback)
|
||||
{
|
||||
int specialKey;
|
||||
int modifierKey;
|
||||
wchar_t charCode = translateKeysym(xcb_key_release_lookup_keysym(S_ATOMS->m_keySyms, ev, 0),
|
||||
specialKey, modifierKey);
|
||||
int modifierMask = translateModifiers(ev->state);
|
||||
if (charCode)
|
||||
m_callback->charKeyUp(charCode,
|
||||
(IWindowCallback::EModifierKey)modifierMask);
|
||||
else if (specialKey)
|
||||
m_callback->specialKeyUp((IWindowCallback::ESpecialKey)specialKey,
|
||||
(IWindowCallback::EModifierKey)modifierMask);
|
||||
else if (modifierKey)
|
||||
m_callback->modKeyUp((IWindowCallback::EModifierKey)modifierKey);
|
||||
}
|
||||
return;
|
||||
}
|
||||
case XCB_BUTTON_PRESS:
|
||||
{
|
||||
xcb_button_press_event_t* ev = (xcb_button_press_event_t*)event;
|
||||
if (m_callback)
|
||||
{
|
||||
int button = translateButton(ev->detail);
|
||||
if (button)
|
||||
{
|
||||
int modifierMask = translateModifiers(ev->state);
|
||||
IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{(unsigned)ev->event_x, (unsigned)ev->event_y},
|
||||
{(unsigned)(ev->event_x / m_pixelFactor), (unsigned)(ev->event_y / m_pixelFactor)},
|
||||
{ev->event_x / (float)m_ww, ev->event_y / (float)m_wh}
|
||||
};
|
||||
m_callback->mouseDown(coord, (IWindowCallback::EMouseButton)button,
|
||||
(IWindowCallback::EModifierKey)modifierMask);
|
||||
}
|
||||
|
||||
/* Also handle legacy scroll events here */
|
||||
if (ev->detail >= 4 && ev->detail <= 7 &&
|
||||
m_hScrollValuator == -1 && m_vScrollValuator == -1)
|
||||
{
|
||||
IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{(unsigned)ev->event_x, (unsigned)ev->event_y},
|
||||
{(unsigned)(ev->event_x / m_pixelFactor), (unsigned)(ev->event_y / m_pixelFactor)},
|
||||
{ev->event_x / (float)m_ww, ev->event_y / (float)m_wh}
|
||||
};
|
||||
IWindowCallback::SScrollDelta scrollDelta =
|
||||
{
|
||||
{0.0, 0.0},
|
||||
false
|
||||
};
|
||||
if (ev->detail == 4)
|
||||
scrollDelta.delta[1] = 1.0;
|
||||
else if (ev->detail == 5)
|
||||
scrollDelta.delta[1] = -1.0;
|
||||
else if (ev->detail == 6)
|
||||
scrollDelta.delta[0] = 1.0;
|
||||
else if (ev->detail == 7)
|
||||
scrollDelta.delta[0] = -1.0;
|
||||
m_callback->scroll(coord, scrollDelta);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
case XCB_BUTTON_RELEASE:
|
||||
{
|
||||
xcb_button_release_event_t* ev = (xcb_button_release_event_t*)event;
|
||||
if (m_callback)
|
||||
{
|
||||
int button = translateButton(ev->detail);
|
||||
if (button)
|
||||
{
|
||||
int modifierMask = translateModifiers(ev->state);
|
||||
IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{(unsigned)ev->event_x, (unsigned)ev->event_y},
|
||||
{(unsigned)(ev->event_x / m_pixelFactor), (unsigned)(ev->event_y / m_pixelFactor)},
|
||||
{ev->event_x / (float)m_ww, ev->event_y / (float)m_wh}
|
||||
};
|
||||
m_callback->mouseUp(coord, (IWindowCallback::EMouseButton)button,
|
||||
(IWindowCallback::EModifierKey)modifierMask);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
case XCB_MOTION_NOTIFY:
|
||||
{
|
||||
xcb_motion_notify_event_t* ev = (xcb_motion_notify_event_t*)event;
|
||||
if (m_callback)
|
||||
{
|
||||
IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{(unsigned)ev->event_x, (unsigned)ev->event_y},
|
||||
{(unsigned)(ev->event_x / m_pixelFactor), (unsigned)(ev->event_y / m_pixelFactor)},
|
||||
{ev->event_x / (float)m_ww, ev->event_y / (float)m_wh}
|
||||
};
|
||||
m_callback->mouseMove(coord);
|
||||
}
|
||||
return;
|
||||
}
|
||||
case XCB_GE_GENERIC:
|
||||
{
|
||||
xcb_ge_event_t* gev = (xcb_ge_event_t*)event;
|
||||
if (gev->pad0 == XINPUT_OPCODE)
|
||||
{
|
||||
switch (gev->event_type)
|
||||
{
|
||||
case XCB_INPUT_MOTION:
|
||||
{
|
||||
xcb_input_motion_event_t* ev = (xcb_input_motion_event_t*)event;
|
||||
if (m_lastInputID != ev->deviceid)
|
||||
_pointingDeviceChanged(ev->deviceid);
|
||||
|
||||
uint32_t* valuators = (uint32_t*)(((char*)ev) + sizeof(xcb_input_motion_event_t) + sizeof(uint32_t) * ev->buttons_len);
|
||||
xcb_input_fp3232_t* valuatorVals = (xcb_input_fp3232_t*)(((char*)valuators) + sizeof(uint32_t) * ev->valuators_len);
|
||||
int cv = 0;
|
||||
double newScroll[2] = {m_hScrollLast, m_vScrollLast};
|
||||
bool didScroll = false;
|
||||
for (int i=0 ; i<32 ; ++i)
|
||||
{
|
||||
if (valuators[0] & (1<<i))
|
||||
{
|
||||
if (i == m_hScrollValuator)
|
||||
{
|
||||
newScroll[0] = fp3232val(&valuatorVals[cv]);
|
||||
didScroll = true;
|
||||
}
|
||||
else if (i == m_vScrollValuator)
|
||||
{
|
||||
newScroll[1] = fp3232val(&valuatorVals[cv]);
|
||||
didScroll = true;
|
||||
}
|
||||
++cv;
|
||||
}
|
||||
}
|
||||
|
||||
IWindowCallback::SScrollDelta scrollDelta =
|
||||
{
|
||||
{newScroll[0] - m_hScrollLast, newScroll[1] - m_vScrollLast},
|
||||
true
|
||||
};
|
||||
|
||||
m_hScrollLast = newScroll[0];
|
||||
m_vScrollLast = newScroll[1];
|
||||
|
||||
if (m_callback && didScroll)
|
||||
{
|
||||
unsigned event_x = ev->event_x >> 16;
|
||||
unsigned event_y = ev->event_y >> 16;
|
||||
IWindowCallback::SWindowCoord coord =
|
||||
{
|
||||
{event_x, event_y},
|
||||
{(unsigned)(event_x / m_pixelFactor), (unsigned)(event_y / m_pixelFactor)},
|
||||
{event_x / (float)m_ww, event_y / (float)m_wh}
|
||||
};
|
||||
m_callback->scroll(coord, scrollDelta);
|
||||
}
|
||||
return;
|
||||
}
|
||||
case XCB_INPUT_TOUCH_BEGIN:
|
||||
{
|
||||
xcb_input_touch_begin_event_t* ev = (xcb_input_touch_begin_event_t*)event;
|
||||
if (m_lastInputID != ev->deviceid)
|
||||
_pointingDeviceChanged(ev->deviceid);
|
||||
|
||||
uint32_t* valuators = (uint32_t*)(((char*)ev) + sizeof(xcb_input_motion_event_t) + sizeof(uint32_t) * ev->buttons_len);
|
||||
xcb_input_fp3232_t* valuatorVals = (xcb_input_fp3232_t*)(((char*)valuators) + sizeof(uint32_t) * ev->valuators_len);
|
||||
int cv = 0;
|
||||
double vals[32] = {};
|
||||
for (int i=0 ; i<32 ; ++i)
|
||||
{
|
||||
if (valuators[0] & (1<<i))
|
||||
{
|
||||
vals[i] = fp3232val(&valuatorVals[cv]);
|
||||
++cv;
|
||||
}
|
||||
}
|
||||
|
||||
IWindowCallback::STouchCoord coord =
|
||||
{
|
||||
{vals[0], vals[1]}
|
||||
};
|
||||
|
||||
if (m_callback)
|
||||
m_callback->touchDown(coord, ev->detail);
|
||||
return;
|
||||
}
|
||||
case XCB_INPUT_TOUCH_UPDATE:
|
||||
{
|
||||
xcb_input_touch_update_event_t* ev = (xcb_input_touch_update_event_t*)event;
|
||||
if (m_lastInputID != ev->deviceid)
|
||||
_pointingDeviceChanged(ev->deviceid);
|
||||
|
||||
uint32_t* valuators = (uint32_t*)(((char*)ev) + sizeof(xcb_input_motion_event_t) + sizeof(uint32_t) * ev->buttons_len);
|
||||
xcb_input_fp3232_t* valuatorVals = (xcb_input_fp3232_t*)(((char*)valuators) + sizeof(uint32_t) * ev->valuators_len);
|
||||
int cv = 0;
|
||||
double vals[32] = {};
|
||||
for (int i=0 ; i<32 ; ++i)
|
||||
{
|
||||
if (valuators[0] & (1<<i))
|
||||
{
|
||||
vals[i] = fp3232val(&valuatorVals[cv]);
|
||||
++cv;
|
||||
}
|
||||
}
|
||||
|
||||
IWindowCallback::STouchCoord coord =
|
||||
{
|
||||
{vals[0], vals[1]}
|
||||
};
|
||||
|
||||
if (m_callback)
|
||||
m_callback->touchMove(coord, ev->detail);
|
||||
return;
|
||||
}
|
||||
case XCB_INPUT_TOUCH_END:
|
||||
{
|
||||
xcb_input_touch_end_event_t* ev = (xcb_input_touch_end_event_t*)event;
|
||||
if (m_lastInputID != ev->deviceid)
|
||||
_pointingDeviceChanged(ev->deviceid);
|
||||
|
||||
uint32_t* valuators = (uint32_t*)(((char*)ev) + sizeof(xcb_input_motion_event_t) + sizeof(uint32_t) * ev->buttons_len);
|
||||
xcb_input_fp3232_t* valuatorVals = (xcb_input_fp3232_t*)(((char*)valuators) + sizeof(uint32_t) * ev->valuators_len);
|
||||
int cv = 0;
|
||||
double vals[32] = {};
|
||||
for (int i=0 ; i<32 ; ++i)
|
||||
{
|
||||
if (valuators[0] & (1<<i))
|
||||
{
|
||||
vals[i] = fp3232val(&valuatorVals[cv]);
|
||||
++cv;
|
||||
}
|
||||
}
|
||||
|
||||
IWindowCallback::STouchCoord coord =
|
||||
{
|
||||
{vals[0], vals[1]}
|
||||
};
|
||||
|
||||
if (m_callback)
|
||||
m_callback->touchUp(coord, ev->detail);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ETouchType getTouchType() const
|
||||
{
|
||||
return m_touchType;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
IWindow* _CWindowXCBNew(const std::string& title, xcb_connection_t* conn)
|
||||
{
|
||||
return new WindowXCB(title, conn);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user