Discussion:
[bonobo_main] What to do when gtk_main is already called
Aditya Pandey
2005-04-04 12:23:57 UTC
Permalink
I have written a plugin to a gtk+ application [so it has already
started gtk_main].
My plugin integrates with gnome-speech which requires bonobo.

For some speech drivers which support callbacks, I need to do
bonobo_main. But since gtk_main is already running, it somehow
interferes with that (I understand that gtk_main_quit is called by
bonobo_main). By interference I mean that dialogs
fail to respond to ok event to close themselves when bonobo_main is called etc.

Is there some alternative to bonobo_main ? I cant replace gtk_main
code and neither can I make sure that gtk_main [part of main app] is
called after bonobo_main [part of plugin].

I could think of 2 alternatives:

1. since bonobo_main calls gtk_main_quit, I do gtk_main after it again [Not sure
if this is the correct method, looks like a hack to me].

2. is bonobo_ui_main for this situation?

Please advise/suggest on which way to go.
--
Aditya Kumar Pandey
----------------------------------------
Contact: +91-9868263500; +91-11-25165432
Aditya Pandey
2005-04-04 13:37:13 UTC
Permalink
I checked the code for gnopernicus as a reference. It is not calling
bonobo_main and calls gtk_main as well. I am wondering how their
bonobo callbacks work?
Any ideas?
Post by Aditya Pandey
I have written a plugin to a gtk+ application [so it has already
started gtk_main].
My plugin integrates with gnome-speech which requires bonobo.
For some speech drivers which support callbacks, I need to do
bonobo_main. But since gtk_main is already running, it somehow
interferes with that (I understand that gtk_main_quit is called by
bonobo_main). By interference I mean that dialogs
fail to respond to ok event to close themselves when bonobo_main is called etc.
Is there some alternative to bonobo_main ? I cant replace gtk_main
code and neither can I make sure that gtk_main [part of main app] is
called after bonobo_main [part of plugin].
1. since bonobo_main calls gtk_main_quit, I do gtk_main after it again [Not sure
if this is the correct method, looks like a hack to me].
2. is bonobo_ui_main for this situation?
Please advise/suggest on which way to go.
--
Aditya Kumar Pandey
----------------------------------------
Contact: +91-9868263500; +91-11-25165432
--
Aditya Kumar Pandey
----------------------------------------
Contact: +91-9868263500; +91-11-25165432
Nickolay V. Shmyrev
2005-04-04 15:11:32 UTC
Permalink
bonobo_main is just a wrapper on GMainLoop creation and start. It only activates bonobo with bonobo_activate and then start main loop. So as gtk_main.
Moreover some developers want to deprecate even gtk_main in the future, and use the replacement from glib.

So you can just use gtk_main, everything will work. But don't forget to call bonobo_activate first.
Post by Aditya Pandey
I checked the code for gnopernicus as a reference. It is not calling
bonobo_main and calls gtk_main as well. I am wondering how their
bonobo callbacks work?
Any ideas?
Post by Aditya Pandey
I have written a plugin to a gtk+ application [so it has already
started gtk_main].
My plugin integrates with gnome-speech which requires bonobo.
For some speech drivers which support callbacks, I need to do
bonobo_main. But since gtk_main is already running, it somehow
interferes with that (I understand that gtk_main_quit is called by
bonobo_main). By interference I mean that dialogs
fail to respond to ok event to close themselves when bonobo_main is called etc.
Is there some alternative to bonobo_main ? I cant replace gtk_main
code and neither can I make sure that gtk_main [part of main app] is
called after bonobo_main [part of plugin].
1. since bonobo_main calls gtk_main_quit, I do gtk_main after it again [Not sure
if this is the correct method, looks like a hack to me].
2. is bonobo_ui_main for this situation?
Please advise/suggest on which way to go.
--
Aditya Kumar Pandey
----------------------------------------
Contact: +91-9868263500; +91-11-25165432
--
Aditya Kumar Pandey
----------------------------------------
Contact: +91-9868263500; +91-11-25165432
_______________________________________________
gnome-components-list mailing list
http://mail.gnome.org/mailman/listinfo/gnome-components-list
Aditya Pandey
2005-04-05 10:59:32 UTC
Permalink
Thanks for replying. I have added call for bonobo_activate, and it
returns true (poa manager is initialized). Still, I cant receive
notification/callback.

Situation:
gtk_main runs already by main application (remember my program is a
plugin that does bonobo activation), I am not able to receive callback
notification.

Is this order of calls necessary -- first bonobo_activate and then
start listening loop (like of gtk_main) ?

Could there be some work around in my situation?
Post by Nickolay V. Shmyrev
bonobo_main is just a wrapper on GMainLoop creation and start. It only activates bonobo with bonobo_activate and then start main loop. So as gtk_main.
Moreover some developers want to deprecate even gtk_main in the future, and use the replacement from glib.
So you can just use gtk_main, everything will work. But don't forget to call bonobo_activate first.
Post by Aditya Pandey
I checked the code for gnopernicus as a reference. It is not calling
bonobo_main and calls gtk_main as well. I am wondering how their
bonobo callbacks work?
Any ideas?
Post by Aditya Pandey
I have written a plugin to a gtk+ application [so it has already
started gtk_main].
My plugin integrates with gnome-speech which requires bonobo.
For some speech drivers which support callbacks, I need to do
bonobo_main. But since gtk_main is already running, it somehow
interferes with that (I understand that gtk_main_quit is called by
bonobo_main). By interference I mean that dialogs
fail to respond to ok event to close themselves when bonobo_main is called etc.
Is there some alternative to bonobo_main ? I cant replace gtk_main
code and neither can I make sure that gtk_main [part of main app] is
called after bonobo_main [part of plugin].
1. since bonobo_main calls gtk_main_quit, I do gtk_main after it again [Not sure
if this is the correct method, looks like a hack to me].
2. is bonobo_ui_main for this situation?
Please advise/suggest on which way to go.
--
Aditya Kumar Pandey
----------------------------------------
Contact: +91-9868263500; +91-11-25165432
--
Aditya Kumar Pandey
----------------------------------------
Contact: +91-9868263500; +91-11-25165432
_______________________________________________
gnome-components-list mailing list
http://mail.gnome.org/mailman/listinfo/gnome-components-list
--
Aditya Kumar Pandey
----------------------------------------
Contact: +91-9868263500; +91-11-25165432
Nickolay V. Shmyrev
2005-04-05 14:36:06 UTC
Permalink
Post by Aditya Pandey
Thanks for replying. I have added call for bonobo_activate, and it
returns true (poa manager is initialized). Still, I cant receive
notification/callback.
gtk_main runs already by main application (remember my program is a
plugin that does bonobo activation), I am not able to receive callback
notification.
Is this order of calls necessary -- first bonobo_activate and then
start listening loop (like of gtk_main) ?
Could there be some work around in my situation?
You are not very clear here. What callback notification are you talking
about. What are you really trying to implement? Detailed description or
small test example would help much better.
Aditya Pandey
2005-04-07 05:53:00 UTC
Permalink
Thanks for replying and helping me.

The idea of making a small test program testing this stuff was good
--as my plugin is a bit big now. I have solved the problem (it was a
stupid mistake in my code).

What I can contribute is a small test gtk version of test-speech
included with gnome-speech. I wrote it to check if bonobo_activate and
gtk_main (done in any order) work to provide callbacks.

build script (makefiles and pkgconfigs are better, but this prog is
more like a proof of concept):

g++ gtk_test-speech.cpp callback.c -o test -g -Wall -DORBIT2=1
-pthread -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include
-I/usr/include/atk-1.0 -I/usr/include/pango-1.0 -I/usr/X11R6/include
-I/usr/include/freetype2 -I/usr/include/glib-2.0
-I/usr/lib/glib-2.0/include -Wl,--export-dynamic -lgtk-x11-2.0
-lgdk-x11-2.0 -latk-1.0 -lgdk_pixbuf-2.0 -lm -lpangoxft-1.0
-lpangox-1.0 -lpango-1.0 -lgobject-2.0 -lgmodule-2.0 -ldl -lglib-2.0
-Wl,--export-dynamic -lbonobo-2 -lbonobo-activation -lORBit-2
-lgnomespeech -L/usr/local/lib -I/usr/local/include/gnome-speech-1.0
-I/usr/include/bonobo-activation-2.0 -I/usr/include/glib-2.0
-I/usr/lib/glib-2.0/include -I/usr/include/orbit-2.0
-I/usr/include/linc-1.0 -I/usr/include/libbonobo-2.0
-I/usr/local/include/gnome-speech-1.0 -Wl,--export-dynamic -pthread
-lgthread-2.0



