*/}}

la_controllers.c 13 KB

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