Changeset 1020 for trunk/core/script.c


Ignore:
Timestamp:
01/04/11 05:09:10 (2 years ago)
Author:
reyalP
Message:

more script code refactoring from ultima in http://chdk.setepontos.com/index.php?topic=5793.msg59228#msg59228
minor modifications from the patch - didn't include ubasic error change, include lstate.h only in luascript.c

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/core/script.c

    r979 r1020  
    88#include "script.h" 
    99#include "console.h" 
     10#include "action_stack.h" 
     11#include "luascript.h" 
     12#include "motion_detector.h" 
     13#include "shot_histogram.h" 
     14#include "lang.h" 
     15#include "gui_lang.h" 
     16#include "kbd.h" 
    1017 
    1118//------------------------------------------------------------------- 
     
    7178static char script_params_update[SCRIPT_NUM_PARAMS]; 
    7279static int script_loaded_params[SCRIPT_NUM_PARAMS]; 
     80static long running_script_stack_name = -1; 
     81static int state_lua_kbd_first_call_to_resume;  // AUJ 
    7382 
    7483//------------------------------------------------------------------- 
     
    439448    } 
    440449} 
     450 
     451static int is_lua() 
     452{ 
     453  int len; 
     454  char const* s; 
     455  s = conf.script_file; 
     456  len = strlen( s ); 
     457  return len >= 4 && ( s[len-1] == 'a' || s[len-1] == 'A' ) 
     458    && ( s[len-2] == 'u' || s[len-2] == 'U' ) 
     459    && ( s[len-3] == 'l' || s[len-3] == 'L' ) 
     460    && s[len-4] == '.'; 
     461} 
     462 
     463static void wait_and_end(void) 
     464{ 
     465        script_console_add_line("PRESS SHUTTER TO CLOSE"); 
     466 
     467        // We're not running any more, but we have scheduled stuff that 
     468        // needs to finish. So keep the script marked as running, but don't 
     469        // call any more scripting functions. 
     470        state_kbd_script_run = 3;        
     471} 
     472 
     473static void process_script() 
     474{   // Note: This function is called from an action stack for AS_SCRIPT_RUN. 
     475     
     476    long t; 
     477    int Lres; 
     478 
     479    if (state_kbd_script_run != 3) { 
     480        if( L ) { 
     481            int top; 
     482            if (state_lua_kbd_first_call_to_resume) { 
     483                state_lua_kbd_first_call_to_resume = 0; 
     484                top = 0; 
     485            } else { 
     486                top = lua_gettop(Lt); 
     487            } 
     488            Lres = lua_resume( Lt, top ); 
     489 
     490            if (Lres != LUA_YIELD && Lres != 0) { 
     491                script_console_add_line( lua_tostring( Lt, -1 ) ); 
     492                if(conf.debug_lua_restart_on_error){ 
     493                    lua_script_reset(); 
     494                    script_start_gui(0); 
     495                } else { 
     496                    wait_and_end(); 
     497                } 
     498                return; 
     499            } 
     500 
     501            if (Lres != LUA_YIELD) { 
     502                script_console_add_line(lang_str(LANG_CONSOLE_TEXT_FINISHED)); 
     503                action_pop(); 
     504                script_end(); 
     505            }     
     506        } else 
     507        { 
     508            ubasic_run(); 
     509            if (ubasic_finished()) { 
     510                script_console_add_line(lang_str(LANG_CONSOLE_TEXT_FINISHED)); 
     511                action_pop(); 
     512                script_end(); 
     513            }     
     514        } 
     515    } 
     516} 
     517 
     518static int script_action_stack(long p) 
     519{ 
     520    // process stack operations 
     521    switch (p) { 
     522        case AS_SCRIPT_RUN: 
     523            if (state_kbd_script_run) 
     524                process_script(); 
     525            else 
     526                action_pop(); 
     527            break; 
     528        case AS_MOTION_DETECTOR: 
     529            if(md_detect_motion()==0) 
     530            { 
     531                action_pop(); 
     532                if (L) 
     533                { 
     534                       // We need to recover the motion detector's 
     535                       // result and push 
     536                       // it onto the thread's stack. 
     537                       lua_pushnumber( Lt, md_get_result() ); 
     538                } else 
     539                { 
     540                    ubasic_set_md_ret(md_get_result()); 
     541                } 
     542            } 
     543            break; 
     544        default: 
     545            if (!action_stack_standard(p) && !state_kbd_script_run) 
     546            { 
     547                /*finished();*/ 
     548                action_pop(); 
     549                script_end(); 
     550            } 
     551            break; 
     552    } 
     553     
     554    return 1; 
     555} 
     556 
     557long script_stack_start() 
     558{ 
     559    running_script_stack_name = action_stack_create(&script_action_stack, AS_SCRIPT_RUN); 
     560    return running_script_stack_name; 
     561} 
     562 
     563int script_is_running() 
     564{ 
     565    return !action_stack_is_finished(running_script_stack_name); 
     566} 
     567 
     568void script_end() 
     569{ 
     570    script_print_screen_end(); 
     571    if( L ) { 
     572      lua_script_reset(); 
     573    } 
     574    else { 
     575      ubasic_end(); 
     576    } 
     577        md_close_motion_detector(); 
     578        shot_histogram_set(0); 
     579    kbd_key_release_all(); 
     580    state_kbd_script_run = 0; 
     581 
     582    conf_update_prevent_shutdown(); 
     583 
     584    vid_bitmap_refresh(); 
     585} 
     586 
     587long script_start_gui( int autostart ) 
     588{ 
     589    int i; 
     590 
     591    shot_histogram_set(0); 
     592    if (autostart) 
     593        auto_started = 1; 
     594    else 
     595        auto_started = 0; 
     596 
     597    kbd_last_clicked = 0; 
     598 
     599    /*if (!autostart)*/ kbd_key_release_all(); 
     600 
     601    console_clear(); 
     602    script_print_screen_init(); 
     603 
     604    if (conf.script_param_save) { 
     605        save_params_values(0); 
     606    } 
     607    if( autostart ) 
     608        script_console_add_line("***Autostart***"); 
     609    else 
     610        script_console_add_line(lang_str(LANG_CONSOLE_TEXT_STARTED)); 
     611 
     612    if( is_lua() ) { 
     613        if( !lua_script_start(script_source_str) ) { 
     614            script_print_screen_end(); 
     615            wait_and_end(); 
     616            return -1; 
     617        } 
     618        for (i=0; i<SCRIPT_NUM_PARAMS; ++i) { 
     619            if( script_params[i][0] ) { 
     620                char var = 'a'+i; 
     621                lua_pushlstring( L, &var, 1 ); 
     622                lua_pushnumber( L, conf.ubasic_vars[i] ); 
     623                lua_settable( L, LUA_GLOBALSINDEX ); 
     624            } 
     625        } 
     626        state_lua_kbd_first_call_to_resume = 1; 
     627    } else { // ubasic 
     628        ubasic_init(script_source_str); 
     629 
     630        for (i=0; i<SCRIPT_NUM_PARAMS; ++i) { 
     631            ubasic_set_variable(i, conf.ubasic_vars[i]); 
     632        } 
     633    } 
     634 
     635    state_kbd_script_run = 1; 
     636 
     637    conf_update_prevent_shutdown(); 
     638 
     639    return script_stack_start(); 
     640} 
     641 
     642long script_start_ptp( char *script , int keep_result ) 
     643{ 
     644  lua_script_start(script); 
     645  lua_keep_result = keep_result; 
     646  state_lua_kbd_first_call_to_resume = 1; 
     647  state_kbd_script_run = 1; 
     648  kbd_set_block(1); 
     649  auto_started = 0; 
     650  return script_stack_start(); 
     651} 
     652 
Note: See TracChangeset for help on using the changeset viewer.