b1588bc2c75cd47e007b9d9a5e338dc2a01eafa4
[projects/chimara/chimara.git] / tests / soundtest.c
1 #include <libchimara/glk.h>
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <string.h>
5
6 void
7 glk_main(void)
8 {
9         if(!glk_gestalt(gestalt_Sound, 0)) {
10                 fprintf(stderr, "Sound not supported.\n");
11                 return;
12         }
13         if(!glk_gestalt(gestalt_SoundVolume, 0)) {
14                 fprintf(stderr, "Sound volume not supported.\n");
15                 return;
16         }
17         if(!glk_gestalt(gestalt_SoundNotify, 0)) {
18                 fprintf(stderr, "Sound notification not supported.\n");
19                 return;
20         }
21         
22         schanid_t sc = glk_schannel_create(0);
23         if(!sc) {
24                 fprintf(stderr, "Could not create sound channel.\n");
25                 return;
26         }
27
28         /* Open the main window. */
29     winid_t mainwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 1);
30     if (!mainwin) {
31         /* It's possible that the main window failed to open. There's
32             nothing we can do without it, so exit. */
33         return;
34     }
35
36         char buffer[1024];
37         int len;
38         int finish = 0;
39
40         event_t ev;
41         while(!finish) {
42                 glk_put_string("\nprompt> ");
43                 glk_request_line_event(mainwin, buffer, 1024, 0);
44                 glk_select(&ev);
45                 printf("Received event:\n");
46                 printf("Type: %d\n", ev.type);
47                 printf("Win: %d\n", glk_window_get_rock(ev.win) );
48                 printf("Var1: %d\n", ev.val1);
49                 printf("Var2: %d\n", ev.val2);
50                 switch(ev.type) {
51                         case evtype_LineInput:
52                                 // Null-terminate string
53                                 len = ev.val1;
54                                 buffer[len] = '\0';
55
56                                 if(strcmp(buffer, "quit") == 0) {
57                                         glk_put_string("That's all, folks.\n");
58                                         finish = 1;
59                                 } else if(strcmp(buffer, "play") == 0) {
60                                         glk_put_string("Playing sound.\n");
61                                         if(!glk_schannel_play(sc, 3)) {
62                                                 fprintf(stderr, "Could not start sound channel.\n");
63                                                 finish = 1;
64                                         }
65                                 } else if(strcmp(buffer, "help") == 0) {
66                                         glk_put_string("Type PLAY or QUIT.\n");
67                                 }
68                                 break;
69                         case evtype_SoundNotify:
70                                 glk_put_string("\nGot sound notify event!\n");
71                                 break;
72                         default:
73                                 ;
74                 }
75         }
76
77         glk_schannel_stop(sc);
78         glk_schannel_destroy(sc);
79 }