re

stuff I reverse-engineered
git clone https://github.com/TanguyAndreani/re
Log | Files | Refs | README

snes-controller.c (3177B)


      1 #include <stdio.h>
      2 #include <stdlib.h>
      3 #include <string.h>
      4 #include <unistd.h>
      5 #include <inttypes.h>
      6 #include <xdo.h>
      7 #include <wchar.h>
      8 #include <locale.h>
      9 
     10 
     11 /* mgba bindings */
     12 #define LEFT    "Left"
     13 #define RIGHT   "Right"
     14 #define UP  "Up"
     15 #define DOWN    "Down"
     16 #define L   "a"
     17 #define R   "s"
     18 #define SELECT  "BackSpace"
     19 #define START   "Return"
     20 #define Y   ""      /* dummy */
     21 #define B   "z"
     22 #define A   "x"
     23 #define X   ""      /* dummy */
     24 
     25 int
     26 main(int argc, char *argv[], char *environ[])
     27 {
     28     (void)setlocale(LC_ALL, "");
     29 
     30     xdo_t          *xdo = xdo_new(":0.0");
     31 
     32     uint8_t     i[32];
     33 
     34     int     start, select, a, b, x, y, l, r, up, down, left, right;
     35     start = select = a = b = x = y = l = r = up = down = left =
     36         right = 0;
     37 
     38     FILE           *c = fopen("/dev/uhid_snes0", "r");
     39     if (c == NULL)
     40         exit(1);
     41 
     42     while (!feof(c) && !ferror(c)) {
     43         fread(&i, 1, 8, c);
     44 
     45 #define up(var, key) \
     46     if (var == 1) \
     47         {xdo_send_keysequence_window_up(xdo, CURRENTWINDOW, key, 0);var=0;}
     48 
     49 #define down(var, key) \
     50     if (var == 0) \
     51         {xdo_send_keysequence_window_down(xdo, CURRENTWINDOW, key, 0);var=1;}
     52 
     53         if (i[3] == 127) {
     54             up(left, LEFT);
     55             up(right, RIGHT);
     56         } else if (i[3] == 255) {
     57             up(left, LEFT);
     58             down(right, RIGHT);
     59         } else if (i[3] == 0) {
     60             down(left, LEFT);
     61             up(right, RIGHT);
     62         }
     63 
     64         if (i[4] == 127) {
     65             up(down, DOWN);
     66             up(up, UP);
     67         } else if (i[4] == 255) {
     68             down(down, DOWN);
     69             up(up, UP);
     70         } else if (i[4] == 0) {
     71             up(down, DOWN);
     72             down(up, UP);
     73         }
     74 
     75         if (i[6] & 0b1) {
     76             down(l, L);
     77         } else {
     78             up(l, L);
     79         }
     80 
     81         if (i[6] & 0b10) {
     82             down(r, R);
     83         } else {
     84             up(r, R);
     85         }
     86 
     87         if (i[6] & 0b10000) {
     88             down(select, SELECT);
     89         } else {
     90             up(select, SELECT);
     91         }
     92 
     93         if (i[6] & 0b100000) {
     94             down(start, START)
     95         } else {
     96             up(start, START);
     97         }
     98 
     99         i[5] = i[5] >> 4;
    100 
    101         if (i[5] & 0b1000) {
    102             down(y, Y);
    103         } else {
    104             up(y, Y);
    105         }
    106 
    107         if (i[5] & 0b100) {
    108             down(b, B);
    109         } else {
    110             up(b, B);
    111         }
    112 
    113         if (i[5] & 0b10) {
    114             down(a, A);
    115         } else {
    116             up(a, A);
    117         }
    118 
    119         if (i[5] & 0b1) {
    120             down(x, X);
    121         } else {
    122             up(x, X);
    123         }
    124 
    125 #ifndef NDEBUG
    126 
    127     if (left == 1)
    128         putchar('<');
    129     else if (right == 1)
    130         putchar('>');
    131     else
    132         putchar('_');
    133 
    134     if (up == 1)
    135         fputwc(L'∧', stdout);
    136     else if (down == 1)
    137         fputwc(L'∨', stdout);
    138     else
    139         putchar('_');
    140 
    141 #define o(x, y) \
    142     if (x == 1) \
    143         putchar(y); \
    144     else putchar('_');
    145 
    146     o(a, 'A');
    147     o(b, 'B');
    148 
    149     o(x, 'X');
    150     o(y, 'Y');
    151 
    152     o(r, 'R');
    153     o(l, 'L');
    154 
    155     o(select, 'S');
    156 
    157 #endif
    158 
    159     if (start == 1)
    160         fputwc(L'⎆', stdout);
    161     else putchar('_');
    162 
    163     putchar('\r');
    164 
    165     }
    166 
    167     fclose(c);
    168 }