Skip to content

exercise_3

bjornstahl edited this page Oct 14, 2021 · 4 revisions

Exercise 3

The third exercise set looks into device input events, timed events and some of the support scripts that can help.

The subtasks to go through here are as follows:

1 - Event Handler

Create a function that receives input events and dump the fields of an input event to the standard output.

Hint 1: The engine will look for a function called applname_input

Hint 2: The argument to the input event function is a key- indexed table.

2 - Clock Ticking

Create a function that spawns a randomly colored rectangle at random positions of the screen every second clock pulse.

Hint 1: Similar to input events, but the suffix is clock_pulse.

Hint 2: Lua built-in function math.random is available.

Hint 3: Look into expire_image and delete_image to prevent the periodic creation from eventually running out of resources and terminating.

3 - Translating Keyboard

There are many somewhat complex problems when it comes to internationalization and input, even for as something as seemingly trivial as getting textual representation of which keypress should translate to what and when.

The naive and overly simple approach to that particular problem is the purpose of this exercise. Find a way to translate the input event handler events for keyboard devices, and print a higher-level representation of a keyboard button event when a button is released. A sample table that can be used can be found in builtin/keyboard.lua.

Hint 1: Look into system_load.

Hint 2: The keysym field in the input table fits as an index into the table returned from running the keyboard script.

Find keys on your keyboard are problematic, and try to answer why those particular keys are mistranslated. The first exercise in this set may help you.

4 - Simple Cursor (deprecated)

5 - Mouse Gestures

There is a more complex mouse support script hiding in builtin/mouse.lua that can be used to get access to events such as over/out/hover/click/doubleclick etc.

This is a rather complex script to use, do not spend too much time on it and only gaze upon its chtulian horrors if you are cat-curious. It has a lot of legacy.

Other exercises are more important and the boiler-plate code in the script can be grabbed from some of the code examples in the source tree. What you want to know is this:

system_load("builtin/mouse.lua")()
local cursor = color_surface(8,8, 0, 255, 0)
mouse_setup(cursor, 65535, 1, true, false)

the '65535' places the cursor at the highest possible order slot (always on top), the '1' indicates that it will only try and evaluate the topmost item if multiple are stacked, the 'true' sets a cached pick to be default rather than quering the engine on every mouse sample, and the 'false' says that the cursor starts visible.

Hint 1: Gestures need access to a timer, connect mouse_tick in your clock_pulse.

Hint 2: mouse_iotbl_input(iotbl) will consume a properly formatted mouse input table.

Hint 3: mouse_setup_native(vid) uses the accelerated path, mouse_setup(vid, layer, depth, cached, hidden, opttbl) uses the manual method.

Hint 4: mouse_addlistener(handler, events) handler table should have a field for own (callback(vid) return true if the handler takes care of the vid), for name and one function entry for each event ('click', 'over', 'out', 'drag', 'press', 'release', 'drop', 'hover', 'motion', 'dblclick', 'rclick').