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