You are not logged in.

#1 2013-04-09 17:52:45

Unia
Member
From: The Netherlands
Registered: 2010-03-30
Posts: 2,470
Website

[SOLVED] Unused parameter, but program won't function without it?

Hey all,

I've come across this situation a few times before but I have always ignored it because I came up with a different approach. This time though my curiosity got the better of me (and I don't yet have another approach tongue) so I want to know why this happens.

I have my program here: GXMS. When you compile it, it says:

gxms.c: In function ‘keypress’:
gxms.c:38:31: let op: unused parameter ‘widget’ [-Wunused-parameter]
 gboolean keypress (GtkWidget *widget, GdkEventKey *event) {

The thing is, when I remove that so-called "unused" parameter, the program doesn't function anymore! This function is supposed to bring up the searchbar, but it does not anymore. Here is the function:

gboolean keypress (GtkWidget *widget, GdkEventKey *event) {
    guint(g) = event->keyval;

    if ((event->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK) {
        switch (g) {
            case GDK_KEY_f:
                show_search(TRUE);
                return TRUE;
                break;
            default:
                return FALSE;
                break;
        }
    }
    if (gtk_widget_has_focus (searchbar)) {
        if (g == GDK_KEY_Escape) {
            show_search (FALSE);
            gtk_widget_grab_focus (view);
            return TRUE;
        }
        if ((g == GDK_KEY_Return) && (event->state & GDK_MOD1_MASK) == GDK_MOD1_MASK)
            search ();
    }
    return FALSE;
}

When I define *widget inside the function, like below, it doesn't work anymore either:

gboolean keypress (GdkEventKey *event) {
    GtkWidget *widget;

    <snip>

Is this a bug in my code, in the compiler, or just one of my mistakes?

Last edited by Unia (2013-04-09 18:39:42)


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#2 2013-04-09 18:07:31

SahibBommelig
Member
From: Germany
Registered: 2010-05-28
Posts: 80

Re: [SOLVED] Unused parameter, but program won't function without it?

You likely pass keypress() as a callback to a function, do you? Well, this function will call your function with 2 parameters. If your function can take only one bad things happen. If you're lucky your compiler may warn you, but don't rely on that - just look up what the callback is supposed to look like.  The Gtk+ Docs will tell you for example.

To suppress the warning you can do either:

// Poor man's approach
void func(int arg) {
    (void) arg;
}
// Place this in some header
#if !defined(SPARSE) && defined(__GNUC__)
   #define unused __attribute__((unused))
#else 
   #define unused
#endif


void func(unused int arg) {
}

See also this.

Offline

#3 2013-04-09 18:19:59

Unia
Member
From: The Netherlands
Registered: 2010-03-30
Posts: 2,470
Website

Re: [SOLVED] Unused parameter, but program won't function without it?

I call keypress() from within the create_window function, like so:

g_signal_connect (window, "key-press-event", G_CALLBACK(keypress), NULL);

AFAIK, that doesn't actually pass any parameters to keypress().


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#4 2013-04-09 18:24:54

SahibBommelig
Member
From: Germany
Registered: 2010-05-28
Posts: 80

Re: [SOLVED] Unused parameter, but program won't function without it?

You seem to have trouble to understand callbacks/gtk's event system it seems. smile

You don't call keypress in this snippet. You pass it as a function pointer to g_signal_connect.
G_CALLBACK is just a macro that casts your function pointer into the GCallback Type, it does not call the function.
keypress() gets always called once "key-press-event" happens.

Offline

#5 2013-04-09 18:27:46

Unia
Member
From: The Netherlands
Registered: 2010-03-30
Posts: 2,470
Website

Re: [SOLVED] Unused parameter, but program won't function without it?

SahibBommelig wrote:

You seem to have trouble to understand callbacks/gtk's event system it seems. smile

You don't call keypress in this snippet. You pass it as a function pointer to g_signal_connect.
G_CALLBACK is just a macro that casts your function pointer into the GCallback Type, it does not call the function.
keypress() gets always called once "key-press-event" happens.

I had that vague idea, yes tongue But I'm still not getting why I need that "unused" variable *widget. My keypress() function does not use it, but it won't work without it.


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#6 2013-04-09 18:31:59

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,012

Re: [SOLVED] Unused parameter, but program won't function without it?

Looks to me like you need to write your keypress function a little differently, but it's been ages since I've done any gtk programming.
Something like:
if widget == searchbar
    do some stuff
else if widget == view
    do other stuff


You're just jealous because the voices only talk to me.

Offline

#7 2013-04-09 18:35:57

SahibBommelig
Member
From: Germany
Registered: 2010-05-28
Posts: 80

Re: [SOLVED] Unused parameter, but program won't function without it?

Well, let's summarize:

  • ...your callback is registered by g_signal_connect, by passing a function pointer to it.

  • ...your applications enters the gtk_main_loop() at some point.

  • ...when the mainloop notices a key-press-event on the widget you registered the callback on, it will take your registered callback and call it with two parameters: The widget the event happened on, and the event that happened.

  • ...The reason you have to take 2 arguments: Gtk+ demands it. If your function doesn't take 2 arguments the best thing that will happen is a crash.

Offline

#8 2013-04-09 18:38:41

Unia
Member
From: The Netherlands
Registered: 2010-03-30
Posts: 2,470
Website

Re: [SOLVED] Unused parameter, but program won't function without it?

Moetunes: Indeed, that works smile

gboolean keypress (GtkWidget *widget, GdkEventKey *event) {
    guint(g) = event->keyval;

    if ((event->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK) {
        switch (g) {
            case GDK_KEY_f:
                show_search(TRUE);
                return TRUE;
                break;
            default:
                return FALSE;
                break;
        }
    }
+   if (widget != searchbar) {
        if (gtk_widget_has_focus (searchbar)) {
            if (g == GDK_KEY_Escape) {
                show_search (FALSE);
                gtk_widget_grab_focus (view);
                return TRUE;
            }
            if ((g == GDK_KEY_Return) && (event->state & GDK_MOD1_MASK) == GDK_MOD1_MASK)
                search ();
        }
+   }
    return FALSE;
}

SahibBommelig: And thanks to you too! Now I know why the above works and I understand the system I'm using a bit more!

Thanks guys!


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#9 2013-04-09 18:41:01

SahibBommelig
Member
From: Germany
Registered: 2010-05-28
Posts: 80

Re: [SOLVED] Unused parameter, but program won't function without it?

One last thing: You don't need this check actually, since you could most likely do:

g_signal_connect (searchbar, "key-press-event", G_CALLBACK(keypress), NULL);

Offline

#10 2013-04-09 18:43:14

Unia
Member
From: The Netherlands
Registered: 2010-03-30
Posts: 2,470
Website

Re: [SOLVED] Unused parameter, but program won't function without it?

SahibBommelig wrote:

One last thing: You don't need this check actually, since you could most likely do:

g_signal_connect (searchbar, "key-press-event", G_CALLBACK(keypress), NULL);

I cant, since I need to capture CTRL+F in view, and not in searchbar, right?


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#11 2013-04-09 18:46:39

SahibBommelig
Member
From: Germany
Registered: 2010-05-28
Posts: 80

Re: [SOLVED] Unused parameter, but program won't function without it?

Well, okay. Misinterpreted your code then.

Offline

#12 2013-04-09 18:59:16

moetunes
Member
From: A comfortable couch
Registered: 2010-10-09
Posts: 1,012

Re: [SOLVED] Unused parameter, but program won't function without it?

The main point being you have multiple widgets defined so you have to do something to check which one the keypress comes from.


You're just jealous because the voices only talk to me.

Offline

#13 2013-04-09 19:09:26

Unia
Member
From: The Netherlands
Registered: 2010-03-30
Posts: 2,470
Website

Re: [SOLVED] Unused parameter, but program won't function without it?

moetunes wrote:

The main point being you have multiple widgets defined so you have to do something to check which one the keypress comes from.

Exactly, I get that now smile


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#14 2013-04-10 07:30:25

Boogie
Member
Registered: 2012-06-17
Posts: 27

Re: [SOLVED] Unused parameter, but program won't function without it?

Is it really necessary to break; switch statement. you return just before break?

    if ((event->state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK) {
        switch (g) {
            case GDK_KEY_f:
                show_search(TRUE);
                return TRUE;
                break;               // This is not needed
            default:
                return FALSE;
                break;               // This is not needed
        }
    }

Offline

#15 2013-04-10 10:27:32

Unia
Member
From: The Netherlands
Registered: 2010-03-30
Posts: 2,470
Website

Re: [SOLVED] Unused parameter, but program won't function without it?

Will see about that! Thanks for your suggestion smile


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#16 2013-04-10 11:27:25

Unia
Member
From: The Netherlands
Registered: 2010-03-30
Posts: 2,470
Website

Re: [SOLVED] Unused parameter, but program won't function without it?

You were right Boogie, code is updated smile


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

Board footer

Powered by FluxBB