File: oeffis-demo-tool.c

package info (click to toggle)
libei 1.3.901-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,900 kB
  • sloc: ansic: 23,219; python: 2,686; xml: 1,264; sh: 142; makefile: 63; cpp: 12; lisp: 2
file content (121 lines) | stat: -rw-r--r-- 3,400 bytes parent folder | download | duplicates (2)
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
/* SPDX-License-Identifier: MIT */
/*
 * Copyright © 2020 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/* A simple tool that connects to an EIS implementation via the RemoteDesktop
 * portal using liboeffis
 */

#include "config.h"

#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <signal.h>
#include <sys/poll.h>

#include "src/util-macros.h"
#include "src/util-mem.h"
#include "src/util-io.h"

#include "liboeffis.h"

#ifndef MESON_BUILDDIR
#error MESON_BUILDDIR must be defined
#endif

DEFINE_UNREF_CLEANUP_FUNC(oeffis);

static bool stop = false;

static void sighandler(int signal) {
	stop = true;
}

static void start_debug_events(int fd)
{
	pid_t pid = fork();
	assert(pid != -1);

	if (pid == 0) {
		_cleanup_free_ char *fdstr = xaprintf("%d", fd);
		execl(MESON_BUILDDIR "/ei-debug-events",
		      "ei-debug-events",
		      "--socketfd", fdstr, NULL);
		fprintf(stderr, "Failed to fork: %m\n");
		exit(1);
	}
}

int main(int argc, char **argv)
{
	_unref_(oeffis) *oeffis = oeffis_new(NULL);

	signal(SIGINT, sighandler);

	struct pollfd fds = {
		.fd = oeffis_get_fd(oeffis),
		.events = POLLIN,
		.revents = 0,
	};

	oeffis_create_session(oeffis, OEFFIS_DEVICE_POINTER|OEFFIS_DEVICE_KEYBOARD);

	while (!stop && poll(&fds, 1, 1000) > -1) {
		oeffis_dispatch(oeffis);

		enum oeffis_event_type e = oeffis_get_event(oeffis);
		switch (e) {
		case OEFFIS_EVENT_NONE:
			break;
		case OEFFIS_EVENT_CONNECTED_TO_EIS: {
			_cleanup_close_ int eisfd = oeffis_get_eis_fd(oeffis);
			printf("# We have an eisfd: lucky number %d\n", eisfd);
			/* Note: unless we get closed/disconnected, we are
			 * started now, or will be once the compositor is
			 * happy with it.
			 *
			 * We still need to keep dispatching events though
			 * to be able to receive the Session.Closed signal
			 * or any disconnections. And we must keep the
			 * oeffis context alive - once the context is
			 * destroyed we disconnect from DBus which will
			 * cause the compositor or the portal to invalidate
			 * our EIS fd.
			 */
			start_debug_events(eisfd);
			break;
		}
		case OEFFIS_EVENT_DISCONNECTED:
			fprintf(stderr, "Disconnected: %s\n", oeffis_get_error_message(oeffis));
			stop = true;
			break;
		case OEFFIS_EVENT_CLOSED:
			printf("Closing\n");
			stop = true;
			break;
		}
	}

	return 0;
}