Commit a2de5867 authored by Thom Cherryhomes's avatar Thom Cherryhomes

Refs #2771 - Implement first pass of UInput class.

parent e24cf0b1
Pipeline #651 passed with stage
in 30 seconds
......@@ -26,7 +26,7 @@ VERSION = 2.0.0
MYDEVTEMPLATEID = 1965
Gen_Dev_Files = $(GENDEV)/Game_PlayerBase.cpp
sources = Main.cpp Game_Player.cpp $(Gen_Dev_Files)
sources = UInput.cpp Main.cpp Game_Player.cpp $(Gen_Dev_Files)
#<-dceag-mh-e->
CXXFLAGS +=
......
......@@ -19,8 +19,19 @@
*
*/
#define DEFAULT_KEYPRESS_DELAY 100 // default keypress delay
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "UInput.h"
using namespace DCE;
/**
* ctor
*/
......@@ -28,6 +39,8 @@ UInput::UInput()
{
fd=0;
DeviceName_set("Game Player Virtual Device");
Initialized_set(false);
KeyPress_Delay_set(DEFAULT_KEYPRESS_DELAY);
memset(&uidev, 0, sizeof(uidev));
}
......@@ -37,6 +50,8 @@ UInput::UInput()
UInput::~UInput()
{
fd=0;
Initialized_set(false);
KeyPress_Delay_set(0);
DeviceName_set("");
memset(&uidev, 0, sizeof(uidev));
}
......@@ -46,7 +61,10 @@ UInput::~UInput()
*/
bool UInput::Init()
{
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, DeviceName_get().c_str());
if (IsInitialized())
return false;;
snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "%s", DeviceName_get().c_str());
SetDeviceCharacteristics();
fd=open("/dev/input/uinput", O_WRONLY | O_NONBLOCK);
......@@ -54,11 +72,9 @@ bool UInput::Init()
return false;
SetDeviceEventBits();
WriteDeviceCharacteristics();
CreateDevice();
Initialized_set(true);
return true;
}
......@@ -67,6 +83,12 @@ bool UInput::Init()
*/
bool UInput::Done()
{
if (!IsInitialized())
return false;
if (ioctl(fd, UI_DEV_DESTROY) < 0)
return false;
return true;
}
......@@ -90,24 +112,26 @@ string UInput::DeviceName_get()
* SetDeviceCharacteristics() - Called to populate uidev struct
* with the apporpriate virtual device characteristics.
*/
bool UInput::SetDeviceCharacteristics()
void UInput::SetDeviceCharacteristics()
{
uidev.id.bustype=BUS_USB;
uidev.id.vendor=0x1234;
uidev.id.product=0xfedc;
uidev.id.version=1;
return true;
}
/**
* SetDeviceEventBits() - Set the virtual device event bits
*/
void UInput::SetDeviceEventBits()
bool UInput::SetDeviceEventBits()
{
// By default, we are a keyboard.
ioctl(fd, UI_SET_EVBIT, EV_KEY);
if (ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0)
{
return false;
}
// Set all keys.
// We want to be able to emit any key event.
for (int i=0; i<KEY_MAX; i++)
{
if (ioctl(fd, UI_SET_KEYBIT, i) < 0)
......@@ -115,20 +139,121 @@ void UInput::SetDeviceEventBits()
return false;
}
}
return true;
}
/**
* WriteDeviceCharacteristics() - Write Device Characteristics to uinput
*/
void UInput::WriteDeviceCharacteristics()
int UInput::WriteDeviceCharacteristics()
{
write(fd, &uidev, sizeof(uidev));
return write(fd, &uidev, sizeof(uidev));
}
/**
* CreateDevice() - Create the virtual device.
*/
void UInput::CreateDevice()
int UInput::CreateDevice()
{
return ioctl(fd, UI_DEV_CREATE);
}
/**
* Initialized_set() - Set initialized flag
*/
void UInput::Initialized_set(bool bInitialized)
{
m_bInitialized=true;
}
/**
* IsInitialized() - Get Initialized flag
*/
bool UInput::IsInitialized()
{
return m_bInitialized;
}
/**
* KeyDown(int iEvent) - Press key down
*/
bool UInput::KeyDown(int iEvent)
{
if (!IsInitialized())
return false;
struct input_event ev[2];
memset(&ev,0,sizeof(ev));
ev[0].type = EV_KEY;
ev[0].code = iEvent;
ev[0].value=1;
ev[1].type = EV_SYN;
ev[1].code = SYN_REPORT;
ev[1].value=0;
if (write(fd,ev,sizeof(ev)) < 2)
return false;
return true;
}
/**
* KeyUp(int iEvent) - Press key up
*/
bool UInput::KeyUp(int iEvent)
{
if (!IsInitialized())
return false;
struct input_event ev[2];
memset(&ev,0,sizeof(ev));
ev[0].type = EV_KEY;
ev[0].code = iEvent;
ev[0].value=0;
ev[1].type = EV_SYN;
ev[1].code = SYN_REPORT;
ev[1].value=0;
if (write(fd,ev,sizeof(ev)) < 2)
return false;
return true;
}
/**
* KeyPress(int iEvent) - Press the requested key down, delay by the Keypress delay, then Press the requested key up.
*/
bool UInput::KeyPress(int iEvent)
{
if (!KeyDown(iEvent))
{
// This failed, but we will try a key up, anyway.
}
usleep(KeyPress_Delay_get()*1000);
if (!KeyUp(iEvent))
{
// This failed, we'll return false.
return false;
}
return true;
}
/**
* KeyPress_Delay_set(int iDelay) - Set the wanted Keypress delay
*/
void UInput::KeyPress_Delay_set(int iDelay)
{
m_iKeypressDelay=iDelay;
}
/**
* KeyPress_Delay_get() - Get the wanted Keypress delay.
*/
int UInput::KeyPress_Delay_get()
{
ioctl(fd, UI_DEV_CREATE);
return m_iKeypressDelay;
}
......@@ -11,6 +11,10 @@
#include <linux/input.h>
#include <linux/uinput.h>
#include <string>
using namespace std;
namespace DCE
{
class UInput
......@@ -23,18 +27,29 @@ namespace DCE
virtual bool Init();
virtual bool Done();
virtual void Initialized_set(bool bInitialized);
virtual bool IsInitialized();
virtual bool KeyUp(int iEvent); // send a key up event to the UInput device
virtual bool KeyDown(int iEvent); // send a key down event to the UInput device
virtual bool KeyPress(int iEvent); // Press a key down, then release up, to UInput device, after keypress delay.
virtual void KeyPress_Delay_set(int iDelay); // Set the wanted keypress delay
virtual int KeyPress_Delay_get(); // Get the current keypress delay.
protected:
virtual void DeviceName_set(string sDeviceName); // Set Device Name
virtual string DeviceName_get(); // Get Device Name
virtual bool SetDeviceCharacteristics(); // Called in init() to set uidev characteristics.
virtual void SetDeviceCharacteristics(); // Called in init() to set uidev characteristics.
virtual bool SetDeviceEventBits(); // Called in init() to set uinput EV_BITs.
virtual bool WriteDeviceCharacteristics(); // Write the device characteristics to uinput
virtual bool CreateDevice(); // Create the uinput device
virtual int WriteDeviceCharacteristics(); // Write the device characteristics to uinput
virtual int CreateDevice(); // Create the uinput device
private:
int fd; // uinput file descriptor
struct uinput_user_dev uidev; // uinput user device
string m_sDeviceName; // Name of device to return via uinput.
bool m_bInitialized; // FLAG: is UInput initialized?
int m_iKeypressDelay; // the keypress delay used in Keypress(int iEvent)
};
}
#endif UINPUT_H
#endif /* UINPUT_H */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment