Added dynamic module loading: now the Glk program (i.e., the
[rodin/chimara.git] / src / model.c
1 #include "stdio.h"
2 #include "glk.h"
3
4 static winid_t mainwin = NULL;
5
6 void sayit(void)
7 {
8         fprintf(stderr, "I'm the interrupt handler!\n");
9 }
10
11 void glk_main(void)
12 {
13     /* Open the main window. */
14     mainwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 1);
15     if (!mainwin) {
16         /* It's possible that the main window failed to open. There's
17             nothing we can do without it, so exit. */
18         return; 
19     }
20     
21         
22 /*    char buffer[256];
23     int i;
24     for(i = 0; i < 256; i++)
25         buffer[i] = (char)glk_char_to_upper(i);
26     
27     frefid_t f = glk_fileref_create_temp(fileusage_BinaryMode, 0);
28     if(f) 
29     {
30     
31         strid_t s = glk_stream_open_file(f, 
32                 filemode_ReadWrite, 0);
33         glk_stream_set_current(s);
34         
35         glk_put_char('X');
36         glk_put_string("Philip en Marijn zijn vet goed.\n");
37         glk_put_buffer(buffer, 256);
38
39         glk_stream_set_position(s, 0, seekmode_Start);
40         glk_set_window(mainwin);
41         glk_put_char( glk_get_char_stream(s) );
42         glk_put_char('\n');
43         g_printerr("Line read: %d\n", glk_get_line_stream(s, buffer, 256));
44         glk_put_string(buffer);
45         int count = glk_get_buffer_stream(s, buffer, 256);
46         g_printerr("Buffer read: %d\n", count);
47         glk_put_buffer(buffer, count);          
48         
49         stream_result_t result;
50         glk_stream_close(s, &result);
51         
52         g_printerr("Read count: %d\nWrite count: %d\n", result.readcount,
53                 result.writecount);
54                 glk_fileref_destroy(f);
55         }
56         */
57
58         glk_set_window(mainwin);
59
60         glk_set_interrupt_handler(&sayit);
61
62         gchar buffer[256];
63         event_t ev;
64         while(1) {
65                 glk_put_string("prompt> ");
66                 glk_request_line_event(mainwin, buffer, 256, 0);
67                 glk_select(&ev);
68                 switch(ev.type) {
69                         default:
70                                 printf("Received event:\n");
71                                 printf("Type: %d\n", ev.type);
72                                 printf("Win: %d\n", glk_window_get_rock(ev.win));
73                                 printf("Var1: %d\n", ev.val1);
74                                 printf("Var2: %d\n", ev.val2);
75                 }
76         }
77         
78         /* Bye bye */
79         glk_exit();
80 }