/*
* 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++;
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;
}
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){
// Get name
ioctl(file, EVIOCGNAME(128), name);
printf("%s ",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);
for (unsigned int j=0; jItem.pNext){ if(c->Error) continue;
int bytes; while((bytes=read(c->fd, &event, sizeof(struct input_event)))>0){
if(event.type == EV_KEY){
printf("b %d %d", event.code, event.value);
if(event.code>=BTN_JOYSTICK && event.code<=BTN_THUMBR){
event.code -= BTN_JOYSTICK;
}elif(event.code>=BTN_TRIGGER_HAPPY && event.code<=BTN_TRIGGER_HAPPY40){
event.code=event.code-BTN_TRIGGER_HAPPY+BTN_THUMBR-BTN_JOYSTICK+1;
}
printf(" %d \n", event.code, event.value);
//if(event.code>=c->NumButtons+BTN_THUMBR) continue;
c->ButtonValues[event.code]=event.value; HasEvent=1;
}
else if(event.type == EV_ABS){
if(event.code>=c->NumAxes+ABS_X) continue;
int axis=event.code-ABS_X;
c->AxisValues[axis]=rint(tnsLinearItp(-32768.0f,32767.0f,((real)event.value/(c->AxisMaxes[axis]-c->AxisMins[axis]))));
HasEvent=1;
//printf("a %d %d\n", event.code, event.value);
}
}
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){
laProp* p=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);
if(id_min) p=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);
if(id_max) p=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);
if(id_min) p=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);
if(id_cmax) p=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);
}
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;
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,0,0);
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");
}