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

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