Now it's time to present you a simple "Hello world" app. It's supposed to be short and easy to understand, and as for now it's as simple as a web.py web framework "Hello world". The app is located here and, well, works for me ;-)
First, you import the wcs module:
import wcs
Then, you send an application request to a window manager. WM returns an application object which has information about your application.
application = wcs.wm.create_new_application("Hello world")
This object contains a Window object, which stores your input and output interfaces you can use for controlling your screen and input device.
window = application.get_window("Hello window")
Now it's time to make an object class for your application. It needs to have a function with a blocking loop that can be interrupted on a call - a simple:
while self.running:
sleep(1)
will do. It also needs to have a function to interrupt the loop (setting the self.running variable to False). This is our stop() method.
But, of course, it needs to have output functions:
self.output.display_data("Hello world", "ENTER to exit")
output.display_data takes an arbitrary number of strings and display them line by line on the screen provided. It also needs a function that'd end the loop on a keypress:
self.input.set_callback('KEY_ENTER', 'stop', self)
This function takes a key ecode, an object and a object's method name which to call on a keypress. An object passing is necessary because Pyro, an IPC solution that's used, cannot pass functions around but can only pass objects and built-in types. There's also a 'KEY_KPENTER' callback which does the same - it's just that Enter key ecodes for a numpad depend on whether Numlock is on or off.
helloworld = HelloWorld(input, output)
That's it, we're done with the class! we'll just make an instance of it and register the object with Pyro, a function that's conveniently hidden by a wcs module, which also takes care of the concurrency - the loop that runs the Pyro daemon is blocking:
wcs.register_object(helloworld) wcs.start_daemon_thread()
Let's ask for a WM to switch context to our application - so that it immediately appears on the screen:
wcs.wm.activate_app(self.app.number)
The last thing is running the blocking loop. WCS has a wrapper for this that gives a graceful shutdown feature. First argument to this wrapper is the function to be run - our run() method, second is the application.destroy function - it lets the WM know about the shutdown and cleans it all up nicely.
wcs.run(helloworld.run, application.shutdown)
So, here's our Hello, world!
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.