Rockbox Development > Starting Development and Compiling

Dynamic text in menu items (was: something related to rb->snprintf and menus)

(1/2) > >>

gbl08ma:
I'm developing a ID3 tag editor for Rockbox (in the future it might support other tags), and to display the file and tag information I'm trying to use the menu API.

The idea is: The tag is displayed in a menu item, and when a user selects that menu item, the vkeyboard opens so user can modify the tag. For now, as my plugin is still a stub, it only has code for showing the ID3v1 info of an opened file (the plugin is a viewer). Taking into account my C coding (in)ability, it's obvious the result would not be something that compiles with errors. :)

Here's the part of the code of the plugin which is causing the build to fail:

--- Code: ---    /* get tag info from new_id3v1 and display it*/
    char* title_display;
    rb->snprintf(title_display, 37, "Title: %s", new_id3v1.title);
    char* artist_display;
    rb->snprintf(artist_display, 38, "Artist: %s", new_id3v1.artist);
    char* album_display;
    rb->snprintf(album_display, 37, "Album: %s", new_id3v1.album);
    char* year_display;
    rb->snprintf(year_display, 10, "Year: %s", new_id3v1.year);
    char* comment_display;
    rb->snprintf(comment_display, 37, "Comment: %s", new_id3v1.comment);
    MENUITEM_STRINGLIST(edittags_menu, "Select a tag to edit", NULL,         /* this is the line 229 */
                        title_display, artist_display, album_display,
                        year_display, comment_display, "Save", "Return",);
    switch (rb->do_menu(&edittags_menu, NULL, NULL, false))
    {
        default: /* no matter what the user selects, we will always return back */
            return true;
    }

--- End code ---
and, on another subroutine

--- Code: ---    char* filename_display;
    rb->snprintf(filename_display, MAX_PATH, "Filename: %s", opened_file);
    char* tagtype_display;
    tagtype_display = "Tag type: ID3v1"; /*in the future this will show the real
                                        tag type; for now it's a placeholder. */
    MENUITEM_STRINGLIST(fileinfo_menu, "File information", NULL,              /* this is the line 197 */
                        filename_display, tagtype_display, "Return",);
    switch (rb->do_menu(&fileinfo_menu, NULL, NULL, false))
    {
        default: /* no matter what the user selects, we will always return back */
        return true;
    }

--- End code ---

(the whole file is attached)

This is the output of make:

