-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
127 lines (108 loc) · 3.35 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <time.h>
typedef struct _key {
guint16 hardward;
guint32 keyval;
guint32 state;
} key;
static clock_t start;
static key keys[] = {{53, GDK_KEY_x, 0},
{33, GDK_KEY_p, 0},
{
28,
GDK_KEY_t,
0,
},
{
58,
GDK_KEY_m,
0,
},
{
53,
GDK_KEY_x,
0,
},
{58, GDK_KEY_m, 0}};
gboolean key_press(GtkWidget *text_view, GdkEventKey *get_ev) {
if (get_ev->send_event) {
if (get_ev->keyval == GDK_KEY_Return) {
clock_t end = clock();
printf("%ld clocks\n", (end - start));
GtkTextBuffer *buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view));
GtkTextIter start_iter, end_iter;
gtk_text_buffer_get_start_iter(buf, &start_iter);
gtk_text_buffer_get_end_iter(buf, &end_iter);
gchar* text = gtk_text_buffer_get_text(buf, &start_iter, &end_iter, TRUE);
printf("%s\n", text);
fflush(stdout);
exit(0);
}
return FALSE;
}
if (get_ev->keyval == GDK_KEY_Return) {
GdkEvent *ev = gdk_event_new(GDK_KEY_PRESS);
ev->key.window = gtk_widget_get_parent_window(text_view);
// enable hangul
{
GdkEventKey *kev = &ev->key;
kev->send_event = TRUE;
kev->hardware_keycode = 102;
kev->keyval = GDK_KEY_Muhenkan;
kev->state = 0;
gdk_event_put(ev);
}
for (int set = 0; set < 40; set++) {
for (int i = 0; i < G_N_ELEMENTS(keys); i++) {
GdkEventKey *kev = &ev->key;
kev->send_event = TRUE;
kev->hardware_keycode = keys[i].hardward;
kev->keyval = keys[i].keyval;
kev->state = keys[i].state;
kev->window = gtk_widget_get_parent_window(text_view);
gdk_event_put(ev);
}
}
// esc
{
GdkEventKey *kev = &ev->key;
kev->send_event = TRUE;
kev->hardware_keycode = 9;
kev->keyval = GDK_KEY_Escape;
kev->state = 0;
gdk_event_put(ev);
}
// enter
{
GdkEventKey *kev = &ev->key;
kev->send_event = TRUE;
kev->hardware_keycode = 36;
kev->keyval = GDK_KEY_Return;
kev->state = 0;
gdk_event_put(ev);
}
gdk_event_free(ev);
start = clock();
return TRUE;
}
return FALSE;
}
void activate(GtkApplication *app, gpointer user_data) {
GtkWidget *window = gtk_application_window_new(GTK_APPLICATION(app));
gtk_window_set_title(GTK_WINDOW(window), "Bench");
GtkWidget *text_view = gtk_text_view_new();
gtk_container_add(GTK_CONTAINER(window), text_view);
gtk_widget_add_events(text_view, GDK_KEY_PRESS_MASK);
gtk_widget_show_all(window);
g_signal_connect(text_view, "key-press-event", G_CALLBACK(key_press), NULL);
}
int main(int argc, char **argv) {
GtkApplication *app = gtk_application_new("github.riey.korean-ime-benchmark",
G_APPLICATION_FLAGS_NONE);
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL);
int status = g_application_run(G_APPLICATION(app), argc, argv);
g_object_unref(app);
return status;
}