summaryrefslogtreecommitdiff
path: root/example-clients/property.c
blob: 4d5e2a22ff8d8088d6f8623801778a91ee54c88b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>

#include <jack/jack.h>
#include <jack/metadata.h>
#include <jack/uuid.h>
#include <jack/session.h>

static int subject_is_client = 0;
static int subject_is_port = 0;
static jack_uuid_t uuid = JACK_UUID_EMPTY_INITIALIZER;
static char* subject = NULL;

static void
show_usage (void)
{
        fprintf (stderr, "\nUsage: jack_property [options] UUID [ key [ value [ type ] ] ]\n");
        fprintf (stderr, "Set/Display JACK properties (metadata).\n\n");
        fprintf (stderr, "Set options:\n");
        fprintf (stderr, "        -s, --set		Set property \"key\" to \"value\" for \"UUID\" with optional MIME type \"type\"\n");
        fprintf (stderr, "        -d, --delete		Remove/delete property \"key\" for \"UUID\"\n");
        fprintf (stderr, "        -d, --delete UUID	Remove/delete all properties for \"UUID\"\n");
        fprintf (stderr, "        -D, --delete-all	Remove/delete all properties\n");
        fprintf (stderr, "        --client		Interpret UUID as a client name, not a UUID\n");
        fprintf (stderr, "        --port		\tInterpret UUID as a port name, not a UUID\n");
        fprintf (stderr, "\nDisplay options:\n");
        fprintf (stderr, "        -l			Show all properties\n");
        fprintf (stderr, "        -l, --list UUID	\tShow value for all properties of UUID\n");
        fprintf (stderr, "        -l, --list UUID key	Show value for key of UUID\n");
        fprintf (stderr, "\nFor more information see https://jackaudio.org/\n");
}

static int
get_subject (jack_client_t* client, char* argv[], int* optind)
{
        if (subject_is_client) {
                char* cstr = argv[(*optind)++];
                char* ustr;

                if ((ustr = jack_get_uuid_for_client_name (client, cstr)) == NULL) {
                        fprintf (stderr, "cannot get UUID for client named %s\n", cstr);
                        return -1;
                }

                if (jack_uuid_parse (ustr, &uuid)) {
                        fprintf (stderr, "cannot parse client UUID as UUID '%s' '%s'\n", cstr, ustr);
                        return -1;
                }

                subject = cstr;

        } else if (subject_is_port) {

                char* pstr = argv[(*optind)++];
                jack_port_t* port;

                if ((port = jack_port_by_name (client, pstr)) == NULL) {
                        fprintf (stderr, "cannot find port name %s\n", pstr);
                        return -1;
                }

                uuid = jack_port_uuid (port);
                subject = pstr;

        } else {
                char* str = argv[(*optind)++];

                if (jack_uuid_parse (str, &uuid)) {
                        fprintf (stderr, "cannot parse subject as UUID\n");
                        return -1;
                }

                subject = str;
        }

        return 0;
}

