File: remote.c

package info (click to toggle)
mpg321 0.3.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 876 kB
  • sloc: ansic: 7,038; sh: 767; makefile: 46
file content (277 lines) | stat: -rw-r--r-- 6,815 bytes parent folder | download | duplicates (5)
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
/*
    mpg321 - a fully free clone of mpg123.
    Copyright (C) 2001 Joe Drew
    
    Originally based heavily upon:
    plaympeg - Sample MPEG player using the SMPEG library
    Copyright (C) 1999 Loki Entertainment Software
    
    Also uses some code from
    mad - MPEG audio decoder
    Copyright (C) 2000-2001 Robert Leslie
    
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "mpg321.h"

#include <sys/time.h>
#include <string.h>
#include <unistd.h>

char remote_input_buf[PATH_MAX + 5];
#ifdef HAVE_ALSA
extern long volume_max;
#endif

static
enum mad_flow remote_parse_input(buffer *buf, playlist *pl)
{
    char input[PATH_MAX + 5]; /* for filename as well as input and space */
    char new_remote_input_buf[PATH_MAX + 5];
    char *arg;
    int numread;
    int alreadyread;
    int linelen;

    fd_set fd;
    struct timeval tv = { 0, 0 };
    FD_ZERO(&fd);
    FD_SET(0,&fd);

    alreadyread = strlen (remote_input_buf);

    if (select (1, &fd, NULL, NULL, &tv))
    {
      if (!(numread = read(0, remote_input_buf + alreadyread, (sizeof(input)-1)-alreadyread)) > 0)
      {
        numread = 0;
        /* fprintf(stderr, "remote_parse_input() called with no input queued!");
        exit(1);
        This never happens.. (read blocks) */
      }
    } else {
      numread = 0;
    }

    remote_input_buf[numread+alreadyread] = '\0';
    
    if ((arg = strchr(remote_input_buf, '\n')))
    {
        *(arg) = '\0';
    }

    linelen = strlen (remote_input_buf);

    strcpy (input, remote_input_buf);
    /* input[linelen] = '\0'; */
    strcpy (new_remote_input_buf, remote_input_buf + linelen + 1);
    /* don't copy the \0... */
    strcpy (remote_input_buf, new_remote_input_buf);

    trim_whitespace(input); /* Trims on left and right side only */

    if (strlen(input) == 0)
        return 0;

    arg = strchr(input, ' ');

    if (arg)
    {
        *(arg++) = '\0';
        arg = strdup(arg);
        trim_whitespace(arg);
    }

    if (strcasecmp(input, "L") == 0 || strcasecmp(input, "LOAD") == 0)
    {
        if(arg)
        {
            status = MPG321_PLAYING;
            play_remote_file(pl, arg);
            current_frame = 0;
            options.seek = 0;
            pause_play(NULL, NULL); /* reset pause data */
            goto stop;
        }

        else
        {
            /* this works because if there's no argument, input is just
              'l' or 'load' */
            printf("@E Missing argument to '%s'\n",input);
            return 0;
        }
    }

    else if (strcasecmp(input, "J") == 0 || strcasecmp(input, "JUMP") == 0)
    {
        if (status == MPG321_PLAYING && arg)
        {
            /* relative seek */
            if (arg[0] == '-' || arg[0] == '+')
            {
                signed long toMove = atol(arg);
            
                /* on forward seeks we don't need to stop decoding */
                enum mad_flow toDo = move(buf, toMove);
                
                if (arg) 
                    free(arg);

                return toDo;
            }
            
            /* absolute seek */
            else
            {
                long toSeek = atol(arg);
                
                seek(buf, toSeek);
                goto stop;
            }
        }
        
        else
        {
            /* mpg123 does no error checking, so we should emulate them */
        }                        
    }
    
    else if (strcasecmp(input, "S") == 0 || strcasecmp(input, "STOP") == 0)
    {
        if (status != MPG321_STOPPED)
        {
            status = MPG321_STOPPED;
            clear_remote_file(pl);
            current_frame = 0;
            pause_play(NULL, NULL); /* reset pause data */
	    if(options.opt & MPG321_ENABLE_BUFFER) 
	    {
		    Decoded_Frames->total_played_frames = 0;
		    Decoded_Frames->stop_playing_file = 1;
	    }
            printf("@P 0\n");
        }
        
        goto stop;
    }
    
    else if (strcasecmp(input, "Q") == 0 || strcasecmp(input, "QUIT") == 0)
    {
        quit_now = 1;
	if(options.opt & MPG321_ENABLE_BUFFER) 
		    Decoded_Frames->quit_now = 1;
        goto stop;
    }
    
    else if (strcasecmp(input, "P") == 0 || strcasecmp(input, "PAUSE") == 0)
    {
        if (status == MPG321_PLAYING || status == MPG321_PAUSED)
        {
            pause_play(buf, pl);
        }

        goto stop;
    }

    else if (strcasecmp(input, "G") == 0 || strcasecmp(input, "GAIN") == 0)
    {
       if (arg)
       {
#ifdef HAVE_ALSA
	       if(options.opt & MPG321_ENABLE_BUFFER)
		       mpg321_alsa_set_volume((long)atol(arg)*volume_max/100);
	       else{
#endif
		       if(!(options.opt & MPG321_ENABLE_BUFFER))
		    	       options.volume = mad_f_tofixed(atoi(arg)/100.0);
#ifdef HAVE_ALSA
	       }
#endif
           free(arg);
       }
       else
       {
#ifdef HAVE_ALSA
	       if(options.opt & MPG321_ENABLE_BUFFER)
	           fprintf(stderr, "@G %lu\n", (int)(100*mpg321_alsa_get_volume()/volume_max));
	       else{
#endif
		       if(!(options.opt & MPG321_ENABLE_BUFFER))
		           fprintf(stderr, "@G %d\n", (int)((double)options.volume / (1 << MAD_F_FRACBITS) * 100.0));
#ifdef HAVE_ALSA
	       }
#endif
       }

       return MAD_FLOW_CONTINUE;
    }

    else 
    {
        fprintf(stderr, "@E Unknown command '%s'\n", input);

    }

    if (arg) 
        free(arg);
    return 0;

stop:
    if (arg)
        free(arg);
    return MAD_FLOW_STOP;    
}

void remote_get_input_wait(buffer *buf)
{
    fd_set fd;
    FD_ZERO(&fd);
    FD_SET(0,&fd);
    
    if (!strlen (remote_input_buf))
    {
      select(1, &fd, NULL, NULL, NULL);
    }

    remote_parse_input(buf, buf->pl);
}

enum mad_flow remote_get_input_nowait(buffer *buf)
{
    fd_set fd;
    struct timeval tv = { 0, 0 };
    FD_ZERO(&fd);
    FD_SET(0,&fd);

    if (!strlen (remote_input_buf))
    {
        if (select(1, &fd, NULL, NULL, &tv))
            return remote_parse_input(buf, buf->pl);

        else
            return 0;
    } 
     
    else 
    {
        return remote_parse_input(buf, buf->pl);
    }
}