homescreen/: Plug-in the launcher in the applicationlauncher
[apps/homescreen.git] / pws / pws.cpp
1 /*
2  * Copyright (c) 2020 Collabora Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <cstdio>
18 #include <cstdlib>
19
20 #include <systemd/sd-event.h>
21 #include <json-c/json.h>
22 #include <assert.h>
23 #include "pws.h"
24
25 #if !defined(JSON_C_TO_STRING_NOSLASHESCAPE)
26 #define JSON_C_TO_STRING_NOSLASHESCAPE 0
27 #endif
28
29 #define PWS_DEBUG
30
31 static void
32 idle(struct pws_data_source *pws_d_source)
33 {
34         struct sd_event *loop = pws_d_source->loop;
35
36         for (;;) {
37                 if (!pws_d_source->callcount)
38                         break;
39
40                 sd_event_run(loop, TIMEOUT_SD_LOOP);
41         }
42 }
43
44 static void
45 dec_callcount(struct pws_data_source *pws_d_source)
46 {
47         if (!pws_d_source)
48                 return;
49
50         pws_d_source->callcount--;
51 }
52
53 static void
54 inc_callcount(struct pws_data_source *pws_d_source)
55 {
56         if (!pws_d_source)
57                 return;
58
59         pws_d_source->callcount++;
60 }
61
62 static void
63 on_pws_reply(void *closure, void *request, struct json_object *result,
64              const char *error, const char *info)
65 {
66         struct pws_data_source *pws_d_source =
67                 static_cast<struct pws_data_source *>(closure);
68
69         assert(pws_d_source != NULL);
70
71 #ifdef PWS_DEBUG
72         fprintf(stdout, "ON-REPLY %s: %s %s\n%s\n", (char*) request, error,
73                         info ?: "",
74                         json_object_to_json_string_ext(result,
75                                 JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE));
76
77         fflush(stdout);
78 #endif
79
80         /* in case of an error do not set the reply */
81         if (!info && !error) {
82                 /* should be cleaned-up after a proper request */
83                 assert(pws_d_source->reply_valid == false);
84
85                 pws_d_source->reply = result;
86                 pws_d_source->reply_valid = true;
87         } else {
88                 fprintf(stdout, "ON-REPLY: err: %s, request: %s, info %s\n",
89                                 error, (char *) request, info);
90         }
91
92         /* necessary when getting the reply to exit idle() */
93         dec_callcount(pws_d_source);
94 }
95
96 static void
97 on_pws_event_create(void *closure, uint16_t event_id, const char *event_name)
98 {
99         struct pws_data_source *pws_d_source =
100                 static_cast<struct pws_data_source *>(closure);
101         (void) pws_d_source;
102
103 #ifdef PWS_DEBUG
104         fprintf(stdout, "ON-EVENT-CREATE: [%d:%s]\n", event_id, event_name);
105         fflush(stdout);
106 #endif
107 }
108
109 static void
110 on_pws_event_remove(void *closure, uint16_t event_id)
111 {
112         struct pws_data_source *pws_d_source =
113                 static_cast<struct pws_data_source *>(closure);
114
115         (void) pws_d_source;
116 #ifdef PWS_DEBUG
117         fprintf(stdout, "ON-EVENT-REMOVE: [%d]\n", event_id);
118         fflush(stdout);
119 #endif
120 }
121
122 static void
123 on_pws_event_subscribe(void *closure, void *request, uint16_t event_id)
124 {
125         struct pws_data_source *pws_d_source =
126                 static_cast<struct pws_data_source *>(closure);
127
128         (void) pws_d_source;
129 #ifdef PWS_DEBUG
130         fprintf(stdout, "ON-EVENT-SUBSCRIBE %s: [%d]\n", (char*)request, event_id);
131         fflush(stdout);
132 #endif
133 }
134
135 static void
136 on_pws_event_unsubscribe(void *closure, void *request, uint16_t event_id)
137 {
138         struct pws_data_source *pws_d_source =
139                 static_cast<struct pws_data_source *>(closure);
140
141         (void) pws_d_source;
142 #ifdef PWS_DEBUG
143         fprintf(stdout, "ON-EVENT-UNSUBSCRIBE %s: [%d]\n", (char*)request, event_id);
144         fflush(stdout);
145 #endif
146 }
147
148 static void
149 on_pws_event_push(void *closure, uint16_t event_id, struct json_object *data)
150 {
151         struct pws_data_source *pws_d_source =
152                 static_cast<struct pws_data_source *>(closure);
153
154         (void) pws_d_source;
155 #ifdef PWS_DEBUG
156         fprintf(stdout, "ON-EVENT-PUSH: [%d]\n%s\n",
157                         event_id,
158                         json_object_to_json_string_ext(data, JSON_C_TO_STRING_NOSLASHESCAPE));
159         fprintf(stdout, "ON-EVENT-PUSH: [%d]\n%s\n",
160                         event_id,
161                         json_object_to_json_string_ext(data, JSON_C_TO_STRING_PRETTY |
162                                                              JSON_C_TO_STRING_NOSLASHESCAPE));
163
164         fflush(stdout);
165 #endif
166 }
167
168 static void
169 on_pws_event_broadcast(void *closure, const char *event_name,
170                       struct json_object *data,
171                       const afb_proto_ws_uuid_t uuid, uint8_t hop)
172 {
173         struct pws_data_source *pws_d_source =
174                 static_cast<struct pws_data_source *>(closure);
175
176         (void) pws_d_source;
177         (void) uuid;
178         (void) hop;
179
180 #ifdef PWS_DEBUG
181         fprintf(stdout, "ON-EVENT-BROADCAST: [%s]\n%s\n",
182                         event_name,
183                         json_object_to_json_string_ext(data, JSON_C_TO_STRING_NOSLASHESCAPE));
184         fprintf(stdout, "ON-EVENT-BROADCAST: [%s]\n%s\n",
185                         event_name,
186                         json_object_to_json_string_ext(data, JSON_C_TO_STRING_PRETTY |
187                                                              JSON_C_TO_STRING_NOSLASHESCAPE));
188         fflush(stdout);
189 #endif
190 }
191
192 /* called when pws hangsup */
193 static void
194 on_pws_hangup(void *closure)
195 {
196         struct pws_data_source *pws_d_source =
197                 static_cast<struct pws_data_source *>(closure);
198
199         (void) pws_d_source;
200 #ifdef PWS_DEBUG
201         printf("ON-HANGUP\n");
202         fflush(stdout);
203 #endif
204
205         exit(EXIT_FAILURE);
206 }
207
208 /* makes a call */
209 static int
210 pws_call(const char *verb, const char *object,
211          struct pws_data_source *pws_d_source)
212 {
213         char *key;
214         struct json_object *o;
215         enum json_tokener_error jerr;
216
217         struct afb_proto_ws *pws = pws_d_source->pws;
218         int rc;
219
220         assert(pws != NULL);
221
222         /* allocates an id for the request */
223         rc = asprintf(&key, "%d:%s", ++pws_d_source->num_id, verb);
224         if (rc < 0)
225                 return -1;
226
227         /* echo the command if asked */
228         fprintf(stdout, "SEND-CALL: %s %s\n", verb, object?:"null");
229
230         inc_callcount(pws_d_source);
231
232         if (object == NULL || object[0] == 0) {
233                 o = NULL;
234         } else {
235                 o = json_tokener_parse_verbose(object, &jerr);
236                 if (jerr != json_tokener_success)
237                         o = json_object_new_string(object);
238         }
239
240         /* send the request */
241         rc = afb_proto_ws_client_call(pws, verb, o, 0, 0, key, NULL);
242         json_object_put(o);
243         if (rc < 0) {
244                 fprintf(stderr, "calling %s(%s) failed: %m\n", verb, object ?: "");
245                 dec_callcount(pws_d_source);
246                 free(key);
247                 return -1;
248         }
249
250         free(key);
251         return 0;
252 }
253
254 /* the callback interface for pws */
255 static struct afb_proto_ws_client_itf pws_itf = {
256         .on_reply = on_pws_reply,
257         .on_event_create = on_pws_event_create,
258         .on_event_remove = on_pws_event_remove,
259         .on_event_subscribe = on_pws_event_subscribe,
260         .on_event_unsubscribe = on_pws_event_unsubscribe,
261         .on_event_push = on_pws_event_push,
262         .on_event_broadcast = on_pws_event_broadcast,
263 };
264
265 struct pws_data_source *
266 pws_data_source_init(const char *connect_to)
267 {
268         struct afb_proto_ws *pws;
269         struct sd_event *sd_loop;
270         struct pws_data_source *pws_d_source = nullptr;
271
272         if (!connect_to) {
273                 fprintf(stderr, "Failed to get a connect_to\n");
274                 return nullptr;
275         }
276
277         if (sd_event_default(&sd_loop) < 0) {
278                 fprintf(stderr, "Failed to get a default event\n");
279                 return nullptr;
280         }
281
282         pws_d_source =
283                 static_cast<struct pws_data_source *>(calloc(1, sizeof(struct pws_data_source)));
284
285         if (!pws_d_source) {
286                 fprintf(stderr, "Failed to allocate memory\n");
287                 return nullptr;
288         }
289
290         pws = afb_ws_client_connect_api(sd_loop, connect_to,
291                                         &pws_itf, pws_d_source);
292         if (!pws) {
293                 fprintf(stderr, "Failed to create a afb_proto_ws\n");
294                 return nullptr;
295         }
296
297         afb_proto_ws_on_hangup(pws, on_pws_hangup);
298
299         pws_d_source->pws = pws;
300         pws_d_source->loop = sd_loop;
301         pws_d_source->callcount = 0;
302         pws_d_source->num_id = 0;
303
304         pws_d_source->reply = nullptr;
305         pws_d_source->reply_valid = false;
306
307         return pws_d_source;
308 }
309
310 static int
311 pws_do_call(struct pws_data_source *pws_d_source,
312             const char *verb, const char *object)
313 {
314         if (!verb || !object || !pws_d_source)
315                 return -1;
316
317         if (pws_call(verb, object, pws_d_source) < 0)
318                 return -1;
319
320         idle(pws_d_source);
321
322         return 0;
323 }
324
325 void
326 pws_data_source_destroy(struct pws_data_source *pws_d_source)
327 {
328         assert(pws_d_source != nullptr);
329
330         sd_event_unref(pws_d_source->loop);
331         free(pws_d_source);
332 }
333
334 void
335 pws_data_source_reply_destroy(struct pws_data_source *pws)
336 {
337         assert(pws->reply_valid == true);
338         pws->reply_valid = false;
339         free(pws->reply);
340 }
341
342
343 int
344 pws_start_process(struct pws_data_source *pws, const char *afm_name)
345 {
346         int rid = -1;
347         if (pws_do_call(pws, "start", afm_name) < 0)
348                 return -1;
349
350         if (pws->reply_valid) {
351                 struct json_object *result = pws->reply;
352
353                 rid = json_object_get_int(result);
354                 fprintf(stdout, "ON-REPLY %s\n",
355                                 json_object_to_json_string_ext(result,
356                                         JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE));
357
358                 pws_data_source_reply_destroy(pws);
359         }
360
361         return rid;
362 }
363
364 bool
365 pws_stop_process(struct pws_data_source *pws, const char *afm_name)
366 {
367         bool term = false;
368         if (pws_do_call(pws, "terminate", afm_name) < 0)
369                 return term;
370
371         if (pws->reply_valid) {
372                 struct json_object *result = pws->reply;
373
374                 term = json_object_get_boolean(result);
375 #ifdef PWS_DEBUG
376                 fprintf(stdout, "ON-REPLY %s\n",
377                                 json_object_to_json_string_ext(result,
378                                 JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE));
379 #endif
380                 pws_data_source_reply_destroy(pws);
381         }
382
383         return term;
384 }
385
386
387 int
388 pws_check_process_is_running(struct pws_data_source *pws, const char *afm_name)
389 {
390         int rid = -1;
391
392         if (pws_do_call(pws, "state", afm_name) < 0)
393                 return rid;
394
395         if (pws->reply_valid) {
396                 struct json_object *result = pws->reply;
397                 struct json_object *obj;
398
399                 json_object_object_get_ex(result, "runid", &obj);
400                 if (obj) {
401                         rid = json_object_get_int(obj);
402                         fprintf(stdout, "Found rid %d\n", rid);
403                 }
404 #ifdef PWS_DEBUG
405                 fprintf(stdout, "ON-REPLY: %s\n",
406                                 json_object_to_json_string_ext(result,
407                                 JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE));
408 #endif
409                 pws_data_source_reply_destroy(pws);
410         }
411
412         return rid;
413 }
414
415 size_t
416 pws_list_runners(struct pws_data_source *pws)
417 {
418         size_t items = 0;
419         if (pws_do_call(pws, "runners", "true") < 0)
420                 return items;
421
422         if (pws->reply_valid) {
423                 struct json_object *result = pws->reply;
424
425                 /* at least one is running */
426                 items = json_object_array_length(result);
427
428 #ifdef PWS_DEBUG
429                 fprintf(stdout, "found %ld items\n", items);
430                 fprintf(stdout, "ON-REPLY: %s\n",
431                                 json_object_to_json_string_ext(result,
432                                 JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE));
433 #endif
434                 pws_data_source_reply_destroy(pws);
435         }
436
437         return items;
438 }
439
440 size_t
441 pws_list_runnables(struct pws_data_source *pws)
442 {
443         size_t items = 0;
444         if (pws_do_call(pws, "runnables", "true") < 0)
445                 return items;
446
447         if (pws->reply_valid) {
448                 struct json_object *result = pws->reply;
449
450                 items = json_object_array_length(result);
451 #ifdef PWS_DEBUG
452                 fprintf(stdout, "found %ld items\n", items);
453                 fprintf(stdout, "ON-REPLY: %s\n",
454                                 json_object_to_json_string_ext(result,
455                                 JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE));
456 #endif
457                 pws_data_source_reply_destroy(pws);
458         }
459
460         return items;
461 }
462
463 /*
464  * need to free(json_string) once you're done with it
465  *
466  * call pws_data_source_reply_destroy(pws) once you're done with it.
467  */
468 size_t
469 pws_get_list_runnables(struct pws_data_source *pws,  struct json_object **json)
470 {
471         size_t items = 0;
472         if (pws_do_call(pws, "runnables", "true") < 0)
473                 return items;
474
475         fprintf(stdout, "pws_get_list_runnables()\n");
476
477         if (pws->reply_valid) {
478                 struct json_object *result = pws->reply;
479
480                 items = json_object_array_length(result);
481 #ifdef PWS_DEBUG
482                 fprintf(stdout, "found %ld items\n", items);
483                 fprintf(stdout, "ON-REPLY: %s\n",
484                                 json_object_to_json_string_ext(result,
485                                 JSON_C_TO_STRING_PRETTY | JSON_C_TO_STRING_NOSLASHESCAPE));
486 #endif
487                 if (items == 0) {
488                         fprintf(stdout, "pws_get_list_runnables() turn items %d, bails sooner\n", items);
489                         *json = NULL;
490                         return items;
491                 }
492
493                 fprintf(stdout, "pws_get_list_runnables() json reply is set\n");
494                 *json = result;
495         }
496
497         fprintf(stdout, "pws_get_list_runnables() turn items %d\n", items);
498         return items;
499 }