Skip to content

Commit

Permalink
improve plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
loganek committed Jan 7, 2017
1 parent 112df09 commit 67b9bd3
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 38 deletions.
10 changes: 5 additions & 5 deletions src/dummytrackerplugin/dummytrackerplugin.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ static void load_config(WT_IDataControlPlugin* obj, const char ***config, int si
}
}

static int plugin_process(WT_IDataControlPlugin* obj,
char[WT_MAX_APP_NAME_LEN],
char[WT_MAX_WIN_TITLE_LEN],
int*)
static int plugin_suspend(WT_IDataControlPlugin* obj,
const char[WT_MAX_APP_NAME_LEN],
const char[WT_MAX_WIN_TITLE_LEN])
{
if (!obj->silent)
{
Expand All @@ -60,4 +59,5 @@ WT_PLUGIN_DEFINE(
create_plugin,
destroy_plugin,
load_config,
plugin_process)
nullptr,
plugin_suspend)
10 changes: 5 additions & 5 deletions src/gnomescreensaverplugin/gnomescreensaver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,9 @@ static void load_config(WT_IDataControlPlugin*, const char ***, int)

}

static int plugin_process(WT_IDataControlPlugin* plugin,
char[WT_MAX_APP_NAME_LEN],
char[WT_MAX_WIN_TITLE_LEN],
int*)
static int plugin_suspend(WT_IDataControlPlugin* plugin,
const char[WT_MAX_APP_NAME_LEN],
const char[WT_MAX_WIN_TITLE_LEN])
{
return plugin->suspend_tracking();
}
Expand All @@ -149,4 +148,5 @@ WT_PLUGIN_DEFINE(
create_plugin,
destroy_plugin,
load_config,
plugin_process)
nullptr,
plugin_suspend)
24 changes: 13 additions & 11 deletions src/tracker/plugincontainer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,37 @@ bool PluginContainer::process_controllers(DataEntry &entry)
{
WT_LOG_D << "Updating data...";

int force_break = 0;
char in_out_app_name[WT_MAX_APP_NAME_LEN] = {0};
char in_out_window_title[WT_MAX_WIN_TITLE_LEN] = {0};
char app_name[WT_MAX_APP_NAME_LEN] = {0};
char window_title[WT_MAX_WIN_TITLE_LEN] = {0};

strncpy(in_out_app_name, entry.proc_name.c_str(), WT_MAX_APP_NAME_LEN-1);
strncpy(in_out_window_title, entry.description.c_str(), WT_MAX_WIN_TITLE_LEN-1);
strncpy(app_name, entry.proc_name.c_str(), WT_MAX_APP_NAME_LEN-1);
strncpy(window_title, entry.description.c_str(), WT_MAX_WIN_TITLE_LEN-1);

for (const auto &plugin : plugins)
{
if (plugin->process_data_entry(in_out_app_name, in_out_window_title, &force_break))
if (plugin->suspend_logging(app_name, window_title))
{
return true;
}
}

if (force_break)
for (const auto &plugin : plugins)
{
if (plugin->update_data_entry(app_name, window_title))
{
WT_LOG_D << "Force break from plugin " << plugin->get_name();
break;
}
}

if (strcmp(in_out_app_name, entry.proc_name.c_str()))
if (strcmp(app_name, entry.proc_name.c_str()))
{
entry.proc_name = in_out_app_name;
entry.proc_name = app_name;
}

if (strcmp(in_out_window_title, entry.description.c_str()))
if (strcmp(window_title, entry.description.c_str()))
{
entry.description = in_out_window_title;
entry.description = window_title;
}

return false;
Expand Down
21 changes: 17 additions & 4 deletions src/tracker/pluginloader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,24 @@ void PluginLoader::try_load_plugin(const std::string &path)
}
}

bool PluginWrapper::process_data_entry(char in_out_app_name[WT_MAX_APP_NAME_LEN],
char in_out_window_title[WT_MAX_WIN_TITLE_LEN],
int* out_force_break)
bool PluginWrapper::update_data_entry(char in_out_app_name[WT_MAX_APP_NAME_LEN],
char in_out_window_title[WT_MAX_WIN_TITLE_LEN])
{
return plugin_info.control_data_func(plugin, in_out_app_name, in_out_window_title, out_force_break);
if (plugin_info.update_data_func)
{
return plugin_info.update_data_func(plugin, in_out_app_name, in_out_window_title);
}
return false;
}

bool PluginWrapper::suspend_logging(char in_out_app_name[WT_MAX_APP_NAME_LEN],
char in_out_window_title[WT_MAX_WIN_TITLE_LEN])
{
if (plugin_info.suspend_tracking_func)
{
return plugin_info.suspend_tracking_func(plugin, in_out_app_name, in_out_window_title);
}
return false;
}

}
13 changes: 9 additions & 4 deletions src/tracker/pluginloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,18 @@ class PluginWrapper
return plugin_info.version;
}

