*/}}

la_controllers.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. #include "la_5.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <fcntl.h>
  6. #include <sys/ioctl.h>
  7. #include <linux/input.h>
  8. #include <linux/joystick.h>
  9. #include <errno.h>
  10. extern LA MAIN;
  11. STRUCTURE(laJoystickEvent){
  12. unsigned int time;
  13. short value;
  14. unsigned char type;
  15. unsigned char number;
  16. };
  17. #define LA_JS_EVENT_BUTTON 0x01 // button pressed/released
  18. #define LA_JS_EVENT_AXIS 0x02 // joystick moved
  19. #define LA_JS_EVENT_INIT 0x80 // initial state of device
  20. #define LA_JS_TYPE_X56_THROTTLE 1
  21. #define LA_JS_TYPE_X56_STICK 2
  22. char LA_JS_BTN_NAMES[LA_JS_MAX_BUTTONS][10];
  23. char LA_JS_AXIS_NAMES[LA_JS_MAX_AXES][10];
  24. int la_IdentifyControllerInternalType(char* name){
  25. if(strstr(name, "X-56") && strstr(name, "Throttle")){ return LA_JS_TYPE_X56_THROTTLE; }
  26. if(strstr(name, "X-56") && strstr(name, "Stick")){ return LA_JS_TYPE_X56_STICK; }
  27. return 0;
  28. }
  29. laController* la_NewController(char* name, char* path, int device, int NumAxes, int NumButtons){
  30. laController* c=memAcquire(sizeof(laController));
  31. logPrint("Found controller %s\n at %s\n with %d axes, %d buttons\n", name, path, NumAxes, NumButtons);
  32. strSafeSet(&c->Name, name); strSafeSet(&c->Path, path); c->fd=device;
  33. c->NumAxes = NumAxes; c->NumButtons=NumButtons;
  34. c->InternalType = la_IdentifyControllerInternalType(name);
  35. lstAppendItem(&MAIN.Controllers,c);
  36. c->UserAssignedID=MAIN.NextControllerID; MAIN.NextControllerID++;
  37. return c;
  38. }
  39. void la_DestroyController(laController* c){
  40. strSafeDestroy(&c->Name); strSafeDestroy(&c->Path);
  41. memFree(c); laNotifyUsers("la.controllers");
  42. }
  43. laController* la_FindControllerWithID(int id){
  44. for(laController* c=MAIN.Controllers.pFirst;c;c=c->Item.pNext){ if(c->UserAssignedID==id) return c; } return 0;
  45. }
  46. void la_InitControllers(){
  47. char path[32]="/dev/input/js";
  48. char name[128]={0};
  49. int numpos=strlen(path);
  50. for(int i=0;i<16;i++){
  51. int fd;
  52. int version; uint8_t axes, buttons;
  53. int btnmapok = 1;
  54. sprintf(&path[numpos],"%d",i);
  55. if ((fd=open(path, O_RDONLY|O_NONBLOCK))<0) { continue; }
  56. ioctl(fd, JSIOCGVERSION, &version);
  57. ioctl(fd, JSIOCGAXES, &axes);
  58. ioctl(fd, JSIOCGBUTTONS, &buttons);
  59. ioctl(fd, JSIOCGNAME(128), name);
  60. laController* c=la_NewController(name, path, fd, axes, buttons);
  61. }
  62. }
  63. void la_UpdateControllerStatus(){
  64. laJoystickEvent event; int HasEvent=0;
  65. for(laController* c=MAIN.Controllers.pFirst;c;c=c->Item.pNext){ if(c->Error) continue;
  66. int bytes; while((bytes=read(c->fd, &event, sizeof(laJoystickEvent)))>0){
  67. if(event.type&LA_JS_EVENT_BUTTON){
  68. if(event.number>=c->NumButtons) continue;
  69. c->ButtonValues[event.number]=event.value; HasEvent=1;
  70. printf("b %d %d\n", event.number, event.value);
  71. }
  72. if(event.type&LA_JS_EVENT_AXIS){
  73. if(event.number>=c->NumAxes) continue;
  74. c->AxisValues[event.number]=event.value;
  75. printf("a %d %d\n", event.number, event.value); HasEvent=1;
  76. }
  77. }
  78. if(bytes<=0){ struct stat buffer; if(stat(c->Path->Ptr,&buffer)<0){ c->Error=1; HasEvent=1; } }
  79. }
  80. if(HasEvent){ laNotifyUsers("la.controllers"); laMappingRequestEval(); }
  81. }
  82. void la_RefreshControllers(){
  83. laController* c; while(c=lstPopItem(&MAIN.Controllers)){ la_DestroyController(c); } MAIN.NextControllerID=0;
  84. la_InitControllers();
  85. }
  86. int OPINV_RefreshControllers(){
  87. la_RefreshControllers(); return LA_FINISHED;
  88. }
  89. void la_AddButtonProp(laPropContainer* pc, char* id, char* name, char* desc, int i, int array_len, char* array_prefix){
  90. laProp* p=laAddEnumProperty(pc, id, name, desc, LA_WIDGET_ENUM_HIGHLIGHT,
  91. array_prefix,0,0,0,offsetof(laController, ButtonValues[i]),0,0,array_len,0,0,0,0,0,0,LA_READ_ONLY);
  92. laAddEnumItemAs(p,"IDLE", "Idle", "Button is not pressed", 0, 0);
  93. laAddEnumItemAs(p,"ACTIVE", "Active", "Button is pressed", 1, 0);
  94. p->ElementBytes=1;
  95. }
  96. void la_AddAxisProp(laPropContainer* pc, char* id, char* name, char* desc, int i, int array_len, char* array_prefix){
  97. laProp* p=laAddIntProperty(pc,id,name,desc,array_len>1?LA_WIDGET_VALUE_METER_2D:LA_WIDGET_VALUE_METER,
  98. 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);
  99. }
  100. void la_AddGenericButtonProps(laPropContainer* pc){
  101. for(int i=0;i<LA_JS_MAX_BUTTONS;i++){ char* name=LA_JS_BTN_NAMES[i]; la_AddButtonProp(pc,name,name,name,i,0,0); }
  102. }
  103. void la_AddGenericAxisProps(laPropContainer* pc){
  104. for(int i=0;i<LA_JS_MAX_AXES;i++){ char* name=LA_JS_AXIS_NAMES[i]; la_AddAxisProp(pc,name,name,name,i,0,0); }
  105. }
  106. void laui_X56Throttle(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context);
  107. void laui_X56Stick(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context);
  108. laPropContainer* LA_PC_JS_GENERIC;
  109. laPropContainer* LA_PC_JS_X56_THROTTLE;
  110. laPropContainer* LA_PC_JS_X56_STICK;
  111. laPropContainer* laget_ControllerType(laController* c){
  112. switch(c->InternalType){
  113. default: case 0: return LA_PC_JS_GENERIC;
  114. case LA_JS_TYPE_X56_THROTTLE: return LA_PC_JS_X56_THROTTLE;
  115. case LA_JS_TYPE_X56_STICK: return LA_PC_JS_X56_STICK;
  116. }
  117. }
  118. void la_RegisterControllerProps(){
  119. for(int i=0;i<LA_JS_MAX_AXES;i++){ sprintf(LA_JS_AXIS_NAMES[i],"a%d",i); }
  120. for(int i=0;i<LA_JS_MAX_BUTTONS;i++){ sprintf(LA_JS_BTN_NAMES[i],"b%d",i); }
  121. laCreateOperatorType("LA_refresh_controllers", "Refresh Controllers", "Look for connected controllers",0,0,0,OPINV_RefreshControllers,0,L'🗘',0);
  122. laPropContainer* pc; laProp* p;
  123. pc=laAddPropertyContainer("la_controller", "Controller", "A joystick/gamepad controller", L'🕹', 0, sizeof(laController), 0,0,1);
  124. LA_PC_JS_GENERIC = pc;
  125. 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);
  126. 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);
  127. 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,LA_AS_IDENTIFIER);
  128. 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);
  129. la_AddGenericButtonProps(pc);
  130. la_AddGenericAxisProps(pc);
  131. pc=laAddPropertyContainer("la_controller_x56_throttle", "X56 Throttle", "X56 Throttle", 0,laui_X56Throttle,sizeof(laController),0,0,1);
  132. LA_PC_JS_X56_THROTTLE = pc;
  133. 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);
  134. laAddSubGroup(pc,"base","Base","Generic controller status", "la_controller",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  135. la_AddAxisProp(pc,"thr1","THR1","Throttle 1",0,0,0);
  136. la_AddAxisProp(pc,"thr2","THR2","Throttle 1",1,0,0);
  137. la_AddAxisProp(pc,"wf","WF","Wheel F (big wheel)",2,0,0);
  138. la_AddAxisProp(pc,"ball","Ball","Thumb ball",3,2,"Left/Right,Up/Down");
  139. la_AddAxisProp(pc,"wg","WG","Wheel G (smaller wheel)",5,0,0);
  140. la_AddAxisProp(pc,"r4","RTY4","Rotary 4",6,0,0);
  141. la_AddAxisProp(pc,"r3","RTY3","Rotary 3",7,0,0);
  142. la_AddButtonProp(pc,"be","E","Button E (thumb flat switch)", 0,0,0);
  143. la_AddButtonProp(pc,"bf","F","Button F (big push down switch)", 1,0,0);
  144. la_AddButtonProp(pc,"bg","G","Button G (smaller up-side-down switch)", 2,0,0);
  145. la_AddButtonProp(pc,"bi","I","Button I (left reverser)", 3,0,0);
  146. la_AddButtonProp(pc,"bh","H","Button H (right reverser)", 4,0,0);
  147. la_AddButtonProp(pc,"sw1","SW1","Switch 1", 5,0,0);
  148. la_AddButtonProp(pc,"sw2","SW2","Switch 2", 6,0,0);
  149. la_AddButtonProp(pc,"sw3","SW3","Switch 3", 7,0,0);
  150. la_AddButtonProp(pc,"sw4","SW4","Switch 4", 8,0,0);
  151. la_AddButtonProp(pc,"sw5","SW5","Switch 5", 9,0,0);
  152. la_AddButtonProp(pc,"sw6","SW6","Switch 6", 10,0,0);
  153. la_AddButtonProp(pc,"t1_up","T1+","Toggle 1+", 11,0,0);
  154. la_AddButtonProp(pc,"t1_dn","T1-","Toggle 1-", 12,0,0);
  155. la_AddButtonProp(pc,"t2_up","T2+","Toggle 2+", 13,0,0);
  156. la_AddButtonProp(pc,"t2_dn","T2-","Toggle 2-", 14,0,0);
  157. la_AddButtonProp(pc,"t3_up","T3+","Toggle 3+", 15,0,0);
  158. la_AddButtonProp(pc,"t3_dn","T3-","Toggle 3-", 16,0,0);
  159. la_AddButtonProp(pc,"t4_up","T4+","Toggle 4+", 17,0,0);
  160. la_AddButtonProp(pc,"t4_dn","T4-","Toggle 4-", 18,0,0);
  161. la_AddButtonProp(pc,"h3","H3","Hat 3 (Upper round hat)", 19,4,"N,E,S,W");
  162. la_AddButtonProp(pc,"h4","H4","Hat 4 (lower jagged hat)", 23,4,"N,E,S,W");
  163. la_AddButtonProp(pc,"pinky_up","P+","Pinky up", 27,0,0);
  164. la_AddButtonProp(pc,"pinky_dn","P-","Pinky down", 28,0,0);
  165. la_AddButtonProp(pc,"dial_fwd","D+","Dial forward", 29,0,0);
  166. la_AddButtonProp(pc,"dial_back","D-","Dial backward", 30,0,0);
  167. la_AddButtonProp(pc,"bball","BP","Ball push", 31,0,0);
  168. la_AddButtonProp(pc,"slider","SLD","Slider", 32,0,0);
  169. la_AddButtonProp(pc,"mode","Mode","Mode switch", 33,3,"M1,M2,S1");
  170. pc=laAddPropertyContainer("la_controller_x56_stick", "X56 Stick", "X56 Stick", 0,laui_X56Stick,sizeof(laController),0,0,1);
  171. LA_PC_JS_X56_STICK = pc;
  172. 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);
  173. laAddSubGroup(pc,"base","Base","Generic controller status", "la_controller",0,0,0,0,0,0,0,0,0,0,0,LA_UDF_LOCAL);
  174. la_AddAxisProp(pc,"stick","Stick","Main stick",0,2,"Left/Right,Up/Down");
  175. la_AddAxisProp(pc,"ball","Ball","Ball stick",2,2,"Left/Right,Up/Down");
  176. la_AddAxisProp(pc,"rudder","Rudder","Ruder twist",4,0,0);
  177. la_AddAxisProp(pc,"pov","POV","POV hat",5,2,"Left/Right,Up/Down");
  178. la_AddButtonProp(pc,"trigger","Trigger","Trigger", 0,0,0);
  179. la_AddButtonProp(pc,"ba","A","Button A", 1,0,0);
  180. la_AddButtonProp(pc,"bb","B","Button B (Side of stick)", 2,0,0);
  181. la_AddButtonProp(pc,"bball","BP","Ball push", 3,0,0); la_AddButtonProp(pc,"bc","BC","Button C (ball push)", 3,0,0);
  182. la_AddButtonProp(pc,"pinky","PK","Pinky small button", 4,0,0); la_AddButtonProp(pc,"bd","BD","Button D pinky small button", 4,0,0);
  183. la_AddButtonProp(pc,"pinkyl","PKL","Pinky lever", 5,0,0);
  184. la_AddButtonProp(pc,"h1","H1","Hat 1 (Upper round hat)", 6,4,"N,E,S,W");
  185. la_AddButtonProp(pc,"h2","H2","Hat 2 (lower jagged hat)", 10,4,"N,E,S,W");
  186. //button 14-16 not sure where it is....
  187. }
  188. void laui_X56Throttle(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  189. laColumn* c=laFirstColumn(uil),*cl, *cr, *crl,*crr, *rc, *vc, *rcl,*rcr;
  190. laSplitColumn(uil,c,0.17); cl=laLeftColumn(c,0); cr=laRightColumn(c,0);
  191. laSplitColumn(uil,cr,0.4); crl=laLeftColumn(cr,0); crr=laRightColumn(cr,0);
  192. laSplitColumn(uil,crr,0.6); rc=laLeftColumn(crr,10); vc=laRightColumn(crr,0);
  193. laSplitColumn(uil,rc,0.4); rcl=laLeftColumn(rc,2); rcr=laRightColumn(rc,0);
  194. laUiItem* b,*ui,*g; laUiList*gu;
  195. laShowItem(uil,c,This,"base.name")->Flags|=LA_TEXT_ALIGN_CENTER;
  196. b=laBeginRow(uil,c,0,0);
  197. laShowItem(uil,c,This,"base.user_assigned_id");
  198. laUiItem* b1=laOnConditionThat(uil,c,laPropExpression(This,"base.error"));{
  199. laShowLabel(uil,c,"Device error.",0,0)->Expand=1;
  200. }laElse(uil,b1);{
  201. laShowItem(uil,c,This,"base.path")->Expand=1;
  202. }laEndCondition(uil,b1);
  203. laEndRow(uil,b);
  204. laShowSeparator(uil,c);
  205. laShowItem(uil,cl,This,"pinky_up");
  206. laShowItem(uil,cl,This,"pinky_dn");
  207. laShowSeparator(uil,cl);
  208. laShowItem(uil,cl,This,"dial_fwd");
  209. laShowItem(uil,cl,This,"dial_back");
  210. b=laBeginRow(uil,crl,0,0);
  211. ui=laShowItem(uil,crl,This,"bi");ui->Expand=1;
  212. ui=laShowItem(uil,crl,This,"bh");ui->Expand=1;
  213. laEndRow(uil,b);
  214. b=laBeginRow(uil,crl,0,0);
  215. ui=laShowItem(uil,crl,This,"thr1");ui->Expand=1;ui->Extra->HeightCoeff=10;ui->Flags|=LA_UI_FLAGS_TRANSPOSE;
  216. ui=laShowItem(uil,crl,This,"thr2");ui->Expand=1;ui->Extra->HeightCoeff=10;ui->Flags|=LA_UI_FLAGS_TRANSPOSE;
  217. laEndRow(uil,b);
  218. laShowItem(uil,rcl,This,"bf");
  219. laShowItem(uil,rcr,This,"wf");
  220. laShowItem(uil,rcl,This,"bg");
  221. laShowItem(uil,rcr,This,"wg");
  222. laShowSeparator(uil,cl);
  223. laShowItem(uil,rcl,This,"slider");
  224. laShowSeparator(uil,rcl);
  225. laShowItem(uil,rcl,This,"be");
  226. laShowItem(uil,rcl,This,"bball");
  227. laShowItem(uil,rcr,This,"ball");
  228. laShowItem(uil,rc,This,"h3")->Flags|=LA_UI_FLAGS_TRANSPOSE;
  229. laShowItem(uil,rc,This,"h4")->Flags|=LA_UI_FLAGS_TRANSPOSE;
  230. laShowItem(uil,vc,This,"t4_up");
  231. laShowItem(uil,vc,This,"t4_dn"); laShowSeparator(uil,vc);
  232. laShowItem(uil,vc,This,"t3_up");
  233. laShowItem(uil,vc,This,"t3_dn"); laShowSeparator(uil,vc);
  234. laShowItem(uil,vc,This,"t2_up");
  235. laShowItem(uil,vc,This,"t2_dn"); laShowSeparator(uil,vc);
  236. laShowItem(uil,vc,This,"t1_up");
  237. laShowItem(uil,vc,This,"t1_dn"); laShowSeparator(uil,vc);
  238. laShowItem(uil,vc,This,"r3");
  239. laShowItem(uil,vc,This,"r4");
  240. laShowSeparator(uil,c);
  241. laShowLabel(uil,cl,"Mode",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  242. laShowItem(uil,cl,This,"mode");
  243. laShowLabel(uil,cr,"Switches",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  244. b=laBeginRow(uil,cr,0,0);
  245. laShowItem(uil,cr,This,"sw1")->Expand=1;
  246. laShowItem(uil,cr,This,"sw3")->Expand=1;
  247. laShowItem(uil,cr,This,"sw5")->Expand=1;
  248. laEndRow(uil,b);
  249. b=laBeginRow(uil,cr,0,0);
  250. laShowItem(uil,cr,This,"sw2")->Expand=1;
  251. laShowItem(uil,cr,This,"sw4")->Expand=1;
  252. laShowItem(uil,cr,This,"sw6")->Expand=1;
  253. laEndRow(uil,b);
  254. }
  255. void laui_X56Stick(laUiList *uil, laPropPack *This, laPropPack *Extra, laColumn *UNUSED, int context){
  256. laColumn* c=laFirstColumn(uil),*cl, *cc, *cr;
  257. laSplitColumn(uil,c,0.2); cl=laLeftColumn(c,10); cr=laRightColumn(c,0);
  258. laSplitColumn(uil,cr,0.8); cc=laLeftColumn(cr,0); cr=laRightColumn(cr,10);
  259. laUiItem* b;
  260. laShowItem(uil,c,This,"base.name")->Flags|=LA_TEXT_ALIGN_CENTER;
  261. b=laBeginRow(uil,c,0,0);
  262. laShowItem(uil,c,This,"base.user_assigned_id");
  263. laUiItem* b1=laOnConditionThat(uil,c,laPropExpression(This,"base.error"));{
  264. laShowLabel(uil,c,"Device error.",0,0)->Expand=1;
  265. }laElse(uil,b1);{
  266. laShowItem(uil,c,This,"base.path")->Expand=1;
  267. }laEndCondition(uil,b1);
  268. laEndRow(uil,b);
  269. laShowSeparator(uil,c);
  270. laShowItem(uil,cl,This,"ba");
  271. laShowItem(uil,cl,This,"pov");
  272. laShowSeparator(uil,cl);
  273. laShowLabel(uil,cl,"Thumb",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  274. laShowItem(uil,cl,This,"bc");
  275. laShowItem(uil,cl,This,"ball");
  276. laShowSeparator(uil,cl);
  277. laShowLabel(uil,cl,"Pinky",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  278. laShowItem(uil,cl,This,"pinkyl");
  279. laShowItem(uil,cl,This,"pinky");
  280. laShowItem(uil,cr,This,"bb");
  281. laShowSeparator(uil,cr);
  282. laShowItem(uil,cc,This,"trigger");
  283. laShowItem(uil,cc,This,"stick");
  284. laShowItem(uil,cc,This,"rudder");
  285. laShowLabel(uil,cr,"H1",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  286. laShowItem(uil,cr,This,"h1");
  287. laShowSeparator(uil,cr);
  288. laShowLabel(uil,cr,"H2",0,0)->Flags|=LA_TEXT_ALIGN_CENTER;
  289. laShowItem(uil,cr,This,"h2");
  290. }