Add skeleton ChimaraBrowser class
[projects/chimara/chimara.git] / libchimara / input.c
1 #include "charset.h"
2 #include "magic.h"
3 #include "input.h"
4 #include "pager.h"
5 #include "chimara-glk-private.h"
6 #include "garglk.h"
7
8 extern GPrivate *glk_data_key;
9
10 /* Forward declarations */
11 static int finish_text_buffer_line_input(winid_t win, gboolean emit_signal);
12 static int finish_text_grid_line_input(winid_t win, gboolean emit_signal);
13 static void cancel_old_input_request(winid_t win);
14
15 /* Internal function: code common to both flavors of char event request */
16 void
17 request_char_event_common(winid_t win, gboolean unicode)
18 {
19         VALID_WINDOW(win, return);
20         g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
21
22         cancel_old_input_request(win);
23
24         flush_window_buffer(win);
25
26         if(win->type == wintype_TextBuffer) {
27                 /* Move the input_position mark to the end of the window_buffer */
28                 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
29                 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
30                 GtkTextIter end_iter;
31                 gtk_text_buffer_get_end_iter(buffer, &end_iter);
32                 gtk_text_buffer_move_mark(buffer, input_position, &end_iter);
33         }
34
35
36         win->input_request_type = unicode? INPUT_REQUEST_CHARACTER_UNICODE : INPUT_REQUEST_CHARACTER;
37         g_signal_handler_unblock( win->widget, win->char_input_keypress_handler );
38
39         gdk_threads_enter();
40         gtk_widget_grab_focus( GTK_WIDGET(win->widget) );
41         gdk_threads_leave();
42
43         /* Emit the "waiting" signal to let listeners know we are ready for input */
44         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
45         g_signal_emit_by_name(glk_data->self, "waiting");
46 }
47
48 /**
49  * glk_request_char_event:
50  * @win: A window to request char events from.
51  *
52  * Request input of a Latin-1 character or special key. A window cannot have
53  * requests for both character and line input at the same time. Nor can it have
54  * requests for character input of both types (Latin-1 and Unicode). It is
55  * illegal to call glk_request_char_event() if the window already has a pending
56  * request for either character or line input.
57  */
58 void
59 glk_request_char_event(winid_t win)
60 {
61         request_char_event_common(win, FALSE);
62 }
63
64 /**
65  * glk_request_char_event_uni:
66  * @win: A window to request char events from.
67  *
68  * Request input of a Unicode character or special key. See
69  * glk_request_char_event().
70  */
71 void
72 glk_request_char_event_uni(winid_t win)
73 {
74         request_char_event_common(win, TRUE);
75 }
76
77 /**
78  * glk_cancel_char_event:
79  * @win: A window to cancel the latest char event request on.
80  *
81  * This cancels a pending request for character input. (Either Latin-1 or
82  * Unicode.) For convenience, it is legal to call glk_cancel_char_event() even
83  * if there is no charcter input request on that window. Glk will ignore the
84  * call in this case.
85  */
86 void
87 glk_cancel_char_event(winid_t win)
88 {
89         VALID_WINDOW(win, return);
90         g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
91
92         if(win->input_request_type == INPUT_REQUEST_CHARACTER || win->input_request_type == INPUT_REQUEST_CHARACTER_UNICODE)
93         {
94                 win->input_request_type = INPUT_REQUEST_NONE;
95                 g_signal_handler_block( win->widget, win->char_input_keypress_handler );
96         }
97 }
98
99 /* Internal function: Request either latin-1 or unicode line input, in a text grid window. */
100 static void
101 text_grid_request_line_event_common(winid_t win, glui32 maxlen, gboolean insert, gchar *inserttext)
102 {
103         /* All outstanding printing _must_ be finished before putting an input entry
104          into the buffer */
105         flush_window_buffer(win);
106
107         gdk_threads_enter();
108
109         GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
110
111     GtkTextMark *cursor = gtk_text_buffer_get_mark(buffer, "cursor_position");
112     GtkTextIter start_iter, end_iter;
113     gtk_text_buffer_get_iter_at_mark(buffer, &start_iter, cursor);
114
115     /* Determine the maximum length of the line input */
116     gint cursorpos = gtk_text_iter_get_line_offset(&start_iter);
117     /* Odd; the Glk spec says the maximum input length is
118     windowwidth - 1 - cursorposition. I say no, because if cursorposition is
119     zero, then the input should fill the whole line. FIXME??? */
120     win->input_length = MIN(win->width - cursorpos, win->line_input_buffer_max_len);
121     end_iter = start_iter;
122     gtk_text_iter_set_line_offset(&end_iter, cursorpos + win->input_length);
123
124         /* If the buffer currently has a selection with one bound in the middle of
125         the input field, then deselect it. Otherwise the input field gets trashed */
126         GtkTextIter start_sel, end_sel;
127         if( gtk_text_buffer_get_selection_bounds(buffer, &start_sel, &end_sel) )
128         {
129                 if( gtk_text_iter_in_range(&start_sel, &start_iter, &end_iter) )
130                         gtk_text_buffer_place_cursor(buffer, &end_sel);
131                 if( gtk_text_iter_in_range(&end_sel, &start_iter, &end_iter) )
132                         gtk_text_buffer_place_cursor(buffer, &start_sel);
133         }
134
135     /* Erase the text currently in the input field and replace it with a GtkEntry */
136     gtk_text_buffer_delete(buffer, &start_iter, &end_iter);
137     win->input_anchor = gtk_text_buffer_create_child_anchor(buffer, &start_iter);
138     win->input_entry = gtk_entry_new();
139         /* Set the entry's font to match that of the window */
140     GtkRcStyle *style = gtk_widget_get_modifier_style(win->widget);     /* Don't free */
141         gtk_widget_modify_font(win->input_entry, style->font_desc);
142         /* Make the entry as small as possible to fit with the text */
143         gtk_entry_set_has_frame(GTK_ENTRY(win->input_entry), FALSE);
144         GtkBorder border = { 0, 0, 0, 0 };
145
146         /* COMPAT: */
147 #if GTK_CHECK_VERSION(2,10,0)
148         gtk_entry_set_inner_border(GTK_ENTRY(win->input_entry), &border);
149 #endif
150     gtk_entry_set_max_length(GTK_ENTRY(win->input_entry), win->input_length);
151     gtk_entry_set_width_chars(GTK_ENTRY(win->input_entry), win->input_length);
152
153     /* Insert pre-entered text if needed */
154     if(insert)
155         gtk_entry_set_text(GTK_ENTRY(win->input_entry), inserttext);
156
157     /* Set background color of entry (TODO: implement as property) */
158     GdkColor background;
159         gdk_color_parse("grey", &background);
160     gtk_widget_modify_base(win->input_entry, GTK_STATE_NORMAL, &background);
161
162     g_signal_connect(win->input_entry, "activate", G_CALLBACK(on_input_entry_activate), win);
163     g_signal_connect(win->input_entry, "key-press-event", G_CALLBACK(on_input_entry_key_press_event), win);
164     win->line_input_entry_changed = g_signal_connect(win->input_entry, "changed", G_CALLBACK(on_input_entry_changed), win);
165
166     gtk_widget_show(win->input_entry);
167     gtk_text_view_add_child_at_anchor(GTK_TEXT_VIEW(win->widget), win->input_entry, win->input_anchor);
168
169         gtk_widget_grab_focus(win->input_entry);
170
171         gdk_threads_leave();
172 }
173
174 /* Internal function: Request either latin-1 or unicode line input, in a text buffer window. */
175 static void
176 text_buffer_request_line_event_common(winid_t win, glui32 maxlen, gboolean insert, gchar *inserttext)
177 {
178         flush_window_buffer(win);
179
180         gdk_threads_enter();
181
182         GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
183
184     /* Move the input_position mark to the end of the window_buffer */
185     GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
186     GtkTextIter end_iter;
187     gtk_text_buffer_get_end_iter(buffer, &end_iter);
188     gtk_text_buffer_move_mark(buffer, input_position, &end_iter);
189
190     /* Set the entire contents of the window_buffer as uneditable
191      * (so input can only be entered at the end) */
192     GtkTextIter start_iter;
193     gtk_text_buffer_get_start_iter(buffer, &start_iter);
194     gtk_text_buffer_remove_tag_by_name(buffer, "uneditable", &start_iter, &end_iter);
195     gtk_text_buffer_apply_tag_by_name(buffer, "uneditable", &start_iter, &end_iter);
196
197     /* Insert pre-entered text if needed */
198     if(insert) {
199         gtk_text_buffer_insert(buffer, &end_iter, inserttext, -1);
200                 gtk_text_buffer_get_end_iter(buffer, &end_iter); /* update after text insertion */
201         }
202
203         /* Apply the correct style to the input prompt */
204         GtkTextIter input_iter;
205     gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
206     gtk_text_buffer_apply_tag_by_name(buffer, "default", &input_iter, &end_iter);
207     gtk_text_buffer_apply_tag_by_name(buffer, "input", &input_iter, &end_iter);
208     gtk_text_buffer_apply_tag_by_name(buffer, "glk-input", &input_iter, &end_iter);
209
210     gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), TRUE);
211
212         g_signal_handler_unblock(buffer, win->insert_text_handler);
213         gtk_widget_grab_focus(win->widget);
214
215         gdk_threads_leave();
216 }
217
218 /**
219  * glk_request_line_event:
220  * @win: A text buffer or text grid window to request line input on.
221  * @buf: A buffer of at least @maxlen bytes.
222  * @maxlen: Length of the buffer.
223  * @initlen: The number of characters in @buf to pre-enter.
224  *
225  * Requests input of a line of Latin-1 characters. A window cannot have requests
226  * for both character and line input at the same time. Nor can it have requests
227  * for line input of both types (Latin-1 and Unicode). It is illegal to call
228  * glk_request_line_event() if the window already has a pending request for
229  * either character or line input.
230  *
231  * The @buf argument is a pointer to space where the line input will be stored.
232  * (This may not be %NULL.) @maxlen is the length of this space, in bytes; the
233  * library will not accept more characters than this. If @initlen is nonzero,
234  * then the first @initlen bytes of @buf will be entered as pre-existing input
235  * — just as if the player had typed them himself. (The player can continue
236  * composing after this pre-entered input, or delete it or edit as usual.)
237  *
238  * The contents of the buffer are undefined until the input is completed (either
239  * by a line input event, or glk_cancel_line_event(). The library may or may not
240  * fill in the buffer as the player composes, while the input is still pending;
241  * it is illegal to change the contents of the buffer yourself.
242  */
243 void
244 glk_request_line_event(winid_t win, char *buf, glui32 maxlen, glui32 initlen)
245 {
246         VALID_WINDOW(win, return);
247         g_return_if_fail(buf);
248         g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
249         g_return_if_fail(initlen <= maxlen);
250
251         cancel_old_input_request(win);
252
253         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
254
255         /* Register the buffer */
256         if(glk_data->register_arr)
257         win->buffer_rock = (*glk_data->register_arr)(buf, maxlen, "&+#!Cn");
258
259         win->input_request_type = INPUT_REQUEST_LINE;
260         win->line_input_buffer = buf;
261         win->line_input_buffer_max_len = maxlen;
262         win->echo_current_line_input = win->echo_line_input;
263         win->current_extra_line_terminators = g_slist_copy(win->extra_line_terminators);
264
265         gchar *inserttext = (initlen > 0)? g_strndup(buf, initlen) : g_strdup("");
266         switch(win->type)
267         {
268             case wintype_TextBuffer:
269                 text_buffer_request_line_event_common(win, maxlen, (initlen > 0), inserttext);
270                 break;
271             case wintype_TextGrid:
272                 text_grid_request_line_event_common(win, maxlen, (initlen > 0), inserttext);
273                 break;
274     }
275         g_free(inserttext);
276         g_signal_handler_unblock(win->widget, win->line_input_keypress_handler);
277
278         /* Emit the "waiting" signal to let listeners know we are ready for input */
279         g_signal_emit_by_name(glk_data->self, "waiting");
280 }
281
282 /**
283  * glk_request_line_event_uni:
284  * @win: A text buffer or text grid window to request line input on.
285  * @buf: A buffer of at least @maxlen characters.
286  * @maxlen: Length of the buffer.
287  * @initlen: The number of characters in @buf to pre-enter.
288  *
289  * Request input of a line of Unicode characters. This works the same as
290  * glk_request_line_event(), except the result is stored in an array of
291  * <type>glui32</type> values instead of an array of characters, and the values
292  * may be any valid Unicode code points.
293  *
294  * If possible, the library should return fully composed Unicode characters,
295  * rather than strings of base and composition characters.
296  *
297  * <note><para>
298  *   Fully-composed characters are the norm for Unicode text, so an
299  *   implementation that ignores this issue will probably produce the right
300  *   result. However, the game may not want to rely on that. Another factor is
301  *   that case-folding can (occasionally) produce non-normalized text.
302  *   Therefore, to cover all its bases, a game should call
303  *   glk_buffer_to_lower_case_uni(), followed by
304  *   glk_buffer_canon_normalize_uni(), before parsing.
305  * </para></note>
306  *
307  * <note><para>
308  *   Earlier versions of this spec said that line input must always be in
309  *   Unicode Normalization Form C. However, this has not been universally
310  *   implemented. It is also somewhat redundant, for the results noted above.
311  *   Therefore, we now merely recommend that line input be fully composed. The
312  *   game is ultimately responsible for all case-folding and normalization. See
313  *   <link linkend="chimara-Unicode-String-Normalization">Unicode String
314  *   Normalization</link>.
315  * </para></note>
316  */
317 void
318 glk_request_line_event_uni(winid_t win, glui32 *buf, glui32 maxlen, glui32 initlen)
319 {
320         VALID_WINDOW(win, return);
321         g_return_if_fail(buf);
322         g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
323         g_return_if_fail(initlen <= maxlen);
324
325         cancel_old_input_request(win);
326         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
327
328         /* Register the buffer */
329         if(glk_data->register_arr)
330         win->buffer_rock = (*glk_data->register_arr)(buf, maxlen, "&+#!Iu");
331
332         win->input_request_type = INPUT_REQUEST_LINE_UNICODE;
333         win->line_input_buffer_unicode = buf;
334         win->line_input_buffer_max_len = maxlen;
335         win->echo_current_line_input = win->echo_line_input;
336         win->current_extra_line_terminators = g_slist_copy(win->extra_line_terminators);
337
338         gchar *utf8;
339         if(initlen > 0) {
340                 utf8 = convert_ucs4_to_utf8(buf, initlen);
341                 if(utf8 == NULL)
342                         return;
343         }
344         else
345                 utf8 = g_strdup("");
346
347     switch(win->type)
348         {
349             case wintype_TextBuffer:
350                 text_buffer_request_line_event_common(win, maxlen, (initlen > 0), utf8);
351                 break;
352             case wintype_TextGrid:
353                 text_grid_request_line_event_common(win, maxlen, (initlen > 0), utf8);
354                 break;
355     }
356     g_signal_handler_unblock(win->widget, win->line_input_keypress_handler);
357         g_free(utf8);
358
359         /* Emit the "waiting" signal to let listeners know we are ready for input */
360         g_signal_emit_by_name(glk_data->self, "waiting");
361 }
362
363 /**
364  * glk_cancel_line_event:
365  * @win: A text buffer or text grid window to cancel line input on.
366  * @event: Will be filled in if the user had already input something.
367  *
368  * This cancels a pending request for line input. (Either Latin-1 or Unicode.)
369  *
370  * The event pointed to by the event argument will be filled in as if the
371  * player had hit <keycap>enter</keycap>, and the input composed so far will be
372  * stored in the buffer; see below. If you do not care about this information,
373  * pass %NULL as the @event argument. (The buffer will still be filled.)
374  *
375  * For convenience, it is legal to call glk_cancel_line_event() even if there
376  * is no line input request on that window. The event type will be set to
377  * %evtype_None in this case.
378  */
379 void
380 glk_cancel_line_event(winid_t win, event_t *event)
381 {
382         VALID_WINDOW(win, return);
383         g_return_if_fail(win->type != wintype_TextBuffer || win->type != wintype_TextGrid);
384
385         if(event != NULL) {
386                 event->type = evtype_None;
387                 event->win = win;
388                 event->val1 = 0;
389                 event->val2 = 0;
390         }
391
392         if(win->input_request_type != INPUT_REQUEST_LINE && win->input_request_type != INPUT_REQUEST_LINE_UNICODE)
393                 return;
394
395         g_signal_handler_block( win->widget, win->line_input_keypress_handler );
396
397         int chars_written = 0;
398
399         gdk_threads_enter();
400         if(win->type == wintype_TextGrid) {
401                 chars_written = finish_text_grid_line_input(win, FALSE);
402         } else if(win->type == wintype_TextBuffer) {
403                 gtk_text_view_set_editable( GTK_TEXT_VIEW(win->widget), FALSE );
404                 GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
405                 g_signal_handler_block(window_buffer, win->insert_text_handler);
406                 chars_written = finish_text_buffer_line_input(win, FALSE);
407         }
408         gdk_threads_leave();
409
410         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
411         if(glk_data->unregister_arr)
412         {
413                 if(win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
414                         (*glk_data->unregister_arr)(win->line_input_buffer_unicode, win->line_input_buffer_max_len, "&+#!Iu", win->buffer_rock);
415                 else
416                 (*glk_data->unregister_arr)(win->line_input_buffer, win->line_input_buffer_max_len, "&+#!Cn", win->buffer_rock);
417     }
418
419         if(event != NULL && chars_written > 0) {
420                 event->type = evtype_LineInput;
421                 event->val1 = chars_written;
422         }
423 }
424
425 /* Helper function: Turn off shutdown key-press-event signal handler */
426 static gboolean
427 turn_off_handler(GNode *node)
428 {
429         winid_t win = node->data;
430         g_signal_handler_block(win->widget, win->shutdown_keypress_handler);
431         return FALSE; /* don't stop */
432 }
433
434 /* Internal function: Callback for signal key-press-event while waiting for shutdown. */
435 gboolean
436 on_shutdown_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
437 {
438         ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(widget, CHIMARA_TYPE_GLK));
439         g_assert(glk);
440         CHIMARA_GLK_USE_PRIVATE(glk, priv);
441         
442         /* Turn off all the signal handlers */
443         if(priv->root_window)
444                 g_node_traverse(priv->root_window, G_IN_ORDER, G_TRAVERSE_LEAVES, -1, (GNodeTraverseFunc)turn_off_handler, NULL);
445         
446         /* Signal the Glk library that it can shut everything down now */
447         g_mutex_lock(priv->shutdown_lock);
448         g_cond_signal(priv->shutdown_key_pressed);
449         g_mutex_unlock(priv->shutdown_lock);
450         
451         return TRUE; /* block the event */
452 }
453
454 /* Internal function: General callback for signal key-press-event on a text buffer or text grid window. Used in character input on both text buffers and grids. Blocked when not in use. */
455 gboolean
456 on_char_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
457 {
458         /* Ignore modifier keys, otherwise the char input will already trigger on 
459         the shift key when the user tries to type a capital letter */
460         if(event->is_modifier)
461                 return FALSE; /* don't stop the event */
462
463         /* All text up to the input position is now regarded as being read by the user */
464         if(win->type == wintype_TextBuffer)
465                 pager_update(win);
466         
467         glui32 keycode = keyval_to_glk_keycode(event->keyval, win->input_request_type == INPUT_REQUEST_CHARACTER_UNICODE);
468
469         ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(widget, CHIMARA_TYPE_GLK));
470         g_assert(glk);
471         event_throw(glk, evtype_CharInput, win, keycode, 0);
472         g_signal_emit_by_name(glk, "char-input", win->rock, event->keyval);
473
474         /* Only one keypress will be handled */
475         win->input_request_type = INPUT_REQUEST_NONE;
476         g_signal_handler_block(win->widget, win->char_input_keypress_handler);
477
478         return TRUE;
479 }
480
481 gboolean
482 on_line_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win)
483 {
484         switch(win->type)
485         {
486                 case wintype_TextBuffer:
487                 {
488                         GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
489                         GtkTextMark *input_position_mark = gtk_text_buffer_get_mark(buffer, "input_position");
490                         GtkTextIter input_position_iter;
491                         gtk_text_buffer_get_iter_at_mark(buffer, &input_position_iter, input_position_mark);
492                         GtkTextIter end_iter;
493                         gtk_text_buffer_get_end_iter(buffer, &end_iter);
494
495                         /* Check whether the cursor is at the prompt or somewhere else in the text */
496                         GtkTextIter selection_start, selection_end;
497                         gtk_text_buffer_get_selection_bounds(buffer, &selection_start, &selection_end);
498                         if(gtk_text_iter_compare(&selection_start, &input_position_iter) < 0) {
499                                 // Cursor is somewhere else in the text, place it at the end if the user starts typing
500                                 if(event->keyval >= GDK_space && event->keyval <= GDK_asciitilde) {
501                                         gtk_text_buffer_place_cursor(buffer, &end_iter);
502                                 } else {
503                                         // User is walking around, let him be.
504                                         return FALSE;
505                                 }
506                         }
507
508                         /* All text up to the input position is now regarded as being read by the user */
509                         pager_update(win);
510
511                         /* History up/down */
512                         if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up
513                                 || event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
514                         {
515                                 /* Prevent falling off the end of the history list */
516                                 if(win->history == NULL)
517                                         return TRUE;
518                                 if( (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
519                                         && win->history_pos && win->history_pos->next == NULL)
520                                         return TRUE;
521                                 if( (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
522                                         && (win->history_pos == NULL || win->history_pos->prev == NULL) )
523                                         return TRUE;
524
525                                 /* Erase any text that was already typed */
526                                 if(win->history_pos == NULL) {
527                                         gchar *current_input = gtk_text_buffer_get_text(buffer, &input_position_iter, &end_iter, FALSE);
528                                         win->history = g_list_prepend(win->history, current_input);
529                                         win->history_pos = win->history;
530                                 }
531
532                                 gtk_text_buffer_delete(buffer, &input_position_iter, &end_iter);
533
534                                 if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
535                                 {
536                                         if(win->history_pos)
537                                                 win->history_pos = g_list_next(win->history_pos);
538                                         else
539                                                 win->history_pos = win->history;
540                                 }
541                                 else /* down */
542                                         win->history_pos = g_list_previous(win->history_pos);
543
544                                 /* Insert the history item into the window */
545                                 gtk_text_buffer_get_end_iter(buffer, &end_iter);
546
547                                 g_signal_handler_block(buffer, win->insert_text_handler);
548                                 gtk_text_buffer_insert_with_tags_by_name(buffer, &end_iter, win->history_pos->data, -1, "default", "input", "glk-input", NULL);
549                                 g_signal_handler_unblock(buffer, win->insert_text_handler);
550
551                                 return TRUE;
552                         }
553
554                         /* Move to beginning/end of input field */
555                         else if(event->keyval == GDK_Home) {
556                                 GtkTextIter input_iter;
557                                 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
558                                 gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
559                                 gtk_text_buffer_place_cursor(buffer, &input_iter);
560                                 return TRUE;
561                         }
562                         else if(event->keyval == GDK_End) {
563                                 gtk_text_buffer_place_cursor(buffer, &end_iter);
564                                 return TRUE;
565                         }
566
567                         /* Handle the line terminators */
568                         else if(event->keyval == GDK_Return || event->keyval == GDK_KP_Enter
569                            || g_slist_find(win->current_extra_line_terminators, GUINT_TO_POINTER(event->keyval)))
570                         {
571                                 /* Remove signal handlers */
572                                 g_signal_handler_block(buffer, win->insert_text_handler);
573                                 g_signal_handler_block(win->widget, win->line_input_keypress_handler);
574
575                                 /* Insert a newline (even if line input was terminated with a different key */
576                                 gtk_text_buffer_get_end_iter(buffer, &end_iter);
577                                 gtk_text_buffer_insert(buffer, &end_iter, "\n", 1);
578                                 gtk_text_buffer_place_cursor(buffer, &end_iter);
579
580                                 /* Make the window uneditable again and retrieve the text that was input */
581                                 gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), FALSE);
582
583                                 int chars_written = finish_text_buffer_line_input(win, TRUE);
584                                 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
585                                 event_throw(glk, evtype_LineInput, win, chars_written, 0);
586                                 return TRUE;
587                         }
588                 }
589                         return FALSE;
590
591                 /* If this is a text grid window, then redirect the key press to the line input GtkEntry */
592                 case wintype_TextGrid:
593                 {
594                         if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up
595                                 || event->keyval == GDK_Down || event->keyval == GDK_KP_Down
596                                 || event->keyval == GDK_Left || event->keyval == GDK_KP_Left
597                                 || event->keyval == GDK_Right || event->keyval == GDK_KP_Right
598                                 || event->keyval == GDK_Tab || event->keyval == GDK_KP_Tab
599                                 || event->keyval == GDK_Page_Up || event->keyval == GDK_KP_Page_Up
600                                 || event->keyval == GDK_Page_Down || event->keyval == GDK_KP_Page_Down
601                                 || event->keyval == GDK_Home || event->keyval == GDK_KP_Home
602                                 || event->keyval == GDK_End || event->keyval == GDK_KP_End)
603                                 return FALSE; /* Don't redirect these keys */
604                         gtk_widget_grab_focus(win->input_entry);
605                         gtk_editable_set_position(GTK_EDITABLE(win->input_entry), -1);
606                         gboolean retval = TRUE;
607                         g_signal_emit_by_name(win->input_entry, "key-press-event", event, &retval);
608                         return retval; /* Block this key event if the entry handled it */
609                 }
610         }
611         return FALSE;
612 }
613
614 /* Internal function: finish handling a line input request, for both text grid and text buffer windows. */
615 static int
616 write_to_window_buffer(winid_t win, const gchar *inserted_text)
617 {
618         int copycount = 0;
619
620     /* Convert the string from UTF-8 to Latin-1 or Unicode */
621     if(win->input_request_type == INPUT_REQUEST_LINE)
622     {
623         gsize bytes_written;
624         gchar *latin1 = convert_utf8_to_latin1(inserted_text, &bytes_written);
625
626         if(latin1 == NULL)
627             return 0;
628
629         /* Place input in the echo stream */
630         if(win->echo_stream != NULL)
631             glk_put_string_stream(win->echo_stream, latin1);
632
633         /* Copy the string (bytes_written does not include the NULL at the end) */
634         copycount = MIN(win->line_input_buffer_max_len, bytes_written);
635         memcpy(win->line_input_buffer, latin1, copycount);
636         g_free(latin1);
637     }
638     else if(win->input_request_type == INPUT_REQUEST_LINE_UNICODE)
639     {
640         glong items_written;
641         gunichar *unicode = convert_utf8_to_ucs4(inserted_text, &items_written);
642
643         if(unicode == NULL)
644             return 0;
645
646         /* Place input in the echo stream */
647         if(win->echo_stream != NULL)
648             glk_put_string_stream_uni(win->echo_stream, unicode);
649
650         /* Copy the string (but not the NULL at the end) */
651         copycount = MIN(win->line_input_buffer_max_len, items_written);
652         memcpy(win->line_input_buffer_unicode, unicode, copycount * sizeof(gunichar));
653         g_free(unicode);
654     }
655     else
656         WARNING("Wrong input request type");
657
658     win->input_request_type = INPUT_REQUEST_NONE;
659         return copycount;
660 }
661
662 /* Internal function: Retrieves the input of a TextBuffer window and stores it in the window buffer.
663  * Returns the number of characters written, suitable for inclusion in a line input event. */
664 static int
665 finish_text_buffer_line_input(winid_t win, gboolean emit_signal)
666 {
667         VALID_WINDOW(win, return 0);
668         g_return_val_if_fail(win->type == wintype_TextBuffer, 0);
669
670         GtkTextIter start_iter, end_iter;
671
672         GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
673         GtkTextMark *input_position = gtk_text_buffer_get_mark(window_buffer, "input_position");
674         gtk_text_buffer_get_iter_at_mark(window_buffer, &start_iter, input_position);
675         gtk_text_buffer_get_end_iter(window_buffer, &end_iter);
676
677         gchar *inserted_text = gtk_text_buffer_get_text(window_buffer, &start_iter, &end_iter, FALSE);
678
679         /* If echoing is turned off, remove the text from the window */
680         if(!win->echo_current_line_input)
681                 gtk_text_buffer_delete(window_buffer, &start_iter, &end_iter);
682
683         /* Don't include the newline in the input */
684         char *last_char = inserted_text + strlen(inserted_text) - 1;
685         if(*last_char == '\n')
686                 *last_char = '\0';
687
688         int chars_written = write_to_window_buffer(win, inserted_text);
689         if(emit_signal)
690         {
691                 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
692                 g_assert(glk);
693                 g_signal_emit_by_name(glk, "line-input", win->rock, inserted_text);
694         }
695
696         /* Add the text to the window input history */
697         if(win->history_pos != NULL)
698         {
699                 g_free(win->history->data);
700                 win->history = g_list_delete_link(win->history, win->history);
701         }
702         if(*inserted_text != 0)
703                 win->history = g_list_prepend(win->history, g_strdup(inserted_text));
704
705         win->history_pos = NULL;
706
707         g_free(inserted_text);
708
709         return chars_written;
710 }
711
712 /* Internal function: Retrieves the input of a TextGrid window and stores it in the window buffer.
713  * Returns the number of characters written, suitable for inclusion in a line input event. */
714 static int
715 finish_text_grid_line_input(winid_t win, gboolean emit_signal)
716 {
717         VALID_WINDOW(win, return 0);
718         g_return_val_if_fail(win->type == wintype_TextGrid, 0);
719
720         GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
721
722         gchar *text = g_strdup( gtk_entry_get_text(GTK_ENTRY(win->input_entry)) );
723         /* Move the focus back into the text view */
724         gtk_widget_grab_focus(win->widget);
725         /* Remove entry widget from text view */
726         /* Should be ok even though this is the widget's own signal handler */
727         gtk_container_remove( GTK_CONTAINER(win->widget), GTK_WIDGET(win->input_entry) );
728         win->input_entry = NULL;
729         /* Delete the child anchor */
730         GtkTextIter start, end;
731         gtk_text_buffer_get_iter_at_child_anchor(buffer, &start, win->input_anchor);
732         end = start;
733         gtk_text_iter_forward_char(&end); /* Point after the child anchor */
734         gtk_text_buffer_delete(buffer, &start, &end);
735         win->input_anchor = NULL;
736
737     gchar *spaces = g_strnfill(win->input_length - g_utf8_strlen(text, -1), ' ');
738     gchar *text_to_insert = g_strconcat(text, spaces, NULL);
739         g_free(spaces);
740     gtk_text_buffer_insert(buffer, &start, text_to_insert, -1);
741     g_free(text_to_insert);
742
743     int chars_written = write_to_window_buffer(win, text);
744     if(emit_signal)
745     {
746                 ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
747                 g_assert(glk);
748                 g_signal_emit_by_name(glk, "line-input", win->rock, text);
749     }
750
751         /* Add the text to the window input history */
752         if(win->history_pos != NULL)
753         {
754                 g_free(win->history->data);
755                 win->history = g_list_delete_link(win->history, win->history);
756         }
757         if(*text != 0)
758                 win->history = g_list_prepend(win->history, g_strdup(text));
759         win->history_pos = NULL;
760
761         g_free(text);
762         return chars_written;
763 }
764
765 /* Internal function: Callback for signal insert-text on a text buffer window.
766 Runs after the default handler has already inserted the text. */
767 void
768 after_window_insert_text(GtkTextBuffer *textbuffer, GtkTextIter *location, gchar *text, gint len, winid_t win)
769 {
770         GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
771
772         /* Set the history position to NULL and erase the text we were already editing */
773         if(win->history_pos != NULL)
774         {
775                 g_free(win->history->data);
776                 win->history = g_list_delete_link(win->history, win->history);
777                 win->history_pos = NULL;
778         }
779
780         /* Apply the 'input' style to the text that was entered */
781         GtkTextIter end_iter;
782         gtk_text_buffer_get_end_iter(window_buffer, &end_iter);
783         GtkTextIter input_iter;
784         GtkTextMark *input_position = gtk_text_buffer_get_mark(window_buffer, "input_position");
785         gtk_text_buffer_get_iter_at_mark(window_buffer, &input_iter, input_position);
786         gtk_text_buffer_apply_tag_by_name(window_buffer, "input", &input_iter, &end_iter);
787 }
788
789 /* Internal function: Callback for signal activate on the line input GtkEntry
790 in a text grid window. */
791 void
792 on_input_entry_activate(GtkEntry *input_entry, winid_t win)
793 {
794         g_signal_handler_block(win->widget, win->line_input_keypress_handler);
795
796         int chars_written = finish_text_grid_line_input(win, TRUE);
797         ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
798         event_throw(glk, evtype_LineInput, win, chars_written, 0);
799 }
800
801 /* Internal function: Callback for signal key-press-event on the line input
802 GtkEntry in a text grid window. */
803 gboolean
804 on_input_entry_key_press_event(GtkEntry *input_entry, GdkEventKey *event, winid_t win)
805 {
806         if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up
807                 || event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
808         {
809                 /* Prevent falling off the end of the history list */
810                 if( (event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
811                         && win->history_pos && win->history_pos->next == NULL)
812                         return TRUE;
813                 if( (event->keyval == GDK_Down || event->keyval == GDK_KP_Down)
814                         && (win->history_pos == NULL || win->history_pos->prev == NULL) )
815                         return TRUE;
816
817                 if(win->history_pos == NULL)
818                 {
819                         const gchar *current_input = gtk_entry_get_text(input_entry);
820                         win->history = g_list_prepend(win->history, g_strdup(current_input));
821                         win->history_pos = win->history;
822                 }
823
824                 if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
825                 {
826                         if(win->history_pos)
827                                 win->history_pos = g_list_next(win->history_pos);
828                         else
829                                 win->history_pos = win->history;
830                 }
831                 else /* down */
832                         win->history_pos = g_list_previous(win->history_pos);
833
834                 /* Insert the history item into the window */
835                 g_signal_handler_block(input_entry, win->line_input_entry_changed);
836                 gtk_entry_set_text(input_entry, win->history_pos->data);
837                 g_signal_handler_unblock(input_entry, win->line_input_entry_changed);
838                 return TRUE;
839         }
840         else if(g_slist_find(win->current_extra_line_terminators, GUINT_TO_POINTER(event->keyval)))
841         {
842                 /* If this key was a line terminator, pretend we pressed enter */
843                 on_input_entry_activate(input_entry, win);
844         }
845         return FALSE;
846 }
847
848 void
849 on_input_entry_changed(GtkEditable *editable, winid_t win)
850 {
851         /* Set the history position to NULL and erase the text we were already editing */
852         if(win->history_pos != NULL)
853         {
854                 g_free(win->history->data);
855                 win->history = g_list_delete_link(win->history, win->history);
856                 win->history_pos = NULL;
857         }
858 }
859
860 glui32
861 keyval_to_glk_keycode(guint keyval, gboolean unicode)
862 {
863         glui32 keycode;
864         switch(keyval) {
865                 case GDK_Up:
866                 case GDK_KP_Up: return keycode_Up;
867                 case GDK_Down:
868                 case GDK_KP_Down: return keycode_Down;
869                 case GDK_Left:
870                 case GDK_KP_Left: return keycode_Left;
871                 case GDK_Right:
872                 case GDK_KP_Right: return keycode_Right;
873                 case GDK_Linefeed:
874                 case GDK_Return:
875                 case GDK_KP_Enter: return keycode_Return;
876                 case GDK_Delete:
877                 case GDK_BackSpace:
878                 case GDK_KP_Delete: return keycode_Delete;
879                 case GDK_Escape: return keycode_Escape;
880                 case GDK_Tab:
881                 case GDK_KP_Tab: return keycode_Tab;
882                 case GDK_Page_Up:
883                 case GDK_KP_Page_Up: return keycode_PageUp;
884                 case GDK_Page_Down:
885                 case GDK_KP_Page_Down: return keycode_PageDown;
886                 case GDK_Home:
887                 case GDK_KP_Home: return keycode_Home;
888                 case GDK_End:
889                 case GDK_KP_End: return keycode_End;
890                 case GDK_F1:
891                 case GDK_KP_F1: return keycode_Func1;
892                 case GDK_F2:
893                 case GDK_KP_F2: return keycode_Func2;
894                 case GDK_F3:
895                 case GDK_KP_F3: return keycode_Func3;
896                 case GDK_F4:
897                 case GDK_KP_F4: return keycode_Func4;
898                 case GDK_F5: return keycode_Func5;
899                 case GDK_F6: return keycode_Func6;
900                 case GDK_F7: return keycode_Func7;
901                 case GDK_F8: return keycode_Func8;
902                 case GDK_F9: return keycode_Func9;
903                 case GDK_F10: return keycode_Func10;
904                 case GDK_F11: return keycode_Func11;
905                 case GDK_F12: return keycode_Func12;
906                 default:
907                         keycode = gdk_keyval_to_unicode(keyval);
908                         /* If keycode is 0, then keyval was not recognized; also return
909                         unknown if Latin-1 input was requested and the character is not in
910                         Latin-1 */
911                         if(keycode == 0 || (!unicode && keycode > 255))
912                                 return keycode_Unknown;
913                         return keycode;
914         }
915 }
916
917 void
918 force_char_input_from_queue(winid_t win, event_t *event)
919 {
920         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
921         guint keyval = GPOINTER_TO_UINT(g_async_queue_pop(glk_data->char_input_queue));
922
923         glk_cancel_char_event(win);
924
925         gdk_threads_enter();
926         ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
927         g_assert(glk);
928         g_signal_emit_by_name(glk, "char-input", win->rock, keyval);
929         gdk_threads_leave();
930
931         event->type = evtype_CharInput;
932         event->win = win;
933         event->val1 = keyval_to_glk_keycode(keyval, win->input_request_type == INPUT_REQUEST_CHARACTER_UNICODE);
934         event->val2 = 0;
935 }
936
937 void
938 force_line_input_from_queue(winid_t win, event_t *event)
939 {
940         ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
941         const gchar *text = g_async_queue_pop(glk_data->line_input_queue);
942         glui32 chars_written = 0;
943
944         gdk_threads_enter();
945         if(win->type == wintype_TextBuffer)
946         {
947                 GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
948                 GtkTextIter start, end;
949
950                 /* Remove signal handlers so the line input doesn't get picked up again */
951                 g_signal_handler_block(buffer, win->insert_text_handler);
952                 g_signal_handler_block(win->widget, win->line_input_keypress_handler);
953
954                 /* Erase any text that was already typed */
955                 GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
956                 gtk_text_buffer_get_iter_at_mark(buffer, &start, input_position);
957                 gtk_text_buffer_get_end_iter(buffer, &end);
958                 gtk_text_buffer_delete(buffer, &start, &end);
959
960                 /* Make the window uneditable again */
961                 gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), FALSE);
962
963                 /* Insert the forced input into the window */
964                 if(win->echo_current_line_input)
965                 {
966                         gtk_text_buffer_get_end_iter(buffer, &end);
967                         gchar *text_to_insert = g_strconcat(text, "\n", NULL);
968                         gtk_text_buffer_insert_with_tags_by_name(buffer, &end, text_to_insert, -1, "default", "input", NULL);
969                 }
970
971                 chars_written = finish_text_buffer_line_input(win, TRUE);
972         }
973         else if(win->type == wintype_TextGrid)
974         {
975                 /* Remove signal handlers so the line input doesn't get picked up again */
976                 g_signal_handler_block(win->widget, win->char_input_keypress_handler);
977
978                 /* Insert the forced input into the window */
979                 gtk_entry_set_text(GTK_ENTRY(win->input_entry), text);
980                 chars_written = finish_text_grid_line_input(win, TRUE);
981         }
982         gdk_threads_leave();
983
984         event->type = evtype_LineInput;
985         event->win = win;
986         event->val1 = chars_written;
987         event->val2 = 0;
988 }
989
990 /*** Internal function: cancels any pending input requests on the window and presents a warning if not INPUT_REQUEST_NONE ***/
991 void
992 cancel_old_input_request(winid_t win)
993 {
994         switch(win->input_request_type) {
995         case INPUT_REQUEST_NONE:
996                 break; /* All is well */
997         case INPUT_REQUEST_CHARACTER:
998         case INPUT_REQUEST_CHARACTER_UNICODE:
999                 glk_cancel_char_event(win);
1000                 WARNING("Cancelling pending char event");
1001                 break;
1002         case INPUT_REQUEST_LINE:
1003         case INPUT_REQUEST_LINE_UNICODE:
1004                 glk_cancel_line_event(win, NULL);
1005                 WARNING("Cancelling pending line event");
1006                 break;
1007         default:
1008                 WARNING("Could not cancel pending input request: unknown input request");
1009         }
1010 }
1011
1012 /**
1013  * glk_set_echo_line_event:
1014  * @win: The window in which to change the echoing behavior.
1015  * @val: Zero to turn off echoing, nonzero for normal echoing.
1016  *
1017  * Normally, after line input is completed or cancelled in a buffer window, the
1018  * library ensures that the complete input line (or its latest state, after
1019  * cancelling) is displayed at the end of the buffer, followed by a newline.
1020  * This call allows you to suppress this behavior. If the @val argument is zero,
1021  * all <emphasis>subsequent</emphasis> line input requests in the given window
1022  * will leave the buffer unchanged after the input is completed or cancelled;
1023  * the player's input will not be printed. If @val is nonzero, subsequent input
1024  * requests will have the normal printing behavior.
1025  *
1026  * <note><para>
1027  *   Note that this feature is unrelated to the window's echo stream.
1028  * </para></note>
1029  *
1030  * If you turn off line input echoing, you can reproduce the standard input
1031  * behavior by following each line input event (or line input cancellation) by
1032  * printing the input line, in the Input style, followed by a newline in the
1033  * original style.
1034  *
1035  * The glk_set_echo_line_event() does not affect a pending line input request.
1036  * It also has no effect in non-buffer windows.
1037  * <note><para>
1038  *   In a grid window, the game can overwrite the input area at will, so there
1039  *   is no need for this distinction.
1040  * </para></note>
1041  *
1042  * Not all libraries support this feature. You can test for it with
1043  * %gestalt_LineInputEcho.
1044  */
1045 void
1046 glk_set_echo_line_event(winid_t win, glui32 val)
1047 {
1048         VALID_WINDOW(win, return);
1049
1050         if(win->type != wintype_TextBuffer)
1051                 return; /* Quietly do nothing */
1052
1053         win->echo_line_input = val? TRUE : FALSE;
1054 }
1055
1056 /* Internal function to convert from a Glk keycode to a GDK keyval. */
1057 static unsigned
1058 keycode_to_gdk_keyval(glui32 keycode)
1059 {
1060         switch (keycode)
1061         {
1062                 case keycode_Left:
1063                         return GDK_Left;
1064                 case keycode_Right:
1065                         return GDK_Right;
1066                 case keycode_Up:
1067                         return GDK_Up;
1068                 case keycode_Down:
1069                         return GDK_Down;
1070                 case keycode_Return:
1071                         return GDK_Return;
1072                 case keycode_Delete:
1073                         return GDK_Delete;
1074                 case keycode_Escape:
1075                         return GDK_Escape;
1076                 case keycode_Tab:
1077                         return GDK_Tab;
1078                 case keycode_PageUp:
1079                         return GDK_Page_Up;
1080                 case keycode_PageDown:
1081                         return GDK_Page_Down;
1082                 case keycode_Home:
1083                         return GDK_Home;
1084                 case keycode_End:
1085                         return GDK_End;
1086                 case keycode_Func1:
1087                         return GDK_F1;
1088                 case keycode_Func2:
1089                         return GDK_F2;
1090                 case keycode_Func3:
1091                         return GDK_F3;
1092                 case keycode_Func4:
1093                         return GDK_F4;
1094                 case keycode_Func5:
1095                         return GDK_F5;
1096                 case keycode_Func6:
1097                         return GDK_F6;
1098                 case keycode_Func7:
1099                         return GDK_F7;
1100                 case keycode_Func8:
1101                         return GDK_F8;
1102                 case keycode_Func9:
1103                         return GDK_F9;
1104                 case keycode_Func10:
1105                         return GDK_F10;
1106                 case keycode_Func11:
1107                         return GDK_F11;
1108                 case keycode_Func12:
1109                         return GDK_F12;
1110                 case keycode_Erase:
1111                         return GDK_BackSpace;
1112         }
1113         unsigned keyval = gdk_unicode_to_keyval(keycode);
1114         if(keyval < 0x01000000) /* magic number meaning illegal unicode point */
1115                 return keyval;
1116         return 0;
1117 }
1118
1119 /* Internal function to decide whether @keycode is a valid line terminator. */
1120 gboolean
1121 is_valid_line_terminator(glui32 keycode)
1122 {
1123         switch(keycode) {
1124                 case keycode_Escape:
1125                 case keycode_Func1:
1126                 case keycode_Func2:
1127                 case keycode_Func3:
1128                 case keycode_Func4:
1129                 case keycode_Func5:
1130                 case keycode_Func6:
1131                 case keycode_Func7:
1132                 case keycode_Func8:
1133                 case keycode_Func9:
1134                 case keycode_Func10:
1135                 case keycode_Func11:
1136                 case keycode_Func12:
1137                         return TRUE;
1138         }
1139         return FALSE;
1140 }
1141
1142 /**
1143  * glk_set_terminators_line_event:
1144  * @win: The window for which to set the line input terminator keys.
1145  * @keycodes: An array of <code>keycode_</code> constants, of length @count.
1146  * @count: The array length of @keycodes.
1147  *
1148  * It is possible to request that other keystrokes complete line input as well.
1149  * (This allows a game to intercept function keys or other special keys during
1150  * line input.) To do this, call glk_set_terminators_line_event(), and pass an
1151  * array of @count keycodes. These must all be special keycodes (see <link
1152  * linkend="chimara-Character-Input">Character Input</link>). Do not include
1153  * regular printable characters in the array, nor %keycode_Return (which
1154  * represents the default <keycap>enter</keycap> key and will always be
1155  * recognized). To return to the default behavior, pass a %NULL or empty array.
1156  *
1157  * The glk_set_terminators_line_event() affects <emphasis>subsequent</emphasis>
1158  * line input requests in the given window. It does not affect a pending line
1159  * input request.
1160  *
1161  * <note><para>
1162  *   This distinction makes life easier for interpreters that set up UI
1163  *   callbacks only at the start of input.
1164  * </para></note>
1165  *
1166  * A library may not support this feature; if it does, it may not support all
1167  * special keys as terminators. (Some keystrokes are reserved for OS or
1168  * interpreter control.) You can test for this with %gestalt_LineTerminators and
1169  * %gestalt_LineTerminatorKey.
1170  */
1171 void
1172 glk_set_terminators_line_event(winid_t win, glui32 *keycodes, glui32 count)
1173 {
1174         VALID_WINDOW(win, return);
1175
1176         g_slist_free(win->extra_line_terminators);
1177         win->extra_line_terminators = NULL;
1178
1179         if(keycodes == NULL || count == 0)
1180                 return;
1181
1182         int i;
1183         for(i = 0; i < count; i++)
1184         {
1185                 unsigned key = keycode_to_gdk_keyval(keycodes[i]);
1186                 if(is_valid_line_terminator(keycodes[i]))
1187                         win->extra_line_terminators = g_slist_prepend(win->extra_line_terminators, GUINT_TO_POINTER(key));
1188                 else
1189                    WARNING_S("Ignoring invalid line terminator", gdk_keyval_name(key));
1190         }
1191 }