⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wmd.c

📁 linux下wii的鼠标驱动
💻 C
字号:
#include <fcntl.h>#include <signal.h>#include <stdarg.h>#include <stdio.h>#include <stdlib.h>#include <sys/stat.h>#include <sys/types.h>#include <time.h>#include <unistd.h>#include "WiimoteLayer.h"int device_node;FILE *log_file;unsigned char LED = 0;int lprintf(const char *format, ...) {  char buffer[4096];  va_list argList;  int temp, len;  char timestamp[1024], *timePtr;  time_t curTime;  va_start(argList, format);  temp = vsnprintf(buffer, sizeof(buffer), format, argList);  va_end(argList);  time(&curTime);  timePtr = ctime(&curTime);  strcpy(timestamp, timePtr);  len = strlen(timestamp);  if (timestamp[len - 1] == '\n') {    timestamp[len - 1] = '\0';  }  fprintf(log_file, "[%s] %s", timestamp, buffer);  fflush(log_file);  return temp;}void handle_int(int arg) {  lprintf("cleaning up\n");  cleanup_wiimote_layer();  if (fclose(log_file) == EOF) {    perror("fclose");    exit(EXIT_FAILURE);  }  if (close(device_node) == -1) {    perror("close");    exit(EXIT_FAILURE);  }  exit(EXIT_SUCCESS);}void poll(Wiimote *wm) {  int x, y;  x = y = 0;  while (get_data(wm) == 1) {    size_t byte_count;    if (wm->data.x != 0) {      x += wm->data.x;      wm->data.x = 0;    }    if (wm->data.y != 0) {      y += wm->data.y;      wm->data.y = 0;    }    if (x < -100 || x > 100) {      wm->data.x = x / 100;      x = x - ((x / 100) * 100);    }    if (y < -100 || y > 100) {      wm->data.y = y / 100;      y = y - ((y / 100) * 100);    }    byte_count = write(device_node, &wm->data, sizeof(struct WiimoteData));    if (byte_count == -1) {      perror("write");      exit(EXIT_FAILURE);    }    byte_count = read(device_node, &wm->data, sizeof(struct WiimoteData));    if (byte_count == -1) {      perror("read");      exit(EXIT_FAILURE);    } else if (byte_count < sizeof(struct WiimoteData)) {      //lprintf("error: read only %d of %d bytes\n", byte_count, sizeof(struct WiimoteData));    } else {      //lprintf("read all bytes\n");    }      if(LED != wm->data.LEDField) {      LED = wm->data.LEDField;    byte_count = send_data(wm);    if (byte_count == -1) {      perror("send_data");      exit(EXIT_FAILURE);    } else if (byte_count < 3) {      //lprintf("error: sent only %d of 3 bytes\n", byte_count);    } else {      //lprintf("sent all bytes\n");    }   }  }}void listen(void) {  Wiimote *wm;  while (1) {    lprintf("connecting\n");    while ((wm = wiimote_connect()) == NULL);    lprintf("connected\n");    set_functionality(wm, WIIPKT_IR_SENSOR);    wm->data.LEDField = 0;    wm->data.isRumble = 0;    lprintf("polling\n");    send_data(wm);    poll(wm);    lprintf("disconnecting\n");    wiimote_disconnect(wm);  }}int main(int argc, char **argv) {  struct sigaction sa;  sa.sa_handler = handle_int;  sigemptyset(&sa.sa_mask);  sa.sa_flags = 0;  if (sigaction(SIGINT, &sa, 0) == -1) {    perror("sigaction");    exit(EXIT_FAILURE);  }  if ((device_node = open(argv[1], O_RDWR)) == -1) {    perror("open");    exit(EXIT_FAILURE);  }  if(!strcmp(argv[2], "stdout"))     log_file = stdout;  else if ((log_file = fopen(argv[2], "a")) == NULL) {    perror("fopen");    exit(EXIT_FAILURE);  }  lprintf("initializing\n");  initialize_wiimote_layer();  lprintf("listening\n");  listen();  return EXIT_SUCCESS;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -