Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Question on IOChannel #385

Closed
lukasstevens opened this issue Oct 4, 2016 · 12 comments
Closed

Question on IOChannel #385

lukasstevens opened this issue Oct 4, 2016 · 12 comments

Comments

@lukasstevens
Copy link

I have a question: Is there a way to listen to stdin in the gtk main loop?
Are there any plans to port GIOChannel which is currently only available through glib_sys::GIOChannel?

@GuillaumeGomez
Copy link
Member

Why would you want to read stdin in a GUI application?

@lukasstevens
Copy link
Author

lukasstevens commented Oct 4, 2016

My use case is relatively specific. I want to create a status bar with the same interface as i3bar. Meaning that the output of the i3status command is piped to my application. The information is then read from the stdin and displayed accordingly. For displaying the information I use gtk-rs and webkit2gtk.

EDIT: I probably could put out the information with a local webserver and view it with the webview.

@gkoz
Copy link
Member

gkoz commented Oct 25, 2016

Reading from stdin should be doable with just Rust's libstd why bother going through glib?

@lukasstevens
Copy link
Author

lukasstevens commented Oct 26, 2016

Maybe I am under the wrong impression, but I thought you can only read input with rusts facilities by adding your function as a callback via idle_add. With an IOChannel you would be able to add it to the event loop and you would get a callback only when there is input to read.

@gkoz
Copy link
Member

gkoz commented Oct 26, 2016

Oh right, with the blocking API in libstd you'd be forced to perform IO in a separate thread... That might not be a bad thing though. BTW I've been hacking on a way to wake up the event loop from other threads via a pipe like gtkmm's Dispatcher does. You wouldn't have to pay for idle_add overhead then.

@lukasstevens
Copy link
Author

Yeah that is exactly what I am doing right now. I start a seperate thread for reading input which is then send over a channel. I then add a callback via idle_add in which I use try_recv to check if there is any new input.

@gkoz
Copy link
Member

gkoz commented Oct 26, 2016

There's an issue that try_recv in a repetitive idle_add callback will still eat CPU unnecessarily. You'll get better results by either using timeout_add or scheduling a one-off recv with idle_add every time there's something to receive like this.

@lukasstevens
Copy link
Author

Thanks for the heads-up. Is there a reason you don't add the callback outside of the thread::spawn? I just moved the receiver to the scope of the callback and then I just try_recv for every callback.

@gkoz
Copy link
Member

gkoz commented Oct 26, 2016

A perpetual idle_add callback will get called continuously eating up any idle CPU. One way to avoid that is to schedule a handler when there's some data to receive and let it disconnect itself after receiving the data (Continue(false)).

@lukasstevens
Copy link
Author

Ah I understand now. Seeing this I think it would be a nice quality of live improvement if there is something like GIOChannel or a similiar solution.

@lukasstevens
Copy link
Author

lukasstevens commented Nov 8, 2016

I was just revisiting this and wanted to implement your solution since it is obviously better, but I encountered the problem that I can't use this method when I want to read input while the gtk main loop is running since gtk may only be accessed from the main thread. This means I can not call gtk::idle_add.

Edit: Scratch that I was using gtk::idle_add when I should have been using glib::idle_add.

@sdroege
Copy link
Member

sdroege commented Jun 19, 2019

@GuillaumeGomez Close this, it's a duplicate of https://github.com/gtk-rs/glib/issues/186 :)

alex179ohm pushed a commit to alex179ohm/gtk that referenced this issue Oct 21, 2019
Add support for gsize/gssize by using usize/isize
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants