Skip to content

Commit

Permalink
Lua Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Butti committed Feb 17, 2019
1 parent e79f059 commit 64cb37d
Show file tree
Hide file tree
Showing 8 changed files with 292 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .cproject
Expand Up @@ -35,6 +35,7 @@
<listOptionValue builtIn="false" value="/usr/lib/x86_64-linux-gnu/glib-2.0/include"/>
<listOptionValue builtIn="false" value="/usr/include/libxml2"/>
<listOptionValue builtIn="false" value="/usr/include/librsvg-2.0"/>
<listOptionValue builtIn="false" value="/usr/include/lua5.3"/>
</option>
<inputType id="cdt.managedbuild.tool.gnu.c.compiler.input.1449567466" superClass="cdt.managedbuild.tool.gnu.c.compiler.input"/>
</tool>
Expand All @@ -48,6 +49,7 @@
<listOptionValue builtIn="false" value="/usr/lib/x86_64-linux-gnu/glib-2.0/include"/>
<listOptionValue builtIn="false" value="/usr/include/libxml2"/>
<listOptionValue builtIn="false" value="/usr/include/librsvg-2.0"/>
<listOptionValue builtIn="false" value="/usr/include/lua5.3"/>
</option>
<inputType id="cdt.managedbuild.tool.gnu.cpp.compiler.input.2018836825" superClass="cdt.managedbuild.tool.gnu.cpp.compiler.input"/>
</tool>
Expand Down
9 changes: 8 additions & 1 deletion plugins/Example/main.lua
@@ -1,3 +1,10 @@
-- This is an example Xournal++ Plugin - copy this to get started

print("test123\n");
print("Hello from Lua Plugin\n");

-- Register all Toolbar actions and intialize all UI stuff
function initUi()
print("Plugin initUi called\n");
end


16 changes: 1 addition & 15 deletions src/control/Control.cpp
Expand Up @@ -70,7 +70,6 @@ Control::Control(GladeSearchpath* gladeSearchPath)
{
XOJ_INIT_TYPE(Control);

this->win = NULL;
this->recent = new RecentManager();
this->undoRedo = new UndoRedoHandler(this);
this->recent->addListener(this);
Expand All @@ -97,22 +96,12 @@ Control::Control(GladeSearchpath* gladeSearchPath)
this->pageTypes = new PageTypeHandler(gladeSearchPath);
this->newPageType = new PageTypeMenu(this->pageTypes, settings, true, true);

this->sidebar = NULL;
this->searchBar = NULL;

this->audioController = new AudioController(this->settings,this);

this->scrollHandler = new ScrollHandler(this);

this->scheduler = new XournalScheduler();

this->autosaveTimeout = 0;

this->statusbar = NULL;
this->lbState = NULL;
this->pgState = NULL;
this->maxState = 0;

this->doc = new Document(this);

// for crashhandling
Expand All @@ -129,10 +118,6 @@ Control::Control(GladeSearchpath* gladeSearchPath)
*/
this->changeTimout = g_timeout_add_seconds(5, (GSourceFunc) checkChangedDocument, this);

this->clipboardHandler = NULL;

this->dragDropHandler = NULL;

this->pageBackgroundChangeController = new PageBackgroundChangeController(this);

this->layerController = new LayerController(this);
Expand All @@ -141,6 +126,7 @@ Control::Control(GladeSearchpath* gladeSearchPath)
this->fullscreenHandler = new FullscreenHandler(settings);

this->pluginController = new PluginController(this);
this->pluginController->registerToolbar();
}

Control::~Control()
Expand Down
22 changes: 11 additions & 11 deletions src/control/Control.h
Expand Up @@ -304,12 +304,12 @@ class Control :
ZoomControl* zoom;

Settings* settings;
MainWindow* win;
MainWindow* win = NULL;

Document* doc;
Document* doc = NULL;

Sidebar* sidebar;
SearchBar* searchBar;
Sidebar* sidebar = NULL;
SearchBar* searchBar = NULL;

ToolHandler* toolHandler;

Expand All @@ -321,7 +321,7 @@ class Control :

AudioController* audioController;

ToolbarDragDropHandler* dragDropHandler;
ToolbarDragDropHandler* dragDropHandler = NULL;

/**
* The cursor handler
Expand All @@ -341,23 +341,23 @@ class Control :
/**
* Our clipboard abstraction
*/
ClipboardHandler* clipboardHandler;
ClipboardHandler* clipboardHandler = NULL;

/**
* The autosave handler ID
*/
int autosaveTimeout;
int autosaveTimeout = 0;
Path lastAutosaveFilename;

XournalScheduler* scheduler;