bool process_data_entry(char in_out_app_name[WT_MAX_APP_NAME_LEN],
char in_out_window_title[WT_MAX_WIN_TITLE_LEN],
int* out_force_break);
bool update_data_entry(char in_out_app_name[WT_MAX_APP_NAME_LEN],
char in_out_window_title[WT_MAX_WIN_TITLE_LEN]);

bool suspend_logging(char in_out_app_name[WT_MAX_APP_NAME_LEN],
char in_out_window_title[WT_MAX_WIN_TITLE_LEN]);

void load_configuration(const char*** config, int size)
{
plugin_info.load_config_func(plugin, config, size);
if (plugin_info.load_config_func)
{
plugin_info.load_config_func(plugin, config, size);
}
}
};

Expand Down
65 changes: 56 additions & 9 deletions src/wtcommon/idatacontrolplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,52 @@
extern "C" {
#endif

/**
* A struct that represents a plugin.
*/
struct WT_IDataControlPlugin;

/**
* Creates an instance of the plugin.
* @return The instance of the WT_IDataControlPlugin struct.
*/
typedef WT_IDataControlPlugin*(*WT_CreateFunc)();
typedef void (*WT_DestroyFunc)(WT_IDataControlPlugin *);
typedef void (*WT_LoadConfigFunc)(WT_IDataControlPlugin*, const char **config[2], int size);

typedef int (*WT_ControlDataFunc)(
/**
* Destroys instance of the plugin.
* @param plugin The instance of the WT_IDataControlPlugin struct.
*/
typedef void (*WT_DestroyFunc)(WT_IDataControlPlugin *plugin);

/**
* Loads configuration to a plugin.
* @param plugin The plugin.
* @param config An array of key-value string pair that contains configuration for the plugin.
* @param size A number of configuration entries.
*/
typedef void (*WT_LoadConfigFunc)(WT_IDataControlPlugin *plugin, const char **config[2], int size);

/**
* Updates data entiers that will be stored in a database.
* @param in_out_app_name An application name of the data entry.
* @param in_out_window_title A window title of the data entry.
* @return Zero, if other plugins should be processed; otherwise, non-zero value.
*/
typedef int (*WT_UpdateDataFunc)(
WT_IDataControlPlugin*,
char in_out_app_name[WT_MAX_APP_NAME_LEN],
char in_out_window_title[WT_MAX_WIN_TITLE_LEN],
int* out_force_break);
char in_out_window_title[WT_MAX_WIN_TITLE_LEN]);

/**
* Returns information whether a tracking should be suspended.
* @param app_name An application name of the data entry.
* @param window_title A window title of the data entry.
* @return Non-zero value, if the tracking should be suspended; otherwise, zero.
*/
typedef int (*WT_SuspendTrackingFunc)(
WT_IDataControlPlugin*,
const char app_name[WT_MAX_APP_NAME_LEN],
const char window_title[WT_MAX_WIN_TITLE_LEN]);

typedef struct {
int version;
Expand All @@ -42,23 +77,35 @@ typedef struct {
WT_CreateFunc create_func;
WT_DestroyFunc destroy_func;
WT_LoadConfigFunc load_config_func;
WT_ControlDataFunc control_data_func;
WT_UpdateDataFunc update_data_func;
WT_SuspendTrackingFunc suspend_tracking_func;
} WT_PluginInfo;

#ifdef __cplusplus
}
#endif


#define WT_PLUGIN_DEFINE(version, rank, name, create_func, destroy_func, load_config_func, control_data_func) \
/** Defines a plugin
*
* @param version A version of the workertracker that the plugin was compiled for.
* @param rank A plugin priority rank - higher value, more important plugin.
* @param name A name of the plugin.
* @param create_func A pointer to the construction function of the WT_CreateFunc signature.
* @param destroy_func A pointer to the destroy function of the WT_DestroyFunc signature.
* @param load_config_func A pointer to the function that loads the configuration, of the WT_LoadConfigFunc signature, optional.
* @param update_data_func A pointer to the function that might update a data, of the WT_UpdateDataFunc signature, optional.
* @param suspend_tracking_func A pointer to the function that might suspend a tracking, of the WT_SuspendTrackingFunc signature, optional.
*/
#define WT_PLUGIN_DEFINE(version, rank, name, create_func, destroy_func, load_config_func, update_data_func, suspend_tracking_func) \
WT_PLUGIN_EXPORT WT_PluginInfo wt_plugin_info = { \
version, \
rank, \
name, \
create_func, \
destroy_func, \
load_config_func, \
control_data_func \
update_data_func, \
suspend_tracking_func \
};


Expand Down

0 comments on commit 67b9bd3

Please sign in to comment.