ioctl(fd, UI_SET_ABSBIT, ABS_Y);
- snprintf(uidev.name, UINPUT_MAX_NAME_SIZE,"linkmeevent%d",ev-10);
+ snprintf(uidev.name, UINPUT_MAX_NAME_SIZE,"linkmeevent%d",ev);
uidev.id.bustype = BUS_USB;
uidev.id.vendor = 0x7777;
uidev.id.product = 0x7777;
/* Steam Controller support */
SDL_bool m_bSteamController;
typedef struct _SDL_JoystickAxisInfo
Sint16 initial_value; /* Initial axis state */
SDL_bool sent_initial_value; /* Whether we've sent the initial axis value */
+//strucct SDL_JoystickAxisInfo;
int handle_as_joystick(int output,SDL_Event e){
+ printf("event type=0x%x\n",e.type);
if( e.type==SDL_JOYAXISMOTION ){
if( e.caxis.value > 0 ) v=255;
+typedef struct _map_event_uevent_links{
+ int out_fd; // uinput handle
+eventlink EvLink[10]; // mapped 4-13 to 0-9
+int make_output_devices(){
+ int output = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
+ die("Cannot open the output device");
+ // make virtual input event with "linkmeevent[4-13]"
+ make_output(output,ev);
+ // find new virtual/input and link to event4-13
+ sprintf(target,"linkmeevent%d",ev);
+ char *dev=find_device(target);
+ sprintf(cmdline,"unlink /dev/input/event%d",ev);
+ sprintf(cmdline,"ln -s %s /dev/input/event%d",dev,ev);
+ EvLink[ev-4].out_fd=output;
+ die("fatal error. linkmeevent device not found.");
+int joystick_add_callback(int jsindex){
+ printf("add index [%d] name : %s \n",jsindex,SDL_JoystickNameForIndex(jsindex));
+ SDL_Joystick *j=SDL_JoystickOpen(jsindex);
+ int n_event_input=atoi(j->hwdata->fname+strlen("/dev/input/event"));
+ if(n_event_input>=14 && n_event_input<=23){
+ if( SDL_IsGameController(jsindex)){
+ SDL_GameController *g=SDL_GameControllerOpen(jsindex);
+ j=SDL_GameControllerGetJoystick(g);
+ printf("is game controller. Handle it\n");
+ EvLink[n_event_input-14].joystick_id=INVALID;
+ EvLink[n_event_input-14].controller_id=jsindex;
+ EvLink[n_event_input-14].instance_id=SDL_JoystickInstanceID(j);
+ EvLink[n_event_input-14].ptr=g;
+ printf("fatal error on open gamecontroller\n");
+ SDL_Joystick *j=SDL_JoystickOpen(jsindex);
+ printf("is not game controller. treat as simple joystick.%s\n",SDL_GetError());
+ EvLink[n_event_input-14].joystick_id=jsindex;
+ EvLink[n_event_input-14].controller_id=INVALID;
+ EvLink[n_event_input-14].instance_id=SDL_JoystickInstanceID(j);
+ EvLink[n_event_input-14].ptr=j;
+ printf("It's not target.\n");
+int joystick_remove_callback(int jsinstance){
+ printf("joystick_remove_callback(%d)\n",jsinstance);
+ if( EvLink[ec].instance_id==jsinstance){
+ if( EvLink[ec].joystick_id!=-1 ){
+ SDL_Joystick* j=(SDL_Joystick*)EvLink[ec].ptr;
+ SDL_GameController* g=(SDL_GameController*)EvLink[ec].ptr;
+ SDL_GameControllerClose(g);
+ EvLink[ec].joystick_id=INVALID;
+ EvLink[ec].controller_id=INVALID;
+ EvLink[ec].instance_id=INVALID;
+int get_output_device(int jsinstance){
+ printf("get_output_device(%d)\n",jsinstance);
+ if( EvLink[ec].instance_id==jsinstance){
+ int output=EvLink[ec].out_fd;
+ printf(" return <%d>\n",output);
+int get_is_gamecontroller(int jsinstance){
+ printf("get_is_gamecontroller(%d)\n",jsinstance);
+ if( EvLink[ec].instance_id==jsinstance){
+ return (EvLink[ec].controller_id==-1)?0:1;
+int emit_abs_xy(int output,int xy,int value){
+ emit(output, EV_ABS, xy, value );
+ emit(output, EV_SYN, SYN_REPORT, 0);
+int joystick_axis_handle(SDL_Event e){
+ if( get_is_gamecontroller(e.jaxis.which )) return;
+ int output=get_output_device(e.jaxis.which);
+ if( output == -1) return -1;
+ if( e.caxis.value > 0 ) v=255;
+ if( e.caxis.value < 0 ) v=0;
+ printf("axis=%d value=%d\n",e.jaxis.axis,e.jaxis.value);
+ emit_abs_xy(output,e.jaxis.axis,v);
+int controller_asix_handle(SDL_Event e){
+ int output=get_output_device(e.caxis.which);
+ if( output == -1) return -1;
+ if( e.caxis.value > 0 ) v=255;
+ if( e.caxis.value < 0 ) v=0;
+ printf("axis=%d value=%d\n",e.caxis.axis,e.caxis.value);
+ emit_abs_xy(output,e.caxis.axis,v);
+int joystick_button_handle(SDL_Event e){
+ if( get_is_gamecontroller(e.jbutton.which)) return;
+ int output=get_output_device(e.jbutton.which);
+ if( output == -1) return -1;
+ printf("Button=%d\n",e.jbutton.button);
+ switch(e.jbutton.button){
+ emit(output,EV_KEY,code,e.jbutton.state==SDL_PRESSED?1:0);
+ emit(output, EV_SYN, SYN_REPORT, 0);
+int controller_button_handle(SDL_Event e){
+ int output=get_output_device(e.caxis.which);
+ if( output == -1) return -1;
+ printf("type=0x%x btn=%d state=%d\n",e.type,e.cbutton.button,e.cbutton.state);
+ switch(e.cbutton.button){
+ case SDL_CONTROLLER_BUTTON_A:
+ case SDL_CONTROLLER_BUTTON_B:
+ case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER:
+ case SDL_CONTROLLER_BUTTON_X:
+ case SDL_CONTROLLER_BUTTON_Y:
+ case SDL_CONTROLLER_BUTTON_LEFTSHOULDER:
+ case SDL_CONTROLLER_BUTTON_START:
+ case SDL_CONTROLLER_BUTTON_BACK:
+ emit(output,EV_KEY,code,e.cbutton.state==SDL_PRESSED?1:0);
+ emit(output, EV_SYN, SYN_REPORT, 0);
int main(int argc, char* argv[])
int joysticktype=-1; // not opened
fprintf(stderr,"%s : </dev/input/eventx>\n",argv[0]);
- int output = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
- die("Cannot open the output device");
+ EvLink[ec].joystick_id=INVALID;
+ EvLink[ec].controller_id=INVALID;
+ EvLink[ec].instance_id=INVALID;
+ EvLink[ec].out_fd=INVALID;
SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER );
// get /dev/input/event[number]
// open joystick or mapped pad
- for (i = 0; i < SDL_NumJoysticks(); ++i) {
- printf("[%d] %s ",i,SDL_JoystickNameForIndex(i));
- SDL_GameController *g=SDL_GameControllerOpen(i);
- printf("is game controller.\n");
- j=SDL_GameControllerGetJoystick(g);
- // check joystick is target event
- if( strcmp(j->hwdata->fname,argv[1])==0 ){
- SDL_GameControllerClose(g);
- printf("is not game controller. treat as simple joystick.%s\n",SDL_GetError());
- // check joystick is target event
- if( strcmp(j->hwdata->fname,argv[1])==0 ){
- if( joysticktype < 0 ){
- fprintf(stderr,"could not found js on %s\n",argv[1]);
- n_event_input=atoi(j->hwdata->fname+strlen("/dev/input/event"));
// make virtual input event with "linkmeevent[4-13]"
- make_output(output,n_event_input);
+ //make_output(output,n_event_input);
// find new virtual/input and link to event4-13
sprintf(target,"linkmeevent%d",n_event_input-10);
sprintf(cmdline,"ln -s %s /dev/input/event%d",dev,n_event_input-10);
die("fatal error. linkmeevent device not found.");
while(SDL_PollEvent(&e)){
+ printf("e.type=0x%x\n",e.type);
+ case SDL_CONTROLLERAXISMOTION:
+ printf("Controller Axis motion.\n");
+ controller_asix_handle(e);
+ case SDL_CONTROLLERBUTTONDOWN:
+ case SDL_CONTROLLERBUTTONUP:
+ printf("Controller Button Up/Down\n");
+ controller_button_handle(e);
+ case SDL_CONTROLLERDEVICEADDED:
+ printf("Controller Device Added id=%d\n",e.cdevice.which);
+ case SDL_CONTROLLERDEVICEREMOVED:
+ printf("Controller Device Removed instance id=%d\n",e.cdevice.which);
+ case SDL_CONTROLLERDEVICEREMAPPED:
+ printf("Controller Device Remapped device id=%d\n",e.cdevice.which);
- handle_as_controller(output,e);
+ case SDL_JOYAXISMOTION: /**< Joystick axis motion */
+ printf("Joystick Axis motion.\n");
+ joystick_axis_handle(e);
- handle_as_joystick(output,e);
+ case SDL_JOYBALLMOTION: /**< Joystick trackball motion */
+ printf("Joystick trackball motion..\n");
+ case SDL_JOYHATMOTION: /**< Joystick hat position change */
+ printf("Joystick hat position change..\n");
+ case SDL_JOYBUTTONDOWN: /**< Joystick button pressed */
+ case SDL_JOYBUTTONUP: /**< Joystick button released */
+ printf("Joystick Button Up/Down.\n");
+ joystick_button_handle(e);
+ case SDL_JOYDEVICEADDED: /**< A new joystick has been inserted into the system */
+ printf("Joystick Device Added js index=%d.\n",e.jdevice.which);
+ joystick_add_callback(e.jdevice.which);
+ if( EvLink[ec].out_fd >= 0){
+ printf("-----------\n");
+ printf("event[%d] is linked to event[%d]\n",ec+4,ec+14);
+ printf("\t joystick_id=%d\n",EvLink[ec].joystick_id);
+ printf("\t controller_id=%d\n",EvLink[ec].controller_id);
+ printf("\t instance id=%d\n",EvLink[ec].instance_id);
+ printf("\t out_fd=%d\n",EvLink[ec].out_fd);
+ printf("-----------\n");
+ case SDL_JOYDEVICEREMOVED: /**< An opened joystick has been removed */
+ printf("Joystick Device Removed js instance id=%d.\n",e.jdevice.which);
+ joystick_remove_callback(e.jdevice.which);