add x10tv ati remote rework, android remote rework, wintv remote tweaks
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / androidcontrol.C
1
2 #include "keys.h"
3 #include "mwindow.h"
4 #include "mwindowgui.h"
5 #include "androidcontrol.h"
6 #include "preferences.h"
7
8
9 int AndroidControl::open(unsigned short port)
10 {
11         sockfd = socket(PF_INET,SOCK_DGRAM,0);
12         if( sockfd < 0 ) { perror("socket:"); return -1; }
13
14         struct linger lgr;
15         lgr.l_onoff = 0;
16         lgr.l_linger = 0;
17
18         if( setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &lgr, sizeof(lgr)) < 0 ) {
19                 perror("setlinger:");
20                 return -1;
21         }
22
23         struct sockaddr_in sadr;
24         memset(&sadr,0,sizeof(sadr));
25         sadr.sin_family = AF_INET;
26         sadr.sin_port = htons(port);
27         sadr.sin_addr.s_addr = 0;
28
29         if( bind(sockfd,(struct sockaddr *)&sadr,sizeof(sadr)) < 0 ) {
30                 perror("bind:");
31                 return -1;
32         }
33
34         return 0;
35 }
36
37 void AndroidControl::close()
38 {
39         if( sockfd >= 0 ) {
40                 ::close(sockfd);
41                 sockfd = -1;
42         }
43 }
44
45 AndroidControl::AndroidControl(MWindowGUI *mwindow_gui)
46  : Thread(1, 0, 0)
47 {
48         this->mwindow_gui = mwindow_gui;
49         Thread::start();
50 }
51
52 bool AndroidControl::is_msg(const char *cp)
53 {
54         if( msg_len != (int)strlen(cp) ) return false;
55         if( strncmp(cp, msg, msg_len) ) return false;
56         return true;
57 }
58
59 void AndroidControl::press(int key)
60 {
61 // printf("press 0x%04x\n",key);
62         if( key == KPMENU && mwindow_gui->keyev_grab_remote() )
63                 printf("android grab remote\n");
64         if( mwindow_gui->key_listener(key) ) return;
65         mwindow_gui->remote_control->remote_key(key);
66 }
67
68 void AndroidControl::run()
69 {
70         unsigned short port = mwindow_gui->mwindow->preferences->android_port;
71         done = open(port) >= 0 ? 0 : 1;
72
73         while( !done ) {
74                 struct sockaddr sadr;
75                 socklen_t sadr_len;
76                 enable_cancel();
77                 int ret = recvfrom(sockfd, buf, sizeof(buf), 0, &sadr, &sadr_len);
78                 if( ret < 0 ) {
79                         perror("AndroidControl::run: recvfrom");
80                         sleep(1);
81                 }
82                 disable_cancel();
83                 // validate message
84                 msg = buf;  msg_len = ret;
85                 // cinelerra android remote magic number
86                 if( msg[0] != 'C' || msg[1] != 'A' || msg[2] != 'R' ) continue;
87                 // version number
88                 if( msg[3] != 1 ) continue;
89                 msg += 4;  msg_len -= 4;
90                 // pin
91                 const char *pin = mwindow_gui->mwindow->preferences->android_pin;
92                 int len = sizeof(mwindow_gui->mwindow->preferences->android_pin);
93                 while( len > 0 && msg_len > 0 && *pin != 0 && *msg != 0 ) {
94                         if( *pin != *msg ) break;
95                         ++pin;  --len;
96                         ++msg;  --msg_len;
97                 }
98                 if( !len || !msg_len || *pin != *msg ) continue;
99                 ++msg; --msg_len;
100                 if( msg_len <= 0 ) continue;
101                 if( is_msg("menu") ) press(KPMENU);
102                 else if( is_msg("key 0") ) press('0');
103                 else if( is_msg("key 1") ) press('1');
104                 else if( is_msg("key 2") ) press('2');
105                 else if( is_msg("key 3") ) press('3');
106                 else if( is_msg("key 4") ) press('4');
107                 else if( is_msg("key 5") ) press('5');
108                 else if( is_msg("key 6") ) press('6');
109                 else if( is_msg("key 7") ) press('7');
110                 else if( is_msg("key 8") ) press('8');
111                 else if( is_msg("key 9") ) press('9');
112                 else if( is_msg("key A") ) press('a');
113                 else if( is_msg("key B") ) press('b');
114                 else if( is_msg("key C") ) press('c');
115                 else if( is_msg("key D") ) press('d');
116                 else if( is_msg("key E") ) press('e');
117                 else if( is_msg("book") ) press(KPBOOK);
118                 else if( is_msg("rplay") ) press(KPREV);
119                 else if( is_msg("stop") ) press(KPSTOP);
120                 else if( is_msg("play") ) press(KPPLAY);
121                 else if( is_msg("media_lt") ) press(LEFT);
122                 else if( is_msg("media_rt") ) press(RIGHT);
123                 else if( is_msg("media_up") ) press(UP);
124                 else if( is_msg("media_dn") ) press(DOWN);
125                 else if( is_msg("pause") ) press(' ');
126                 else if( is_msg("slow_lt") ) press(KPRECD);
127                 else if( is_msg("slow_rt") ) press(KPAUSE);
128                 else if( is_msg("fast_lt") ) press(KPBACK);
129                 else if( is_msg("fast_rt") ) press(KPFWRD);
130                 else if( is_msg("fscrn") ) press(KPFSCRN);
131                 else if( is_msg("mute") ) press(KPMUTE);
132                 else if( is_msg("vol_up") ) press(KPVOLUP);
133                 else if( is_msg("vol_dn") ) press(KPVOLDN);
134                 else if( is_msg("ch_up") ) press(KPCHUP);
135                 else if( is_msg("ch_dn") ) press(KPCHDN);
136                 else if( is_msg("key dot") ) press('.');
137                 else if( is_msg("key cc") ) press(KPCC);
138                 else if( is_msg("key tv") ) press(KPTV);
139                 else if( is_msg("hand") ) press(KPHAND);
140                 else if( is_msg("suspend") ) {
141                         system("sync; sleep 1; acpitool -s");
142                 }
143                 else if( is_msg("power") ) {
144                         system("sync; sleep 1; poweroff");
145                 }
146                 else {
147                         printf("AndroidControl::run: unkn msg: %s\n", msg);
148                         sleep(1);
149                 }
150         }
151 }
152
153 AndroidControl::~AndroidControl()
154 {
155         if( Thread::running() ) {
156                 done = 1;
157                 Thread::cancel();
158         }
159         Thread::join();
160 }
161