New lua command - file_browser() almost done - need help... - General Discussion and Assistance - CHDK Forum supplierdeeply

New lua command - file_browser() almost done - need help...

  • 5 Replies
  • 2268 Views
New lua command - file_browser() almost done - need help...
« on: 07 / October / 2011, 07:31:31 »
Advertisements
Hello!

In the thread of my text editor philmoz said about the file browser:

Quote
There's a file browser already built into CHDK.
I wonder if it would be better to provide methods to call it from within uBasic / Lua scripts rather than write another one inside your editor script?

So I tried. It was very simple to change core/luascript.c file in the chdk source to provide new function. Changes as follows:

-new includes:
Code: [Select]
#include "gui_fselect.h"
#include "gui.h"
#include "gui_lang.h"

-new functions (I placed them somewhere above register_func, near line 1950) :
Code: [Select]
static void file_browser_selected(const char *fn) {
    state_kbd_script_run = 1;           //browser leaves the keyboard
    script_stack_start();               //continue the script
    //what to do with fn?
}

static int luaCB_file_browser( lua_State* L ) {
    state_kbd_script_run = 0;           //script gives the keyboard for the browser
    gui_fselect_init(LANG_STR_FILE_BROWSER, "A",file_browser_selected);
    lua_pushstring(L, "where can I take the path from?");
  return 1;
}

- anf finally new function in chdk_funcs:
Code: [Select]
   FUNC(file_browser)

I have now the problem. How can I use lua_pushstring to push the fn which is believed to contain the string-path of selected file? At the moment new lua command file_browser() just loads filebrowser and doesn't return fn...
if (2*b || !2*b) {
    cout<<question
}

Compile error: poor Yorick

*

Offline reyalp

  • ******
  • 11919
Re: New lua command - file_browser() almost done - need help...
« Reply #1 on: 09 / October / 2011, 00:51:08 »
You would have to make the script yield (like the wait click or sleep functions), or store the filename in a global or something and provide function check whether the file selection has completed. None of this strikes me as a good idea, but yielding is probably the lesser evil. That means you probably have to add something to action_stack.

Keep in mind the actual gui runs in spytask and the script stuff runs in kbd task.
Don't forget what the H stands for.

*

Offline philmoz

  • *****
  • 3107
    • Photos
Re: New lua command - file_browser() almost done - need help...
« Reply #2 on: 09 / October / 2011, 04:06:45 »
After discussing this with reyalp on IRC, I've modified outsliders code above and this is now available in changeset 1359.

I've also changed it so you pass the startup directory to the file browser from the script - for example:
      file = file_browser("A/CHDK/SCRIPTS")
will invoke the CHDK file browser in the SCRIPTS directory and will return the selected file name to the 'file' variable.

If no file is selected (e.g. Menu is used to exit the file browser) then the return value will be null.

Phil.
CHDK ports:
  sx30is (1.00c, 1.00h, 1.00l, 1.00n & 1.00p)
  g12 (1.00c, 1.00e, 1.00f & 1.00g)
  sx130is (1.01d & 1.01f)
  ixus310hs (1.00a & 1.01a)
  sx40hs (1.00d, 1.00g & 1.00i)
  g1x (1.00e, 1.00f & 1.00g)

Re: New lua command - file_browser() almost done - need help...
« Reply #3 on: 09 / October / 2011, 08:44:46 »
After discussing this with reyalp on IRC, I've modified outsliders code above and this is now available in changeset 1359.

I'm glad to see that;) Thank you! Today I'll simplify the code of my text editor using this new command. I can also add a description of this command on wkia. But all these things I'll do in the evening.
if (2*b || !2*b) {
    cout<<question
}

Compile error: poor Yorick


*

Offline rudi

  • ***
  • 126
  • A590IS_101B, SX260HS_100B
Re: New lua command - file_browser() almost done - need help...
« Reply #4 on: 09 / October / 2011, 14:47:27 »
Good work, thanks.

If file_browser() runs without path then crash my cam. A solution is in CHDK-DE trunk#828.

rudi

*

Offline philmoz

  • *****
  • 3107
    • Photos
Re: New lua command - file_browser() almost done - need help...
« Reply #5 on: 09 / October / 2011, 14:57:53 »
Good work, thanks.

If file_browser() runs without path then crash my cam. A solution is in CHDK-DE trunk#828.

rudi

Thanks for that, added in changeset 1360

Phil.
CHDK ports:
  sx30is (1.00c, 1.00h, 1.00l, 1.00n & 1.00p)
  g12 (1.00c, 1.00e, 1.00f & 1.00g)
  sx130is (1.01d & 1.01f)
  ixus310hs (1.00a & 1.01a)
  sx40hs (1.00d, 1.00g & 1.00i)
  g1x (1.00e, 1.00f & 1.00g)

 

Related Topics