2 * This file is part of MPlayer.
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.
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.
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.
21 #include <lirc/lirc_client.h>
33 static struct lirc_config
*lirc_config
;
34 char *lirc_configfile
;
36 static char* cmd_buf
= NULL
;
39 mp_input_lirc_init(void) {
43 mp_tmsg(MSGT_LIRC
,MSGL_V
,"Setting up LIRC support...\n");
44 if((lirc_sock
=lirc_init("mplayer",0))==-1){
45 mp_tmsg(MSGT_LIRC
,MSGL_V
,"Failed to open LIRC support. You will not be able to use your remote control.\n");
49 mode
= fcntl(lirc_sock
, F_GETFL
);
50 if (mode
< 0 || fcntl(lirc_sock
, F_SETFL
, mode
| O_NONBLOCK
) < 0) {
51 mp_msg(MSGT_LIRC
, MSGL_ERR
, "setting non-blocking mode failed: %s\n",
57 if(lirc_readconfig( lirc_configfile
,&lirc_config
,NULL
)!=0 ){
58 mp_tmsg(MSGT_LIRC
,MSGL_ERR
,"Failed to read LIRC config file %s.\n",
59 lirc_configfile
== NULL
? "~/.lircrc" : lirc_configfile
);
67 int mp_input_lirc_read(int fd
,char* dest
, int s
) {
69 char *code
= NULL
,*c
= NULL
;
71 // We have something in the buffer return it
73 int l
= strlen(cmd_buf
), w
= l
> s
? s
: l
;
74 memcpy(dest
,cmd_buf
,w
);
77 memmove(cmd_buf
,&cmd_buf
[w
],l
+1);
85 // Nothing in the buffer, poll the lirc fd
86 if(lirc_nextcode(&code
) != 0) {
87 mp_msg(MSGT_LIRC
,MSGL_ERR
,"Lirc error :(\n");
91 if(!code
) return MP_INPUT_NOTHING
;
93 // We put all cmds in a single buffer separated by \n
94 while((r
= lirc_code2char(lirc_config
,code
,&c
))==0 && c
!=NULL
) {
98 cmd_buf
= realloc(cmd_buf
,cl
+l
+2);
99 memcpy(&cmd_buf
[cl
],c
,l
);
101 cmd_buf
[cl
-1] = '\n';
108 return MP_INPUT_DEAD
;
109 else if(cmd_buf
) // return the first command in the buffer
110 return mp_input_lirc_read(fd
,dest
,s
);
112 return MP_INPUT_RETRY
;
116 int mp_input_lirc_close(int fd
)
120 lirc_freeconfig(lirc_config
);