1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
|
/*
* SPDX-FileCopyrightText: 2017~2017 CSSlayer <wengxt@gmail.com>
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*/
#include "fcitxgwatcher.h"
#define FCITX_MAIN_SERVICE_NAME "org.fcitx.Fcitx5"
#define FCITX_PORTAL_SERVICE_NAME "org.freedesktop.portal.Fcitx"
typedef struct _FcitxGWatcherPrivate FcitxGWatcherPrivate;
struct _FcitxGWatcher {
GObject parent_instance;
/* instance member */
FcitxGWatcherPrivate *priv;
};
/**
* FcitxGWatcher:
*
* A FcitxGWatcher allow to create a input context via DBus
*/
struct _FcitxGWatcherPrivate {
gboolean watched;
guint watch_id;
guint portal_watch_id;
gchar *main_owner, *portal_owner;
gboolean watch_portal;
gboolean available;
GCancellable *cancellable;
GDBusConnection *connection;
};
G_DEFINE_TYPE_WITH_PRIVATE(FcitxGWatcher, fcitx_g_watcher, G_TYPE_OBJECT);
enum { AVAILABLITY_CHANGED_SIGNAL, LAST_SIGNAL };
static guint signals[LAST_SIGNAL] = {0};
static void _fcitx_g_watcher_clean_up(FcitxGWatcher *self);
static void _fcitx_g_watcher_get_bus_finished(GObject *source_object,
GAsyncResult *res,
gpointer user_data);
static void _fcitx_g_watcher_update_availability(FcitxGWatcher *self);
static void fcitx_g_watcher_finalize(GObject *object);
static void fcitx_g_watcher_dispose(GObject *object);
static void fcitx_g_watcher_class_init(FcitxGWatcherClass *klass) {
GObjectClass *gobject_class;
gobject_class = G_OBJECT_CLASS(klass);
gobject_class->dispose = fcitx_g_watcher_dispose;
gobject_class->finalize = fcitx_g_watcher_finalize;
/* install signals */
/**
* FcitxGWatcher::availability-changed:
* @watcher: A FcitxGWatcher
* @available: whether fcitx service is available.
*
* Emit when connected to fcitx and created ic
*/
signals[AVAILABLITY_CHANGED_SIGNAL] = g_signal_new(
"availability-changed", FCITX_G_TYPE_WATCHER, G_SIGNAL_RUN_LAST, 0,
NULL, NULL, g_cclosure_marshal_VOID__BOOLEAN, G_TYPE_NONE, 1,
G_TYPE_BOOLEAN);
}
static void fcitx_g_watcher_init(FcitxGWatcher *self) {
self->priv = fcitx_g_watcher_get_instance_private(self);
self->priv->connection = NULL;
self->priv->cancellable = NULL;
self->priv->watch_id = 0;
self->priv->portal_watch_id = 0;
self->priv->main_owner = NULL;
self->priv->portal_owner = NULL;
self->priv->watched = FALSE;
}
static void fcitx_g_watcher_finalize(GObject *object) {
if (G_OBJECT_CLASS(fcitx_g_watcher_parent_class)->finalize != NULL)
G_OBJECT_CLASS(fcitx_g_watcher_parent_class)->finalize(object);
}
static void fcitx_g_watcher_dispose(GObject *object) {
FcitxGWatcher *self = FCITX_G_WATCHER(object);
if (self->priv->watched) {
fcitx_g_watcher_unwatch(self);
}
if (G_OBJECT_CLASS(fcitx_g_watcher_parent_class)->dispose != NULL)
G_OBJECT_CLASS(fcitx_g_watcher_parent_class)->dispose(object);
}
static void _fcitx_g_watcher_appear(G_GNUC_UNUSED GDBusConnection *conn,
const gchar *name, const gchar *name_owner,
gpointer user_data) {
g_return_if_fail(FCITX_G_IS_WATCHER(user_data));
FcitxGWatcher *self = FCITX_G_WATCHER(user_data);
if (g_strcmp0(name, FCITX_MAIN_SERVICE_NAME) == 0) {
g_free(self->priv->main_owner);
self->priv->main_owner = g_strdup(name_owner);
} else if (g_strcmp0(name, FCITX_PORTAL_SERVICE_NAME) == 0) {
g_free(self->priv->portal_owner);
self->priv->portal_owner = g_strdup(name_owner);
}
_fcitx_g_watcher_update_availability(self);
}
static void _fcitx_g_watcher_vanish(G_GNUC_UNUSED GDBusConnection *conn,
const gchar *name, gpointer user_data) {
g_return_if_fail(FCITX_G_IS_WATCHER(user_data));
FcitxGWatcher *self = FCITX_G_WATCHER(user_data);
if (g_strcmp0(name, FCITX_MAIN_SERVICE_NAME) == 0) {
g_free(self->priv->main_owner);
self->priv->main_owner = NULL;
} else if (g_strcmp0(name, FCITX_PORTAL_SERVICE_NAME) == 0) {
g_free(self->priv->portal_owner);
self->priv->portal_owner = NULL;
}
_fcitx_g_watcher_update_availability(self);
}
static void _fcitx_g_watcher_start_watch(FcitxGWatcher *self) {
if (!self->priv->watched) {
return;
}
g_clear_object(&self->priv->cancellable);
self->priv->cancellable = g_cancellable_new();
gchar *address = g_dbus_address_get_for_bus_sync(
G_BUS_TYPE_SESSION, self->priv->cancellable, NULL);
if (!address) {
g_clear_object(&self->priv->cancellable);
return;
}
g_object_ref(self);
g_dbus_connection_new_for_address(
address,
G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
NULL, self->priv->cancellable, _fcitx_g_watcher_get_bus_finished, self);
g_free(address);
}
/**
* fcitx_g_watcher_watch
* @self: a #FcitxGWatcher
*
* Watch for the fcitx serivce.
*/
void fcitx_g_watcher_watch(FcitxGWatcher *self) {
g_return_if_fail(!self->priv->watched);
self->priv->watched = TRUE;
_fcitx_g_watcher_start_watch(self);
};
static gboolean _fcitx_g_watcher_recheck(gpointer user_data) {
FcitxGWatcher *self = user_data;
_fcitx_g_watcher_start_watch(self);
return FALSE;
}
static void
_fcitx_g_watcher_connection_closed(GDBusConnection *connection G_GNUC_UNUSED,
gboolean remote_peer_vanished G_GNUC_UNUSED,
GError *error G_GNUC_UNUSED,
gpointer user_data) {
g_return_if_fail(user_data != NULL);
g_return_if_fail(FCITX_G_IS_WATCHER(user_data));
FcitxGWatcher *self = FCITX_G_WATCHER(user_data);
_fcitx_g_watcher_clean_up(self);
if (self->priv->watched) {
_fcitx_g_watcher_update_availability(self);
g_timeout_add_full(G_PRIORITY_DEFAULT, 100, _fcitx_g_watcher_recheck,
g_object_ref(self), g_object_unref);
}
}
static void
_fcitx_g_watcher_get_bus_finished(G_GNUC_UNUSED GObject *source_object,
GAsyncResult *res, gpointer user_data) {
g_return_if_fail(user_data != NULL);
g_return_if_fail(FCITX_G_IS_WATCHER(user_data));
FcitxGWatcher *self = FCITX_G_WATCHER(user_data);
// Need to call finish because clean up, otherwise cancellable will set the
// return value.
GDBusConnection *connection =
g_dbus_connection_new_for_address_finish(res, NULL);
_fcitx_g_watcher_clean_up(self);
if (!connection) {
g_object_unref(self);
return;
}
self->priv->connection = connection;
g_dbus_connection_set_exit_on_close(self->priv->connection, FALSE);
g_signal_connect(self->priv->connection, "closed",
(GCallback)_fcitx_g_watcher_connection_closed, self);
self->priv->watch_id =
g_bus_watch_name(G_BUS_TYPE_SESSION, FCITX_MAIN_SERVICE_NAME,
G_BUS_NAME_WATCHER_FLAGS_NONE, _fcitx_g_watcher_appear,
_fcitx_g_watcher_vanish, self, NULL);
if (self->priv->watch_portal) {
self->priv->portal_watch_id = g_bus_watch_name(
G_BUS_TYPE_SESSION, FCITX_PORTAL_SERVICE_NAME,
G_BUS_NAME_WATCHER_FLAGS_NONE, _fcitx_g_watcher_appear,
_fcitx_g_watcher_vanish, self, NULL);
}
_fcitx_g_watcher_update_availability(self);
/* unref for _fcitx_g_watcher_start_watch */
g_object_unref(self);
}
/**
* fcitx_g_watcher_unwatch
* @self: a #FcitxGWatcher
*
* Unwatch for the fcitx serivce, should only be called after
* calling fcitx_g_watcher_watch.
*/
void fcitx_g_watcher_unwatch(FcitxGWatcher *self) {
g_return_if_fail(self->priv->watched);
self->priv->watched = FALSE;
_fcitx_g_watcher_clean_up(self);
}
/**
* fcitx_g_watcher_new:
*
* New a #FcitxGWatcher
*
* Returns: A newly allocated #FcitxGWatcher
**/
FcitxGWatcher *fcitx_g_watcher_new() {
FcitxGWatcher *self = g_object_new(FCITX_G_TYPE_WATCHER, NULL);
return FCITX_G_WATCHER(self);
}
/**
* fcitx_g_watcher_is_valid:
* @connection: A #FcitxGWatcher
*
* Check #FcitxGWatcher is valid to communicate with Fcitx
*
* Returns: #FcitxGWatcher is valid or not
**/
gboolean fcitx_g_watcher_is_service_available(FcitxGWatcher *self) {
return self->priv->available;
}
/**
* fcitx_g_watcher_get_connection:
* self: A #FcitxGWatcher
*
* Return the current #GDBusConnection
*
* Returns: (transfer none): #GDBusConnection for current connection
**/
GDBusConnection *fcitx_g_watcher_get_connection(FcitxGWatcher *self) {
return self->priv->connection;
}
void _fcitx_g_watcher_clean_up(FcitxGWatcher *self) {
if (self->priv->cancellable) {
g_cancellable_cancel(self->priv->cancellable);
}
if (self->priv->watch_id) {
g_bus_unwatch_name(self->priv->watch_id);
self->priv->watch_id = 0;
}
if (self->priv->portal_watch_id) {
g_bus_unwatch_name(self->priv->portal_watch_id);
self->priv->portal_watch_id = 0;
}
if (self->priv->connection) {
g_signal_handlers_disconnect_by_data(self->priv->connection, self);
}
g_clear_pointer(&self->priv->main_owner, g_free);
g_clear_pointer(&self->priv->portal_owner, g_free);
g_clear_object(&self->priv->cancellable);
g_clear_object(&self->priv->connection);
}
/**
* fcitx_g_watcher_set_watch_portal:
* self: A #FcitxGWatcher
* watch: to monitor the portal service or not.
*
**/
void fcitx_g_watcher_set_watch_portal(FcitxGWatcher *self, gboolean watch) {
self->priv->watch_portal = watch;
}
void _fcitx_g_watcher_update_availability(FcitxGWatcher *self) {
gboolean available = self->priv->connection &&
(self->priv->main_owner || self->priv->portal_owner);
if (available != self->priv->available) {
self->priv->available = available;
g_signal_emit(self, signals[AVAILABLITY_CHANGED_SIGNAL], 0);
}
}
/**
* fcitx_g_watcher_get_service_name:
* @self: A #FcitxGWatcher
*
* Returns: (transfer none): an available service name.
*
**/
const gchar *fcitx_g_watcher_get_service_name(FcitxGWatcher *self) {
if (self->priv->main_owner) {
return self->priv->main_owner;
}
if (self->priv->portal_owner) {
return self->priv->portal_owner;
}
return NULL;
}
|