*/}}
1
0

la_controllers.c 15 KB

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