Purge the pseudo-code examples

They haven't been kept up-to-date with API changes, so let's point to
the actual working demo tools instead.
This commit is contained in:
Peter Hutterer 2023-05-04 14:10:05 +10:00
parent a902d5dbd8
commit e1b2db8c10
3 changed files with 2 additions and 234 deletions

View file

@ -132,8 +132,8 @@ is identical regardless of backend.
High-level summary
------------------
A pseudo-code implementation for server and client are available in
the [`examples/`](https://gitlab.freedesktop.org/libinput/libei/-/tree/master/examples)
Simple demo implementations for server and client are available in
the [`tools/`](https://gitlab.freedesktop.org/libinput/libei/-/tree/master/tools)
directory.
The server starts a `libeis` context (which can be integrated with flatpak

View file

@ -1,98 +0,0 @@
# 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

View file

@ -1,134 +0,0 @@
# This is pseudocode, illustrating a libeis server implementation
#
# This pseudocode assumes to be part of a compositor that can handle
# input events. It assumes policy decisions as to whether a client may
# connect are made in the portal or the compositor.
#
function main():
ctx = eis_new()
eis_init(ctx)
while True:
poll(eis_get_fd()):
event = eis_get_event();
handler = functions[eis_event_get_type(event)]
handler(event)
for client in myclients:
eis_client_disconnect(client)
eis_unref(ctx)
function event_client_connect(event):
client = eis_event_get_client(event)
if do_not_allow_emulated_input:
eis_client_disconnect(client)
return
# for the portal backend we're assuming that the client has been
# authenticated by the user, otherwise some authentication needs to
# be done here
eis_client_connect(client)
myclients[client] = eis_client_ref(client)
# Send a seats out to the client. Could be the list of seats the
# server uses anyway, or custom ones.
seat = eis_client_new_seat(client, "default")
eis_seat_allow_capability(seat, EIS_DEVICE_CAP_POINTER)
eis_seat_allow_capability(seat, EIS_DEVICE_CAP_KEYBOARD)
eis_seat_add(seat)
function event_client_disconnect(event):
client = eis_event_get_client(event)
eis_client_unref(client)
function event_device_added(event):
client = eis_event_get_client(event)
device = eis_event_get_device(event)
if client has too many devices:
eis_device_disconnect(device)
return
if disallow_touch_input:
eis_device_disable_capability(EIS_DEVICE_CAP_TOUCH)
if eis_device_has_capability(EIS_DEVICE_CAP_POINTER_ABSOLUTE):
# this is the fixed range give the client. Let's check if there's a
# monitor with those dimensions and map the device to that monitor
# That's a server private implementation detail, just used as
# example here
w = eis_device_pointer_get_width()
h = eis_device_pointer_get_height()
if (w, h) matches an output:
eis_device_set_user_data(output)
if eis_device_has_capability(EIS_DEVICE_CAP_KEYBOARD):
keymap = eis_device_keyboard_get_keymap()
# We do not implement per-device keymap in this pseudoserver, notify
# the client. If we did, here'd be the place to assign the map.
if keymap != -1:
eis_device_keyboard_set_keymap(device, -1)
eis_device_connect(device)
# Allow the client to send events
eis_device_resume(capabilities)
function event_pointer_motion(event):
x = eis_event_pointer_get_x(event)
y = eis_event_pointer_get_y(event)
compositor_handle_pointer_motion(x, y)
function event_pointer_motion_absolute(event):
x = eis_event_pointer_get_absolute_x(event)
y = eis_event_pointer_get_absolute_y(event)
# the output we map to, see event_device_added
output = eis_device_get_userdata()
sx = x/1000
sy = y/1000
compositor_handle_pointer_motion_absolute(output, sx, sy)
function event_pointer_keyboard_key(event):
device = eis_event_get_device(event)
key = eis_event_keyboard_get_key(event)
press = eis_event_keyboard_get_key_is_press(event)
# double escape pauses the device, as an example
if press and key == KEY_ESC and last_key == KEY_ESC:
eis_device_pause()
discard_events = true
# We may have paused the client but events can be in mid flight, so
# we need to process all incoming events regardless. libeis will filter
# only once the client has received the pause notification.
update_client_key_state(key, press)
if not discard_events:
compositor_handle_key(key, press)
function compositor_vt_switch_callback():
for client in myclients:
for device in devices[client]:
if vt_in:
eis_device_resume(device)
else
eis_device_pause(device)
# Compositor callback if no more emulated input should be handled
# from a specific client
function compositor_callback_terminate_EI(condition):
for client in myclients:
if client matches condition:
eis_client_disconnect(client)