~rootmos/lua-hack

5854f6c12211013bf5c376afe1a1d00bdebc6757 — Gustav Behm 1 year, 2 months ago f938eae
Update notification
2 files changed, 137 insertions(+), 11 deletions(-)

M notify/go.lua
M notify/notify.c
M notify/go.lua => notify/go.lua +7 -1
@@ 1,4 1,10 @@
local N = require("notify")
N.app_name = "foo"

N:new{ summary = "hello", body = "foobar", id = 8, timeout = 7000 }:show()
local n = N:new{ summary = "hello", body = "foobar", timeout = 10000 }:show()

os.execute("sleep 1")

n.body = "baz"
n.timeout = 500
n:show()

M notify/notify.c => notify/notify.c +130 -10
@@ 77,7 77,8 @@ static int notification_show(lua_State* L)
        return lua_error(L);
    }

    luaR_return(L, 0);
    lua_pushvalue(L, 1);
    luaR_return(L, 1);
}

static int notification_index(lua_State* L)


@@ 112,6 113,16 @@ static int notification_index(lua_State* L)
        luaR_return(L, 1);
    }

    lua_pushliteral(L, "icon");
    r = lua_rawequal(L, 2, -1);
    lua_pop(L, 1);
    if(r) {
        gchar* icon;
        g_object_get(notification->n, "icon-name", &icon, NULL);
        lua_pushstring(L, icon);
        luaR_return(L, 1);
    }

    lua_pushliteral(L, "app_name");
    r = lua_rawequal(L, 2, -1);
    lua_pop(L, 1);


@@ 136,6 147,100 @@ static int notification_index(lua_State* L)
    luaR_return(L, 1);
}

static int notification_newindex(lua_State* L)
{
    luaR_stack(L);
    struct notification* notification = luaL_checkudata(L, 1, TYPE_NOTIFICATION);
    luaL_checkany(L, 2);
    luaL_checkany(L, 3);

    lua_pushliteral(L, "summary");
    int r = lua_rawequal(L, 2, -1);
    lua_pop(L, 1);
    if(r) {
        const gchar *summary, *body, *icon;
        int t = lua_type(L, 3);
        if(t == LUA_TSTRING) {
            summary = lua_tostring(L, 3);
            if(summary == NULL) {
                failwith("lua_tostring failed unexpectedly");
            }
        } else if(t == LUA_TNIL) {
            summary = NULL;
        } else {
            return luaL_argerror(L, 3, "unexpected type for summary field");
        }
        g_object_get(notification->n, "body", &body, "icon-name", &icon, NULL);

        if(!notify_notification_update(notification->n, summary, body, icon)) {
            return luaL_error(L, "notification update failed");
        }

        luaR_return(L, 0);
    }

    lua_pushliteral(L, "body");
    r = lua_rawequal(L, 2, -1);
    lua_pop(L, 1);
    if(r) {
        const gchar *summary, *body, *icon;
        int t = lua_type(L, 3);
        if(t == LUA_TSTRING) {
            body = lua_tostring(L, 3);
            if(body == NULL) {
                failwith("lua_tostring failed unexpectedly");
            }
        } else if(t == LUA_TNIL) {
            body = NULL;
        } else {
            return luaL_argerror(L, 3, "unexpected type for body field");
        }
        g_object_get(notification->n, "summary", &summary, "icon-name", &icon, NULL);

        if(!notify_notification_update(notification->n, summary, body, icon)) {
            return luaL_error(L, "notification update failed");
        }

        luaR_return(L, 0);
    }

    lua_pushliteral(L, "icon");
    r = lua_rawequal(L, 2, -1);
    lua_pop(L, 1);
    if(r) {
        const gchar *summary, *body, *icon;
        int t = lua_type(L, 3);
        if(t == LUA_TSTRING) {
            icon = lua_tostring(L, 3);
            if(icon == NULL) {
                failwith("lua_tostring failed unexpectedly");
            }
        } else if(t == LUA_TNIL) {
            icon = NULL;
        } else {
            return luaL_argerror(L, 3, "unexpected type for icon field");
        }
        g_object_get(notification->n, "summary", &summary, "body", &body, NULL);

        if(!notify_notification_update(notification->n, summary, body, icon)) {
            return luaL_error(L, "notification update failed");
        }

        luaR_return(L, 0);
    }

    lua_pushliteral(L, "timeout");
    r = lua_rawequal(L, 2, -1);
    lua_pop(L, 1);
    if(r) {
        gint timeout = luaL_checkinteger(L, 3);
        notify_notification_set_timeout(notification->n, timeout);
        luaR_return(L, 0);
    }

    return luaL_argerror(L, 2, "unpexpected field");
}

