Re: [gtk-list] gpointer and callbacks




dross writes: 
> 
> I'm trying to write a simple audio interface, and am setting sliders to
> adjust the volume and mic levels. I have a callback function that I would
> like to pass an integer identifier, identifing the device I want to change
> the level for. The problem is I can't seem to pass an integer to this function.
> 
> I am new to gtk and I don't quite understand what a gpointer is, and in all the
> 
> examples I found only strings where used. Can you pass any type using gpointer?
>

cool -- I'm working on an audio interface as well, and I came across this
problem.  gpointer is a "generic pointer" that seems to behave exactly
like (void *).  So you can use any kind of pointer for the data parameter
and in the gtk_signal_connect function call, as long as they match.
In your case, the call should be (assuming device is an int):

 gtk_signal_connect (GTK_OBJECT (vol_adj),
    "value_changed",  GTK_SIGNAL_FUNC (adjust_level), (gpointer)&device);

as you have it and then the callback should be defined:

void adjust_level (GtkAdjustment *adj, int *pdevice);

As you expand functionality, you'll probably want to pass more than 
just device id to the callback.  For example, if you don't trust that
the callback is triggered only when the value is changed, you'll 
want to store the last value in the data parameter and then execute
the callback code only when the value really has changed, for better
efficiency.  You can do this by wrapping the id and value in a struct, 
such as:

typedef struct control {
   int device;
   float volume;
} control;

Then you pass something of type (control *) in gtk_signal_connect,
and define the callback as

void adjust_level(GtkAdjustment *adj, control *pc) {
   // declarations
   if (pc->value != adj->value) {
      // execute callback code
      pc->value = adj->value;
   }
}

Hope this helps, and good luck!
--Harvey

ps. Hey -- how are you solving the problem of getting gtk_main() and the
audio loop to run concurrently (sharing the same memory space)?  I
haven't a clue about how to do this simply. I have everything set up
so the GUI and the audio processing stuff send the right messages
but getting both main()'s to run at the same time is a big ?.   I've 
looked at some documentation about unix threads but it all seems very 
complicated.  I'm not implementing an OS, just writing an audio 
application!



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