Debian Codegraph @

src/sbservices.c

raw
1 /*
2 * sbservices.c
3 * com.apple.springboardservices service implementation.
4 *
5 * Copyright (c) 2009 Nikias Bassen, All Rights Reserved.
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 #include <string.h>
26 #include <stdlib.h>
27
28 #ifndef _MSC_VER
29 #include <unistd.h>
30 #endif
31
32 #include <plist/plist.h>
33
34 #include "sbservices.h"
35 #include "property_list_service.h"
36 #include "common/debug.h"
37
38 /**
39 * Locks an sbservices client, used for thread safety.
40 *
41 * @param client sbservices client to lock.
42 */
43 static void sbservices_lock(sbservices_client_t client)
44 {
45 debug_info("Locked");
46 mutex_lock(&client->mutex);
47 }
48
49 /**
50 * Unlocks an sbservices client, used for thread safety.
51 *
52 * @param client sbservices client to unlock
53 */
54 static void sbservices_unlock(sbservices_client_t client)
55 {
56 debug_info("Unlocked");
57 mutex_unlock(&client->mutex);
58 }
59
60 /**
61 * Convert a property_list_service_error_t value to a sbservices_error_t value.
62 * Used internally to get correct error codes.
63 *
64 * @param err A property_list_service_error_t error code
65 *
66 * @return A matching sbservices_error_t error code,
67 * SBSERVICES_E_UNKNOWN_ERROR otherwise.
68 */
69 static sbservices_error_t sbservices_error(property_list_service_error_t err)
70 {
71 switch (err) {
72 case PROPERTY_LIST_SERVICE_E_SUCCESS:
73 return SBSERVICES_E_SUCCESS;
74 case PROPERTY_LIST_SERVICE_E_INVALID_ARG:
75 return SBSERVICES_E_INVALID_ARG;
76 case PROPERTY_LIST_SERVICE_E_PLIST_ERROR:
77 return SBSERVICES_E_PLIST_ERROR;
78 case PROPERTY_LIST_SERVICE_E_MUX_ERROR:
79 return SBSERVICES_E_CONN_FAILED;
80 default:
81 break;
82 }
83 return SBSERVICES_E_UNKNOWN_ERROR;
84 }
85
86 sbservices_error_t sbservices_client_new(idevice_t device, lockdownd_service_descriptor_t service, sbservices_client_t *client)
87 {
88 property_list_service_client_t plistclient = NULL;
89 sbservices_error_t err = sbservices_error(property_list_service_client_new(device, service, &plistclient));
90 if (err != SBSERVICES_E_SUCCESS) {
91 return err;
92 }
93
94 sbservices_client_t client_loc = (sbservices_client_t) malloc(sizeof(struct sbservices_client_private));
95 client_loc->parent = plistclient;
96 mutex_init(&client_loc->mutex);
97
98 *client = client_loc;
99 return SBSERVICES_E_SUCCESS;
100 }
101
102 sbservices_error_t sbservices_client_start_service(idevice_t device, sbservices_client_t * client, const char* label)
103 {
104 sbservices_error_t err = SBSERVICES_E_UNKNOWN_ERROR;
105 service_client_factory_start_service(device, SBSERVICES_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(sbservices_client_new), &err);
106 return err;
107 }
108
109 sbservices_error_t sbservices_client_free(sbservices_client_t client)
110 {
111 if (!client)
112 return SBSERVICES_E_INVALID_ARG;
113
114 sbservices_error_t err = sbservices_error(property_list_service_client_free(client->parent));
115 client->parent = NULL;
116 mutex_destroy(&client->mutex);
117 free(client);
118
119 return err;
120 }
121
122 sbservices_error_t sbservices_get_icon_state(sbservices_client_t client, plist_t *state, const char *format_version)
123 {
124 if (!client || !client->parent || !state)
125 return SBSERVICES_E_INVALID_ARG;
126
127 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR;
128
129 plist_t dict = plist_new_dict();
130 plist_dict_set_item(dict, "command", plist_new_string("getIconState"));
131 if (format_version) {
132 plist_dict_set_item(dict, "formatVersion", plist_new_string(format_version));
133 }
134
135 sbservices_lock(client);
136
137 res = sbservices_error(property_list_service_send_binary_plist(client->parent, dict));
138 if (res != SBSERVICES_E_SUCCESS) {
139 debug_info("could not send plist, error %d", res);
140 goto leave_unlock;
141 }
142 plist_free(dict);
143 dict = NULL;
144
145 res = sbservices_error(property_list_service_receive_plist(client->parent, state));
146 if (res != SBSERVICES_E_SUCCESS) {
147 debug_info("could not get icon state, error %d", res);
148 if (*state) {
149 plist_free(*state);
150 *state = NULL;
151 }
152 }
153
154 leave_unlock:
155 if (dict) {
156 plist_free(dict);
157 }
158 sbservices_unlock(client);
159 return res;
160 }
161
162 sbservices_error_t sbservices_set_icon_state(sbservices_client_t client, plist_t newstate)
163 {
164 if (!client || !client->parent || !newstate)
165 return SBSERVICES_E_INVALID_ARG;
166
167 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR;
168
169 plist_t dict = plist_new_dict();
170 plist_dict_set_item(dict, "command", plist_new_string("setIconState"));
171 plist_dict_set_item(dict, "iconState", plist_copy(newstate));
172
173 sbservices_lock(client);
174
175 res = sbservices_error(property_list_service_send_binary_plist(client->parent, dict));
176 if (res != SBSERVICES_E_SUCCESS) {
177 debug_info("could not send plist, error %d", res);
178 }
179
180 uint32_t bytes = 0;
181 service_receive_with_timeout(client->parent->parent, malloc(4), 4, &bytes, 2000);
182 debug_info("setIconState response: %u", bytes);
183
184 if (dict) {
185 plist_free(dict);
186 }
187 sbservices_unlock(client);
188 return res;
189 }
190
191 sbservices_error_t sbservices_get_icon_pngdata(sbservices_client_t client, const char *bundleId, char **pngdata, uint64_t *pngsize)
192 {
193 if (!client || !client->parent || !bundleId || !pngdata)
194 return SBSERVICES_E_INVALID_ARG;
195
196 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR;
197
198 plist_t dict = plist_new_dict();
199 plist_dict_set_item(dict, "command", plist_new_string("getIconPNGData"));
200 plist_dict_set_item(dict, "bundleId", plist_new_string(bundleId));
201
202 sbservices_lock(client);
203
204 res = sbservices_error(property_list_service_send_binary_plist(client->parent, dict));
205 if (res != SBSERVICES_E_SUCCESS) {
206 debug_info("could not send plist, error %d", res);
207 goto leave_unlock;
208 }
209 plist_free(dict);
210
211 dict = NULL;
212 res = sbservices_error(property_list_service_receive_plist(client->parent, &dict));
213 if (res == SBSERVICES_E_SUCCESS) {
214 plist_t node = plist_dict_get_item(dict, "pngData");
215 if (node) {
216 plist_get_data_val(node, pngdata, pngsize);
217 }
218 }
219
220 leave_unlock:
221 if (dict) {
222 plist_free(dict);
223 }
224 sbservices_unlock(client);
225 return res;
226 }
227
228 sbservices_error_t sbservices_get_interface_orientation(sbservices_client_t client, sbservices_interface_orientation_t* interface_orientation)
229 {
230 if (!client || !client->parent || !interface_orientation)
231 return SBSERVICES_E_INVALID_ARG;
232
233 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR;
234
235 plist_t dict = plist_new_dict();
236 plist_dict_set_item(dict, "command", plist_new_string("getInterfaceOrientation"));
237
238 sbservices_lock(client);
239
240 res = sbservices_error(property_list_service_send_binary_plist(client->parent, dict));
241 if (res != SBSERVICES_E_SUCCESS) {
242 debug_info("could not send plist, error %d", res);
243 goto leave_unlock;
244 }
245 plist_free(dict);
246 dict = NULL;
247
248 res = sbservices_error(property_list_service_receive_plist(client->parent, &dict));
249 if (res == SBSERVICES_E_SUCCESS) {
250 plist_t node = plist_dict_get_item(dict, "interfaceOrientation");
251 if (node) {
252 uint64_t value = SBSERVICES_INTERFACE_ORIENTATION_UNKNOWN;
253 plist_get_uint_val(node, &value);
254 *interface_orientation = (sbservices_interface_orientation_t)value;
255 }
256 }
257
258 leave_unlock:
259 if (dict) {
260 plist_free(dict);
261 }
262 sbservices_unlock(client);
263 return res;
264 }
265
266 sbservices_error_t sbservices_get_home_screen_wallpaper_pngdata(sbservices_client_t client, char **pngdata, uint64_t *pngsize)
267 {
268 if (!client || !client->parent || !pngdata)
269 return SBSERVICES_E_INVALID_ARG;
270
271 sbservices_error_t res = SBSERVICES_E_UNKNOWN_ERROR;
272
273 plist_t dict = plist_new_dict();
274 plist_dict_set_item(dict, "command", plist_new_string("getHomeScreenWallpaperPNGData"));
275
276 sbservices_lock(client);
277
278 res = sbservices_error(property_list_service_send_binary_plist(client->parent, dict));
279 if (res != SBSERVICES_E_SUCCESS) {
280 debug_info("could not send plist, error %d", res);
281 goto leave_unlock;
282 }
283 plist_free(dict);
284
285 dict = NULL;
286 res = sbservices_error(property_list_service_receive_plist(client->parent, &dict));
287 if (res == SBSERVICES_E_SUCCESS) {
288 plist_t node = plist_dict_get_item(dict, "pngData");
289 if (node) {
290 plist_get_data_val(node, pngdata, pngsize);
291 }
292 }
293
294 leave_unlock:
295 if (dict) {
296 plist_free(dict);
297 }
298 sbservices_unlock(client);
299 return res;
300 }