subscribe error event for app
[apps/agl-service-windowmanager.git] / src / main.cpp
1 /*
2  * Copyright (c) 2017 TOYOTA MOTOR CORPORATION
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 <unistd.h>
18 #include <algorithm>
19 #include <mutex>
20 #include <json.h>
21 #include <json.hpp>
22 #include "app.hpp"
23 #include "result.hpp"
24 #include "json_helper.hpp"
25 #include "util.hpp"
26 #include "wayland_ivi_wm.hpp"
27
28 extern "C"
29 {
30 #include <afb/afb-binding.h>
31 #include <systemd/sd-event.h>
32 }
33
34 typedef struct wmClientCtxt
35 {
36     std::string name;
37     std::string role;
38     wmClientCtxt(const char *appName, const char* appRole)
39     {
40         name = appName;
41         role = role;
42     }
43 } wmClientCtxt;
44
45 struct afb_instance
46 {
47     std::unique_ptr<wl::display> display;
48     wm::App app;
49
50     afb_instance() : display{new wl::display}, app{this->display.get()} {}
51
52     int init();
53 };
54
55 struct afb_instance *g_afb_instance;
56 std::mutex binding_m;
57
58 int afb_instance::init()
59 {
60     return this->app.init();
61 }
62
63 int display_event_callback(sd_event_source *evs, int /*fd*/, uint32_t events,
64                            void * /*data*/)
65 {
66     ST();
67
68     if ((events & EPOLLHUP) != 0)
69     {
70         HMI_ERROR("wm", "The compositor hung up, dying now.");
71         delete g_afb_instance;
72         g_afb_instance = nullptr;
73         goto error;
74     }
75
76     if ((events & EPOLLIN) != 0u)
77     {
78         {
79             STN(display_read_events);
80             g_afb_instance->app.display->read_events();
81             g_afb_instance->app.set_pending_events();
82         }
83         {
84             // We want do dispatch pending wayland events from within
85             // the API context
86             STN(winman_ping_api_call);
87             afb_service_call("windowmanager", "ping", json_object_new_object(),
88                              [](void *c, int st, json_object *j) {
89                                  STN(winman_ping_api_call_return);
90                              },
91                              nullptr);
92         }
93     }
94
95     return 0;
96
97 error:
98     sd_event_source_unref(evs);
99     if (getenv("WINMAN_EXIT_ON_HANGUP") != nullptr)
100     {
101         exit(1);
102     }
103     return -1;
104 }
105
106 int _binding_init()
107 {
108     HMI_NOTICE("wm", "WinMan ver. %s", WINMAN_VERSION_STRING);
109
110     if (g_afb_instance != nullptr)
111     {
112         HMI_ERROR("wm", "Wayland context already initialized?");
113         return 0;
114     }
115
116     if (getenv("XDG_RUNTIME_DIR") == nullptr)
117     {
118         HMI_ERROR("wm", "Environment variable XDG_RUNTIME_DIR not set");
119         goto error;
120     }
121
122     {
123         // wait until wayland compositor starts up.
124         int cnt = 0;
125         g_afb_instance = new afb_instance;
126         while (!g_afb_instance->display->ok())
127         {
128             cnt++;
129             if (20 <= cnt)
130             {
131                 HMI_ERROR("wm", "Could not connect to compositor");
132                 goto error;
133             }
134             HMI_ERROR("wm", "Wait to start weston ...");
135             sleep(1);
136             delete g_afb_instance;
137             g_afb_instance = new afb_instance;
138         }
139     }
140
141     if (g_afb_instance->init() == -1)
142     {
143         HMI_ERROR("wm", "Could not connect to compositor");
144         goto error;
145     }
146
147     {
148         int ret = sd_event_add_io(afb_daemon_get_event_loop(), nullptr,
149                                   g_afb_instance->display->get_fd(), EPOLLIN,
150                                   display_event_callback, g_afb_instance);
151         if (ret < 0)
152         {
153             HMI_ERROR("wm", "Could not initialize afb_instance event handler: %d", -ret);
154             goto error;
155         }
156     }
157
158     atexit([] { delete g_afb_instance; });
159
160     return 0;
161
162 error:
163     delete g_afb_instance;
164     g_afb_instance = nullptr;
165     return -1;
166 }
167
168 int binding_init() noexcept
169 {
170     try
171     {
172         return _binding_init();
173     }
174     catch (std::exception &e)
175     {
176         HMI_ERROR("wm", "Uncaught exception in binding_init(): %s", e.what());
177     }
178     return -1;
179 }
180
181 static bool checkFirstReq(afb_req req)
182 {
183     wmClientCtxt *ctxt = (wmClientCtxt *)afb_req_context_get(req);
184     return (ctxt) ? false : true;
185 }
186
187 static void cbRemoveClientCtxt(void *data)
188 {
189     wmClientCtxt *ctxt = (wmClientCtxt *)data;
190     if (ctxt == nullptr)
191     {
192         return;
193     }
194     HMI_DEBUG("wm", "remove app %s", ctxt->name.c_str());
195     // Lookup surfaceID and remove it because App is dead.
196     auto pSid = g_afb_instance->app.id_alloc.lookup(ctxt->role.c_str());
197     if (pSid)
198     {
199         auto sid = *pSid;
200         g_afb_instance->app.id_alloc.remove_id(sid);
201         g_afb_instance->app.layers.remove_surface(sid);
202         g_afb_instance->app.controller->sprops.erase(sid);
203         g_afb_instance->app.controller->surfaces.erase(sid);
204         HMI_DEBUG("wm", "delete surfaceID %d", sid);
205     }
206     g_afb_instance->app.removeClient(ctxt->name);
207     delete ctxt;
208 }
209
210 void windowmanager_requestsurface(afb_req req) noexcept
211 {
212     std::lock_guard<std::mutex> guard(binding_m);
213 #ifdef ST
214     ST();
215 #endif
216     if (g_afb_instance == nullptr)
217     {
218         afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
219         return;
220     }
221
222     try
223     {
224         const char *a_drawing_name = afb_req_value(req, "drawing_name");
225         if (!a_drawing_name)
226         {
227             afb_req_fail(req, "failed", "Need char const* argument drawing_name");
228             return;
229         }
230
231         /* Create Security Context */
232         bool isFirstReq = checkFirstReq(req);
233         if (!isFirstReq)
234         {
235             wmClientCtxt *ctxt = (wmClientCtxt *)afb_req_context_get(req);
236             HMI_DEBUG("wm", "You're %s.", ctxt->name.c_str());
237             if (ctxt->name != std::string(a_drawing_name))
238             {
239                 afb_req_fail_f(req, "failed", "Dont request with other name: %s for now", a_drawing_name);
240                 HMI_DEBUG("wm", "Don't request with other name: %s for now", a_drawing_name);
241                 return;
242             }
243         }
244
245         auto ret = g_afb_instance->app.api_request_surface(
246             afb_req_get_application_id(req), a_drawing_name);
247
248         if (isFirstReq)
249         {
250             wmClientCtxt *ctxt = new wmClientCtxt(afb_req_get_application_id(req), a_drawing_name);
251             HMI_DEBUG("wm", "create session for %s", ctxt->name.c_str());
252             afb_req_session_set_LOA(req, 1);
253             afb_req_context_set(req, ctxt, cbRemoveClientCtxt);
254         }
255         else
256         {
257             HMI_DEBUG("wm", "session already created for %s", a_drawing_name);
258         }
259
260         if (ret.is_err())
261         {
262             afb_req_fail(req, "failed", ret.unwrap_err());
263             return;
264         }
265
266         afb_req_success(req, json_object_new_int(ret.unwrap()), "success");
267     }
268     catch (std::exception &e)
269     {
270         afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurface: %s", e.what());
271         return;
272     }
273 }
274
275 void windowmanager_requestsurfacexdg(afb_req req) noexcept
276 {
277     std::lock_guard<std::mutex> guard(binding_m);
278 #ifdef ST
279     ST();
280 #endif
281     if (g_afb_instance == nullptr)
282     {
283         afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
284         return;
285     }
286
287     try
288     {
289         json_object *jreq = afb_req_json(req);
290
291         json_object *j_drawing_name = nullptr;
292         if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
293         {
294             afb_req_fail(req, "failed", "Need char const* argument drawing_name");
295             return;
296         }
297         char const *a_drawing_name = json_object_get_string(j_drawing_name);
298
299         json_object *j_ivi_id = nullptr;
300         if (!json_object_object_get_ex(jreq, "ivi_id", &j_ivi_id))
301         {
302             afb_req_fail(req, "failed", "Need char const* argument ivi_id");
303             return;
304         }
305         char const *a_ivi_id = json_object_get_string(j_ivi_id);
306
307         auto ret = g_afb_instance->app.api_request_surface(
308             afb_req_get_application_id(req), a_drawing_name, a_ivi_id);
309         if (ret != nullptr)
310         {
311             afb_req_fail(req, "failed", ret);
312             return;
313         }
314
315         afb_req_success(req, NULL, "success");
316     }
317     catch (std::exception &e)
318     {
319         afb_req_fail_f(req, "failed", "Uncaught exception while calling requestsurfacexdg: %s", e.what());
320         return;
321     }
322 }
323
324 void windowmanager_activatesurface(afb_req req) noexcept
325 {
326     std::lock_guard<std::mutex> guard(binding_m);
327 #ifdef ST
328     ST();
329 #endif
330     if (g_afb_instance == nullptr)
331     {
332         afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
333         return;
334     }
335
336     try
337     {
338         const char *a_drawing_name = afb_req_value(req, "drawing_name");
339         if (!a_drawing_name)
340         {
341             afb_req_fail(req, "failed", "Need char const* argument drawing_name");
342             return;
343         }
344
345         const char *a_drawing_area = afb_req_value(req, "drawing_area");
346         if (!a_drawing_area)
347         {
348             afb_req_fail(req, "failed", "Need char const* argument drawing_area");
349             return;
350         }
351
352         g_afb_instance->app.api_activate_surface(
353             afb_req_get_application_id(req),
354             a_drawing_name, a_drawing_area,
355             [&req](const char *errmsg) {
356                 if (errmsg != nullptr)
357                 {
358                     HMI_ERROR("wm", errmsg);
359                     afb_req_fail(req, "failed", errmsg);
360                     return;
361                 }
362                 afb_req_success(req, NULL, "success");
363             });
364     }
365     catch (std::exception &e)
366     {
367         HMI_WARNING("wm", "failed", "Uncaught exception while calling activatesurface: %s", e.what());
368         return;
369     }
370 }
371
372 void windowmanager_deactivatesurface(afb_req req) noexcept
373 {
374     std::lock_guard<std::mutex> guard(binding_m);
375 #ifdef ST
376     ST();
377 #endif
378     if (g_afb_instance == nullptr)
379     {
380         afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
381         return;
382     }
383
384     try
385     {
386         const char *a_drawing_name = afb_req_value(req, "drawing_name");
387         if (!a_drawing_name)
388         {
389             afb_req_fail(req, "failed", "Need char const* argument drawing_name");
390             return;
391         }
392
393         g_afb_instance->app.api_deactivate_surface(
394             afb_req_get_application_id(req), a_drawing_name,
395             [&req](const char *errmsg) {
396                 if (errmsg != nullptr)
397                 {
398                     HMI_ERROR("wm", errmsg);
399                     afb_req_fail(req, "failed", errmsg);
400                     return;
401                 }
402                 afb_req_success(req, NULL, "success");
403             });
404     }
405     catch (std::exception &e)
406     {
407         HMI_WARNING("wm", "Uncaught exception while calling deactivatesurface: %s", e.what());
408         return;
409     }
410 }
411
412 void windowmanager_enddraw(afb_req req) noexcept
413 {
414     std::lock_guard<std::mutex> guard(binding_m);
415 #ifdef ST
416     ST();
417 #endif
418     if (g_afb_instance == nullptr)
419     {
420         afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
421         return;
422     }
423
424     try
425     {
426         const char *a_drawing_name = afb_req_value(req, "drawing_name");
427         if (!a_drawing_name)
428         {
429             afb_req_fail(req, "failed", "Need char const* argument drawing_name");
430             return;
431         }
432         afb_req_success(req, NULL, "success");
433
434         g_afb_instance->app.api_enddraw(
435             afb_req_get_application_id(req), a_drawing_name);
436     }
437     catch (std::exception &e)
438     {
439         HMI_WARNING("wm", "failed", "Uncaught exception while calling enddraw: %s", e.what());
440         return;
441     }
442 }
443
444 void windowmanager_getdisplayinfo_thunk(afb_req req) noexcept
445 {
446     std::lock_guard<std::mutex> guard(binding_m);
447 #ifdef ST
448     ST();
449 #endif
450     if (g_afb_instance == nullptr)
451     {
452         afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
453         return;
454     }
455
456     try
457     {
458         auto ret = g_afb_instance->app.api_get_display_info();
459         if (ret.is_err())
460         {
461             afb_req_fail(req, "failed", ret.unwrap_err());
462             return;
463         }
464
465         afb_req_success(req, ret.unwrap(), "success");
466     }
467     catch (std::exception &e)
468     {
469         afb_req_fail_f(req, "failed", "Uncaught exception while calling getdisplayinfo: %s", e.what());
470         return;
471     }
472 }
473
474 void windowmanager_getareainfo_thunk(afb_req req) noexcept
475 {
476     std::lock_guard<std::mutex> guard(binding_m);
477 #ifdef ST
478     ST();
479 #endif
480     if (g_afb_instance == nullptr)
481     {
482         afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
483         return;
484     }
485
486     try
487     {
488         json_object *jreq = afb_req_json(req);
489
490         json_object *j_drawing_name = nullptr;
491         if (!json_object_object_get_ex(jreq, "drawing_name", &j_drawing_name))
492         {
493             afb_req_fail(req, "failed", "Need char const* argument drawing_name");
494             return;
495         }
496         char const *a_drawing_name = json_object_get_string(j_drawing_name);
497
498         auto ret = g_afb_instance->app.api_get_area_info(a_drawing_name);
499         if (ret.is_err())
500         {
501             afb_req_fail(req, "failed", ret.unwrap_err());
502             return;
503         }
504
505         afb_req_success(req, ret.unwrap(), "success");
506     }
507     catch (std::exception &e)
508     {
509         afb_req_fail_f(req, "failed", "Uncaught exception while calling getareainfo: %s", e.what());
510         return;
511     }
512 }
513
514 void windowmanager_wm_subscribe(afb_req req) noexcept
515 {
516     std::lock_guard<std::mutex> guard(binding_m);
517 #ifdef ST
518     ST();
519 #endif
520     if (g_afb_instance == nullptr)
521     {
522         afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
523         return;
524     }
525
526     try
527     {
528         json_object *jreq = afb_req_json(req);
529         json_object *j = nullptr;
530         if (!json_object_object_get_ex(jreq, "event", &j))
531         {
532             afb_req_fail(req, "failed", "Need char const* argument event");
533             return;
534         }
535         int event_type = json_object_get_int(j);
536         const char *event_name = g_afb_instance->app.kListEventName[event_type];
537         struct afb_event event = g_afb_instance->app.map_afb_event[event_name];
538         int ret = afb_req_subscribe(req, event);
539         if (ret)
540         {
541             afb_req_fail(req, "failed", "Error: afb_req_subscribe()");
542             return;
543         }
544         afb_req_success(req, NULL, "success");
545
546         g_afb_instance->app.subscribeEventForApp(afb_req_get_application_id(req), req, event_name);
547     }
548     catch (std::exception &e)
549     {
550         afb_req_fail_f(req, "failed", "Uncaught exception while calling wm_subscribe: %s", e.what());
551         return;
552     }
553 }
554
555 void windowmanager_list_drawing_names(afb_req req) noexcept
556 {
557     std::lock_guard<std::mutex> guard(binding_m);
558 #ifdef ST
559     ST();
560 #endif
561     if (g_afb_instance == nullptr)
562     {
563         afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
564         return;
565     }
566
567     try
568     {
569
570         nlohmann::json j = g_afb_instance->app.id_alloc.name2id;
571         auto ret = wm::Ok(json_tokener_parse(j.dump().c_str()));
572         if (ret.is_err())
573         {
574             afb_req_fail(req, "failed", ret.unwrap_err());
575             return;
576         }
577
578         afb_req_success(req, ret.unwrap(), "success");
579     }
580     catch (std::exception &e)
581     {
582         afb_req_fail_f(req, "failed", "Uncaught exception while calling list_drawing_names: %s", e.what());
583         return;
584     }
585 }
586
587 void windowmanager_ping(afb_req req) noexcept
588 {
589     std::lock_guard<std::mutex> guard(binding_m);
590 #ifdef ST
591     ST();
592 #endif
593     if (g_afb_instance == nullptr)
594     {
595         afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
596         return;
597     }
598
599     try
600     {
601
602         g_afb_instance->app.api_ping();
603
604         afb_req_success(req, NULL, "success");
605     }
606     catch (std::exception &e)
607     {
608         afb_req_fail_f(req, "failed", "Uncaught exception while calling ping: %s", e.what());
609         return;
610     }
611 }
612
613 void windowmanager_debug_status(afb_req req) noexcept
614 {
615     std::lock_guard<std::mutex> guard(binding_m);
616 #ifdef ST
617     ST();
618 #endif
619     if (g_afb_instance == nullptr)
620     {
621         afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
622         return;
623     }
624
625     try
626     {
627
628         json_object *jr = json_object_new_object();
629         json_object_object_add(jr, "surfaces",
630                                to_json(g_afb_instance->app.controller->sprops));
631         json_object_object_add(jr, "layers", to_json(g_afb_instance->app.controller->lprops));
632
633         afb_req_success(req, jr, "success");
634     }
635     catch (std::exception &e)
636     {
637         afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_status: %s", e.what());
638         return;
639     }
640 }
641
642 void windowmanager_debug_layers(afb_req req) noexcept
643 {
644     std::lock_guard<std::mutex> guard(binding_m);
645 #ifdef ST
646     ST();
647 #endif
648     if (g_afb_instance == nullptr)
649     {
650         afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
651         return;
652     }
653
654     try
655     {
656         auto ret = wm::Ok(json_tokener_parse(g_afb_instance->app.layers.to_json().dump().c_str()));
657
658         afb_req_success(req, ret, "success");
659     }
660     catch (std::exception &e)
661     {
662         afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_layers: %s", e.what());
663         return;
664     }
665 }
666
667 void windowmanager_debug_surfaces(afb_req req) noexcept
668 {
669     std::lock_guard<std::mutex> guard(binding_m);
670 #ifdef ST
671     ST();
672 #endif
673     if (g_afb_instance == nullptr)
674     {
675         afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
676         return;
677     }
678
679     try
680     {
681
682         auto ret = wm::Ok(to_json(g_afb_instance->app.controller->sprops));
683         if (ret.is_err())
684         {
685             afb_req_fail(req, "failed", ret.unwrap_err());
686             return;
687         }
688
689         afb_req_success(req, ret.unwrap(), "success");
690     }
691     catch (std::exception &e)
692     {
693         afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_surfaces: %s", e.what());
694         return;
695     }
696 }
697
698 void windowmanager_debug_terminate(afb_req req) noexcept
699 {
700     std::lock_guard<std::mutex> guard(binding_m);
701 #ifdef ST
702     ST();
703 #endif
704     if (g_afb_instance == nullptr)
705     {
706         afb_req_fail(req, "failed", "Binding not initialized, did the compositor die?");
707         return;
708     }
709
710     try
711     {
712
713         if (getenv("WINMAN_DEBUG_TERMINATE") != nullptr)
714         {
715             raise(SIGKILL); // afb-daemon kills it's pgroup using TERM, which
716                             // doesn't play well with perf
717         }
718
719         afb_req_success(req, NULL, "success");
720     }
721     catch (std::exception &e)
722     {
723         afb_req_fail_f(req, "failed", "Uncaught exception while calling debug_terminate: %s", e.what());
724         return;
725     }
726 }
727
728 const struct afb_verb_v2 windowmanager_verbs[] = {
729     {"requestsurface", windowmanager_requestsurface, nullptr, nullptr, AFB_SESSION_NONE},
730     {"requestsurfacexdg", windowmanager_requestsurfacexdg, nullptr, nullptr, AFB_SESSION_NONE},
731     {"activatesurface", windowmanager_activatesurface, nullptr, nullptr, AFB_SESSION_LOA_1},
732     {"deactivatesurface", windowmanager_deactivatesurface, nullptr, nullptr, AFB_SESSION_LOA_1},
733     {"enddraw", windowmanager_enddraw, nullptr, nullptr, AFB_SESSION_LOA_1},
734     {"getdisplayinfo", windowmanager_getdisplayinfo_thunk, nullptr, nullptr, AFB_SESSION_NONE},
735     {"getareainfo", windowmanager_getareainfo_thunk, nullptr, nullptr, AFB_SESSION_LOA_1},
736     {"wm_subscribe", windowmanager_wm_subscribe, nullptr, nullptr, AFB_SESSION_NONE},
737     {"list_drawing_names", windowmanager_list_drawing_names, nullptr, nullptr, AFB_SESSION_NONE},
738     {"ping", windowmanager_ping, nullptr, nullptr, AFB_SESSION_NONE},
739     {"debug_status", windowmanager_debug_status, nullptr, nullptr, AFB_SESSION_NONE},
740     {"debug_layers", windowmanager_debug_layers, nullptr, nullptr, AFB_SESSION_NONE},
741     {"debug_surfaces", windowmanager_debug_surfaces, nullptr, nullptr, AFB_SESSION_NONE},
742     {"debug_terminate", windowmanager_debug_terminate, nullptr, nullptr, AFB_SESSION_NONE},
743     {}};
744
745 extern "C" const struct afb_binding_v2 afbBindingV2 = {
746     "windowmanager", nullptr, nullptr, windowmanager_verbs, nullptr, binding_init, nullptr, 0};