0

I am developing a simple application in C with GTK+2.0 GUI, on Linux of course, this application is designed to control a device which is connected over the USB port and is using FTDI driver to emulate RS232 asynchronious protocol over the USB port.

I wanted to create event driven control, like signals in GTK which are used to detect when the button is clicked et cetera. I found glib library for this, I have read in the documentation it seems clear. I know I must use functions like g_io_add_watch() to add event to be detected, that I can define my functions which would be "triggered" by this event and that for example "G_IO_IN" means it will be triggered when there is some output from the device. I just cannot find any usefull examples on the web.

The other thing is, I do not understand how to use GTK+2.0 and glib at once, because both have their own program loops to detect events (gtk_main() for GTK+2.0 and GMainLoop in glib).

I would be greatfull if you share some interesting examples, links, tutorials et cetera. Maybe I need to know more specific vocabulary, because I cannot find anything usefull on the web to solve this problem. Thanks!

Marek
  • 1,413
  • 2
  • 20
  • 36
  • GTK+ uses glib, so for the GUI stuff you rarely need to use the glib API directly. A notable exception is the `g_signal_connect()` family of functions, for connecting GTK+ signal handlers to your graphical elements (like when clicking a button in your example) since the outdated `gtk_signal_connect()` has been depreciated a long time ago. On the other hand, `gtk_main()` is still the preferred way for starting the main GUI loop. – Harry K. May 19 '13 at 10:20
  • yes, I am using `g_signal_connect()` to define GUI events, I'm just not sure how to integrate detection of ftdi driver signals, so it would be possible to use the divice like G_OBJECT in order to create an event with `g_signal_connect()` ? – Marek May 19 '13 at 10:24
  • Oh I see, unfortunately I cannot help you with the ftdi driver signals (have not enough experience). But for emitting GTK+ signals have a look at the `g_signal_emit_()` family of functions, along with the documentation around them. You can also create, emit and handle your own GTK+ signals (via `marshallers` if I recall correctly). – Harry K. May 19 '13 at 10:29
  • I see, so it seems possible to define custom GTK+ signals, I looked up the keyword `marshallers`. Problem isn't solved yet, but thanks, at least I'm not at dead end anymore. – Marek May 19 '13 at 10:41

1 Answers1

0

GTK+ uses GLib, and the main loop is not an exception. This means the GTK+ loop is a GMainLoop, hence all the GLib functions can be used with it.

Here is a reference to the GTK+ code to prove this.

ntd
  • 7,372
  • 1
  • 27
  • 44
  • I understand. So please tell me, if I reference `#include ` will I reference (by transition) also glib? And if I define events by `g_io_add_watch(...)` and later start the `gtk_main()` loop will it wait for those events to occur? – Marek May 19 '13 at 11:11
  • `#include ` will pull in `#include ` and `#include `. This is not documented so I personally include them explicitely in my projects. For the second question the answer is "yes, you can use `g_io_add_watch()`" and "no, the main cycle will not wait". `g_io_add_watch()` does not pause the cycle but it devises a callback call. – ntd May 19 '13 at 11:25