|
4 | 4 | * SPDX-License-Identifier: LGPL-2.1-or-later |
5 | 5 | * |
6 | 6 | */ |
| 7 | +#include <cassert> |
7 | 8 | #include <charconv> |
8 | 9 | #include <cstdint> |
9 | 10 | #include <cstdio> |
|
16 | 17 | #include <string> |
17 | 18 | #include <utility> |
18 | 19 | #include <getopt.h> |
| 20 | +#include "fcitx-utils/awaiter.h" |
| 21 | +#include "fcitx-utils/coroutine.h" |
19 | 22 | #include "fcitx-utils/dbus/bus.h" |
| 23 | +#include "fcitx-utils/dbus/coroutine.h" |
20 | 24 | #include "fcitx-utils/dbus/message.h" |
21 | 25 | #include "fcitx-utils/dbus/servicewatcher.h" |
22 | 26 | #include "fcitx-utils/environ.h" |
@@ -83,84 +87,93 @@ class Launcher { |
83 | 87 |
|
84 | 88 | if (oldOwner.empty() && newOwner.empty()) { |
85 | 89 | // This is initial query, let's just start service. |
86 | | - auto message = bus_.createMethodCall("org.freedesktop.DBus", "/", |
87 | | - "org.freedesktop.DBus", |
88 | | - "StartServiceByName"); |
89 | | - message << "org.fcitx.Fcitx5"; |
90 | | - message << 0U; |
91 | | - message.send(); |
| 90 | + startServiceTask_ = |
| 91 | + std::make_unique<CoroutineTask<void>>(startService()); |
| 92 | + startServiceTask_->resume(); |
92 | 93 | return; |
93 | 94 | } |
94 | 95 |
|
95 | 96 | if (!newOwner.empty() && connectedName_.empty()) { |
96 | | - delayedConnection_ = loop_.addTimeEvent( |
97 | | - CLOCK_MONOTONIC, now(CLOCK_MONOTONIC) + 1000000, 0, |
98 | | - [this, newOwner](EventSource *, uint64_t) { |
99 | | - connectTo(newOwner); |
100 | | - return true; |
101 | | - }); |
| 97 | + connectedName_ = newOwner; |
| 98 | + connectionTask_ = |
| 99 | + std::make_unique<CoroutineTask<void>>(connectTo()); |
| 100 | + connectionTask_->resume(); |
102 | 101 | } |
103 | 102 | } |
104 | 103 |
|
105 | | - void connectTo(const std::string &newOwner) { |
106 | | - connectedName_ = newOwner; |
107 | | - if (fd_.isValid()) { |
108 | | - if (reopen_) { |
109 | | - auto message = |
110 | | - bus_.createMethodCall(newOwner.data(), "/controller", |
111 | | - "org.fcitx.Fcitx.Controller1", |
112 | | - "ReopenWaylandConnectionSocket"); |
113 | | - message << display_; |
114 | | - message << fd_; |
115 | | - reply_ = message.callAsync(0, [this](dbus::Message &message) { |
116 | | - reply(message); |
117 | | - return true; |
118 | | - }); |
119 | | - fd_.release(); |
120 | | - } else { |
121 | | - auto message = |
122 | | - bus_.createMethodCall(newOwner.data(), "/controller", |
123 | | - "org.fcitx.Fcitx.Controller1", |
124 | | - "OpenWaylandConnectionSocket"); |
125 | | - message << fd_; |
126 | | - reply_ = message.callAsync(0, [this](dbus::Message &message) { |
127 | | - reply(message); |
128 | | - return true; |
129 | | - }); |
130 | | - fd_.release(); |
| 104 | + Coroutine<void> startService() { |
| 105 | + try { |
| 106 | + auto message = bus_.createMethodCall("org.freedesktop.DBus", "/", |
| 107 | + "org.freedesktop.DBus", |
| 108 | + "StartServiceByName"); |
| 109 | + message << "org.fcitx.Fcitx5"; |
| 110 | + message << 0U; |
| 111 | + auto result = |
| 112 | + co_await dbus::AsyncReturn<uint32_t>(std::move(message)); |
| 113 | + if (result != 1 && result != 2) { |
| 114 | + throw std::runtime_error( |
| 115 | + "Unexpected response from StartServiceByName: " + |
| 116 | + std::to_string(result)); |
131 | 117 | } |
132 | | - } else { |
133 | | - auto message = bus_.createMethodCall(newOwner.data(), "/controller", |
134 | | - "org.fcitx.Fcitx.Controller1", |
135 | | - "OpenWaylandConnection"); |
136 | | - message << display_; |
137 | | - reply_ = message.callAsync(0, [this](dbus::Message &message) { |
138 | | - reply(message); |
139 | | - return true; |
140 | | - }); |
| 118 | + } catch (const std::exception &e) { |
| 119 | + done_ = true; |
| 120 | + error_ = true; |
| 121 | + FCITX_ERROR() << "Failed to start Fcitx service: " << e.what(); |
| 122 | + loop_.exit(); |
141 | 123 | } |
| 124 | + assert(startServiceTask_); |
| 125 | + std::move(*startServiceTask_).detach_handle(); |
142 | 126 | } |
143 | 127 |
|
144 | | - void reply(dbus::Message &message) { |
145 | | - if (message.isError()) { |
| 128 | + Coroutine<void> connectTo() { |
| 129 | + try { |
| 130 | + co_await TimeAwaiter::after(loop_, 1000000); |
| 131 | + if (fd_.isValid()) { |
| 132 | + if (reopen_) { |
| 133 | + auto message = bus_.createMethodCall( |
| 134 | + connectedName_.data(), "/controller", |
| 135 | + "org.fcitx.Fcitx.Controller1", |
| 136 | + "ReopenWaylandConnectionSocket"); |
| 137 | + message << display_; |
| 138 | + message << fd_; |
| 139 | + fd_.release(); |
| 140 | + co_await dbus::AsyncReturn<>(std::move(message)); |
| 141 | + } else { |
| 142 | + auto message = bus_.createMethodCall( |
| 143 | + connectedName_.data(), "/controller", |
| 144 | + "org.fcitx.Fcitx.Controller1", |
| 145 | + "OpenWaylandConnectionSocket"); |
| 146 | + message << fd_; |
| 147 | + fd_.release(); |
| 148 | + co_await dbus::AsyncReturn<>(std::move(message)); |
| 149 | + } |
| 150 | + } else { |
| 151 | + auto message = bus_.createMethodCall( |
| 152 | + connectedName_.data(), "/controller", |
| 153 | + "org.fcitx.Fcitx.Controller1", "OpenWaylandConnection"); |
| 154 | + message << display_; |
| 155 | + co_await dbus::AsyncReturn<>(std::move(message)); |
| 156 | + } |
| 157 | + } catch (const std::exception &e) { |
146 | 158 | done_ = true; |
147 | 159 | error_ = true; |
148 | | - FCITX_ERROR() << "DBus call error: " << message.errorName() |
149 | | - << message.errorMessage(); |
| 160 | + FCITX_ERROR() << "Failed to open Wayland connection: " << e.what(); |
150 | 161 | loop_.exit(); |
151 | 162 | } |
| 163 | + assert(connectionTask_); |
| 164 | + std::move(*connectionTask_).detach_handle(); |
152 | 165 | } |
153 | 166 |
|
154 | 167 | dbus::Bus bus_{dbus::BusType::Session}; |
155 | 168 | std::unique_ptr<dbus::ServiceWatcher> watcher_; |
156 | 169 | EventLoop loop_; |
157 | 170 | std::unique_ptr<dbus::ServiceWatcherEntry> slot_; |
158 | | - std::unique_ptr<dbus::Slot> reply_; |
| 171 | + std::unique_ptr<CoroutineTask<void>> startServiceTask_; |
| 172 | + std::unique_ptr<CoroutineTask<void>> connectionTask_; |
159 | 173 | std::string connectedName_; |
160 | 174 | UnixFD fd_; |
161 | 175 | std::string display_; |
162 | 176 | bool done_ = false; |
163 | | - std::unique_ptr<EventSource> delayedConnection_; |
164 | 177 | bool error_ = false; |
165 | 178 | bool reopen_ = false; |
166 | 179 | }; |
|
0 commit comments