Commit 6283a805 authored by Julian Bouzas's avatar Julian Bouzas
Browse files

m-lua-scripting: add WpFileMonitor API

parent 16fced89
......@@ -6,6 +6,7 @@
* SPDX-License-Identifier: MIT
*/
#include <glib/gstdio.h>
#include <wp/wp.h>
#include <pipewire/pipewire.h>
#include <wplua/wplua.h>
......@@ -54,9 +55,44 @@ glib_get_real_time (lua_State *L)
return 1;
}
static gboolean
access_parse_mode (const gchar * mode_str, gint *mode)
{
*mode = 0;
if (!mode_str)
return FALSE;
else {
for (guint i = 0; i < strlen (mode_str); i++) {
switch (mode_str[i]) {
case 'r': *mode |= R_OK; break;
case 'w': *mode |= W_OK; break;
case 'x': *mode |= X_OK; break;
case 'f': *mode |= F_OK; break;
case '-': break;
default:
return FALSE;
}
}
}
return TRUE;
}
static int
glib_access (lua_State *L)
{
const gchar *filename = luaL_checkstring (L, 1);
int mode = 0;
if (!access_parse_mode (luaL_checkstring (L, 2), &mode))
luaL_error (L, "invalid mode string: '%s'", lua_tostring (L, 2));
lua_pushboolean (L, g_access (filename, mode) >= 0);
return 1;
}
static const luaL_Reg glib_methods[] = {
{ "get_monotonic_time", glib_get_monotonic_time },
{ "get_real_time", glib_get_real_time },
{ "access", glib_access },
{ NULL, NULL }
};
......@@ -1260,6 +1296,69 @@ static const luaL_Reg state_methods[] = {
{ NULL, NULL }
};
/* WpFileMonitor */
static int
file_monitor_new (lua_State *L)
{
WpFileMonitor *fm = wp_file_monitor_new (get_wp_core (L));
wplua_pushobject (L, fm);
return 1;
}
static int
file_monitor_open (lua_State *L)
{
WpFileMonitor *fm = wplua_checkobject (L, 1, WP_TYPE_FILE_MONITOR);
GIOCondition condition = luaL_checkinteger (L, 2);
lua_pushboolean (L, wp_file_monitor_open (fm, condition));
return 1;
}
static int
file_monitor_close (lua_State *L)
{
WpFileMonitor *fm = wplua_checkobject (L, 1, WP_TYPE_FILE_MONITOR);
wp_file_monitor_close (fm);
return 0;
}
static int
file_monitor_is_open (lua_State *L)
{
WpFileMonitor *fm = wplua_checkobject (L, 1, WP_TYPE_FILE_MONITOR);
lua_pushboolean (L, wp_file_monitor_is_open (fm));
return 1;
}
static int
file_monitor_add_watch (lua_State *L)
{
WpFileMonitor *fm = wplua_checkobject (L, 1, WP_TYPE_FILE_MONITOR);
const gchar *filename = luaL_checkstring (L, 2);
guint32 mask = luaL_checkinteger (L, 3);
lua_pushinteger (L, wp_file_monitor_add_watch (fm, filename, mask));
return 1;
}
static int
file_monitor_remove_watch (lua_State *L)
{
WpFileMonitor *fm = wplua_checkobject (L, 1, WP_TYPE_FILE_MONITOR);
gint id = luaL_checkinteger (L, 2);
wp_file_monitor_remove_watch (fm, id);
return 0;
}
static const luaL_Reg file_monitor_methods[] = {
{ "open", file_monitor_open },
{ "close", file_monitor_close },
{ "is_open", file_monitor_is_open },
{ "add_watch", file_monitor_add_watch },
{ "remove_watch", file_monitor_remove_watch },
{ NULL, NULL }
};
void
wp_lua_scripting_api_init (lua_State *L)
{
......@@ -1315,6 +1414,8 @@ wp_lua_scripting_api_init (lua_State *L)
NULL, pipewire_object_methods);
wplua_register_type_methods (L, WP_TYPE_STATE,
state_new, state_methods);
wplua_register_type_methods (L, WP_TYPE_FILE_MONITOR,
file_monitor_new, file_monitor_methods);
wplua_load_uri (L, URI_API, 0, 0, &error);
if (G_UNLIKELY (error))
......
......@@ -137,6 +137,33 @@ local Feature = {
},
}
local FileMonitorFlag = {
IO = {
IN = 1, -- GLIB_SYSDEF_POLLIN
OUT = 4, -- GLIB_SYSDEF_POLLOUT
PRI = 2, -- GLIB_SYSDEF_POLLPRI
ERR = 16, -- GLIB_SYSDEF_POLLHUP
HUP = 8, -- GLIB_SYSDEF_POLLERR
NVAL = 32, -- GLIB_SYSDEF_POLLNVAL
},
IN = {
ACCESS = 0x00000001,
MODIFY = 0x00000002,
ATTRIB = 0x00000004,
CLOSE_WRITE = 0x00000008,
CLOSE_NOWRITE = 0x00000010,
CLOSE = 0x00000008 | 0x00000010,
OPEN = 0x00000020,
MOVED_FROM = 0x00000040,
MOVED_TO = 0x00000080,
MOVE = 0x00000040 | 0x00000080,
CREATE = 0x00000100,
DELETE = 0x00000200,
DELETE_SELF = 0x00000400,
MOVE_SELF = 0x00000800,
},
}
SANDBOX_EXPORT = {
Debug = Debug,
Id = Id,
......@@ -157,4 +184,6 @@ SANDBOX_EXPORT = {
Link = WpLink_new,
Pod = WpSpaPod,
State = WpState_new,
FileMonitorFlag = FileMonitorFlag,
FileMonitor = WpFileMonitor_new,
}
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