gtk_test-speech.cpp:

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <glib/gmain.h>
#include <libbonobo.h>
#include <gnome-speech/gnome-speech.h>
#include "callback.h"
#include <gtk/gtk.h>

GtkWidget * app, *window, * dialog, * push1, * push2, * layout1, * layout2;
GNOME_Speech_Speaker speaker;
GNOME_Speech_VoiceInfoList *voices;
CORBA_Object o;
Callback *cb = NULL;
BonoboObject * cbObject;

static void callback_test (GNOME_Speech_Speaker s);
static CORBA_Object select_server ();
static int setup_speaker (GNOME_Speech_SynthesisDriver driver,
CORBA_Environment *ev);

static void setup_bonobo ()
{
/*initialise bonobo*/
CORBA_Environment ev;
CORBA_exception_init (&ev);
if (!bonobo_init (NULL, NULL))
{
g_error ("Can't initialize Bonobo...\n");
}
CORBA_exception_free (&ev);
if (!bonobo_activate ())
fprintf (stderr, "Bonobo activation failed");


o = select_server ();
if (o == CORBA_OBJECT_NIL)
{
printf ("No server selected.\n");
return ;
}

/* Atempt to initialize the server/driver and speaker */
CORBA_exception_init (&ev);
if (GNOME_Speech_SynthesisDriver_driverInit (o, &ev))
setup_speaker (o, &ev);
CORBA_exception_free (&ev);

}

void push1_activate(GtkButton * wid, gpointer data)
{
setup_bonobo();

/*start callback test*/
callback_test (speaker);
printf("Button 1 clicked\n");
}

void push2_activate(GtkButton * wid, gpointer data)
{
/* if (cb)
bonobo_object_unref (cb);
*/
bonobo_object_release_unref (o, NULL);
bonobo_object_release_unref (speaker, NULL);
CORBA_free (voices);
bonobo_debug_shutdown ();
// bonobo_main_quit();
gtk_main_quit();
printf("Button 2 clicked\n");
}


static void
speaker_say_print (GNOME_Speech_Speaker speaker,
const char *format,
...)
{
va_list args;
char *str;
CORBA_Environment ev;

va_start (args, format);

CORBA_exception_init (&ev);

str = g_strdup_vprintf (format, args);
printf ("%s", str);
GNOME_Speech_Speaker_say (speaker, str, &ev);
if (BONOBO_EX (&ev))
printf ("Exception %s writing '%s'",
bonobo_exception_get_text (&ev), str);
g_free (str);

CORBA_exception_free (&ev);

va_end (args);
}

static CORBA_Object
select_server ()
{

int i;
Bonobo_ServerInfoList *servers;
Bonobo_ServerInfo *info;
char input[81];
CORBA_Object rv;

CORBA_Environment ev;
CORBA_exception_init (&ev);
servers = bonobo_activation_query (
"repo_ids.has_one (['IDL:GNOME/Speech/SynthesisDriver:0.2',
'IDL:GNOME/Speech/SynthesisDriver:0.3'])",
NULL, &ev);
if (BONOBO_EX (&ev)) {
return CORBA_OBJECT_NIL;
}
CORBA_exception_free (&ev);

if (!servers)
{
return CORBA_OBJECT_NIL;
}
for (i = 0; i < servers->_length; i++)
{
info = &servers->_buffer[i];
printf ("%d: %s\n", i+1, info->iid);
}
// printf ("\nSelect a server: ");
//fgets (input, 80, stdin);
i = 0;//atoi (input);
//i--;
if (i < 0 || i > servers->_length)
{
printf ("Server not found.\n");
CORBA_free (servers);
return CORBA_OBJECT_NIL;
}
info = &servers->_buffer[i];
printf ("Atempting to activate %s.\n", info->iid);

CORBA_Environment ev2;
CORBA_exception_init (&ev2);
rv = bonobo_activation_activate_from_id (
(const Bonobo_ActivationID) info->iid,
0, NULL, &ev);
CORBA_free (servers);

CORBA_exception_free (&ev2);
return rv;
}

