| 1 | |
| 2 | typedef struct { |
| 3 | GtkIMContext *context; |
| 4 | WebKitWebView *web; |
| 5 | char *owner; |
| 6 | gboolean composing, resetting; |
| 7 | guint end_idle; |
| 8 | } CageNativeInput; |
| 9 | |
| 10 | static CageNativeInput native_input; |
| 11 | |
| 12 | static void input_emit(const char *kind, const char *text, int start, int end) { |
| 13 | if (!native_input.owner) return; |
| 14 | JsonObject *event = json_object_new(); |
| 15 | json_object_set_string_member(event, "owner", native_input.owner); |
| 16 | json_object_set_string_member(event, "kind", kind); |
| 17 | if (text) json_object_set_string_member(event, "text", text); |
| 18 | json_object_set_int_member(event, "selectionStart", start); |
| 19 | json_object_set_int_member(event, "selectionEnd", end); |
| 20 | JsonNode *node = json_node_new(JSON_NODE_OBJECT); |
| 21 | json_node_take_object(node, event); |
| 22 | JsonGenerator *generator = json_generator_new(); |
| 23 | json_generator_set_root(generator, node); |
| 24 | char *json = json_generator_to_data(generator, NULL); |
| 25 | char *script = g_strdup_printf("window.dispatchEvent(new CustomEvent('cage-native-input',{detail:%s}));", json); |
| 26 | webkit_web_view_evaluate_javascript(native_input.web, script, -1, NULL, NULL, NULL, NULL, NULL); |
| 27 | g_free(script); g_free(json); json_node_free(node); g_object_unref(generator); |
| 28 | } |
| 29 | |
| 30 | static void input_cancel(void) { |
| 31 | if (native_input.end_idle) { g_source_remove(native_input.end_idle); native_input.end_idle = 0; } |
| 32 | if (native_input.composing) input_emit("compositionend", "", 0, 0); |
| 33 | native_input.composing = FALSE; |
| 34 | native_input.resetting = TRUE; |
| 35 | gtk_im_context_reset(native_input.context); |
| 36 | native_input.resetting = FALSE; |
| 37 | } |
| 38 | |
| 39 | static int input_utf16_length(const char *text, int characters) { |
| 40 | int length = 0; |
| 41 | for (const char *p = text; *p && characters-- > 0; p = g_utf8_next_char(p)) |
| 42 | length += g_utf8_get_char(p) > 0xffff ? 2 : 1; |
| 43 | return length; |
| 44 | } |
| 45 | |
| 46 | static void input_preedit(GtkIMContext *context, gpointer data) { |
| 47 | (void)data; |
| 48 | if (!native_input.owner || native_input.resetting) return; |
| 49 | char *text = NULL; |
| 50 | PangoAttrList *attributes = NULL; |
| 51 | int cursor = 0; |
| 52 | gtk_im_context_get_preedit_string(context, &text, &attributes, &cursor); |
| 53 | if (text && *text) { |
| 54 | if (native_input.end_idle) { g_source_remove(native_input.end_idle); native_input.end_idle = 0; } |
| 55 | if (!native_input.composing) { native_input.composing = TRUE; input_emit("compositionstart", NULL, 0, 0); } |
| 56 | int end = input_utf16_length(text, cursor); |
| 57 | input_emit("compositionupdate", text, end, end); |
| 58 | } else if (native_input.composing) { |
| 59 | |
| 60 | |
| 61 | } |
| 62 | if (attributes) pango_attr_list_unref(attributes); |
| 63 | g_free(text); |
| 64 | } |
| 65 | |
| 66 | static gboolean input_preedit_ended(gpointer data) { |
| 67 | (void)data; |
| 68 | native_input.end_idle = 0; |
| 69 | if (native_input.composing) { native_input.composing = FALSE; input_emit("compositionend", "", 0, 0); } |
| 70 | return G_SOURCE_REMOVE; |
| 71 | } |
| 72 | static void input_preedit_end(GtkIMContext *context, gpointer data) { |
| 73 | (void)context; (void)data; |
| 74 | if (!native_input.end_idle) native_input.end_idle = g_idle_add(input_preedit_ended, NULL); |
| 75 | } |
| 76 | static void input_commit(GtkIMContext *context, const char *text, gpointer data) { |
| 77 | (void)context; (void)data; |
| 78 | if (!native_input.owner || native_input.resetting) return; |
| 79 | if (native_input.end_idle) { g_source_remove(native_input.end_idle); native_input.end_idle = 0; } |
| 80 | input_emit(native_input.composing ? "compositionend" : "input", text, 0, 0); |
| 81 | native_input.composing = FALSE; |
| 82 | } |
| 83 | static gboolean input_key(GtkWidget *widget, GdkEventKey *event, gpointer data) { |
| 84 | (void)widget; (void)data; |
| 85 | return native_input.owner && gtk_im_context_filter_keypress(native_input.context, event); |
| 86 | } |
| 87 | static gboolean input_focus_out(GtkWidget *widget, GdkEventFocus *event, gpointer data) { |
| 88 | (void)widget; (void)event; (void)data; |
| 89 | if (native_input.owner) { |
| 90 | input_cancel(); input_emit("deactivated", NULL, 0, 0); |
| 91 | gtk_im_context_focus_out(native_input.context); |
| 92 | g_clear_pointer(&native_input.owner, g_free); |
| 93 | } |
| 94 | return FALSE; |
| 95 | } |
| 96 | |
| 97 | static void input_initialize(WebKitWebView *web) { |
| 98 | native_input.web = web; |
| 99 | native_input.context = gtk_im_multicontext_new(); |
| 100 | gtk_im_context_set_use_preedit(native_input.context, TRUE); |
| 101 | g_signal_connect(native_input.context, "preedit-changed", G_CALLBACK(input_preedit), NULL); |
| 102 | g_signal_connect(native_input.context, "preedit-end", G_CALLBACK(input_preedit_end), NULL); |
| 103 | g_signal_connect(native_input.context, "commit", G_CALLBACK(input_commit), NULL); |
| 104 | g_signal_connect(web, "key-press-event", G_CALLBACK(input_key), NULL); |
| 105 | g_signal_connect(web, "key-release-event", G_CALLBACK(input_key), NULL); |
| 106 | g_signal_connect(web, "focus-out-event", G_CALLBACK(input_focus_out), NULL); |
| 107 | } |
| 108 | |
| 109 | static gboolean input_handle(JsonObject *request) { |
| 110 | const char *operation = json_object_has_member(request, "operation") ? json_object_get_string_member(request, "operation") : ""; |
| 111 | const char *owner = json_object_has_member(request, "owner") ? json_object_get_string_member(request, "owner") : ""; |
| 112 | if (!g_uuid_string_is_valid(owner)) return FALSE; |
| 113 | if (!strcmp(operation, "activate")) { |
| 114 | if (g_strcmp0(owner, native_input.owner)) { input_cancel(); g_free(native_input.owner); native_input.owner = g_strdup(owner); } |
| 115 | gtk_im_context_set_client_window(native_input.context, gtk_widget_get_window(GTK_WIDGET(native_input.web))); |
| 116 | gtk_im_context_focus_in(native_input.context); |
| 117 | } else if (g_strcmp0(owner, native_input.owner)) return FALSE; |
| 118 | if (!strcmp(operation, "activate") || !strcmp(operation, "caret")) { |
| 119 | double scale = webkit_web_view_get_zoom_level(native_input.web); |
| 120 | double x = json_object_get_double_member_with_default(request, "x", 0); |
| 121 | double y = json_object_get_double_member_with_default(request, "y", 0); |
| 122 | double height = json_object_get_double_member_with_default(request, "height", 24); |
| 123 | if (!isfinite(x) || !isfinite(y) || !isfinite(height) || height <= 0 || fabs(x) > 32768 || fabs(y) > 32768 || height > 4096) return FALSE; |
| 124 | GdkRectangle caret = {(int)(x * scale), (int)(y * scale), 0, MAX(1, (int)(height * scale))}; |
| 125 | gtk_im_context_set_cursor_location(native_input.context, &caret); |
| 126 | } else if (!strcmp(operation, "cancel")) input_cancel(); |
| 127 | else if (!strcmp(operation, "deactivate")) { |
| 128 | input_cancel(); gtk_im_context_focus_out(native_input.context); |
| 129 | g_clear_pointer(&native_input.owner, g_free); |
| 130 | } else return FALSE; |
| 131 | return TRUE; |
| 132 | } |