4 /* Global current stream */
5 static strid_t current_stream = NULL;
6 /* List of streams currently in existence */
7 static GList *stream_list = NULL;
11 * @str: A stream, or #NULL.
12 * @rockptr: Return location for the next window's rock, or #NULL.
14 * Iterates over the list of streams; if @str is #NULL, it returns the first
15 * stream, otherwise the next stream after @str. If there are no more, it
16 * returns #NULL. The stream's rock is stored in @rockptr. If you don't want
17 * the rocks to be returned, you may set @rockptr to #NULL.
19 * The order in which streams are returned is arbitrary. The order may change
20 * every time you create or destroy a stream, invalidating the iteration.
22 * Returns: the next stream, or #NULL if there are no more.
25 glk_stream_iterate(strid_t str, glui32 *rockptr)
30 retnode = stream_list;
32 retnode = str->stream_list->next;
33 strid_t retval = retnode? (strid_t)retnode->data : NULL;
35 /* Store the stream's rock in rockptr */
37 *rockptr = glk_stream_get_rock(retval);
43 * glk_stream_get_rock:
46 * Returns the stream @str's rock value.
48 * Returns: A rock value.
51 glk_stream_get_rock(strid_t str)
53 g_return_val_if_fail(str != NULL, 0);
57 /* Internal function: create a window stream to go with window. */
59 window_stream_new(winid_t window)
61 /* Create stream and connect it to window */
62 strid_t s = g_new0(struct glk_stream_struct, 1);
63 s->file_mode = filemode_Write;
64 s->stream_type = STREAM_TYPE_WINDOW;
66 /* Add it to the global stream list */
67 stream_list = g_list_prepend(stream_list, s);
68 s->stream_list = stream_list;
74 * glk_stream_set_current:
75 * @str: An output stream, or NULL.
77 * Sets the current stream to @str, or to nothing if @str is #NULL.
80 glk_stream_set_current(strid_t str)
82 if(str != NULL && str->file_mode != filemode_Write)
84 g_warning("glk_stream_set_current: "
85 "Cannot set current stream to non output stream");
92 /* Internal function: change illegal (control) characters in a string to a
93 placeholder character. Must free returned string afterwards. */
95 remove_latin1_control_characters(gchar *s)
97 gchar *retval = g_strdup(s);
99 for(ptr = (unsigned char *)retval; *ptr != '\0'; ptr++)
100 if( (*ptr < 32 && *ptr != 10) || (*ptr >= 127 && *ptr <= 159) )
102 /* Our placeholder character is '?'; other options are possible,
103 like printing "0x7F" or something */
109 * @s: A null-terminated string in Latin-1 encoding.
111 * Prints @s to the current stream.
114 glk_put_string(char *s)
116 /* Illegal to print to the current stream if it is NULL */
117 g_return_if_fail(current_stream != NULL);
119 GError *error = NULL;
120 gchar *canonical, *utf8;
122 switch(current_stream->stream_type)
124 case STREAM_TYPE_WINDOW:
125 canonical = remove_latin1_control_characters(s);
126 utf8 = g_convert(canonical, -1, "UTF-8", "ISO-8859-1", NULL, NULL,
132 g_warning("glk_put_string: "
133 "Error during latin1->utf8 conversion: %s",
139 /* Each window type has a different way of printing to it */
140 switch(current_stream->window->window_type)
142 /* Printing to a these windows' streams does nothing */
145 case wintype_Graphics:
146 current_stream->write_count += strlen(s);
148 /* Text buffer window */
149 case wintype_TextBuffer:
151 GtkTextBuffer *buffer = gtk_text_view_get_buffer(
152 GTK_TEXT_VIEW(current_stream->window->widget) );
155 gtk_text_buffer_get_end_iter(buffer, &iter);
157 gtk_text_buffer_insert(buffer, &iter, utf8, -1);
159 current_stream->write_count += strlen(s);
162 g_warning("glk_put_string: "
163 "Writing to this kind of window unsupported.");
169 g_warning("glk_put_string: "
170 "Writing to this kind of stream unsupported.");