wmbattery: use lower-case prefixes for PKG_CHECK_MODULES checks.
[dockapps.git] / wmradio / fifo.c
blobdc3b16518eee156ba65c8e0db0e40acef308b050
1 /*
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.
20 #include "fifo.h"
21 #include "wmradio.h"
22 #include<stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <time.h>
31 #define FIFO_FILE_PREFIX "/tmp/wmradio_"
32 FILE *fifo;
34 #define MAX_STRINGS 40
35 char *read_line(FILE *file)
37 char *str=(char *)malloc(MAX_STRINGS);
38 int fin=0;
39 int size=MAX_STRINGS;
40 int retries;
41 struct timespec pause = { 0, 100000000 };
43 if (fgets(str,MAX_STRINGS,file)==NULL)
45 free(str);
46 return NULL;
48 for(retries=3; retries; retries--) {
49 if(!fin) {
50 fin=1;
51 if (strchr(str,'\n')==NULL) {
52 char *tmp=(char *)malloc(size+MAX_STRINGS-1);
53 nanosleep(&pause, &pause);
54 strcpy(tmp,str);
55 fgets(tmp+size-1,MAX_STRINGS,file);
56 free(str);
57 str=tmp;
58 size+=MAX_STRINGS-1;
59 fin=0;
63 if(!fin) {
64 free(str);
65 return NULL;
67 return str;
70 char * fifo_filename() {
71 static char *name = NULL;
72 char buffer[100];
74 if (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);
80 return name;
83 void fifo_init() {
84 int fifo_fd;
85 mkfifo(fifo_filename(), 0666);
86 fifo_fd = open(fifo_filename(), O_NONBLOCK | O_RDONLY);
87 fifo = fdopen(fifo_fd, "r");
90 void fifo_close() {
91 fclose(fifo);
92 unlink(fifo_filename());
95 void fifo_parse () {
96 char *line = read_line(fifo);
97 char *command;
98 char *rest=NULL;
100 if (line) {
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"))
125 int i;
126 if (sscanf(rest,"%i",&i)==1) {
127 if (i>=0 && i<6)
128 wmradio_command(SET_PRESET, i);
131 else if (!strcasecmp(command,"SAVE_PRESET"))
133 int i;
134 if ((sscanf(rest,"%i",&i))==1) {
135 if (i>=0 && i<6)
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);
155 free(command);
156 free(line);