File: ui.c

package info (click to toggle)
aylet 0.5-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 264 kB
  • ctags: 403
  • sloc: ansic: 3,648; makefile: 46; sh: 23
file content (308 lines) | stat: -rw-r--r-- 6,510 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/* aylet 0.2, a .AY music file player.
 * Copyright (C) 2001 Russell Marks and Ian Collier. See main.c for licence.
 *
 * ui.c - curses UI code (and no-UI UI :-)).
 */

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <signal.h>
#include <curses.h>
#include "main.h"

#include "ui.h"


static int frame_x=1,frame_y=5;	/* frame_y is adjusted */
/* XXX should adjust frame_x when >80 cols, too... */

static int used_curses_ui=0,need_update=1,need_nexttrack=0;


static void clearscreen(void)
{
wclear(stdscr);
move(0,0); refresh();
}


static void draw_frame(void)
{
int y=frame_y;

move(frame_y,frame_x);
addstr(",--------------------------------------");
addstr("--------------------------------------.");
move(frame_y+12,frame_x);
addstr("`--------------------------------------");
addstr("--------------------------------------'");
move(frame_y+6,frame_x+1);
addstr("--------------------------------------");
addstr("--------------------------------------");
move(frame_y+9,frame_x+1);
addstr("--------------------------------------");
addstr("--------------------------------------");

for(y=frame_y+1;y<frame_y+12;y++)
  {
  mvaddch(y,frame_x,'|');
  mvaddch(y,frame_x+77,'|');
  }

mvaddstr(frame_y+1,frame_x+2,"   File:");
mvaddstr(frame_y+2,frame_x+2,"   Misc:");
mvaddstr(frame_y+3,frame_x+2," Author:");
mvaddstr(frame_y+4,frame_x+2," Tracks:");
mvaddstr(frame_y+5,frame_x+2,"Playing:");
mvaddstr(frame_y+7,frame_x+2," Status:");
mvaddstr(frame_y+8,frame_x+2,"   Time:");
for(y=frame_y+7;y<=frame_y+8;y++)
  {
  mvaddch(y,frame_x+24,'|');
  mvaddch(y,frame_x+52,'|');
  }
mvaddstr(frame_y+7,frame_x+30,"[H]igh-speed:");
mvaddstr(frame_y+7,frame_x+55,"[S]top after:");
mvaddstr(frame_y+8,frame_x+55,"[F]ade time:");

mvaddstr(frame_y+10,frame_x+4,
         "[ z - prev | x - play | c - pause | v - stop | b - next |"
         " r - restart ]");
mvaddstr(frame_y+11,frame_x+12,
         "[ space - next file | del - previous file | q - quit ]");

move(0,0);
refresh();
}


static char *fitwidth(char *str,int decr)
{
static char buf[65+1];	/* width allowed, plus NUL */
int len=strlen(str);
int maxlen=sizeof(buf)-1-decr;

memset(buf,' ',maxlen);
buf[maxlen]=0;

if(str && sizeof(buf)-decr>=1)
  {
  if(len>maxlen) len=maxlen;
  memcpy(buf,str,len);
  }

return(buf);
}


static void draw_status(char *filename,char *misc,
                        char *author,int track,char *playingstr)
{
char *ptr=strrchr(filename,'/');

mvaddstr(frame_y+1,frame_x+11,fitwidth(ptr?ptr+1:filename,0));
mvaddstr(frame_y+2,frame_x+11,fitwidth(misc,0));
mvaddstr(frame_y+3,frame_x+11,fitwidth(author,0));
mvprintw(frame_y+4,frame_x+11,"%3d",aydata.num_tracks);
mvprintw(frame_y+5,frame_x+11,"%3d - %s",track,fitwidth(playingstr,6));

mvaddstr(frame_y+7,frame_x+11,paused?"paused ":(playing?"playing":"stopped"));
mvaddstr(frame_y+7,frame_x+44,highspeed?"on ":"off");

move(frame_y+7,frame_x+69);
if(!stopafter)
  addstr("--:--");
else
  printw("%2d:%02d",stopafter/60,stopafter%60);

mvprintw(frame_y+8,frame_x+68,"%2d sec",fadetime);

move(0,0);
refresh();
}


