/*
* LaGUI: A graphical application framework.
* Copyright (C) 2022 Wu Yiming
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "la_5.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
extern LA MAIN;
STRUCTURE(laJoystickEvent){
unsigned int time;
short value;
unsigned char type;
unsigned char number;
};
#define LA_JS_EVENT_BUTTON 0x01 // button pressed/released
#define LA_JS_EVENT_AXIS 0x02 // joystick moved
#define LA_JS_EVENT_INIT 0x80 // initial state of device
#define LA_JS_TYPE_X56_THROTTLE 1
#define LA_JS_TYPE_X56_STICK 2
char LA_JS_BTN_NAMES[LA_JS_MAX_BUTTONS][10];
char LA_JS_AXIS_NAMES[LA_JS_MAX_AXES][10];
int la_IdentifyControllerInternalType(char* name){
if(strstr(name, "X-56") && strstr(name, "Throttle")){ return LA_JS_TYPE_X56_THROTTLE; }
if(strstr(name, "X-56") && strstr(name, "Stick")){ return LA_JS_TYPE_X56_STICK; }
return 0;
}
laController* la_NewController(char* name, char* path, int device, int NumAxes, int NumButtons){
laController* c=memAcquire(sizeof(laController));
logPrint("Found controller %s\n at %s\n with %d axes, %d buttons\n", name, path, NumAxes, NumButtons);
strSafeSet(&c->Name, name); strSafeSet(&c->Path, path); c->fd=device;
c->NumAxes = NumAxes; c->NumButtons=NumButtons;
c->InternalType = la_IdentifyControllerInternalType(name);
lstAppendItem(&MAIN.Controllers,c);
c->UserAssignedID=MAIN.NextControllerID; MAIN.NextControllerID++;
return c;
}
void la_DestroyController(laController* c){
strSafeDestroy(&c->Name); strSafeDestroy(&c->Path);
memFree(c); laNotifyUsers("la.controllers");
}
laController* la_FindControllerWithID(int id){
for(laController* c=MAIN.Controllers.pFirst;c;c=c->Item.pNext){ if(c->UserAssignedID==id) return c; } return 0;
}
void la_InitControllers(){
char path[32]="/dev/input/js";
char name[128]={0};
int numpos=strlen(path);
for(int i=0;i<16;i++){
int fd;
int version; uint8_t axes, buttons;
int btnmapok = 1;
sprintf(&path[numpos],"%d",i);
if ((fd=open(path, O_RDONLY|O_NONBLOCK))<0) { continue; }
ioctl(fd, JSIOCGVERSION, &version);
ioctl(fd, JSIOCGAXES, &axes);
ioctl(fd, JSIOCGBUTTONS, &buttons);
ioctl(fd, JSIOCGNAME(128), name);
laController* c=la_NewController(name, path, fd, axes, buttons);
}
}
void la_UpdateControllerStatus(){
laJoystickEvent event; int HasEvent=0;
for(laController* c=MAIN.Controllers.pFirst;c;c=c->Item.pNext){ if(c->Error) continue;
int bytes; while((bytes=read(c->fd, &event, sizeof(laJoystickEvent)))>0){
if(event.type&LA_JS_EVENT_BUTTON){
if(event.number>=c->NumButtons) continue;
c->ButtonValues[event.number]=event.value; HasEvent=1;
printf("b %d %d\n", event.number, event.value);
}
if(event.type&LA_JS_EVENT_AXIS){
if(event.number>=c->NumAxes) continue;
c->AxisValues[event.number]=event.value;
printf("a %d %d\n", event.number, event.value); HasEvent=1;
}
}
if(bytes<=0){ struct stat buffer; if(stat(c->Path->Ptr,&buffer)<0){ c->Error=1; HasEvent=1; } }
}
if(HasEvent){ laNotifyUsers("la.controllers"); laMappingRequestEval(); }
}
void la_RefreshControllers(){
laController* c; while(c=lstPopItem(&MAIN.Controllers)){ la_DestroyController(c); } MAIN.NextControllerID=0;
la_InitControllers();
}
int OPINV_RefreshControllers(){
la_RefreshControllers();
la_ScanWacomDevices(MAIN.dpy,XIAllDevices);
return LA_FINISHED;
}
void la_AddButtonProp(laPropContainer* pc, char* id, char* name, char* desc, int i, int array_len, char* array_prefix){
laProp* p=laAddEnumProperty(pc, id, name, desc, LA_WIDGET_ENUM_HIGHLIGHT,
array_prefix,0,0,0,offsetof(laController, ButtonValues[i]),0,0,array_len,0,0,0,0,0,0,LA_READ_ONLY);
laAddEnumItemAs(p,"IDLE", "Idle", "Button is not pressed", 0, 0);
laAddEnumItemAs(p,"ACTIVE", "Active", "Button is pressed", 1, 0);
p->ElementBytes=1;
}
void la_AddAxisProp(laPropContainer* pc, char* id, char* name, char* desc, int i, int array_len, char* array_prefix){
laProp* p=laAddIntProperty(pc,id,name,desc,array_len>1?LA_WIDGET_VALUE_METER_2D:LA_WIDGET_VALUE_METER,
array_prefix,0,32768,-32767,1,0,0,offsetof(laController, AxisValues[i]),0,0,array_len,0,0,0,0,0,0,0,LA_READ_ONLY);
}
void la_AddGenericButtonProps(laPropContainer* pc){
for(int i=0;iInternalType){
default: case 0: return LA_PC_JS_GENERIC;
case LA_JS_TYPE_X56_THROTTLE: return LA_PC_JS_X56_THROTTLE;
case LA_JS_TYPE_X56_STICK: return LA_PC_JS_X56_STICK;
}
}
void la_RegisterControllerProps(){
for(int i=0;iFlags|=LA_TEXT_ALIGN_CENTER;
b=laBeginRow(uil,c,0,0);
laShowItem(uil,c,This,"base.user_assigned_id");
laUiItem* b1=laOnConditionThat(uil,c,laPropExpression(This,"base.error"));{
laShowLabel(uil,c,"Device error.",0,0)->Expand=1;
}laElse(uil,b1);{
laShowItem(uil,c,This,"base.path")->Expand=1;
}laEndCondition(uil,b1);
laEndRow(uil,b);
laShowSeparator(uil,c);
laShowItem(uil,cl,This,"pinky_up");
laShowItem(uil,cl,This,"pinky_dn");
laShowSeparator(uil,cl);
laShowItem(uil,cl,This,"dial_fwd");
laShowItem(uil,cl,This,"dial_back");
b=laBeginRow(uil,crl,0,0);
ui=laShowItem(uil,crl,This,"bi");ui->Expand=1;
ui=laShowItem(uil,crl,This,"bh");ui->Expand=1;
laEndRow(uil,b);
b=laBeginRow(uil,crl,0,0);
ui=laShowItem(uil,crl,This,"thr1");ui->Expand=1;ui->Extra->HeightCoeff=10;ui->Flags|=LA_UI_FLAGS_TRANSPOSE;
ui=laShowItem(uil,crl,This,"thr2");ui->Expand=1;ui->Extra->HeightCoeff=10;ui->Flags|=LA_UI_FLAGS_TRANSPOSE;
laEndRow(uil,b);
laShowItem(uil,rcl,This,"bf");
laShowItem(uil,rcr,This,"wf");
laShowItem(uil,rcl,This,"bg");
laShowItem(uil,rcr,This,"wg");
laShowSeparator(uil,cl);
laShowItem(uil,rcl,This,"slider");
laShowSeparator(uil,rcl);
laShowItem(uil,rcl,This,"be");
laShowItem(uil,rcl,This,"bball");
laShowItem(uil,rcr,This,"ball");
laShowItem(uil,rc,This,"h3")->Flags|=LA_UI_FLAGS_TRANSPOSE;
laShowItem(uil,rc,This,"h4")->Flags|=LA_UI_FLAGS_TRANSPOSE;
laShowItem(uil,vc,This,"t4_up");
laShowItem(uil,vc,This,"t4_dn"); laShowSeparator(uil,vc);
laShowItem(uil,vc,This,"t3_up");
laShowItem(uil,vc,This,"t3_dn"); laShowSeparator(uil,vc);
laShowItem(uil,vc,This,"t2_up");
laShowItem(uil,vc,This,"t2_dn"); laShowSeparator(uil,vc);
laShowItem(uil,vc,This,"t1_up");
laShowItem(uil,vc,This,"t1_dn"); laShowSeparator(uil,vc);
laShowItem(uil,vc,This,"r3");
laShowItem(uil,vc,This,"r4");
laShowSeparator(uil,c);
laShowLabel(uil,cl,"Mode",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
laShowItem(uil,cl,This,"mode");
laShowLabel(uil,cr,"Switches",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
b=laBeginRow(uil,cr,0,0);
laShowItem(uil,cr,This,"sw1")->Expand=1;
laShowItem(uil,cr,This,"sw3")->Expand=1;
laShowItem(uil,cr,This,"sw5")->Expand=1;
laEndRow(uil,b);
b=laBeginRow(uil,cr,0,0);
laShowItem(uil,cr,This,"sw2")->Expand=1;
laShowItem(uil,cr,This,"sw4")->Expand=1;
laShowItem(uil,cr,This,"sw6")->Expand=1;
laEndRow(uil,b);
}
void laui_X56Stick(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
laColumn* c=laFirstColumn(uil),*cl, *cc, *cr;
laSplitColumn(uil,c,0.2); cl=laLeftColumn(c,10); cr=laRightColumn(c,0);
laSplitColumn(uil,cr,0.8); cc=laLeftColumn(cr,0); cr=laRightColumn(cr,10);
laUiItem* b;
laShowItem(uil,c,This,"base.name")->Flags|=LA_TEXT_ALIGN_CENTER;
b=laBeginRow(uil,c,0,0);
laShowItem(uil,c,This,"base.user_assigned_id");
laUiItem* b1=laOnConditionThat(uil,c,laPropExpression(This,"base.error"));{
laShowLabel(uil,c,"Device error.",0,0)->Expand=1;
}laElse(uil,b1);{
laShowItem(uil,c,This,"base.path")->Expand=1;
}laEndCondition(uil,b1);
laEndRow(uil,b);
laShowSeparator(uil,c);
laShowItem(uil,cl,This,"ba");
laShowItem(uil,cl,This,"pov");
laShowSeparator(uil,cl);
laShowLabel(uil,cl,"Thumb",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
laShowItem(uil,cl,This,"bc");
laShowItem(uil,cl,This,"ball");
laShowSeparator(uil,cl);
laShowLabel(uil,cl,"Pinky",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
laShowItem(uil,cl,This,"pinkyl");
laShowItem(uil,cl,This,"pinky");
laShowItem(uil,cr,This,"bb");
laShowSeparator(uil,cr);
laShowItem(uil,cc,This,"trigger");
laShowItem(uil,cc,This,"stick");
laShowItem(uil,cc,This,"rudder");
laShowLabel(uil,cr,"H1",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
laShowItem(uil,cr,This,"h1");
laShowSeparator(uil,cr);
laShowLabel(uil,cr,"H2",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
laShowItem(uil,cr,This,"h2");
}