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

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