--- Code: ---gabriel@somePC:~/rockbox/build$ make
CC apps/plugins/id3_editor.c
/home/gabriel/rockbox/apps/plugins/id3_editor.c: In function ‘show_file_info’:
/home/gabriel/rockbox/apps/plugins/id3_editor.c:197: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:197: error: (near initialization for ‘fileinfo_menu_[0]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:197: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:197: error: (near initialization for ‘fileinfo_menu_[1]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c: In function ‘show_edittags_menu’:
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: (near initialization for ‘edittags_menu_[0]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: (near initialization for ‘edittags_menu_[1]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: (near initialization for ‘edittags_menu_[2]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: (near initialization for ‘edittags_menu_[3]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: (near initialization for ‘edittags_menu_[4]’)
make: ** [/home/gabriel/rockbox/build/apps/plugins/id3_editor.o] Erro 1
gabriel@somePC:~/rockbox/build$
gabriel@GabrielUbuntuDesktop:~/rockbox/build$ make
CC apps/plugins/id3_editor.c
/home/gabriel/rockbox/apps/plugins/id3_editor.c: In function ‘show_file_info’:
/home/gabriel/rockbox/apps/plugins/id3_editor.c:197: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:197: error: (near initialization for ‘fileinfo_menu_[0]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:197: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:197: error: (near initialization for ‘fileinfo_menu_[1]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c: In function ‘show_edittags_menu’:
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: (near initialization for ‘edittags_menu_[0]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: (near initialization for ‘edittags_menu_[1]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: (near initialization for ‘edittags_menu_[2]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: (near initialization for ‘edittags_menu_[3]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:229: error: (near initialization for ‘edittags_menu_[4]’)
make: ** [/home/gabriel/rockbox/build/apps/plugins/id3_editor.o] Erro 1
gabriel@GabrielUbuntuDesktop:~/rockbox/build$

--- End code ---

I think that this errors are because I'm trying to use not-constant variables on MENUITEM_STRINGLIST, at least it's what it seems, because on another menu I'm using only strings and no variables, this way:

--- Code: ---MENUITEM_STRINGLIST(main_menu, "Tag editor", NULL, "File information",
                        "Edit tags", "Exit",);

--- End code ---
...and it doesn't show any error on the compiler.

If, like I think, it's because I'm trying to make menu items out of char* variables that were mixed with others using rb->snprintf, then how can I achieve the same effect ("Title: One Song", "Artist: Some Artist", etc.) using menus, or if menus can't be used, some other way to display data where user can scroll to the tag s/he wants to edit.

Anticipated thanks
Gabriel

PS: about the uploaded file, most likely the code is ugly, I didn't check if it follows the coding guidelines (I think so), and certainly contains more errors than what the compiler reports; I played a bit with C and C++ two years ago for curiosity, but in the meantime I forgot most of the few things I learned... I hope that with Rockbox I can definitely learn C again.
PS2: I'll create a patch in the tracker when I think it's worth, that is, when I get something that compiles.

bluebrother:

--- Code: ---    /* get tag info from new_id3v1 and display it*/
    char* title_display;
    rb->snprintf(title_display, 37, "Title: %s", new_id3v1.title);

--- End code ---

This is wrong -- snprint() doesn't allocate the buffer it writes the output data to. You need to allocate the memory yourself you're pointing snprintf() to.

Haven't looked into other issues yet.

gbl08ma:
Thanks for your help...
I changed the variables from char* to char[10], where 10 is the size of the buffer. Also, replaced

--- Code: ---tagtype_display = "Tag type: ID3v1"
--- End code ---
for

--- Code: ---rb->snprintf(tagtype_display, 15, "Tag type: ID3v1");
--- End code ---
and the build result is...

--- Code: ---gabriel@somePC:~/rockbox/build$ make
CC apps/plugins/id3_editor.c
/home/gabriel/rockbox/apps/plugins/id3_editor.c: In function ‘show_file_info’:
/home/gabriel/rockbox/apps/plugins/id3_editor.c:207: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:207: error: (near initialization for ‘fileinfo_menu_[0]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:207: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:207: error: (near initialization for ‘fileinfo_menu_[1]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c: In function ‘show_edittags_menu’:
/home/gabriel/rockbox/apps/plugins/id3_editor.c:239: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:239: error: (near initialization for ‘edittags_menu_[0]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:239: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:239: error: (near initialization for ‘edittags_menu_[1]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:239: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:239: error: (near initialization for ‘edittags_menu_[2]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:239: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:239: error: (near initialization for ‘edittags_menu_[3]’)
/home/gabriel/rockbox/apps/plugins/id3_editor.c:239: error: initializer element is not constant
/home/gabriel/rockbox/apps/plugins/id3_editor.c:239: error: (near initialization for ‘edittags_menu_[4]’)
make: ** [/home/gabriel/rockbox/build/apps/plugins/id3_editor.o] Erro 1

--- End code ---

The line numbers of the error are different because I added some extra documentation comments at the start of the file.

Attached goes my modified id3_editor.c.

bluebrother:
From a quick look into menu.h it doesn't look like you're using the correct macro -- MENUITEM_RETURNVALUE_DYNTEXT sounds like a better match to me. But I'm not familiar with that code and haven't checked right now so I might also be wrong. However, I suggest checking menu.h :)

gbl08ma:
I used the code that was on the wiki page about how to use the menu API, perhaps it is wrong or I modified it in some wrong way.

Navigation

[0] Message Index

[#] Next page

Go to full version