From 3b3b11dfef4fc103b5d40ecc0dead3a12dbcdb7e Mon Sep 17 00:00:00 2001 From: itsmattkc <34096995+itsmattkc@users.noreply.github.com> Date: Fri, 24 May 2024 10:04:08 -0700 Subject: [PATCH] improve pipes --- app/backend.cpp | 2 -- app/mainwindow.cpp | 8 +++-- lib/pipe.c | 89 ---------------------------------------------- lib/status.c | 2 +- lib/util.c | 2 +- lib/vanilla.c | 11 ++++-- lib/vanilla.h | 1 + pipe/main.c | 4 --- 8 files changed, 16 insertions(+), 103 deletions(-) delete mode 100644 lib/pipe.c diff --git a/app/backend.cpp b/app/backend.cpp index 4f6838f..f50ed65 100644 --- a/app/backend.cpp +++ b/app/backend.cpp @@ -81,8 +81,6 @@ void Backend::interrupt() uint8_t cc = VANILLA_PIPE_IN_INTERRUPT; write(m_pipeOut, &cc, sizeof(cc)); m_pipeMutex.unlock(); - - ignoreByte(m_pipeIn); } else { vanilla_stop(); } diff --git a/app/mainwindow.cpp b/app/mainwindow.cpp index 463084f..924a6ed 100644 --- a/app/mainwindow.cpp +++ b/app/mainwindow.cpp @@ -156,7 +156,7 @@ MainWindow::MainWindow(QWidget *parent) : QWidget(parent) QMetaObject::invokeMethod(m_audioHandler, &AudioHandler::run, Qt::QueuedConnection); connect(m_backend, &Backend::videoAvailable, m_videoDecoder, &VideoDecoder::sendPacket); - connect(m_backend, &Backend::syncCompleted, m_connectBtn, &QPushButton::setEnabled); + connect(m_backend, &Backend::syncCompleted, this, [this](bool e){if (e) m_connectBtn->setEnabled(true);}); connect(m_videoDecoder, &VideoDecoder::frameReady, m_viewer, &Viewer::setImage); connect(m_backend, &Backend::audioAvailable, m_audioHandler, &AudioHandler::write); connect(m_backend, &Backend::vibrate, m_gamepadHandler, &GamepadHandler::vibrate, Qt::DirectConnection); @@ -196,8 +196,6 @@ MainWindow::~MainWindow() SDL_Quit(); - setConnectedState(false); - delete m_viewer; } @@ -286,6 +284,10 @@ void MainWindow::setConnectedState(bool on) QMetaObject::invokeMethod(m_backend, &Backend::connectToConsole, Qt::QueuedConnection, m_wirelessInterfaceComboBox->currentText()); } else { + if (m_backend) { + m_backend->interrupt(); + } + m_connectBtn->setText(tr("Connect")); m_viewer->setImage(QImage()); diff --git a/lib/pipe.c b/lib/pipe.c deleted file mode 100644 index bd17d11..0000000 --- a/lib/pipe.c +++ /dev/null @@ -1,89 +0,0 @@ -#include -#include -#include -#include - -#include "status.h" -#include "vanilla.h" - -void console_event_handler(void *context, int type, const char *data, size_t data_size) -{ - switch (type) { - case VANILLA_EVENT_VIDEO: - print_info("RECEIVED VIDEO PACKET OF SIZE %llu", data_size); - break; - case VANILLA_EVENT_AUDIO: - print_info("RECEIVED AUDIO PACKET OF SIZE %llu", data_size); - break; - } -} - -int main(int argc, char **argv) -{ - print_status(VANILLA_READY); - - char *line = NULL; - size_t size; - - while (1) { - ssize_t sz = getline(&line, &size, stdin); - if (sz == -1) { - break; - } - - static const int cmd_max_args = 10; - static const int cmd_max_arg_length = 20; - int cmd_nb_arg = 0; - char cmd_args[cmd_max_args][cmd_max_arg_length]; - - static const char *delim = " \n"; - char *token = strtok(line, delim); - while (token && cmd_nb_arg < cmd_max_args) { - strncpy(cmd_args[cmd_nb_arg], token, cmd_max_arg_length); - token = strtok(NULL, delim); - cmd_nb_arg++; - } - - if (cmd_nb_arg == 0) { - print_status(VANILLA_UNKNOWN_COMMAND); - continue; - } - - if (!strcmp("SYNC", cmd_args[0])) { - if (cmd_nb_arg != 3) { - print_status(VANILLA_INVALID_ARGUMENT); - continue; - } - - const char *wireless_interface = cmd_args[1]; - const char *code = cmd_args[2]; - - int sync_status = vanilla_sync_with_console(wireless_interface, atoi(code)); - if (sync_status == VANILLA_SUCCESS) { - print_status(VANILLA_SUCCESS); - } else { - print_status(VANILLA_ERROR); - } - } else if (!strcmp("CONNECT", cmd_args[0])) { - if (cmd_nb_arg != 2) { - print_status(VANILLA_INVALID_ARGUMENT); - continue; - } - - const char *wireless_interface = cmd_args[1]; - - int status = vanilla_connect_to_console(wireless_interface, console_event_handler, NULL); - if (status == VANILLA_SUCCESS) { - print_status(VANILLA_SUCCESS); - } else { - print_status(VANILLA_ERROR); - } - } else if (!strcmp("EXIT", cmd_args[0])) { - break; - } else { - print_status(VANILLA_UNKNOWN_COMMAND); - } - } - - return 0; -} \ No newline at end of file diff --git a/lib/status.c b/lib/status.c index 2cd7cf2..f63bc07 100644 --- a/lib/status.c +++ b/lib/status.c @@ -26,7 +26,7 @@ void print_info(const char *errstr, ...) vanilla_log_no_newline(VANILLA_STATUS_STRINGS[-VANILLA_INFO]); vanilla_log_no_newline(" "); - vanilla_log_no_newline(errstr, args); + vanilla_log_no_newline_va(errstr, args); vanilla_log_no_newline("\n"); va_end(args); diff --git a/lib/util.c b/lib/util.c index 8bed7ee..93aa77e 100644 --- a/lib/util.c +++ b/lib/util.c @@ -74,7 +74,7 @@ size_t get_home_directory_file(const char *filename, char *buf, size_t buf_size) size_t get_max_path_length() { - return pathconf(".", _PC_PATH_MAX);; + return pathconf(".", _PC_PATH_MAX); } void interrupt_handler(int signum) diff --git a/lib/vanilla.c b/lib/vanilla.c index 6dfaf25..3c5b0ed 100644 --- a/lib/vanilla.c +++ b/lib/vanilla.c @@ -111,13 +111,18 @@ void vanilla_log_no_newline(const char *format, ...) va_list va; va_start(va, format); - if (custom_logger) { - custom_logger(format, va); - } + vanilla_log_no_newline_va(format, va); va_end(va); } +void vanilla_log_no_newline_va(const char *format, va_list args) +{ + if (custom_logger) { + custom_logger(format, args); + } +} + void vanilla_install_logger(void (*logger)(const char *, va_list)) { custom_logger = logger; diff --git a/lib/vanilla.h b/lib/vanilla.h index d8add2b..58aacec 100644 --- a/lib/vanilla.h +++ b/lib/vanilla.h @@ -112,6 +112,7 @@ void vanilla_set_touch(int x, int y); */ void vanilla_log(const char *format, ...); void vanilla_log_no_newline(const char *format, ...); +void vanilla_log_no_newline_va(const char *format, va_list args); /** * Install custom logger diff --git a/pipe/main.c b/pipe/main.c index 2dee627..c652fba 100644 --- a/pipe/main.c +++ b/pipe/main.c @@ -246,10 +246,6 @@ int main() if (current_action != 0) { void *ret; vanilla_stop(); - write_control_code(VANILLA_PIPE_ERR_SUCCESS); - pthread_join(current_action, &ret); - } else { - write_control_code(VANILLA_PIPE_ERR_INVALID); } break; }