Mercurial > mplayer.hg
annotate input/lirc.c @ 15938:cf34ac76bd15
-identify variable names should follow [A-Z_][A-Z0-9_]* convention
author | ranma |
---|---|
date | Thu, 07 Jul 2005 17:35:05 +0000 |
parents | 8549fc0fb5b1 |
children | a68ede010f66 |
rev | line source |
---|---|
4432 | 1 |
2 #include "../config.h" | |
3 | |
4 #ifdef HAVE_LIRC | |
5 | |
6 #include <lirc/lirc_client.h> | |
7 #include <errno.h> | |
8 #include <stdio.h> | |
9 #include <string.h> | |
10 #include <unistd.h> | |
11 #include <sys/types.h> | |
7883 | 12 #include <sys/time.h> |
4432 | 13 #include <stdlib.h> |
14 | |
15 #include "../mp_msg.h" | |
16 #include "../help_mp.h" | |
7883 | 17 #include "input.h" |
4432 | 18 |
19 static struct lirc_config *lirc_config; | |
4823
d25b898c4c44
Make old and new lirc support independant from each other
albeu
parents:
4526
diff
changeset
|
20 char *lirc_configfile; |
4432 | 21 |
7883 | 22 static char* cmd_buf = NULL; |
4432 | 23 |
24 int | |
25 mp_input_lirc_init(void) { | |
26 int lirc_sock; | |
27 | |
28 mp_msg(MSGT_LIRC,MSGL_INFO,MSGTR_SettingUpLIRC); | |
29 if((lirc_sock=lirc_init("mplayer",1))==-1){ | |
30 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCopenfailed MSGTR_LIRCdisabled); | |
31 return -1; | |
32 } | |
33 | |
34 if(lirc_readconfig( lirc_configfile,&lirc_config,NULL )!=0 ){ | |
35 mp_msg(MSGT_LIRC,MSGL_ERR,MSGTR_LIRCcfgerr MSGTR_LIRCdisabled, | |
36 lirc_configfile == NULL ? "~/.lircrc" : lirc_configfile); | |
37 lirc_deinit(); | |
38 return -1; | |
39 } | |
40 | |
7883 | 41 return lirc_sock; |
4432 | 42 } |
43 | |
7883 | 44 int mp_input_lirc_read(int fd,char* dest, int s) { |
45 fd_set fds; | |
46 struct timeval tv; | |
47 int r,cl = 0; | |
48 char *code = NULL,*c = NULL; | |
4432 | 49 |
7883 | 50 // We have something in the buffer return it |
51 if(cmd_buf != NULL) { | |
52 int l = strlen(cmd_buf), w = l > s ? s : l; | |
53 memcpy(dest,cmd_buf,w); | |
54 l -= w; | |
55 if(l > 0) | |
56 memmove(cmd_buf,&cmd_buf[w],l+1); | |
57 else { | |
58 free(cmd_buf); | |
59 cmd_buf = NULL; | |
60 } | |
61 return w; | |
62 } | |
63 | |
64 // Nothing in the buffer, pool the lirc fd | |
65 FD_ZERO(&fds); | |
66 FD_SET(fd,&fds); | |
67 memset(&tv,0,sizeof(tv)); | |
68 while((r = select(fd+1,&fds,NULL,NULL,&tv)) <= 0) { | |
69 if(r < 0) { | |
70 if(errno == EINTR) | |
71 continue; | |
72 mp_msg(MSGT_INPUT,MSGL_ERR,"Select error : %s\n",strerror(errno)); | |
73 return MP_INPUT_ERROR; | |
74 } else | |
75 return MP_INPUT_NOTHING; | |
76 } | |
77 | |
78 // There's something to read | |
79 if(lirc_nextcode(&code) != 0) { | |
80 mp_msg(MSGT_INPUT,MSGL_ERR,"Lirc error :(\n"); | |
81 return MP_INPUT_DEAD; | |
82 } | |
4432 | 83 |
7883 | 84 if(!code) return MP_INPUT_NOTHING; |
85 | |
86 // We put all cmds in a single buffer separated by \n | |
87 while((r = lirc_code2char(lirc_config,code,&c))==0 && c!=NULL) { | |
88 int l = strlen(c); | |
89 if(l <= 0) | |
4432 | 90 continue; |
7883 | 91 cmd_buf = realloc(cmd_buf,cl+l+2); |
92 memcpy(&cmd_buf[cl],c,l); | |
93 cl += l+1; | |
94 cmd_buf[cl-1] = '\n'; | |
95 cmd_buf[cl] = '\0'; | |
4432 | 96 } |
7883 | 97 |
98 free(code); | |
99 | |
100 if(r < 0) | |
101 return MP_INPUT_DEAD; | |
102 else if(cmd_buf) // return the first command in the buffer | |
103 return mp_input_lirc_read(fd,dest,s); | |
104 else | |
15825 | 105 return MP_INPUT_RETRY; |
7883 | 106 |
4432 | 107 } |
108 | |
109 void | |
7883 | 110 mp_input_lirc_close(int fd) { |
111 if(cmd_buf) { | |
112 free(cmd_buf); | |
113 cmd_buf = NULL; | |
4432 | 114 } |
7883 | 115 lirc_freeconfig(lirc_config); |
116 lirc_deinit(); | |
4432 | 117 } |
118 | |
119 #endif |