root/modules/script_shoot_hook.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. script_shoot_hooks_reset
  2. script_shoot_hook_set
  3. script_shoot_hook_run
  4. script_shoot_hook_ready
  5. script_shoot_hook_continue
  6. script_shoot_hook_count

   1 #include "stdlib.h"
   2 #include "string.h"
   3 #include "ctype.h"
   4 #include "script_shoot_hook.h"
   5 #include "script_api.h"
   6 
   7 typedef struct {
   8     int count; // how many times the hook was called (since script start)
   9     int timeout; // timeout for blocking
  10     int active; // hook has been reached, set from hooked task, cleared from script to resume
  11 } script_shoot_hook_t;
  12 
  13 const char* shoot_hook_names[SCRIPT_NUM_SHOOT_HOOKS] = {
  14     "hook_preshoot",
  15     "hook_shoot",
  16     "hook_raw",
  17 };
  18 
  19 static script_shoot_hook_t hooks[SCRIPT_NUM_SHOOT_HOOKS];
  20 
  21 // initialize, when script is loaded or unloaded
  22 void script_shoot_hooks_reset(void)
  23 {
  24     memset(hooks,0,sizeof(hooks));
  25 }
  26 
  27 // enable hooking with specified timeout, 0 to disable
  28 void script_shoot_hook_set(int hook,int timeout) {
  29     hooks[hook].timeout = timeout;
  30 }
  31 
  32 void rawop_update_hook_status(int active);
  33 
  34 // called from hooked task, returns when hook is released or times out
  35 void script_shoot_hook_run(int hook)
  36 {
  37     int timeleft = hooks[hook].timeout;
  38     hooks[hook].count++;
  39     // only mark hook active if it was set
  40     if(timeleft > 0) {
  41         // notify rawop when in raw hook, so it can update valid raw status
  42         // and values that might change between shots
  43         if(hook == SCRIPT_SHOOT_HOOK_RAW) {
  44             rawop_update_hook_status(1);
  45         }
  46         hooks[hook].active = 1;
  47         while(timeleft > 0 && hooks[hook].active) {
  48             msleep(10);
  49             timeleft -= 10;
  50         }
  51         if(hook == SCRIPT_SHOOT_HOOK_RAW) {
  52             rawop_update_hook_status(0);
  53         }
  54         hooks[hook].active = 0;
  55     }
  56     // TODO may want to record timeout
  57 }
  58 
  59 // returns true when hooked task in hook_run
  60 int script_shoot_hook_ready(int hook)
  61 {
  62     return hooks[hook].active;
  63 }
  64 
  65 // called from script to allow hook_run to return
  66 void script_shoot_hook_continue(int hook)
  67 {
  68     hooks[hook].active = 0;
  69 }
  70 
  71 // return number of times hook has been called (for this script)
  72 int script_shoot_hook_count(int hook)
  73 {
  74     return hooks[hook].count;
  75 }
  76 

/* [<][>][^][v][top][bottom][index][help] */