libei/examples/libei-client.pseudo
Peter Hutterer 8fc654bfb0 Purge the portal code
The original idea here was that we would have an EmulatedInput portal
that allows the application to connect directly to the EIS
implementation to exchange input events - instead of ping-ponging DBus
events through the xdg-desktop-portal as the RemoteDesktop portal
requires.

This is no longer accurate, there are suggested PRs open to add
RemoteDesktop.ConnectToEIS to achieve the same through the existing
RemoteDesktop interface [1] and to add a new InputCapture portal
to allow for events to be sent to a libei receiver context [2].

The example EmulatedInput portal is thus superfluous and can be removed
from here.

We could switch the ei_setup_backend_portal() code to use RemoteDesktop
or InputCapture, depending on the context type, the utility of this is
questionable. Interaction with portals is complex, one needs to
implement the Session/Request interfaces correctly and in the case of
InputCapture also handle the complex zones/pointer barrier setup.
libportal does some of this (or it will, anyway) so it's more useful for
an application to use libportal and then just pass the received fd to
libei.

If there is a future need for this to be handled as part of libei, we
can (re)implement this, but for now it's best to just purge all of this.

[1] https://github.com/flatpak/xdg-desktop-portal/pull/762
[2] https://github.com/flatpak/xdg-desktop-portal/pull/714
2022-12-08 10:45:47 +10:00

98 lines
3.1 KiB
Text

# This is pseudocode, illustrating a libei client implementation
function main():
ctx = ei_new()
ei_connect(ctx)
ei_dispatch(ctx)
# let's say this is a blocking wait
event = ei_get_event();
if ei_event_get_type(event) == EI_EVENT_DISCONNECT:
print("Sorry, server denied us")
return
if ei_event_get_type(event) == EI_EVENT_CONNECT:
print("We're connected")
# wait for a seat we can attach devices to
# let's assume the seat added event is in the first batch of events
event = ei_get_event();
while our_seat == NULL:
event = ei_get_event();
if ei_event_get_type(event) == EI_EVENT_SEAT_ADDED:
our_seat = ei_event_get_seat(event)
# Could also create one device here with both caps but splitting them
# means they can get paused independently
ptr = ei_create_device(our_seat)
ei_device_configure_capability(CAP_POINTER)
ei_device_configure_name("pseudopointer")
ei_device_add(ptr)
kbd = ei_create_device(our_seat)
ei_device_configure_capability(CAP_KEYBOARD)
ei_device_configure_name("pseudokeyboard");
keymap_fd = compile_keymap("us", "dvorak")
ei_device_keyboard_configure_keymap(kbd, FORMAT_XKB, keymap_fd);
ei_device_add(kbd)
event = ei_get_event()
device = ei_event_get_device()
if ei_event_get_type(event) == EI_EVENT_DEVICE_ADDED:
if device == ptr:
# The server may not use the name we suggested but it does
# tell us the chosen name
print("Pointer was created: %s", ei_device_get_name(device))
else:
print("Keyboard was created: %s", ei_device_get_name(device))
if ei_device_keyboard_get_keymap_source != EI_KEYMAP_SOURCE_CLIENT:
print("Server did not accept our keymap")
# handle the keymap (if not -1)
else if ei_event_get_type(event) == EI_EVENT_DEVICE_REMOVED:
if device == ptr:
print("We're not allowed a pointer device")
elif device == kbd:
print("We're not allowed a keyboard device")
# Our devices start in paused mode
paused = True
while True:
poll(ei_get_fd()):
if paused:
wait_for_event(EI_EVENT_DEVICE_RESUMED)
wait_for_event(EI_EVENT_DEVICE_RESUMED)
paused = False
continue
event = ei_get_event();
handler = functions[ei_event_get_type(event)]
handler(event)
ei_disconnect(ctx)
function event_disconnect(event):
print("Ooops, the server kicked us off")
ei_unref(ctx);
sys.exit(1)
function event_keyboard_keymap(event):
# see README comments
keymap_fd = ei_event_keyboard_get_keymap(event))
recompile_macros(keymap_fd)
# Called by our actual application to emulate input
function move_pointer(x, y):
ei_device_pointer_motion(ptr, x, y)
function macro(macro):
for key in macro:
ei_device_keyboard_key(kbd, key, true)
msleep(12); # fake an interval
ei_device_keyboard_key(kbd, key, false)
msleep(12); # fake an interval