| 1 | /* |
|---|
| 2 | * CHDK-FLAT Module System. |
|---|
| 3 | * |
|---|
| 4 | * (c)2011 Sergey Taranenko aka tsvstar |
|---|
| 5 | * |
|---|
| 6 | * Specific "shortcuts", dynamic libraries binding |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | #include "modules.h" |
|---|
| 10 | #include "module_load.h" |
|---|
| 11 | #include "camera.h" |
|---|
| 12 | #include "stdlib.h" |
|---|
| 13 | #include "conf.h" |
|---|
| 14 | #include "gui_draw.h" |
|---|
| 15 | #include "dng.h" |
|---|
| 16 | #include "lang.h" |
|---|
| 17 | |
|---|
| 18 | /************* GENERIC ******/ |
|---|
| 19 | |
|---|
| 20 | static int bind_module_generic( void** export_list, void** lib, int count, int major, int minor ) |
|---|
| 21 | { |
|---|
| 22 | *lib = 0; |
|---|
| 23 | |
|---|
| 24 | // Unbind |
|---|
| 25 | if ( !export_list ) { |
|---|
| 26 | return 0; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | // Bind |
|---|
| 30 | if ( (unsigned int)export_list[0] != EXPORTLIST_MAGIC_NUMBER ) |
|---|
| 31 | return 1; |
|---|
| 32 | if ( (unsigned int)export_list[1] < count ) |
|---|
| 33 | return 1; |
|---|
| 34 | |
|---|
| 35 | *lib = (void*)export_list[2]; |
|---|
| 36 | |
|---|
| 37 | if ( !*lib ) |
|---|
| 38 | return 1; |
|---|
| 39 | if ( (major > 0) && !API_VERSION_MATCH_REQUIREMENT( *((int*)(*lib)), major, minor ) ) { |
|---|
| 40 | *lib=0; |
|---|
| 41 | return 1; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | return 0; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | // Return: 0-fail, addr-ok |
|---|
| 48 | static void* module_load_generic(void **lib, char *name, int (*callback)(void**), int flags) |
|---|
| 49 | { |
|---|
| 50 | if (*lib == 0) |
|---|
| 51 | { |
|---|
| 52 | int module_idx = module_load(name, callback); |
|---|
| 53 | if ( module_idx < 0 ) |
|---|
| 54 | module_unload(name); |
|---|
| 55 | else |
|---|
| 56 | { |
|---|
| 57 | if (flags) |
|---|
| 58 | module_set_flags(module_idx, flags); |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | return *lib; |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | /************* DYNAMIC LIBRARY RAWOPERATION ******/ |
|---|
| 66 | |
|---|
| 67 | #if CAM_SENSOR_BITS_PER_PIXEL==10 |
|---|
| 68 | #define MODULE_NAME_RAWOP "_rawop10.flt" |
|---|
| 69 | #elif CAM_SENSOR_BITS_PER_PIXEL==12 |
|---|
| 70 | #define MODULE_NAME_RAWOP "_rawop12.flt" |
|---|
| 71 | #elif CAM_SENSOR_BITS_PER_PIXEL==14 |
|---|
| 72 | #define MODULE_NAME_RAWOP "_rawop14.flt" |
|---|
| 73 | #else |
|---|
| 74 | #error define set_raw_pixel for sensor bit depth |
|---|
| 75 | #endif |
|---|
| 76 | |
|---|
| 77 | // This is to minimize sharing sym to use this lib in other modules |
|---|
| 78 | struct librawop_sym* librawop; |
|---|
| 79 | |
|---|
| 80 | void module_rawop_unload() |
|---|
| 81 | { |
|---|
| 82 | if (librawop) |
|---|
| 83 | { |
|---|
| 84 | module_unload(MODULE_NAME_RAWOP); |
|---|
| 85 | librawop = 0; |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | static int bind_module_rawop( void** export_list ) |
|---|
| 90 | { |
|---|
| 91 | return bind_module_generic(export_list, (void**)&librawop, 1, 0, 0); |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | // Return: 0-fail, otherwise - bind list |
|---|
| 95 | struct librawop_sym* module_rawop_load() |
|---|
| 96 | { |
|---|
| 97 | return module_load_generic((void**)&librawop, MODULE_NAME_RAWOP, bind_module_rawop, 0); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | |
|---|
| 101 | /************* DYNAMIC LIBRARY EDGE OVERLAY ******/ |
|---|
| 102 | |
|---|
| 103 | // Storage and interface for edge overlay 'image' buffer. |
|---|
| 104 | // This is so the previous overlay can survive if the module gets unloaded |
|---|
| 105 | // (Need these for the module export list, even if OPT_EDGEOVERLAY is not defined). |
|---|
| 106 | static void* saved_edgebuf = 0; |
|---|
| 107 | static int saved_edgestate = 0; |
|---|
| 108 | |
|---|
| 109 | void module_restore_edge(void **buf, int *state) { *buf = saved_edgebuf; *state = saved_edgestate; } |
|---|
| 110 | void module_save_edge(void* buf, int state) { saved_edgebuf = buf; saved_edgestate = state; } |
|---|
| 111 | |
|---|
| 112 | #ifdef OPT_EDGEOVERLAY |
|---|
| 113 | |
|---|
| 114 | #define MODULE_NAME_EDGEOVR "edgeovr.flt" |
|---|
| 115 | |
|---|
| 116 | struct libedgeovr_sym* libedgeovr; |
|---|
| 117 | |
|---|
| 118 | static int bind_module_edgeovr( void** export_list ) |
|---|
| 119 | { |
|---|
| 120 | return bind_module_generic(export_list, (void**)&libedgeovr, 1, 1, 0); |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | // Return: 0-fail, 1-ok |
|---|
| 124 | struct libedgeovr_sym* module_edgeovr_load() |
|---|
| 125 | { |
|---|
| 126 | // This flag is because edgeovr called each tick |
|---|
| 127 | // If module loading failed, then do not try to load it until reboot |
|---|
| 128 | // (otherwise we will try to load module each tick) |
|---|
| 129 | static int flag_load_fail = 0; |
|---|
| 130 | |
|---|
| 131 | if ( flag_load_fail==0 ) |
|---|
| 132 | { |
|---|
| 133 | module_load_generic((void**)&libedgeovr, MODULE_NAME_EDGEOVR, bind_module_edgeovr, MODULE_FLAG_DISABLE_AUTOUNLOAD); |
|---|
| 134 | |
|---|
| 135 | if (libedgeovr == 0) |
|---|
| 136 | flag_load_fail = 1; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | return libedgeovr; |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | // edgeovr module will never unload to keep its picture |
|---|
| 144 | // void module_edgeovr_unload() {} |
|---|
| 145 | |
|---|
| 146 | #endif |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | /************* DYNAMIC LIBRARY MOTION DETECT ******/ |
|---|
| 150 | |
|---|
| 151 | #define MODULE_NAME_MDETECT "mdetect.flt" |
|---|
| 152 | |
|---|
| 153 | struct libmotiondetect_sym* libmotiondetect; |
|---|
| 154 | |
|---|
| 155 | static int bind_module_motiondetect( void** export_list ) |
|---|
| 156 | { |
|---|
| 157 | return bind_module_generic(export_list, (void**)&libmotiondetect, 1, 1, 0); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | // Return: 0-fail, 1-ok |
|---|
| 161 | struct libmotiondetect_sym* module_mdetect_load() |
|---|
| 162 | { |
|---|
| 163 | return module_load_generic((void**)&libmotiondetect, MODULE_NAME_MDETECT, bind_module_motiondetect, MODULE_FLAG_DISABLE_AUTOUNLOAD); |
|---|
| 164 | } |
|---|
| 165 | |
|---|
| 166 | |
|---|
| 167 | /************* DYNAMIC LIBRARY ZEBRA ******/ |
|---|
| 168 | |
|---|
| 169 | #define MODULE_NAME_ZEBRA "zebra.flt" |
|---|
| 170 | |
|---|
| 171 | struct libzebra_sym* libzebra; |
|---|
| 172 | |
|---|
| 173 | static int bind_module_zebra( void** export_list ) |
|---|
| 174 | { |
|---|
| 175 | return bind_module_generic(export_list, (void**)&libzebra, 1, 1, 0); |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | // Return: 0-fail, 1-ok |
|---|
| 179 | struct libzebra_sym* module_zebra_load() |
|---|
| 180 | { |
|---|
| 181 | return module_load_generic((void**)&libzebra, MODULE_NAME_ZEBRA, bind_module_zebra, MODULE_FLAG_DISABLE_AUTOUNLOAD); |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | |
|---|
| 185 | /************* DYNAMIC LIBRARY CURVES ******/ |
|---|
| 186 | |
|---|
| 187 | #if defined(OPT_CURVES) |
|---|
| 188 | |
|---|
| 189 | #define MODULE_NAME_CURVES "curves.flt" |
|---|
| 190 | |
|---|
| 191 | struct libcurves_sym* libcurves; |
|---|
| 192 | |
|---|
| 193 | static int bind_module_curves( void** export_list ) |
|---|
| 194 | { |
|---|
| 195 | return bind_module_generic(export_list, (void**)&libcurves, 1, 1, 0); |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | // Return: 0-fail, addr-ok |
|---|
| 199 | struct libcurves_sym* module_curves_load() |
|---|
| 200 | { |
|---|
| 201 | return module_load_generic((void**)&libcurves, MODULE_NAME_CURVES, bind_module_curves, 0); |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | #endif |
|---|
| 205 | |
|---|
| 206 | |
|---|
| 207 | /************* DYNAMIC LIBRARY GRIDS ******/ |
|---|
| 208 | |
|---|
| 209 | #define MODULE_NAME_GRIDS "grids.flt" |
|---|
| 210 | |
|---|
| 211 | struct libgrids_sym* libgrids; |
|---|
| 212 | |
|---|
| 213 | static int bind_module_grids( void** export_list ) |
|---|
| 214 | { |
|---|
| 215 | return bind_module_generic(export_list, (void**)&libgrids, 1, 1, 0); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | // Return: 0-fail, addr-ok |
|---|
| 219 | struct libgrids_sym* module_grids_load() |
|---|
| 220 | { |
|---|
| 221 | return module_load_generic((void**)&libgrids, MODULE_NAME_GRIDS, bind_module_grids, 0); |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | |
|---|
| 225 | /************* MODULE PALETTE ******/ |
|---|
| 226 | |
|---|
| 227 | void module_palette_run(int mode, color st_color, void (*on_select)(color clr)) |
|---|
| 228 | { |
|---|
| 229 | unsigned int argv[] = { mode, |
|---|
| 230 | (unsigned int)st_color, |
|---|
| 231 | (unsigned int)on_select }; |
|---|
| 232 | module_run("palette.flt", 0, sizeof(argv)/sizeof(argv[0]), argv, UNLOAD_IF_ERR); |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | |
|---|
| 236 | /************* MODULE MPOPUP ******/ |
|---|
| 237 | |
|---|
| 238 | void module_mpopup_init(struct mpopup_item* popup_actions, const unsigned int flags, void (*on_select)(unsigned int actn), int mode) |
|---|
| 239 | { |
|---|
| 240 | unsigned int argv[] = { (unsigned int)popup_actions, |
|---|
| 241 | (unsigned int)flags, |
|---|
| 242 | (unsigned int)on_select, |
|---|
| 243 | (unsigned int)mode, |
|---|
| 244 | }; |
|---|
| 245 | module_run("mpopup.flt", 0, sizeof(argv)/sizeof(argv[0]), argv, UNLOAD_IF_ERR); |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | |
|---|
| 249 | /************* MODULE FSELECT ******/ |
|---|
| 250 | |
|---|
| 251 | void module_fselect_init_w_mode(int title, const char* prev_dir, const char* default_dir, void (*on_select)(const char *fn), unsigned int key_redraw_mode) |
|---|
| 252 | { |
|---|
| 253 | unsigned int argv[] = { title, |
|---|
| 254 | (unsigned int)prev_dir, |
|---|
| 255 | (unsigned int)default_dir, |
|---|
| 256 | (unsigned int)on_select, |
|---|
| 257 | (unsigned int)key_redraw_mode, |
|---|
| 258 | }; |
|---|
| 259 | module_run("fselect.flt", 0, sizeof(argv)/sizeof(argv[0]), argv, UNLOAD_IF_ERR); |
|---|
| 260 | } |
|---|
| 261 | |
|---|
| 262 | void module_fselect_init(int title, const char* prev_dir, const char* default_dir, void (*on_select)(const char *fn)) |
|---|
| 263 | { |
|---|
| 264 | module_fselect_init_w_mode(title, prev_dir, default_dir, on_select, 0/*key_redraw_mode*/); |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | /************* MODULE DNG ******/ |
|---|
| 268 | |
|---|
| 269 | #define MODULE_NAME_DNG "_dng.flt" |
|---|
| 270 | |
|---|
| 271 | // This is to keep module in memory while it required by anyone |
|---|
| 272 | static int module_dng_semaphore; |
|---|
| 273 | |
|---|
| 274 | struct libdng_sym* libdng; |
|---|
| 275 | |
|---|
| 276 | void module_dng_unload(int owner) |
|---|
| 277 | { |
|---|
| 278 | #if DNG_SUPPORT |
|---|
| 279 | if (libdng==0) |
|---|
| 280 | return; |
|---|
| 281 | |
|---|
| 282 | module_dng_semaphore &= ~owner; |
|---|
| 283 | if (module_dng_semaphore) |
|---|
| 284 | return; |
|---|
| 285 | |
|---|
| 286 | module_unload(MODULE_NAME_DNG); |
|---|
| 287 | #endif |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | static int bind_module_dng( void** export_list ) |
|---|
| 291 | { |
|---|
| 292 | return bind_module_generic(export_list, (void**)&libdng, 1, 1, 0); |
|---|
| 293 | } |
|---|
| 294 | |
|---|
| 295 | // Return: 0-fail, otherwise - bind list |
|---|
| 296 | struct libdng_sym* module_dng_load(int owner) |
|---|
| 297 | { |
|---|
| 298 | #if DNG_SUPPORT |
|---|
| 299 | module_dng_semaphore |= owner; |
|---|
| 300 | |
|---|
| 301 | module_load_generic((void**)&libdng, MODULE_NAME_DNG, bind_module_dng, MODULE_FLAG_DISABLE_AUTOUNLOAD); |
|---|
| 302 | |
|---|
| 303 | if (libdng == 0) |
|---|
| 304 | module_dng_semaphore=0; |
|---|
| 305 | |
|---|
| 306 | return libdng; |
|---|
| 307 | #else |
|---|
| 308 | return 0; |
|---|
| 309 | #endif |
|---|
| 310 | } |
|---|
| 311 | |
|---|
| 312 | // Make convertion or check operation exsitsing |
|---|
| 313 | // Parameter: fn = filename or 0 to just check is operation possible |
|---|
| 314 | // Return: 0-fail, 1-ok |
|---|
| 315 | //-------------------------------------------------------- |
|---|
| 316 | int module_convert_dng_to_chdk_raw(char* fn) |
|---|
| 317 | { |
|---|
| 318 | #if DNG_SUPPORT |
|---|
| 319 | if ( fn==0 ) |
|---|
| 320 | return module_check_is_exist(MODULE_NAME_DNG); |
|---|
| 321 | if ( !module_dng_load(LIBDNG_OWNED_BY_CONVERT) ) |
|---|
| 322 | return 0; |
|---|
| 323 | libdng->convert_dng_to_chdk_raw(fn); |
|---|
| 324 | module_dng_unload(LIBDNG_OWNED_BY_CONVERT); |
|---|
| 325 | return 1; |
|---|
| 326 | #else |
|---|
| 327 | return 0; |
|---|
| 328 | #endif |
|---|
| 329 | } |
|---|
| 330 | |
|---|
| 331 | |
|---|
| 332 | /************* MODULE TBOX ******/ |
|---|
| 333 | |
|---|
| 334 | #define MODULE_NAME_TBOX "_tbox.flt" |
|---|
| 335 | |
|---|
| 336 | struct libtextbox_sym* libtextbox; |
|---|
| 337 | |
|---|
| 338 | static int bind_module_tbox( void** export_list ) |
|---|
| 339 | { |
|---|
| 340 | return bind_module_generic(export_list, (void**)&libtextbox, 1, 1, 0); |
|---|
| 341 | } |
|---|
| 342 | |
|---|
| 343 | // Return: 0-fail, addr-ok |
|---|
| 344 | struct libtextbox_sym* module_tbox_load() |
|---|
| 345 | { |
|---|
| 346 | return module_load_generic((void**)&libtextbox, MODULE_NAME_TBOX, bind_module_tbox, 0); |
|---|
| 347 | } |
|---|