Why use the wmctrljs library

kevin VOYER
3 min readFeb 13, 2021

Here we are going to talk about how to use wmctrl features using nodejs.

First of all why using wmctrl with nodejs, so it is pretty rare to interact with the remote graphic environnement using nodejs because the most of the times we use nodejs as a server rest API and we don’t have any interaction with the graphics elements on the server.

However we can reach some use cases were through a web server we want to interact with the graphical environment on the remote. Lets take the exemple of the AbcDesktopio project which is a project I contributed and it is the reason why I created the wmctrljs library in the first place.

Some features implemented in this project allow users to do several interactions with the windows he created in his desktop:

  • Close a window by providing a window’s id to the backend. Here only one interaction with X11 is made.
  • Maximise the size of a window, here also a window’s id is provide to the backend and only one interaction is made with X11.
  • Split the screen with all windows, this feature requires to make six interactions with X11 for each window present on the user’s desktop. On top of this, to be able of calculate the position of each window and theirs size we need to get the size of the screen and get the id of all windows. So just for this feature we need to make (2 + 6 * N) interactions with X11, where N is the number of window.

Encountered problems when using wmctrl using fork exec

As we saw in the previous example some use case appends where we have to make a lot of interactions with X11. For serveral reason it is unacceptable of making those interactions using fork exec.

  • First of all if we do a fork exec at each interaction with X11 we will be face to big performance issues.
  • Also we have to do dirty parsing of the wmctrl’s standard out which is very slow, and this approach doesn't allow us to handle correctly the errors that X11 provide to us.
  • Especially the possibility of having a breach in your endpoint verfification is not exclude, and that can allow a user to execute command on your remote server through the fork exec call.

What wmctrljs library provide

The wmctrljs library has been created for allowing nodejs developers to do the same interactions with X11 than the wmctrl binary command does and more. All of this without doing any fork exec command, to do this wmctrljs use the C code used by the wmctrl command. And then a binding between the Js and C/C++ world is made.

All actions possible through this library is available in synchronous ans asynchronous ways.

In the synchronous way all interactions with X11 is made on the same thread as the JS runtime. This is a good approach but not the best one, however this is way more efficient than a fork exec.

The asynchronous way is the fact of using an AsyncWorker to make the interactions with X11. An AsyncWorker allow you to make an heavy task on a separate thread of the JS runtime and getting a result in the Javascript’s event loop’s queue at the end of the task.

You can learn more about NodeJS’s AsyncWorker on the following link. https://github.com/nodejs/node-addon-api/blob/main/doc/async_worker.md

--

--

kevin VOYER
0 Followers

The skills of a computer scientist are the vector of his success