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