2 * Copyright (C) 12 Jun 2003 Tomas Cermak
4 * This file is part of wmradio program.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include <sys/types.h>
31 #define FIFO_FILE_PREFIX "/tmp/wmradio_"
34 #define MAX_STRINGS 40
35 char *read_line(FILE *file
)
37 char *str
=(char *)malloc(MAX_STRINGS
);
41 struct timespec pause
= { 0, 100000000 };
43 if (fgets(str
,MAX_STRINGS
,file
)==NULL
)
48 for(retries
=3; retries
; retries
--) {
51 if (strchr(str
,'\n')==NULL
) {
52 char *tmp
=(char *)malloc(size
+MAX_STRINGS
-1);
53 nanosleep(&pause
, &pause
);
55 fgets(tmp
+size
-1,MAX_STRINGS
,file
);
70 char * fifo_filename() {
71 static char *name
= NULL
;
75 snprintf(buffer
,sizeof(buffer
),"%i",getuid());
76 name
= malloc(sizeof(char)*strlen(buffer
)+
77 sizeof(FIFO_FILE_PREFIX
)+1);
78 sprintf(name
, "%s%s", FIFO_FILE_PREFIX
, buffer
);
85 mkfifo(fifo_filename(), 0666);
86 fifo_fd
= open(fifo_filename(), O_NONBLOCK
| O_RDONLY
);
87 fifo
= fdopen(fifo_fd
, "r");
92 unlink(fifo_filename());
96 char *line
= read_line(fifo
);
101 command
=(char *)malloc(strlen(line
)+1);
102 sscanf(line
,"%s",command
);
103 rest
=line
+strlen(command
);
105 if (!strcasecmp(command
,"TUNE_PLUS")) {
106 wmradio_command(TUNE_PLUS
, 0);
108 else if (!strcasecmp(command
,"TUNE_MINUS"))
110 wmradio_command(TUNE_MINUS
, 0);
112 if (!strcasecmp(command
,"FINE_TUNE_PLUS")) {
113 wmradio_command(TUNE_PLUS
, 1);
115 else if (!strcasecmp(command
,"FINE_TUNE_MINUS"))
117 wmradio_command(TUNE_MINUS
, 1);
119 else if (!strcasecmp(command
,"POWER"))
121 wmradio_command(POWER_SWITCH
, 0);
123 else if (!strcasecmp(command
,"SET_PRESET"))
126 if (sscanf(rest
,"%i",&i
)==1) {
128 wmradio_command(SET_PRESET
, i
);
131 else if (!strcasecmp(command
,"SAVE_PRESET"))
134 if ((sscanf(rest
,"%i",&i
))==1) {
136 wmradio_command(SAVE_PRESET
, i
);
139 else if (!strcasecmp(command
,"SCAN"))
141 wmradio_command(SCAN
, 0);
143 else if (!strcasecmp(command
,"TUNE_NAME_PREV"))
145 wmradio_command(TUNE_NAME_PREV
, 0);
147 else if (!strcasecmp(command
,"TUNE_NAME_NEXT"))
149 wmradio_command(TUNE_NAME_NEXT
, 0);
151 else if (!strcasecmp(command
,"READ_CONFIG"))
153 wmradio_command(READ_CONFIG
, 0);