source: trunk/platform/generic/wrappers.c @ 1119

Revision 1119, 28.3 KB checked in by reyalP, 2 years ago (diff)

stidio inline cleanup from philmoz in http://chdk.setepontos.com/index.php?topic=650.msg63691#msg63691

  • Property svn:eol-style set to native
Line 
1#include "camera.h"
2#include "lolevel.h"
3#include "platform.h"
4#include "conf.h"
5#include "math.h"
6#include "levent.h"
7
8#if CAM_DRYOS
9#define _U      0x01    /* upper */
10#define _L      0x02    /* lower */
11#define _D      0x04    /* digit */
12#define _C      0x08    /* cntrl */
13#define _P      0x10    /* punct */
14#define _S      0x20    /* white space (space/lf/tab) */
15#define _X      0x40    /* hex digit */
16#define _SP     0x80    /* hard space (0x20) */
17static int _ctype(int c,int t) {
18static unsigned char ctypes[] = {
19_C,_C,_C,_C,_C,_C,_C,_C,                        /* 0-7 */
20_C,_C|_S,_C|_S,_C|_S,_C|_S,_C|_S,_C,_C,         /* 8-15 */
21_C,_C,_C,_C,_C,_C,_C,_C,                        /* 16-23 */
22_C,_C,_C,_C,_C,_C,_C,_C,                        /* 24-31 */
23_S|_SP,_P,_P,_P,_P,_P,_P,_P,                    /* 32-39 */
24_P,_P,_P,_P,_P,_P,_P,_P,                        /* 40-47 */
25_D,_D,_D,_D,_D,_D,_D,_D,                        /* 48-55 */
26_D,_D,_P,_P,_P,_P,_P,_P,                        /* 56-63 */
27_P,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U|_X,_U,      /* 64-71 */
28_U,_U,_U,_U,_U,_U,_U,_U,                        /* 72-79 */
29_U,_U,_U,_U,_U,_U,_U,_U,                        /* 80-87 */
30_U,_U,_U,_P,_P,_P,_P,_P,                        /* 88-95 */
31_P,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L|_X,_L,      /* 96-103 */
32_L,_L,_L,_L,_L,_L,_L,_L,                        /* 104-111 */
33_L,_L,_L,_L,_L,_L,_L,_L,                        /* 112-119 */
34_L,_L,_L,_P,_P,_P,_P,_C,                        /* 120-127 */
35// since the following have nothing set, we can save memory by leaving them out
36#if 0
370,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                /* 128-143 */
380,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                /* 144-159 */
390,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                /* 160-175 */
400,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                /* 176-191 */
410,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                /* 192-207 */
420,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                /* 208-223 */
430,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,                /* 224-239 */
440,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0         /* 240-255 */
45#endif
46};
47    // have to handle EOF (-1)
48    if( (unsigned)c >= sizeof(ctypes)) {
49        return 0;
50    }
51    return ctypes[c] & t;
52}
53#endif
54
55void msleep(long msec)
56{
57    _SleepTask(msec);
58}
59
60#ifndef CAM_DRYOS
61void task_lock()
62{
63    _taskLock();
64}
65
66void task_unlock()
67{
68    _taskUnlock();
69}
70
71const char *task_name(int id)
72{
73    return _taskName(id);
74}
75
76int task_id_list_get(int *idlist,int size)
77{
78    return _taskIdListGet(idlist,size);
79}
80#endif
81
82long get_property_case(long id, void *buf, long bufsize)
83{
84    return _GetPropertyCase(id, buf, bufsize);
85}
86
87long set_property_case(long id, void *buf, long bufsize)
88{
89    return _SetPropertyCase(id, buf, bufsize);
90}
91
92void remount_filesystem()
93{
94    _Unmount_FileSystem();
95    _Mount_FileSystem();
96}
97
98void mark_filesystem_bootable()
99{
100    _UpdateMBROnFlash(0, 0x40, "BOOTDISK");
101}
102
103void __attribute__((weak)) vid_bitmap_refresh()
104{
105    _RefreshPhysicalScreen(1);
106}
107
108
109long get_parameter_data(long id, void *buf, long bufsize)
110{
111    return _GetParameterData(id|0x4000, buf, bufsize);
112}
113
114long set_parameter_data(long id, void *buf, long bufsize)
115{
116    return _SetParameterData(id|0x4000, buf, bufsize);
117}
118
119long lens_get_zoom_pos()
120{
121    return _GetZoomLensCurrentPosition();
122}
123
124void lens_set_zoom_pos(long newpos)
125{
126}
127
128long lens_get_zoom_point()
129{
130    return _GetZoomLensCurrentPoint();
131}
132
133void lens_set_zoom_point(long newpt)
134{
135#if defined (CAMERA_s95)
136        long startTime;
137#endif
138
139    if (newpt < 0) {
140        newpt = 0;
141    } else if (newpt >= zoom_points) {
142        newpt = zoom_points-1;
143    }
144
145#if defined(CAMERA_sx30) || defined(CAMERA_g12)
146        if (lens_get_zoom_point() != newpt)
147        {
148                // Get current digital zoom mode & state
149                // state == 1 && mode == 0 --> Digital Zoom Standard
150                int digizoom_mode, digizoom_state, digizoom_pos;
151                get_property_case(PROPCASE_DIGITAL_ZOOM_MODE,&digizoom_mode,sizeof(digizoom_mode));
152                get_property_case(PROPCASE_DIGITAL_ZOOM_STATE,&digizoom_state,sizeof(digizoom_state));
153                get_property_case(PROPCASE_DIGITAL_ZOOM_POSITION,&digizoom_pos,sizeof(digizoom_pos));
154                if ((digizoom_state == 1) && (digizoom_mode == 0) && (digizoom_pos != 0))
155                {
156                        // reset digital zoom in case camera is in this zoom range
157                        extern void _PT_MoveDigitalZoomToWide();
158                        _PT_MoveDigitalZoomToWide();
159                }
160
161  #if defined(CAMERA_sx30)
162                // SX30 - _MoveZoomLensWithPoint crashes camera
163                // _PT_MoveOpticalZoomAt works, and updates PROPCASE_OPTICAL_ZOOM_POSITION; but doesn't wait for zoom to finish
164                extern void _PT_MoveOpticalZoomAt(long*);
165                _PT_MoveOpticalZoomAt(&newpt);
166  #else
167            _MoveZoomLensWithPoint((short*)&newpt);
168  #endif
169
170                // have to sleep here, zoom_busy set in another task, without sleep this will hang
171                while (zoom_busy) msleep(10);
172
173                // g12 & sx30 only use this value for optical zoom
174                zoom_status=ZOOM_OPTICAL_MAX;
175
176  #if defined(CAMERA_g12)
177            _SetPropertyCase(PROPCASE_OPTICAL_ZOOM_POSITION, &newpt, sizeof(newpt));
178  #endif
179        }
180#else   // !(CAMERA_g12 || CAMERA_sx30)
181    _MoveZoomLensWithPoint((short*)&newpt);
182
183  #if defined (CAMERA_s95)
184        // this will hang sometimes on s95 when zoom_busy gets stuck as a 1
185        // we add a timeout as a work-around for this problem
186        startTime = get_tick_count();
187        while (get_tick_count() < (startTime + 2000)) {
188                if (!zoom_busy)
189                        break;
190        }
191  #else // !CAMERA_s95
192        while (zoom_busy) ;
193  #endif // !CAMERA_s95
194
195    if (newpt==0) zoom_status=ZOOM_OPTICAL_MIN;
196    else if (newpt >= zoom_points) zoom_status=ZOOM_OPTICAL_MAX;
197    else zoom_status=ZOOM_OPTICAL_MEDIUM;
198    _SetPropertyCase(PROPCASE_OPTICAL_ZOOM_POSITION, &newpt, sizeof(newpt));
199#endif  // !(CAMERA_g12 || CAMERA_sx30)
200}
201
202void lens_set_zoom_speed(long newspd)
203{
204    if (newspd < 5) {
205        newspd = 5;
206    } else if (newspd > 100) {
207        newspd = 100;
208    }
209    _SetZoomActuatorSpeedPercent((short*)&newspd);
210}
211
212void lens_set_focus_pos(long newpos)
213{
214#if defined(CAMERA_g12) // G12 crashes if in Continuous AF mode and try to call _MoveFocusLensToDistance
215        int af_mode;
216        get_property_case(PROPCASE_CONTINUOUS_AF,&af_mode,sizeof(af_mode));
217        if (af_mode == 0)       // can only set focus distance when not in continuous AF mode
218#endif
219        {
220                _MoveFocusLensToDistance((short*)&newpos);
221                //while (focus_busy);
222                while ((shooting_is_flash_ready()!=1) || (focus_busy));
223                newpos = _GetFocusLensSubjectDistance();
224                _SetPropertyCase(PROPCASE_SUBJECT_DIST1, &newpos, sizeof(newpos));
225                _SetPropertyCase(PROPCASE_SUBJECT_DIST2, &newpos, sizeof(newpos));
226        }
227}
228
229void play_sound(unsigned sound)
230{
231        static const int sounds[]={ 0x2001, //startup sound
232                                0x2002, //shutter sound
233                                0x2003, //button press sound
234                                0x2004, //self-timer sound
235                                0xC211, //short beep
236                                50000,  // AF confirmation
237                                0xC507, // error beep imo
238                                0x400D, // LONG ERROR BEEP CONTINIUOUS- warning, cannot be stopped (yet)
239                            };
240    if(sound >= sizeof(sounds)/sizeof(sounds[0]))
241        return;
242
243    _PT_PlaySound(sounds[sound], 0);
244}
245
246long stat_get_vbatt()
247{
248    return _VbattGet();
249}
250
251int get_battery_temp()
252{
253    return _GetBatteryTemperature();
254}
255
256int get_ccd_temp()
257{
258    return _GetCCDTemperature();
259}
260
261int get_optical_temp()
262{
263    return _GetOpticalTemperature();
264}
265
266long get_tick_count()
267{
268long t;
269#if !CAM_DRYOS
270    _GetSystemTime(&t);
271    return t;
272#else
273    return (int)_GetSystemTime(&t);
274#endif
275}
276
277/*int creat (const char *name, int flags)
278{
279    return _creat(name, flags);
280}*/
281int open (const char *name, int flags, int mode )
282{
283#ifdef CAM_DRYOS_2_3_R39
284    if(name[0]!='A')return -1;
285#endif
286#if defined(CAM_STARTUP_CRASH_FILE_OPEN_FIX)    // enable fix for camera crash at startup when opening the conf / font files
287                                                                                                // see http://chdk.setepontos.com/index.php?topic=6179.0
288        #define O_RDONLY 0                                                      // copied from stdlib.h (including stdlib.h causes compile errors due to function definition mismatch - needs fixing)
289        if (flags == O_RDONLY)                                          // At startup opening the conf / font files conflicts with Canon task if use _Open. Camera can randomly crash.
290                return _open(name, flags, mode);
291#endif
292    return _Open(name, flags, mode);
293}
294int close (int fd)
295{
296    return _Close(fd);
297}
298int write (int fd, void *buffer, long nbytes)
299{
300    return _Write(fd, buffer, nbytes);
301}
302int read (int fd, void *buffer, long nbytes)
303{
304    return _Read(fd, buffer, nbytes);
305}
306int lseek (int fd, long offset, int whence)
307{
308    return _lseek(fd, offset, whence); /* yes, it's lower-case lseek here since Lseek calls just lseek (A610) */
309}
310long mkdir(const char *dirname)
311{
312    return _mkdir(dirname);
313}
314
315int remove(const char *name) {
316    return _Remove(name);
317}
318
319int errnoOfTaskGet(int tid) {
320#if !CAM_DRYOS
321    return _errnoOfTaskGet(tid);
322#else
323    return 0;
324#endif
325}
326
327int isdigit(int c) {
328#if !CAM_DRYOS
329    return _isdigit(c);
330#else
331    return _ctype(c,_D);
332#endif
333}
334
335int isspace(int c) {
336#if !CAM_DRYOS
337    return _isspace(c);
338#else
339    return _ctype(c,_S);
340#endif
341
342}
343
344int isalpha(int c) {
345#if !CAM_DRYOS
346    return _isalpha(c);
347#else
348    return _ctype(c,(_U|_L));
349#endif
350}
351
352int isupper(int c) {
353#if !CAM_DRYOS
354    return _isupper(c);
355#else
356    return _ctype(c,_U);
357#endif
358
359}
360
361int islower(int c) {
362#if !CAM_DRYOS
363    return _islower(c);
364#else
365    return _ctype(c,_L);
366#endif
367
368}
369
370int ispunct(int c) {
371#if !CAM_DRYOS
372    return _ispunct(c);
373#else
374    return _ctype(c,_P);
375#endif
376}
377
378int isxdigit(int c) {
379#if !CAM_DRYOS
380    return _isxdigit(c);
381#else
382    return _ctype(c,(_X|_D));
383#endif
384}
385
386int isalnum(int c) {
387    return (isdigit(c) || isalpha(c));
388}
389
390int iscntrl(int c) {
391#if !CAM_DRYOS
392    return ((c >=0 && c <32) || c == 127); // don't want to require the whole ctype table on vxworks just for this one
393#else
394    return _ctype(c,_C);
395#endif
396}
397
398long strlen(const char *s) {
399    return _strlen(s);
400}
401
402int strcmp(const char *s1, const char *s2) {
403    return _strcmp(s1, s2);
404}
405
406int strncmp(const char *s1, const char *s2, long n) {
407    return _strncmp(s1, s2, n);
408}
409
410char *strchr(const char *s, int c) {
411    return _strchr(s, c);
412}
413
414char *strcpy(char *dest, const char *src) {
415    return _strcpy(dest, src);
416}
417
418char *strncpy(char *dest, const char *src, long n) {
419    return _strncpy(dest, src, n);
420}
421
422char *strcat(char *dest, const char *app) {
423    return _strcat(dest, app);
424}
425
426char *strrchr(const char *s, int c) {
427#if defined (CAMERA_s95)
428        // unable to find strrchr in s95 - we use our own fn
429        char *result = 0;
430
431        c = (char) c;
432
433        do {
434                if (c == *s)
435                        result = (char*) s;
436        } while (*s++ != '\0');
437
438        return result;
439#else
440    return _strrchr(s, c);
441#endif
442}
443
444long strtol(const char *nptr, char **endptr, int base) {
445    return _strtol(nptr, endptr, base);
446}
447
448unsigned long strtoul(const char *nptr, char **endptr, int base) {
449#if CAM_DRYOS
450    return (unsigned long)_strtolx(nptr, endptr, base, 0);
451#else
452    return _strtoul(nptr, endptr, base);
453#endif
454}
455
456char *strpbrk(const char *s, const char *accept) {
457#if !CAM_DRYOS
458    return _strpbrk(s, accept);
459#else
460    const char *sc1,*sc2;
461
462    for( sc1 = s; *sc1 != '\0'; ++sc1) {
463     for( sc2 = accept; *sc2 != '\0'; ++sc2) {
464      if (*sc1 == *sc2) return (char *) sc1;
465     }
466    }
467return (void*)0;
468#endif
469}
470
471long sprintf(char *s, const char *st, ...)
472{
473    long res;
474    __builtin_va_list va;
475    __builtin_va_start(va, st);
476    res = _vsprintf(s, st, va);
477    __builtin_va_end(va);
478    return res;
479}
480
481// strerror exists on vxworks cams,
482// but it does about the same thing as this
483const char *strerror(int en) {
484#if !CAM_DRYOS
485    static char msg[20];
486    sprintf(msg,"errno 0x%X",en);
487    return msg;
488#else
489    return "error";
490#endif
491}
492
493int tolower(int c) {
494#if !CAM_DRYOS
495        return _tolower(c);
496#else
497        return isupper(c) ? c - 'A' + 'a' : c;
498#endif
499}
500
501int toupper(int c) {
502#if !CAM_DRYOS
503        return _toupper(c);
504#else
505        return islower(c) ? c - 'a' + 'A' : c;
506#endif
507}
508
509unsigned long time(unsigned long *timer) {
510    return _time(timer);
511}
512
513int utime(char *file, void *newTimes) {
514#if !CAM_DRYOS
515  return _utime(file, newTimes);
516#else
517 int res=0;
518 int fd;
519 fd = _open(file, 0, 0);
520
521#ifdef CAM_DRYOS_2_3_R39
522   if (fd>=0) {
523       _close(fd);
524       res=_SetFileTimeStamp(file, ((int*)newTimes)[0] , ((int*)newTimes)[1]);
525   }
526#else
527     if (fd>=0) {
528      res=_SetFileTimeStamp(fd, ((int*)newTimes)[0] , ((int*)newTimes)[1]);
529      _close(fd);
530     }
531     // return value compatibe with utime: ok=0 fail=-1
532#endif
533  return (res)?0:-1;
534#endif
535}
536
537void *localtime(const unsigned long *_tod) {
538#if !CAM_DRYOS
539    return _localtime(_tod);
540#else
541// for DRYOS cameras do something with this!  - sizeof(x[]) must be >= sizeof(struct tm) :  'static int x[9];'
542  static int x[9];
543  return _LocalTime(_tod, &x);
544#endif
545}
546
547long strftime(char *s, unsigned long maxsize, const char *format, /*const struct tm*/ void *timp) {
548        return _strftime(s,maxsize,format,timp);
549}
550
551/*time_t*/ long mktime(/*struct tm*/ void *timp) {
552#if !CAM_DRYOS
553        return _mktime(timp);
554#else
555        int timp_ext[10]; // struct tm + a ptr
556        _memcpy(timp_ext,timp,9*sizeof(int));
557        timp_ext[9]=0;
558        long retval = _mktime_ext(&timp_ext);
559        _memcpy(timp,timp_ext,9*sizeof(int));
560        return retval;
561#endif
562}
563
564double _log(double x) {
565    return __log(x);
566}
567
568double _log10(double x) {
569    return __log10(x);
570}
571
572double _pow(double x, double y) {
573    return __pow(x, y);
574}
575
576double _sqrt(double x) {
577    return __sqrt(x);
578}
579
580#ifdef OPT_EXMEM_MALLOC
581// I set this up to 16 mb and it still booted...
582#ifndef EXMEM_HEAP_SKIP
583#define EXMEM_HEAP_SKIP 0
584#endif
585#ifndef EXMEM_BUFFER_SIZE
586#define EXMEM_BUFFER_SIZE (1024*1024*2) // default size if not specified by camera
587#endif
588#define EXMEM_HEAP_SIZE (EXMEM_BUFFER_SIZE+EXMEM_HEAP_SKIP)     // desired space + amount to skip for the movie buffers (if needed)
589// these aren't currently needed elsewhere
590/*
591void * exmem_alloc(unsigned pool_id, unsigned size)
592{
593        return _exmem_alloc(pool_id,size,0);
594}
595
596void exmem_free(unsigned pool_id)
597{
598        _exmem_free(pool_id);
599}
600*/
601
602static void *exmem_heap;
603
604void *suba_init(void *heap, unsigned size, unsigned rst, unsigned mincell);
605void *suba_alloc(void *heap, unsigned size, unsigned zero);
606int suba_free(void *heap, void *p);
607
608void exmem_malloc_init() {
609        // pool zero is EXMEM_RAMDISK on d10
610        void *mem = _exmem_alloc(0,EXMEM_HEAP_SIZE,0);
611        if(mem) {
612                exmem_heap = suba_init(mem,EXMEM_HEAP_SIZE-EXMEM_HEAP_SKIP,1,1024);
613        }
614}
615
616void *malloc(unsigned size) {
617        if(exmem_heap)
618                return suba_alloc(exmem_heap,size,0);
619        else
620                return _malloc(size);
621}
622void free(void *p) {
623        if(exmem_heap && (p >= exmem_heap))
624                suba_free(exmem_heap,p);
625        else
626                _free(p);
627}
628// regular malloc
629#else
630void *malloc(long size) {
631    return _malloc(size);
632}
633
634void free(void *p) {
635    return _free(p);
636}
637#endif
638
639void *memcpy(void *dest, const void *src, long n) {
640    return _memcpy(dest, src, n);
641}
642
643void *memset(void *s, int c, int n) {
644    return _memset(s, c, n);
645}
646
647int memcmp(const void *s1, const void *s2, long n) {
648    return _memcmp(s1, s2, n);
649}
650
651void *memchr(const void *s, int c, int n) {
652#if !CAM_DRYOS
653        return _memchr(s,c,n);
654#else
655        while (n-- > 0) {
656                if (*(char *)s == c)
657                        return (void *)s;
658                s++;
659        }
660        return (void *)0;
661#endif
662}
663
664int rand(void) {
665    return _rand();
666}
667
668void *srand(unsigned int seed) {
669    return _srand(seed);
670}
671
672void qsort(void *__base, int __nelem, int __size, int (*__cmp)(const void *__e1, const void *__e2)) {
673    _qsort(__base, __nelem, __size, __cmp);
674}
675
676void *opendir(const char* name) {
677    return _opendir(name);
678}
679
680void* readdir(void *d) {
681# if !CAM_DRYOS
682    return _readdir(d);
683#else
684// for DRYOS cameras  A650, A720  do something with this!  - sizeof(de[]) must be >= sizeof(struct dirent): 'static char de[40];'
685  static char de[40];
686  _ReadFastDir(d, &de);
687  return de[0]? &de : (void*)0;
688#endif
689}
690
691int closedir(void *d) {
692    return _closedir(d);
693}
694
695void rewinddir(void *d) {
696    return _rewinddir(d);
697}
698
699int stat(char *name, void *pStat) {
700    return _stat(name, pStat);
701}
702
703void *umalloc(long size) {
704    return _AllocateUncacheableMemory(size);
705}
706
707void ufree(void *p) {
708    return _FreeUncacheableMemory(p);
709}
710
711static int shutdown_disabled = 0;
712void disable_shutdown() {
713    if (!shutdown_disabled) {
714        _LockMainPower();
715        shutdown_disabled = 1;
716    }
717}
718
719void enable_shutdown() {
720    if (shutdown_disabled) {
721        _UnlockMainPower();
722        shutdown_disabled = 0;
723    }
724}
725void camera_shutdown_in_a_second(void){
726int i;
727//#if CAM_DRYOS
728//#else
729_SetAutoShutdownTime(1); // 1 sec
730for (i=0;i<200;i++) _UnlockMainPower(); // set power unlock counter to 200 or more, because every keyboard function call try to lock power again ( if "Disable LCD off" menu is "alt" or "script").
731//#endif
732}
733long MakeDirectory_Fut(const char *dirname) {
734 return _MakeDirectory_Fut(dirname,-1); // meaning of second arg is not clear, firmware seems to use -1
735}
736
737long fopen(const char *filename, const char *mode) {
738    return _Fopen_Fut(filename,mode);
739}
740
741long fclose(long f) {
742    return _Fclose_Fut((long)f);
743}
744
745long fread(void *buf, long elsize, long count, long f) {
746    return _Fread_Fut(buf,elsize,count,(long)f);
747}
748
749long fwrite(const void *buf, long elsize, long count, long f) {
750    return _Fwrite_Fut(buf,elsize,count,(long)f);
751}
752
753long fseek(long file, long offset, long whence) {
754    return _Fseek_Fut((long)file,offset,whence);
755}
756
757long feof(long file) {
758    return _Feof_Fut((long)file);
759}
760
761long fflush(long file) {
762    return _Fflush_Fut((long)file);
763}
764
765char *fgets(char *buf, int n, long f) {
766    return _Fgets_Fut(buf,n,(int)f);
767}
768
769long RenameFile_Fut(const char *oldname, const char *newname) {
770 return _RenameFile_Fut(oldname, newname);
771}
772
773int rename(const char *oldname, const char *newname){
774 // doesn't appear to work on a540
775 return _rename(oldname, newname);
776}
777
778long DeleteFile_Fut(const char *name) {
779 return _DeleteFile_Fut(name);
780}
781
782unsigned int GetFreeCardSpaceKb(void){
783 return (_GetDrive_FreeClusters(0)*(_GetDrive_ClusterSize(0)>>9))>>1;
784}
785
786unsigned int GetTotalCardSpaceKb(void){
787 return (_GetDrive_TotalClusters(0)*(_GetDrive_ClusterSize(0)>>9))>>1;
788}
789
790
791unsigned int GetJpgCount(void){
792
793 return strtol(camera_jpeg_count_str(),((void*)0),0);
794}
795
796unsigned int GetRawCount(void){
797 return GetFreeCardSpaceKb()/((hook_raw_size() / 1024)+GetFreeCardSpaceKb()/GetJpgCount());
798
799}
800
801void EnterToCompensationEVF(void)
802{
803  _EnterToCompensationEVF();
804}
805
806void ExitFromCompensationEVF()
807{
808  _ExitFromCompensationEVF();
809}
810
811void TurnOnBackLight(void)
812{
813  _TurnOnBackLight();
814}
815
816void TurnOffBackLight(void)
817{
818  _TurnOffBackLight();
819}
820
821void DoAFLock(void)
822{
823  _DoAFLock();
824}
825
826void UnlockAF(void)
827{
828  _UnlockAF();
829}
830
831#if CAM_MULTIPART
832
833#define SECTOR_SIZE 512
834static char *mbr_buf=(void*)0;
835static unsigned long drive_sectors;
836
837int is_mbr_loaded()
838{
839        return (mbr_buf == (void*)0) ? 0 : 1;
840}
841
842#ifndef CAM_DRYOS
843
844int mbr_read(char* mbr_sector, unsigned long drive_total_sectors, unsigned long *part_start_sector,  unsigned long *part_length){
845// return value: 1 - success, 0 - fail
846// called only in VxWorks
847
848 int offset=0x10; // points to partition #2
849 int valid;
850
851 if ((mbr_sector[0x1FE]!=0x55) || (mbr_sector[0x1FF]!=0xAA)) return 0; // signature check
852
853 mbr_buf=_AllocateUncacheableMemory(SECTOR_SIZE);
854 _memcpy(mbr_buf,mbr_sector,SECTOR_SIZE);
855 drive_sectors=drive_total_sectors;
856
857 while(offset>=0) {
858
859  *part_start_sector=(*(unsigned short*)(mbr_sector+offset+0x1C8)<<16) | *(unsigned short*)(mbr_sector+offset+0x1C6);
860  *part_length=(*(unsigned short*)(mbr_sector+offset+0x1CC)<<16) | *(unsigned short*)(mbr_sector+offset+0x1CA);
861
862  valid= (*part_start_sector) && (*part_length) &&
863         (*part_start_sector<=drive_total_sectors) &&
864         (*part_start_sector+*part_length<=drive_total_sectors) &&
865         ((mbr_sector[offset+0x1BE]==0) || (mbr_sector[offset+0x1BE]==0x80)); // status: 0x80 (active) or 0 (non-active)
866
867  if (valid && ((mbr_sector[0x1C2+offset]==0x0B) || (mbr_sector[0x1C2+offset]==0x0C))) break;   // FAT32 secondary partition
868
869  offset-=0x10;
870
871 }
872
873 return valid;
874}
875
876#else
877
878int mbr_read_dryos(unsigned long drive_total_sectors, char* mbr_sector ){
879// Called only in DRYOS
880 mbr_buf=_AllocateUncacheableMemory(SECTOR_SIZE);
881 _memcpy(mbr_buf,mbr_sector,SECTOR_SIZE);
882 drive_sectors=drive_total_sectors;
883 return drive_total_sectors;
884}
885
886#endif
887
888int get_part_count(void){
889 unsigned long part_start_sector, part_length;
890 char part_status, part_type;
891 int i;
892 int count=0;
893 if (is_mbr_loaded())
894 {
895         for (i=0; i<=1;i++){
896          part_start_sector=(*(unsigned short*)(mbr_buf+i*16+0x1C8)<<16) | *(unsigned short*)(mbr_buf+i*16+0x1C6);
897          part_length=(*(unsigned short*)(mbr_buf+i*16+0x1CC)<<16) | *(unsigned short*)(mbr_buf+i*16+0x1CA);
898          part_status=mbr_buf[i*16+0x1BE];
899          part_type=mbr_buf[0x1C2+i*16];
900          if ( part_start_sector && part_length && part_type && ((part_status==0) || (part_status==0x80)) ) count++;
901         }
902 }
903 return count;
904}
905
906void swap_partitions(void){
907        if (is_mbr_loaded())
908        {
909         int i;
910         char c;
911         for(i=0;i<16;i++){
912          c=mbr_buf[i+0x1BE];
913          mbr_buf[i+0x1BE]=mbr_buf[i+0x1CE];
914          mbr_buf[i+0x1CE]=c;
915         }
916         _WriteSDCard(0,0,1,mbr_buf);
917        }
918}
919
920void create_partitions(void){
921        if (is_mbr_loaded())
922        {
923         unsigned long start, length;
924         char type;
925
926         _memset(mbr_buf,0,SECTOR_SIZE);
927
928         start=1; length=2*1024*1024/SECTOR_SIZE; //2 Mb
929         type=1; // FAT primary
930         mbr_buf[0x1BE + 4]=type;
931         mbr_buf[0x1BE + 8]=start;   mbr_buf[0x1BE + 9]=start>>8;   mbr_buf[0x1BE + 10]=start>>16;  mbr_buf[0x1BE + 11]=start>>24;
932         mbr_buf[0x1BE + 12]=length; mbr_buf[0x1BE + 13]=length>>8; mbr_buf[0x1BE + 14]=length>>16; mbr_buf[0x1BE + 15]=length>>24;
933
934         start=start+length; length=drive_sectors-start-1;
935         type=0x0B;  //FAT32 primary;
936         mbr_buf[0x1CE + 4]=type;
937         mbr_buf[0x1CE + 8]=start;   mbr_buf[0x1CE + 9]=start>>8;   mbr_buf[0x1CE + 10]=start>>16;  mbr_buf[0x1CE + 11]=start>>24;
938         mbr_buf[0x1CE + 12]=length; mbr_buf[0x1CE + 13]=length>>8; mbr_buf[0x1CE + 14]=length>>16; mbr_buf[0x1CE + 15]=length>>24;
939
940         mbr_buf[0x1FE]=0x55; mbr_buf[0x1FF]=0xAA; // signature;
941
942         _WriteSDCard(0,0,1,mbr_buf);
943        }
944}
945
946#endif
947
948int mute_on_zoom(int x){
949 static int old_busy=0;
950 int busy=zoom_busy||focus_busy;
951 if (old_busy!=busy) {
952  if (busy) {
953#if CAM_CAN_MUTE_MICROPHONE
954   if (conf.mute_on_zoom) _TurnOffMic();
955#endif
956   }
957   else {
958#if CAM_CAN_MUTE_MICROPHONE
959  if (conf.mute_on_zoom) _TurnOnMic();
960#endif
961#if CAM_EV_IN_VIDEO
962  if (get_ev_video_avail()) set_ev_video_avail(0);
963#endif
964  }
965  old_busy=busy;
966 }
967 return x; // preserve R0 if called from assembler
968}
969
970
971#if CAM_AF_SCAN_DURING_VIDEO_RECORD
972void MakeAFScan(void){
973 int a=0, save;
974 if (zoom_busy || focus_busy) return;
975 save=some_flag_for_af_scan;
976 some_flag_for_af_scan=0;
977#if CAM_AF_SCAN_DURING_VIDEO_RECORD == 2
978 parameter_for_af_scan=3;
979#endif
980 _MakeAFScan(&a, 3);
981 some_flag_for_af_scan=save;
982#if defined(CAMERA_g12)
983 int ae_lock;
984 get_property_case(PROPCASE_AE_LOCK,&ae_lock,sizeof(ae_lock));
985 if (ae_lock == 0)                                              // AE not locked so ensure it is unlocked after re-focus
986         _ExpCtrlTool_StartContiAE(0,0);
987 else                                                                   // AE locked before so re-lock after
988         _ExpCtrlTool_StopContiAE(0,0);
989#else
990 _ExpCtrlTool_StartContiAE(0,0);
991#endif
992}
993#endif
994
995long __attribute__((weak)) get_jogdial_direction(void){
996 return 0;
997}
998
999#if defined (DNG_EXT_FROM)
1000
1001#define DNG_EXT_TO ".DNG"
1002
1003typedef int(*p_some_f)(char*, int);
1004
1005extern p_some_f some_f_for_dng;  // camera variable!
1006extern char* second_ext_for_dng; // camera variable!
1007
1008p_some_f default_some_f;
1009char *   default_second_ext;
1010
1011char *_strstr (const char *s1, const char *s2)
1012{
1013  const char *p = s1;
1014  const int len = _strlen (s2);
1015
1016  for (; (p = _strchr (p, *s2)) != 0; p++)
1017    {
1018      if (_strncmp (p, s2, len) == 0)
1019        return (char *)p;
1020    }
1021  return (0);
1022}
1023
1024
1025int my_some_f(char *s, int x){
1026  char *f;
1027  f=_strstr(s, DNG_EXT_FROM);
1028  if (f) _memcpy(f, DNG_EXT_TO, sizeof(DNG_EXT_TO)-1);
1029  return default_some_f(s, x);
1030}
1031
1032void save_ext_for_dng(void){
1033 default_some_f=some_f_for_dng;
1034 default_second_ext=second_ext_for_dng;
1035}
1036
1037void change_ext_to_dng(void){
1038 some_f_for_dng=my_some_f;
1039 second_ext_for_dng=DNG_EXT_TO;
1040}
1041
1042void change_ext_to_default(void){
1043 some_f_for_dng=default_some_f;
1044 second_ext_for_dng=default_second_ext;
1045}
1046
1047#endif
1048
1049
1050static long drv_struct[16];
1051
1052long dh_err()
1053{
1054    return -1;
1055}
1056
1057void drv_self_hide()
1058{
1059#if !CAM_DRYOS
1060    long drvnum;
1061
1062    drvnum = _iosDrvInstall(dh_err,dh_err,dh_err,dh_err,dh_err,dh_err,dh_err);
1063    if (drvnum >= 0)
1064        _iosDevAdd(drv_struct, "A/DISKBOOT.BIN", drvnum);
1065#endif
1066}
1067
1068void drv_self_unhide(){
1069#if !CAM_DRYOS
1070 _iosDevDelete(drv_struct);
1071#endif
1072}
1073
1074int  apex2us(int apex_tv){
1075#if CAM_EXT_TV_RANGE
1076/*
1077 Extended Tv, by barberofcivil, http://chdk.setepontos.com/index.php/topic,4392.0.html
1078 Explanation by reyalP:
1079 In every port, the original shutter overrides (as opposed to super long exposure) worked by
1080 setting the propcase values at some point after auto-exposure has happened (except in manual
1081 modes, where the manual control propcases may be used instead). The Canon code previously took
1082 these values unchanged for short exposures. In newer cameras, like on the SX10 / SD980, the value
1083 is changed, apparently some time after it has been retrieved from the propcase. We know this is
1084 the case, because the propcase value itself doesn't get clamped to the allowed range (if it did,
1085 barberofcivil's code wouldn't work).
1086*/
1087        short tv;
1088        tv = shooting_get_tv96();
1089        if (tv<-576 || tv!=apex_tv) return 1000000.0*pow(2.0, -tv/96.0);
1090        else return _apex2us(apex_tv);
1091#else
1092        return 0;
1093#endif
1094}
1095
1096void PostLogicalEventForNotPowerType(unsigned id, unsigned x) {
1097        _PostLogicalEventForNotPowerType(id,x);
1098}
1099
1100void PostLogicalEventToUI(unsigned id, unsigned x) {
1101        _PostLogicalEventToUI(id,x);
1102}
1103
1104void SetLogicalEventActive(unsigned id, unsigned state) {
1105        _SetLogicalEventActive(id, state);
1106}
1107
1108void SetScriptMode(unsigned mode) {
1109        _SetScriptMode(mode);
1110}
1111
1112// TODO this belongs lib.c, but not all cameras include it
1113// same as bitmap width for most cameras, override in platform/sub/lib.c as needed
1114int __attribute__((weak)) vid_get_viewport_width() {
1115        return vid_get_bitmap_screen_width();
1116}
1117
1118// same as viewport width for most cameras, override in platform/sub/lib.c as needed
1119int __attribute__((weak)) vid_get_viewport_buffer_width() {
1120        return vid_get_viewport_width();
1121}
1122
1123// viewport x offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
1124int __attribute__((weak)) vid_get_viewport_xoffset() {
1125        return 0;
1126}
1127
1128// viewport y offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
1129int __attribute__((weak)) vid_get_viewport_yoffset() {
1130        return 0;
1131}
1132
1133// viewport image offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
1134// returns the byte offset into the viewport buffer where the image pixels start (to skip any black borders)
1135// see G12 port for sample implementation
1136int __attribute__((weak)) vid_get_viewport_image_offset() {
1137        return 0;
1138}
1139
1140// viewport image offset - used when image size != viewport size (zebra, histogram, motion detect & edge overlay)
1141// returns the byte offset to skip at the end of a viewport buffer row to get to the next row.
1142// see G12 port for sample implementation
1143int __attribute__((weak)) vid_get_viewport_row_offset() {
1144        return 0;
1145}
1146
1147// for cameras with two (or more?) RAW buffers this can be used to speed up DNG creation by
1148// calling reverse_bytes_order only once. Override in platform/sub/lib.c
1149char __attribute__((weak)) *hook_alt_raw_image_addr() {
1150        return hook_raw_image_addr();
1151}
1152
1153void __attribute__((weak)) vid_turn_off_updates()
1154{
1155}
1156
1157void __attribute__((weak)) vid_turn_on_updates()
1158{
1159}
1160
1161// use _GetFocusLensSubjectDistance for this on dryos, vx functions are basically equivlent
1162// not used in CHDK currently for either OS
1163#ifdef CAM_DRYOS
1164long __attribute__((weak)) _GetCurrentTargetDistance()
1165{
1166        return _GetFocusLensSubjectDistance();
1167}
1168#endif
1169
1170#ifdef CAM_CHDK_PTP
1171int add_ptp_handler(int opcode, ptp_handler handler, int unknown)
1172{
1173  return _add_ptp_handler(opcode,handler,unknown);
1174}
1175
1176// this would make more sense in generic/main.c but not all a cameras use it
1177void init_chdk_ptp_task() {
1178  _CreateTask("InitCHDKPTP", 0x19, 0x2000, init_chdk_ptp, 0);
1179};
1180
1181#endif
1182
1183void ExitTask()
1184{
1185  _ExitTask();
1186}
1187
1188// TODO not in sigs for vx yet
1189#ifndef CAM_DRYOS
1190void __attribute__((weak)) _reboot_fw_update(const char *fw_update)
1191{
1192        return;
1193}
1194#endif
1195
1196// TODO mode switch function should detect if USB is connected or not,
1197// and do regular or special switch as needed
1198#ifdef CAM_DRYOS
1199int __attribute__((weak)) switch_mode_usb(int mode)
1200{
1201#ifdef CAM_CHDK_PTP
1202    if ( mode == 0 ) {
1203        _Rec2PB();
1204        _set_control_event(0x80000902); // 0x10A5 ConnectUSBCable
1205    } else if ( mode == 1 ) {
1206        _set_control_event(0x902); // 0x10A6 DisconnectUSBCable
1207        _PB2Rec();
1208    } else return 0;
1209    return 1;
1210#else
1211  return 0;
1212#endif // CAM_CHDK_PTP
1213}
1214
1215#else // vxworks
1216// this doesn't need any special functions so it's defined even without CHDK_CAM_PTP
1217int __attribute__((weak)) switch_mode_usb(int mode)
1218{
1219    if ( mode == 0 ) {
1220        levent_set_play();
1221    } else if ( mode == 1 ) {
1222        levent_set_record();
1223    } else return 0;
1224    return 1;
1225}
1226#endif // vxworks
1227/*
1228// this wrapper isn't currently needed
1229// 7 calls functions and sets some MMIOs, but doesn't disable caches and actually restart
1230// 3 skips one function call on some cameras, but does restart
1231void Restart(unsigned option) {
1232        _Restart(option);
1233}
1234*/
1235
Note: See TracBrowser for help on using the repository browser.