OpenCPN Partial API docs
dbus_server.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2023 Alec Leamas *
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 2 of the License, or *
7  * (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License *
15  * along with this program; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18  ***************************************************************************
19  */
20 
21 #ifndef DBUS_HANDLER_H__
22 #define DBUS_HANDLER_H__
23 
24 #include <functional>
25 #include <thread>
26 
27 #include <gio/gio.h>
28 
29 #include "model/instance_check.h"
30 #include "model/local_api.h"
31 #include "observable_evtvar.h"
32 
40 static const char* const kDbusName = "org.opencpn.OpenCPN";
41 
43 static const char* const kDbusObject = "/org/opencpn/OpenCPN";
44 
46 static const char* const kDbusInterface = "opencpn.desktop";
47 
49 static const gchar introspection_xml[] = R"""(
50 <node>
51  <interface name='opencpn.desktop'>
52  <annotation name='org.gtk.GDBus.Annotation' value='OnInterface'/>
53  <annotation name='org.gtk.GDBus.Annotation' value='AlsoOnInterface'/>
54  <method name='Ping'>
55  <annotation name='org.gtk.GDBus.Annotation' value='OnMethod'/>
56  <arg type='u' name='level' direction='out'/>
57  <arg type='b' name='stat' direction='out'/>
58  </method>
59  <method name='Raise'>
60  <annotation name='org.gtk.GDBus.Annotation' value='OnMethod'/>
61  <!-- In the GUI case, raise application to top. -->
62  </method>
63  <method name='Open'>
64  <annotation name='org.gtk.GDBus.Annotation' value='OnMethod'/>
65  <arg type='s' name='level' direction='in'/>
66  <arg type='b' name='stat' direction='out'/>
67  </method>
68  <method name='GetRestEndpoint'>
69  <annotation name='org.gtk.GDBus.Annotation' value='OnMethod'/>
70  <arg type='s' name='endpoint' direction='out'/>
71  </method>
72  <method name='Quit'>
73  <annotation name='org.gtk.GDBus.Annotation' value='OnMethod'/>
74  </method>
75  </interface>
76 </node>
77 )""";
78 
80 static void HandleMethodCall(GDBusConnection* connection, const gchar* sender,
81  const gchar* object_path,
82  const gchar* interface_name,
83  const gchar* method_name, GVariant* parameters,
84  GDBusMethodInvocation* invocation,
85  gpointer user_data);
86 
94 class DbusServer : public LocalServerApi, public InstanceCheck {
95 public:
96 
97  static DbusServer& GetInstance();
98 
100  static void Disconnect();
101 
102  DbusServer(DbusServer&) = delete;
103  void operator=(const DbusServer&) = delete;
104 
105  virtual ~DbusServer();
106 
120  void WaitUntilValid() override;
121 
123  GDBusNodeInfo* introspection_data;
124 
129  bool IsMainInstance() { return m_is_main_instance; }
130 
131  void SetMainInstance(bool value) { m_is_main_instance = value; }
132 
133  DbusServer* UnitTestInstance() { return new DbusServer(); }
134 
135 protected:
136  DbusServer();
137 
138 private:
139  static DbusServer* s_instance;
140  bool m_is_main_instance;
141  unsigned m_owner_id;
142 };
143 
144 #endif // DBUS_HANDLER_H__
Basic interface to low-level DBus library.
Definition: dbus_server.h:94
GDBusNodeInfo * introspection_data
Callback context.
Definition: dbus_server.h:123
static void Disconnect()
Clear current singleton instance and disconnect from session bus.
void WaitUntilValid() override
Wait until ready.
bool IsMainInstance()
Check if this instance is the only one.
Definition: dbus_server.h:129
Common interface for all instance checkers.
Base interface for local server command handling.
Definition: local_api.h:62
The local API has a server side handling commands and a client part issuing commands.