Skip to content

Commit

Permalink
Merge pull request ReimuNotMoe#229 from mnutt/stdin
Browse files Browse the repository at this point in the history
stdin command to allow freeform keyboard input
  • Loading branch information
ReimuNotMoe authored Mar 2, 2024
2 parents ad9e9ee + f0eec67 commit e6beba0
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if (${RC_GIT_VER} EQUAL 0)
endif()

set(SOURCE_FILES_DAEMON Daemon/ydotoold.c)
set(SOURCE_FILES_CLIENT Client/ydotool.c Client/tool_click.c Client/tool_mousemove.c Client/tool_type.c Client/tool_key.c)
set(SOURCE_FILES_CLIENT Client/ydotool.c Client/tool_click.c Client/tool_mousemove.c Client/tool_type.c Client/tool_key.c Client/tool_stdin.c)

add_executable(ydotoold ${SOURCE_FILES_DAEMON})
install(TARGETS ydotoold DESTINATION ${CMAKE_INSTALL_BINDIR})
Expand Down
130 changes: 130 additions & 0 deletions Client/tool_stdin.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#include <string.h>
#include <stdint.h>
#include <termios.h>
#include "ydotool.h"

#define FLAG_UPPERCASE 0x80000000
#define FLAG_CTRL 0x40000000

static const int32_t ascii2keycode_map[128] = {
// 00 - 0f
-1,KEY_A|FLAG_CTRL,KEY_B|FLAG_CTRL,KEY_C|FLAG_CTRL,KEY_D|FLAG_CTRL,KEY_E|FLAG_CTRL,KEY_F|FLAG_CTRL,KEY_G|FLAG_CTRL,
KEY_BACKSPACE,KEY_TAB,KEY_ENTER,KEY_K|FLAG_CTRL,KEY_L|FLAG_CTRL,KEY_M|FLAG_CTRL,KEY_N|FLAG_CTRL,KEY_O|FLAG_CTRL,

// 10 - 1f
KEY_P|FLAG_CTRL,KEY_Q|FLAG_CTRL,KEY_R|FLAG_CTRL,KEY_S|FLAG_CTRL,KEY_T|FLAG_CTRL,KEY_U|FLAG_CTRL,KEY_V|FLAG_CTRL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|FLAG_UPPERCASE,KEY_Y|FLAG_UPPERCASE,KEY_Z|FLAG_UPPERCASE,KEY_LEFTBRACE,KEY_BACKSLASH,KEY_RIGHTBRACE,KEY_6|FLAG_UPPERCASE,KEY_MINUS|FLAG_UPPERCASE,

// 60 - 6f
KEY_GRAVE,KEY_A,KEY_B,KEY_C,KEY_D,KEY_E,KEY_F,KEY_G,
KEY_H,KEY_I,KEY_J,KEY_K,KEY_L,KEY_M,KEY_N,KEY_O,

// 70 - 7f
KEY_P,KEY_Q,KEY_R,KEY_S,KEY_T,KEY_U,KEY_V,KEY_W,
KEY_X,KEY_Y,KEY_Z,KEY_LEFTBRACE|FLAG_UPPERCASE,KEY_BACKSLASH|FLAG_UPPERCASE,KEY_RIGHTBRACE|FLAG_UPPERCASE,KEY_GRAVE|FLAG_UPPERCASE,KEY_BACKSPACE
};

static const int32_t ascii2ctrlcode_map[12] = {
KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_LEFT, -1, -1, -1, -1, -1, -1, KEY_PAGEUP, KEY_PAGEDOWN
};

static int opt_key_delay_ms = 20;
static int opt_key_hold_ms = 20;
static int opt_next_delay_ms = 0;

static struct termios old_tio;

static void restore_terminal() {
// Restore the old terminal attributes
tcsetattr(STDIN_FILENO, TCSANOW, &old_tio);
}

static void handle_signal(int sig) {
if (sig == SIGINT) {
restore_terminal();
exit(0);
}
}

static void configure_terminal() {
struct termios new_tio;

// Get the current terminal attributes
tcgetattr(STDIN_FILENO, &old_tio);

// Set the new attributes for the terminal (non-canonical, no echo)
new_tio = old_tio;
new_tio.c_lflag &= (~ICANON & ~ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &new_tio);

atexit(restore_terminal); // Ensure terminal settings are restored on exit
signal(SIGINT, handle_signal); // Handle CTRL-C
}

int tool_stdin(int argc, char **argv) {
configure_terminal(); // Set terminal to raw mode

printf("Type anything (CTRL-C to exit):\n");

while (1) {
char buffer[4] = {0};
read(STDIN_FILENO, buffer, 3);

printf("Key code: %d %d %d\n", buffer[0], buffer[1], buffer[2]);

char c = buffer[0];

// Convert char to keycode and flags based on the ascii2keycode_map
int kdef = ascii2keycode_map[(int)c];

if ((int)buffer[0] == 27 && (int)buffer[1] == 91 && (int)buffer[2] >= 65 && (int)buffer[2] <= 76) {
kdef = ascii2ctrlcode_map[(int)buffer[2] - 65];
}

if (kdef == -1) continue; // Skip unsupported characters
printf(" Maps to: %d\n", kdef);

uint16_t kc = kdef & 0xffff; // Extract keycode
bool isUppercase = (kdef & FLAG_UPPERCASE) != 0;
bool isCtrl = (kdef & FLAG_CTRL) != 0;

// Emit key events
if (isUppercase) {
printf(" Sending shift\n");
uinput_emit(EV_KEY, KEY_LEFTSHIFT, 1, 1); // Press shift for uppercase
}
if (isCtrl) {
printf(" Sending ctrl\n");
uinput_emit(EV_KEY, KEY_LEFTCTRL, 1, 1); // Press ctrl
}
uinput_emit(EV_KEY, kc, 1, 1); // Key down
usleep(opt_key_hold_ms * 1000); // Hold key
uinput_emit(EV_KEY, kc, 0, 1); // Key up
if (isCtrl) {
uinput_emit(EV_KEY, KEY_LEFTCTRL, 0, 1); // Release ctrl
}
if (isUppercase) {
uinput_emit(EV_KEY, KEY_LEFTSHIFT, 0, 1); // Release shift for uppercase
}

usleep(opt_key_delay_ms * 1000); // Delay between keys
}

return 0;
}
1 change: 1 addition & 0 deletions Client/ydotool.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ static const struct tool_def tool_list[] = {
{"key", tool_key},
{"debug", tool_debug},
{"bakers", tool_bakers},
{"stdin", tool_stdin},
};

static void show_help() {
Expand Down
1 change: 1 addition & 0 deletions Client/ydotool.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,4 @@ extern int tool_click(int argc, char **argv);
extern int tool_mousemove(int argc, char **argv);
extern int tool_type(int argc, char **argv);
extern int tool_key(int argc, char **argv);
extern int tool_stdin(int argc, char **argv);

0 comments on commit e6beba0

Please sign in to comment.