Commit 5b6484ac authored by Carsten Fuhrmann's avatar Carsten Fuhrmann
Browse files

added example hot_key functions

parent 52fb4e27
......@@ -62,6 +62,76 @@ static int wcharToUtf8(const wchar_t* str, char** result) {
#pragma endregion
/****************************** Helper functions ********************************/
/*
* Following functions are helper for the callbacks.
*/
#pragma region helper functions
uint64 getCurrentChannel(uint64 serverConnectionHandlerID)
{
uint64 curChannel;
anyID curClient;
ts3Functions.getClientID(serverConnectionHandlerID, &curClient);
ts3Functions.getChannelOfClient(serverConnectionHandlerID, curClient, &curChannel);
return curChannel;
}
QString clientID2Name(uint64 serverConnectionHandlerID, anyID clientID)
{
char* tmp;
if (ts3Functions.getClientVariableAsString(serverConnectionHandlerID, clientID, CLIENT_NICKNAME, &tmp) != ERROR_ok)
return QString();
QString name(tmp);
ts3plugin_freeMemory(tmp);
return name;
}
QString channelID2Name(uint64 serverConnectionHandlerID, uint64 channelID)
{
char* tmp;
if (ts3Functions.getChannelVariableAsString(serverConnectionHandlerID, channelID, CHANNEL_NAME, &tmp) != ERROR_ok)
return QString();
QString name(tmp);
ts3plugin_freeMemory(tmp);
return name;
}
OverlayController* getController(uint64 serverConnectionHandlerID)
{
auto tmp = g_serverList.find(serverConnectionHandlerID);
if (tmp == g_serverList.end())
{
OverlayController* ret = new OverlayController(ts3Functions, serverConnectionHandlerID);
g_serverList.insert(serverConnectionHandlerID, ret);
return ret;
}
return tmp.value();
}
/* Helper function to create a hotkey */
static struct PluginHotkey* createHotkey(const char* keyword, const char* description) {
struct PluginHotkey* hotkey = (struct PluginHotkey*)malloc(sizeof(struct PluginHotkey));
_strcpy(hotkey->keyword, PLUGIN_HOTKEY_BUFSZ, keyword);
_strcpy(hotkey->description, PLUGIN_HOTKEY_BUFSZ, description);
return hotkey;
}
/* Some makros to make the code to create hotkeys a bit more readable */
#define BEGIN_CREATE_HOTKEYS(x) const size_t sz = x + 1; size_t n = 0; *hotkeys = (struct PluginHotkey**)malloc(sizeof(struct PluginHotkey*) * sz);
#define CREATE_HOTKEY(a, b) (*hotkeys)[n++] = createHotkey(a, b);
#define END_CREATE_HOTKEYS (*hotkeys)[n++] = NULL; assert(n == sz);
#pragma endregion
/*********************************** Required functions ************************************/
/*
* If any of these required functions is not implemented, TS3 will refuse to load the plugin
......@@ -118,6 +188,9 @@ void ts3plugin_setFunctionPointers(const struct TS3Functions funcs) {
* If the function returns 1 on failure, the plugin will be unloaded again.
*/
int ts3plugin_init() {
char pluginPath[PATH_BUFSIZE];
ts3Functions.getPluginPath(pluginPath, PATH_BUFSIZE, pluginID);
return 0; /* 0 = success, 1 = failure, -2 = failure but client will not show a "failed to load" warning */
/* -2 is a very special case and should only be used if a plugin displays a dialog (e.g. overlay) asking the user to disable
......@@ -131,6 +204,10 @@ void ts3plugin_shutdown() {
while (g_serverList.size() > 0)
delete g_serverList.take(g_serverList.firstKey());
if (pluginID) {
free(pluginID);
pluginID = NULL;
}
/*
* Note:
......@@ -171,6 +248,9 @@ void ts3plugin_configure(void* handle, void* qParentWidget) {
* Note the passed pluginID parameter is no longer valid after calling this function, so you must copy it and store it in the plugin.
*/
void ts3plugin_registerPluginID(const char* id) {
const size_t sz = strlen(id) + 1;
pluginID = (char*)malloc(sz * sizeof(char));
_strcpy(pluginID, sz, id);
}
/* Plugin command keyword. Return NULL or "" if not used. */
......@@ -219,61 +299,22 @@ int ts3plugin_requestAutoload() {
return 0; /* 1 = request autoloaded, 0 = do not request autoload */
}
#pragma endregion
/****************************** Helper functions ********************************/
/*
* Following functions are helper for the callbacks.
* Initialize plugin hotkeys. If your plugin does not use this feature, this function can be omitted.
* Hotkeys require ts3plugin_registerPluginID and ts3plugin_freeMemory to be implemented.
* This function is automatically called by the client after ts3plugin_init.
*/
void ts3plugin_initHotkeys(struct PluginHotkey*** hotkeys) {
/* Register hotkeys giving a keyword and a description.
* The keyword will be later passed to ts3plugin_onHotkeyEvent to identify which hotkey was triggered.
* The description is shown in the clients hotkey dialog. */
BEGIN_CREATE_HOTKEYS(3); /* Create 3 hotkeys. Size must be correct for allocating memory. */
CREATE_HOTKEY("keyword_1", "Test hotkey 1");
CREATE_HOTKEY("keyword_2", "Test hotkey 2");
CREATE_HOTKEY("keyword_3", "Test hotkey 3");
END_CREATE_HOTKEYS;
#pragma region helper functions
uint64 getCurrentChannel(uint64 serverConnectionHandlerID)
{
uint64 curChannel;
anyID curClient;
ts3Functions.getClientID(serverConnectionHandlerID, &curClient);
ts3Functions.getChannelOfClient(serverConnectionHandlerID, curClient, &curChannel);
return curChannel;
}
QString clientID2Name(uint64 serverConnectionHandlerID, anyID clientID)
{
char* tmp;
if(ts3Functions.getClientVariableAsString(serverConnectionHandlerID, clientID, CLIENT_NICKNAME, &tmp) != ERROR_ok)
return QString();
QString name(tmp);
ts3plugin_freeMemory(tmp);
return name;
}
QString channelID2Name(uint64 serverConnectionHandlerID, uint64 channelID)
{
char* tmp;
if(ts3Functions.getChannelVariableAsString(serverConnectionHandlerID, channelID, CHANNEL_NAME, &tmp) != ERROR_ok)
return QString();
QString name(tmp);
ts3plugin_freeMemory(tmp);
return name;
}
OverlayController* getController(uint64 serverConnectionHandlerID)
{
auto tmp = g_serverList.find(serverConnectionHandlerID);
if (tmp == g_serverList.end())
{
OverlayController* ret = new OverlayController(ts3Functions, serverConnectionHandlerID);
g_serverList.insert(serverConnectionHandlerID, ret);
return ret;
}
return tmp.value();
/* The client will call ts3plugin_freeMemory to release all allocated memory */
}
#pragma endregion
......@@ -725,6 +766,8 @@ void ts3plugin_onMenuItemEvent(uint64 serverConnectionHandlerID, enum PluginMenu
/* This function is called if a plugin hotkey was pressed. Omit if hotkeys are unused. */
void ts3plugin_onHotkeyEvent(const char* keyword) {
printf("PLUGIN: Hotkey event: %s\n", keyword);
/* Identify the hotkey by keyword ("keyword_1", "keyword_2" or "keyword_3" in this example) and handle here... */
}
/* Called when recording a hotkey has finished after calling ts3Functions.requestHotkeyInputDialog */
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment