use xdo type and key to implement keyboard
[premotedroid-server-perl.git] / premotedroid-server.pl
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4
5 use IO::Socket::INET;
6 use Data::Dump qw(dump);
7
8 my $sock = IO::Socket::INET->new(
9         Listen    => 5,
10 #       LocalAddr => 'localhost',
11         LocalPort => 64788,
12         Proto     => 'tcp',
13         Reuse     => 1,
14 ) || die $!;
15
16 warn "listen on ", dump $sock->sockaddr, $sock->sockport;
17
18 sub readUTF {
19         my $client = shift;
20         read $client, my $len, 2;
21         $len = unpack( 'n', $len );
22         read $client, my $utf, $len;
23         warn "## readUTF $len [$utf]";
24         return $utf;
25 }
26
27 # from PRemoteDroid Protocol/src/org/pierre/remotedroid/protocol/action/PRemoteDroidAction.java
28 use constant MOUSE_MOVE => 0;
29 use constant MOUSE_CLICK => 1;
30 use constant MOUSE_WHEEL => 2;
31 use constant KEYBOARD => 3;
32 use constant AUTHENTIFICATION => 4;
33 use constant AUTHENTIFICATION_RESPONSE => 5;
34 use constant SCREEN_CAPTURE_REQUEST => 6;
35 use constant SCREEN_CAPTURE_RESPONSE => 7;
36 use constant FILE_EXPLORE_REQUEST => 8;
37 use constant FILE_EXPLORE_RESPONSE => 9;
38
39 open(my $xdo, '|-', 'xdotool -') || die $!;
40 select($xdo); $|=1;
41
42 my $keysyms = {
43         -1 => 'BackSpace',
44         10 => 'Return',
45 };
46
47 while ( my $client = $sock->accept() ) {
48
49         warn "connect from ", dump $client->peeraddr, $client->peerport;
50
51         while ( read $client, my $command, 1 ) {
52                 $command = ord $command;
53                 warn "# command: $command\n";
54                 if ( $command == MOUSE_MOVE ) {
55                         read $client, my $move, 4;
56                         my ( $x, $y ) = unpack 's>s>', $move; # big-endian 16 bit
57                         warn "MOVE $x $y\n";
58                         print $xdo "mousemove_relative -- $x $y\n";
59                 } elsif ( $command == MOUSE_CLICK ) {
60                         read $client, my $b, 2;
61                         my ( $button, $state ) = unpack 'cc', $b;
62                         warn "MOUSE_CLICK $button $state\n";
63                         print $xdo 'mouse' . ( $state ? 'down' : 'up' ) . ' ' . $button . "\n";
64                 } elsif ( $command == MOUSE_WHEEL ) {
65                         read $client, my $amount, 1;
66                         $amount = unpack 'c', $amount;
67                         warn "MOUSE_WHEEL $amount\n";
68                 } elsif ( $command == AUTHENTIFICATION ) {
69                         my $auth = readUTF $client;
70                         warn "AUTHENTIFICATION [$auth]\n";
71                         print $client pack 'cc', AUTHENTIFICATION_RESPONSE, 1; # FIXME anything goes
72                 } elsif ( $command == KEYBOARD ) {
73                         read $client, my $unicode, 4;
74                         my $key = unpack 'l>', $unicode;
75                         my $command = 'type';
76                         if ( defined $keysyms->{$key} ) {
77                                 $key = $keysyms->{$key};
78                                 $command = 'key';
79                         } else {
80                                 $key = chr($key);
81                                 $command = 'key' if $key =~ m/^\w$/;
82                         }
83                         warn uc($command)," $key\n";
84                         print $xdo "$command '$key'\n";
85                 } else {
86                         die "UNSUPPORTED";
87                 }
88         }
89
90         warn "client disconnected\n";
91
92 }