1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Language wrappers for callbacks

This commit is contained in:
James Booth 2013-08-18 00:10:10 +01:00
parent 8cae5c13b0
commit f15b61e4cb
6 changed files with 67 additions and 55 deletions

View File

@ -56,6 +56,8 @@ tests_testsuite_SOURCES = \
src/plugins/plugins.h src/plugins/plugins.c \
src/plugins/api.h src/plugins/api.c \
src/plugins/callbacks.h src/plugins/callbacks.c \
src/plugins/python_plugins.h src/plugins/python_plugins.c \
src/plugins/python_api.h src/plugins/python_api.c \
tests/test_roster.c tests/test_common.c tests/test_history.c \
tests/test_autocomplete.c tests/testsuite.c tests/test_parser.c \
tests/test_jid.c

View File

@ -47,7 +47,8 @@ api_cons_show(const char * const message)
void
api_register_command(const char *command_name, int min_args, int max_args,
const char *usage, const char *short_help, const char *long_help, void *callback)
const char *usage, const char *short_help, const char *long_help, void *callback,
void(*callback_func)(PluginCommand *command, gchar **args))
{
PluginCommand *command = malloc(sizeof(PluginCommand));
command->command_name = command_name;
@ -57,15 +58,18 @@ api_register_command(const char *command_name, int min_args, int max_args,
command->short_help = short_help;
command->long_help = long_help;
command->callback = callback;
command->callback_func =callback_func;
callbacks_add_command(command);
}
void
api_register_timed(void *callback, int interval_seconds)
api_register_timed(void *callback, int interval_seconds,
void (*callback_func)(PluginTimedFunction *timed_function))
{
PluginTimedFunction *timed_function = malloc(sizeof(PluginTimedFunction));
timed_function->callback = callback;
timed_function->callback_func = callback_func;
timed_function->interval_seconds = interval_seconds;
timed_function->timer = g_timer_new();

View File

@ -23,6 +23,8 @@
#ifndef API_H
#define API_H
#include "plugins/callbacks.h"
void api_cons_alert(void);
void api_cons_show(const char * const message);
void api_notify(const char *message, const char *category, int timeout_ms);
@ -31,7 +33,8 @@ char * api_get_current_recipient(void);
void api_register_command(const char *command_name, int min_args, int max_args,
const char *usage, const char *short_help, const char *long_help,
void *callback);
void api_register_timed(void *callback, int interval_seconds);
void *callback, void(*callback_func)(PluginCommand *command, gchar **args));
void api_register_timed(void *callback, int interval_seconds,
void (*callback_func)(PluginTimedFunction *timed_function));
#endif

View File

@ -46,53 +46,6 @@ callbacks_add_timed(PluginTimedFunction *timed_function)
p_timed_functions = g_slist_append(p_timed_functions, timed_function);
}
void
python_command_callback(PluginCommand *command, gchar **args)
{
PyObject *p_args = NULL;
int num_args = g_strv_length(args);
if (num_args == 0) {
if (command->max_args == 1) {
p_args = Py_BuildValue("(O)", Py_BuildValue(""));
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
} else {
PyObject_CallObject(command->callback, p_args);
}
} else if (num_args == 1) {
p_args = Py_BuildValue("(s)", args[0]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
} else if (num_args == 2) {
p_args = Py_BuildValue("ss", args[0], args[1]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
} else if (num_args == 3) {
p_args = Py_BuildValue("sss", args[0], args[1], args[2]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
} else if (num_args == 4) {
p_args = Py_BuildValue("ssss", args[0], args[1], args[2], args[3]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
} else if (num_args == 5) {
p_args = Py_BuildValue("sssss", args[0], args[1], args[2], args[3], args[4]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
}
if (PyErr_Occurred()) {
PyErr_Print();
PyErr_Clear();
}
}
void
python_timed_callback(PluginTimedFunction *timed_function)
{
PyObject_CallObject(timed_function->callback, NULL);
}
gboolean
plugins_run_command(const char * const input)
{
@ -113,7 +66,7 @@ plugins_run_command(const char * const input)
}
return TRUE;
} else {
python_command_callback(command, args);
command->callback_func(command, args);
g_strfreev(split);
return TRUE;
}
@ -135,7 +88,7 @@ plugins_run_timed(void)
gdouble elapsed = g_timer_elapsed(timed_function->timer, NULL);
if (timed_function->interval_seconds > 0 && elapsed >= timed_function->interval_seconds) {
python_timed_callback(timed_function);
timed_function->callback_func(timed_function);
g_timer_start(timed_function->timer);
}

View File

@ -33,10 +33,12 @@ typedef struct p_command {
const char *short_help;
const char *long_help;
void *callback;
void (*callback_func)(struct p_command *command, gchar **args);
} PluginCommand;
typedef struct p_timed_function {
void *callback;
void (*callback_func)(struct p_timed_function *timed_function);
int interval_seconds;
GTimer *timer;
} PluginTimedFunction;

View File

@ -25,6 +25,7 @@
#include <glib.h>
#include "plugins/api.h"
#include "plugins/callbacks.h"
#include "profanity.h"
#include "ui/notifier.h"
#include "ui/ui.h"
@ -47,6 +48,53 @@ python_api_cons_show(PyObject *self, PyObject *args)
return Py_BuildValue("");
}
void
python_command_callback(PluginCommand *command, gchar **args)
{
PyObject *p_args = NULL;
int num_args = g_strv_length(args);
if (num_args == 0) {
if (command->max_args == 1) {
p_args = Py_BuildValue("(O)", Py_BuildValue(""));
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
} else {
PyObject_CallObject(command->callback, p_args);
}
} else if (num_args == 1) {
p_args = Py_BuildValue("(s)", args[0]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
} else if (num_args == 2) {
p_args = Py_BuildValue("ss", args[0], args[1]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
} else if (num_args == 3) {
p_args = Py_BuildValue("sss", args[0], args[1], args[2]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
} else if (num_args == 4) {
p_args = Py_BuildValue("ssss", args[0], args[1], args[2], args[3]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
} else if (num_args == 5) {
p_args = Py_BuildValue("sssss", args[0], args[1], args[2], args[3], args[4]);
PyObject_CallObject(command->callback, p_args);
Py_XDECREF(p_args);
}
if (PyErr_Occurred()) {
PyErr_Print();
PyErr_Clear();
}
}
void
python_timed_callback(PluginTimedFunction *timed_function)
{
PyObject_CallObject(timed_function->callback, NULL);
}
static PyObject*
python_api_register_command(PyObject *self, PyObject *args)
{
@ -65,7 +113,7 @@ python_api_register_command(PyObject *self, PyObject *args)
if (p_callback && PyCallable_Check(p_callback)) {
api_register_command(command_name, min_args, max_args, usage,
short_help, long_help, p_callback);
short_help, long_help, p_callback, python_command_callback);
}
return Py_BuildValue("");
@ -82,7 +130,7 @@ python_api_register_timed(PyObject *self, PyObject *args)
}
if (p_callback && PyCallable_Check(p_callback)) {
api_register_timed(p_callback, interval_seconds);
api_register_timed(p_callback, interval_seconds, python_timed_callback);
}
return Py_BuildValue("");