lua+js: Implement utils.getpid()

Usable for uniquely identifying mpv instances from
subprocesses, controlling mpv with AppleScript, ...

Adds a new mp_getpid() wrapper for cross-platform reasons.
This commit is contained in:
sfan5 2017-12-17 01:25:23 +01:00 committed by Kevin Mitchell
parent f17246fec1
commit 8f9785d128
5 changed files with 51 additions and 0 deletions

View File

@ -180,6 +180,8 @@ Otherwise, where the Lua APIs return ``nil`` on error, JS returns ``undefined``.
``mp.utils.subprocess_detached(t)`` ``mp.utils.subprocess_detached(t)``
``mp.utils.getpid()`` (LE)
``mp.add_hook(type, priority, fn)`` ``mp.add_hook(type, priority, fn)``
Additional utilities Additional utilities

View File

@ -682,6 +682,10 @@ strictly part of the guaranteed API.
The function returns ``nil``. The function returns ``nil``.
``utils.getpid()``
Returns the process ID of the running mpv process. This can be used to identify
the calling mpv when launching (detached) subprocesses.
``utils.parse_json(str [, trail])`` ``utils.parse_json(str [, trail])``
Parses the given string argument as JSON, and returns it as a Lua table. On Parses the given string argument as JSON, and returns it as a Lua table. On
error, returns ``nil, error``. (Currently, ``error`` is just a string error, returns ``nil, error``. (Currently, ``error`` is just a string

29
osdep/getpid.h Normal file
View File

@ -0,0 +1,29 @@
/*
* getpid wrapper
*
* This file is part of mpv.
*
* mpv is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* mpv is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#ifdef _WIN32
#include <windows.h>
#define mp_getpid() GetCurrentProcessId()
#else // POSIX
#include <sys/types.h>
#include <unistd.h>
#define mp_getpid() getpid()
#endif

View File

@ -40,6 +40,7 @@
#include "osdep/subprocess.h" #include "osdep/subprocess.h"
#include "osdep/timer.h" #include "osdep/timer.h"
#include "osdep/threads.h" #include "osdep/threads.h"
#include "osdep/getpid.h"
#include "stream/stream.h" #include "stream/stream.h"
#include "sub/osd.h" #include "sub/osd.h"
#include "core.h" #include "core.h"
@ -990,6 +991,12 @@ static void script_subprocess_detached(js_State *J, void *af)
af_subprocess_common(J, 1, af); af_subprocess_common(J, 1, af);
} }
// args: none
static void script_getpid(js_State *J)
{
js_pushnumber(J, mp_getpid());
}
// args: prefixed file name, data (c-str) // args: prefixed file name, data (c-str)
static void script_write_file(js_State *J, void *af) static void script_write_file(js_State *J, void *af)
{ {
@ -1296,6 +1303,7 @@ static const struct fn_entry utils_fns[] = {
AF_ENTRY(get_user_path, 1), AF_ENTRY(get_user_path, 1),
AF_ENTRY(subprocess, 1), AF_ENTRY(subprocess, 1),
AF_ENTRY(subprocess_detached, 1), AF_ENTRY(subprocess_detached, 1),
FN_ENTRY(getpid, 0),
FN_ENTRY(read_file, 2), FN_ENTRY(read_file, 2),
AF_ENTRY(write_file, 2), AF_ENTRY(write_file, 2),

View File

@ -44,6 +44,7 @@
#include "osdep/subprocess.h" #include "osdep/subprocess.h"
#include "osdep/timer.h" #include "osdep/timer.h"
#include "osdep/threads.h" #include "osdep/threads.h"
#include "osdep/getpid.h"
#include "stream/stream.h" #include "stream/stream.h"
#include "sub/osd.h" #include "sub/osd.h"
#include "core.h" #include "core.h"
@ -1252,6 +1253,12 @@ static int script_subprocess_detached(lua_State *L)
return 1; return 1;
} }
static int script_getpid(lua_State *L)
{
lua_pushnumber(L, mp_getpid());
return 1;
}
static int script_parse_json(lua_State *L) static int script_parse_json(lua_State *L)
{ {
mp_lua_optarg(L, 2); mp_lua_optarg(L, 2);
@ -1338,6 +1345,7 @@ static const struct fn_entry utils_fns[] = {
FN_ENTRY(join_path), FN_ENTRY(join_path),
FN_ENTRY(subprocess), FN_ENTRY(subprocess),
FN_ENTRY(subprocess_detached), FN_ENTRY(subprocess_detached),
FN_ENTRY(getpid),
FN_ENTRY(parse_json), FN_ENTRY(parse_json),
FN_ENTRY(format_json), FN_ENTRY(format_json),
{0} {0}