4 #include "mwindowgui.h"
5 #include "androidcontrol.h"
6 #include "preferences.h"
9 int AndroidControl::open(unsigned short port)
11 sockfd = socket(PF_INET,SOCK_DGRAM,0);
12 if( sockfd < 0 ) { perror("socket:"); return -1; }
18 if( setsockopt(sockfd, SOL_SOCKET, SO_LINGER, &lgr, sizeof(lgr)) < 0 ) {
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;
29 if( bind(sockfd,(struct sockaddr *)&sadr,sizeof(sadr)) < 0 ) {
37 void AndroidControl::close()
45 AndroidControl::AndroidControl(MWindowGUI *mwindow_gui)
48 this->mwindow_gui = mwindow_gui;
55 bool AndroidControl::is_msg(const char *cp)
57 if( msg_len != (int)strlen(cp) ) return false;
58 if( strncmp(cp, msg, msg_len) ) return false;
62 void AndroidControl::press(int key)
64 // printf("press 0x%04x\n",key);
65 if( key == KPMENU && mwindow_gui->keyev_grab_remote() )
66 printf("android grab remote\n");
67 if( mwindow_gui->key_listener(key) ) return;
68 mwindow_gui->remote_control->remote_key(key);
71 void AndroidControl::run()
73 unsigned short port = mwindow_gui->mwindow->preferences->android_port;
74 done = open(port) >= 0 ? 0 : 1;
80 int ret = recvfrom(sockfd, buf, sizeof(buf), 0, &sadr, &sadr_len);
82 perror("AndroidControl::run: recvfrom");
87 msg = buf; msg_len = ret;
88 // cinelerra android remote magic number
89 if( msg[0] != 'C' || msg[1] != 'A' || msg[2] != 'R' ) continue;
91 if( msg[3] != 1 ) continue;
92 msg += 4; msg_len -= 4;
94 const char *pin = mwindow_gui->mwindow->preferences->android_pin;
95 int len = sizeof(mwindow_gui->mwindow->preferences->android_pin);
96 while( len > 0 && msg_len > 0 && *pin != 0 && *msg != 0 ) {
97 if( *pin != *msg ) break;
101 if( !len || !msg_len || *pin != *msg ) continue;
103 if( msg_len <= 0 ) continue;
104 if( is_msg("menu") ) press(KPMENU);
105 else if( is_msg("key 0") ) press('0');
106 else if( is_msg("key 1") ) press('1');
107 else if( is_msg("key 2") ) press('2');
108 else if( is_msg("key 3") ) press('3');
109 else if( is_msg("key 4") ) press('4');
110 else if( is_msg("key 5") ) press('5');
111 else if( is_msg("key 6") ) press('6');
112 else if( is_msg("key 7") ) press('7');
113 else if( is_msg("key 8") ) press('8');
114 else if( is_msg("key 9") ) press('9');
115 else if( is_msg("key A") ) press('a');
116 else if( is_msg("key B") ) press('b');
117 else if( is_msg("key C") ) press('c');
118 else if( is_msg("key D") ) press('d');
119 else if( is_msg("key E") ) press('e');
120 else if( is_msg("book") ) press(KPBOOK);
121 else if( is_msg("rplay") ) press(KPREV);
122 else if( is_msg("stop") ) press(KPSTOP);
123 else if( is_msg("play") ) press(KPPLAY);
124 else if( is_msg("media_lt") ) press(LEFT);
125 else if( is_msg("media_rt") ) press(RIGHT);
126 else if( is_msg("media_up") ) press(UP);
127 else if( is_msg("media_dn") ) press(DOWN);
128 else if( is_msg("pause") ) press(' ');
129 else if( is_msg("slow_lt") ) press(KPRECD);
130 else if( is_msg("slow_rt") ) press(KPAUSE);
131 else if( is_msg("fast_lt") ) press(KPBACK);
132 else if( is_msg("fast_rt") ) press(KPFWRD);
133 else if( is_msg("fscrn") ) press(KPFSCRN);
134 else if( is_msg("mute") ) press(KPMUTE);
135 else if( is_msg("vol_up") ) press(KPVOLUP);
136 else if( is_msg("vol_dn") ) press(KPVOLDN);
137 else if( is_msg("ch_up") ) press(KPCHUP);
138 else if( is_msg("ch_dn") ) press(KPCHDN);
139 else if( is_msg("key dot") ) press('.');
140 else if( is_msg("key cc") ) press(KPCC);
141 else if( is_msg("key tv") ) press(KPTV);
142 else if( is_msg("hand") ) press(KPHAND);
143 else if( is_msg("suspend") ) {
144 system("sync; sleep 1; acpitool -s");
146 else if( is_msg("power") ) {
147 system("sync; sleep 1; poweroff");
150 printf("AndroidControl::run: unkn msg: %s\n", msg);
156 AndroidControl::~AndroidControl()
158 if( Thread::running() ) {