Skip to content

Commit

Permalink
Add cast and type-check functions for Session and Request
Browse files Browse the repository at this point in the history
We would usually get them from the G_DECLARE_TYPE macro family but we
cannot use them right now because they have no namespace and the
derivable classes would need private data and our data is public.

For now, just define the functions manually where it makes sense. In the
longer term, we should switch towards the G_DECLARE_TYPE macros.
  • Loading branch information
swick authored and GeorgesStavracas committed Oct 4, 2024
1 parent 1b5a169 commit a116a91
Show file tree
Hide file tree
Showing 17 changed files with 215 additions and 135 deletions.
2 changes: 1 addition & 1 deletion src/background.c
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ handle_request_background_in_thread_func (GTask *task,
gpointer task_data,
GCancellable *cancellable)
{
Request *request = (Request *)task_data;
Request *request = REQUEST (task_data);
GVariant *options;
const char *id;
Permission permission;
Expand Down
2 changes: 1 addition & 1 deletion src/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ handle_access_camera_in_thread_func (GTask *task,
gpointer task_data,
GCancellable *cancellable)
{
Request *request = (Request *)task_data;
Request *request = REQUEST (task_data);
gboolean allowed;

allowed = query_permission_sync (request);
Expand Down
26 changes: 12 additions & 14 deletions src/clipboard.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ handle_request_clipboard (XdpDbusClipboard *object,

SESSION_AUTOLOCK_UNREF (session);

if (!is_remote_desktop_session (session))
if (!IS_REMOTE_DESKTOP_SESSION (session))
{
g_dbus_method_invocation_return_error (invocation,
G_DBUS_ERROR,
G_DBUS_ERROR_ACCESS_DENIED,
"Invalid session type");
return G_DBUS_METHOD_INVOCATION_HANDLED;
}
remote_desktop_session = (RemoteDesktopSession *)session;
remote_desktop_session = REMOTE_DESKTOP_SESSION (session);

if (!remote_desktop_session_can_request_clipboard (remote_desktop_session))
{
Expand Down Expand Up @@ -130,7 +130,7 @@ handle_set_selection (XdpDbusClipboard *object,

SESSION_AUTOLOCK_UNREF (session);

if (!is_remote_desktop_session (session))
if (!IS_REMOTE_DESKTOP_SESSION (session))
{
g_dbus_method_invocation_return_error (invocation,
G_DBUS_ERROR,
Expand All @@ -139,7 +139,7 @@ handle_set_selection (XdpDbusClipboard *object,
return G_DBUS_METHOD_INVOCATION_HANDLED;
}
else if (!remote_desktop_session_is_clipboard_enabled (
(RemoteDesktopSession *)session))
REMOTE_DESKTOP_SESSION (session)))
{
g_dbus_method_invocation_return_error (invocation,
G_DBUS_ERROR,
Expand Down Expand Up @@ -240,7 +240,7 @@ handle_selection_write (XdpDbusClipboard *object,

SESSION_AUTOLOCK_UNREF (session);

if (!is_remote_desktop_session (session))
if (!IS_REMOTE_DESKTOP_SESSION (session))
{
g_dbus_method_invocation_return_error (invocation,
G_DBUS_ERROR,
Expand All @@ -249,7 +249,7 @@ handle_selection_write (XdpDbusClipboard *object,
return G_DBUS_METHOD_INVOCATION_HANDLED;
}
else if (!remote_desktop_session_is_clipboard_enabled (
(RemoteDesktopSession *)session))
REMOTE_DESKTOP_SESSION (session)))
{
g_dbus_method_invocation_return_error (invocation,
G_DBUS_ERROR,
Expand Down Expand Up @@ -291,7 +291,7 @@ handle_selection_write_done (XdpDbusClipboard *object,

SESSION_AUTOLOCK_UNREF (session);

if (!is_remote_desktop_session (session))
if (!IS_REMOTE_DESKTOP_SESSION (session))
{
g_dbus_method_invocation_return_error (invocation,
G_DBUS_ERROR,
Expand All @@ -300,7 +300,7 @@ handle_selection_write_done (XdpDbusClipboard *object,
return G_DBUS_METHOD_INVOCATION_HANDLED;
}
else if (!remote_desktop_session_is_clipboard_enabled (
(RemoteDesktopSession *)session))
REMOTE_DESKTOP_SESSION (session)))
{
g_dbus_method_invocation_return_error (invocation,
G_DBUS_ERROR,
Expand Down Expand Up @@ -381,7 +381,7 @@ handle_selection_read (XdpDbusClipboard *object,

SESSION_AUTOLOCK_UNREF (session);

if (!is_remote_desktop_session (session))
if (!IS_REMOTE_DESKTOP_SESSION (session))
{
g_dbus_method_invocation_return_error (invocation,
G_DBUS_ERROR,
Expand All @@ -390,7 +390,7 @@ handle_selection_read (XdpDbusClipboard *object,
return G_DBUS_METHOD_INVOCATION_HANDLED;
}
else if (!remote_desktop_session_is_clipboard_enabled (
(RemoteDesktopSession *)session))
REMOTE_DESKTOP_SESSION (session)))
{
g_dbus_method_invocation_return_error (invocation,
G_DBUS_ERROR,
Expand Down Expand Up @@ -452,8 +452,7 @@ selection_transfer_cb (XdpDbusImplClipboard *impl,

SESSION_AUTOLOCK_UNREF (session);

RemoteDesktopSession *remote_desktop_session =
(RemoteDesktopSession *)session;
RemoteDesktopSession *remote_desktop_session = REMOTE_DESKTOP_SESSION (session);

if (remote_desktop_session &&
remote_desktop_session_is_clipboard_enabled (remote_desktop_session) &&
Expand Down Expand Up @@ -489,8 +488,7 @@ selection_owner_changed_cb (XdpDbusImplClipboard *impl,

SESSION_AUTOLOCK_UNREF (session);

RemoteDesktopSession *remote_desktop_session =
(RemoteDesktopSession *)session;
RemoteDesktopSession *remote_desktop_session = REMOTE_DESKTOP_SESSION (session);

if (remote_desktop_session &&
remote_desktop_session_is_clipboard_enabled (remote_desktop_session) &&
Expand Down
28 changes: 22 additions & 6 deletions src/global-shortcuts.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,23 @@ GType global_shortcuts_session_get_type (void);

G_DEFINE_TYPE (GlobalShortcutsSession, global_shortcuts_session, session_get_type ())

G_GNUC_UNUSED static inline GlobalShortcutsSession *
GLOBAL_SHORTCUTS_SESSION (gpointer ptr)
{
return G_TYPE_CHECK_INSTANCE_CAST (ptr, global_shortcuts_session_get_type (), GlobalShortcutsSession);
}

G_GNUC_UNUSED static inline gboolean
IS_GLOBAL_SHORTCUTS_SESSION (gpointer ptr)
{
return G_TYPE_CHECK_INSTANCE_TYPE (ptr, global_shortcuts_session_get_type ());
}

static void
global_shortcuts_session_close (Session *session)
{
GlobalShortcutsSession *global_shortcuts_session = (GlobalShortcutsSession *)session;
GlobalShortcutsSession *global_shortcuts_session =
GLOBAL_SHORTCUTS_SESSION (session);

global_shortcuts_session->closed = TRUE;
}
Expand Down Expand Up @@ -133,7 +146,7 @@ global_shortcuts_session_new (GVariant *options,
if (session)
g_debug ("global shortcuts session owned by '%s' created", session->sender);

return (GlobalShortcutsSession *) session;
return GLOBAL_SHORTCUTS_SESSION (session);
}

static void
Expand Down Expand Up @@ -253,7 +266,7 @@ handle_create_session (XdpDbusGlobalShortcuts *object,
request_set_impl_request (request, impl_request);
request_export (request, g_dbus_method_invocation_get_connection (invocation));

session = (Session *)global_shortcuts_session_new (options, request, &error);
session = SESSION (global_shortcuts_session_new (options, request, &error));
if (!session)
{
g_dbus_method_invocation_return_gerror (invocation, error);
Expand Down Expand Up @@ -593,7 +606,8 @@ activated_cb (XdpDbusImplGlobalShortcuts *impl,
{
GDBusConnection *connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (impl));
g_autoptr(Session) session = lookup_session (session_id);
GlobalShortcutsSession *global_shortcuts_session = (GlobalShortcutsSession *)session;
GlobalShortcutsSession *global_shortcuts_session =
GLOBAL_SHORTCUTS_SESSION (session);

g_debug ("Received activated %s for %s", session_id, shortcut_id);

Expand All @@ -619,7 +633,8 @@ deactivated_cb (XdpDbusImplGlobalShortcuts *impl,
{
GDBusConnection *connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (impl));
g_autoptr(Session) session = lookup_session (session_id);
GlobalShortcutsSession *global_shortcuts_session = (GlobalShortcutsSession *)session;
GlobalShortcutsSession *global_shortcuts_session =
GLOBAL_SHORTCUTS_SESSION (session);

g_debug ("Received deactivated %s for %s", session_id, shortcut_id);

Expand All @@ -643,7 +658,8 @@ shortcuts_changed_cb (XdpDbusImplGlobalShortcuts *impl,
{
GDBusConnection *connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (impl));
g_autoptr(Session) session = lookup_session (session_id);
GlobalShortcutsSession *global_shortcuts_session = (GlobalShortcutsSession *)session;
GlobalShortcutsSession *global_shortcuts_session =
GLOBAL_SHORTCUTS_SESSION (session);

g_debug ("Received ShortcutsChanged %s", session_id);

Expand Down
20 changes: 16 additions & 4 deletions src/inhibit.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ handle_inhibit_in_thread_func (GTask *task,
gpointer task_data,
GCancellable *cancellable)
{
Request *request = (Request *)task_data;
Request *request = REQUEST (task_data);
const char *window;
guint32 flags;
GVariant *options;
Expand Down Expand Up @@ -265,10 +265,22 @@ GType inhibit_session_get_type (void);

G_DEFINE_TYPE (InhibitSession, inhibit_session, session_get_type ())

G_GNUC_UNUSED static inline InhibitSession *
INHIBIT_SESSION (gpointer ptr)
{
return G_TYPE_CHECK_INSTANCE_CAST (ptr, inhibit_session_get_type (), InhibitSession);
}

G_GNUC_UNUSED static inline gboolean
IS_INHIBIT_SESSION (gpointer ptr)
{
return G_TYPE_CHECK_INSTANCE_TYPE (ptr, inhibit_session_get_type ());
}

static void
inhibit_session_close (Session *session)
{
InhibitSession *inhibit_session = (InhibitSession *)session;
InhibitSession *inhibit_session = INHIBIT_SESSION (session);

inhibit_session->closed = TRUE;

Expand Down Expand Up @@ -421,7 +433,7 @@ handle_create_monitor (XdpDbusInhibit *object,
request_set_impl_request (request, impl_request);
request_export (request, g_dbus_method_invocation_get_connection (invocation));

session = (Session *)inhibit_session_new (arg_options, request, &error);
session = SESSION (inhibit_session_new (arg_options, request, &error));
if (!session)
{
g_dbus_method_invocation_return_gerror (invocation, error);
Expand Down Expand Up @@ -495,7 +507,7 @@ state_changed_cb (XdpDbusImplInhibit *impl,
{
GDBusConnection *connection = g_dbus_proxy_get_connection (G_DBUS_PROXY (impl));
g_autoptr(Session) session = lookup_session (session_id);
InhibitSession *inhibit_session = (InhibitSession *)session;
InhibitSession *inhibit_session = INHIBIT_SESSION (session);
gboolean active = FALSE;
guint32 session_state = 0;

Expand Down
Loading

0 comments on commit a116a91

Please sign in to comment.