Skip to content

Commit

Permalink
Merge branch 'rpi-eventloop'
Browse files Browse the repository at this point in the history
  • Loading branch information
itsmattkc committed Nov 14, 2024
2 parents e62c607 + 4ecf7c0 commit e9bd0fa
Show file tree
Hide file tree
Showing 24 changed files with 1,654 additions and 153 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ build/
.cache/
*.AppImage
squashfs-root/

buildpi/
rpi-ffmpeg/
14 changes: 11 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
SET(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")

SET(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
set(CMAKE_C_STANDARD 23)

add_subdirectory(lib)
OPTION(VANILLA_BUILD_DESKTOP "Build Qt app for Linux desktop" ON)
OPTION(VANILLA_BUILD_RPI "Build SDL2 app for Raspberry Pi" OFF)
OPTION(VANILLA_BUILD_TESTS "Build unit tests for Vanilla" OFF)

add_subdirectory(lib)
if (NOT ANDROID)
add_subdirectory(pipe)
endif()

add_subdirectory(app)
if (VANILLA_BUILD_DESKTOP)
add_subdirectory(app)
endif()
if (VANILLA_BUILD_RPI)
add_subdirectory(rpi)
endif()
39 changes: 21 additions & 18 deletions app/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,22 @@
#include <unistd.h>
#include <vanilla.h>

void vanillaEventHandler(void *context, int type, const char *data, size_t dataLength)
{
Backend *backend = static_cast<Backend*>(context);

switch (type) {
case VANILLA_EVENT_VIDEO:
emit backend->videoAvailable(QByteArray(data, dataLength));
break;
case VANILLA_EVENT_AUDIO:
emit backend->audioAvailable(QByteArray(data, dataLength));
break;
case VANILLA_EVENT_VIBRATE:
emit backend->vibrate(*data);
break;
void Backend::vanillaEventHandler()
{
vanilla_event_t event;

while (vanilla_wait_event(&event)) {
switch (event.type) {
case VANILLA_EVENT_VIDEO:
emit videoAvailable(QByteArray((const char *) event.data, event.size));
break;
case VANILLA_EVENT_AUDIO:
emit audioAvailable(QByteArray((const char *) event.data, event.size));
break;
case VANILLA_EVENT_VIBRATE:
emit vibrate(*event.data);
break;
}
}
}

Expand Down Expand Up @@ -70,12 +72,13 @@ int Backend::syncInternal(uint16_t code)

void Backend::connectToConsole()
{
QtConcurrent::run(&Backend::connectInternal, this);
connectInternal();
QtConcurrent::run(&Backend::vanillaEventHandler, this);
}

int Backend::connectInternal()
{
return vanilla_start(vanillaEventHandler, this);
return vanilla_start(0);
}

void Backend::updateTouch(int x, int y)
Expand Down Expand Up @@ -175,7 +178,7 @@ int BackendViaInternalPipe::syncInternal(uint16_t code)

int BackendViaInternalPipe::connectInternal()
{
return vanilla_start_udp(vanillaEventHandler, this, QHostAddress(QHostAddress::LocalHost).toIPv4Address());
return vanilla_start(QHostAddress(QHostAddress::LocalHost).toIPv4Address());
}

BackendViaExternalPipe::BackendViaExternalPipe(const QHostAddress &udpServer, QObject *parent) : Backend(parent)
Expand All @@ -190,5 +193,5 @@ int BackendViaExternalPipe::syncInternal(uint16_t code)

int BackendViaExternalPipe::connectInternal()
{
return vanilla_start_udp(vanillaEventHandler, this, m_serverAddress.toIPv4Address());
return vanilla_start(m_serverAddress.toIPv4Address());
}
2 changes: 2 additions & 0 deletions app/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ public slots:
private slots:
void syncFutureCompleted();

void vanillaEventHandler();

};

class BackendViaInternalPipe : public Backend
Expand Down
7 changes: 7 additions & 0 deletions docker/rpi/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FROM --platform=linux/arm/v6 arm32v6/alpine

RUN apk update
RUN apk upgrade
RUN apk add build-base cmake libnl3-dev openssl-dev sdl2-dev linux-headers ffmpeg-dev eudev-dev

CMD [ "make", "-j32", "-C", "/data/buildpi" ]
9 changes: 9 additions & 0 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ if (NOT ANDROID)
endif()

install(TARGETS vanilla)

if (VANILLA_BUILD_TESTS)
add_executable(bittest
test/bittest.c
)

target_link_libraries(bittest PRIVATE vanilla m)
target_include_directories(bittest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
endif()
29 changes: 17 additions & 12 deletions lib/gamepad/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,24 @@ typedef struct {
uint32_t video_format;
} AudioPacketVideoFormat;

void handle_audio_packet(vanilla_event_handler_t event_handler, void *context, char *data, size_t len)
void handle_audio_packet(gamepad_context_t *ctx, unsigned char *data, size_t len)
{
for (int byte = 0; byte < len; byte++) {
//
// === IMPORTANT NOTE! ===
//
// This for loop skips ap->format, ap->seq_id, and ap->timestamp to save processing.
// If you want those, you'll have to adjust this loop.
//
for (int byte = 0; byte < 4; byte++) {
data[byte] = (unsigned char) reverse_bits(data[byte], 8);
}

AudioPacket *ap = (AudioPacket *) data;

ap->format = reverse_bits(ap->format, 3);
ap->seq_id = reverse_bits(ap->seq_id, 10);
// ap->format = reverse_bits(ap->format, 3);
// ap->seq_id = reverse_bits(ap->seq_id, 10);
ap->payload_size = reverse_bits(ap->payload_size, 16);
ap->timestamp = reverse_bits(ap->timestamp, 32);
for (int byte = 0; byte < ap->payload_size; ++byte) {
ap->payload[byte] = (unsigned char) reverse_bits(ap->payload[byte], 8);
}
// ap->timestamp = reverse_bits(ap->timestamp, 32);

if (ap->type == TYPE_VIDEO) {
AudioPacketVideoFormat *avp = (AudioPacketVideoFormat *) ap->payload;
Expand All @@ -56,22 +59,24 @@ void handle_audio_packet(vanilla_event_handler_t event_handler, void *context, c
return;
}

event_handler(context, VANILLA_EVENT_AUDIO, ap->payload, ap->payload_size);
if (ap->payload_size) {
push_event(ctx->event_loop, VANILLA_EVENT_AUDIO, ap->payload, ap->payload_size);
}

uint8_t vibrate_val = ap->vibrate;
event_handler(context, VANILLA_EVENT_VIBRATE, &vibrate_val, sizeof(vibrate_val));
push_event(ctx->event_loop, VANILLA_EVENT_VIBRATE, &vibrate_val, sizeof(vibrate_val));
}

void *listen_audio(void *x)
{
struct gamepad_thread_context *info = (struct gamepad_thread_context *) x;
gamepad_context_t *info = (gamepad_context_t *) x;
unsigned char data[2048];
ssize_t size;
do {
size = recv(info->socket_aud, data, sizeof(data), 0);
if (size > 0) {
if (is_stop_code(data, size)) break;
handle_audio_packet(info->event_handler, info->context, data, size);
handle_audio_packet(info, data, size);
}
} while (!is_interrupted());

Expand Down
2 changes: 1 addition & 1 deletion lib/gamepad/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ void handle_command_packet(int skt, CmdHeader *request)

void *listen_command(void *x)
{
struct gamepad_thread_context *info = (struct gamepad_thread_context *)x;
gamepad_context_t *info = (gamepad_context_t *)x;

unsigned char data[sizeof(CmdHeader) + 2048];
ssize_t size;
Expand Down
Loading

0 comments on commit e9bd0fa

Please sign in to comment.