Separated library source code from testing code, fixing #6
[projects/chimara/chimara.git] / tests / gridtest.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <assert.h>
5 #include <libchimara/glk.h>
6
7 void glk_main(void)
8 {
9     event_t ev;
10     winid_t mainwin = glk_window_open(0, 0, 0, wintype_TextGrid, 0);
11     if(!mainwin)
12         return;
13     
14     glk_set_window(mainwin);
15     glk_put_string("Philip en Marijn zijn vet goed.\n");
16     glk_put_string("A veeeeeeeeeeeeeeeeeeeeeeeeeeeery looooooooooooooooooooooooong striiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiing.\n");
17     
18     int count;
19     for(count = 0; count < 30; count++)
20         glk_put_string("I want to write past the end of this text buffer! ");
21     
22     glui32 width, height;
23     glk_window_get_size(mainwin, &width, &height);
24     fprintf(stderr, "\nWidth: %d\nHeight: %d\nPress a key in the window, not in the terminal.\n", width, height);
25     glk_request_char_event(mainwin);
26     while(1) {
27         glk_select(&ev);
28         if(ev.type == evtype_CharInput)
29             break;
30     }
31     
32     glk_window_move_cursor(mainwin, 15, 15);
33     glk_put_string(". . ");
34     glk_window_move_cursor(mainwin, 15, 16);
35     glk_put_string(" . .");
36     glk_window_move_cursor(mainwin, 15, 17);
37     glk_put_string(". . ");
38     glk_window_move_cursor(mainwin, 15, 18);
39     glk_put_string(" . .");
40     fprintf(stderr, "Cursor location test.\nPress another key.\n");
41     glk_request_char_event(mainwin);
42     while(1) {
43         glk_select(&ev);
44         if(ev.type == evtype_CharInput)
45             break;
46     }
47     
48     char *buffer = calloc(256, sizeof(char));
49     assert(buffer);
50     
51     fprintf(stderr, "Line input field until end of line\n");
52     glk_window_move_cursor(mainwin, 10, 20);
53     glk_request_line_event(mainwin, buffer, 256, 0);
54     while(1) {
55         glk_select(&ev);
56         if(ev.type == evtype_LineInput)
57             break;
58     }
59     
60     fprintf(stderr, "Now edit your previous line input\n");
61     glk_window_move_cursor(mainwin, 10, 22);
62     glk_request_line_event(mainwin, buffer, 256, strlen(buffer));
63     while(1) {
64         glk_select(&ev);
65         if(ev.type == evtype_LineInput)
66             break;
67     }
68     
69         char *text = calloc(ev.val1 + 1, sizeof(char));
70         assert(text);
71         strncpy(text, buffer, ev.val1);
72         text[ev.val1] = '\0';
73     fprintf(stderr, "Your string was: '%s'.\nPress another key to clear the window and exit.\n", text);
74         free(text);
75     glk_request_char_event(mainwin);
76     while(1) {
77         glk_select(&ev);
78         if(ev.type == evtype_CharInput)
79             break;
80     }
81     
82     glk_window_clear(mainwin);
83     free(buffer);
84 }