*/}}

la_controllers.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * LaGUI: A graphical application framework.
  3. * Copyright (C) 2022-2023 Wu Yiming
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "la_5.h"
  19. #include <math.h>
  20. #ifdef __linux__
  21. #include <fcntl.h>
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <sys/ioctl.h>
  25. #include <sys/inotify.h>
  26. #include <linux/input.h>
  27. #include <linux/joystick.h>
  28. #include <fcntl.h>
  29. #include <unistd.h>
  30. #include <errno.h>
  31. #include <X11/extensions/XInput2.h>
  32. #endif
  33. extern LA MAIN;
  34. #define LA_JS_EVENT_BUTTON 0x01 // button pressed/released
  35. #define LA_JS_EVENT_AXIS 0x02 // joystick moved
  36. #define LA_JS_EVENT_INIT 0x80 // initial state of device
  37. #define LA_JS_TYPE_X56_THROTTLE 1
  38. #define LA_JS_TYPE_X56_STICK 2
  39. char LA_JS_BTN_NAMES[LA_JS_MAX_BUTTONS][12];
  40. char LA_JS_BTN_MAP_NAMES[LA_JS_MAX_BUTTONS][12];
  41. char LA_JS_AXIS_NAMES[LA_JS_MAX_AXES][12];
  42. char LA_JS_AXIS_MAP_NAMES[LA_JS_MAX_AXES][12];
  43. char LA_JS_AXIS_CMAX_NAMES[LA_JS_MAX_AXES][12];
  44. char LA_JS_AXIS_CMIN_NAMES[LA_JS_MAX_AXES][12];
  45. char LA_JS_AXIS_LMAX_NAMES[LA_JS_MAX_AXES][12];
  46. char LA_JS_AXIS_LMIN_NAMES[LA_JS_MAX_AXES][12];
  47. int la_IdentifyControllerInternalType(char* name){
  48. if(strstr(name, "X-56") && strstr(name, "Throttle")){ return LA_JS_TYPE_X56_THROTTLE; }
  49. if(strstr(name, "X-56") && strstr(name, "Stick")){ return LA_JS_TYPE_X56_STICK; }
  50. return 0;
  51. }
  52. void la_EnsureControllerNames(laController* c){
  53. laPropContainer*pc=la_EnsureSubTarget(LA_PROP_CONTROLLER,c);
  54. if(!pc){ return c; }
  55. for(laProp* p=pc->Props.pFirst;p;p=p->Item.pNext){
  56. if(p->PropertyType & LA_PROP_INT && p->Offset){ int off=p->Offset;
  57. if(off >= offsetof(laController,AxisValues[0]) && off <= offsetof(laController,AxisValues[LA_JS_MAX_AXES-1])){
  58. int index = (off - offsetof(laController,AxisValues[0]))/sizeof(u16bit);
  59. if(p->Len<2){ strSafeSet(&c->AxisNames[index], p->Name); }
  60. else for(int i=0;i<p->Len;i++){ strSafePrint(&c->AxisNames[index+i],"%s.%d", p->Name,i+1); }
  61. }
  62. }elif(p->PropertyType & LA_PROP_ENUM && p->Offset){ int off=p->Offset;
  63. if(off >= offsetof(laController,ButtonValues[0]) && off <= offsetof(laController,ButtonValues[LA_JS_MAX_BUTTONS-1])){
  64. int index = (off - offsetof(laController,ButtonValues[0]));
  65. if(p->Len<2){ strSafeSet(&c->ButtonNames[index], p->Name); }
  66. else for(int i=0;i<p->Len;i++){ strSafePrint(&c->ButtonNames[index+i],"%s.%d", p->Name,i+1); }
  67. }
  68. }
  69. }
  70. }
  71. laController* la_NewController(char* name, char* path, int device, int NumAxes, int NumButtons){
  72. laController* c=memAcquire(sizeof(laController));
  73. logPrint("Found controller %s\n at %s\n with %d axes, %d buttons\n", name, path, NumAxes, NumButtons);
  74. strSafeSet(&c->Name, name); strSafeSet(&c->Path, path); c->fd=device;
  75. c->NumAxes = NumAxes; c->NumButtons=NumButtons;
  76. c->InternalType = la_IdentifyControllerInternalType(name);
  77. lstPushItem(&MAIN.Controllers,c);
  78. c->UserAssignedID=MAIN.NextControllerID; MAIN.NextControllerID++;
  79. la_EnsureControllerNames(c);
  80. return c;
  81. }
  82. void la_DestroyController(laController* c){
  83. strSafeDestroy(&c->Name); strSafeDestroy(&c->Path); if(c->fd){ close(c->fd); }
  84. lstRemoveItem(&MAIN.Controllers,c); laNotifyInstanceUsers(c); memFree(c); laNotifyUsers("la.controllers");
  85. }
  86. laController* la_FindControllerWithID(int id){
  87. for(laController* c=MAIN.Controllers.pFirst;c;c=c->Item.pNext){ if(c->UserAssignedID==id) return c; } return 0;
  88. }
  89. void la_ReadControllerAxisLimit(laController* c, int i, int Min, int Max){
  90. c->AxisMins[i] = Min;
  91. c->AxisMaxes[i] = Max;
  92. c->AxisLimitMins[i] = -32768*0.95;
  93. c->AxisLimitMaxes[i] = 32767*0.95;
  94. }
  95. int la_ControllerButtonToMap(int btn){
  96. if(btn<BTN_MISC) return -1;
  97. if(btn<=BTN_GEAR_UP) return btn-BTN_MISC;
  98. if(btn<BTN_DPAD_UP) return -1;
  99. if(btn<=BTN_DPAD_RIGHT) return BTN_GEAR_UP-BTN_MISC+1+btn-BTN_DPAD_UP;
  100. if(btn<BTN_TRIGGER_HAPPY) return -1;
  101. if(btn<=BTN_TRIGGER_HAPPY40) return BTN_GEAR_UP-BTN_MISC+1+BTN_DPAD_RIGHT-BTN_DPAD_UP+1+btn-BTN_TRIGGER_HAPPY;
  102. return -1;
  103. }
  104. int la_ControllerButtonToIndex(laController* c,int btn){
  105. int map=la_ControllerButtonToMap(btn); if(map<0) return -1; return c->ButtonsMap[map];
  106. }
  107. int la_ControllerAxisToMap(int abs){
  108. if(abs<ABS_X) return -1; if(abs>ABS_MAX) return -1;
  109. return abs;
  110. }
  111. int la_ControllerAxisToIndex(laController* c,int abs){
  112. int map=la_ControllerAxisToMap(abs); if(map<0) return -1; return c->AxisMap[map];
  113. }
  114. char* laControllerIDGetAxisName(int id, int axis){
  115. laController* c=la_FindControllerWithID(id); if(!c){ return "?"; }
  116. return SSTR(c->AxisNames[axis]);
  117. }
  118. char* laControllerIDGetButtonName(int id, int button){
  119. laController* c=la_FindControllerWithID(id); if(!c){ return "?"; }
  120. return SSTR(c->ButtonNames[button]);
  121. }
  122. int laControllerIDGetAxis(int id, char* name){
  123. laController* c=la_FindControllerWithID(id); if(!c){ return -1; }
  124. for(int i=0;i<LA_JS_MAX_AXES;i++){ if(strSame(SSTR(c->AxisNames[i]),name)) return i; } return -1;
  125. }
  126. int laControllerIDGetButton(int id, char* name){
  127. laController* c=la_FindControllerWithID(id); if(!c){ return -1; }
  128. for(int i=0;i<LA_JS_MAX_AXES;i++){ if(strSame(SSTR(c->ButtonNames[i]),name)) return i; } return -1;
  129. }
  130. void la_InitControllers(){
  131. #ifdef __linux__
  132. char path[32]="/dev/input/js";
  133. char name[128]={0};
  134. int numpos=strlen(path);
  135. char fileName[32];
  136. for (int i=0; i<32; ++i) {
  137. sprintf(fileName, "/dev/input/event%d", i);
  138. int file = open(fileName, O_RDWR | O_NONBLOCK);
  139. if (file != -1){
  140. ioctl(file, EVIOCGNAME(128), name);
  141. barray_t *abs_barray = barray_init(ABS_CNT);
  142. ioctl(file, EVIOCGBIT(EV_ABS, ABS_CNT), barray_data(abs_barray));
  143. size_t abs_count = barray_count_set(abs_barray);
  144. barray_t *key_barray = barray_init(KEY_CNT);
  145. ioctl(file, EVIOCGBIT(EV_KEY, KEY_CNT), barray_data(key_barray));
  146. size_t key_count = barray_count_set(key_barray);
  147. if(!abs_count && !key_count){ close(file); continue; }
  148. laController* c=la_NewController(name, fileName, file, abs_count, key_count);
  149. int nextid=0;
  150. for (unsigned int j=0; j<KEY_CNT; j++){
  151. if(barray_is_set(key_barray,j)){
  152. int mapid=la_ControllerButtonToMap(j); if(mapid<0){ continue; }
  153. c->ButtonsMap[mapid]=nextid; nextid++;
  154. }
  155. }
  156. nextid=0;
  157. for (unsigned int j=0; j<ABS_CNT; j++){ struct input_absinfo axisInfo;
  158. if (barray_is_set(abs_barray,j) && (ioctl(file, EVIOCGABS(j), &axisInfo) != -1)){
  159. int mapid=la_ControllerAxisToMap(j); if(mapid<0){ continue; }
  160. la_ReadControllerAxisLimit(c,nextid,axisInfo.minimum,axisInfo.maximum);
  161. c->AxisMap[mapid]=nextid; nextid++;
  162. }
  163. }
  164. barray_free(abs_barray);
  165. barray_free(key_barray);
  166. }
  167. }
  168. #endif
  169. }
  170. void la_UpdateControllerStatus(){
  171. #ifdef __linux__
  172. struct input_event event; int HasEvent=0;
  173. for(laController* c=MAIN.Controllers.pFirst;c;c=c->Item.pNext){ if(c->Error) continue;
  174. if(!c->Error && !c->fd){ c->fd=open(SSTR(c->Path), O_RDWR | O_NONBLOCK); if(c->fd<0){ c->Error=1; } continue; }
  175. int bytes; while((bytes=read(c->fd, &event, sizeof(struct input_event)))>0){
  176. if(event.type == EV_KEY){
  177. int idx = la_ControllerButtonToIndex(c,event.code); if(idx<0) continue;
  178. c->ButtonValues[idx]=event.value; HasEvent=1;
  179. MAIN.LastControllerKey=idx; MAIN.LastControllerKeyDevice=c->UserAssignedID; MAIN.ControllerHasNewKey = 1; laRetriggerOperators();
  180. }
  181. else if(event.type == EV_ABS){
  182. int idx = la_ControllerAxisToIndex(c,event.code); if(idx<0) continue; HasEvent=1;
  183. c->AxisValues[idx]=rint(tnsLinearItp(-32768.0f,32767.0f,(((real)event.value-c->AxisMins[idx])/(c->AxisMaxes[idx]-c->AxisMins[idx]))));
  184. if(abs(c->AxisValues[idx]-c->SaveAxisValues[idx])>10000){ c->SaveAxisValues[idx]=c->AxisValues[idx];
  185. MAIN.LastControllerAxis=idx; MAIN.LastControllerAxisDevice=c->UserAssignedID; MAIN.ControllerHasNewAxis = 1; laRetriggerOperators();
  186. }
  187. }
  188. }
  189. if(bytes<=0){ struct stat buffer; if(stat(c->Path->Ptr,&buffer)<0){ c->Error=1; HasEvent=1; } }
  190. }
  191. if(HasEvent){ laNotifyUsers("la.controllers"); laMappingRequestEval(); }
  192. #endif
  193. }
  194. void la_CopyControllerConfig(laController* to, laController* from){
  195. strSafeSet(&to->Path,SSTR(from->Path));
  196. to->UserAssignedID = from->UserAssignedID;
  197. memcpy(to->AxisCenterMaxes,from->AxisCenterMaxes,sizeof(int16_t)*LA_JS_MAX_AXES);
  198. memcpy(to->AxisCenterMins,from->AxisCenterMins,sizeof(int16_t)*LA_JS_MAX_AXES);
  199. memcpy(to->AxisLimitMaxes,from->AxisLimitMaxes,sizeof(int16_t)*LA_JS_MAX_AXES);
  200. memcpy(to->AxisLimitMins,from->AxisLimitMins,sizeof(int16_t)*LA_JS_MAX_AXES);
  201. memcpy(to->ButtonsMap,from->ButtonsMap,sizeof(u8bit)*LA_JS_MAX_BUTTONS);
  202. memcpy(to->AxisMap,from->AxisMap,sizeof(u8bit)*LA_JS_MAX_AXES);
  203. }
  204. void la_RemoveDuplicatedControllers(){
  205. laController* NextC;
  206. for(laController* fc=MAIN.Controllers.pFirst;fc;fc=fc->Item.pNext){
  207. if(fc->fd>=0){ close(fc->fd); fc->fd=0; }
  208. }
  209. for(laController* c=MAIN.Controllers.pFirst;c;c=c->Item.pNext){
  210. for(laController* fc=c->Item.pNext;fc;fc=NextC){
  211. NextC=fc->Item.pNext;
  212. if(strSame(SSTR(fc->Name),SSTR(c->Name))){
  213. la_CopyControllerConfig(c,fc); la_EnsureControllerNames(c);
  214. la_DestroyController(fc);
  215. }
  216. }
  217. }
  218. }
  219. void la_RefreshControllers(){
  220. la_InitControllers();
  221. la_RemoveDuplicatedControllers();
  222. }
  223. int OPINV_RefreshControllers(laOperator* a, laEvent* e){
  224. la_RefreshControllers();
  225. #ifdef __linux__
  226. la_ScanWacomDevices(MAIN.dpy,XIAllDevices);
  227. #endif
  228. #ifdef _WIN32
  229. MAIN.WinTabOpened = 0;
  230. if(MAIN.CurrentWindow){ la_OpenWacomWinTab(MAIN.CurrentWindow->win); }
  231. #endif
  232. laNotifyUsers("la.controllers");
  233. return LA_FINISHED;
  234. }
  235. int OPINV_RemoveController(laOperator* a, laEvent* e){
  236. if(!a->This || !a->This->EndInstance) return LA_FINISHED;
  237. laEnableYesNoPanel(0, 0, "Confirm?", "Will remove this controller config.", e->x-200, e->y, 200, e);
  238. return LA_RUNNING;
  239. }
  240. int OPMOD_RemoveController(laOperator* a, laEvent* e){
  241. if(!a->This || !a->This->EndInstance) return LA_FINISHED; laController* c=a->This->EndInstance;
  242. if(a->ConfirmData){
  243. if(a->ConfirmData->Mode == LA_CONFIRM_OK){
  244. la_DestroyController(c); laNotifyUsers("la.controllers");
  245. }
  246. return LA_FINISHED;
  247. }
  248. return LA_FINISHED;
  249. }
  250. void lapost_Controller(laController* c){
  251. c->InternalType = la_IdentifyControllerInternalType(SSTR(c->Name));
  252. c->fd=open(SSTR(c->Path),O_RDWR | O_NONBLOCK); if(c->fd<0){ c->Error=1; }
  253. }
  254. void* lagetraw_ControllerButtonMap(laController* c, int* r_size, int* ret_is_copy){
  255. int use_size=/*sizeof(int16_t)*LA_JS_MAX_AXES*4 +*/ sizeof(u8bit)*LA_JS_MAX_BUTTONS + sizeof(u8bit)*LA_JS_MAX_AXES;
  256. char* data=malloc(use_size); char* p=data;
  257. //memcpy(p,c->AxisCenterMaxes,sizeof(int16_t)*LA_JS_MAX_AXES); p+= sizeof(int16_t)*LA_JS_MAX_AXES;
  258. //memcpy(p,c->AxisCenterMins,sizeof(int16_t)*LA_JS_MAX_AXES); p+= sizeof(int16_t)*LA_JS_MAX_AXES;
  259. //memcpy(p,c->AxisLimitMaxes,sizeof(int16_t)*LA_JS_MAX_AXES); p+= sizeof(int16_t)*LA_JS_MAX_AXES;
  260. //memcpy(p,c->AxisLimitMins,sizeof(int16_t)*LA_JS_MAX_AXES); p+= sizeof(int16_t)*LA_JS_MAX_AXES;
  261. memcpy(p,c->ButtonsMap,sizeof(u8bit)*LA_JS_MAX_BUTTONS); p+= sizeof(u8bit)*LA_JS_MAX_BUTTONS;
  262. memcpy(p,c->AxisMap,sizeof(u8bit)*LA_JS_MAX_AXES); p+= sizeof(u8bit)*LA_JS_MAX_AXES;
  263. return data;
  264. }
  265. void lasetraw_ControllerButtonMap(laController* c, void* data, int DataSize){
  266. int use_size=/*sizeof(int16_t)*LA_JS_MAX_AXES*4 +*/ sizeof(u8bit)*LA_JS_MAX_BUTTONS + sizeof(u8bit)*LA_JS_MAX_AXES;
  267. if(DataSize!=use_size){ c->Error=1; return; /* need reinit */ }
  268. char* p=data;
  269. //memcpy(c->AxisCenterMaxes,p,sizeof(int16_t)*LA_JS_MAX_AXES); p+= sizeof(int16_t)*LA_JS_MAX_AXES;
  270. //memcpy(c->AxisCenterMins,p,sizeof(int16_t)*LA_JS_MAX_AXES); p+= sizeof(int16_t)*LA_JS_MAX_AXES;
  271. //memcpy(c->AxisLimitMaxes,p,sizeof(int16_t)*LA_JS_MAX_AXES); p+= sizeof(int16_t)*LA_JS_MAX_AXES;
  272. //memcpy(c->AxisLimitMins,p,sizeof(int16_t)*LA_JS_MAX_AXES); p+= sizeof(int16_t)*LA_JS_MAX_AXES;
  273. memcpy(c->ButtonsMap,p,sizeof(u8bit)*LA_JS_MAX_BUTTONS); p+= sizeof(u8bit)*LA_JS_MAX_BUTTONS;
  274. memcpy(c->AxisMap,p,sizeof(u8bit)*LA_JS_MAX_AXES); p+= sizeof(u8bit)*LA_JS_MAX_AXES;
  275. }
  276. void la_AddButtonProp(laPropContainer* pc, char* id, char* id_map, char* name, char* desc, int i, int array_len, char* array_prefix){
  277. laProp* p=laAddEnumProperty(pc, id, name, desc, LA_WIDGET_ENUM_HIGHLIGHT,
  278. array_prefix,0,0,0,offsetof(laController, ButtonValues[i]),0,0,array_len,0,0,0,0,0,0,LA_READ_ONLY|LA_UDF_IGNORE);
  279. laAddEnumItemAs(p,"IDLE", "Idle", "Button is not pressed", 0, 0);
  280. laAddEnumItemAs(p,"ACTIVE", "Active", "Button is pressed", 1, 0);
  281. p->ElementBytes=1;
  282. laAddIntProperty(pc,id_map,name,desc,0,
  283. array_prefix,0,0,0,1,0,0,offsetof(laController, ButtonsMap[i]),0,0,array_len,0,0,0,0,0,0,0,0)->ElementBytes = 1;
  284. }
  285. void la_AddAxisProp(laPropContainer* pc, char* id, char* name, char* id_min, char* id_max, char* id_cmin, char* id_cmax, char* id_map, char* desc, int i, int array_len, char* array_prefix){
  286. laAddIntProperty(pc,id,name,desc,array_len>1?LA_WIDGET_VALUE_METER_2D:LA_WIDGET_METER_TYPE2,
  287. 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|LA_UDF_IGNORE)->ElementBytes = 2;
  288. laAddIntProperty(pc,id_map,name,desc,0,
  289. array_prefix,0,0,0,1,0,0,offsetof(laController, AxisMap[i]),0,0,array_len,0,0,0,0,0,0,0,0)->ElementBytes = 1;
  290. if(id_min) laAddIntProperty(pc,id_min,id_min,desc,0,
  291. 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;
  292. if(id_max) laAddIntProperty(pc,id_max,id_max,desc,0,
  293. 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;
  294. if(id_cmin) laAddIntProperty(pc,id_cmax,id_cmax,desc,0,
  295. 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;
  296. if(id_cmax) laAddIntProperty(pc,id_cmin,id_cmin,desc,0,
  297. 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;
  298. }
  299. #define ADD_BUTTON_PROP(pc,id,name,desc,i,array_len,array_prefix) \
  300. la_AddButtonProp(pc,id,id "_map",name,desc,i,array_len,array_prefix)
  301. #define ADD_AXIS_PROP(pc,id,name,desc,i,array_len,array_prefix) \
  302. la_AddAxisProp(pc,id,name,id "_min",id "_max",id "_cmin",id "_cmax", id "_map",desc,i,array_len,array_prefix)
  303. void la_AddGenericButtonProps(laPropContainer* pc){
  304. for(int i=0;i<LA_JS_MAX_BUTTONS;i++){ char* name=LA_JS_BTN_NAMES[i]; la_AddButtonProp(pc,name,LA_JS_BTN_MAP_NAMES[i],name,name,i,0,0); }
  305. }
  306. void la_AddGenericAxisProps(laPropContainer* pc){
  307. for(int i=0;i<LA_JS_MAX_AXES;i++){ char* name=LA_JS_BTN_NAMES[i]; la_AddAxisProp(pc,name, name, LA_JS_AXIS_LMIN_NAMES[i], LA_JS_AXIS_LMAX_NAMES[i],
  308. LA_JS_AXIS_CMIN_NAMES[i],LA_JS_AXIS_CMAX_NAMES[i], LA_JS_AXIS_MAP_NAMES[i], name,i,0,0); }
  309. }
  310. void laui_GenericJoystick(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context);
  311. void laui_X56Throttle(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context);
  312. void laui_X56Stick(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context);
  313. laPropContainer* LA_PC_JS_GENERIC;
  314. laPropContainer* LA_PC_JS_X56_THROTTLE;
  315. laPropContainer* LA_PC_JS_X56_STICK;
  316. laPropContainer* laget_ControllerType(laController* c){
  317. switch(c->InternalType){
  318. default: case 0: return LA_PC_JS_GENERIC;
  319. case LA_JS_TYPE_X56_THROTTLE: return LA_PC_JS_X56_THROTTLE;
  320. case LA_JS_TYPE_X56_STICK: return LA_PC_JS_X56_STICK;
  321. }
  322. }
  323. void la_RegisterControllerProps(){
  324. for(int i=0;i<LA_JS_MAX_AXES;i++){ sprintf(LA_JS_AXIS_NAMES[i],"a%d",i);
  325. sprintf(LA_JS_AXIS_MAP_NAMES[i],"a%d_map",i);
  326. sprintf(LA_JS_AXIS_CMAX_NAMES[i],"a%d_cmax",i);
  327. sprintf(LA_JS_AXIS_CMIN_NAMES[i],"a%d_cmin",i);
  328. sprintf(LA_JS_AXIS_LMAX_NAMES[i],"a%d_max",i);
  329. sprintf(LA_JS_AXIS_LMIN_NAMES[i],"a%d_min",i);
  330. }
  331. for(int i=0;i<LA_JS_MAX_BUTTONS;i++){ sprintf(LA_JS_BTN_NAMES[i],"b%d",i); sprintf(LA_JS_BTN_MAP_NAMES[i],"b%d_map",i); }
  332. laCreateOperatorType("LA_refresh_controllers", "Refresh Controllers", "Look for connected controllers",0,0,0,OPINV_RefreshControllers,0,U'🗘',0);
  333. laCreateOperatorType("LA_remove_controller", "Remove Controller", "Remove controller config",0,0,0,OPINV_RemoveController,OPMOD_RemoveController,U'🞫',0);
  334. laPropContainer* pc; laProp* p;
  335. pc=laAddPropertyContainer("la_controller", "Controller", "A joystick/gamepad controller", U'🕹', laui_GenericJoystick, sizeof(laController), lapost_Controller,0,1);
  336. LA_PC_JS_GENERIC = pc;
  337. laAddStringProperty(pc,"name","Name","Name of the controller", LA_WIDGET_STRING_PLAIN,0,0,0,1,offsetof(laController,Name),0,0,0,0,LA_READ_ONLY|LA_AS_IDENTIFIER);
  338. laAddStringProperty(pc,"path","Path","Device path to the controller", LA_WIDGET_STRING_PLAIN,0,0,0,1,offsetof(laController,Path),0,0,0,0,LA_READ_ONLY);
  339. laAddIntProperty(pc,"user_assigned_id", "ID", "User assigned ID", 0,0,0,0,0,0,0,0,offsetof(laController, UserAssignedID),0,0,0,0,0,0,0,0,0,0,0);
  340. laAddIntProperty(pc,"error", "Error", "Device error", 0,0,0,0,0,0,0,0,offsetof(laController, Error),0,0,0,0,0,0,0,0,0,0,0);
  341. laAddOperatorProperty(pc,"remove","Remove","Remove this controller config","LA_remove_controller",0,0);
  342. la_AddGenericButtonProps(pc);
  343. la_AddGenericAxisProps(pc);
  344. pc=laAddPropertyContainer("la_controller_x56_throttle", "X56 Throttle", "X56 Throttle", 0,laui_X56Throttle,sizeof(laController),0,0,1);
  345. LA_PC_JS_X56_THROTTLE = pc;
  346. laAddStringProperty(pc,"name","Name","Name of the controller", LA_WIDGET_STRING_PLAIN,0,0,0,1,offsetof(laController,Name),0,0,0,0,LA_READ_ONLY|LA_AS_IDENTIFIER);
  347. laAddSubGroup(pc,"base","Base","Generic controller status", "la_controller",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  348. ADD_AXIS_PROP(pc,"thr1","THR1","Throttle 1",0,0,0);
  349. ADD_AXIS_PROP(pc,"thr2","THR2","Throttle 1",1,0,0);
  350. ADD_AXIS_PROP(pc,"wf","WF","Wheel F (big wheel)",2,0,0);
  351. ADD_AXIS_PROP(pc,"ball","Ball","Thumb ball",3,2,"Left/Right,Up/Down");
  352. ADD_AXIS_PROP(pc,"wg","WG","Wheel G (smaller wheel)",5,0,0);
  353. ADD_AXIS_PROP(pc,"r4","RTY4","Rotary 4",6,0,0);
  354. ADD_AXIS_PROP(pc,"r3","RTY3","Rotary 3",7,0,0);
  355. ADD_BUTTON_PROP(pc,"be","E","Button E (thumb flat switch)", 0,0,0);
  356. ADD_BUTTON_PROP(pc,"bf","F","Button F (big push down switch)", 1,0,0);
  357. ADD_BUTTON_PROP(pc,"bg","G","Button G (smaller up-side-down switch)", 2,0,0);
  358. ADD_BUTTON_PROP(pc,"bi","I","Button I (left reverser)", 3,0,0);
  359. ADD_BUTTON_PROP(pc,"bh","H","Button H (right reverser)", 4,0,0);
  360. ADD_BUTTON_PROP(pc,"sw1","SW1","Switch 1", 5,0,0);
  361. ADD_BUTTON_PROP(pc,"sw2","SW2","Switch 2", 6,0,0);
  362. ADD_BUTTON_PROP(pc,"sw3","SW3","Switch 3", 7,0,0);
  363. ADD_BUTTON_PROP(pc,"sw4","SW4","Switch 4", 8,0,0);
  364. ADD_BUTTON_PROP(pc,"sw5","SW5","Switch 5", 9,0,0);
  365. ADD_BUTTON_PROP(pc,"sw6","SW6","Switch 6", 10,0,0);
  366. ADD_BUTTON_PROP(pc,"t1_up","T1+","Toggle 1+", 11,0,0);
  367. ADD_BUTTON_PROP(pc,"t1_dn","T1-","Toggle 1-", 12,0,0);
  368. ADD_BUTTON_PROP(pc,"t2_up","T2+","Toggle 2+", 13,0,0);
  369. ADD_BUTTON_PROP(pc,"t2_dn","T2-","Toggle 2-", 14,0,0);
  370. ADD_BUTTON_PROP(pc,"t3_up","T3+","Toggle 3+", 15,0,0);
  371. ADD_BUTTON_PROP(pc,"t3_dn","T3-","Toggle 3-", 16,0,0);
  372. ADD_BUTTON_PROP(pc,"t4_up","T4+","Toggle 4+", 17,0,0);
  373. ADD_BUTTON_PROP(pc,"t4_dn","T4-","Toggle 4-", 18,0,0);
  374. ADD_BUTTON_PROP(pc,"h3","H3","Hat 3 (Upper round hat)", 19,4,"N,E,S,W");
  375. ADD_BUTTON_PROP(pc,"h4","H4","Hat 4 (lower jagged hat)", 23,4,"N,E,S,W");
  376. ADD_BUTTON_PROP(pc,"pinky_up","P+","Pinky up", 27,0,0);
  377. ADD_BUTTON_PROP(pc,"pinky_dn","P-","Pinky down", 28,0,0);
  378. ADD_BUTTON_PROP(pc,"dial_fwd","D+","Dial forward", 29,0,0);
  379. ADD_BUTTON_PROP(pc,"dial_back","D-","Dial backward", 30,0,0);
  380. ADD_BUTTON_PROP(pc,"bball","BP","Ball push", 31,0,0);
  381. ADD_BUTTON_PROP(pc,"slider","SLD","Slider", 32,0,0);
  382. ADD_BUTTON_PROP(pc,"mode","Mode","Mode switch", 33,3,"M1,M2,S1");
  383. pc=laAddPropertyContainer("la_controller_x56_stick", "X56 Stick", "X56 Stick", 0,laui_X56Stick,sizeof(laController),0,0,1);
  384. LA_PC_JS_X56_STICK = pc;
  385. laAddStringProperty(pc,"name","Name","Name of the controller", LA_WIDGET_STRING_PLAIN,0,0,0,1,offsetof(laController,Name),0,0,0,0,LA_READ_ONLY|LA_AS_IDENTIFIER);
  386. laAddSubGroup(pc,"base","Base","Generic controller status", "la_controller",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  387. ADD_AXIS_PROP(pc,"stick","Stick","Main stick",0,2,"Left/Right,Up/Down");
  388. ADD_AXIS_PROP(pc,"ball","Ball","Ball stick",2,2,"Left/Right,Up/Down");
  389. ADD_AXIS_PROP(pc,"rudder","Rudder","Ruder twist",4,0,0);
  390. ADD_AXIS_PROP(pc,"pov","POV","POV hat",5,2,"Left/Right,Up/Down");
  391. ADD_BUTTON_PROP(pc,"trigger","Trigger","Trigger", 0,0,0);
  392. ADD_BUTTON_PROP(pc,"ba","A","Button A", 1,0,0);
  393. ADD_BUTTON_PROP(pc,"bb","B","Button B (Side of stick)", 2,0,0);
  394. ADD_BUTTON_PROP(pc,"bball","BP","Ball push", 3,0,0); ADD_BUTTON_PROP(pc,"bc","BC","Button C (ball push)", 3,0,0);
  395. ADD_BUTTON_PROP(pc,"pinky","PK","Pinky small button", 4,0,0); ADD_BUTTON_PROP(pc,"bd","BD","Button D pinky small button", 4,0,0);
  396. ADD_BUTTON_PROP(pc,"pinkyl","PKL","Pinky lever", 5,0,0);
  397. ADD_BUTTON_PROP(pc,"h1","H1","Hat 1 (Upper round hat)", 6,4,"N,E,S,W");
  398. ADD_BUTTON_PROP(pc,"h2","H2","Hat 2 (lower jagged hat)", 10,4,"N,E,S,W");
  399. //button 14-16 not sure where it is....
  400. }
  401. void laui_GenericJoystick(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  402. laColumn* c=laFirstColumn(uil); laUiItem* b,*b1;
  403. char buf[32];
  404. laShowItem(uil,c,This,"name")->Flags|=LA_TEXT_ALIGN_CENTER;
  405. b=laBeginRow(uil,c,0,0);
  406. laShowItem(uil,c,This,"user_assigned_id");
  407. b1=laOnConditionThat(uil,c,laPropExpression(This,"error"));{
  408. laShowLabel(uil,c,"Device error.",0,0)->Expand=1;
  409. }laElse(uil,b1);{
  410. laShowItem(uil,c,This,"path")->Expand=1;
  411. laShowItem(uil,c,This,"remove");
  412. }laEndCondition(uil,b1);
  413. laEndRow(uil,b);
  414. laShowSeparator(uil,c);
  415. laShowLabel(uil,c,"Axes",0,0);
  416. for(int i=0;i<8;i++){
  417. if(!(i%2)){ b=laBeginRow(uil,c,0,0); }
  418. sprintf(buf,"a%d",i);
  419. laShowLabel(uil,c,buf,0,0); laShowItem(uil,c,This,buf)->Expand=1;
  420. if(i%2){ laEndRow(uil,b); }
  421. }
  422. b1=laOnConditionToggle(uil,c,0,0,0,0,0); strSafeSet(&b1->ExtraInstructions,"text=More axes..."); b1->Flags|=LA_TEXT_ALIGN_LEFT;
  423. for(int i=8;i<LA_JS_MAX_AXES;i++){
  424. if(!(i%2)){ b=laBeginRow(uil,c,0,0); }
  425. sprintf(buf,"a%d",i);
  426. laShowLabel(uil,c,buf,0,0); laShowItem(uil,c,This,buf)->Expand=1;
  427. if(i%2){ laEndRow(uil,b); }
  428. }
  429. laEndCondition(uil,b1);
  430. laShowSeparator(uil,c);
  431. laShowLabel(uil,c,"Buttons",0,0);
  432. for(int i=0;i<16;i++){
  433. if(!(i%2)){ b=laBeginRow(uil,c,0,0); }
  434. sprintf(buf,"b%d",i);
  435. laShowLabel(uil,c,buf,0,0); laShowItem(uil,c,This,buf)->Expand=1;
  436. if(i%2){ laEndRow(uil,b); }
  437. }
  438. b1=laOnConditionToggle(uil,c,0,0,0,0,0); strSafeSet(&b1->ExtraInstructions,"text=More buttons..."); b1->Flags|=LA_TEXT_ALIGN_LEFT;
  439. for(int i=8;i<LA_JS_MAX_BUTTONS;i++){
  440. if(!(i%2)){ b=laBeginRow(uil,c,0,0); }
  441. sprintf(buf,"b%d",i);
  442. laShowLabel(uil,c,buf,0,0); laShowItem(uil,c,This,buf)->Expand=1;
  443. if(i%2){ laEndRow(uil,b); }
  444. }
  445. laEndCondition(uil,b1);
  446. }
  447. void laui_X56Throttle(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  448. laColumn* c=laFirstColumn(uil),*cl, *cr, *crl,*crr, *rc, *vc, *rcl,*rcr;
  449. laSplitColumn(uil,c,0.17); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
  450. laSplitColumn(uil,cr,0.4); crl=laLeftColumn(cr,0); crr=laRightColumn(cr,0);
  451. laSplitColumn(uil,crr,0.6); rc=laLeftColumn(crr,10); vc=laRightColumn(crr,0);
  452. laSplitColumn(uil,rc,0.4); rcl=laLeftColumn(rc,2); rcr=laRightColumn(rc,0);
  453. laUiItem* b,*ui,*g; laUiList*gu;
  454. laUiItem* JB=laShowInvisibleItem(uil,c,This,"base");
  455. laShowItem(uil,c,&JB->PP, "name")->Flags|=LA_TEXT_ALIGN_CENTER;
  456. b=laBeginRow(uil,c,0,0);
  457. laShowItem(uil,c,&JB->PP, "user_assigned_id");
  458. laUiItem* b1=laOnConditionThat(uil,c,laPropExpression(&JB->PP, "error"));{
  459. laShowLabel(uil,c,"Device error.",0,0)->Expand=1;
  460. }laElse(uil,b1);{
  461. laShowItem(uil,c,&JB->PP, "path")->Expand=1;
  462. laShowItem(uil,c,&JB->PP, "remove");
  463. }laEndCondition(uil,b1);
  464. laEndRow(uil,b);
  465. laShowSeparator(uil,c);
  466. laShowItem(uil,cl,This,"pinky_up");
  467. laShowItem(uil,cl,This,"pinky_dn");
  468. laShowSeparator(uil,cl);
  469. laShowItem(uil,cl,This,"dial_fwd");
  470. laShowItem(uil,cl,This,"dial_back");
  471. b=laBeginRow(uil,crl,0,0);
  472. ui=laShowItem(uil,crl,This,"bi");ui->Expand=1;
  473. ui=laShowItem(uil,crl,This,"bh");ui->Expand=1;
  474. laEndRow(uil,b);
  475. b=laBeginRow(uil,crl,0,0);
  476. ui=laShowItem(uil,crl,This,"thr1");ui->Expand=1;ui->Extra->HeightCoeff=10;ui->Flags|=LA_UI_FLAGS_TRANSPOSE;
  477. laShowCompoundValue(ui,LA_SLOT_MARKER_1,This,"thr1_min");
  478. laShowCompoundValue(ui,LA_SLOT_MARKER_2,This,"thr1_max");
  479. ui=laShowItem(uil,crl,This,"thr2");ui->Expand=1;ui->Extra->HeightCoeff=10;ui->Flags|=LA_UI_FLAGS_TRANSPOSE;
  480. laShowCompoundValue(ui,LA_SLOT_MARKER_1,This,"thr2_min");
  481. laShowCompoundValue(ui,LA_SLOT_MARKER_2,This,"thr2_max");
  482. laEndRow(uil,b);
  483. b=laBeginRow(uil,crl,1,1);
  484. laShowItem(uil,crl,This,"thr1_min")->Flags|=LA_UI_FLAGS_KNOB; laShowItem(uil,crl,This,"thr1_max")->Flags|=LA_UI_FLAGS_KNOB;
  485. //laShowSeparator(uil,crl)->Expand=1;
  486. laShowItem(uil,crl,This,"thr2_min")->Flags|=LA_UI_FLAGS_KNOB; laShowItem(uil,crl,This,"thr2_max")->Flags|=LA_UI_FLAGS_KNOB;
  487. laEndRow(uil,b);
  488. laShowItem(uil,rcl,This,"bf");
  489. laShowItem(uil,rcr,This,"wf");
  490. laShowItem(uil,rcl,This,"bg");
  491. laShowItem(uil,rcr,This,"wg");
  492. laShowSeparator(uil,cl);
  493. laShowItem(uil,rcl,This,"slider");
  494. laShowSeparator(uil,rcl);
  495. laShowItem(uil,rcl,This,"be");
  496. laShowItem(uil,rcl,This,"bball");
  497. laShowItem(uil,rcr,This,"ball");
  498. laShowItem(uil,rc,This,"h3")->Flags|=LA_UI_FLAGS_TRANSPOSE;
  499. laShowItem(uil,rc,This,"h4")->Flags|=LA_UI_FLAGS_TRANSPOSE;
  500. laShowItem(uil,vc,This,"t4_up");
  501. laShowItem(uil,vc,This,"t4_dn"); laShowSeparator(uil,vc);
  502. laShowItem(uil,vc,This,"t3_up");
  503. laShowItem(uil,vc,This,"t3_dn"); laShowSeparator(uil,vc);
  504. laShowItem(uil,vc,This,"t2_up");
  505. laShowItem(uil,vc,This,"t2_dn"); laShowSeparator(uil,vc);
  506. laShowItem(uil,vc,This,"t1_up");
  507. laShowItem(uil,vc,This,"t1_dn"); laShowSeparator(uil,vc);
  508. laShowItem(uil,vc,This,"r3");
  509. laShowItem(uil,vc,This,"r4");
  510. laShowSeparator(uil,c);
  511. laShowLabel(uil,cl,"Mode",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  512. laShowItem(uil,cl,This,"mode");
  513. laShowLabel(uil,cr,"Switches",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  514. b=laBeginRow(uil,cr,0,0);
  515. laShowItem(uil,cr,This,"sw1")->Expand=1;
  516. laShowItem(uil,cr,This,"sw3")->Expand=1;
  517. laShowItem(uil,cr,This,"sw5")->Expand=1;
  518. laEndRow(uil,b);
  519. b=laBeginRow(uil,cr,0,0);
  520. laShowItem(uil,cr,This,"sw2")->Expand=1;
  521. laShowItem(uil,cr,This,"sw4")->Expand=1;
  522. laShowItem(uil,cr,This,"sw6")->Expand=1;
  523. laEndRow(uil,b);
  524. }
  525. void laui_X56Stick(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  526. laColumn* c=laFirstColumn(uil),*cl, *cc, *cr;
  527. laSplitColumn(uil,c,0.2); cl=laLeftColumn(c,10); cr=laRightColumn(c,0);
  528. laSplitColumn(uil,cr,0.8); cc=laLeftColumn(cr,0); cr=laRightColumn(cr,10);
  529. laUiItem* b;
  530. laUiItem* JB=laShowInvisibleItem(uil,c,This,"base");
  531. laShowItem(uil,c,&JB->PP, "name")->Flags|=LA_TEXT_ALIGN_CENTER;
  532. b=laBeginRow(uil,c,0,0);
  533. laShowItem(uil,c,&JB->PP, "user_assigned_id");
  534. laUiItem* b1=laOnConditionThat(uil,c,laPropExpression(&JB->PP, "error"));{
  535. laShowLabel(uil,c,"Device error.",0,0)->Expand=1;
  536. }laElse(uil,b1);{
  537. laShowItem(uil,c,&JB->PP, "path")->Expand=1;
  538. laShowItem(uil,c,&JB->PP, "remove");
  539. }laEndCondition(uil,b1);
  540. laEndRow(uil,b);
  541. laShowSeparator(uil,c);
  542. laShowItem(uil,cl,This,"ba");
  543. laShowItem(uil,cl,This,"pov");
  544. laShowSeparator(uil,cl);
  545. laShowLabel(uil,cl,"Thumb",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  546. laShowItem(uil,cl,This,"bc");
  547. laShowItem(uil,cl,This,"ball");
  548. laShowSeparator(uil,cl);
  549. laShowLabel(uil,cl,"Pinky",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  550. laShowItem(uil,cl,This,"pinkyl");
  551. laShowItem(uil,cl,This,"pinky");
  552. laShowItem(uil,cr,This,"bb");
  553. laShowSeparator(uil,cr);
  554. laShowItem(uil,cc,This,"trigger");
  555. laShowItem(uil,cc,This,"stick");
  556. laShowItem(uil,cc,This,"rudder");
  557. laShowLabel(uil,cr,"H1",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  558. laShowItem(uil,cr,This,"h1");
  559. laShowSeparator(uil,cr);
  560. laShowLabel(uil,cr,"H2",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  561. laShowItem(uil,cr,This,"h2");
  562. }