Debian Codegraph @

xpc/xpc_wrapper_darwin.c

raw symbols SCIP
1 #include <dispatch/dispatch.h>
2 #include <xpc/xpc.h>
3 #include <xpc/connection.h>
4 #include <Block.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7
8 #include "_cgo_export.h"
9
10 //
11 // types and errors are implemented as macros
12 // create some real objects to make them accessible to Go
13 //
14 xpc_type_t TYPE_ERROR = XPC_TYPE_ERROR;
15
16 xpc_type_t TYPE_ARRAY = XPC_TYPE_ARRAY;
17 xpc_type_t TYPE_DATA = XPC_TYPE_DATA;
18 xpc_type_t TYPE_DICT = XPC_TYPE_DICTIONARY;
19 xpc_type_t TYPE_INT64 = XPC_TYPE_INT64;
20 xpc_type_t TYPE_STRING = XPC_TYPE_STRING;
21 xpc_type_t TYPE_UUID = XPC_TYPE_UUID;
22
23 xpc_object_t ERROR_CONNECTION_INVALID = (xpc_object_t) XPC_ERROR_CONNECTION_INVALID;
24 xpc_object_t ERROR_CONNECTION_INTERRUPTED = (xpc_object_t) XPC_ERROR_CONNECTION_INTERRUPTED;
25 xpc_object_t ERROR_CONNECTION_TERMINATED = (xpc_object_t) XPC_ERROR_TERMINATION_IMMINENT;
26
27 const ptr_to_uuid_t ptr_to_uuid(void *p) { return (ptr_to_uuid_t)p; }
28
29
30 //
31 // connect to XPC service
32 //
33 xpc_connection_t XpcConnect(char *service, uintptr_t ctx) {
34 dispatch_queue_t queue = dispatch_queue_create(service, 0);
35 xpc_connection_t conn = xpc_connection_create_mach_service(service, queue, XPC_CONNECTION_MACH_SERVICE_PRIVILEGED);
36
37 // making a local copy, that should be made "persistent" with the following Block_copy
38 // GoInterface ictx = *((GoInterface*)ctx);
39
40 xpc_connection_set_event_handler(conn,
41 Block_copy(^(xpc_object_t event) {
42 handleXpcEvent(event, ctx);
43 })
44 );
45
46 xpc_connection_resume(conn);
47 return conn;
48 }
49
50 void XpcSendMessage(xpc_connection_t conn, xpc_object_t message, bool release, bool reportDelivery) {
51 xpc_connection_send_message(conn, message);
52 xpc_connection_send_barrier(conn, ^{
53 // Block is invoked on connection's target queue
54 // when 'message' has been sent.
55 if (reportDelivery) { // maybe this could be a callback
56 puts("message delivered");
57 }
58 });
59 if (release) {
60 xpc_release(message);
61 }
62 }
63
64 void XpcArrayApply(uintptr_t v, xpc_object_t arr) {
65 xpc_array_apply(arr, ^bool(size_t index, xpc_object_t value) {
66 arraySet(v, index, value);
67 return true;
68 });
69 }
70
71 void XpcDictApply(uintptr_t v, xpc_object_t dict) {
72 xpc_dictionary_apply(dict, ^bool(const char *key, xpc_object_t value) {
73 dictSet(v, (char *)key, value);
74 return true;
75 });
76 }
77
78 void XpcUUIDGetBytes(void *v, xpc_object_t uuid) {
79 const uint8_t *src = xpc_uuid_get_bytes(uuid);
80 uint8_t *dest = (uint8_t *)v;
81
82 for (int i=0; i < sizeof(uuid_t); i++) {
83 dest[i] = src[i];
84 }
85 }