root/core/lib_thumb.c

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

DEFINITIONS

This source file includes following definitions.
  1. opendir
  2. opendir_chdk
  3. readdir
  4. readdir
  5. closedir
  6. _ctype
  7. isdigit
  8. isspace
  9. isalpha
  10. isupper
  11. islower
  12. ispunct
  13. isxdigit
  14. iscntrl
  15. tolower
  16. toupper
  17. iscntrl
  18. isalnum
  19. strpbrk
  20. strstr
  21. memchr
  22. get_localtime
  23. GetJpgCount
  24. GetRawCount
  25. vid_get_viewport_image_offset
  26. vid_get_viewport_row_offset
  27. pal_clean
  28. pal_restore
  29. handle_clean_overlay
  30. handle_analog_av_in_rec

   1 // routines compiled as thumb
   2 #include "platform.h"
   3 #include "conf.h"
   4 #include "gui_draw.h"
   5 #include "keyboard.h"
   6 
   7 #if defined(CAM_DRYOS)
   8 #include "chdk-dir.c"
   9 #endif
  10 
  11 //---------------------------------------------------------------------
  12 
  13 // CHDK dir functions
  14 
  15 extern int   fw_closedir(void *d);
  16 extern void *fw_opendir(const char* name);
  17 
  18 DIR *opendir(const char* name)
  19 {
  20     return opendir_chdk(name,OPENDIR_FL_NONE);
  21 }
  22 
  23 DIR *opendir_chdk(const char* name, unsigned flags)
  24 {
  25     // Create CHDK DIR structure
  26     DIR *dir = malloc(sizeof(DIR));
  27     // If malloc failed return failure
  28     if (dir == 0) return NULL;
  29 
  30 #if defined(CAM_DRYOS)
  31     extern int get_fstype(void);
  32     // try built-in routine first, but only on FAT
  33     if ((get_fstype() < 4) && (flags & OPENDIR_FL_CHDK_LFN))
  34     {
  35         dir->fw_dir = 0;
  36         dir->cam_DIR = CHDKOpenDir(name);
  37     }
  38     else
  39     {
  40         dir->cam_DIR = NULL;
  41     }
  42     if (!dir->cam_DIR)
  43     {
  44         // try firmware routine if built-in failed
  45         dir->fw_dir = 1;
  46         dir->cam_DIR = fw_opendir(name);
  47     }
  48 #else
  49     dir->cam_DIR = fw_opendir(name);
  50 #endif
  51 
  52     // Init readdir return value
  53     dir->dir.d_name[0] = 0;
  54 
  55     // If failed clean up and return failure
  56     if (!dir->cam_DIR)
  57     {
  58         free(dir);
  59         return NULL;
  60     }
  61 
  62     return dir;
  63 }
  64 
  65 #ifndef CAM_DRYOS
  66 
  67 // Internal VxWorks dirent structure returned by readdir
  68 struct __dirent
  69 {
  70     char            d_name[100];
  71 };
  72 
  73 struct dirent* readdir(DIR *d)
  74 {
  75     if (d && d->cam_DIR)
  76     {
  77         // Get next entry from firmware function
  78         extern void *fw_readdir(void *d);
  79         struct __dirent *de = fw_readdir(d->cam_DIR);
  80         // Return next directory name if present, else return 0 (end of list)
  81         if (de)
  82         {
  83             strcpy(d->dir.d_name,de->d_name);
  84             return &d->dir;
  85         }
  86         else
  87         {
  88             d->dir.d_name[0] = 0;
  89         }
  90     }
  91     return NULL;
  92 }
  93 
  94 #else // dryos
  95 
  96 struct dirent * readdir(DIR *d)
  97 {
  98     if (d && d->cam_DIR)
  99     {
 100         if (d->fw_dir == 0)
 101         {
 102             CHDKReadDir(d->cam_DIR, d->dir.d_name);
 103         }
 104         else
 105         {
 106             extern int fw_readdir(void *d, void* dd); // DRYOS
 107             fw_readdir(d->cam_DIR, d->dir.d_name);
 108         }
 109         return d->dir.d_name[0]? &d->dir : NULL;
 110     }
 111     return NULL;
 112 }
 113 
 114 #endif // dryos dir functions
 115 
 116 int closedir(DIR *d)
 117 {
 118     int rv = -1;
 119     if (d && d->cam_DIR)
 120     {
 121 #if defined(CAM_DRYOS)
 122         if (d->fw_dir == 0)
 123         {
 124             rv = CHDKCloseDir(d->cam_DIR);
 125         }
 126         else
 127 #endif
 128         rv = fw_closedir(d->cam_DIR);
 129 
 130         // Mark closed (just in case)
 131         d->cam_DIR = 0;
 132         // Free allocated memory
 133         free(d);    
 134     }
 135     return rv;
 136 }
 137 
 138 //----------------------------------------------------------------------------
 139 // Char Wrappers (ARM stubs not required)
 140 
 141 #if CAM_DRYOS
 142 
 143 #define _U      0x01    /* upper */
 144 #define _L      0x02    /* lower */
 145 #define _D      0x04    /* digit */
 146 #define _C      0x20    /* cntrl */
 147 #define _P      0x10    /* punct */
 148 #define _S      0x40    /* white space (space/lf/tab) */
 149 #define _X      0x80    /* hex digit */
 150 #define _SP     0x08    /* hard space (0x20) */
 151 static int _ctype(int c,int t) {
 152     extern unsigned char ctypes[];  // Firmware ctypes table (in stubs_entry.S)
 153     return ctypes[c&0xFF] & t;
 154 }
 155 
 156 int isdigit(int c) { return _ctype(c,_D); }
 157 int isspace(int c) { return _ctype(c,_S); }
 158 int isalpha(int c) { return _ctype(c,(_U|_L)); }
 159 int isupper(int c) { return _ctype(c,_U); }
 160 int islower(int c) { return _ctype(c,_L); }
 161 int ispunct(int c) { return _ctype(c,_P); }
 162 int isxdigit(int c) { return _ctype(c,(_X|_D)); }
 163 int iscntrl(int c) { return _ctype(c,_C); }
 164 
 165 int tolower(int c) { return isupper(c) ? c | 0x20 : c; }
 166 int toupper(int c) { return islower(c) ? c & ~0x20 : c; }
 167 
 168 #else
 169 
 170 // don't want to require the whole ctype table on vxworks just for this one
 171 int iscntrl(int c) { return ((c >=0 && c <32) || c == 127); }
 172 
 173 #endif
 174 
 175 int isalnum(int c) { return (isdigit(c) || isalpha(c)); }
 176 
 177 //----------------------------------------------------------------------------
 178 
 179 #if CAM_DRYOS
 180 char *strpbrk(const char *s, const char *accept)
 181 {
 182     const char *sc1,*sc2;
 183 
 184     for (sc1 = s; *sc1 != '\0'; ++sc1)
 185     {
 186         for (sc2 = accept; *sc2 != '\0'; ++sc2)
 187         {
 188             if (*sc1 == *sc2)
 189                 return (char*)sc1;
 190         }
 191     }
 192     return 0;
 193 }
 194 #endif
 195 
 196 char *strstr(const char *s1, const char *s2)
 197 {
 198     const char *p = s1;
 199     const int len = strlen(s2);
 200 
 201     for (; (p = strchr(p, *s2)) != 0; p++)
 202     {
 203         if (strncmp(p, s2, len) == 0)
 204             return (char*)p;
 205     }
 206     return (0);
 207 }
 208 
 209 #if CAM_DRYOS
 210 void *memchr(const void *s, int c, int n)
 211 {
 212     while (n-- > 0)
 213     {
 214         if (*(char *)s == c)
 215             return (void *)s;
 216         s++;
 217     }
 218     return (void *)0;
 219 }
 220 #endif
 221 
 222 //----------------------------------------------------------------------------
 223 
 224 struct tm *get_localtime()
 225 {
 226     time_t t = time(NULL);
 227     return localtime(&t);
 228 }
 229 
 230 //----------------------------------------------------------------------------
 231 
 232 unsigned int GetJpgCount(void)
 233 {
 234     return strtol(camera_jpeg_count_str(),((void*)0),0);
 235 }
 236 
 237 unsigned int GetRawCount(void)
 238 {
 239     unsigned free_kb = GetFreeCardSpaceKb();
 240     unsigned raw_kb =  camera_sensor.raw_size/1024;
 241     unsigned jpgcount = GetJpgCount();
 242     unsigned avg_jpg_kb = (jpgcount>0)? free_kb/jpgcount : 0;
 243 
 244     // 0.25 raw margin
 245     unsigned margin_kb = raw_kb/4;
 246     if(free_kb <= raw_kb + margin_kb) {
 247         return 0;
 248     }
 249     free_kb -= margin_kb;
 250     return free_kb/(raw_kb+avg_jpg_kb);
 251 }
 252 
 253 //----------------------------------------------------------------------------
 254 
 255 // viewport image offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
 256 // returns the byte offset into the viewport buffer where the image pixels start (to skip any black borders)
 257 // see G12 port for sample implementation
 258 int vid_get_viewport_image_offset() {
 259     return (vid_get_viewport_yoffset() * vid_get_viewport_byte_width() * vid_get_viewport_yscale()) + (vid_get_viewport_xoffset() * 3);
 260 }
 261 
 262 // viewport image offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
 263 // returns the byte offset to skip at the end of a viewport buffer row to get to the next row.
 264 // see G12 port for sample implementation
 265 int vid_get_viewport_row_offset() {
 266 #ifndef THUMB_FW
 267     return (vid_get_viewport_byte_width() * vid_get_viewport_yscale()) - (vid_get_viewport_width_proper() * 6 / 4);
 268 #else
 269     return (vid_get_viewport_byte_width() * vid_get_viewport_yscale()) - (vid_get_viewport_width_proper() * 4 / 2);
 270 #endif
 271 }
 272 
 273 //----------------------------------------------------------------------------
 274 
 275 #ifdef CAM_CLEAN_OVERLAY
 276 /*
 277  * Making the Canon overlay invisible under selected conditions
 278  * Meant to be used on DIGIC 6 models that allow HDMI output in rec mode
 279  * On m3 and m10, same palette (0) is used in rec mode and during recording
 280  * Issues:
 281  * - regardless of setting, modes with color submode icon (such as hybrid auto) continue to display that (blue) icon
 282  * - when exposure controls are used, drop shadow of invisible osd elements may appear on screen
 283  */
 284 extern char **palette_buffer_ptr[];
 285 extern char active_palette_buffer;
 286 extern char palette_control;
 287 static char *palbackup = NULL;      // backup for the original palette
 288 static int palette_is_clean = 0;
 289 static long pending_screenerase = 0;
 290 const unsigned int handled_palette = 0; // model dependent constant, 0 for m3 and m10
 291 void pal_clean() {
 292     if (palette_is_clean) return;
 293     if (active_palette_buffer == handled_palette) {
 294         if (palbackup == NULL) {
 295             palbackup = malloc(256*4); // 256 entries, 4 bytes each
 296             if (palbackup) {
 297                 memcpy(palbackup, *palette_buffer_ptr[handled_palette]+4, 256*4);
 298             }
 299         }
 300         if (palbackup) {
 301             memset(*palette_buffer_ptr[handled_palette]+4, 0, 256*4);
 302             pending_screenerase = get_tick_count() + 50; // arbitrary 50msec delay
 303             palette_is_clean = 1;
 304         }
 305     }
 306 }
 307 void pal_restore() {
 308     if (!palette_is_clean || (palbackup == NULL)) return;
 309     memcpy(*palette_buffer_ptr[handled_palette]+4, palbackup, 256*4);
 310     pending_screenerase = get_tick_count() + 10; // arbitrary 10msec delay
 311     palette_is_clean = 0;
 312 }
 313 void handle_clean_overlay() {
 314     if ((conf.clean_overlay == 1) && camera_info.state.mode_rec) { // clean during rec mode
 315         pal_clean();
 316     }
 317     else if ((conf.clean_overlay == 2) && is_video_recording()) { // clean during video recording only
 318         pal_clean();
 319     }
 320     else {
 321         pal_restore();
 322     }
 323     if (pending_screenerase && (get_tick_count()>=pending_screenerase) && !draw_is_suspended()) {
 324         pending_screenerase = 0;
 325 
 326 #if 1
 327         // following seems to be effective for removing/redrawing Canon overlay
 328         palette_control |= 3; // magic constant, valid for m10 and m3
 329         vid_bitmap_refresh();
 330 #else
 331         // following is for the case when palette_control + vid_bitmap_refresh is not effective
 332         extern void _displayblankscreen();
 333         extern void _undisplayblankscreen();
 334         _displayblankscreen();
 335         _undisplayblankscreen();
 336 #endif
 337     }
 338 }
 339 #endif // CAM_CLEAN_OVERLAY
 340 
 341 #ifdef CAM_UNLOCK_ANALOG_AV_IN_REC
 342 void handle_analog_av_in_rec(void) {
 343     extern void SetVideoOutType(int);
 344     extern int GetVideoOutType(void);
 345     if (!camera_info.state.mode_rec) {
 346         return;
 347     }
 348     int vot = GetVideoOutType();
 349     if(conf.unlock_av_out_in_rec) {
 350         if(get_analog_av_physw_mod() && vot == 0) {
 351             SetVideoOutType(shooting_get_analog_video_standard());
 352         } else if(!get_analog_av_physw_mod() && vot != 0) {
 353             SetVideoOutType(0);
 354         }
 355     } else {
 356         if(vot != 0) {
 357             SetVideoOutType(0);
 358         }
 359     }
 360 }
 361 #endif  // CAM_UNLOCK_ANALOG_AV_IN_REC
 362 //----------------------------------------------------------------------------

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