How to use Gtkmm-3 and standard input together?

Asked

Viewed 92 times

4

I want to make a program with Gtk that reads data from the standard input and interprets making drawings in a DrawingArea.

The concept is simple, but I came across a problem: after I called Gtk::Application::run the only code written by me to execute is the sign on_draw of DrawingArea.

I need to receive the standard input and I can’t do it in the method on_draw because the program would stop responding.

What I want is to be able to send data at any time, and that the drawing update in real time.

The input, interpretation and display of the drawing on the screen are easy to implement, but doing them together seems a difficult task.

And then how could I do it? A very simple example would be very useful (basically what I want is to be able to enter text while the function Gtk::Application::run executes)

2 answers

2

I never used Gtkmm, however, in gtk "standard" I see two ways to do this (in linux)...

  1. The simplest way: Raise a thread by reading the standard input with fgets and including the data in a Gasyncqueue read by a timer in the main thread. The queue is to prevent the thread from accessing the Gui message queue at an "improper time".

  2. More complicated: "encapsulate" the Handle of the standard input into an GIO (via g_unix_input_stream) and treat like any other data source. This only uses a thread and avoids any synchronization problems; this is what I usually use.

0

One solution to your problem is to use inter-process communication (CPI). This way, you divide your program into two parts:

  • The first will start the other process (program) that will make the drawings and then works only to receive the user input values.
  • The second part will "listen" to the commands sent by the first process and make the drawings.

To do this, you can use your own gtkmm-3 (example) or another library that does the served. For example: Boost.Interprocess.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.