X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=libchimara%2Finput.c;h=7f9b693cf9c8bc7738a7482789d84387f23d7024;hb=17f4686de3c28558aecac29a4d46de8c035bcee5;hp=d59191641ac2d892eb40ddec1e8215eddf09b83d;hpb=8b8b592abd23f01d0e083011b309c6f02f8183d5;p=projects%2Fchimara%2Fchimara.git
diff --git a/libchimara/input.c b/libchimara/input.c
index d591916..7f9b693 100644
--- a/libchimara/input.c
+++ b/libchimara/input.c
@@ -3,6 +3,7 @@
#include "input.h"
#include "pager.h"
#include "chimara-glk-private.h"
+#include "garglk.h"
extern GPrivate *glk_data_key;
@@ -22,7 +23,15 @@ request_char_event_common(winid_t win, gboolean unicode)
flush_window_buffer(win);
- ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
+ if(win->type == wintype_TextBuffer) {
+ /* Move the input_position mark to the end of the window_buffer */
+ GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
+ GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
+ GtkTextIter end_iter;
+ gtk_text_buffer_get_end_iter(buffer, &end_iter);
+ gtk_text_buffer_move_mark(buffer, input_position, &end_iter);
+ }
+
win->input_request_type = unicode? INPUT_REQUEST_CHARACTER_UNICODE : INPUT_REQUEST_CHARACTER;
g_signal_handler_unblock( win->widget, win->char_input_keypress_handler );
@@ -32,13 +41,8 @@ request_char_event_common(winid_t win, gboolean unicode)
gdk_threads_leave();
/* Emit the "waiting" signal to let listeners know we are ready for input */
+ ChimaraGlkPrivate *glk_data = g_private_get(glk_data_key);
g_signal_emit_by_name(glk_data->self, "waiting");
-
- /* Schedule a check for the pager */
- /*
- if(win->type == wintype_TextBuffer)
- g_idle_add(pager_check, win);
- */
}
/**
@@ -96,6 +100,10 @@ glk_cancel_char_event(winid_t win)
static void
text_grid_request_line_event_common(winid_t win, glui32 maxlen, gboolean insert, gchar *inserttext)
{
+ /* All outstanding printing _must_ be finished before putting an input entry
+ into the buffer */
+ flush_window_buffer(win);
+
gdk_threads_enter();
GtkTextBuffer *buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
@@ -195,7 +203,9 @@ text_buffer_request_line_event_common(winid_t win, glui32 maxlen, gboolean inser
/* Apply the correct style to the input prompt */
GtkTextIter input_iter;
gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
+ gtk_text_buffer_apply_tag_by_name(buffer, "default", &input_iter, &end_iter);
gtk_text_buffer_apply_tag_by_name(buffer, "input", &input_iter, &end_iter);
+ gtk_text_buffer_apply_tag_by_name(buffer, "glk-input", &input_iter, &end_iter);
gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), TRUE);
@@ -203,11 +213,6 @@ text_buffer_request_line_event_common(winid_t win, glui32 maxlen, gboolean inser
gtk_widget_grab_focus(win->widget);
gdk_threads_leave();
-
- /* Schedule a check for the pager */
- /*
- g_idle_add(pager_check, win);
- */
}
/**
@@ -254,6 +259,8 @@ glk_request_line_event(winid_t win, char *buf, glui32 maxlen, glui32 initlen)
win->input_request_type = INPUT_REQUEST_LINE;
win->line_input_buffer = buf;
win->line_input_buffer_max_len = maxlen;
+ win->echo_current_line_input = win->echo_line_input;
+ win->current_extra_line_terminators = g_slist_copy(win->extra_line_terminators);
gchar *inserttext = (initlen > 0)? g_strndup(buf, initlen) : g_strdup("");
switch(win->type)
@@ -284,11 +291,28 @@ glk_request_line_event(winid_t win, char *buf, glui32 maxlen, glui32 initlen)
* glui32 values instead of an array of characters, and the values
* may be any valid Unicode code points.
*
- * The result will be in Unicode Normalization Form C. This basically means that
- * composite characters will be single characters where possible, instead of
- * sequences of base and combining marks. See
- * Unicode Standard Annex
- * #15 for the details.
+ * If possible, the library should return fully composed Unicode characters,
+ * rather than strings of base and composition characters.
+ *
+ *
+ * Fully-composed characters are the norm for Unicode text, so an
+ * implementation that ignores this issue will probably produce the right
+ * result. However, the game may not want to rely on that. Another factor is
+ * that case-folding can (occasionally) produce non-normalized text.
+ * Therefore, to cover all its bases, a game should call
+ * glk_buffer_to_lower_case_uni(), followed by
+ * glk_buffer_canon_normalize_uni(), before parsing.
+ *
+ *
+ *
+ * Earlier versions of this spec said that line input must always be in
+ * Unicode Normalization Form C. However, this has not been universally
+ * implemented. It is also somewhat redundant, for the results noted above.
+ * Therefore, we now merely recommend that line input be fully composed. The
+ * game is ultimately responsible for all case-folding and normalization. See
+ * Unicode String
+ * Normalization.
+ *
*/
void
glk_request_line_event_uni(winid_t win, glui32 *buf, glui32 maxlen, glui32 initlen)
@@ -305,11 +329,11 @@ glk_request_line_event_uni(winid_t win, glui32 *buf, glui32 maxlen, glui32 initl
if(glk_data->register_arr)
win->buffer_rock = (*glk_data->register_arr)(buf, maxlen, "&+#!Iu");
-
-
win->input_request_type = INPUT_REQUEST_LINE_UNICODE;
win->line_input_buffer_unicode = buf;
win->line_input_buffer_max_len = maxlen;
+ win->echo_current_line_input = win->echo_line_input;
+ win->current_extra_line_terminators = g_slist_copy(win->extra_line_terminators);
gchar *utf8;
if(initlen > 0) {
@@ -460,6 +484,27 @@ on_line_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win
switch(win->type)
{
case wintype_TextBuffer:
+ {
+ GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
+ GtkTextMark *input_position_mark = gtk_text_buffer_get_mark(buffer, "input_position");
+ GtkTextIter input_position_iter;
+ gtk_text_buffer_get_iter_at_mark(buffer, &input_position_iter, input_position_mark);
+ GtkTextIter end_iter;
+ gtk_text_buffer_get_end_iter(buffer, &end_iter);
+
+ /* Check whether the cursor is at the prompt or somewhere else in the text */
+ GtkTextIter selection_start, selection_end;
+ gtk_text_buffer_get_selection_bounds(buffer, &selection_start, &selection_end);
+ if(gtk_text_iter_compare(&selection_start, &input_position_iter) < 0) {
+ // Cursor is somewhere else in the text, place it at the end if the user starts typing
+ if(event->keyval >= GDK_space && event->keyval <= GDK_asciitilde) {
+ gtk_text_buffer_place_cursor(buffer, &end_iter);
+ } else {
+ // User is walking around, let him be.
+ return FALSE;
+ }
+ }
+
/* All text up to the input position is now regarded as being read by the user */
pager_update(win);
@@ -477,20 +522,14 @@ on_line_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win
&& (win->history_pos == NULL || win->history_pos->prev == NULL) )
return TRUE;
- GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
- GtkTextIter start, end;
/* Erase any text that was already typed */
- GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
- gtk_text_buffer_get_iter_at_mark(buffer, &start, input_position);
- gtk_text_buffer_get_end_iter(buffer, &end);
-
if(win->history_pos == NULL) {
- gchar *current_input = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
+ gchar *current_input = gtk_text_buffer_get_text(buffer, &input_position_iter, &end_iter, FALSE);
win->history = g_list_prepend(win->history, current_input);
win->history_pos = win->history;
}
- gtk_text_buffer_delete(buffer, &start, &end);
+ gtk_text_buffer_delete(buffer, &input_position_iter, &end_iter);
if(event->keyval == GDK_Up || event->keyval == GDK_KP_Up)
{
@@ -503,17 +542,17 @@ on_line_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win
win->history_pos = g_list_previous(win->history_pos);
/* Insert the history item into the window */
- gtk_text_buffer_get_end_iter(buffer, &end);
+ gtk_text_buffer_get_end_iter(buffer, &end_iter);
g_signal_handler_block(buffer, win->insert_text_handler);
- gtk_text_buffer_insert_with_tags_by_name(buffer, &end, win->history_pos->data, -1, "input", NULL);
+ gtk_text_buffer_insert_with_tags_by_name(buffer, &end_iter, win->history_pos->data, -1, "default", "input", "glk-input", NULL);
g_signal_handler_unblock(buffer, win->insert_text_handler);
+
return TRUE;
}
/* Move to beginning/end of input field */
else if(event->keyval == GDK_Home) {
- GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
GtkTextIter input_iter;
GtkTextMark *input_position = gtk_text_buffer_get_mark(buffer, "input_position");
gtk_text_buffer_get_iter_at_mark(buffer, &input_iter, input_position);
@@ -521,22 +560,32 @@ on_line_input_key_press_event(GtkWidget *widget, GdkEventKey *event, winid_t win
return TRUE;
}
else if(event->keyval == GDK_End) {
- GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
- GtkTextIter end_iter;
- gtk_text_buffer_get_end_iter(buffer, &end_iter);
gtk_text_buffer_place_cursor(buffer, &end_iter);
return TRUE;
}
- /* Handle the enter key, which could occur in the middle of the sentence. */
- else if(event->keyval == GDK_Return || event->keyval == GDK_KP_Enter) {
- GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(win->widget));
- GtkTextIter end_iter;
+ /* Handle the line terminators */
+ else if(event->keyval == GDK_Return || event->keyval == GDK_KP_Enter
+ || g_slist_find(win->current_extra_line_terminators, GUINT_TO_POINTER(event->keyval)))
+ {
+ /* Remove signal handlers */
+ g_signal_handler_block(buffer, win->insert_text_handler);
+ g_signal_handler_block(win->widget, win->line_input_keypress_handler);
+
+ /* Insert a newline (even if line input was terminated with a different key */
gtk_text_buffer_get_end_iter(buffer, &end_iter);
+ gtk_text_buffer_insert(buffer, &end_iter, "\n", 1);
gtk_text_buffer_place_cursor(buffer, &end_iter);
- return FALSE;
- }
+ /* Make the window uneditable again and retrieve the text that was input */
+ gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), FALSE);
+
+ int chars_written = finish_text_buffer_line_input(win, TRUE);
+ ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
+ event_throw(glk, evtype_LineInput, win, chars_written, 0);
+ return TRUE;
+ }
+ }
return FALSE;
/* If this is a text grid window, then redirect the key press to the line input GtkEntry */
@@ -618,21 +667,23 @@ finish_text_buffer_line_input(winid_t win, gboolean emit_signal)
VALID_WINDOW(win, return 0);
g_return_val_if_fail(win->type == wintype_TextBuffer, 0);
- GtkTextIter start_iter, end_iter, last_character;
+ GtkTextIter start_iter, end_iter;
GtkTextBuffer *window_buffer = gtk_text_view_get_buffer( GTK_TEXT_VIEW(win->widget) );
GtkTextMark *input_position = gtk_text_buffer_get_mark(window_buffer, "input_position");
gtk_text_buffer_get_iter_at_mark(window_buffer, &start_iter, input_position);
gtk_text_buffer_get_end_iter(window_buffer, &end_iter);
- gtk_text_buffer_get_end_iter(window_buffer, &last_character);
- gtk_text_iter_backward_cursor_position(&last_character);
- gchar* last_char = gtk_text_buffer_get_text(window_buffer, &last_character, &end_iter, FALSE);
+ gchar *inserted_text = gtk_text_buffer_get_text(window_buffer, &start_iter, &end_iter, FALSE);
- if( strchr(last_char, '\n') != NULL )
- gtk_text_iter_backward_cursor_position(&end_iter);
+ /* If echoing is turned off, remove the text from the window */
+ if(!win->echo_current_line_input)
+ gtk_text_buffer_delete(window_buffer, &start_iter, &end_iter);
- gchar* inserted_text = gtk_text_buffer_get_text(window_buffer, &start_iter, &end_iter, FALSE);
+ /* Don't include the newline in the input */
+ char *last_char = inserted_text + strlen(inserted_text) - 1;
+ if(*last_char == '\n')
+ *last_char = '\0';
int chars_written = write_to_window_buffer(win, inserted_text);
if(emit_signal)
@@ -712,10 +763,7 @@ finish_text_grid_line_input(winid_t win, gboolean emit_signal)
}
/* Internal function: Callback for signal insert-text on a text buffer window.
-Runs after the default handler has already inserted the text.
-FIXME: This function assumes that newline was the last character typed into the
-window. That assumption is wrong if, for example, text containing a newline was
-pasted into the window. */
+Runs after the default handler has already inserted the text. */
void
after_window_insert_text(GtkTextBuffer *textbuffer, GtkTextIter *location, gchar *text, gint len, winid_t win)
{
@@ -728,19 +776,6 @@ after_window_insert_text(GtkTextBuffer *textbuffer, GtkTextIter *location, gchar
win->history = g_list_delete_link(win->history, win->history);
win->history_pos = NULL;
}
- if( strchr(text, '\n') != NULL )
- {
- /* Remove signal handlers */
- g_signal_handler_block(window_buffer, win->insert_text_handler);
- g_signal_handler_block(win->widget, win->line_input_keypress_handler);
-
- /* Make the window uneditable again and retrieve the text that was input */
- gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), FALSE);
-
- int chars_written = finish_text_buffer_line_input(win, TRUE);
- ChimaraGlk *glk = CHIMARA_GLK(gtk_widget_get_ancestor(win->widget, CHIMARA_TYPE_GLK));
- event_throw(glk, evtype_LineInput, win, chars_written, 0);
- }
/* Apply the 'input' style to the text that was entered */
GtkTextIter end_iter;
@@ -748,7 +783,9 @@ after_window_insert_text(GtkTextBuffer *textbuffer, GtkTextIter *location, gchar
GtkTextIter input_iter;
GtkTextMark *input_position = gtk_text_buffer_get_mark(window_buffer, "input_position");
gtk_text_buffer_get_iter_at_mark(window_buffer, &input_iter, input_position);
+ gtk_text_buffer_apply_tag_by_name(window_buffer, "default", &input_iter, &end_iter);
gtk_text_buffer_apply_tag_by_name(window_buffer, "input", &input_iter, &end_iter);
+ gtk_text_buffer_apply_tag_by_name(window_buffer, "glk-input", &input_iter, &end_iter);
}
/* Internal function: Callback for signal activate on the line input GtkEntry
@@ -802,6 +839,11 @@ on_input_entry_key_press_event(GtkEntry *input_entry, GdkEventKey *event, winid_
g_signal_handler_unblock(input_entry, win->line_input_entry_changed);
return TRUE;
}
+ else if(g_slist_find(win->current_extra_line_terminators, GUINT_TO_POINTER(event->keyval)))
+ {
+ /* If this key was a line terminator, pretend we pressed enter */
+ on_input_entry_activate(input_entry, win);
+ }
return FALSE;
}
@@ -921,9 +963,13 @@ force_line_input_from_queue(winid_t win, event_t *event)
gtk_text_view_set_editable(GTK_TEXT_VIEW(win->widget), FALSE);
/* Insert the forced input into the window */
- gtk_text_buffer_get_end_iter(buffer, &end);
- gchar *text_to_insert = g_strconcat(text, "\n", NULL);
- gtk_text_buffer_insert_with_tags_by_name(buffer, &end, text_to_insert, -1, "input", NULL);
+ if(win->echo_current_line_input)
+ {
+ gtk_text_buffer_get_end_iter(buffer, &end);
+ gchar *text_to_insert = g_strconcat(text, "\n", NULL);
+ gtk_text_buffer_insert_with_tags_by_name(buffer, &end, text_to_insert, -1, "default", "input", "glk-input", NULL);
+ }
+
chars_written = finish_text_buffer_line_input(win, TRUE);
}
else if(win->type == wintype_TextGrid)
@@ -964,3 +1010,184 @@ cancel_old_input_request(winid_t win)
WARNING("Could not cancel pending input request: unknown input request");
}
}
+
+/**
+ * glk_set_echo_line_event:
+ * @win: The window in which to change the echoing behavior.
+ * @val: Zero to turn off echoing, nonzero for normal echoing.
+ *
+ * Normally, after line input is completed or cancelled in a buffer window, the
+ * library ensures that the complete input line (or its latest state, after
+ * cancelling) is displayed at the end of the buffer, followed by a newline.
+ * This call allows you to suppress this behavior. If the @val argument is zero,
+ * all subsequent line input requests in the given window
+ * will leave the buffer unchanged after the input is completed or cancelled;
+ * the player's input will not be printed. If @val is nonzero, subsequent input
+ * requests will have the normal printing behavior.
+ *
+ *
+ * Note that this feature is unrelated to the window's echo stream.
+ *
+ *
+ * If you turn off line input echoing, you can reproduce the standard input
+ * behavior by following each line input event (or line input cancellation) by
+ * printing the input line, in the Input style, followed by a newline in the
+ * original style.
+ *
+ * The glk_set_echo_line_event() does not affect a pending line input request.
+ * It also has no effect in non-buffer windows.
+ *
+ * In a grid window, the game can overwrite the input area at will, so there
+ * is no need for this distinction.
+ *
+ *
+ * Not all libraries support this feature. You can test for it with
+ * %gestalt_LineInputEcho.
+ */
+void
+glk_set_echo_line_event(winid_t win, glui32 val)
+{
+ VALID_WINDOW(win, return);
+
+ if(win->type != wintype_TextBuffer)
+ return; /* Quietly do nothing */
+
+ win->echo_line_input = val? TRUE : FALSE;
+}
+
+/* Internal function to convert from a Glk keycode to a GDK keyval. */
+static unsigned
+keycode_to_gdk_keyval(glui32 keycode)
+{
+ switch (keycode)
+ {
+ case keycode_Left:
+ return GDK_Left;
+ case keycode_Right:
+ return GDK_Right;
+ case keycode_Up:
+ return GDK_Up;
+ case keycode_Down:
+ return GDK_Down;
+ case keycode_Return:
+ return GDK_Return;
+ case keycode_Delete:
+ return GDK_Delete;
+ case keycode_Escape:
+ return GDK_Escape;
+ case keycode_Tab:
+ return GDK_Tab;
+ case keycode_PageUp:
+ return GDK_Page_Up;
+ case keycode_PageDown:
+ return GDK_Page_Down;
+ case keycode_Home:
+ return GDK_Home;
+ case keycode_End:
+ return GDK_End;
+ case keycode_Func1:
+ return GDK_F1;
+ case keycode_Func2:
+ return GDK_F2;
+ case keycode_Func3:
+ return GDK_F3;
+ case keycode_Func4:
+ return GDK_F4;
+ case keycode_Func5:
+ return GDK_F5;
+ case keycode_Func6:
+ return GDK_F6;
+ case keycode_Func7:
+ return GDK_F7;
+ case keycode_Func8:
+ return GDK_F8;
+ case keycode_Func9:
+ return GDK_F9;
+ case keycode_Func10:
+ return GDK_F10;
+ case keycode_Func11:
+ return GDK_F11;
+ case keycode_Func12:
+ return GDK_F12;
+ case keycode_Erase:
+ return GDK_BackSpace;
+ }
+ unsigned keyval = gdk_unicode_to_keyval(keycode);
+ if(keyval < 0x01000000) /* magic number meaning illegal unicode point */
+ return keyval;
+ return 0;
+}
+
+/* Internal function to decide whether @keycode is a valid line terminator. */
+gboolean
+is_valid_line_terminator(glui32 keycode)
+{
+ switch(keycode) {
+ case keycode_Escape:
+ case keycode_Func1:
+ case keycode_Func2:
+ case keycode_Func3:
+ case keycode_Func4:
+ case keycode_Func5:
+ case keycode_Func6:
+ case keycode_Func7:
+ case keycode_Func8:
+ case keycode_Func9:
+ case keycode_Func10:
+ case keycode_Func11:
+ case keycode_Func12:
+ return TRUE;
+ }
+ return FALSE;
+}
+
+/**
+ * glk_set_terminators_line_event:
+ * @win: The window for which to set the line input terminator keys.
+ * @keycodes: An array of keycode_ constants, of length @count.
+ * @count: The array length of @keycodes.
+ *
+ * It is possible to request that other keystrokes complete line input as well.
+ * (This allows a game to intercept function keys or other special keys during
+ * line input.) To do this, call glk_set_terminators_line_event(), and pass an
+ * array of @count keycodes. These must all be special keycodes (see Character Input). Do not include
+ * regular printable characters in the array, nor %keycode_Return (which
+ * represents the default enter key and will always be
+ * recognized). To return to the default behavior, pass a %NULL or empty array.
+ *
+ * The glk_set_terminators_line_event() affects subsequent
+ * line input requests in the given window. It does not affect a pending line
+ * input request.
+ *
+ *
+ * This distinction makes life easier for interpreters that set up UI
+ * callbacks only at the start of input.
+ *
+ *
+ * A library may not support this feature; if it does, it may not support all
+ * special keys as terminators. (Some keystrokes are reserved for OS or
+ * interpreter control.) You can test for this with %gestalt_LineTerminators and
+ * %gestalt_LineTerminatorKey.
+ */
+void
+glk_set_terminators_line_event(winid_t win, glui32 *keycodes, glui32 count)
+{
+ VALID_WINDOW(win, return);
+
+ g_slist_free(win->extra_line_terminators);
+ win->extra_line_terminators = NULL;
+
+ if(keycodes == NULL || count == 0)
+ return;
+
+ int i;
+ for(i = 0; i < count; i++)
+ {
+ unsigned key = keycode_to_gdk_keyval(keycodes[i]);
+ if(is_valid_line_terminator(keycodes[i]))
+ win->extra_line_terminators = g_slist_prepend(win->extra_line_terminators, GUINT_TO_POINTER(key));
+ else
+ WARNING_S("Ignoring invalid line terminator", gdk_keyval_name(key));
+ }
+}