static void initialize_store_wrapper (struct dt_imageio_module_storage_t *self, dt_imageio_module_data_t *data, dt_imageio_module_format_t *format, dt_imageio_module_data_t *fdata, GList **images, const gboolean high_quality) { gboolean has_lock = dt_lua_lock(); lua_State *L =darktable.lua_state.state; lua_getfield(L,LUA_REGISTRYINDEX,"dt_lua_storages"); lua_getfield(L,-1,self->plugin_name); lua_getfield(L,-1,"initialize_store"); if(lua_isnil(L,-1)) { lua_pop(L,3); dt_lua_unlock(has_lock); return; } luaA_push_typeid(L,self->parameter_lua_type,data); luaA_push_typeid(L,format->parameter_lua_type,fdata); GList* imgids =*images; lua_newtable(L); while(imgids) { luaA_push(L,dt_lua_image_t,&(imgids->data)); luaL_ref(L,-2); imgids = g_list_next(imgids); } lua_pushboolean(L,high_quality); lua_storage_t *d = (lua_storage_t*) data; if(!d->data_created) { lua_pushlightuserdata(L,d); lua_newtable(L); lua_settable(L,LUA_REGISTRYINDEX); d->data_created = true; } lua_pushlightuserdata(L,data); lua_gettable(L,LUA_REGISTRYINDEX); dt_lua_do_chunk_silent(L,5,1); if(!lua_isnoneornil(L,-1)) { luaL_checktype(L,-1,LUA_TTABLE); g_list_free(*images); GList *new_images=NULL; lua_pushnil(L); while(lua_next(L,-2)){ dt_lua_image_t imgid; luaA_to(L,dt_lua_image_t,&imgid,-1); new_images = g_list_prepend(new_images,GINT_TO_POINTER(imgid)); lua_pop(L,1); } new_images = g_list_reverse(new_images); *images = new_images; } lua_pop(L,3); dt_lua_unlock(has_lock); }
int dt_lua_dofile_silent(lua_State *L, const char *filename, int nargs, int nresults) { if(luaL_loadfile(L, filename)) { dt_print(DT_DEBUG_LUA, "LUA ERROR %s\n", lua_tostring(L, -1)); lua_pop(L, 1); return -1; } lua_insert(L, -(nargs + 1)); return dt_lua_do_chunk_silent(L, nargs, nresults); }
int dt_lua_dostring_silent(lua_State *L, const char *command, int nargs, int nresults) { int load_result = luaL_loadstring(L, command); if(load_result != LUA_OK) { dt_print(DT_DEBUG_LUA, "LUA ERROR %s\n", lua_tostring(L, -1)); lua_pop(L, 1); return -1; } lua_insert(L, -(nargs + 1)); return dt_lua_do_chunk_silent(L, nargs, nresults); }
void dt_lua_init(lua_State *L, const char *lua_command) { char tmp_path[PATH_MAX] = { 0 }; // init the lua environment lua_CFunction *cur_type = init_funcs; while(*cur_type) { (*cur_type)(L); cur_type++; } assert(lua_gettop(L) == 0); // if you are here, you have probably added an initialisation function that is not stack clean // build the table containing the configuration info lua_getglobal(L, "package"); dt_lua_goto_subtable(L, "loaded"); lua_pushstring(L, "darktable"); dt_lua_push_darktable_lib(L); lua_settable(L, -3); lua_pop(L, 1); lua_getglobal(L, "package"); lua_getfield(L, -1, "path"); lua_pushstring(L, ";"); dt_loc_get_datadir(tmp_path, sizeof(tmp_path)); lua_pushstring(L, tmp_path); lua_pushstring(L, "/lua/?.lua"); lua_pushstring(L, ";"); dt_loc_get_user_config_dir(tmp_path, sizeof(tmp_path)); lua_pushstring(L, tmp_path); lua_pushstring(L, "/lua/?.lua"); lua_concat(L, 7); lua_setfield(L, -2, "path"); lua_pop(L, 1); lua_pushcfunction(L,run_early_script); lua_pushstring(L,lua_command); if(darktable.gui) { dt_lua_do_chunk_later(L,1); } else { dt_lua_do_chunk_silent(L,1,0); } // allow other threads to wake up and do their job dt_lua_unlock(); }
int dt_lua_widget_trigger_callback(lua_State *L) { int nargs = lua_gettop(L) -2; lua_widget * widget; luaA_to(L,lua_widget,&widget,1); const char* name = lua_tostring(L,2); lua_getuservalue(L,1); lua_getfield(L,-1,name); if(!lua_isnil(L,-1)) { lua_pushvalue(L,1); for(int i = 0 ; i < nargs ; i++) { lua_pushvalue(L,i+3); } dt_lua_do_chunk_silent(L,nargs+1,0); dt_lua_redraw_screen(); } return 0; }
static int32_t async_callback_job(dt_job_t *job) { dt_lua_lock(); lua_State* L= darktable.lua_state.state; async_call_data* data = (async_call_data*)dt_control_job_get_params(job); lua_pushcfunction(L,data->pusher); int nargs =0; GList* cur_elt = data->extra; while(cur_elt) { GList * type_type_elt = cur_elt; cur_elt = g_list_next(cur_elt); GList * type_elt = cur_elt; cur_elt = g_list_next(cur_elt); GList * data_elt = cur_elt; cur_elt = g_list_next(cur_elt); switch(GPOINTER_TO_INT(type_type_elt->data)) { case LUA_ASYNC_TYPEID_WITH_FREE: // skip the destructor cur_elt = g_list_next(cur_elt); // do not break case LUA_ASYNC_TYPEID: luaA_push_type(L,GPOINTER_TO_INT(type_elt->data),data_elt->data); break; case LUA_ASYNC_TYPENAME_WITH_FREE: // skip the destructor cur_elt = g_list_next(cur_elt); // do not break case LUA_ASYNC_TYPENAME: luaA_push_type(L,luaA_type_find(L,type_elt->data),&data_elt->data); break; case LUA_ASYNC_DONE: default: // should never happen g_assert(false); break; } nargs++; } dt_lua_do_chunk_silent(L,nargs,0); dt_lua_redraw_screen(); dt_lua_unlock(); return 0; }
static int32_t do_chunk_later_callback(dt_job_t *job) { dt_lua_lock(); lua_State* L= darktable.lua_state.state; int reference = GPOINTER_TO_INT(dt_control_job_get_params(job)); lua_getfield(L, LUA_REGISTRYINDEX, "dt_lua_bg_threads"); lua_pushinteger(L,reference); lua_gettable(L,-2); lua_State* thread = lua_tothread(L,-1); lua_pop(L,2); dt_lua_do_chunk_silent(thread,lua_gettop(thread)-1,0); lua_getfield(L, LUA_REGISTRYINDEX, "dt_lua_bg_threads"); lua_pushinteger(L,reference); lua_pushnil(L); lua_settable(L,-3); lua_pop(L,1); dt_lua_unlock(); return 0; }
static void finalize_store_wrapper (struct dt_imageio_module_storage_t *self, dt_imageio_module_data_t *data) { gboolean has_lock = dt_lua_lock(); lua_State *L =darktable.lua_state.state; lua_getfield(L,LUA_REGISTRYINDEX,"dt_lua_storages"); lua_getfield(L,-1,self->plugin_name); lua_getfield(L,-1,"finalize_store"); if(lua_isnil(L,-1)) { lua_pop(L,3); dt_lua_unlock(has_lock); return; } luaA_push_typeid(L,self->parameter_lua_type,data); lua_storage_t *d = (lua_storage_t*) data; GList* imgids =d->imgids; GList* file_names = d->file_names; lua_newtable(L); while(imgids) { luaA_push(L,dt_lua_image_t,&(imgids->data)); lua_pushstring(L,file_names->data); lua_settable(L,-3); imgids = g_list_next(imgids); file_names = g_list_next(file_names); } if(!d->data_created) { lua_pushlightuserdata(darktable.lua_state.state,d); lua_newtable(darktable.lua_state.state); lua_settable(darktable.lua_state.state,LUA_REGISTRYINDEX); d->data_created = true; } lua_pushlightuserdata(L,data); lua_gettable(L,LUA_REGISTRYINDEX); dt_lua_do_chunk_silent(L,3,0); lua_pop(L,2); dt_lua_unlock(has_lock); }
static int store_wrapper(struct dt_imageio_module_storage_t *self,struct dt_imageio_module_data_t *self_data, const int imgid, dt_imageio_module_format_t *format, dt_imageio_module_data_t *fdata, const int num, const int total, const gboolean high_quality) { /* construct a temporary file name */ char tmpdir[PATH_MAX]= {0}; gboolean from_cache = FALSE; dt_loc_get_tmp_dir (tmpdir, sizeof(tmpdir)); char dirname[PATH_MAX]; dt_image_full_path(imgid, dirname, sizeof(dirname), &from_cache); dt_image_path_append_version(imgid, dirname, sizeof(dirname)); gchar * filename = g_path_get_basename( dirname ); gchar * end = g_strrstr( filename,".")+1; g_strlcpy( end, format->extension(fdata), sizeof(dirname)-(end-dirname)); gchar* complete_name = g_build_filename( tmpdir, filename, (char *)NULL ); if(dt_imageio_export(imgid, complete_name, format, fdata, high_quality,FALSE,self,self_data) != 0) { fprintf(stderr, "[%s] could not export to file: `%s'!\n", self->name(self),complete_name); g_free(complete_name); g_free(filename); return 1; } lua_storage_t *d = (lua_storage_t*) self_data; d->imgids = g_list_prepend(d->imgids,(void*)(intptr_t)imgid); d->file_names = g_list_prepend(d->file_names,complete_name); gboolean has_lock = dt_lua_lock(); if(!d->data_created) { lua_pushlightuserdata(darktable.lua_state.state,d); lua_newtable(darktable.lua_state.state); lua_settable(darktable.lua_state.state,LUA_REGISTRYINDEX); d->data_created = true; } lua_State *L =darktable.lua_state.state; lua_getfield(L,LUA_REGISTRYINDEX,"dt_lua_storages"); lua_getfield(L,-1,self->plugin_name); lua_getfield(L,-1,"store"); if(lua_isnil(L,-1)) { lua_pop(L,3); dt_lua_unlock(has_lock); g_free(filename); return 1; } luaA_push_typeid(L,self->parameter_lua_type,self_data); luaA_push(L,dt_lua_image_t,&imgid); luaA_push_typeid(L,format->parameter_lua_type,fdata); lua_pushstring(L,complete_name); lua_pushnumber(L,num); lua_pushnumber(L,total); lua_pushboolean(L,high_quality); lua_pushlightuserdata(L,self_data); lua_gettable(L,LUA_REGISTRYINDEX); dt_lua_do_chunk_silent(L,8,1); int result = lua_toboolean(L,-1); lua_pop(L,3); dt_lua_unlock(has_lock); g_free(filename); return result; }
static int register_storage(lua_State *L) { lua_settop(L,6); 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; 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"); } 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(tmp,storage->params_size(storage)); storage->parameter_lua_type = dt_lua_init_type_typeid(darktable.lua_state.state,type_id); luaA_struct_typeid(darktable.lua_state.state,type_id); dt_lua_register_storage_typeid(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_typeid(L,storage->parameter_lua_type,sdata); luaA_push_typeid(L,format->parameter_lua_type,fdata); format->free_params(format,fdata); storage->free_params(storage,sdata); dt_lua_do_chunk_silent(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); } } dt_imageio_insert_storage(storage); return 0; }