Force set/unset keyboard focus
[staging/xdg-launcher.git] / src / runxdg.hpp
1 /*
2  * Copyright (c) 2017 Panasonic Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all
12  * copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 #ifndef RUNXDG_HPP
23 #define RUNXDG_HPP
24
25 #include <string>
26 #include <vector>
27 #include <map>
28 #include <algorithm>
29
30 #include <gio/gio.h>
31
32 #include <ilm/ilm_control.h>
33 #include <ilm/ilm_input.h>
34
35 #include <libwindowmanager.h>
36 #include <libhomescreen.hpp>
37
38 #define AGL_FATAL(fmt, ...) fatal("ERROR: " fmt "\n", ##__VA_ARGS__)
39 #define AGL_WARN(fmt, ...) warn("WARNING: " fmt "\n", ##__VA_ARGS__)
40 #define AGL_DEBUG(fmt, ...) debug("DEBUG: " fmt "\n", ##__VA_ARGS__)
41 #define AGL_TRACE(file,line) debug("%s:%d\n", file,line);
42
43 void fatal (const char* format, ...);
44 void warn (const char* format, ...);
45 void debug (const char* format, ...);
46
47 class ILMControl
48 {
49   public:
50     ILMControl(notificationFunc callback, void *user_data) {
51         ilm_init();
52         ilm_registerNotification(callback, user_data);
53     }
54
55     ~ILMControl(void) {
56         ilm_unregisterNotification();
57         ilm_destroy();
58         AGL_DEBUG("ilm_destory().\n");
59     }
60 };
61
62 class Launcher
63 {
64   public:
65     virtual void register_surfpid(pid_t surf_pid) = 0;
66     virtual void unregister_surfpid(pid_t surf_pid) = 0;
67     virtual pid_t find_surfpid_by_rid(pid_t app_pid) = 0;
68
69     virtual int launch(std::string& name) = 0;
70     virtual void loop(volatile sig_atomic_t& e_flag) = 0;
71
72     int m_rid = 0;
73 };
74
75 class POSIXLauncher : public Launcher
76 {
77   private:
78     std::vector<pid_t> m_pid_v;
79
80   public:
81     std::vector<std::string> m_args_v;
82
83     void register_surfpid(pid_t surf_pid);
84     void unregister_surfpid(pid_t surf_pid);
85     pid_t find_surfpid_by_rid(pid_t rid);
86
87     int launch(std::string& name);
88     void loop(volatile sig_atomic_t& e_flag);
89 };
90
91 class AFMLauncher : public Launcher
92 {
93   private:
94     std::map<int, int> m_pgids;  // pair of <afm:rid, ivi:pid>
95
96   public:
97     void register_surfpid(pid_t surf_pid);
98     void unregister_surfpid(pid_t surf_pid);
99     pid_t find_surfpid_by_rid(pid_t app_pid);
100 };
101
102 class AFMDBusLauncher : public AFMLauncher
103 {
104   public:
105     int launch(std::string& name);
106     void loop(volatile sig_atomic_t& e_flag) {
107       while (!(e_flag)) { sleep(60*60*24); } }
108
109   private:
110     int get_dbus_message_bus(GBusType bus_type, GDBusConnection* &conn);
111
112     const char* DBUS_SERVICE   = "org.AGL.afm.user";
113     const char* DBUS_PATH      = "/org/AGL/afm/user";
114     const char* DBUS_INTERFACE = "org.AGL.afm.user";
115 };
116
117 class AFMWebSocketLauncher : public AFMLauncher
118 {
119   // not implemented yet
120   public:
121     int launch(std::string& name) { return 0; }
122     void loop(volatile sig_atomic_t& e_flag) {
123       while (!(e_flag)) { sleep(60*60*24); } }
124 };
125
126 class RunXDG
127 {
128   public:
129     RunXDG(int port, const char* token, const char* id);
130
131     void start(void);
132     void notify_ivi_control_cb(ilmObjectType object, t_ilm_uint id,
133                                t_ilm_bool created);
134     static void notify_ivi_control_cb_static (ilmObjectType object,
135                                               t_ilm_uint id,
136                                               t_ilm_bool created,
137                                               void *user_data);
138   private:
139     std::string m_role;
140     std::string m_path;
141
142     std::string m_id;
143
144     int m_port;
145     std::string m_token;
146
147     Launcher *m_launcher;
148
149     LibWindowmanager *m_wm;
150     LibHomeScreen *m_hs;
151     ILMControl *m_ic;
152
153     t_ilm_surface m_ivi_id;
154
155     std::map<int, int> m_surfaces;  // pair of <afm:rid, ivi:id>
156
157     bool m_pending_create = false;
158
159     int init_wm(void);
160     int init_hs(void);
161
162     int parse_config(const char *file);
163
164     void setup_surface(void);
165 };
166
167 #endif  // RUNXDG_HPP