deltarune-irc/main.c

136 lines
2.4 KiB
C

#include "irc.h"
#include "cfg.h"
#include <sys/time.h>
#include <linux/uinput.h>
#include <fcntl.h>
IRCC_client client;
void die(const char *msg) {
puts(msg);
exit(1);
}
void emit(int fd, int type, int code, int val) {
struct input_event ie;
ie.type = type;
ie.code = code;
ie.value = val;
ie.time.tv_sec = 0;
ie.time.tv_usec = 0;
write(fd, &ie, sizeof(ie));
}
void press(int fd, int event) {
emit(fd, EV_KEY, event, 1);
emit(fd, EV_SYN, SYN_REPORT, 0);
usleep(200000);
emit(fd, EV_KEY, event, 0);
emit(fd, EV_SYN, SYN_REPORT, 0);
}
void recvinfo(int fd) {
while (1) {
unsigned int irc_status = IRCC_recv(&client);
if (irc_status == IRCC_DISCONNECTED)
die("Disconnected");
else if (client.msg != NULL && irc_status == IRCC_PRIVMSG) {
for (int i = 0; i < strlen(client.msg); i++) {
usleep(20000);
switch (client.msg[i]) {
case 'w':
press(fd, KEY_UP);
break;
case 's':
press(fd, KEY_DOWN);
break;
case 'a':
press(fd, KEY_LEFT);
break;
case 'd':
press(fd, KEY_RIGHT);
break;
case 'z':
press(fd, KEY_Z);
break;
case 'x':
press(fd, KEY_X);
break;
case 'c':
press(fd, KEY_C);
break;
case 'e':
press(fd, KEY_ENTER);
break;
default:
break;
}
}
}
}
}
int main(void) {
//Init uinput
int fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
if (fd < 0)
die("WTF! Were uinput? Try modprobe uinput");
ioctl(fd, UI_SET_EVBIT, EV_KEY);
ioctl(fd, UI_SET_KEYBIT, KEY_RIGHT);
ioctl(fd, UI_SET_KEYBIT, KEY_UP);
ioctl(fd, UI_SET_KEYBIT, KEY_DOWN);
ioctl(fd, UI_SET_KEYBIT, KEY_LEFT);
ioctl(fd, UI_SET_KEYBIT, KEY_ENTER);
ioctl(fd, UI_SET_KEYBIT, KEY_C);
ioctl(fd, UI_SET_KEYBIT, KEY_Z);
ioctl(fd, UI_SET_KEYBIT, KEY_X);
struct uinput_setup usetup;
memset(&usetup, 0, sizeof(usetup));
usetup.id.bustype = BUS_USB;
usetup.id.vendor = 0x1234;
usetup.id.product = 0x5678;
strcpy(usetup.name, "IrcKeyboard");
ioctl(fd, UI_DEV_SETUP, &usetup);
ioctl(fd, UI_DEV_CREATE);
sleep(2);
//512 - size of raw buffer (max irc)
IRCC_init(&client);
int status = IRCC_connect(&client, HOST, PORT);
if (status == IRCC_ERROR)
die("Conn refused");
//Register
IRCC_register(&client, NICK);
sleep(5);
for (size_t i = 0; i < sizeof(channels) / sizeof(char *); i++) {
if (channels[i] == NULL)
break;
IRCC_join(&client, channels[i], NULL);
}
recvinfo(fd);
}