/*
* LaGUI: A graphical application framework.
* Copyright (C) 2022-2023 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
#ifdef __linux__
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#endif
extern LA MAIN;
#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++;
laPropContainer*pc=la_EnsureSubTarget(LA_PROP_CONTROLLER,c);
if(!pc){ return c; }
for(laProp* p=pc->Props.pFirst;p;p=p->Item.pNext){
if(p->PropertyType & LA_PROP_INT && p->Offset){ int off=p->Offset;
if(off >= offsetof(laController,AxisValues[0]) && off <= offsetof(laController,AxisValues[LA_JS_MAX_AXES-1])){
int index = (off - offsetof(laController,AxisValues[0]))/sizeof(u16bit);
if(p->Len<2){ strSafeSet(&c->AxisNames[index], p->Name); }
else for(int i=0;iLen;i++){ strSafePrint(&c->AxisNames[index+i],"%s.%d", p->Name,i+1); }
}
}elif(p->PropertyType & LA_PROP_ENUM && p->Offset){ int off=p->Offset;
if(off >= offsetof(laController,ButtonValues[0]) && off <= offsetof(laController,ButtonValues[LA_JS_MAX_BUTTONS-1])){
int index = (off - offsetof(laController,ButtonValues[0]));
if(p->Len<2){ strSafeSet(&c->ButtonNames[index], p->Name); }
else for(int i=0;iLen;i++){ strSafePrint(&c->ButtonNames[index+i],"%s.%d", p->Name,i+1); }
}
}
}
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_ReadControllerAxisLimit(laController* c, int i, int Min, int Max){
c->AxisMins[i] = Min;
c->AxisMaxes[i] = Max;
c->AxisLimitMins[i] = -32768*0.95;
c->AxisLimitMaxes[i] = 32767*0.95;
}
int la_ControllerButtonToMap(int btn){
if(btnButtonsMap[map];
}
int la_ControllerAxisToMap(int abs){
if(absABS_MAX) return -1;
return abs;
}
int la_ControllerAxisToIndex(laController* c,int abs){
int map=la_ControllerAxisToMap(abs); if(map<0) return -1; return c->AxisMap[map];
}
char* laControllerIDGetAxisName(int id, int axis){
laController* c=la_FindControllerWithID(id); if(!c){ return "?"; }
return SSTR(c->AxisNames[axis]);
}
char* laControllerIDGetButtonName(int id, int button){
laController* c=la_FindControllerWithID(id); if(!c){ return "?"; }
return SSTR(c->ButtonNames[button]);
}
int laControllerIDGetAxis(int id, char* name){
laController* c=la_FindControllerWithID(id); if(!c){ return -1; }
for(int i=0;iAxisNames[i]),name)) return i; } return -1;
}
int laControllerIDGetButton(int id, char* name){
laController* c=la_FindControllerWithID(id); if(!c){ return -1; }
for(int i=0;iButtonNames[i]),name)) return i; } return -1;
}
void la_InitControllers(){
#ifdef __linux__
char path[32]="/dev/input/js";
char name[128]={0};
int numpos=strlen(path);
char fileName[32];
for (int i=0; i<32; ++i) {
sprintf(fileName, "/dev/input/event%d", i);
int file = open(fileName, O_RDWR | O_NONBLOCK);
if (file != -1){
ioctl(file, EVIOCGNAME(128), name);
barray_t *abs_barray = barray_init(ABS_CNT);
ioctl(file, EVIOCGBIT(EV_ABS, ABS_CNT), barray_data(abs_barray));
size_t abs_count = barray_count_set(abs_barray);
barray_t *key_barray = barray_init(KEY_CNT);
ioctl(file, EVIOCGBIT(EV_KEY, KEY_CNT), barray_data(key_barray));
size_t key_count = barray_count_set(key_barray);
if(!abs_count && !key_count){ close(file); continue; }
laController* c=la_NewController(name, fileName, file, abs_count, key_count);
int nextid=0;
for (unsigned int j=0; jButtonsMap[mapid]=nextid; nextid++;
}
}
nextid=0;
for (unsigned int j=0; jAxisMap[mapid]=nextid; nextid++;
}
}
barray_free(abs_barray);
barray_free(key_barray);
}
}
#endif
}
void la_UpdateControllerStatus(){
#ifdef __linux__
struct input_event 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(struct input_event)))>0){
if(event.type == EV_KEY){
int idx = la_ControllerButtonToIndex(c,event.code); if(idx<0) continue;
c->ButtonValues[idx]=event.value; HasEvent=1;
MAIN.LastControllerKey=idx; MAIN.LastControllerKeyDevice=c->UserAssignedID; MAIN.ControllerHasNewKey = 1; laRetriggerOperators();
}
else if(event.type == EV_ABS){
int idx = la_ControllerAxisToIndex(c,event.code); if(idx<0) continue; HasEvent=1;
c->AxisValues[idx]=rint(tnsLinearItp(-32768.0f,32767.0f,(((real)event.value-c->AxisMins[idx])/(c->AxisMaxes[idx]-c->AxisMins[idx]))));
if(idx>4)
printf("%d %d\n",event.value,idx);
if(abs(c->AxisValues[idx]-c->SaveAxisValues[idx])>10000){ c->SaveAxisValues[idx]=c->AxisValues[idx];
MAIN.LastControllerAxis=idx; MAIN.LastControllerAxisDevice=c->UserAssignedID; MAIN.ControllerHasNewAxis = 1; laRetriggerOperators();
}
}
}
if(bytes<=0){ struct stat buffer; if(stat(c->Path->Ptr,&buffer)<0){ c->Error=1; HasEvent=1; } }
}
if(HasEvent){ laNotifyUsers("la.controllers"); laMappingRequestEval(); }
#endif
}
void la_RefreshControllers(){
laController* c; while(c=lstPopItem(&MAIN.Controllers)){ la_DestroyController(c); } MAIN.NextControllerID=0;
la_InitControllers();
}
int OPINV_RefreshControllers(){
la_RefreshControllers();
#ifdef __linux__
la_ScanWacomDevices(MAIN.dpy,XIAllDevices);
#endif
#ifdef _WIN32
MAIN.WinTabOpened = 0;
if(MAIN.CurrentWindow){ la_OpenWacomWinTab(MAIN.CurrentWindow->win); }
#endif
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* id_min, char* id_max, char* id_cmin, char* id_cmax, char* desc, int i, int array_len, char* array_prefix){
laAddIntProperty(pc,id,name,desc,array_len>1?LA_WIDGET_VALUE_METER_2D:LA_WIDGET_METER_TYPE2,
array_prefix,0,32767,-32768,1,0,0,offsetof(laController, AxisValues[i]),0,0,array_len,0,0,0,0,0,0,0,LA_READ_ONLY)->ElementBytes = 2;
if(id_min) laAddIntProperty(pc,id_min,id_min,desc,0,
array_prefix,0,-25000,-32768,1,0,0,offsetof(laController, AxisLimitMins[i]),0,0,array_len,0,0,0,0,0,0,0,0)->ElementBytes = 2;
if(id_max) laAddIntProperty(pc,id_max,id_max,desc,0,
array_prefix,0,32767,25000,1,0,0,offsetof(laController, AxisLimitMaxes[i]),0,0,array_len,0,0,0,0,0,0,0,0)->ElementBytes = 2;
if(id_cmin) laAddIntProperty(pc,id_cmax,id_cmax,desc,0,
array_prefix,0,-25000,-32768,1,0,0,offsetof(laController, AxisCenterMins[i]),0,0,array_len,0,0,0,0,0,0,0,0)->ElementBytes = 2;
if(id_cmax) laAddIntProperty(pc,id_cmin,id_cmin,desc,0,
array_prefix,0,32767,25000,1,0,0,offsetof(laController, AxisCenterMaxes[i]),0,0,array_len,0,0,0,0,0,0,0,0)->ElementBytes = 2;
}
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,"user_assigned_id");
b1=laOnConditionThat(uil,c,laPropExpression(This,"error"));{
laShowLabel(uil,c,"Device error.",0,0)->Expand=1;
}laElse(uil,b1);{
laShowItem(uil,c,This,"path")->Expand=1;
}laEndCondition(uil,b1);
laEndRow(uil,b);
laShowSeparator(uil,c);
laShowLabel(uil,c,"Axes",0,0);
for(int i=0;i<8;i++){
if(!(i%2)){ b=laBeginRow(uil,c,0,0); }
sprintf(buf,"a%d",i);
laShowLabel(uil,c,buf,0,0); laShowItem(uil,c,This,buf)->Expand=1;
if(i%2){ laEndRow(uil,b); }
}
b1=laOnConditionToggle(uil,c,0,0,0,0,0); strSafeSet(&b1->ExtraInstructions,"text=More axes..."); b1->Flags|=LA_TEXT_ALIGN_LEFT;
for(int i=8;iExpand=1;
if(i%2){ laEndRow(uil,b); }
}
laEndCondition(uil,b1);
laShowSeparator(uil,c);
laShowLabel(uil,c,"Buttons",0,0);
for(int i=0;i<16;i++){
if(!(i%2)){ b=laBeginRow(uil,c,0,0); }
sprintf(buf,"b%d",i);
laShowLabel(uil,c,buf,0,0); laShowItem(uil,c,This,buf)->Expand=1;
if(i%2){ laEndRow(uil,b); }
}
b1=laOnConditionToggle(uil,c,0,0,0,0,0); strSafeSet(&b1->ExtraInstructions,"text=More buttons..."); b1->Flags|=LA_TEXT_ALIGN_LEFT;
for(int i=8;iExpand=1;
if(i%2){ laEndRow(uil,b); }
}
laEndCondition(uil,b1);
}
void laui_X56Throttle(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
laColumn* c=laFirstColumn(uil),*cl, *cr, *crl,*crr, *rc, *vc, *rcl,*rcr;
laSplitColumn(uil,c,0.17); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
laSplitColumn(uil,cr,0.4); crl=laLeftColumn(cr,0); crr=laRightColumn(cr,0);
laSplitColumn(uil,crr,0.6); rc=laLeftColumn(crr,10); vc=laRightColumn(crr,0);
laSplitColumn(uil,rc,0.4); rcl=laLeftColumn(rc,2); rcr=laRightColumn(rc,0);
laUiItem* b,*ui,*g; laUiList*gu;
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,"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;
laShowCompoundValue(ui,LA_SLOT_MARKER_1,This,"thr1_min");
laShowCompoundValue(ui,LA_SLOT_MARKER_2,This,"thr1_max");
ui=laShowItem(uil,crl,This,"thr2");ui->Expand=1;ui->Extra->HeightCoeff=10;ui->Flags|=LA_UI_FLAGS_TRANSPOSE;
laShowCompoundValue(ui,LA_SLOT_MARKER_1,This,"thr2_min");
laShowCompoundValue(ui,LA_SLOT_MARKER_2,This,"thr2_max");
laEndRow(uil,b);
b=laBeginRow(uil,crl,1,1);
laShowItem(uil,crl,This,"thr1_min")->Flags|=LA_UI_FLAGS_KNOB; laShowItem(uil,crl,This,"thr1_max")->Flags|=LA_UI_FLAGS_KNOB;
//laShowSeparator(uil,crl)->Expand=1;
laShowItem(uil,crl,This,"thr2_min")->Flags|=LA_UI_FLAGS_KNOB; laShowItem(uil,crl,This,"thr2_max")->Flags|=LA_UI_FLAGS_KNOB;
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");
}