summaryrefslogtreecommitdiffstats
path: root/tools/idevicescreenshot.c
diff options
context:
space:
mode:
Diffstat (limited to 'tools/idevicescreenshot.c')
-rw-r--r--tools/idevicescreenshot.c111
1 files changed, 111 insertions, 0 deletions
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 @@
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <errno.h>
5
6#include <libimobiledevice/libimobiledevice.h>
7#include <libimobiledevice/lockdown.h>
8#include <libimobiledevice/screenshotr.h>
9
10void print_usage(int argc, char **argv);
11
12int main(int argc, char **argv)
13{
14 idevice_t device = NULL;
15 lockdownd_client_t lckd = NULL;
16 screenshotr_client_t shotr = NULL;
17 uint16_t port = 0;
18 int result = -1;
19 int i;
20 char *uuid = NULL;
21
22 /* parse cmdline args */
23 for (i = 1; i < argc; i++) {
24 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug")) {
25 idevice_set_debug_level(1);
26 continue;
27 }
28 else if (!strcmp(argv[i], "-u") || !strcmp(argv[i], "--uuid")) {
29 i++;
30 if (!argv[i] || (strlen(argv[i]) != 40)) {
31 print_usage(argc, argv);
32 return 0;
33 }
34 uuid = strdup(argv[i]);
35 continue;
36 }
37 else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
38 print_usage(argc, argv);
39 return 0;
40 }
41 else {
42 print_usage(argc, argv);
43 return 0;
44 }
45 }
46
47 if (IDEVICE_E_SUCCESS != idevice_new(&device, uuid)) {
48 printf("No device found, is it plugged in?\n");
49 if (uuid) {
50 free(uuid);
51 }
52 return -1;
53 }
54 if (uuid) {
55 free(uuid);
56 }
57
58 if (LOCKDOWN_E_SUCCESS != lockdownd_client_new_with_handshake(device, &lckd, NULL)) {
59 idevice_free(device);
60 printf("Exiting.\n");
61 return -1;
62 }
63
64 lockdownd_start_service(lckd, "com.apple.mobile.screenshotr", &port);
65 lockdownd_client_free(lckd);
66 if (port > 0) {
67 if (screenshotr_client_new(device, port, &shotr) != SCREENSHOTR_E_SUCCESS) {
68 printf("Could not connect to screenshotr!\n");
69 } else {
70 char *imgdata = NULL;
71 uint64_t imgsize = 0;
72 if (screenshotr_take_screenshot(shotr, &imgdata, &imgsize) == SCREENSHOTR_E_SUCCESS) {
73 FILE *f = fopen("screenshot.tiff", "w");
74 if (f) {
75 if (fwrite(imgdata, 1, (size_t)imgsize, f) == (size_t)imgsize) {
76 printf("Screenshot saved to screenshot.tiff\n");
77 result = 0;
78 } else {
79 printf("Could not save screenshot to file!\n");
80 }
81 fclose(f);
82 } else {
83 printf("Could not open screenshot.tiff for writing: %s\n", strerror(errno));
84 }
85 } else {
86 printf("Could not get screenshot!\n");
87 }
88 screenshotr_client_free(shotr);
89 }
90 } else {
91 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");
92 }
93 idevice_free(device);
94
95 return result;
96}
97
98void print_usage(int argc, char **argv)
99{
100 char *name = NULL;
101
102 name = strrchr(argv[0], '/');
103 printf("Usage: %s [OPTIONS]\n", (name ? name + 1: argv[0]));
104 printf("Gets a screenshot from the connected iPhone/iPod Touch.\n");
105 printf("NOTE: A mounted developer disk image is required on the device, otherwise\n");
106 printf(" the screenshotr service is not available.\n\n");
107 printf(" -d, --debug\t\tenable communication debugging\n");
108 printf(" -u, --uuid UUID\ttarget specific device by its 40-digit device UUID\n");
109 printf(" -h, --help\t\tprints usage information\n");
110 printf("\n");
111}