Add custom renderer
[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         glk_set_window(mainwin);
36         glk_put_string("Copy a sound file to the current directory and rename it "
37             "to SND3. Supported formats: AIFF, OGG, MOD, S3M, IT, XM. Type 'play' "
38             "to play it.\n");
39
40         char buffer[1024];
41         int len;
42         int finish = 0;
43         int repeat = 1;
44
45         event_t ev;
46         while(!finish) {
47                 glk_put_string("\nprompt> ");
48                 glk_request_line_event(mainwin, buffer, 1024, 0);
49                 glk_select(&ev);
50                 printf("Received event:\n");
51                 printf("Type: %d\n", ev.type);
52                 printf("Win: ");
53                 if(ev.win)
54                         printf( "%d\n", glk_window_get_rock(ev.win) );
55                 else
56                         printf("NULL\n");
57                 printf("Var1: %d\n", ev.val1);
58                 printf("Var2: %d\n", ev.val2);
59                 switch(ev.type) {
60                         case evtype_LineInput:
61                                 // Null-terminate string
62                                 len = ev.val1;
63                                 buffer[len] = '\0';
64
65                                 if(strcmp(buffer, "quit") == 0) {
66                                         glk_put_string("That's all, folks.\n");
67                                         finish = 1;
68                                 } else if(strcmp(buffer, "play") == 0) {
69                                         glk_put_string("Playing sound.\n");
70                                         if(!glk_schannel_play_ext(sc, 3, repeat, 1)) {
71                                                 fprintf(stderr, "Could not start sound channel.\n");
72                                                 finish = 1;
73                                         }
74                                 } else if(strcmp(buffer, "stop") == 0) {
75                                         glk_put_string("Stopping sound.\n");
76                                         glk_schannel_stop(sc);
77                                 } else if(strcmp(buffer, "repeat") == 0) {
78                                         glk_put_string("Setting repeat to ");
79                                         if(repeat == 1) {
80                                                 glk_put_string("TWICE.\n");
81                                                 repeat = 2;
82                                         } else if(repeat == 2) {
83                                                 glk_put_string("INFINITE.\n");
84                                                 repeat = -1;
85                                         } else if(repeat == -1) {
86                                                 glk_put_string("DON'T PLAY.\n");
87                                                 repeat = 0;
88                                         } else if(repeat == 0) {
89                                                 glk_put_string("ONCE.\n");
90                                                 repeat = 1;
91                                         }
92                                 } else if(strcmp(buffer, "help") == 0) {
93                                         glk_put_string("Type PLAY or REPEAT or STOP or QUIT.\n");
94                                 }
95                                 break;
96                         case evtype_SoundNotify:
97                                 glk_cancel_line_event(mainwin, NULL);
98                                 glk_put_string("\nGot sound notify event!\n");
99                                 break;
100                         default:
101                                 ;
102                 }
103         }
104
105         glk_schannel_stop(sc);
106         glk_schannel_destroy(sc);
107 }