Question: Is it possible to check if gtk_init was already called even of different major version?



Hello!

Our team is developing crossplatform Qt application and use some gtk
functions for X11 version but we're stacked with a problem described
further.

Normally Qt initializes GTK by calling gtk_init() function by itself.
But in some builds (e.g. LSB) Qt use some fallback solutions and
doesn't do that. For these cases we wrote native gtk code, which uses
gtk-3 shared library.
And here is a problem. Normally gtk_init_check() works well and
returns true if gtk_init() was already called. But if we use
gtk_init_check() from gtk-3 library while gtk_init() from gtk-2.0
library was already called (e.g. by Qt), program fails with
segmentation fault.
In that case we need some other solution to detect if GTK was already
initialized. Currently we are checking if gtk_settings_get_default()
returns NULL for that purposes. But documentation says nothing about
does it also means that GTK wasn't initialized previously?

Sample code:

libGtk = dlopen("libgtk-3.so.0", RTLD_LAZY);
...
gtkInitCheck = (GtkInitCheckFn)dlsym(libGtk, "gtk_init_check");
gTypeInit = (GTypeInitFn)dlsym(libGtk, "g_type_init");
gtkSettingGetDefault = (GtkSettingGetDefaultFn)dlsym(libGtk,
"gtk_settings_get_default");
...
gTypeInit();
settings = gtkSettingGetDefault();
/* Does settings == NULL means that gtk_init wasn't called before? */
/* If no, gtkInitCheck(NULL, NULL) will throw segmentation fault if
gtk_init() from gtk-2.0 was called. */
if (!settings && gtkInitCheck(NULL, NULL)) {
gTypeInit();
settings = gtkSettingGetDefault();
}

Of course, we could avoid that problem simply by using gtk-2.0 shared
library for our code but it's a really fallback way, currently we want
to use gtk-3 if it's possible.

Thanks!


--
Best regards,
Kirill Medvedev


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]