/**
* State / Blocking attributes
*/
GtkWidget* statusbar;
GtkLabel* lbState;
GtkProgressBar* pgState;
int maxState;
GtkWidget* statusbar = NULL;
GtkLabel* lbState = NULL;
GtkProgressBar* pgState = NULL;
int maxState = 0;
bool isBlocking;

GladeSearchpath* gladeSearchPath;
Expand Down
186 changes: 178 additions & 8 deletions src/plugin/Plugin.cpp
@@ -1,27 +1,197 @@
#include "Plugin.h"

#include <config-features.h>
#include <config.h>

#ifdef ENABLE_PLUGINS

Plugin::Plugin(string path)
: path(path)
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>

#define LOAD_FROM_INI(target, group, key) \
{ \
char* value = g_key_file_get_string(config, group, key, NULL); \
if (value != NULL) \
{ \
target = value; \
g_free(value); \
} \
}


Plugin::Plugin(string name, string path)
: name(name),
path(path)
{
XOJ_INIT_TYPE(Plugin);


loadIni();
loadScript();
}

Plugin::~Plugin()
{
XOJ_CHECK_TYPE(Plugin);

if (lua)
{
// Clean up, free the Lua state var
lua_close(lua);
lua = NULL;
}

XOJ_RELEASE_TYPE(Plugin);
}

//void Plugin::aaaaaa()
//{
// XOJ_CHECK_TYPE(Plugin);
//}

/**
* Register toolbar item and all other UI stuff
*/
void Plugin::registerToolbar()
{
XOJ_CHECK_TYPE(Plugin);

if (!this->valid)
{
return;
}

// lua_register
// lua_atpanic
// luaL_ref LUA_REGISTRYINDEX
/*
*
lua_getfield(luaState, LUA_REGISTRYINDEX, "SciTE_InitialState");
if (lua_istable(luaState, -1)) {
clear_table(luaState, LUA_GLOBALSINDEX, true);
merge_table(luaState, LUA_GLOBALSINDEX, -1, true);
lua_pop(luaState, 1);
*
*
*
* */

if (callFunction("initUi"))
{
g_message("Plugin «%s» UI initialized", name.c_str());
}
else
{
g_message("Plugin «%s» has no UI init", name.c_str());
}
}

/**
* Load ini file
*/
void Plugin::loadIni()
{
XOJ_CHECK_TYPE(Plugin);

GKeyFile* config = g_key_file_new();
g_key_file_set_list_separator(config, ',');

string filename = path + "/plugin.ini";
if (!g_key_file_load_from_file(config, filename.c_str(), G_KEY_FILE_NONE, NULL))
{
g_key_file_free(config);
return;
}

LOAD_FROM_INI(author, "about", "author");
LOAD_FROM_INI(version, "about", "version");

if (version == "<xournalpp>")
{
version = PROJECT_VERSION;
}

LOAD_FROM_INI(mainfile, "plugin", "mainfile");

g_key_file_free(config);

this->valid = true;
}

/**
* Load the plugin script
*/
void Plugin::loadScript()
{
XOJ_CHECK_TYPE(Plugin);

if (mainfile == "")
{
this->valid = false;
return;
}

if (mainfile.find("..") != std::string::npos)
{
g_warning("Plugin «%s» contains unsupported path «%s»", name.c_str(), mainfile.c_str());
this->valid = false;
return;
}

// Create Lua state variable
lua = luaL_newstate();

// Load Lua libraries
luaL_openlibs(lua);

// Load but don't run the Lua script
string luafile = path + "/" + mainfile;
if (luaL_loadfile(lua, luafile.c_str()))
{
// Error out if file can't be read
g_warning("Could not run plugin Lua file: «%s»", luafile.c_str());
this->valid = false;
return;
}

// Register Plugin object to Lua instance
lua_pushnil(luaState);
lua_setfield(lua, LUA_REGISTRYINDEX, "Xournalpp_Plugin");


// Run the loaded Lua script
if (lua_pcall(lua, 0, 0, 0))
{
g_warning("Could not run plugin Lua file: «%s», error.", luafile.c_str());
this->valid = false;
return;
}
}

/**
* Execute lua function
*/
bool Plugin::callFunction(string fnc)
{
lua_getglobal(lua, fnc.c_str());

// Run the function
if (lua_pcall(lua, 0, 0, 0))
{
// Failed
return false;
}

return true;
}

/**
* Check if this plugin is valid
*/
bool Plugin::isValid()
{
XOJ_CHECK_TYPE(Plugin);

return valid;
}

#endif

0 comments on commit 64cb37d

Please sign in to comment.