From 694ef2a4664c67d1598af33181412b22911e309d Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Thu, 8 Sep 2022 16:10:07 -0600 Subject: [PATCH] Switch (badly) to C++ This is so I can use capnproto without adding a 3rd party compiler. --- Makefile | 4 ++-- capture.c | 23 +++++++++++++---------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 3680ec7..3993711 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,10 @@ bin: mkdir -p bin capture: bin capture.c - gcc capture.c -o bin/capture + g++ capture.c -o bin/capture clean: rm -Rf bin playback: bin playback.c - gcc playback.c -o bin/playback + g++ playback.c -o bin/playback diff --git a/capture.c b/capture.c index 692191a..ef04ad7 100644 --- a/capture.c +++ b/capture.c @@ -1,7 +1,9 @@ #include #include +#include #include #include +#include #include #include @@ -11,10 +13,10 @@ #define MAX_EVENTS 16 #define CONTENT_BUFFER_SIZE 32 -static char* KEYBOARD = "k"; -static char* MOUSE = "m"; +constexpr std::string_view KEYBOARD = "k"; +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_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 diff; 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_nsec = now.tv_nsec; - printf("%ld %ld,%s,%s\n", - diff.tv_sec, - diff.tv_nsec, - type, - content_buffer); + std::cout << + diff.tv_sec << " " << + diff.tv_nsec << "," << + type << "," << + content_buffer << std::endl; fflush(stdout); return 0; @@ -179,7 +181,7 @@ int stream_events(char* mouse_path, char* keyboard_path, int hotkey_scancode) { struct input_event i_event; while(running) { 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++) { int result = read(events[i].data.fd, &i_event, sizeof(struct input_event)); if(result < 0) { @@ -214,4 +216,5 @@ int stream_events(char* mouse_path, char* keyboard_path, int hotkey_scancode) { } } } + return 0; }