int main (int argc, char* argv[])
{
        jack_client_t* client = NULL;
        jack_options_t options = JackNoStartServer;
        char* key = NULL;
        char* value = NULL;
        char* type = NULL;
        int set = 1;
        int delete = 0;
        int delete_all = 0;
        int c;
        int option_index;
        extern int optind;
        struct option long_options[] = {
            { "set", 0, 0, 's' },
            { "delete", 0, 0, 'd' },
            { "delete-all", 0, 0, 'D' },
            { "list", 0, 0, 'l' },
            { "client", 0, 0, 'c' },
            { "port", 0, 0, 'p' },
            { 0, 0, 0, 0 }
        };

        if (argc < 2) {
                show_usage ();
                exit (1);
        }

        while ((c = getopt_long (argc, argv, "sdDlaApc", long_options, &option_index)) >= 0) {
            switch (c) {
            case 's':
                        if (argc < 5) {
                                show_usage ();
                                exit (1);
                        }
                        set = 1;
                        break;
                case 'd':
                        if (argc < 3) {
                                show_usage ();
                                return 1;
                        }
                        set = 0;
                        delete = 1;
                        break;

                case 'D':
                        delete = 0;
                        set = 0;
                        delete_all = 1;
                        break;

                case 'l':
                        set = 0;
                        delete = 0;
                        delete_all = 0;
                        break;

                case 'p':
                        subject_is_port = 1;
                        break;

                case 'c':
                        subject_is_client = 1;
                        break;

                case '?':
                default:
                        show_usage ();
                        exit (1);
                }
        }

        if ((client = jack_client_open ("jack-property", options, NULL)) == 0) {
                fprintf (stderr, "Cannot connect to JACK server\n");
                exit (1);
        }

        if (delete_all) {

                if (jack_remove_all_properties (client) == 0) {
                        printf ("JACK metadata successfully delete\n");
                        exit (0);
                }
                exit (1);
        }

        if (delete) {

                int args_left = argc - optind;

                if (args_left < 1) {
                        show_usage ();
                        exit (1);
                }

                /* argc == 3: delete all properties for a subject
                   argc == 4: delete value of key for subject
                */

                if (args_left >= 2) {

                        if (get_subject (client, argv, &optind)) {
                                return 1;
                        }

                        key = argv[optind++];

                        if (jack_remove_property (client, uuid, key)) {
                                fprintf (stderr, "\"%s\" property not removed for %s\n", key, subject);
                                exit (1);
                        }

                } else {

                        if (get_subject (client, argv, &optind)) {
                                return 1;
                        }

                        if (jack_remove_properties (client, uuid) < 0) {
                                fprintf (stderr, "cannot remove properties for UUID %s\n", subject);
                                exit (1);
                        }
                }

        }  else if (set) {

                int args_left = argc - optind;

                if (get_subject (client, argv, &optind)) {
                        return -1;
                }

                key = argv[optind++];
                value = argv[optind++];

                if (args_left >= 3) {
                        type = argv[optind++];
                } else {
                        type = "";
                }

                if (jack_set_property (client, uuid, key, value, type)) {
                        fprintf (stderr, "cannot set value for key %s of %s\n", key, subject);
                        exit (1);
                }

        } else {

                /* list properties */

                int args_left = argc - optind;

                if (args_left >= 2) {

                        /* list properties for a UUID/key pair */

                        if (get_subject (client, argv, &optind)) {
                                return -1;
                        }

                        key = argv[optind++];

                        if (jack_get_property (uuid, key, &value, &type) == 0) {
                                printf ("%s\n", value);
                                free (value);
                                if (type) {
                                        free (type);
                                }
                        } else {
                                fprintf (stderr, "Value not found for %s of %s\n", key, subject);
                                exit (1);
                        }

                } else if (args_left == 1) {

                        /* list all properties for a given UUID */

                        jack_description_t description;
                        int cnt, n;

                        if (get_subject (client, argv, &optind)) {
                                return -1;
                        }

                        if ((cnt = jack_get_properties (uuid, &description)) < 0) {
                                fprintf (stderr, "could not retrieve properties for %s\n", subject);
                                exit (1);
                        }

                        for (n = 0; n < cnt; ++n) {
                                if (description.properties[n].type) {
                                        printf ("key: %s value: %s type: %s\n",
                                                description.properties[n].key,
                                                description.properties[n].data,
                                                description.properties[n].type);
                                } else {
                                        printf ("key: %s value: %s\n",
                                                description.properties[n].key,
                                                description.properties[n].data);
                                }
                        }

                        jack_free_description (&description, 0);

                } else {

                        /* list all properties */

                        jack_description_t* description;
                        int cnt, n;
                        size_t p;
                        char buf[JACK_UUID_STRING_SIZE];

                        if ((cnt = jack_get_all_properties (&description)) < 0) {
                                fprintf (stderr, "could not retrieve all properties\n");
                                exit (1);
                        }

                        for (n = 0; n < cnt; ++n) {
                                jack_uuid_unparse (description[n].subject, buf);
                                printf ("%s\n", buf);
                                for (p = 0; p < description[n].property_cnt; ++p) {
                                        if (description[n].properties[p].type) {
                                                printf ("key: %s value: %s type: %s\n",
                                                        description[n].properties[p].key,
                                                        description[n].properties[p].data,
                                                        description[n].properties[p].type);
                                        } else {
                                                printf ("key: %s value: %s\n",
                                                        description[n].properties[p].key,
                                                        description[n].properties[p].data);
                                        }
                                }
                                jack_free_description (&description[n], 0);
                        }

                        free (description);
                }
        }


        (void) jack_client_close (client);
        return 0;
}