Skip to content

Commit

Permalink
feat: better dynamic IPC socket path
Browse files Browse the repository at this point in the history
  • Loading branch information
sitiom committed Jan 22, 2023
1 parent 861bfe5 commit f8dce53
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,10 @@ For Windows, config file is located in where the `mpv.exe` executable is.
* **active** (default: `yes`): whether to activate at launch
* **client_id**: specify your own client ID to [customize](#customization) the images shown in Rich Presence
* **binary_path**: full path to mpv-discord's binary file
* **socket_path** (default: `/tmp/mpvsocket`):
* `use_static_socket_path=yes`: set the full path to the static IPC socket path
* `use_static_socket_path=no`: set the full path to the directory placing the IPC socket with a *dynamic name*
* **use_static_socket_path** (default: `yes`): whether to use static IPC socket path or *dynamic name* in the path
* **socket_path** (default: `/tmp/mpvsocket` or `mpvsocket` in Windows):
* You can use `{pid}` to use a dynamic name for the socket path (i.e., `/tmp/mpvsocket_{pid}`)
* **autohide_threshold** (default: `0`): time in seconds before hiding the presence once player is paused (`0` is off)

*dynamic name* is in the format of `mpv-discord-1234` where `1234` will be the PID of the mpv instance.

## Customization

Go to [Discord Developer Portal](https://discord.com/developers/applications),
Expand Down
2 changes: 1 addition & 1 deletion script-opts/discord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ key=D
active=yes
client_id=737663962677510245
binary_path=
socket_path=/tmp/mpvsocket
socket_path=/tmp/mpvsocket -- or mpvsocket in Windows
use_static_socket_path=yes
autohide_threshold=0
29 changes: 16 additions & 13 deletions scripts/discord.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@ local msg = require("mp.msg")
local opts = require("mp.options")
local utils = require("mp.utils")

function detect_platform() --function to detect platform
local o = {}
if mp.get_property_native('options/vo-mmcss-profile', o) ~= o then
return "windows"
elseif mp.get_property_native('options/input-app-events', o) ~= o then
return "darwin"
end
return "linux"
end

local options = {
key = "D",
active = true,
client_id = "737663962677510245",
binary_path = "",
socket_path = "/tmp/mpvsocket",
use_static_socket_path = true,
socket_path = (detect_platform() ~= "windows" and "/tmp/" or "") .. "mpvsocket",
autohide_threshold = 0,
}
opts.read_options(options, "discord")
Expand Down Expand Up @@ -37,15 +46,9 @@ local version = "1.6.1"
msg.info(("mpv-discord v%s by tnychn"):format(version))

local socket_path = options.socket_path
if not options.use_static_socket_path then
local pid = utils.getpid()
local filename = ("mpv-discord-%s"):format(pid)
if socket_path == "" then
socket_path = "/tmp/" -- default
end
socket_path = utils.join_path(socket_path, filename)
elseif socket_path == "" then
msg.fatal("Missing socket path in config file.")
socket_path = socket_path:gsub("{pid}", utils.getpid())
if socket_path == "" then
msg.fatal "Missing socket path in config file."
os.exit(1)
end
msg.info(("(mpv-ipc): %s"):format(socket_path))
Expand Down Expand Up @@ -92,9 +95,9 @@ mp.register_event("shutdown", function()
if cmd ~= nil then
stop()
end
if not options.use_static_socket_path then
if detect_platform() ~= "windows" then
os.remove(socket_path)
end
end
end)

if options.autohide_threshold > 0 then
Expand Down

0 comments on commit f8dce53

Please sign in to comment.