Debian Codegraph @

tools/idevicescreenshot.c

raw
1 /*
2 * idevicescreenshot.c
3 * Gets a screenshot from a device
4 *
5 * Copyright (C) 2010 Nikias Bassen <[email protected]>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #define TOOL_NAME "idevicescreenshot"
27
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <getopt.h>
32 #include <errno.h>
33 #include <time.h>
34 #include <unistd.h>
35 #ifndef _WIN32
36 #include <signal.h>
37 #endif
38
39 #include <libimobiledevice/libimobiledevice.h>
40 #include <libimobiledevice/lockdown.h>
41 #include <libimobiledevice/screenshotr.h>
42
43 static void get_image_filename(char *imgdata, char **filename)
44 {
45 // If the provided filename already has an extension, use it as is.
46 if (*filename) {
47 char *last_dot = strrchr(*filename, '.');
48 if (last_dot && !strchr(last_dot, '/')) {
49 return;
50 }
51 }
52
53 // Find the appropriate file extension for the filename.
54 const char *fileext = NULL;
55 if (memcmp(imgdata, "\x89PNG", 4) == 0) {
56 fileext = ".png";
57 } else if (memcmp(imgdata, "MM\x00*", 4) == 0) {
58 fileext = ".tiff";
59 } else {
60 printf("WARNING: screenshot data has unexpected image format.\n");
61 fileext = ".dat";
62 }
63
64 // If a filename without an extension is provided, append the extension.
65 // Otherwise, generate a filename based on the current time.
66 char *basename = NULL;
67 if (*filename) {
68 basename = (char*)malloc(strlen(*filename) + 1);
69 strcpy(basename, *filename);
70 free(*filename);
71 *filename = NULL;
72 } else {
73 time_t now = time(NULL);
74 basename = (char*)malloc(32);
75 strftime(basename, 31, "screenshot-%Y-%m-%d-%H-%M-%S", gmtime(&now));
76 }
77
78 // Ensure the filename is unique on disk.
79 char *unique_filename = (char*)malloc(strlen(basename) + strlen(fileext) + 7);
80 sprintf(unique_filename, "%s%s", basename, fileext);
81 int i;
82 for (i = 2; i < (1 << 16); i++) {
83 if (access(unique_filename, F_OK) == -1) {
84 *filename = unique_filename;
85 break;
86 }
87 sprintf(unique_filename, "%s-%d%s", basename, i, fileext);
88 }
89 if (!*filename) {
90 free(unique_filename);
91 }
92 free(basename);
93 }
94
95 static void print_usage(int argc, char **argv, int is_error)
96 {
97 char *name = strrchr(argv[0], '/');
98 fprintf(is_error ? stderr : stdout, "Usage: %s [OPTIONS] [FILE]\n", (name ? name + 1: argv[0]));
99 fprintf(is_error ? stderr : stdout,
100 "\n"
101 "Gets a screenshot from a connected device.\n"
102 "\n"
103 "The image is in PNG format for iOS 9+ and otherwise in TIFF format.\n"
104 "The screenshot is saved as an image with the given FILE name.\n"
105 "If FILE has no extension, FILE will be a prefix of the saved filename.\n"
106 "If FILE is not specified, \"screenshot-DATE\", will be used as a prefix\n"
107 "of the filename, e.g.:\n"
108 " ./screenshot-2013-12-31-23-59-59.tiff\n"
109 "\n"
110 "NOTE: A mounted developer disk image is required on the device, otherwise\n"
111 "the screenshotr service is not available.\n"
112 "\n"
113 " -u, --udid UDID target specific device by UDID\n"
114 " -n, --network connect to network device\n"
115 " -d, --debug enable communication debugging\n"
116 " -h, --help prints usage information\n"
117 " -v, --version prints version information\n"
118 "\n"
119 "Homepage: <" PACKAGE_URL ">\n"
120 "Bug Reports: <" PACKAGE_BUGREPORT ">\n"
121 );
122 }
123
124 int main(int argc, char **argv)
125 {
126 idevice_t device = NULL;
127 lockdownd_client_t lckd = NULL;
128 lockdownd_error_t ldret = LOCKDOWN_E_UNKNOWN_ERROR;
129 screenshotr_client_t shotr = NULL;
130 lockdownd_service_descriptor_t service = NULL;
131 int result = -1;
132 const char *udid = NULL;
133 int use_network = 0;
134 char *filename = NULL;
135 int c = 0;
136 const struct option longopts[] = {
137 { "debug", no_argument, NULL, 'd' },
138 { "help", no_argument, NULL, 'h' },
139 { "udid", required_argument, NULL, 'u' },
140 { "network", no_argument, NULL, 'n' },
141 { "version", no_argument, NULL, 'v' },
142 { NULL, 0, NULL, 0}
143 };
144
145 #ifndef _WIN32
146 signal(SIGPIPE, SIG_IGN);
147 #endif
148 /* parse cmdline args */
149
150 /* parse cmdline arguments */
151 while ((c = getopt_long(argc, argv, "dhu:nv", longopts, NULL)) != -1) {
152 switch (c) {
153 case 'd':
154 idevice_set_debug_level(1);
155 break;
156 case 'u':
157 if (!*optarg) {
158 fprintf(stderr, "ERROR: UDID argument must not be empty!\n");
159 print_usage(argc, argv, 1);
160 return 2;
161 }
162 udid = optarg;
163 break;
164 case 'n':
165 use_network = 1;
166 break;
167 case 'h':
168 print_usage(argc, argv, 0);
169 return 0;
170 case 'v':
171 printf("%s %s\n", TOOL_NAME, PACKAGE_VERSION);
172 return 0;
173 default:
174 print_usage(argc, argv, 1);
175 return 2;
176 }
177 }
178 argc -= optind;
179 argv += optind;
180
181 if (argv[0]) {
182 filename = strdup(argv[0]);
183 }
184
185 if (IDEVICE_E_SUCCESS != idevice_new_with_options(&device, udid, (use_network) ? IDEVICE_LOOKUP_NETWORK : IDEVICE_LOOKUP_USBMUX)) {
186 if (udid) {
187 printf("No device found with udid %s.\n", udid);
188 } else {
189 printf("No device found.\n");
190 }
191 return -1;
192 }
193
194 if (LOCKDOWN_E_SUCCESS != (ldret = lockdownd_client_new_with_handshake(device, &lckd, TOOL_NAME))) {
195 idevice_free(device);
196 printf("ERROR: Could not connect to lockdownd, error code %d\n", ldret);
197 return -1;
198 }
199
200 lockdownd_error_t lerr = lockdownd_start_service(lckd, SCREENSHOTR_SERVICE_NAME, &service);
201 lockdownd_client_free(lckd);
202 if (lerr == LOCKDOWN_E_SUCCESS) {
203 if (screenshotr_client_new(device, service, &shotr) != SCREENSHOTR_E_SUCCESS) {
204 printf("Could not connect to screenshotr!\n");
205 } else {
206 char *imgdata = NULL;
207 uint64_t imgsize = 0;
208 if (screenshotr_take_screenshot(shotr, &imgdata, &imgsize) == SCREENSHOTR_E_SUCCESS) {
209 get_image_filename(imgdata, &filename);
210 if (!filename) {
211 printf("FATAL: Could not find a unique filename!\n");
212 } else {
213 FILE *f = fopen(filename, "wb");
214 if (f) {
215 if (fwrite(imgdata, 1, (size_t)imgsize, f) == (size_t)imgsize) {
216 printf("Screenshot saved to %s\n", filename);
217 result = 0;
218 } else {
219 printf("Could not save screenshot to file %s!\n", filename);
220 }
221 fclose(f);
222 } else {
223 printf("Could not open %s for writing: %s\n", filename, strerror(errno));
224 }
225 }
226 } else {
227 printf("Could not get screenshot!\n");
228 }
229 screenshotr_client_free(shotr);
230 }
231 } else {
232 printf("Could not start screenshotr service: %s\nRemember that you have to mount the Developer disk image on your device if you want to use the screenshotr service.\n", lockdownd_strerror(lerr));
233 }
234
235 if (service)
236 lockdownd_service_descriptor_free(service);
237
238 idevice_free(device);
239 free(filename);
240
241 return result;
242 }