tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / sd / source / ui / remotecontrol / AvahiNetworkService.cxx
blob7708e6eb7d71e3fc34444be44439b695ea7f7402
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <config_dbus.h>
12 #include <iostream>
13 #include <limits>
14 #include <new>
15 #include <assert.h>
17 #include <avahi-client/client.h>
18 #include <avahi-client/publish.h>
20 #include <avahi-common/alternative.h>
21 #include <avahi-common/error.h>
22 #include <avahi-common/thread-watch.h>
23 #include <comphelper/random.hxx>
25 #if ENABLE_DBUS
26 #include <dbus/dbus.h>
27 #endif
29 #include <sal/log.hxx>
31 #include "AvahiNetworkService.hxx"
32 #include "ZeroconfService.hxx"
34 using namespace sd;
36 static AvahiClient *client = nullptr;
37 static AvahiThreadedPoll *threaded_poll = nullptr;
38 static AvahiEntryGroup *group = nullptr;
39 static AvahiNetworkService *avahiService = nullptr;
41 static bool create_services(AvahiClient *c);
43 static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state, AVAHI_GCC_UNUSED void *userdata) {
44 assert(g == group || group == nullptr);
45 group = g;
47 /* Called whenever the entry group state changes */
49 switch (state) {
50 case AVAHI_ENTRY_GROUP_ESTABLISHED :
51 /* The entry group has been established successfully */
52 SAL_INFO( "sdremote.wifi", "Service '" << avahiService->getName() << "' successfully established." );
53 break;
55 case AVAHI_ENTRY_GROUP_COLLISION : {
56 char *n;
58 /* A service name collision with a remote service
59 * happened. Let's pick a new name */
60 n = avahi_alternative_service_name(avahiService->getName().c_str());
61 avahiService->setName(n);
63 SAL_INFO( "sdremote.wifi", "Service name collision, renaming service to '" << avahiService->getName() << "'");
65 /* And recreate the services */
66 create_services(avahi_entry_group_get_client(g));
67 break;
70 case AVAHI_ENTRY_GROUP_FAILURE :
72 SAL_WARN("sdremote.wifi", "Entry group failure: " << avahi_strerror(avahi_client_errno(avahi_entry_group_get_client(g))));
74 /* Some kind of failure happened while we were registering our services */
75 avahi_threaded_poll_quit(threaded_poll);
76 break;
78 case AVAHI_ENTRY_GROUP_UNCOMMITED:
79 case AVAHI_ENTRY_GROUP_REGISTERING:
84 static bool create_services(AvahiClient *c) {
85 assert(c);
87 /* If this is the first time we're called, let's create a new
88 * entry group if necessary */
89 if(!client)
90 return false;
92 if (!group)
93 if (!(group = avahi_entry_group_new(c, entry_group_callback, nullptr))) {
94 SAL_WARN("sdremote.wifi", "avahi_entry_group_new() failed: " << avahi_strerror(avahi_client_errno(c)));
95 avahiService->clear();
96 return false;
99 /* If the group is empty (either because it was just created, or
100 * because it was reset previously, add our entries. */
102 if (avahi_entry_group_is_empty(group)) {
103 SAL_INFO("sdremote.wifi", "Adding service '" << avahiService->getName() << "'");
104 char r[128];
105 int nRandom = comphelper::rng::uniform_int_distribution(0, std::numeric_limits<int>::max());
106 snprintf(r, sizeof(r), "random=%i", nRandom);
107 int ret = avahi_entry_group_add_service(
108 group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, static_cast<AvahiPublishFlags>(0),
109 avahiService->getName().c_str(), kREG_TYPE, nullptr, nullptr, 1599, "local", r, nullptr
111 if (ret < 0) {
113 if (ret == AVAHI_ERR_COLLISION){
114 /* A service name collision with a local service happened. Let's
115 * pick a new name */
116 char *n = avahi_alternative_service_name(avahiService->getName().c_str());
117 avahiService->setName(n);
119 SAL_WARN("sdremote.wifi", "Service name collision, renaming service to '" << avahiService->getName() << "'");
121 avahi_entry_group_reset(group);
123 return create_services(c);
126 SAL_WARN("sdremote.wifi", "Failed to add _impressremote._tcp service: " << avahi_strerror(ret));
127 avahiService->clear();
128 return false;
131 /* Tell the server to register the service */
132 if ((ret = avahi_entry_group_commit(group)) < 0) {
133 SAL_WARN("sdremote.wifi", "Failed to commit entry group: " << avahi_strerror(ret));
134 avahiService->clear();
135 return false;
139 return true; //Services we're already created
142 static void client_callback(AvahiClient *c, AvahiClientState state, AVAHI_GCC_UNUSED void * userdata) {
143 assert(c);
145 /* Called whenever the client or server state changes */
147 switch (state) {
148 case AVAHI_CLIENT_S_RUNNING:
149 create_services(c);
150 break;
151 case AVAHI_CLIENT_FAILURE:
152 SAL_WARN("sdremote.wifi", "Client failure: " << avahi_strerror(avahi_client_errno(c)));
153 avahiService->clear();
154 break;
155 case AVAHI_CLIENT_S_COLLISION:
156 case AVAHI_CLIENT_S_REGISTERING:
157 if (group)
158 avahi_entry_group_reset(group);
159 break;
160 case AVAHI_CLIENT_CONNECTING:
165 void AvahiNetworkService::setup() {
166 #if ENABLE_DBUS
167 // Sure, without ENABLE_DBUS it probably makes no sense to try to use this Avahi stuff either,
168 // but this is just a stop-gap measure to get this to even compile for now with the probably
169 // pointless combination of configurable options --enable-avahi --enable-dbus --disable-gui.
171 // Avahi internally uses D-Bus, which requires the following in order to be
172 // thread-safe (and we potentially access D-Bus from different threads in
173 // different places of the code base):
174 if (!dbus_threads_init_default()) {
175 throw std::bad_alloc();
177 #endif
179 int error = 0;
180 avahiService = this;
181 if (!(threaded_poll = avahi_threaded_poll_new())) {
182 SAL_WARN("sdremote.wifi", "avahi_threaded_poll_new '" << avahiService->getName() << "' failed");
183 return;
186 if (!(client = avahi_client_new(avahi_threaded_poll_get(threaded_poll), static_cast<AvahiClientFlags>(0), client_callback, nullptr, &error))) {
187 SAL_WARN("sdremote.wifi", "avahi_client_new failed");
188 return;
191 if(!create_services(client))
192 return;
194 /* Finally, start the event loop thread */
195 if (avahi_threaded_poll_start(threaded_poll) < 0) {
196 SAL_WARN("sdremote.wifi", "avahi_threaded_poll_start failed");
197 return;
201 void AvahiNetworkService::clear() {
202 /* Call this when the app shuts down */
203 if(threaded_poll)
204 avahi_threaded_poll_stop(threaded_poll);
205 if(client)
206 avahi_client_free(client);
207 if(threaded_poll)
208 avahi_threaded_poll_free(threaded_poll);