Switch (badly) to C++

This is so I can use capnproto without adding a 3rd party compiler.
This commit is contained in:
Eli Ribble 2022-09-08 16:10:07 -06:00
parent a461c8eb6a
commit 694ef2a466
2 changed files with 15 additions and 12 deletions

View File

@ -4,10 +4,10 @@ bin:
mkdir -p bin mkdir -p bin
capture: bin capture.c capture: bin capture.c
gcc capture.c -o bin/capture g++ capture.c -o bin/capture
clean: clean:
rm -Rf bin rm -Rf bin
playback: bin playback.c playback: bin playback.c
gcc playback.c -o bin/playback g++ playback.c -o bin/playback

View File

@ -1,7 +1,9 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <iostream>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
@ -11,10 +13,10 @@
#define MAX_EVENTS 16 #define MAX_EVENTS 16
#define CONTENT_BUFFER_SIZE 32 #define CONTENT_BUFFER_SIZE 32
static char* KEYBOARD = "k"; constexpr std::string_view KEYBOARD = "k";
static char* MOUSE = "m"; constexpr std::string_view MOUSE = "m";
int dump_event(struct timespec* start, struct input_event* event, char* type); int dump_event(struct timespec* start, struct input_event* event, const std::string_view& type);
int event_content_keyboard(char* buffer, int buffer_size, struct input_event* event); int event_content_keyboard(char* buffer, int buffer_size, struct input_event* event);
int event_content_mouse(char* buffer, int buffer_size, struct input_event* event); int event_content_mouse(char* buffer, int buffer_size, struct input_event* event);
@ -48,7 +50,7 @@ static inline void time_diff(struct timeval* a, struct timespec* b, struct timev
} }
} }
int dump_event(struct timespec* start, struct input_event* event, char* type) { int dump_event(struct timespec* start, struct input_event* event, const std::string_view& type) {
struct timespec now; struct timespec now;
struct timespec diff; struct timespec diff;
char content_buffer[CONTENT_BUFFER_SIZE]; char content_buffer[CONTENT_BUFFER_SIZE];
@ -76,11 +78,11 @@ int dump_event(struct timespec* start, struct input_event* event, char* type) {
start->tv_sec = now.tv_sec; start->tv_sec = now.tv_sec;
start->tv_nsec = now.tv_nsec; start->tv_nsec = now.tv_nsec;
printf("%ld %ld,%s,%s\n", std::cout <<
diff.tv_sec, diff.tv_sec << " " <<
diff.tv_nsec, diff.tv_nsec << "," <<
type, type << "," <<
content_buffer); content_buffer << std::endl;
fflush(stdout); fflush(stdout);
return 0; return 0;
@ -179,7 +181,7 @@ int stream_events(char* mouse_path, char* keyboard_path, int hotkey_scancode) {
struct input_event i_event; struct input_event i_event;
while(running) { while(running) {
int event_count = epoll_wait(epoll_fd, events, MAX_EVENTS, -1); int event_count = epoll_wait(epoll_fd, events, MAX_EVENTS, -1);
char* type; std::string_view type;
for(int i = 0; i < event_count; i++) { for(int i = 0; i < event_count; i++) {
int result = read(events[i].data.fd, &i_event, sizeof(struct input_event)); int result = read(events[i].data.fd, &i_event, sizeof(struct input_event));
if(result < 0) { if(result < 0) {
@ -214,4 +216,5 @@ int stream_events(char* mouse_path, char* keyboard_path, int hotkey_scancode) {
} }
} }
} }
return 0;
} }