libei/examples/libei-client.pseudo

76 lines
2.3 KiB
Text
Raw Normal View History

# This is pseudocode, illustrating a libei client implementation
function main():
ctx = ei_new()
ei_portal_connect(ctx)
# let's say this is a blocking wait
event = ei_get_event();
if ei_event_get_type(event) == EI_EVENT_TYPE_DISCONNECT:
print("Sorry, server denied us")
return
# Could also create one device here with both caps
ptr = ei_create_device(ctx, CAP_POINTER, "pseudopointer");
kbd = ei_create_device(ctx, CAP_KEYBOARD, "pseudokeyboard");
keymap_fd = compile_keymap("us", "dvorak")
ei_device_keyboard_set_keymap(kbd, FORMAT_XKB, keymap_fd);
event = ei_get_event()
device = ei_event_get_device()
if ei_event_get_type(event) == EI_EVENT_TYPE_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))
else if ei_event_get_type(event) == EI_EVENT_TYPE_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 suspended mode
suspended = True
while True:
poll(ei_get_fd()):
if suspended:
wait_for_event(EI_EVENT_POINTER_RESUME or
EI_EVENT_KEYBOARD_RESUME)
suspended = 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