summaryrefslogtreecommitdiffstats
path: root/cython/screenshotr.pxi
blob: a53fab237eb354fc5d2923776a5de772fd9d3b00 (plain)
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
cdef extern from "libimobiledevice/screenshotr.h":
    cdef struct screenshotr_client_private:
        pass
    ctypedef screenshotr_client_private *screenshotr_client_t

    ctypedef enum screenshotr_error_t:
        SCREENSHOTR_E_SUCCESS = 0
        SCREENSHOTR_E_INVALID_ARG = -1
        SCREENSHOTR_E_PLIST_ERROR = -2
        SCREENSHOTR_E_MUX_ERROR = -3
        SCREENSHOTR_E_BAD_VERSION = -4
        SCREENSHOTR_E_UNKNOWN_ERROR = -256

    screenshotr_error_t screenshotr_client_new(idevice_t device, uint16_t port, screenshotr_client_t * client)
    screenshotr_error_t screenshotr_client_free(screenshotr_client_t client)
    screenshotr_error_t screenshotr_take_screenshot(screenshotr_client_t client, char **imgdata, uint64_t *imgsize)

cdef class ScreenshotrError(BaseError):
    def __init__(self, *args, **kwargs):
        self._lookup_table = {
            SCREENSHOTR_E_SUCCESS: "Success",
            SCREENSHOTR_E_INVALID_ARG: "Invalid argument",
            SCREENSHOTR_E_PLIST_ERROR: "Property list error",
            SCREENSHOTR_E_MUX_ERROR: "MUX error",
            SCREENSHOTR_E_BAD_VERSION: "Bad version",
            SCREENSHOTR_E_UNKNOWN_ERROR: "Unknown error"
        }
        BaseError.__init__(self, *args, **kwargs)

cdef class ScreenshotrClient(DeviceLinkService):
    __service_name__ = "com.apple.mobile.screenshotr"
    cdef screenshotr_client_t _c_client

    def __cinit__(self, iDevice device not None, int port, *args, **kwargs):
        self.handle_error(screenshotr_client_new(device._c_dev, port, &self._c_client))

    def __dealloc__(self):
        cdef screenshotr_error_t err
        if self._c_client is not NULL:
            err = screenshotr_client_free(self._c_client)
            self.handle_error(err)

    cpdef bytes take_screenshot(self):
        cdef:
            char* c_data = NULL
            uint64_t data_size
            bytes result
            screenshotr_error_t err

        err = screenshotr_take_screenshot(self._c_client, &c_data, &data_size)
        try:
            self.handle_error(err)

            result = c_data[:data_size]
            return result
        except Exception, e:
            if c_data != NULL:
                free(c_data)
            raise

    cdef inline BaseError _error(self, int16_t ret):
        return ScreenshotrError(ret)