Skip to content
Advertisement

How to send and execute multiple events in one file stream by writting to “/dev/input/EventX”

I am writting to a specific eventX file in /dev/input/, to trigger “hardware” events. The problem is, the events get executed, only after the file has closed and to trigger new events, the file needs to be reopened. It’s as if all the events are put on a queue and then when the file closes, they get executed. For example

Task: Perform mouse press.

JavaScript

The send_mouse_click_event, which depends on the function send, in defined as follows

JavaScript

My question is, Is there any event I can send() to indicate that I want to execute all the items that are placed in the queue.

Closing and opening a file, is an expensive operation, and having to do that multiple times is a performance killer. There must be a better way that keeps the file stream open.

Advertisement

Answer

Unless buffering has been disabled in the open call by setting the buffering parameter to 0, the file stream will be either fully buffered or line buffered. This depends on the type of file being opened and on the open mode, but binary file streams are never set to line buffered mode.

In buffered mode, the write method will write to the buffer, but the buffer contents will not be written to the underlying file until either the buffer is full, or a newline is written in line buffered mode.

The flush and close methods will write the buffer contents to the underlying file. The close method also closes the file afterwards.

In OP’s code, adding file_stream.flush() to the end of the send function will write the buffer contents through to the underlying file every time the send function is called. An alternative is to add filestream.flush() to the end the send_mouse_click_event function so that the data written by the three calls to send is flushed to the underlying file in one go.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement