Usable for uniquely identifying mpv instances from subprocesses, controlling mpv with AppleScript, ... Adds a new mp_getpid() wrapper for cross-platform reasons.
Unified · Split
| @@ -180,6 +180,8 @@ Otherwise, where the Lua APIs return ``nil`` on error, JS returns ``undefined``. · 179 unmodified lines | ||
| 180 | 180 | |
| 181 | 181 | ``mp.utils.subprocess_detached(t)`` |
| 182 | 182 | |
| 183 | +``mp.utils.getpid()`` (LE) | |
| 184 | + | |
| 183 | 185 | ``mp.add_hook(type, priority, fn)`` |
| 184 | 186 | |
| 185 | 187 | Additional utilities |
| @@ -682,6 +682,10 @@ strictly part of the guaranteed API. · 681 unmodified lines | ||
| 682 | 682 | |
| 683 | 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 | 689 | ``utils.parse_json(str [, trail])`` |
| 686 | 690 | Parses the given string argument as JSON, and returns it as a Lua table. On |
| 687 | 691 | error, returns ``nil, error``. (Currently, ``error`` is just a string |
| @@ -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 | |
| @@ -40,6 +40,7 @@ · 39 unmodified lines | ||
| 40 | 40 | #include "osdep/subprocess.h" |
| 41 | 41 | #include "osdep/timer.h" |
| 42 | 42 | #include "osdep/threads.h" |
| 43 | +#include "osdep/getpid.h" | |
| 43 | 44 | #include "stream/stream.h" |
| 44 | 45 | #include "sub/osd.h" |
| 45 | 46 | #include "core.h" |
| @@ -990,6 +991,12 @@ static void script_subprocess_detached(js_State *J, void *af) · 944 unmodified lines | ||
| 990 | 991 | af_subprocess_common(J, 1, af); |
| 991 | 992 | } |
| 992 | 993 | |
| 994 | +// args: none | |
| 995 | +static void script_getpid(js_State *J) | |
| 996 | +{ | |
| 997 | + js_pushnumber(J, mp_getpid()); | |
| 998 | +} | |
| 999 | + | |
| 993 | 1000 | // args: prefixed file name, data (c-str) |
| 994 | 1001 | static 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 | 1303 | AF_ENTRY(get_user_path, 1), |
| 1297 | 1304 | AF_ENTRY(subprocess, 1), |
| 1298 | 1305 | AF_ENTRY(subprocess_detached, 1), |
| 1306 | + FN_ENTRY(getpid, 0), | |
| 1299 | 1307 | |
| 1300 | 1308 | FN_ENTRY(read_file, 2), |
| 1301 | 1309 | AF_ENTRY(write_file, 2), |
| @@ -44,6 +44,7 @@ · 43 unmodified lines | ||
| 44 | 44 | #include "osdep/subprocess.h" |
| 45 | 45 | #include "osdep/timer.h" |
| 46 | 46 | #include "osdep/threads.h" |
| 47 | +#include "osdep/getpid.h" | |
| 47 | 48 | #include "stream/stream.h" |
| 48 | 49 | #include "sub/osd.h" |
| 49 | 50 | #include "core.h" |
| @@ -1252,6 +1253,12 @@ static int script_subprocess_detached(lua_State *L) · 1202 unmodified lines | ||
| 1252 | 1253 | return 1; |
| 1253 | 1254 | } |
| 1254 | 1255 | |
| 1256 | +static int script_getpid(lua_State *L) | |
| 1257 | +{ | |
| 1258 | + lua_pushnumber(L, mp_getpid()); | |
| 1259 | + return 1; | |
| 1260 | +} | |
| 1261 | + | |
| 1255 | 1262 | static int script_parse_json(lua_State *L) |
| 1256 | 1263 | { |
| 1257 | 1264 | mp_lua_optarg(L, 2); |
| @@ -1338,6 +1345,7 @@ static const struct fn_entry utils_fns[] = { · 80 unmodified lines | ||
| 1338 | 1345 | FN_ENTRY(join_path), |
| 1339 | 1346 | FN_ENTRY(subprocess), |
| 1340 | 1347 | FN_ENTRY(subprocess_detached), |
| 1348 | + FN_ENTRY(getpid), | |
| 1341 | 1349 | FN_ENTRY(parse_json), |
| 1342 | 1350 | FN_ENTRY(format_json), |
| 1343 | 1351 | {0} |