static void draw_status_timeonly(void)
{
mvprintw(frame_y+8,frame_x+11,"%2d:%02d  ",tunetime.min,tunetime.sec);

move(0,0);
refresh();
}


static int non_ui_frame(void)
{
static int oldfile=-1;
static int oldtrack=-1;

if(need_update)
  {
  need_update=0;
  if(ay_file!=oldfile)
    {
    char *filename=ay_filenames[ay_file];
    char *ptr=strrchr(filename,'/');

    fprintf(stderr,
            "\n   File: %s\n   Misc: %s\n Author: %s\n Tracks: %3d\n",
            ptr?ptr+1:filename,aydata.miscstr,aydata.authorstr,
            aydata.num_tracks);
    }
  if(ay_file!=oldfile || ay_track!=oldtrack)
    {
    oldtrack=ay_track;
    fprintf(stderr,
            "Playing: %3d - %s\n",
            ay_track+1,aydata.tracks[ay_track].namestr);
    }
  oldfile=ay_file;
  }

/* quit if we stop (i.e. when we've played all tracks) */
if(!playing && !paused)
  return(action_callback(cb_quit));

if(need_nexttrack)
  {
  need_nexttrack=0;
  return(action_callback(cb_next_track));
  }

return(1);
}


/* called per 1/50th, this deals with the usual events.
 * returns zero if we want to stop the current track.
 */
int ui_frame(void)
{
enum cb_action_tag action;

if(!use_ui)
  return(non_ui_frame());

if(need_update)
  {
  need_update=0;
  draw_frame();
  draw_status(ay_filenames[ay_file],aydata.miscstr,aydata.authorstr,
              ay_track+1,aydata.tracks[ay_track].namestr);
  }

/* update time display */
draw_status_timeonly();

/* read keys */
action=cb_none;
switch(getch())
  {
  case 27: case 'q':	action=cb_quit; break;
  case 'h':		action=cb_highspeed; break;
#if KEY_BACKSPACE!=8
  case 8:
#endif
#if KEY_DC!=127
  case 127:
#endif
  case KEY_BACKSPACE: case KEY_DC:
    action=cb_prev_file; break;
  case ' ':		action=cb_next_file; break;
  case 'z':		action=cb_prev_track; break;
  case 'b':		action=cb_next_track; break;
  case 'x':		action=cb_play; break;
  case 'c':		action=cb_pause; break;
  case 'v':		action=cb_stop; break;
  case 'r':		action=cb_restart; break;
  case 'S':		action=cb_dec_stopafter; break;
  case 's':		action=cb_inc_stopafter; break;
  case 'F':		action=cb_dec_fadetime; break;
  case 'f':		action=cb_inc_fadetime; break;
  }

if(action==cb_none)
  return(1);

return(action_callback(action));
}


/* called if playback status has changed without us knowing. */
void ui_change_notify(void)
{
need_update=1;
}


static void non_ui_ctrlc(int foo)
{
if(tunetime.min==0 && tunetime.sec==0 &&
   tunetime.subsecframes<25)
  playing=0;	/* quit */
else
  need_nexttrack=1;
}


static void non_ui_init(void)
{
struct sigaction sa;

sa.sa_flags=SA_RESTART;
sa.sa_handler=non_ui_ctrlc;
sigaction(SIGINT,&sa,NULL);

need_update=1;
}


void ui_init(int argc,char **argv)
{
if(!use_ui)
  {
  non_ui_init();
  return;
  }

used_curses_ui=1;

initscr();
cbreak(); noecho();
curs_set(0);	/* remove cursor (if possible) */
keypad(stdscr,TRUE);
nodelay(stdscr,1);

/* XXX do I need to enable SIGWINCH handling in ncurses myself? */

/* check term size */
if(COLS<80 || LINES<13)
  {
  ui_end();
  fprintf(stderr,"aylet: "
  		"sorry, need a terminal with at least 80 cols and 13 lines.\n");
  exit(1);
  }

frame_y=(LINES-13)/2;

clearscreen();
}


void ui_end(void)
{
if(!use_ui || !used_curses_ui) return;

clearscreen();
move(LINES-1,0);
refresh();
nodelay(stdscr,0);
echo(); nocbreak();
endwin();
putchar('\n');

/* in case of error exit, since stderr will be used immediately after... */
fflush(stdout);
}