Initial push
[sannot.git] / sannot.c
blob8f582269fe819100b8554104f57710b3a3b6311b
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdbool.h>
4 #include <jack/jack.h>
5 #include "sannot.h"
6 #include "ui.h"
7 #include "input.h"
9 struct annotation * annotations = NULL;
11 struct annotation * current_annotation = NULL;
13 struct annotation * selected_annotation = NULL;
15 // find the last annotation before this frame (if any)
16 struct annotation * annotationAt(jack_nframes_t frame)
18 struct annotation * current = annotations;
19 struct annotation * previous = NULL;
20 while (current != NULL)
22 if (current->frame > frame)
24 return previous;
26 previous = current;
27 current = current->next;
29 return previous;
32 void addannotation(struct annotation * annotation)
34 struct annotation * previous = annotationAt(annotation->frame);
35 if (previous == NULL)
37 annotation->next = annotations;
38 annotations = annotation;
40 else
42 annotation->prev = previous;
43 if (previous->next != NULL)
45 previous->next->prev = annotation;
47 previous->next = annotation;
49 selected_annotation = annotation;
52 void createannotation(jack_nframes_t frame, int indentation, char* text)
54 struct annotation * new_annotation = (struct annotation*) malloc(sizeof(struct annotation));
55 new_annotation->frame = frame;
56 new_annotation->indentation = 0;
57 new_annotation->text = text;
58 new_annotation->next = NULL;
59 new_annotation->prev = NULL;
60 addannotation(new_annotation);
63 int size = 80;
64 void editannotation(jack_client_t * client)
66 if (selected_annotation == NULL)
67 return;
68 mvprintw(0,0,"Enter text for this annotation: ");
69 echo();
70 getnstr(selected_annotation->text, size);
71 noecho();
72 halfdelay(10);
75 void newannotation(jack_client_t * client)
77 jack_nframes_t frame = transport_refresh(client);
78 char* text = (char*) malloc(size * sizeof(char));
79 mvprintw(0,0,"Enter text for new annotation: ");
80 echo();
81 getnstr(text, size);
82 noecho();
83 halfdelay(10);
85 createannotation(frame, 0, text);
88 int main()
90 bool quitRequested = false;
91 jack_client_t * client = jack_client_open("sannot", JackNullOption, NULL);
92 if (client == NULL)
94 fprintf(stderr, "could not create JACK client\n");
95 return 1;
98 // Main loop: when a command is entered or when the jack transport reaches
99 // a new annotation, handle this and refresh the screen.
100 init_ui();
101 refresh_ui(transport_refresh(client));
102 while (!quitRequested)
104 struct annotation * new_annotation = annotationAt(transport_refresh(client));
105 refresh_ui(transport_refresh(client));
106 if (new_annotation != current_annotation)
108 current_annotation = new_annotation;
109 refresh_ui(transport_refresh(client));
111 // waits for the next command - times out if none arrives, so
112 // we can check if we need to view another annotation. At some point we
113 // might want to implement this as a 'real' multi-threaded event loop, but
114 // for now this should do fine.
115 switch (getCommand())
117 case none:
118 break;
119 case quit:
120 quitRequested = true;
121 break;
122 case start:
123 transport_start(client);
124 break;
125 case stop:
126 transport_stop(client);
127 break;
128 case toggle_startstop:
129 transport_toggle(client);
130 break;
131 case transport_rewind:
132 transport_move(client, -40000);
133 break;
134 case transport_forward:
135 transport_move(client, 40000);
136 break;
137 case home:
138 transport_moveto(client, 0);
139 break;
140 case edit:
141 editannotation(client);
142 refresh_ui(transport_refresh(client));
143 break;
144 case input:
145 newannotation(client);
146 refresh_ui(transport_refresh(client));
147 break;
148 case up:
149 if (selected_annotation != NULL && selected_annotation->prev != NULL)
150 selected_annotation = selected_annotation->prev;
151 break;
152 case down:
153 if (selected_annotation != NULL && selected_annotation->next != NULL)
154 selected_annotation = selected_annotation->next;
155 break;
156 case indent:
157 if (selected_annotation != NULL)
158 selected_annotation->indentation++;
159 break;
160 case unindent:
161 if (selected_annotation != NULL && selected_annotation->indentation > 0)
162 selected_annotation->indentation--;
163 break;
164 //refresh_ui();
168 exit_ui();
169 jack_client_close(client);
171 return 0;