Ignore:
Timestamp:
Mar 9, 2022, 5:44:06 PM (3 years ago)
Author:
Sanahuja Guillaume
Message:

change default usb method for ds3, now input event, no need to be root

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/Controller/DualShock3/src/DualShock3.cpp

    r318 r467  
    3939#include <bluetooth/hci_lib.h>
    4040#include <sys/file.h>
     41#include <linux/input.h>
    4142
    4243#define USB_DIR_IN 0x80
     
    113114  dataSize = 6;
    114115  datas = new int8_t[dataSize];
     116  for(int i=0;i<dataSize;i++) datas[i]=0;
    115117
    116118  GroupBox *settingsGroupBox =
     
    148150      setup_device(dev);
    149151    }
    150   } else if (connectionType == Usb) {
     152  } else if (connectionType == UsbHidRaw) {
    151153    int nr, i;
    152154    unsigned char buf[128];
     
    180182    }
    181183    if (i == 255) {
    182       Thread::Err("sixad-raw::open(hidrawX) - failed to open hidraw device\n");
     184      Thread::Err("open(hidrawX) - failed to open hidraw device\n");
    183185      return;
    184186    }
     
    186188    // block until PS button is pressed
    187189    if ((nr = read(usb_fd, buf, sizeof(buf))) < 0) {
    188       Thread::Err("sixad-raw::read(fd) - failed to read from device\n");
     190      Thread::Err("read(fd) - failed to read from device\n");
    189191    }
    190192
    191193    if (nr < 49 || nr > 50) {
    192       Thread::Err("sixad-raw::read(fd) - not a sixaxis (nr = %i )\n", nr);
     194      Thread::Err("read(fd) - not a sixaxis (nr = %i )\n", nr);
     195    }
     196  } else if (connectionType == UsbEvent) {
     197    int nr, i;
     198    unsigned char buf[128];
     199
     200    for (i = 0; i < 255; i++) {
     201      ostringstream dev_name;
     202      dev_name << "/dev/input/event" << i;
     203      if ((usb_fd = open(dev_name.str().c_str(), O_RDONLY)) >= 0) {
     204        int res=flock(usb_fd, LOCK_EX|LOCK_NB);
     205        if(res<0) {
     206          Thread::Warn("%s seems to be locked by another application\n", dev_name.str().c_str());
     207          close(usb_fd);
     208          continue;
     209        }
     210       
     211        unsigned short id[4];
     212        res = ioctl(usb_fd, EVIOCGID, id);
     213        if (res < 0) {
     214          Thread::Err("ioctl error (EVIOCGID) on %s\n",dev_name.str().c_str());
     215        } else {
     216          if (id[ID_VENDOR] == 0x054c && id[ID_PRODUCT] == 0x0268) {
     217            Printf("successfully opened %s\n", dev_name.str().c_str());
     218            Printf("Press PS button to turn the controller on\n");
     219            break;
     220          }
     221        }
     222        flock(usb_fd, LOCK_UN);
     223        close(usb_fd);
     224      }
     225    }
     226    if (i == 255) {
     227      Thread::Err("open(/dev/input/eventX) - failed to open event device\n");
     228      Thread::Err("you can try with the old usb_hidraw method\n");
     229      return;
     230    }
     231   
     232    // block until PS button is pressed
     233    struct input_event tmp;
     234    if ((nr = read(usb_fd, &tmp, sizeof(struct input_event))) < 0) {
     235      Thread::Err("read(fd) - failed to read from device\n");
    193236    }
    194237  }
     
    197240
    198241DualShock3::~DualShock3() {
    199   if (connectionType == Usb) {
     242  if (connectionType == UsbHidRaw || connectionType == UsbEvent) {
    200243    flock(usb_fd, LOCK_UN);
    201244    close(usb_fd);
     
    335378    return false;
    336379
    337   } else if (connectionType == Usb) {
     380  } else if (connectionType == UsbHidRaw) {
    338381    int nr = read(usb_fd, report, sizeof(report));
    339382    return parse_report_sixaxis_ds3(report, nr);
     383  } else if (connectionType == UsbEvent) {
     384    return parse_input_event();
    340385  }
    341386  return false;
     387}
     388
     389uint8_t DualShock3::SetBitValue(int8_t &reg,uint8_t mask,uint8_t value) {
     390  reg=(reg&~mask) | value*mask;
     391}
     392
     393bool DualShock3::parse_input_event(void) {
     394  struct input_event ev[64];
     395        int i, rd;
     396
     397  rd = read(usb_fd, ev, sizeof(struct input_event) * 64);
     398
     399  if (rd < (int) sizeof(struct input_event)) {
     400    printf("expected %d bytes, got %d\n", (int) sizeof(struct input_event), rd);
     401    return false;
     402  }
     403
     404  for (i = 0; i < rd / sizeof(struct input_event); i++) {
     405    if (ev[i].type == EV_ABS) {
     406      //code 2 and 5 are z/rz (l2 and r2)
     407      if(ev[i].code==0) datas[2] = compute_dead_zone(0, ev[i].value);//x
     408      if(ev[i].code==1) datas[3] = compute_dead_zone(1, ev[i].value);//y
     409      if(ev[i].code==3) datas[4] = compute_dead_zone(2, ev[i].value);//rx
     410      if(ev[i].code==4) datas[5] = compute_dead_zone(3, ev[i].value);//ry
     411    }
     412    if (ev[i].type == EV_KEY) {
     413      if(ev[i].code==BTN_START) SetBitValue(datas[0],0x08,ev[i].value);
     414      if(ev[i].code==BTN_SELECT) SetBitValue(datas[0],0x01,ev[i].value);
     415      if(ev[i].code==BTN_WEST) SetBitValue(datas[1],0x80,ev[i].value);// square
     416      if(ev[i].code==BTN_NORTH) SetBitValue(datas[1],0x10,ev[i].value);// triangle
     417      if(ev[i].code==BTN_EAST) SetBitValue(datas[1],0x20,ev[i].value);// circle
     418      if(ev[i].code==BTN_SOUTH) SetBitValue(datas[1],0x40,ev[i].value);// cross
     419      if(ev[i].code==BTN_TL) SetBitValue(datas[1],0x04,ev[i].value);
     420      if(ev[i].code==BTN_TL2) SetBitValue(datas[1],0x01,ev[i].value);
     421      if(ev[i].code==BTN_THUMBL) SetBitValue(datas[0],0x02,ev[i].value);
     422      if(ev[i].code==BTN_TR) SetBitValue(datas[1],0x08,ev[i].value);
     423      if(ev[i].code==BTN_TR2) SetBitValue(datas[1],0x02,ev[i].value);
     424      if(ev[i].code==BTN_THUMBR) SetBitValue(datas[0],0x04,ev[i].value);
     425      if(ev[i].code==BTN_DPAD_UP) SetBitValue(datas[0],0x10,ev[i].value);
     426      if(ev[i].code==BTN_DPAD_DOWN) SetBitValue(datas[0],0x40,ev[i].value);
     427      if(ev[i].code==BTN_DPAD_LEFT) SetBitValue(datas[0],0x80,ev[i].value);
     428      if(ev[i].code==BTN_DPAD_RIGHT) SetBitValue(datas[0],0x20,ev[i].value);
     429    }
     430 
     431    if (GetTime() > (last_voltage_time + 5 * (Time)1000000000)) {
     432      // toute les 5 secondes
     433      // report battery charge level
     434      batteryChargeLevel->SetText("battery: usb connected");
     435      last_voltage_time = GetTime();
     436    }
     437  }
     438  return true;
    342439}
    343440
     
    357454        batteryChargeLevel->SetText("battery: %i/5", r[30]);
    358455      }
    359       if (connectionType == Usb) {
     456      if (connectionType == UsbHidRaw) {
    360457        batteryChargeLevel->SetText("battery: usb connected");
    361458      }
     
    388485
    389486void DualShock3::GetButtonData() {
    390   // static uint8_t old_start_button=0;
    391487  button->GetMutex();
    392488  button->SetValueNoMutex(0, 0, (datas[0] & 0x08) == 0 ? 0 : 1); // start
    393   /*
    394       uint8_t start_button=datas[0]&0x08;
    395       if (start_button!=old_start_button) {
    396           if (start_button==0) {
    397               Thread::Info("Debug: start button released\n");
    398           } else {
    399               Thread::Info("Debug: start button pressed\n");
    400           }
    401           old_start_button=start_button;
    402       }
    403   */
    404489  button->SetValueNoMutex(1, 0, (datas[0] & 0x01) == 0 ? 0 : 1); // select
    405490  button->SetValueNoMutex(2, 0, (datas[1] & 0x80) == 0 ? 0 : 1); // square
Note: See TracChangeset for help on using the changeset viewer.