summaryrefslogtreecommitdiffstats
path: root/fb2png/ascreenshooter.py
blob: 1e994840e35b76d62da30e18444aab395033559d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/python

import socket
import sys
import struct
import time

# debug
VERBOSE = True

def D(msg):
    if VERBOSE: print(msg)

# "struct fbinfo" is defined in $T/system/core/adb/framebuffer_service.c
def fbinfo_unpack(data):
    keys = ("version",
            "bpp",
            "size",
            "width",
            "height",
            "red_offset",
            "red_length",
            "blue_offset",
            "blue_length",
            "green_offset",
            "green_length",
            "alpha_offset",
            "alpha_length"
        )
    # the data is little-endian
    values = struct.unpack("<IIIIIIIIIIIII",data)

    D("dump struct fbinfo")
    i = 0
    for key in keys:
        D("%14s: %-12d" % (key, values[i]))
        i = i + 1



def save():
    f = open('dump', 'w')
    while True:
        data = s.recv(4096 * 16)
        if data == "":
            break
        f.write(data)
    f.close()


def communicate(cmd=None):
    if cmd != None:
        buf = "%04x%s" % (len(cmd), cmd)
        D("<< " + buf)
        s.send(buf)
    data = s.recv(4096)

    D(">> [%s]" % len(data))
    D(data)

    if data[0:4] == 'FAIL':
        return False
    else:
        return True


target = ''
# use getopt module in future
for arg in sys.argv:
    if arg == '-q':
        VERBOSE = False
    if target != 'any':
        # compatiable with "adb -d", redirect commands to usb
        if arg == '-d':
            target = 'usb'
        # compatiable with "adb -e", redirect commands to emulator
        elif arg == '-e':
            target = 'local'

if target == '': target ='any'

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

D("connecting")
try:
    s.connect(("localhost", 5037))
except socket.error:
    print 'Cannot connect to localhost:5037'
    print socket.error
    sys.exit(0)

D("connected")

if not communicate("host:transport-%s" % target):
    sys.exit(1)
#communicate("host:transport-usb:shell:ls /data")
communicate("framebuffer:")

data = s.recv(52)
fbinfo_unpack(data)

t0 = float(time.time())
save()
t1 = float(time.time())
print t1 - t0