2021-08-16 19:09:59 -04:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2021 Red Hat Inc.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2023-08-07 05:50:23 -04:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2021-08-16 19:09:59 -04:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <libeis.h>
|
|
|
|
|
|
|
|
#include "backends/meta-eis.h"
|
|
|
|
#include "backends/meta-eis-client.h"
|
|
|
|
#include "clutter/clutter-mutter.h"
|
2021-08-17 20:41:33 -04:00
|
|
|
#include "meta/util.h"
|
2021-08-16 19:09:59 -04:00
|
|
|
|
|
|
|
typedef struct _MetaEventSource MetaEventSource;
|
|
|
|
|
|
|
|
struct _MetaEventSource
|
|
|
|
{
|
|
|
|
GSource source;
|
|
|
|
|
2023-08-15 06:49:16 -04:00
|
|
|
MetaEis *eis;
|
2021-08-16 19:09:59 -04:00
|
|
|
GPollFD event_poll_fd;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct _MetaEis
|
|
|
|
{
|
|
|
|
GObject parent_instance;
|
|
|
|
MetaBackend *backend;
|
|
|
|
|
|
|
|
struct eis *eis;
|
|
|
|
MetaEventSource *event_source;
|
|
|
|
|
2023-08-28 11:21:35 -04:00
|
|
|
MetaEisDeviceTypes device_types;
|
|
|
|
|
2021-08-16 19:09:59 -04:00
|
|
|
GHashTable *eis_clients; /* eis_client => MetaEisClient */
|
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (MetaEis, meta_eis, G_TYPE_OBJECT)
|
|
|
|
|
|
|
|
MetaBackend *
|
2023-08-15 06:49:16 -04:00
|
|
|
meta_eis_get_backend (MetaEis *eis)
|
2021-08-16 19:09:59 -04:00
|
|
|
{
|
2023-08-15 06:49:16 -04:00
|
|
|
return eis->backend;
|
2021-08-16 19:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-08-15 06:49:16 -04:00
|
|
|
meta_eis_remove_client (MetaEis *eis,
|
2021-08-16 19:09:59 -04:00
|
|
|
struct eis_client *eis_client)
|
|
|
|
{
|
2023-08-15 06:49:16 -04:00
|
|
|
g_hash_table_remove (eis->eis_clients, eis_client);
|
2021-08-16 19:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-08-15 06:49:16 -04:00
|
|
|
meta_eis_add_client (MetaEis *eis,
|
2021-08-16 19:09:59 -04:00
|
|
|
struct eis_client *eis_client)
|
|
|
|
{
|
2023-08-15 06:49:16 -04:00
|
|
|
MetaEisClient *client;
|
2021-08-16 19:09:59 -04:00
|
|
|
|
2023-08-15 06:49:16 -04:00
|
|
|
client = meta_eis_client_new (eis, eis_client);
|
2021-08-16 19:09:59 -04:00
|
|
|
|
2023-08-15 06:49:16 -04:00
|
|
|
g_hash_table_insert (eis->eis_clients,
|
2021-08-16 19:09:59 -04:00
|
|
|
eis_client_ref (eis_client),
|
2023-08-15 06:49:16 -04:00
|
|
|
client);
|
2021-08-16 19:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-08-15 06:49:16 -04:00
|
|
|
process_event (MetaEis *eis,
|
2021-08-16 19:09:59 -04:00
|
|
|
struct eis_event *event)
|
|
|
|
{
|
|
|
|
enum eis_event_type type = eis_event_get_type (event);
|
|
|
|
struct eis_client *eis_client = eis_event_get_client (event);
|
2023-08-15 06:49:16 -04:00
|
|
|
MetaEisClient *client;
|
2021-08-16 19:09:59 -04:00
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case EIS_EVENT_CLIENT_CONNECT:
|
2023-08-15 06:49:16 -04:00
|
|
|
meta_eis_add_client (eis, eis_client);
|
2021-08-16 19:09:59 -04:00
|
|
|
break;
|
|
|
|
case EIS_EVENT_CLIENT_DISCONNECT:
|
2023-08-15 06:49:16 -04:00
|
|
|
meta_eis_remove_client (eis, eis_client);
|
2021-08-16 19:09:59 -04:00
|
|
|
break;
|
|
|
|
default:
|
2023-08-15 06:49:16 -04:00
|
|
|
client = g_hash_table_lookup (eis->eis_clients, eis_client);
|
|
|
|
if (!client)
|
2021-08-16 19:09:59 -04:00
|
|
|
{
|
|
|
|
g_warning ("Event for unknown EIS client: %s",
|
|
|
|
eis_client_get_name (eis_client));
|
|
|
|
return;
|
|
|
|
}
|
2023-08-15 06:49:16 -04:00
|
|
|
meta_eis_client_process_event (client, event);
|
2021-08-16 19:09:59 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-08-15 06:49:16 -04:00
|
|
|
process_events (MetaEis *eis)
|
2021-08-16 19:09:59 -04:00
|
|
|
{
|
|
|
|
struct eis_event *e;
|
|
|
|
|
2023-08-15 06:49:16 -04:00
|
|
|
while ((e = eis_get_event (eis->eis)))
|
2021-08-16 19:09:59 -04:00
|
|
|
{
|
2023-08-15 06:49:16 -04:00
|
|
|
process_event (eis, e);
|
2021-08-16 19:09:59 -04:00
|
|
|
eis_event_unref (e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static MetaEventSource *
|
2023-08-15 06:49:16 -04:00
|
|
|
meta_event_source_new (MetaEis *eis,
|
2021-08-16 19:09:59 -04:00
|
|
|
int fd,
|
|
|
|
GSourceFuncs *event_funcs)
|
|
|
|
{
|
|
|
|
GSource *source;
|
|
|
|
MetaEventSource *event_source;
|
|
|
|
|
|
|
|
source = g_source_new (event_funcs, sizeof (MetaEventSource));
|
|
|
|
event_source = (MetaEventSource *) source;
|
|
|
|
|
|
|
|
/* setup the source */
|
2023-08-15 06:49:16 -04:00
|
|
|
event_source->eis = eis;
|
2021-08-16 19:09:59 -04:00
|
|
|
|
|
|
|
event_source->event_poll_fd.fd = fd;
|
|
|
|
event_source->event_poll_fd.events = G_IO_IN;
|
|
|
|
|
|
|
|
/* and finally configure and attach the GSource */
|
|
|
|
g_source_set_priority (source, CLUTTER_PRIORITY_EVENTS);
|
|
|
|
g_source_add_poll (source, &event_source->event_poll_fd);
|
|
|
|
g_source_set_can_recurse (source, TRUE);
|
|
|
|
g_source_attach (source, NULL);
|
|
|
|
|
|
|
|
return event_source;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
meta_event_source_free (MetaEventSource *source)
|
|
|
|
{
|
|
|
|
GSource *g_source = (GSource *) source;
|
|
|
|
|
|
|
|
/* ignore the return value of close, it's not like we can do something
|
|
|
|
* about it */
|
|
|
|
close (source->event_poll_fd.fd);
|
|
|
|
|
|
|
|
g_source_destroy (g_source);
|
|
|
|
g_source_unref (g_source);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
meta_event_prepare (GSource *g_source,
|
|
|
|
int *timeout_ms)
|
|
|
|
{
|
|
|
|
MetaEventSource *source = (MetaEventSource *) g_source;
|
2023-08-15 06:49:16 -04:00
|
|
|
MetaEis *eis = source->eis;
|
2021-08-16 19:09:59 -04:00
|
|
|
struct eis_event *e;
|
|
|
|
|
|
|
|
*timeout_ms = -1;
|
|
|
|
|
2023-08-15 06:49:16 -04:00
|
|
|
e = eis_peek_event (eis->eis);
|
2021-08-16 19:09:59 -04:00
|
|
|
if (e)
|
|
|
|
{
|
|
|
|
eis_event_unref (e);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
meta_event_check (GSource *source)
|
|
|
|
{
|
|
|
|
MetaEventSource *event_source = (MetaEventSource *) source;
|
|
|
|
gboolean retval;
|
|
|
|
|
|
|
|
retval = !!(event_source->event_poll_fd.revents & G_IO_IN);
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
meta_event_dispatch (GSource *g_source,
|
|
|
|
GSourceFunc callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
MetaEventSource *source = (MetaEventSource *) g_source;
|
2023-08-15 06:49:16 -04:00
|
|
|
MetaEis *eis = source->eis;
|
2021-08-16 19:09:59 -04:00
|
|
|
|
2023-08-15 06:49:16 -04:00
|
|
|
eis_dispatch (eis->eis);
|
|
|
|
process_events (eis);
|
2021-08-16 19:09:59 -04:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static GSourceFuncs eis_event_funcs = {
|
|
|
|
meta_event_prepare,
|
|
|
|
meta_event_check,
|
|
|
|
meta_event_dispatch,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2021-08-17 20:41:33 -04:00
|
|
|
static void
|
|
|
|
eis_logger (struct eis *eis,
|
|
|
|
enum eis_log_priority priority,
|
|
|
|
const char *message,
|
|
|
|
struct eis_log_context *ctx)
|
|
|
|
{
|
|
|
|
switch (priority)
|
|
|
|
{
|
|
|
|
case EIS_LOG_PRIORITY_DEBUG:
|
|
|
|
meta_topic (META_DEBUG_EIS, "%s", message);
|
|
|
|
break;
|
|
|
|
case EIS_LOG_PRIORITY_WARNING:
|
|
|
|
g_warning ("%s", message);
|
|
|
|
break;
|
|
|
|
case EIS_LOG_PRIORITY_ERROR:
|
|
|
|
g_error ("%s", message);
|
|
|
|
break;
|
|
|
|
case EIS_LOG_PRIORITY_INFO:
|
|
|
|
default:
|
|
|
|
g_info ("%s", message);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-28 11:52:50 -04:00
|
|
|
int
|
2023-08-15 06:49:16 -04:00
|
|
|
meta_eis_add_client_get_fd (MetaEis *eis)
|
2022-09-28 11:52:50 -04:00
|
|
|
{
|
2023-08-15 06:49:16 -04:00
|
|
|
return eis_backend_fd_add_client (eis->eis);
|
2022-09-28 11:52:50 -04:00
|
|
|
}
|
|
|
|
|
2021-08-16 19:09:59 -04:00
|
|
|
MetaEis *
|
2023-08-28 11:21:35 -04:00
|
|
|
meta_eis_new (MetaBackend *backend,
|
|
|
|
MetaEisDeviceTypes device_types)
|
2021-08-16 19:09:59 -04:00
|
|
|
{
|
2023-08-15 06:49:16 -04:00
|
|
|
MetaEis *eis;
|
2021-08-16 19:09:59 -04:00
|
|
|
int fd;
|
|
|
|
|
2023-08-15 06:49:16 -04:00
|
|
|
eis = g_object_new (META_TYPE_EIS, NULL);
|
|
|
|
eis->backend = backend;
|
2023-08-28 11:21:35 -04:00
|
|
|
eis->device_types = device_types;
|
2021-08-16 19:09:59 -04:00
|
|
|
|
2023-08-15 06:49:16 -04:00
|
|
|
eis->eis = eis_new (eis);
|
|
|
|
eis_log_set_handler (eis->eis, eis_logger);
|
|
|
|
eis_log_set_priority (eis->eis, EIS_LOG_PRIORITY_DEBUG);
|
2023-08-28 10:49:48 -04:00
|
|
|
eis_setup_backend_fd (eis->eis);
|
2021-08-17 20:41:33 -04:00
|
|
|
|
2023-08-15 06:49:16 -04:00
|
|
|
fd = eis_get_fd (eis->eis);
|
|
|
|
eis->event_source = meta_event_source_new (eis, fd, &eis_event_funcs);
|
2021-08-16 19:09:59 -04:00
|
|
|
|
2023-08-15 06:49:16 -04:00
|
|
|
return eis;
|
2021-08-16 19:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-08-15 06:49:16 -04:00
|
|
|
meta_eis_init (MetaEis *eis)
|
2021-08-16 19:09:59 -04:00
|
|
|
{
|
2023-08-15 06:49:16 -04:00
|
|
|
eis->eis_clients = g_hash_table_new_full (g_direct_hash, g_direct_equal,
|
|
|
|
(GDestroyNotify) eis_client_unref,
|
|
|
|
(GDestroyNotify) g_object_unref);
|
2021-08-16 19:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-08-29 14:28:24 -04:00
|
|
|
meta_eis_dispose (GObject *object)
|
2021-08-16 19:09:59 -04:00
|
|
|
{
|
2023-08-15 06:49:16 -04:00
|
|
|
MetaEis *eis = META_EIS (object);
|
2021-08-16 19:09:59 -04:00
|
|
|
|
2023-08-15 06:49:16 -04:00
|
|
|
g_clear_pointer (&eis->event_source, meta_event_source_free);
|
|
|
|
g_clear_pointer (&eis->eis, eis_unref);
|
|
|
|
g_clear_pointer (&eis->eis_clients, g_hash_table_destroy);
|
2023-08-22 15:57:07 -04:00
|
|
|
|
2023-08-29 14:28:24 -04:00
|
|
|
G_OBJECT_CLASS (meta_eis_parent_class)->dispose (object);
|
2021-08-16 19:09:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
meta_eis_class_init (MetaEisClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
2023-08-29 14:28:24 -04:00
|
|
|
object_class->dispose = meta_eis_dispose;
|
2021-08-16 19:09:59 -04:00
|
|
|
}
|
2023-08-28 11:21:35 -04:00
|
|
|
|
|
|
|
MetaEisDeviceTypes
|
|
|
|
meta_eis_get_device_types (MetaEis *eis)
|
|
|
|
{
|
|
|
|
return eis->device_types;
|
|
|
|
}
|