Skip to content

Commit

Permalink
cairo-gtk: Add support for key input
Browse files Browse the repository at this point in the history
  • Loading branch information
ringlej committed Oct 5, 2023
1 parent 1cc3bc3 commit 29f7b5c
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 30 deletions.
27 changes: 22 additions & 5 deletions c_src/device/cairo/cairo_gtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,25 @@ static gboolean on_button_event(GtkWidget* widget,
return FALSE;
}

int mods = 0; // TODO: decipher event->state (GdkModifierType)
send_mouse_button(KEYMAP_GDK,
event->button,
action,
event->state,
event->x, event->y);

guint button = event->button - 1;

send_mouse_button(button, action, mods, event->x, event->y);
return TRUE;
}

static gboolean on_key_event(GtkWidget* widget,
GdkEventKey* event,
gpointer data)
{
int action = (event->type == GDK_KEY_PRESS) ? 1 : 0;
uint32_t unicode = gdk_keyval_to_unicode(event->keyval);
send_key(KEYMAP_GDK, event->keyval, event->hardware_keycode, action, event->state);
if (!(event->keyval & 0xF000) && event->type == GDK_KEY_PRESS) {
send_codepoint(KEYMAP_GDK, unicode, event->state);
}
return TRUE;
}

Expand Down Expand Up @@ -134,11 +147,15 @@ int device_init(const device_opts_t* p_opts,
gtk_widget_set_events(g_cairo_gtk.window,
GDK_POINTER_MOTION_MASK |
GDK_BUTTON_PRESS_MASK |
GDK_BUTTON_RELEASE_MASK);
GDK_BUTTON_RELEASE_MASK |
GDK_KEY_PRESS_MASK |
GDK_KEY_RELEASE_MASK);

g_signal_connect(G_OBJECT(g_cairo_gtk.window), "motion-notify-event", G_CALLBACK(on_motion_event), NULL);
g_signal_connect(G_OBJECT(g_cairo_gtk.window), "button-press-event", G_CALLBACK(on_button_event), NULL);
g_signal_connect(G_OBJECT(g_cairo_gtk.window), "button-release-event", G_CALLBACK(on_button_event), NULL);
g_signal_connect(G_OBJECT(g_cairo_gtk.window), "key-press-event", G_CALLBACK(on_key_event), NULL);
g_signal_connect(G_OBJECT(g_cairo_gtk.window), "key-release-event", G_CALLBACK(on_key_event), NULL);

GtkDrawingArea* drawing_area = (GtkDrawingArea*)gtk_drawing_area_new();
gtk_container_add(GTK_CONTAINER(g_cairo_gtk.window), (GtkWidget*)drawing_area);
Expand Down
6 changes: 3 additions & 3 deletions c_src/device/nvg/glfw.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ void reshape_window(GLFWwindow* window, int w, int h)
//---------------------------------------------------------
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
send_key(key, scancode, action, mods);
send_key(KEYMAP_GLFW, key, scancode, action, mods);
}

//---------------------------------------------------------
void charmods_callback(GLFWwindow* window, unsigned int codepoint, int mods)
{
send_codepoint(codepoint, mods);
send_codepoint(KEYMAP_GLFW, codepoint, mods);
}

//---------------------------------------------------------
Expand All @@ -123,7 +123,7 @@ void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
double x, y;
glfwGetCursorPos(window, &x, &y);
send_mouse_button(button, action, mods, x, y);
send_mouse_button(KEYMAP_GLFW, button, action, mods, x, y);
}

//---------------------------------------------------------
Expand Down
14 changes: 9 additions & 5 deletions c_src/scenic/comms.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,29 +241,31 @@ void send_reshape( int window_width, int window_height )
PACK(typedef struct msg_key_t
{
uint32_t msg_id;
uint32_t keymap;
uint32_t key;
uint32_t scancode;
uint32_t action;
uint32_t mods;
}) msg_key_t;

void send_key(int key, int scancode, int action, int mods)
void send_key(keymap_t keymap, int key, int scancode, int action, int mods)
{
msg_key_t msg = { MSG_OUT_KEY, key, scancode, action, mods };
msg_key_t msg = { MSG_OUT_KEY, keymap, key, scancode, action, mods };
write_cmd((byte*) &msg, sizeof(msg_key_t));
}

//---------------------------------------------------------
PACK(typedef struct msg_codepoint_t
{
uint32_t msg_id;
uint32_t keymap;
uint32_t codepoint;
uint32_t mods;
}) msg_codepoint_t;

void send_codepoint(unsigned int codepoint, int mods)
void send_codepoint(keymap_t keymap, unsigned int codepoint, int mods)
{
msg_codepoint_t msg = { MSG_OUT_CODEPOINT, codepoint, mods };
msg_codepoint_t msg = { MSG_OUT_CODEPOINT, keymap, codepoint, mods };
write_cmd((byte*) &msg, sizeof(msg_codepoint_t));
}

Expand All @@ -285,17 +287,19 @@ void send_cursor_pos(float xpos, float ypos)
PACK(typedef struct msg_mouse_button_t
{
uint32_t msg_id;
uint32_t keymap;
uint32_t button;
uint32_t action;
uint32_t mods;
float xpos;
float ypos;
}) msg_mouse_button_t;

void send_mouse_button(int button, int action, int mods, float xpos, float ypos)
void send_mouse_button(keymap_t keymap, int button, int action, int mods, float xpos, float ypos)
{
msg_mouse_button_t msg = {
MSG_OUT_MOUSE_BUTTON,
keymap,
button,
action,
mods,
Expand Down
15 changes: 11 additions & 4 deletions c_src/scenic/comms.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ typedef enum {
_MSG_OUT_SIZE_ = 0XFFFFFFFF,
} msg_out_t;

typedef enum {
KEYMAP_GLFW = 0x01,
KEYMAP_GDK = 0x02,

_KEYMAP_SIZE_ = 0xFFFFFFFF,
} keymap_t;

int read_exact(byte* buf, int len);
int write_exact(byte* buf, int len);
int read_msg_length(struct timeval * ptv);
Expand Down Expand Up @@ -127,11 +134,11 @@ void render(driver_data_t* p_data);

void send_image_miss(unsigned int img_id);

void send_reshape( int window_width, int window_height );
void send_key(int key, int scancode, int action, int mods);
void send_codepoint(unsigned int codepoint, int mods);
void send_reshape(int window_width, int window_height);
void send_key(keymap_t keymap, int key, int scancode, int action, int mods);
void send_codepoint(keymap_t keymap, unsigned int codepoint, int mods);
void send_cursor_pos(float xpos, float ypos);
void send_mouse_button(int button, int action, int mods, float xpos,
void send_mouse_button(keymap_t keymap, int button, int action, int mods, float xpos,
float ypos);
void send_scroll(float xoffset, float yoffset, float xpos, float ypos);
void send_cursor_enter(int entered, float xpos, float ypos);
Expand Down
Loading

0 comments on commit 29f7b5c

Please sign in to comment.