static void
speech_interrupt_test (GNOME_Speech_Speaker s)
{
gint i;
gint delay;
CORBA_Environment ev;

CORBA_exception_init (&ev);

for (i = 0; i < 100; i++) {
gchar *str = g_strdup_printf ("Test %d\n", i);
GNOME_Speech_Speaker_say (s, str, &ev);
g_free (str);
delay = rand ()%1000000;
usleep (delay);
GNOME_Speech_Speaker_stop (s, &ev);
fprintf (stderr, "%d\r", i);
}
CORBA_exception_free (&ev);
}


static void
wait_test (GNOME_Speech_Speaker s)
{
CORBA_Environment ev;

CORBA_exception_init (&ev);
GNOME_Speech_Speaker_wait (s, &ev);
if (BONOBO_EX (&ev)) {
speaker_say_print (s, "This driver doesn't know how to wait.\n");
return;
}
speaker_say_print (s, "Speech Wait Test.\n");
speaker_say_print (s, "Peter Piper picked a peck of pickled peppers.
If Peter Piper picked a peck of pickled peppers, how many pecks of
pickled peppers did Peter Piper pick?\n");
GNOME_Speech_Speaker_wait (s, &ev);
CORBA_exception_free (&ev);
}


static void
parameter_tests (GNOME_Speech_Speaker speaker)
{
GNOME_Speech_ParameterList *list;
int i;
gboolean done = FALSE;
CORBA_Environment ev;

CORBA_exception_init (&ev);
list = GNOME_Speech_Speaker_getSupportedParameters (speaker, &ev);
if (BONOBO_EX (&ev) || list->_length == 0) {
if (!BONOBO_EX (&ev))
CORBA_free (list);
speaker_say_print (speaker, "No parameters supported.\n");
CORBA_exception_free (&ev);
return;
}

speaker_say_print (speaker, "%d parameters supported.\n", list->_length);
for (i = 0; i < list->_length; i++) {
GNOME_Speech_Parameter *p = &(list->_buffer[i]);

speaker_say_print (speaker, "parameter %d: %s.\n", i + 1, p->name);
speaker_say_print (speaker, " minimum value: %.2lf.\n", p->min);
speaker_say_print (speaker, " current value: %.2lf.\n", p->current);
speaker_say_print (speaker, " maximum value: %.2lf.\n", p->max);
speaker_say_print (speaker, " %s\n",
(p->enumerated) ? "Is enumerated."
: "Is not enumerated.");
}

while (!done) {
gchar input[81];
gint choice;
gdouble new_value;
GNOME_Speech_Parameter *p;

speaker_say_print (speaker, "Parameter to change (1 through %d, "
"0 to exit): ", list->_length);
fgets (input, 80, stdin);
choice = atoi(input);
if (choice == 0) {
done = 1;
break;
}
p = &(list->_buffer[choice-1]);
GNOME_Speech_Speaker_stop (speaker, &ev);
speaker_say_print (speaker, "new %s? %.2lf through %.2lf.",
p->name, p->min, p->max);
fgets (input, 80, stdin);
new_value = atof (input);
printf ("Setting to %lf.\n", new_value);
GNOME_Speech_Speaker_setParameterValue (speaker, p->name, new_value, &ev);
}

CORBA_free (list);
CORBA_exception_free (&ev);
}


static void
callback_test (GNOME_Speech_Speaker s)
{
cb = callback_new ();
cbObject = BONOBO_OBJECT (cb);
CORBA_Environment ev;
gboolean res;

if (!s) {
fprintf (stderr, "Speaker not initialised before callback test\n");
return;
}

CORBA_exception_init (&ev);
res = GNOME_Speech_Speaker_registerSpeechCallback (s,
bonobo_object_corba_objref (cbObject), &ev);
if (!res || BONOBO_EX (&ev)) {
speaker_say_print (s, "This driver does not support callbacks.\n");
bonobo_object_unref (cb);
CORBA_exception_free (&ev);
return;
}
speaker_say_print (s,"Now is the time for all good men to come to the
aid of their country. Now is the time for all good men to come to the
aid of their country. Now is the time for all good men to come to the
aid of their country. Now is the time for all good men to come to the
aid of their country. Now is the time for all good men to come to the
aid of their country.\n");
//bonobo_main ();
CORBA_exception_free (&ev);
}



static int
setup_speaker (GNOME_Speech_SynthesisDriver driver, CORBA_Environment *ev)
{
char input[81];
int choice = -1;
CORBA_string driver_name, driver_version;
CORBA_string synth_name, synth_version;
gint i;

driver_name = GNOME_Speech_SynthesisDriver__get_driverName (driver, ev);
if (!BONOBO_EX (ev))
{
printf ("Driver name: %s\n", driver_name);
CORBA_free (driver_name);
}
driver_version = GNOME_Speech_SynthesisDriver__get_driverVersion (driver, ev);
if (!BONOBO_EX (ev))
{
printf ("Driver version: %s\n", driver_version);
CORBA_free (driver_version);
}
synth_name = GNOME_Speech_SynthesisDriver__get_synthesizerName (driver, ev);
if (!BONOBO_EX (ev))
{
printf ("Synthesizer name: %s\n", synth_name);
CORBA_free (synth_name);
}
synth_version = GNOME_Speech_SynthesisDriver__get_synthesizerVersion
(driver, ev);
if (!BONOBO_EX (ev))
{
printf ("Synthesizer Version: %s\n", synth_version);
CORBA_free (synth_version);
}

/* Display list of voices */

voices = GNOME_Speech_SynthesisDriver_getAllVoices (driver, ev);
if (BONOBO_EX (ev)) {
fprintf (stderr, "Exception %s getting voice list.\n",
bonobo_exception_get_text (ev));
return -1;
}

if (voices->_length < 1) {
printf ("No voices, bailing out.\n");
return -1;
}

choice =0;
speaker = GNOME_Speech_SynthesisDriver_createSpeaker (driver,
&voices->_buffer[choice],
ev);

if (BONOBO_EX (ev)) {
fprintf (stderr, "Exception %s creating speaker.\n",
bonobo_exception_get_text (ev));
return -1;
}

if (speaker == CORBA_OBJECT_NIL) {
fprintf (stderr, "Error creating speaker.\n");
return -1;
}

for (i = 0; i < voices->_length; i++) {
speaker_say_print (speaker, "%d. %s\n", i+1, voices->_buffer[i].name);
}

/*
speaker_say_print (speaker, "\nPlease select a \"test\".\n\n");
speaker_say_print (speaker, "1. Parameter test.\n");
speaker_say_print (speaker, "2. Callback test.\n");
speaker_say_print (speaker, "3. Speech Interrupt test.\n");
speaker_say_print (speaker, "4. Wait test.\n");
speaker_say_print (speaker, "0. Exit.\n");
printf ("\nSelect test: ");
fgets (input, 80, stdin);
choice = atoi (input);
switch (choice)
{
case 1:
GNOME_Speech_Speaker_stop (speaker, ev);
parameter_tests (speaker);
GNOME_Speech_Speaker_stop (speaker, ev);
break;
case 2:
GNOME_Speech_Speaker_stop (speaker, ev);
callback_test (speaker);
GNOME_Speech_Speaker_stop (speaker, ev);
break;
case 3:
GNOME_Speech_Speaker_stop (speaker, ev);
speech_interrupt_test (speaker);
break;
case 4:
GNOME_Speech_Speaker_stop (speaker, ev);
wait_test (speaker);
break;
case 0:
GNOME_Speech_Speaker_stop (speaker, ev);
GNOME_Speech_Speaker_say (speaker, "Goodbye.", ev);
bonobo_object_release_unref (speaker, NULL);
CORBA_free (voices);
return -1;
}
*/
return 0;
}

main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
app = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request(app, 200, 200);

layout1= gtk_layout_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(app), layout1);

push1 = gtk_button_new_with_label("throw dialog");
g_signal_connect(G_OBJECT(push1), "clicked", G_CALLBACK(push1_activate), NULL);
gtk_layout_put(GTK_LAYOUT(layout1), push1, 5, 5);

push2 = gtk_button_new_with_label("quit");
g_signal_connect(G_OBJECT(push2), "clicked", G_CALLBACK(push2_activate), NULL);
gtk_layout_put(GTK_LAYOUT(layout1), push2, 115, 5);

gtk_widget_show(push1);
gtk_widget_show(push2);
gtk_widget_show(layout1);
gtk_widget_show(app);

/*initialise event loop*/
// bonobo_main();
gtk_main();


return 0;
}

Loading...