static int notification_new(lua_State* L)
{
    luaR_stack(L);


@@ 143,6 248,7 @@ static int notification_new(lua_State* L)

    const char* summary = NULL;
    const char* body = NULL;
    const char* icon = NULL;
    const char* app_name = NULL;
    gint timeout = NOTIFY_EXPIRES_DEFAULT;
    gint id = 0;


@@ 154,13 260,16 @@ static int notification_new(lua_State* L)
                if(summary == NULL) {
                    failwith("lua_tostring failed unexpectedly");
                }
                debug("summary: %s", summary);
            } else if(body == NULL) {
                body = lua_tostring(L, arg);
                if(body == NULL) {
                    failwith("lua_tostring failed unexpectedly");
                }
                debug("body: %s", body);
            } else if(icon == NULL) {
                icon = lua_tostring(L, arg);
                if(icon == NULL) {
                    failwith("lua_tostring failed unexpectedly");
                }
            } else {
                return luaL_argerror(L, arg, "unexpected string argument");
            }


@@ 171,7 280,6 @@ static int notification_new(lua_State* L)
                if(summary == NULL) {
                    failwith("lua_tostring failed unexpectedly");
                }
                debug("summary: %s", summary);
            } else if(f != LUA_TNIL) {
                return luaL_argerror(L, arg, "summary field not a string");
            }


@@ 183,12 291,22 @@ static int notification_new(lua_State* L)
                if(body == NULL) {
                    failwith("lua_tostring failed unexpectedly");
                }
                debug("body: %s", body);
            } else if(f != LUA_TNIL) {
                return luaL_argerror(L, arg, "body field not a string");
            }
            lua_pop(L, 1);

            f = lua_getfield(L, arg, "icon");
            if(f == LUA_TSTRING) {
                icon = lua_tostring(L, -1);
                if(icon == NULL) {
                    failwith("lua_tostring failed unexpectedly");
                }
            } else if(f != LUA_TNIL) {
                return luaL_argerror(L, arg, "icon field not a string");
            }
            lua_pop(L, 1);

            f = lua_getfield(L, arg, "app_name");
            if(f == LUA_TSTRING) {
                app_name = lua_tostring(L, -1);


@@ 219,14 337,13 @@ static int notification_new(lua_State* L)
                int isnum;
                id = lua_tointegerx(L, -1, &isnum);
                if(!isnum) {
                    return luaL_argerror(L, arg, "timeout field not an integer");
                    return luaL_argerror(L, arg, "id field not an integer");
                }
                debug("timeout: %d", timeout);
                debug("id: %d", id);
            } else if(f != LUA_TNIL) {
                return luaL_argerror(L, arg, "timeout field not a number");
                return luaL_argerror(L, arg, "id field not a number");
            }
            lua_pop(L, 1);

        } else {
            return luaL_argerror(L, arg, "unexpected argument");
        }


@@ 237,7 354,7 @@ static int notification_new(lua_State* L)
    notification_ref(notification);
    notification->notify = notify_ref(notify);

    notification->n = notify_notification_new(summary, body, NULL);
    notification->n = notify_notification_new(summary, body, icon);
    CHECK_NOT(notification->n, NULL, "notify_notification_new");

    notify_notification_set_timeout(notification->n, timeout);


@@ 256,6 373,9 @@ static int notification_new(lua_State* L)
        lua_pushcclosure(L, notification_index, 1);
        lua_setfield(L, -2, "__index");

        lua_pushcfunction(L, notification_newindex);
        lua_setfield(L, -2, "__newindex");

        lua_pushcfunction(L, notification_gc);
        lua_setfield(L, -2, "__gc");
    }