luaA_Type dt_lua_init_type_typeid(lua_State* L, const char*type_name,size_t size) { luaA_Type my_type = luaA_type_add(type_name,size); luaL_newmetatable(L,type_name); lua_pushstring(L,type_name); lua_setfield(L,-2,"__luaA_TypeName"); lua_pushnumber(L,my_type); lua_setfield(L,-2,"__luaA_Type"); luaA_conversion_typeid(my_type,dt_lua_autotype_full_pushfunc,dt_lua_autotype_tofunc); lua_pushcfunction(L,dt_lua_autotype_next); lua_setfield(L,-2,"__next"); lua_pushcfunction(L,dt_lua_autotype_pairs); lua_setfield(L,-2,"__pairs"); lua_pushcfunction(L,dt_lua_autotype_index); lua_setfield(L,-2,"__index"); lua_pushcfunction(L,dt_lua_autotype_newindex); lua_setfield(L,-2,"__newindex"); lua_newtable(L); lua_setfield(L,-2,"__get"); lua_newtable(L); lua_setfield(L,-2,"__set"); // remove the metatable lua_pop(L,1); return my_type; }
int dt_lua_init_early_types(lua_State *L) { luaA_conversion(L, char_20, push_char_array, to_char20); luaA_conversion_push(L, const char_20, push_char_array); luaA_conversion(L, char_32, push_char_array, to_char32); luaA_conversion_push(L, const char_32, push_char_array); luaA_conversion(L, char_52, push_char_array, to_char52); luaA_conversion_push(L, const char_52, push_char_array); luaA_conversion(L, char_64, push_char_array, to_char64); luaA_conversion_push(L, const char_64, push_char_array); luaA_conversion(L, char_128, push_char_array, to_char128); luaA_conversion_push(L, const char_128, push_char_array); luaA_conversion(L, char_256, push_char_array, to_char256); luaA_conversion_push(L, const char_256, push_char_array); luaA_conversion(L, char_512, push_char_array, to_char512); luaA_conversion_push(L, const char_512, push_char_array); luaA_conversion(L, char_1024, push_char_array, to_char1024); luaA_conversion_push(L, const char_1024, push_char_array); luaA_conversion(L, char_filename_length, push_char_array, to_charfilename_length); luaA_conversion_push(L, const char_filename_length, push_char_array); luaA_conversion(L, char_path_length, push_char_array, to_charpath_length); luaA_conversion_push(L, const char_path_length, push_char_array); luaA_conversion(L, int32_t, luaA_push_int, luaA_to_int); luaA_conversion_push(L, const int32_t, luaA_push_int); luaA_conversion_push(L, const_string, luaA_push_const_char_ptr); luaA_conversion(L, protected_double, push_protected_double, luaA_to_double); luaA_conversion(L, progress_double, push_progress_double, to_progress_double); luaA_conversion_push_type(L, luaA_type_add(L,"unknown",sizeof(void*)), unknown_pushfunc); // table of gpointer values lua_newtable(L); lua_newtable(L); lua_pushstring(L, "kv"); lua_setfield(L, -2, "__mode"); lua_setmetatable(L, -2); lua_setfield(L, LUA_REGISTRYINDEX, "dt_lua_gpointer_values"); luaA_enum(L,dt_lua_orientation_t); luaA_enum_value_name(L,dt_lua_orientation_t,GTK_ORIENTATION_HORIZONTAL,"horizontal"); luaA_enum_value_name(L,dt_lua_orientation_t,GTK_ORIENTATION_VERTICAL,"vertical"); luaA_enum(L, dt_lua_align_t); luaA_enum_value_name(L, dt_lua_align_t, GTK_ALIGN_FILL, "fill"); luaA_enum_value_name(L, dt_lua_align_t, GTK_ALIGN_START, "start"); luaA_enum_value_name(L, dt_lua_align_t, GTK_ALIGN_END, "end"); luaA_enum_value_name(L, dt_lua_align_t, GTK_ALIGN_CENTER, "center"); luaA_enum_value_name(L, dt_lua_align_t, GTK_ALIGN_BASELINE, "baseline"); luaA_enum(L, dt_lua_ellipsize_mode_t); luaA_enum_value_name(L, dt_lua_ellipsize_mode_t, PANGO_ELLIPSIZE_NONE, "none"); luaA_enum_value_name(L, dt_lua_ellipsize_mode_t, PANGO_ELLIPSIZE_START, "start"); luaA_enum_value_name(L, dt_lua_ellipsize_mode_t, PANGO_ELLIPSIZE_MIDDLE, "middle"); luaA_enum_value_name(L, dt_lua_ellipsize_mode_t, PANGO_ELLIPSIZE_END, "end"); return 0; }
static int dt_imageio_load_module_format (dt_imageio_module_format_t *module, const char *libname, const char *plugin_name) { module->widget = NULL; module->parameter_lua_type = LUAA_INVALID_TYPE; g_strlcpy(module->plugin_name, plugin_name, sizeof(module->plugin_name)); module->module = g_module_open(libname, G_MODULE_BIND_LAZY); if(!module->module) goto error; int (*version)(); if(!g_module_symbol(module->module, "dt_module_dt_version", (gpointer)&(version))) goto error; if(version() != dt_version()) { fprintf(stderr, "[imageio_load_module] `%s' is compiled for another version of dt (module %d (%s) != dt %d (%s)) !\n", libname, abs(version()), version() < 0 ? "debug" : "opt", abs(dt_version()), dt_version() < 0 ? "debug" : "opt"); goto error; } if(!g_module_symbol(module->module, "name", (gpointer)&(module->name))) goto error; if(!g_module_symbol(module->module, "init", (gpointer)&(module->init))) goto error; if(!g_module_symbol(module->module, "cleanup", (gpointer)&(module->cleanup))) goto error; if(!g_module_symbol(module->module, "gui_reset", (gpointer)&(module->gui_reset))) goto error; if(!g_module_symbol(module->module, "gui_init", (gpointer)&(module->gui_init))) goto error; if(!g_module_symbol(module->module, "gui_cleanup", (gpointer)&(module->gui_cleanup))) goto error; if(!g_module_symbol(module->module, "mime", (gpointer)&(module->mime))) goto error; if(!g_module_symbol(module->module, "extension", (gpointer)&(module->extension))) goto error; if(!g_module_symbol(module->module, "dimension", (gpointer)&(module->dimension))) module->dimension = _default_format_dimension; if(!g_module_symbol(module->module, "params_size", (gpointer)&(module->params_size))) goto error; if(!g_module_symbol(module->module, "get_params", (gpointer)&(module->get_params))) goto error; if(!g_module_symbol(module->module, "free_params", (gpointer)&(module->free_params))) goto error; if(!g_module_symbol(module->module, "set_params", (gpointer)&(module->set_params))) goto error; if(!g_module_symbol(module->module, "write_image", (gpointer)&(module->write_image))) goto error; if(!g_module_symbol(module->module, "bpp", (gpointer)&(module->bpp))) goto error; if(!g_module_symbol(module->module, "flags", (gpointer)&(module->flags))) module->flags = _default_format_flags; if(!g_module_symbol(module->module, "levels", (gpointer)&(module->levels))) module->levels = _default_format_levels; if(!g_module_symbol(module->module, "read_image", (gpointer)&(module->read_image))) module->read_image = NULL; #ifdef USE_LUA { char pseudo_type_name[1024]; snprintf(pseudo_type_name,sizeof(pseudo_type_name),"dt_imageio_module_format_data_%s",module->plugin_name); luaA_Type my_type = luaA_type_add(pseudo_type_name,module->params_size(module)); module->parameter_lua_type = dt_lua_init_type_typeid(darktable.lua_state.state,my_type); luaA_struct_typeid(darktable.lua_state.state,my_type); dt_lua_register_format_typeid(darktable.lua_state.state,module,my_type); #endif module->init(module); #ifdef USE_LUA dt_lua_register_type_callback_type_typeid(darktable.lua_state.state,my_type,NULL,NULL,my_type); } #endif return 0; error: fprintf(stderr, "[imageio_load_module] failed to open format `%s': %s\n", plugin_name, g_module_error()); if(module->module) g_module_close(module->module); return 1; }
static int register_storage(lua_State *L) { lua_settop(L,5); lua_getfield(L,LUA_REGISTRYINDEX,"dt_lua_storages"); lua_newtable(L); dt_imageio_module_storage_t * storage = malloc(sizeof(dt_imageio_module_storage_t)); memcpy(storage,&ref_storage,sizeof(dt_imageio_module_storage_t)); const char * plugin_name = luaL_checkstring(L,1); lua_pushvalue(L,1); lua_setfield(L,-2,"plugin_name"); strncpy(storage->plugin_name,plugin_name,127); luaL_checkstring(L,2); lua_pushvalue(L,2); lua_setfield(L,-2,"name"); if(!lua_isnoneornil(L,3)) { luaL_checktype(L,3,LUA_TFUNCTION); lua_pushvalue(L,3); lua_setfield(L,-2,"store"); } if(lua_isnil(L,4) ) { storage->finalize_store = NULL; } else { luaL_checktype(L,4,LUA_TFUNCTION); lua_pushvalue(L,4); lua_setfield(L,-2,"finalize_store"); } if(!lua_isnoneornil(L,5)) { luaL_checktype(L,5,LUA_TFUNCTION); lua_pushvalue(L,5); lua_setfield(L,-2,"supported"); } lua_setfield(L,-2,plugin_name); char tmp[1024]; snprintf(tmp,1024,"dt_imageio_module_data_pseudo_%s",storage->plugin_name); luaA_Type type_id = luaA_type_add(tmp,storage->params_size(storage)); storage->parameter_lua_type = dt_lua_init_type_typeid(darktable.lua_state,type_id); luaA_struct_typeid(darktable.lua_state,type_id); //dt_lua_register_type_callback_default_typeid(L,tmp,extra_data_index,extra_data_newindex,extra_data_next); //dt_lua_register_type_callback_number_typeid(L,tmp,extra_data_index,extra_data_newindex,extra_data_length); dt_lua_register_storage_typeid(darktable.lua_state,storage,type_id); dt_imageio_insert_storage(storage); return 0; }
luaA_Type dt_lua_init_singleton(lua_State* L, const char* unique_name) { char tmp_name[1024]; snprintf(tmp_name,1024,"dt_lua_singleton_%s",unique_name); luaA_Type type_id = dt_lua_init_type_typeid(L,luaA_type_add(tmp_name,sizeof(void*))); void *tmp=NULL; luaA_push_typeid(L,type_id,&tmp); lua_getmetatable(L,-1); lua_pushboolean(L,true); lua_setfield(L,-2,"__is_singleton"); lua_pop(L,1); return type_id; }
luaA_Type dt_lua_init_widget_type_type(lua_State *L, dt_lua_widget_type_t* widget_type,const char* lua_type,GType gtk_type) { luaA_Type type_id = dt_lua_init_gpointer_type_type(L,luaA_type_add(L,lua_type,sizeof(gpointer))); widget_type->associated_type = type_id; widget_type->gtk_type = gtk_type; dt_lua_type_register_parent_type(L, type_id, widget_type->parent->associated_type); lua_newtable(L); dt_lua_type_setmetafield_type(L,type_id,"__gtk_signals"); // add to the table lua_pushlightuserdata(L, widget_type); lua_pushcclosure(L, get_widget_params, 1); lua_pushcclosure(L,dt_lua_gtk_wrap,1); dt_lua_module_entry_new(L, -1, "widget", widget_type->name); lua_pop(L, 1); return type_id; };
int dt_lua_init_early_types(lua_State *L) { luaA_conversion(L, char_20, push_char_array, to_char20); luaA_conversion_push(L, const char_20, push_char_array); luaA_conversion(L, char_32, push_char_array, to_char32); luaA_conversion_push(L, const char_32, push_char_array); luaA_conversion(L, char_52, push_char_array, to_char52); luaA_conversion_push(L, const char_52, push_char_array); luaA_conversion(L, char_64, push_char_array, to_char64); luaA_conversion_push(L, const char_64, push_char_array); luaA_conversion(L, char_128, push_char_array, to_char128); luaA_conversion_push(L, const char_128, push_char_array); luaA_conversion(L, char_256, push_char_array, to_char256); luaA_conversion_push(L, const char_256, push_char_array); luaA_conversion(L, char_512, push_char_array, to_char512); luaA_conversion_push(L, const char_512, push_char_array); luaA_conversion(L, char_1024, push_char_array, to_char1024); luaA_conversion_push(L, const char_1024, push_char_array); luaA_conversion(L, char_filename_length, push_char_array, to_charfilename_length); luaA_conversion_push(L, const char_filename_length, push_char_array); luaA_conversion(L, char_path_length, push_char_array, to_charpath_length); luaA_conversion_push(L, const char_path_length, push_char_array); luaA_conversion(L, int32_t, luaA_push_int, luaA_to_int); luaA_conversion_push(L, const int32_t, luaA_push_int); luaA_conversion_push(L, const_string, luaA_push_const_char_ptr); luaA_conversion(L, protected_double, push_protected_double, luaA_to_double); luaA_conversion(L, progress_double, push_progress_double, to_progress_double); luaA_conversion_push_type(L, luaA_type_add(L,"unknown",sizeof(void*)), unknown_pushfunc); // table of gpointer values lua_newtable(L); lua_newtable(L); lua_pushstring(L, "kv"); lua_setfield(L, -2, "__mode"); lua_setmetatable(L, -2); lua_setfield(L, LUA_REGISTRYINDEX, "dt_lua_gpointer_values"); return 0; }
luaA_Type dt_lua_init_singleton(lua_State *L, const char *unique_name, void *data) { char tmp_name[1024]; snprintf(tmp_name, sizeof(tmp_name), "dt_lua_singleton_%s", unique_name); luaA_Type type_id = luaA_type_add(L, tmp_name, sizeof(void *)); init_metatable(L, type_id); void **udata = lua_newuserdata(L, sizeof(void *)); lua_newtable(L); lua_setuservalue(L, -2); if(!data) { memset(udata, 0, sizeof(void *)); } else { *udata = data; luaL_getsubtable(L, LUA_REGISTRYINDEX, "dt_lua_gpointer_values"); lua_pushlightuserdata(L, data); lua_pushvalue(L,-3); lua_settable(L,-3); lua_pop(L,1); } lua_pushvalue(L, -1); luaL_setmetatable(L, tmp_name); lua_setfield(L, -3, "__singleton"); if(luaL_getmetafield(L, -1, "__init")) { lua_pushvalue(L, -2); // the new alocated object lua_pushlightuserdata(L, (void *)data); // forced to cast.. lua_call(L, 2, 0); } lua_remove(L, -2); return type_id; }
static int register_storage(lua_State *L) { lua_settop(L, 7); lua_getfield(L, LUA_REGISTRYINDEX, "dt_lua_storages"); lua_newtable(L); dt_imageio_module_storage_t *storage = malloc(sizeof(dt_imageio_module_storage_t)); memcpy(storage, &ref_storage, sizeof(dt_imageio_module_storage_t)); storage->gui_data = malloc(sizeof(lua_storage_gui_t)); lua_storage_gui_t *data = storage->gui_data; const char *plugin_name = luaL_checkstring(L, 1); lua_pushvalue(L, 1); lua_setfield(L, -2, "plugin_name"); g_strlcpy(storage->plugin_name, plugin_name, sizeof(storage->plugin_name)); const char *name = luaL_checkstring(L, 2); lua_pushvalue(L, 2); lua_setfield(L, -2, "name"); data->name = strdup(name); data->supported_formats = NULL; data->widget = NULL; if(!lua_isnoneornil(L, 3)) { luaL_checktype(L, 3, LUA_TFUNCTION); lua_pushvalue(L, 3); lua_setfield(L, -2, "store"); } if(lua_isnil(L, 4)) { storage->finalize_store = NULL; } else { luaL_checktype(L, 4, LUA_TFUNCTION); lua_pushvalue(L, 4); lua_setfield(L, -2, "finalize_store"); } if(!lua_isnoneornil(L, 5)) { luaL_checktype(L, 5, LUA_TFUNCTION); lua_pushvalue(L, 5); lua_setfield(L, -2, "supported"); } if(lua_isnil(L, 6)) { storage->initialize_store = NULL; } else { luaL_checktype(L, 6, LUA_TFUNCTION); lua_pushvalue(L, 6); lua_setfield(L, -2, "initialize_store"); } if(lua_isnil(L, 7)) { storage->gui_init = empty_wrapper; storage->gui_reset = empty_wrapper; storage->gui_cleanup = empty_wrapper; } else { lua_widget widget; luaA_to(L,lua_widget,&widget,7); dt_lua_widget_bind(L,widget); data->widget = widget; } lua_setfield(L, -2, plugin_name); char tmp[1024]; snprintf(tmp, sizeof(tmp), "dt_imageio_module_data_pseudo_%s", storage->plugin_name); luaA_Type type_id = luaA_type_add(L, tmp, storage->params_size(storage)); storage->parameter_lua_type = dt_lua_init_type_type(darktable.lua_state.state, type_id); luaA_struct_type(darktable.lua_state.state, type_id); dt_lua_register_storage_type(darktable.lua_state.state, storage, type_id); GList *it = darktable.imageio->plugins_format; if(!lua_isnoneornil(L, 5)) { while(it) { lua_pushvalue(L, 5); dt_imageio_module_format_t *format = (dt_imageio_module_format_t *)it->data; dt_imageio_module_data_t *sdata = storage->get_params(storage); dt_imageio_module_data_t *fdata = format->get_params(format); luaA_push_type(L, storage->parameter_lua_type, sdata); luaA_push_type(L, format->parameter_lua_type, fdata); format->free_params(format, fdata); storage->free_params(storage, sdata); dt_lua_treated_pcall(L,2,1); int result = lua_toboolean(L, -1); lua_pop(L, 1); if(result) { data->supported_formats = g_list_prepend(data->supported_formats, format); } it = g_list_next(it); } } else { // all formats are supported while(it) { dt_imageio_module_format_t *format = (dt_imageio_module_format_t *)it->data; data->supported_formats = g_list_prepend(data->supported_formats, format); it = g_list_next(it); } } storage->gui_init(storage); if(storage->widget) g_object_ref(storage->widget); dt_imageio_insert_storage(storage); return 0; }
static int dt_imageio_load_module_storage(dt_imageio_module_storage_t *module, const char *libname, const char *plugin_name) { module->widget = NULL; module->parameter_lua_type = LUAA_INVALID_TYPE; g_strlcpy(module->plugin_name, plugin_name, sizeof(module->plugin_name)); dt_print(DT_DEBUG_CONTROL, "[imageio_load_module] loading storage module `%s' from %s\n", plugin_name, libname); module->module = g_module_open(libname, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL); if(!module->module) goto error; int (*version)(); if(!g_module_symbol(module->module, "dt_module_dt_version", (gpointer) & (version))) goto error; if(version() != dt_version()) { fprintf( stderr, "[imageio_load_module] `%s' is compiled for another version of dt (module %d (%s) != dt %d (%s)) !\n", libname, abs(version()), version() < 0 ? "debug" : "opt", abs(dt_version()), dt_version() < 0 ? "debug" : "opt"); goto error; } if(!g_module_symbol(module->module, "dt_module_mod_version", (gpointer) & (module->version))) goto error; if(!g_module_symbol(module->module, "name", (gpointer) & (module->name))) goto error; if(!g_module_symbol(module->module, "gui_reset", (gpointer) & (module->gui_reset))) goto error; if(darktable.gui) { if(!g_module_symbol(module->module, "gui_init", (gpointer) & (module->gui_init))) goto error; } else { module->gui_init = _default_storage_nop; } if(!g_module_symbol(module->module, "gui_cleanup", (gpointer) & (module->gui_cleanup))) goto error; if(!g_module_symbol(module->module, "init", (gpointer) & (module->init))) goto error; if(!g_module_symbol(module->module, "store", (gpointer) & (module->store))) goto error; if(!g_module_symbol(module->module, "legacy_params", (gpointer) & (module->legacy_params))) module->legacy_params = NULL; if(!g_module_symbol(module->module, "params_size", (gpointer) & (module->params_size))) goto error; if(!g_module_symbol(module->module, "get_params", (gpointer) & (module->get_params))) goto error; if(!g_module_symbol(module->module, "free_params", (gpointer) & (module->free_params))) goto error; if(!g_module_symbol(module->module, "initialize_store", (gpointer) & (module->initialize_store))) module->initialize_store = NULL; if(!g_module_symbol(module->module, "finalize_store", (gpointer) & (module->finalize_store))) module->finalize_store = NULL; if(!g_module_symbol(module->module, "set_params", (gpointer) & (module->set_params))) goto error; if(!g_module_symbol(module->module, "supported", (gpointer) & (module->supported))) module->supported = _default_supported; if(!g_module_symbol(module->module, "dimension", (gpointer) & (module->dimension))) module->dimension = _default_storage_dimension; if(!g_module_symbol(module->module, "recommended_dimension", (gpointer) & (module->recommended_dimension))) module->recommended_dimension = _default_storage_dimension; if(!g_module_symbol(module->module, "export_dispatched", (gpointer) & (module->export_dispatched))) module->export_dispatched = _default_storage_nop; #ifdef USE_LUA { char pseudo_type_name[1024]; snprintf(pseudo_type_name, sizeof(pseudo_type_name), "dt_imageio_module_storage_data_%s", module->plugin_name); luaA_Type my_type = luaA_type_add(darktable.lua_state.state, pseudo_type_name, module->params_size(module)); module->parameter_lua_type = dt_lua_init_type_type(darktable.lua_state.state, my_type); luaA_struct_type(darktable.lua_state.state, my_type); dt_lua_register_storage_type(darktable.lua_state.state, module, my_type); #endif module->init(module); #ifdef USE_LUA lua_pushcfunction(darktable.lua_state.state, dt_lua_type_member_luaautoc); dt_lua_type_register_struct_type(darktable.lua_state.state, my_type); } #endif return 0; error: fprintf(stderr, "[imageio_load_module] failed to open storage `%s': %s\n", plugin_name, g_module_error()); if(module->module) g_module_close(module->module); return 1; }