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
180180
181181 ``mp.utils.subprocess_detached(t)``
182182
183+``mp.utils.getpid()`` (LE)
184+
183185 ``mp.add_hook(type, priority, fn)``
184186
185187 Additional utilities
DOCS/man/lua.rst+4−0
@@ -682,6 +682,10 @@ strictly part of the guaranteed API. · 681 unmodified lines
682682
683683 The function returns ``nil``.
684684
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+
685689 ``utils.parse_json(str [, trail])``
686690 Parses the given string argument as JSON, and returns it as a Lua table. On
687691 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
4040 #include "osdep/subprocess.h"
4141 #include "osdep/timer.h"
4242 #include "osdep/threads.h"
43+#include "osdep/getpid.h"
4344 #include "stream/stream.h"
4445 #include "sub/osd.h"
4546 #include "core.h"
@@ -990,6 +991,12 @@ static void script_subprocess_detached(js_State *J, void *af) · 944 unmodified lines
990991 af_subprocess_common(J, 1, af);
991992 }
992993
994+// args: none
995+static void script_getpid(js_State *J)
996+{
997+ js_pushnumber(J, mp_getpid());
998+}
999+
9931000 // args: prefixed file name, data (c-str)
9941001 static void script_write_file(js_State *J, void *af)
9951002 {
@@ -1296,6 +1303,7 @@ static const struct fn_entry utils_fns[] = { · 300 unmodified lines
12961303 AF_ENTRY(get_user_path, 1),
12971304 AF_ENTRY(subprocess, 1),
12981305 AF_ENTRY(subprocess_detached, 1),
1306+ FN_ENTRY(getpid, 0),
12991307
13001308 FN_ENTRY(read_file, 2),
13011309 AF_ENTRY(write_file, 2),
player/lua.c+8−0
@@ -44,6 +44,7 @@ · 43 unmodified lines
4444 #include "osdep/subprocess.h"
4545 #include "osdep/timer.h"
4646 #include "osdep/threads.h"
47+#include "osdep/getpid.h"
4748 #include "stream/stream.h"
4849 #include "sub/osd.h"
4950 #include "core.h"
@@ -1252,6 +1253,12 @@ static int script_subprocess_detached(lua_State *L) · 1202 unmodified lines
12521253 return 1;
12531254 }
12541255
1256+static int script_getpid(lua_State *L)
1257+{
1258+ lua_pushnumber(L, mp_getpid());
1259+ return 1;
1260+}
1261+
12551262 static int script_parse_json(lua_State *L)
12561263 {
12571264 mp_lua_optarg(L, 2);
@@ -1338,6 +1345,7 @@ static const struct fn_entry utils_fns[] = { · 80 unmodified lines
13381345 FN_ENTRY(join_path),
13391346 FN_ENTRY(subprocess),
13401347 FN_ENTRY(subprocess_detached),
1348+ FN_ENTRY(getpid),
13411349 FN_ENTRY(parse_json),
13421350 FN_ENTRY(format_json),
13431351 {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