Move the text input callback to C

This commit is contained in:
Kovid Goyal 2017-09-15 18:33:45 +05:30
parent ddbe0703a3
commit a4d71bcf5c
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
5 changed files with 35 additions and 30 deletions

View File

@ -15,10 +15,7 @@ from .fast_data_types import (
layout_sprite_map
)
from .fonts.render import render_cell_wrapper, set_font_family
from .keys import (
get_key_map, get_sent_data, get_shortcut, interpret_key_event,
interpret_text_event
)
from .keys import get_key_map, get_sent_data, get_shortcut, interpret_key_event
from .session import create_session
from .tabs import SpecialWindow, TabManager
from .utils import (
@ -75,7 +72,6 @@ class Boss:
self.opts, self.args = opts, args
self.glfw_window = glfw_window
glfw_window.framebuffer_size_callback = self.on_window_resize
glfw_window.char_mods_callback = self.on_text_input
glfw_window.key_callback = self.on_key
glfw_window.window_focus_callback = self.on_focus
load_shader_programs()
@ -175,13 +171,6 @@ class Boss:
if t is not None:
return t.active_window
def on_text_input(self, window, codepoint, mods):
w = self.active_window
if w is not None:
data = interpret_text_event(codepoint, mods, w)
if data:
w.write_to_child(data)
def on_key(self, window, key, scancode, action, mods):
func = None
if action == GLFW_PRESS or action == GLFW_REPEAT:

View File

@ -318,3 +318,4 @@ void set_mouse_cursor(MouseShape);
void mouse_event(int, int);
void scroll_event(double, double);
void set_special_key_combo(int glfw_key, int mods);
void on_text_input(unsigned int codepoint, int mods);

View File

@ -36,7 +36,7 @@ typedef struct {
PyObject_HEAD
GLFWwindow *window;
PyObject *framebuffer_size_callback, *char_mods_callback, *key_callback, *window_focus_callback;
PyObject *framebuffer_size_callback, *key_callback, *window_focus_callback;
GLFWcursor *standard_cursor, *click_cursor, *arrow_cursor;
} WindowWrapper;
@ -57,7 +57,7 @@ framebuffer_size_callback(GLFWwindow UNUSED *w, int width, int height) {
static void
char_mods_callback(GLFWwindow UNUSED *w, unsigned int codepoint, int mods) {
global_state.cursor_blink_zero_time = monotonic();
WINDOW_CALLBACK(char_mods_callback, "Ii", codepoint, mods);
on_text_input(codepoint, mods);
}
static void
@ -259,7 +259,7 @@ glfw_init_hint_string(PyObject UNUSED *self, PyObject *args) {
static void
dealloc(WindowWrapper* self) {
the_window = NULL;
Py_CLEAR(self->framebuffer_size_callback); Py_CLEAR(self->char_mods_callback); Py_CLEAR(self->key_callback); Py_CLEAR(self->window_focus_callback);
Py_CLEAR(self->framebuffer_size_callback); Py_CLEAR(self->key_callback); Py_CLEAR(self->window_focus_callback);
if (self->window != NULL) glfwDestroyWindow(self->window);
Py_TYPE(self)->tp_free((PyObject*)self);
}
@ -446,7 +446,6 @@ static PyMethodDef methods[] = {
static PyMemberDef members[] = {
#define CBE(name) {#name, T_OBJECT_EX, offsetof(WindowWrapper, name), 0, #name}
CBE(framebuffer_size_callback),
CBE(char_mods_callback),
CBE(key_callback),
CBE(window_focus_callback),
{NULL}

View File

@ -6,7 +6,8 @@
*/
#include "keys.h"
#include "data-types.h"
#include "state.h"
#include <GLFW/glfw3.h>
const uint8_t*
key_to_bytes(int glfw_key, bool smkx, bool extended, int mods, int action) {
@ -29,6 +30,32 @@ set_special_key_combo(int glfw_key, int mods) {
needs_special_handling[k] = true;
}
static inline Window*
active_window() {
Tab *t = global_state.tabs + global_state.active_tab;
Window *w = t->windows + t->active_window;
if (w->render_data.screen) return w;
return NULL;
}
void
on_text_input(unsigned int codepoint, int mods) {
Window *w = active_window();
static char buf[10];
if (w != NULL) {
Screen *screen = w->render_data.screen;
bool handle_event = (
mods <= GLFW_MOD_SHIFT ||
(!screen->modes.mEXTENDED_KEYBOARD && (mods == GLFW_MOD_ALT || mods == (GLFW_MOD_ALT | GLFW_MOD_SHIFT)))
) ? true : false; // non text input is handle in on_key_input
if (handle_event) {
unsigned int sz = encode_utf8(codepoint, buf);
if (sz) schedule_write_to_child(w->id, buf, sz);
}
}
}
#define PYWRAP1(name) static PyObject* py##name(PyObject UNUSED *self, PyObject *args)
#define PA(fmt, ...) if(!PyArg_ParseTuple(args, fmt, __VA_ARGS__)) return NULL;
#define M(name, arg_type) {#name, (PyCFunction)py##name, arg_type, NULL}

View File

@ -146,7 +146,7 @@ action_map = {
def extended_key_event(key, mods, action):
if key >= defines.GLFW_KEY_LAST or key == defines.GLFW_KEY_UNKNOWN or (
# Shifted printable key should be handled by interpret_text_event()
# Shifted printable key should be handled by on_text_input()
mods == defines.GLFW_MOD_SHIFT and 32 <= key <= 126
):
return b''
@ -170,7 +170,7 @@ def key_to_bytes(key, smkx, extended, mods, action):
# Map Ctrl-key to ascii control code
data.extend(control_codes[key])
elif mods in alt_mods and key in alt_codes:
# Printable keys handled by interpret_text_event()
# Printable keys handled by on_text_input()
data.extend((alt_codes if mods == defines.GLFW_MOD_ALT else shift_alt_codes)[key])
else:
key_map = cursor_key_mode_map[smkx]
@ -194,17 +194,6 @@ def interpret_key_event(key, scancode, mods, window, action, get_localized_key=g
return b''
def interpret_text_event(codepoint, mods, window):
screen = window.screen
if mods > defines.GLFW_MOD_SHIFT:
if mods in alt_mods and not screen.extended_keyboard:
data = chr(codepoint).encode('utf-8')
return b'\x1b' + data
return b'' # Handled by interpret_key_event above
data = chr(codepoint).encode('utf-8')
return data
def get_shortcut(keymap, mods, key, scancode):
key = get_localized_key(key, scancode)
return keymap.get((mods & 0b1111, key))