root/modules/gui_grid.c

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

DEFINITIONS

This source file includes following definitions.
  1. grid_lines_free_data
  2. process_title
  3. process_element
  4. parse_grid_file
  5. grid_lines_load
  6. gui_grid_draw_osd
  7. _module_loader
  8. _module_unloader
  9. _module_can_unload

   1 #include "camera_info.h"
   2 #include "stdlib.h"
   3 #include "keyboard.h"
   4 #include "conf.h"
   5 #include "gui.h"
   6 #include "gui_draw.h"
   7 #include "gui_lang.h"
   8 #include "gui_grid.h"
   9 #include "fileutil.h"
  10 
  11 #include "module_def.h"
  12 
  13 //-------------------------------------------------------------------
  14 #define GRID_REDRAW_INTERVAL        4
  15 
  16 //-------------------------------------------------------------------
  17 typedef enum {
  18     GRID_ELEM_LINE,
  19     GRID_ELEM_RECT,
  20     GRID_ELEM_FILLED_RECT,
  21     GRID_ELEM_ELLIPSE,
  22     GRID_ELEM_FILLED_ELLIPSE,
  23 } grid_elem_type;
  24 
  25 struct gline {
  26     grid_elem_type      type;
  27     coord               x0,y0,x1,y1;
  28     color               clf, clb;
  29     struct gline        *next;
  30 };
  31 
  32 static int grid_loading = 0;
  33 static int interval = GRID_REDRAW_INTERVAL;
  34 static struct gline *head=NULL, *top=NULL;
  35 static const char *grid_default =
  36     "@title Default grid\n"
  37     "@line 0,79,359,79,17\n"
  38     "@line 0,160,359,160,17\n";
  39 
  40 //-------------------------------------------------------------------
  41 static void grid_lines_free_data() {
  42     struct gline  *ptr = head, *prev;
  43 
  44     while (ptr) {
  45         prev=ptr;
  46         ptr=ptr->next;
  47         free(prev);
  48     }
  49     head=top=NULL;
  50 }
  51 
  52 //-------------------------------------------------------------------
  53 static void process_title(const char *title) {
  54     register const char *ptr = title;
  55     register int i=0;
  56 
  57     while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces
  58     while (i<(sizeof(conf.grid_title)-1) && ptr[i] && ptr[i]!='\r' && ptr[i]!='\n') {
  59         conf.grid_title[i]=ptr[i];
  60         ++i;
  61     }
  62     conf.grid_title[i]=0;
  63 }
  64 
  65 //-------------------------------------------------------------------
  66 static void process_element(const char *str, int n, grid_elem_type type) {
  67     register const char *ptr = str;
  68     char *nptr;
  69     register int i;
  70     long nums[6];
  71     struct gline  *gptr;
  72 
  73     for (i=0; i<(sizeof(nums)/sizeof(nums[0])) && i<n; ++i) {
  74         while (ptr[0]==' ' || ptr[0]=='\t' || ptr[0]==',') ++ptr; // whitespaces
  75         nums[i] = strtol(ptr, &nptr, 0);
  76         if (nptr == ptr) { // error
  77             return;
  78         } else {
  79             ptr = nptr;
  80         }
  81     }
  82 
  83     gptr = malloc(sizeof(struct gline));
  84    
  85     if (gptr) {
  86       gptr->type=type;
  87       gptr->x0=nums[0];  gptr->y0=nums[1];
  88       gptr->x1=nums[2];  gptr->y1=nums[3];
  89       gptr->clf=nums[4]; gptr->clb=nums[5];
  90       if (!head) head=gptr;
  91       if (top) top->next=gptr;
  92       gptr->next=NULL;
  93       top=gptr;
  94     }
  95 }
  96 
  97 //-------------------------------------------------------------------
  98 static int parse_grid_file(char *ptr, int size)
  99 {
 100     conf.grid_title[0]=0;
 101 
 102     if (size > 0)
 103     {
 104         grid_lines_free_data();
 105 
 106         while (ptr[0])
 107         {
 108             while (ptr[0]==' ' || ptr[0]=='\t') ++ptr; // whitespaces
 109             if (ptr[0]=='@')
 110             {
 111                 if (strncmp("@title", ptr, 6)==0) {
 112                     ptr+=6;
 113                     process_title(ptr);
 114                 } else if (strncmp("@line", ptr, 5)==0) {
 115                     ptr+=5;
 116                     process_element(ptr, 5, GRID_ELEM_LINE);
 117                 } else if (strncmp("@rectf", ptr, 6)==0) {
 118                     ptr+=6;
 119                     process_element(ptr, 6, GRID_ELEM_FILLED_RECT);
 120                 } else if (strncmp("@rect", ptr, 5)==0) {
 121                     ptr+=5;
 122                     process_element(ptr, 5, GRID_ELEM_RECT);
 123                 } else if (strncmp("@elpsf", ptr, 6)==0) {
 124                     ptr+=6;
 125                     process_element(ptr, 5, GRID_ELEM_FILLED_ELLIPSE);
 126                 } else if (strncmp("@elps", ptr, 5)==0) {
 127                     ptr+=5;
 128                     process_element(ptr, 5, GRID_ELEM_ELLIPSE);
 129                 }
 130             }
 131             while (ptr[0] && ptr[0]!='\n') ++ptr; // unless end of line
 132             if (ptr[0]) ++ptr;
 133         }
 134     }
 135 
 136     return size;
 137 }
 138 
 139 //-------------------------------------------------------------------
 140 void grid_lines_load(const char *fn)
 141 {
 142     grid_loading = 1;
 143     if (process_file(fn, parse_grid_file, 1) > 0)  // non-zero length file found?
 144     {
 145         if (conf.grid_title[0]==0)                 // use filename if no @title string found
 146         {
 147             char* c = strrchr(fn, '/');
 148             strncpy(conf.grid_title, (c)?c+1:fn, sizeof(conf.grid_title));
 149             conf.grid_title[sizeof(conf.grid_title)-1] = 0;
 150         }
 151         strcpy(conf.grid_lines_file, fn);
 152     }
 153     grid_loading = 0;
 154 }
 155 
 156 //-------------------------------------------------------------------
 157 void gui_grid_draw_osd(int force)
 158 {
 159     if (camera_info.state.mode_rec_or_review && conf.show_grid_lines)
 160     {
 161         struct gline *ptr;
 162         twoColors col = user_color(conf.grid_color);
 163 
 164         if (force || --interval==0) {
 165             for (ptr=head; ptr; ptr=ptr->next) {
 166                 switch (ptr->type) {
 167                     case GRID_ELEM_LINE:
 168                         draw_line(ptr->x0, ptr->y0, ptr->x1, ptr->y1, (conf.grid_force_color)?FG_COLOR(col):ptr->clf);
 169                         break;
 170                     case GRID_ELEM_RECT:
 171                         draw_rectangle(ptr->x0, ptr->y0, ptr->x1, ptr->y1, (conf.grid_force_color)?col:MAKE_COLOR(ptr->clb, ptr->clf), RECT_BORDER1);
 172                         break;
 173                     case GRID_ELEM_FILLED_RECT:
 174                         draw_rectangle(ptr->x0, ptr->y0, ptr->x1, ptr->y1, (conf.grid_force_color)?col:MAKE_COLOR(ptr->clb, ptr->clf), RECT_BORDER1|DRAW_FILLED);
 175                         break;
 176                     case GRID_ELEM_ELLIPSE:
 177                         draw_ellipse(ptr->x0, ptr->y0, (unsigned int)(ptr->x1), (unsigned int)(ptr->y1), (conf.grid_force_color)?FG_COLOR(col):ptr->clf, 0);
 178                         break;
 179                     case GRID_ELEM_FILLED_ELLIPSE:
 180                         draw_ellipse(ptr->x0, ptr->y0, (unsigned int)(ptr->x1), (unsigned int)(ptr->y1), (conf.grid_force_color)?BG_COLOR(col):ptr->clf, DRAW_FILLED);
 181                         break;
 182                 }
 183             }
 184             interval = GRID_REDRAW_INTERVAL;
 185         }
 186     }
 187 }
 188 
 189 // =========  MODULE INIT =================
 190 
 191 /***************** BEGIN OF AUXILARY PART *********************
 192   ATTENTION: DO NOT REMOVE OR CHANGE SIGNATURES IN THIS SECTION
 193  **************************************************************/
 194 
 195 //---------------------------------------------------------
 196 // PURPOSE:   Perform on-load initialisation
 197 // RETURN VALUE: 1 error, 0 ok
 198 //---------------------------------------------------------
 199 int _module_loader( unsigned int* chdk_export_list )
 200 {
 201     grid_lines_load(conf.grid_lines_file);
 202     return 0;
 203 }
 204 
 205 //---------------------------------------------------------
 206 // PURPOSE: Finalize module operations (close allocs, etc)
 207 // RETURN VALUE: 0-ok, 1-fail
 208 //---------------------------------------------------------
 209 int _module_unloader()
 210 {
 211     grid_lines_free_data();
 212     return 0;
 213 }
 214 
 215 int _module_can_unload()
 216 {
 217     return (conf.show_grid_lines == 0 && !grid_loading);
 218 }
 219 
 220 /******************** Module Information structure ******************/
 221 
 222 libgrids_sym _libgrids =
 223 {
 224     {
 225          _module_loader, _module_unloader, _module_can_unload, 0, 0
 226     },
 227 
 228     gui_grid_draw_osd,
 229     grid_lines_load
 230 };
 231 
 232 ModuleInfo _module_info =
 233 {
 234     MODULEINFO_V1_MAGICNUM,
 235     sizeof(ModuleInfo),
 236     GUI_GRID_VERSION,                   // Module version
 237 
 238     ANY_CHDK_BRANCH, 0, OPT_ARCHITECTURE,                       // Requirements of CHDK version
 239     ANY_PLATFORM_ALLOWED,               // Specify platform dependency
 240 
 241     (int32_t)"Grids (dll)",
 242     MTYPE_EXTENSION,            //Grid Display
 243 
 244     &_libgrids.base,
 245 
 246     CONF_VERSION,               // CONF version
 247     ANY_VERSION,                // CAM SCREEN version
 248     ANY_VERSION,                // CAM SENSOR version
 249     ANY_VERSION,                // CAM INFO version
 250 };
 251 
 252 /*************** END OF AUXILARY PART *******************/

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