Re: [gtk-list] Re: gpointer and callbacks



Thanks Petter,

Your explanation has made everything much clearer. I made 2 global constants as IDs
for the devices, and I passed these to the callback function and it seems to work
now - does this look ok?

const int  MIXER_VOLUME = SOUND_MIXER_VOLUME, MIXER_MIC = SOUND_MIXER_MIC;

void setup_window (GtkWidget * parent)
{
    .
    .

  device = SOUND_MIXER_VOLUME;
  volume = get_level(device);
  vol_adj = (GtkAdjustment*)gtk_adjustment_new ((volume & 0x000000ff), 0, 100, 1, 1,
0);
  hscale = gtk_hscale_new (GTK_ADJUSTMENT (vol_adj));
  gtk_signal_connect (GTK_OBJECT (vol_adj), "value_changed",
        GTK_SIGNAL_FUNC (adjust_level), (gpointer)&MIXER_VOLUME);
    .
    .
}

Also is *(int*)data the right way to de-reference the pointer.

Thanks to everyone who has responded. My C is a bit rusty, so I often get myself in
a mess with pointers.

Darryl Ross

Petter Gustad wrote:

> From: dross <dross@melbpc.org.au>
> Subject: [gtk-list] gpointer and callbacks
> Date: Wed, 14 Oct 1998 20:08:26 +0000
>
> >
> >
> > 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?
>
> gpointer is a generic pointer. You could cast it to whatever data
> structure pointer you're pointing to.
>
> You can pass data which are smaller than a pointer in the pointer
> itself (potential portability problem warning):
>
> void adjust_level (GtkAdjustment * adj, gpointer data)
> {
>     int someint = (int)data;
> ...
>
> void setup_window (GtkWidget * parent)
> {
>    int device
> ...
>    hscale = gtk_hscale_new (GTK_ADJUSTMENT (vol_adj));
>    gtk_signal_connect (GTK_OBJECT (vol_adj), "value_changed",
>                        GTK_SIGNAL_FUNC (adjust_level), (gpointer)device);
>
> Your code looks like a time-bomb since you pass a pointer to data on
> the stack (device). When your setup_window function returns your stack
> frame is likely to be overwritten. You should either declare "device"
> as static (now your code is not reentrant) or allocate it dynamically,
> but then you have to remember to deallocate, otherwise your
> application might leak memory.
>
> If you use a pointer to an int you could (using a static variable):
>
> void adjust_level (GtkAdjustment * adj, gpointer data)
> {
>     int device = *(int*)data;
> ...
>
> void setup_window (GtkWidget * parent)
> {
> ...
>    static int device;
>
>    device = ...
>
>    gtk_signal_connect (GTK_OBJECT (mic_adj), "value_changed",
>                        GTK_SIGNAL_FUNC (adjust_level), (gpointer)&device);
>
> Examples are not compiled and might of course contain errors...
>
> Petter
> --
> ________________________________________________________________________
> Petter Gustad     8'h2B | (~8'h2B) - Hamlet     http://home.sol.no/~pegu
> #include <stdio.h>/* compile/run this program to get my email address */
> int main(void) {printf ("pegu\100computer\056org\nmy opinions only\n");}
>
> --
> To unsubscribe: mail -s unsubscribe gtk-list-request@redhat.com < /dev/null



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