Skip to content

Instantly share code, notes, and snippets.

@RyanSquared
Created November 5, 2015 17:55
Show Gist options
  • Save RyanSquared/3f8d46c6fc85c4267e08 to your computer and use it in GitHub Desktop.
Save RyanSquared/3f8d46c6fc85c4267e08 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <libnotify/notify.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#ifndef LUA_LIBNOTIFY
#define LUA_LIBNOTIFY
#include "notify.h"
/* ******************** */
/* Auxilliary functions */
/* ******************** */
void lua_libnotify_checkinit() {
if (!notify_is_initted()) {
notify_init("Lua");
}
return;
}
NotifyNotification *lua_libnotify_check(lua_State *L) {
NotifyNotification *note = (NotifyNotification*)lua_touserdata(L, 1);
return note;
}
void lua_libnotify_prep(lua_State *L, int startpos, const char *summary, const char *body, const char *icon) {
summary = luaL_checkstring(L, 1 + startpos);
if (lua_type(L, 2 + startpos) == LUA_TSTRING) {
body = lua_tostring(L, 2 + startpos);
} else {
body = NULL;
}
if (lua_type(L, 3 + startpos) == LUA_TSTRING) {
icon = lua_tostring(L, 3 + startpos);
} else {
icon = NULL;
}
return;
}
NotifyNotification *lua_libnotify_new(lua_State *L, const char *summary, const char *body, const char *icon) {
lua_libnotify_prep(L, 0, summary, body, icon);
return notify_notification_new(summary, body, icon);
}
/* ******************** */
/* ** Library funcs ** */
/* ******************** */
int lua_libnotify_init(lua_State *L) {
const static char *name;
if (lua_isstring(L, 1)) {
name = lua_tostring(L, 1);
} else {
name = LUA_LIBNOTIFY_NAME;
}
if (notify_is_initted()) {
notify_uninit();
}
notify_init(name);
lua_pushstring(L, notify_get_app_name());
return 1;
}
int lua_libnotify_uninit(lua_State *L) {
notify_uninit();
return 0;
}
int lua_libnotify_getappname(lua_State *L) {
lua_pushstring(L, notify_get_app_name());
return 1;
}
static const struct luaL_Reg lualibnotify[] = {
/* Base library functions */
{"init", lua_libnotify_init},
{"getappname", lua_libnotify_getappname},
{ NULL, NULL }
};
/* ******************* */
/* * Index functions * */
/* ******************* */
int lua_libnotify_show(lua_State *L) {
if (!notify_notification_show(lua_libnotify_check(L), NULL)) { /* NULL pointer for GError */
lua_pushstring(L, LUA_LIBNOTIFY_ERR_SHOW);
lua_error(L);
}
return 0;
}
static const struct luaL_Reg lualibNotifyNotification[] = {
/* API calls for published objects */
{"show", lua_libnotify_show},
{ NULL, NULL }
};
/* ******************* */
/* Metatable functions */
/* ******************* */
int lua_libnotify__call(lua_State *L) {
const char *summary = NULL;
const char *body = NULL;
const char *icon = NULL;
/* Allow for urgency set for notification */
/* Custom compilation allows to return userdata for .close() ::TODO:: */
lua_libnotify_prep(L, 1, summary, body, icon); /* Call prep instead of new, don't actually make userdata */
/* NotifyNotification *notification = (NotifyNotification *)lua_newuserdata(L, sizeof(NotifyNotification));
notify_notification_update(notification, summary, body, icon);*/
lua_libnotify_checkinit();
NotifyNotification *notification = notify_notification_new(summary, body, icon);
notify_notification_show(notification, NULL);
g_object_unref(notification);
return 0;
}
/* ******************* */
/* require function */
/* ******************* */
int luaopen_notify(lua_State *L) {
/* Create library */
luaL_newlib(L, lualibnotify); /* ::STACK:: 1 notify */
/* Create class library */
lua_pushstring(L, "api"); /* ::STACK:: 2 ..., "api" */
luaL_newlib(L, lualibNotifyNotification); /* ::STACK:: 3 ..., lualibNotifyNotification */
lua_rawset(L, -3); /* ::STACK:: 1 notify */
/* Create metatable */
lua_newtable(L); /* ::STACK:: 2 ..., notifymt */
/* Populate metatable */
lua_pushstring(L, "__index"); /* ::STACK:: 3 ..., "__index" */
/* Retrieve index 'api' from 'lualibnotify' */
lua_pushstring(L, "api"); /* ::STACK:: 4 ..., "api" */
lua_rawget(L, -4); /* ::STACK:: 4 notify, notifymt, "__index", lualibNotifyNotification */
/* Set __index to NotifyNotification */
lua_rawset(L, -3); /* ::STACK:: 2 notify, notifymt */
/* Set __gc to uninit */
lua_pushstring(L, "__gc"); /* ::STACK:: 3 notify, notifymy, "__gc" */
lua_pushcfunction(L, lua_libnotify_uninit); /* ::STACK:: 4 ..., lua_libnotify_uninit */
lua_rawset(L, -3); /* ::STACK:: 2 notify, notifymt */
/* Set __call to show a notification */
lua_pushstring(L, "__call"); /* ::STACK:: 3 ..., "__call" */
lua_pushcfunction(L, lua_libnotify__call); /* ::STACK:: 4 ..., lua_libnotify__call */
lua_rawset(L, -3); /* ::STACK:: 2 notify, notifymt */
/* Set the metatable without the global metatable registery */
lua_setmetatable(L, -2); /* ::STACK:: 1 notify */
return 1;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment