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

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