~ubuntu-branches/ubuntu/oneiric/xmp/oneiric

« back to all changes in this revision

Viewing changes to src/main/main.c

  • Committer: Bazaar Package Importer
  • Author(s): Artur Rona
  • Date: 2010-01-15 01:15:22 UTC
  • mfrom: (1.1.8 upstream) (4.2.3 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100115011522-r4otlsg687ee1yx4
Tags: 3.0.0+20090923-1ubuntu1
* Merge from debian testing (LP: #507732).  Reamining changes:
  + Add PulseAudio support:
    - debian/rules: Add --enable-pulseaudio to ./configure
  + RFE Compile with 128 bit mixer:
    - dpatch support

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include <time.h>
30
30
#include <sys/time.h>
31
31
#include <sys/types.h>
 
32
#ifdef __CYGWIN__
 
33
#include <sys/select.h>
 
34
#endif
32
35
#ifdef HAVE_SIGNAL_H
33
36
#include <signal.h>
34
37
#endif
72
75
int randomize = 0;
73
76
int loadonly = 0;
74
77
int nocmd = 0;
 
78
int showtime = 0;
75
79
#ifdef HAVE_SYS_RTPRIO_H
76
80
int rt = 0;
77
81
#endif
96
100
#endif
97
101
 
98
102
void get_options (int, char **, struct xmp_options *, xmp_context);
 
103
void init_drivers (void);
99
104
 
100
105
static xmp_context ctx;
 
106
static int paused;
101
107
 
102
108
 
103
109
static int set_tty()
129
135
    if (background)
130
136
        return -1;
131
137
 
132
 
    if (tcsetattr (0, TCSAFLUSH, &term) < 0) {
133
 
        fprintf (stderr, "can't reset terminal!\n");
 
138
    if (tcsetattr(0, TCSAFLUSH, &term) < 0) {
 
139
        fprintf(stderr, "can't reset terminal!\n");
134
140
        return -1;
135
141
    }
136
142
#endif
145
151
static void sigtstp_handler()
146
152
{
147
153
    if (!stopped) {
148
 
        fprintf (stderr, "] - STOPPED\n");
 
154
        if (showtime)
 
155
            fprintf(stderr, "\n");
 
156
        else if (verbosity)
 
157
            fprintf(stderr, "] - STOPPED\n");
149
158
        xmp_timer_stop(ctx);
150
159
        stopped = 1;
151
160
    }
207
216
 
208
217
#endif /* HAVE_SIGNAL_H */
209
218
 
 
219
#ifdef __CYGWIN__
 
220
/*
 
221
 * from daniel åkerud <daniel.akerud@gmail.com>
 
222
 * date Tue, Jul 28, 2009 at 9:59 AM
 
223
 *
 
224
 * Under Cygwin, the read() in process_echoback blocks because VTIME = 0
 
225
 * is not handled correctly. To fix this you can either:
 
226
 *
 
227
 * 1. Enable "tty emulation" in Cygwin using an environment variable.
 
228
 * http://www.mail-archive.com/cygwin@cygwin.com/msg99417.html
 
229
 * For me this is _very_ slow and I can see the characters as they are
 
230
 * typed out when running xmp. I have not investigated why this is
 
231
 * happening, but there is of course a reason why this mode is not
 
232
 * enabled by default.
 
233
 * 
 
234
 * 2. Do a select() before read()ing if the platform is Cygwin.
 
235
 * This makes Cygwin builds work out of the box with no fiddling around,
 
236
 * but does impose a neglectible cpu overhead (for Cygwin builds only).
 
237
 */
 
238
static int stdin_ready_for_reading()
 
239
{
 
240
    fd_set fds;
 
241
    struct timeval tv;
 
242
    int ret;
 
243
    
 
244
    tv.tv_sec = 0;
 
245
    tv.tv_usec = 0;
 
246
    
 
247
    FD_ZERO(&fds);
 
248
    FD_SET(STDIN_FILENO, &fds);
 
249
    
 
250
    ret = select(STDIN_FILENO + 1, &fds, NULL, NULL, &tv);
 
251
    
 
252
    if (ret > 0 && FD_ISSET(STDIN_FILENO, &fds))
 
253
        return 1;
 
254
    
 
255
    return 0;
 
256
}
 
257
#endif
210
258
 
211
259
static void process_echoback(unsigned long i)
212
260
{
213
261
    unsigned long msg = i >> 4;
214
 
    unsigned char cmd;
215
262
    static int _pos = -1;
216
263
    static int tpo, bpm, nch, _tpo = -1, _bpm = -1;
217
264
    static int pos, pat;
218
 
    static int pause = 0;
219
 
    int k;
220
265
 
221
266
#ifdef SIGUSR1
222
267
    if (sigusr == SIGUSR1) {
223
268
        skip = 1;
224
269
        xmp_mod_stop(ctx);
225
 
        if (pause)
226
 
            pause = xmp_mod_pause(ctx);
 
270
        paused = 0;
227
271
        sigusr = 0;
228
272
    } else if (sigusr == SIGUSR2) {
229
273
        skip = -1;
230
274
        xmp_mod_stop(ctx);
231
 
        if (pause)
232
 
            pause = xmp_mod_pause(ctx);
 
275
        paused = 0;
233
276
        sigusr = 0;
234
277
    }
235
278
#endif
237
280
    if (background)
238
281
        return;
239
282
 
 
283
    if (showtime) {
 
284
        switch (i & 0xf) {
 
285
        case XMP_ECHO_TIME:
 
286
            if (1 || showtime) {
 
287
                int rem = mi.time / 100 - msg;
 
288
                fprintf(stderr, "\r%02d:%02d:%02d.%d  ",
 
289
                        (int)(msg / (60 * 600)), (int)((msg / 600) % 60),
 
290
                        (int)((msg / 10) % 60), (int)(msg % 10));
 
291
                fprintf(stderr, "-%02d:%02d:%02d.%d",
 
292
                        (int)(rem / (60 * 600)), (int)((rem / 600) % 60),
 
293
                        (int)((rem / 10) % 60), (int)(rem % 10));
 
294
            }
 
295
            break;
 
296
        }
 
297
    }
240
298
    if (verbosity) {
241
299
        switch (i & 0xf) {
242
300
        case XMP_ECHO_BPM:
255
313
                max_nch = nch;
256
314
            break;
257
315
        case XMP_ECHO_ROW:
 
316
            if (showtime)
 
317
                break;
258
318
            rows += 1;
259
319
            tot_nch += nch;
260
320
            if
271
331
            break;
272
332
        }
273
333
    }
 
334
}
 
335
 
 
336
 
 
337
void read_keyboard()
 
338
{
 
339
    unsigned char cmd;
 
340
    int k;
274
341
 
275
342
    /* Interactive commands */
276
343
 
278
345
        return;
279
346
 
280
347
#if defined HAVE_TERMIOS_H && !defined WIN32
 
348
#ifdef __CYGWIN__
 
349
    k = 0;
 
350
    if (stdin_ready_for_reading())
 
351
#endif
281
352
    k = read(0, &cmd, 1);
282
353
#elif defined WIN32
283
354
    k = cmd = kbhit() ? getch() : 0;
301
372
        case 'q':       /* quit */
302
373
            skip = -2;
303
374
            xmp_mod_stop(ctx);
304
 
            if (pause)
305
 
                pause = xmp_mod_pause(ctx);
 
375
            paused = 0;
306
376
            break;
307
377
        case 'f':       /* jump to next order */
308
378
            xmp_ord_next(ctx);
309
 
            if (pause)
310
 
                pause = xmp_mod_pause(ctx);
 
379
            paused = 0;
311
380
            break;
312
381
        case 'b':       /* jump to previous order */
313
382
            xmp_ord_prev(ctx);
314
 
            if (pause)
315
 
                pause = xmp_mod_pause(ctx);
 
383
            paused = 0;
316
384
            break;
317
385
        case 'n':       /* skip to next module */
318
386
            skip = 1;
319
387
            xmp_mod_stop(ctx);
320
 
            if (pause)
321
 
                pause = xmp_mod_pause(ctx);
 
388
            paused = 0;
322
389
            break;
323
390
        case 'p':       /* skip to previous module */
324
391
            skip = -1;
325
392
            xmp_mod_stop(ctx);
326
 
            if (pause)
327
 
                pause = xmp_mod_pause(ctx);
 
393
            paused = 0;
328
394
            break;
329
 
        case ' ':       /* pause module */
330
 
            fprintf (stderr, "%s",  (pause = xmp_mod_pause(ctx))
331
 
                ? "] - PAUSED\b\b\b\b\b\b\b\b\b\b"
332
 
                : "]         \b\b\b\b\b\b\b\b\b\b");
 
395
        case ' ':       /* paused module */
 
396
            paused ^= 1;
 
397
            if (verbosity) {
 
398
                fprintf (stderr, "%s",  paused ?
 
399
                                "] - PAUSED\b\b\b\b\b\b\b\b\b\b" :
 
400
                                "]         \b\b\b\b\b\b\b\b\b\b");
 
401
            }
333
402
            break;
334
403
        case '1':
335
404
        case '2':
367
436
}
368
437
 
369
438
 
370
 
int main (int argc, char **argv)
 
439
int main(int argc, char **argv)
371
440
{
372
441
    int i, t, lf_flag, first, num_mod, verb = 0;
373
 
    time_t t0, t1;
 
442
    time_t t0, t1, t2, t3;
374
443
#ifndef WIN32
375
444
    struct timeval tv;
376
445
    struct timezone tz;
384
453
    long rc;
385
454
#endif
386
455
 
 
456
    init_drivers();
 
457
 
387
458
    ctx = xmp_create_context();
388
459
 
389
460
    xmp_init(ctx, argc, argv);
444
515
        }
445
516
    }
446
517
 
447
 
    xmp_register_event_callback(process_echoback);
 
518
    xmp_register_event_callback(ctx, process_echoback);
448
519
 
449
520
    if (opt->verbosity) {
450
 
        fprintf (stderr, "Extended Module Player %s %s\n"
451
 
        "Copyright (C) 1996-2009 Claudio Matsuoka and Hipolito Carraro Jr\n",
452
 
            xmp_version, xmp_date);
453
 
#ifdef __EMX__
454
 
        fprintf(stderr, "OS/2 Port by Kevin Langman (langman@earthling.net)\n" );
455
 
#endif
 
521
        fprintf(stderr, "Extended Module Player " VERSION "\n"
 
522
        "Copyright (C) 1996-2009 Claudio Matsuoka and Hipolito Carraro Jr\n");
456
523
    }
457
524
 
458
525
    if (probeonly || (opt->verbosity)) {
512
579
 
513
580
    set_tty ();
514
581
 
 
582
    paused = 0;
515
583
    time (&t0);
516
584
 
517
585
    num_mod = lf_flag = 0;
569
637
        if (loadonly)
570
638
            goto skip_play;
571
639
 
572
 
        t = xmp_play_module(ctx);
 
640
        /* Play the module */
 
641
        time(&t2);
 
642
        xmp_player_start(ctx);
 
643
        for (;;) {
 
644
                read_keyboard();
 
645
 
 
646
                if (paused) {
 
647
                        usleep(100000);
 
648
                } else {
 
649
                        if (xmp_player_frame(ctx) != 0)
 
650
                                break;
 
651
                        xmp_play_buffer(ctx);
 
652
                }
 
653
        }
 
654
        xmp_player_end(ctx);
 
655
        time(&t3);
 
656
        t = difftime(t3, t2);
573
657
 
574
658
        xmp_release_module(ctx);
575
659
 
 
660
        if (showtime) {
 
661
            fprintf(stderr, "\r                       \r");
 
662
        }
576
663
        if (opt->verbosity && !background) {
577
664
            fprintf (stderr,
578
665
"\rElapsed time   : %dmin%02ds %s                                \n",