Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
- Remove printf() call on release builds. This is more deterministic
  than printf() modifiers. We only use it printing u64, f32 variables.
  • Loading branch information
e2dk4r committed Oct 25, 2024
1 parent 1b1a786 commit b249ca1
Show file tree
Hide file tree
Showing 2 changed files with 322 additions and 43 deletions.
129 changes: 109 additions & 20 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,38 @@ main(int argc, char *argv[])
struct memory_chunk *MemoryForJoystickReadEvents =
mem_push_chunk(&memory_block, sizeof(struct op_joystick_read), maxGamepadCount);

struct string stdoutBuffer = {
.data = mem_push(&memory_block, 256),
.len = 256,
};
struct string stringBuffer = {
.data = mem_push(&memory_block, 32),
.len = 32,
};

#if GAMEPAD_IDLE_INHIBIT_DEBUG
printf("total memory usage (in bytes): %lu\n", memory_block.used);
printf("total memory wasted (in bytes): %lu\n", memory_block.total - memory_block.used);
{
struct string string;
u64 length = 0;

#define PRINTLN_U64(prefix, value) \
string = StringFromZeroTerminated((u8 *)prefix); \
memcpy(stdoutBuffer.data + length, string.data, string.len); \
length += string.len; \
string = FormatU64(&stringBuffer, value); \
memcpy(stdoutBuffer.data + length, string.data, string.len); \
length += string.len; \
string = StringFromZeroTerminated((u8 *)"\n"); \
memcpy(stdoutBuffer.data + length, string.data, string.len); \
length += string.len

PRINTLN_U64("total memory usage (in bytes): ", memory_block.used);
PRINTLN_U64("total memory wasted (in bytes): ", memory_block.total - memory_block.used);
#undef PRINTLN_U64

// print buffered string to output
write(STDOUT_FILENO, stdoutBuffer.data, length);
}
#endif

/* io_uring */
Expand Down Expand Up @@ -932,8 +961,8 @@ main(int argc, char *argv[])
struct input_event *event = &op->event;

#if GAMEPAD_IDLE_INHIBIT_DEBUG
printf("%p fd: %d time: %ld.%ld type: %d code: %d value: %d\n", op, op_joystick_poll->fd, event->input_event_sec,
event->input_event_usec, event->type, event->code, event->value);
printf("%p fd: %d time: %ldsec%ldus type: %d code: %d value: %d\n", op, op_joystick_poll->fd,
event->input_event_sec, event->input_event_usec, event->type, event->code, event->value);
#endif
struct gamepad *gamepad = op_joystick_poll->gamepad;
if (event->type == EV_SYN) {
Expand Down Expand Up @@ -1037,24 +1066,84 @@ main(int argc, char *argv[])
}
}

printf("Gamepad #%p a: %d b: %d x: %d y: %d ls: %d %.2f,%.2f rs: %d %.2f,%.2f lb: %d lt: %.2f rb: %d rt: %.2f "
"home: %d back: %d start: %d\n",
// pointer
gamepad,
// buttons
gamepad->a, gamepad->b, gamepad->x, gamepad->y,
// left stick
gamepad->ls, gamepad->lsX, gamepad->lsY,
// right stick
gamepad->rs, gamepad->rsX, gamepad->rsY,
// left button, left trigger
gamepad->lb, gamepad->lt,
// right button, right trigger
gamepad->rb, gamepad->rt,
// buttons
gamepad->home, gamepad->back, gamepad->start);
struct string string;
u64 length = 0;

string = StringFromZeroTerminated((u8 *)"Gamepad #");
memcpy(stdoutBuffer.data + length, string.data, string.len);
length += string.len;

#define PRINT_BUTTON(prefix, button) \
string = StringFromZeroTerminated((u8 *)" " prefix ": "); \
memcpy(stdoutBuffer.data + length, string.data, string.len); \
length += string.len; \
string = FormatU64(&stringBuffer, gamepad->button); \
memcpy(stdoutBuffer.data + length, string.data, string.len); \
length += string.len

#define PRINT_ANALOG(prefix, stick, stickX, stickY) \
string = StringFromZeroTerminated((u8 *)" " prefix ": "); \
memcpy(stdoutBuffer.data + length, string.data, string.len); \
length += string.len; \
string = FormatU64(&stringBuffer, gamepad->stick); \
memcpy(stdoutBuffer.data + length, string.data, string.len); \
length += string.len; \
string = StringFromZeroTerminated((u8 *)" "); \
memcpy(stdoutBuffer.data + length, string.data, string.len); \
length += string.len; \
string = FormatF32(&stringBuffer, gamepad->stickX, 2); \
memcpy(stdoutBuffer.data + length, string.data, string.len); \
length += string.len; \
string = StringFromZeroTerminated((u8 *)","); \
memcpy(stdoutBuffer.data + length, string.data, string.len); \
length += string.len; \
string = FormatF32(&stringBuffer, gamepad->stickY, 2); \
memcpy(stdoutBuffer.data + length, string.data, string.len); \
length += string.len

#define PRINT_TRIGGER(prefix, trigger) \
string = StringFromZeroTerminated((u8 *)" " prefix ": "); \
memcpy(stdoutBuffer.data + length, string.data, string.len); \
length += string.len; \
string = FormatF32(&stringBuffer, gamepad->trigger, 2); \
memcpy(stdoutBuffer.data + length, string.data, string.len); \
length += string.len

PRINT_BUTTON("a", a);
PRINT_BUTTON("b", b);
PRINT_BUTTON("x", x);
PRINT_BUTTON("y", y);

PRINT_ANALOG("ls", ls, lsX, lsY);
PRINT_ANALOG("rs", rs, rsX, rsY);

PRINT_BUTTON("lb", lb);
PRINT_TRIGGER("lt", lt);

PRINT_BUTTON("rb", rb);
PRINT_TRIGGER("rt", rt);

PRINT_BUTTON("home", home);
PRINT_BUTTON("back", back);
PRINT_BUTTON("start", start);

string = StringFromZeroTerminated((u8 *)"\n");
memcpy(stdoutBuffer.data + length, string.data, string.len);
length += string.len;

write(STDOUT_FILENO, stdoutBuffer.data, length);

#undef PRINT_BUTTON
#undef PRINT_ANALOG
#undef PRINT_TRIGGER
}

// DEBUG: get file path from file descriptor
// char path[256];
// snprintf(path, sizeof(path), "/proc/self/fd/%d", op_joystick_poll->fd);
// readlink(path, path, sizeof(path));
// printf("fd: %d path: %s\n", op_joystick_poll->fd, path);

// NOTE: When there is more data to read
// 1 - Try to read another joystick event
struct op_joystick_read *submitOp = op;
Expand Down
Loading

0 comments on commit b249ca1

Please sign in to comment.