History

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.

sfan5 committed 8 years ago

Commit 8f9785d1 parent f17246f5 files · +51 −0Browse files

Unified · Split

5 files
  1. DOCS/man/javascript.rst +2 −0
  2. DOCS/man/lua.rst +4 −0
  3. osdep/getpid.h +29 −0
  4. player/javascript.c +8 −0
  5. player/lua.c +8 −0
DOCS/man/javascript.rst+2−0
@@ -180,6 +180,8 @@ Otherwise, where the Lua APIs return ``nil`` on error, JS returns ``undefined``. · 179 unmodified lines
180 180
181``mp.utils.subprocess_detached(t)``181``mp.utils.subprocess_detached(t)``
182 182
183``mp.utils.getpid()`` (LE)
184
183``mp.add_hook(type, priority, fn)``185``mp.add_hook(type, priority, fn)``
184 186
185Additional utilities187Additional utilities
DOCS/man/lua.rst+4−0
@@ -682,6 +682,10 @@ strictly part of the guaranteed API. · 681 unmodified lines
682 682
683 The function returns ``nil``.683 The function returns ``nil``.
684 684
685``utils.getpid()``
686 Returns the process ID of the running mpv process. This can be used to identify
687 the calling mpv when launching (detached) subprocesses.
688
685``utils.parse_json(str [, trail])``689``utils.parse_json(str [, trail])``
686 Parses the given string argument as JSON, and returns it as a Lua table. On690 Parses the given string argument as JSON, and returns it as a Lua table. On
687 error, returns ``nil, error``. (Currently, ``error`` is just a string691 error, returns ``nil, error``. (Currently, ``error`` is just a string
osdep/getpid.h+29−0
@@ -0,0 +1,29 @@
1/*
2 * getpid wrapper
3 *
4 * This file is part of mpv.
5 *
6 * mpv is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * mpv is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with mpv. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#ifdef _WIN32
23#include <windows.h>
24#define mp_getpid() GetCurrentProcessId()
25#else // POSIX
26#include <sys/types.h>
27#include <unistd.h>
28#define mp_getpid() getpid()
29#endif
player/javascript.c+8−0
@@ -40,6 +40,7 @@ · 39 unmodified lines
40#include "osdep/subprocess.h"40#include "osdep/subprocess.h"
41#include "osdep/timer.h"41#include "osdep/timer.h"
42#include "osdep/threads.h"42#include "osdep/threads.h"
43#include "osdep/getpid.h"
43#include "stream/stream.h"44#include "stream/stream.h"
44#include "sub/osd.h"45#include "sub/osd.h"
45#include "core.h"46#include "core.h"
@@ -990,6 +991,12 @@ static void script_subprocess_detached(js_State *J, void *af) · 944 unmodified lines
990 af_subprocess_common(J, 1, af);991 af_subprocess_common(J, 1, af);
991}992}
992 993
994// args: none
995static void script_getpid(js_State *J)
996{
997 js_pushnumber(J, mp_getpid());
998}
999
993// args: prefixed file name, data (c-str)1000// args: prefixed file name, data (c-str)
994static void script_write_file(js_State *J, void *af)1001static void script_write_file(js_State *J, void *af)
995{1002{
@@ -1296,6 +1303,7 @@ static const struct fn_entry utils_fns[] = { · 300 unmodified lines
1296 AF_ENTRY(get_user_path, 1),1303 AF_ENTRY(get_user_path, 1),
1297 AF_ENTRY(subprocess, 1),1304 AF_ENTRY(subprocess, 1),
1298 AF_ENTRY(subprocess_detached, 1),1305 AF_ENTRY(subprocess_detached, 1),
1306 FN_ENTRY(getpid, 0),
1299 1307
1300 FN_ENTRY(read_file, 2),1308 FN_ENTRY(read_file, 2),
1301 AF_ENTRY(write_file, 2),1309 AF_ENTRY(write_file, 2),
player/lua.c+8−0
@@ -44,6 +44,7 @@ · 43 unmodified lines
44#include "osdep/subprocess.h"44#include "osdep/subprocess.h"
45#include "osdep/timer.h"45#include "osdep/timer.h"
46#include "osdep/threads.h"46#include "osdep/threads.h"
47#include "osdep/getpid.h"
47#include "stream/stream.h"48#include "stream/stream.h"
48#include "sub/osd.h"49#include "sub/osd.h"
49#include "core.h"50#include "core.h"
@@ -1252,6 +1253,12 @@ static int script_subprocess_detached(lua_State *L) · 1202 unmodified lines
1252 return 1;1253 return 1;
1253}1254}
1254 1255
1256static int script_getpid(lua_State *L)
1257{
1258 lua_pushnumber(L, mp_getpid());
1259 return 1;
1260}
1261
1255static int script_parse_json(lua_State *L)1262static int script_parse_json(lua_State *L)
1256{1263{
1257 mp_lua_optarg(L, 2);1264 mp_lua_optarg(L, 2);
@@ -1338,6 +1345,7 @@ static const struct fn_entry utils_fns[] = { · 80 unmodified lines
1338 FN_ENTRY(join_path),1345 FN_ENTRY(join_path),
1339 FN_ENTRY(subprocess),1346 FN_ENTRY(subprocess),
1340 FN_ENTRY(subprocess_detached),1347 FN_ENTRY(subprocess_detached),
1348 FN_ENTRY(getpid),
1341 FN_ENTRY(parse_json),1349 FN_ENTRY(parse_json),
1342 FN_ENTRY(format_json),1350 FN_ENTRY(format_json),
1343 {0}1351 {0}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t