Mercurial > mplayer.hg
annotate input/lirc.c @ 28177:8eaa8e269bd6
synced with r28211
author | Gabrov |
---|---|
date | Tue, 30 Dec 2008 12:48:17 +0000 |
parents | 142c53391eb7 |
children | 1dc02f91a9b4 |
rev | line source |
---|---|
28112 | 1 /* |
2 * This file is part of MPlayer. | |
3 * | |
4 * MPlayer is free software; you can redistribute it and/or modify | |
5 * it under the terms of the GNU General Public License as published by | |
6 * the Free Software Foundation; either version 2 of the License, or | |
7 * (at your option) any later version. | |
8 * | |
9 * MPlayer is distributed in the hope that it will be useful, | |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 * GNU General Public License for more details. | |
13 * | |
14 * You should have received a copy of the GNU General Public License along | |
15 * with MPlayer; if not, write to the Free Software Foundation, Inc., | |
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 */ | |
4432 | 18 |
16860 | 19 #include "config.h" |
4432 | 20 |
21 #include <lirc/lirc_client.h> | |
22 #include <errno.h> | |
23 #include <stdio.h> | |
24 #include <string.h> | |
25 #include <unistd.h> | |
26 #include <sys/types.h> | |
7883 | 27 #include <sys/time.h> |
4432 | 28 #include <stdlib.h> |
29 | |
16860 | 30 #include "mp_msg.h" |
31 #include "help_mp.h" | |
7883 | 32 #include "input.h" |
4432 | 33 |
34 static struct lirc_config *lirc_config; | |
4823
d25b898c4c44
Make old and new lirc support independant from each other
albeu
parents:
4526
diff
changeset
|
35 char *lirc_configfile; |
4432 | 36 |
7883 | 37 static char* cmd_buf = NULL; |
4432 | 38 |
39 int | |
40 mp_input_lirc_init(void) { | |
41 int lirc_sock; | |
42 | |
20185 | 43 mp_msg(MSGT_LIRC,MSGL_V,MSGTR_SettingUpLIRC); |
4432 | 44 if((lirc_sock=lirc_init("mplayer",1))==-1){ |
20185 | 45 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCopenfailed); |
4432 | 46 return -1; |
47 } | |
48 | |
49 if(lirc_readconfig( lirc_configfile,&lirc_config,NULL )!=0 ){ | |
20191 | 50 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCcfgerr, |
4432 | 51 lirc_configfile == NULL ? "~/.lircrc" : lirc_configfile); |
52 lirc_deinit(); | |
53 return -1; | |
54 } | |
55 | |
7883 | 56 return lirc_sock; |
4432 | 57 } |
58 | |
7883 | 59 int mp_input_lirc_read(int fd,char* dest, int s) { |
60 fd_set fds; | |
61 struct timeval tv; | |
62 int r,cl = 0; | |
63 char *code = NULL,*c = NULL; | |
4432 | 64 |
7883 | 65 // We have something in the buffer return it |
66 if(cmd_buf != NULL) { | |
67 int l = strlen(cmd_buf), w = l > s ? s : l; | |
68 memcpy(dest,cmd_buf,w); | |
69 l -= w; | |
70 if(l > 0) | |
71 memmove(cmd_buf,&cmd_buf[w],l+1); | |
72 else { | |
73 free(cmd_buf); | |
74 cmd_buf = NULL; | |
75 } | |
76 return w; | |
77 } | |
78 | |
79 // Nothing in the buffer, pool the lirc fd | |
80 FD_ZERO(&fds); | |
81 FD_SET(fd,&fds); | |
82 memset(&tv,0,sizeof(tv)); | |
83 while((r = select(fd+1,&fds,NULL,NULL,&tv)) <= 0) { | |
84 if(r < 0) { | |
85 if(errno == EINTR) | |
86 continue; | |
87 mp_msg(MSGT_INPUT,MSGL_ERR,"Select error : %s\n",strerror(errno)); | |
88 return MP_INPUT_ERROR; | |
89 } else | |
90 return MP_INPUT_NOTHING; | |
91 } | |
92 | |
93 // There's something to read | |
94 if(lirc_nextcode(&code) != 0) { | |
95 mp_msg(MSGT_INPUT,MSGL_ERR,"Lirc error :(\n"); | |
96 return MP_INPUT_DEAD; | |
97 } | |
4432 | 98 |
7883 | 99 if(!code) return MP_INPUT_NOTHING; |
100 | |
101 // We put all cmds in a single buffer separated by \n | |
102 while((r = lirc_code2char(lirc_config,code,&c))==0 && c!=NULL) { | |
103 int l = strlen(c); | |
104 if(l <= 0) | |
4432 | 105 continue; |
7883 | 106 cmd_buf = realloc(cmd_buf,cl+l+2); |
107 memcpy(&cmd_buf[cl],c,l); | |
108 cl += l+1; | |
109 cmd_buf[cl-1] = '\n'; | |
110 cmd_buf[cl] = '\0'; | |
4432 | 111 } |
7883 | 112 |
113 free(code); | |
114 | |
115 if(r < 0) | |
116 return MP_INPUT_DEAD; | |
117 else if(cmd_buf) // return the first command in the buffer | |
118 return mp_input_lirc_read(fd,dest,s); | |
119 else | |
15825 | 120 return MP_INPUT_RETRY; |
7883 | 121 |
4432 | 122 } |
123 | |
124 void | |
7883 | 125 mp_input_lirc_close(int fd) { |
126 if(cmd_buf) { | |
127 free(cmd_buf); | |
128 cmd_buf = NULL; | |
4432 | 129 } |
7883 | 130 lirc_freeconfig(lirc_config); |
131 lirc_deinit(); | |
4432 | 132 } |