From a5d7f3815adc052a8fb5ec71bf66386c2384d7d1 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Wed, 17 Feb 2010 16:33:18 +0100 Subject: New screenshotr service plus idevicescreenshot tool [#113 state:resolved] --- include/Makefile.am | 1 + include/libimobiledevice/screenshotr.h | 54 ++++++++++ src/Makefile.am | 1 + src/screenshotr.c | 186 +++++++++++++++++++++++++++++++++ src/screenshotr.h | 31 ++++++ tools/Makefile.am | 7 +- tools/idevicescreenshot.c | 111 ++++++++++++++++++++ 7 files changed, 390 insertions(+), 1 deletion(-) create mode 100644 include/libimobiledevice/screenshotr.h create mode 100644 src/screenshotr.c create mode 100644 src/screenshotr.h create mode 100644 tools/idevicescreenshot.c diff --git a/include/Makefile.am b/include/Makefile.am index 0cb3c50..9248e06 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -6,5 +6,6 @@ nobase_include_HEADERS = libimobiledevice/libimobiledevice.h \ libimobiledevice/installation_proxy.h \ libimobiledevice/sbservices.h \ libimobiledevice/mobile_image_mounter.h \ + libimobiledevice/screenshotr.h \ libimobiledevice/mobilesync.h \ libimobiledevice/mobilebackup.h diff --git a/include/libimobiledevice/screenshotr.h b/include/libimobiledevice/screenshotr.h new file mode 100644 index 0000000..077f50c --- /dev/null +++ b/include/libimobiledevice/screenshotr.h @@ -0,0 +1,54 @@ +/** + * @file libimobiledevice/screenshotr.h + * @brief Screenshot service implementation + * \internal + * + * Copyright (c) 2010 Nikias Bassen All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef ISCREENSHOTR_H +#define ISCREENSHOTR_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +/* Error Codes */ +#define SCREENSHOTR_E_SUCCESS 0 +#define SCREENSHOTR_E_INVALID_ARG -1 +#define SCREENSHOTR_E_PLIST_ERROR -2 +#define SCREENSHOTR_E_MUX_ERROR -3 +#define SCREENSHOTR_E_BAD_VERSION -4 + +#define SCREENSHOTR_E_UNKNOWN_ERROR -256 + +typedef int16_t screenshotr_error_t; + +struct screenshotr_client_int; +typedef struct screenshotr_client_int *screenshotr_client_t; + +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); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/Makefile.am b/src/Makefile.am index e847d7c..87fada0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -17,5 +17,6 @@ libimobiledevice_la_SOURCES = idevice.c idevice.h \ installation_proxy.c installation_proxy.h\ sbservices.c sbservices.h\ mobile_image_mounter.c mobile_image_mounter.h\ + screenshotr.c screenshotr.h\ mobilesync.c mobilesync.h\ mobilebackup.c mobilebackup.h diff --git a/src/screenshotr.c b/src/screenshotr.c new file mode 100644 index 0000000..f8866d0 --- /dev/null +++ b/src/screenshotr.c @@ -0,0 +1,186 @@ +/* + * screenshotr.c + * com.apple.mobile.screenshotr implementation. + * + * Copyright (c) 2010 Nikias Bassen All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#include +#include +#include +#include + +#include "screenshotr.h" +#include "device_link_service.h" +#include "debug.h" + +#define SCREENSHOTR_VERSION_INT1 100 +#define SCREENSHOTR_VERSION_INT2 0 + +/** + * Convert a device_link_service_error_t value to a screenshotr_error_t value. + * Used internally to get correct error codes. + * + * @param err An device_link_service_error_t error code + * + * @return A matching screenshotr_error_t error code, + * SCREENSHOTR_E_UNKNOWN_ERROR otherwise. + */ +static screenshotr_error_t screenshotr_error(device_link_service_error_t err) +{ + switch (err) { + case DEVICE_LINK_SERVICE_E_SUCCESS: + return SCREENSHOTR_E_SUCCESS; + case DEVICE_LINK_SERVICE_E_INVALID_ARG: + return SCREENSHOTR_E_INVALID_ARG; + case DEVICE_LINK_SERVICE_E_PLIST_ERROR: + return SCREENSHOTR_E_PLIST_ERROR; + case DEVICE_LINK_SERVICE_E_MUX_ERROR: + return SCREENSHOTR_E_MUX_ERROR; + case DEVICE_LINK_SERVICE_E_BAD_VERSION: + return SCREENSHOTR_E_BAD_VERSION; + default: + break; + } + return SCREENSHOTR_E_UNKNOWN_ERROR; +} + +/** + * Makes a connection to the screenshotr service on the device. + * + * @param device The device to connect to. + * @param port Destination port (usually given by lockdownd_start_service). + * @param client Pointer that will be set to a newly allocated + * screenshotr_client_t upon successful return. + * + * @return SCREENSHOTR_E_SUCCESS on success, SCREENSHOTR_E_INVALID ARG if one + * or more parameters are invalid, or SCREENSHOTR_E_CONN_FAILED if the + * connection to the device could not be established. + */ +screenshotr_error_t screenshotr_client_new(idevice_t device, uint16_t port, + screenshotr_client_t * client) +{ + if (!device || port == 0 || !client || *client) + return SCREENSHOTR_E_INVALID_ARG; + + device_link_service_client_t dlclient = NULL; + screenshotr_error_t ret = screenshotr_error(device_link_service_client_new(device, port, &dlclient)); + if (ret != SCREENSHOTR_E_SUCCESS) { + return ret; + } + + screenshotr_client_t client_loc = (screenshotr_client_t) malloc(sizeof(struct screenshotr_client_int)); + client_loc->parent = dlclient; + + /* perform handshake */ + ret = screenshotr_error(device_link_service_version_exchange(dlclient, SCREENSHOTR_VERSION_INT1, SCREENSHOTR_VERSION_INT2)); + if (ret != SCREENSHOTR_E_SUCCESS) { + debug_info("version exchange failed, error %d", ret); + screenshotr_client_free(client_loc); + return ret; + } + + *client = client_loc; + + return ret; +} + +/** + * Disconnects a screenshotr client from the device. + * + * @param client The client to disconnect. + * + * @return SCREENSHOTR_E_SUCCESS on success, or SCREENSHOTR_E_INVALID_ARG + * if client is NULL. + */ +screenshotr_error_t screenshotr_client_free(screenshotr_client_t client) +{ + if (!client) + return SCREENSHOTR_E_INVALID_ARG; + device_link_service_disconnect(client->parent); + screenshotr_error_t err = screenshotr_error(device_link_service_client_free(client->parent)); + free(client); + return err; +} + +/** + * Get a screen shot from the connected device. + * + * @param client The connection screenshotr service client. + * @param imgdata Pointer that will point to a newly allocated buffer + * containing TIFF image data upon successful return. It is up to the + * caller to free the memory. + * @param imgsize Pointer to a uint64_t that will be set to the size of the + * buffer imgdata points to upon successful return. + * + * @return SCREENSHOTR_E_SUCCESS on success, SCREENSHOTR_E_INVALID_ARG if + * one or more parameters are invalid, or another error code if an + * error occured. + */ +screenshotr_error_t screenshotr_take_screenshot(screenshotr_client_t client, char **imgdata, uint64_t *imgsize) +{ + if (!client || !client->parent || !imgdata) + return SCREENSHOTR_E_INVALID_ARG; + + screenshotr_error_t res = SCREENSHOTR_E_UNKNOWN_ERROR; + + plist_t dict = plist_new_dict(); + plist_dict_insert_item(dict, "MessageType", plist_new_string("ScreenShotRequest")); + + res = screenshotr_error(device_link_service_process_message(client->parent, dict)); + plist_free(dict); + if (res != SCREENSHOTR_E_SUCCESS) { + debug_info("could not send plist, error %d", res); + return res; + } + + dict = NULL; + res = screenshotr_error(device_link_service_get_process_message(client->parent, &dict)); + if (res != SCREENSHOTR_E_SUCCESS) { + debug_info("could not get screenshot data, error %d", res); + goto leave; + } + if (!dict) { + debug_info("did not receive screenshot data!"); + res = SCREENSHOTR_E_PLIST_ERROR; + goto leave; + } + + plist_t node = plist_dict_get_item(dict, "MessageType"); + char *strval = NULL; + plist_get_string_val(node, &strval); + if (!strval || strcmp(strval, "ScreenShotReply")) { + debug_info("invalid screenshot data received!"); + res = SCREENSHOTR_E_PLIST_ERROR; + goto leave; + } + node = plist_dict_get_item(dict, "ScreenShotData"); + if (!node || plist_get_node_type(node) != PLIST_DATA) { + debug_info("no PNG data received!"); + res = SCREENSHOTR_E_PLIST_ERROR; + goto leave; + } + + plist_get_data_val(node, imgdata, imgsize); + res = SCREENSHOTR_E_SUCCESS; + +leave: + if (dict) + plist_free(dict); + + return res; +} diff --git a/src/screenshotr.h b/src/screenshotr.h new file mode 100644 index 0000000..5f842a7 --- /dev/null +++ b/src/screenshotr.h @@ -0,0 +1,31 @@ +/* + * screenshotr.h + * com.apple.mobile.screenshotr definitions. + * + * Copyright (c) 2010 Nikias Bassen All Rights Reserved. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ +#ifndef SCREENSHOTR_H +#define SCREENSHOTR_H + +#include "libimobiledevice/screenshotr.h" +#include "device_link_service.h" + +struct screenshotr_client_int { + device_link_service_client_t parent; +}; + +#endif diff --git a/tools/Makefile.am b/tools/Makefile.am index b4fa69e..472ebab 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -3,7 +3,7 @@ INCLUDES = -I$(top_srcdir)/include AM_CFLAGS = $(GLOBAL_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) $(LFS_CFLAGS) AM_LDFLAGS = $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) -bin_PROGRAMS = idevice_id ideviceinfo idevicesyslog idevicebackup ideviceimagemounter +bin_PROGRAMS = idevice_id ideviceinfo idevicesyslog idevicebackup ideviceimagemounter idevicescreenshot ideviceinfo_SOURCES = ideviceinfo.c ideviceinfo_CFLAGS = $(AM_CFLAGS) @@ -29,3 +29,8 @@ ideviceimagemounter_SOURCES = ideviceimagemounter.c ideviceimagemounter_CFLAGS = $(AM_CFLAGS) ideviceimagemounter_LDFLAGS = $(AM_LDFLAGS) ideviceimagemounter_LDADD = ../src/libimobiledevice.la + +idevicescreenshot_SOURCES = idevicescreenshot.c +idevicescreenshot_CFLAGS = $(AM_CFLAGS) +idevicescreenshot_LDFLAGS = $(AM_LDFLAGS) +idevicescreenshot_LDADD = ../src/libimobiledevice.la diff --git a/tools/idevicescreenshot.c b/tools/idevicescreenshot.c new file mode 100644 index 0000000..ca4454c --- /dev/null +++ b/tools/idevicescreenshot.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include + +#include +#include +#include + +void print_usage(int argc, char **argv); + +int main(int argc, char **argv) +{ + idevice_t device = NULL; + lockdownd_client_t lckd = NULL; + screenshotr_client_t shotr = NULL; + uint16_t port = 0; + int result = -1; + int i; + char *uuid = NULL; + + /* parse cmdline args */ + for (i = 1; i < argc; i++) { + if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) { + idevice_set_debug_level(1); + continue; + } + else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--uuid")) { + i++; + if (!argv[i] || (strlen(argv[i]) != 40)) { + print_usage(argc, argv); + return 0; + } + uuid = strdup(argv[i]); + continue; + } + else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) { + print_usage(argc, argv); + return 0; + } + else { + print_usage(argc, argv); + return 0; + } + } + + if (IDEVICE_E_SUCCESS != idevice_new(&device, uuid)) { + printf("No device found, is it plugged in?\n"); + if (uuid) { + free(uuid); + } + return -1; + } + if (uuid) { + free(uuid); + } + + if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &lckd, NULL)) { + idevice_free(device); + printf("Exiting.\n"); + return -1; + } + + lockdownd_start_service(lckd, "com.apple.mobile.screenshotr", &port); + lockdownd_client_free(lckd); + if (port > 0) { + if (screenshotr_client_new(device, port, &shotr) != SCREENSHOTR_E_SUCCESS) { + printf("Could not connect to screenshotr!\n"); + } else { + char *imgdata = NULL; + uint64_t imgsize = 0; + if (screenshotr_take_screenshot(shotr, &imgdata, &imgsize) == SCREENSHOTR_E_SUCCESS) { + FILE *f = fopen("screenshot.tiff", "w"); + if (f) { + if (fwrite(imgdata, 1, (size_t)imgsize, f) == (size_t)imgsize) { + printf("Screenshot saved to screenshot.tiff\n"); + result = 0; + } else { + printf("Could not save screenshot to file!\n"); + } + fclose(f); + } else { + printf("Could not open screenshot.tiff for writing: %s\n", strerror(errno)); + } + } else { + printf("Could not get screenshot!\n"); + } + screenshotr_client_free(shotr); + } + } else { + printf("Could not start screenshotr service! Remember that you have to mount the Developer disk image on your device if you want to use the screenshotr service.\n"); + } + idevice_free(device); + + return result; +} + +void print_usage(int argc, char **argv) +{ + char *name = NULL; + + name = strrchr(argv[0], '/'); + printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0])); + printf("Gets a screenshot from the connected iPhone/iPod Touch.\n"); + printf("NOTE: A mounted developer disk image is required on the device, otherwise\n"); + printf(" the screenshotr service is not available.\n\n"); + printf(" -d, --debug\t\tenable communication debugging\n"); + printf(" -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n"); + printf(" -h, --help\t\tprints usage information\n"); + printf("\n"); +} -- cgit v1.1-32-gdbae