Code to get and use machine id during DnD

This commit is contained in:
Kovid Goyal
2026-04-09 07:47:57 +05:30
parent c08409a981
commit 0684c13898
3 changed files with 48 additions and 2 deletions

View File

@@ -133,8 +133,8 @@ terminal emulator must then inform the OS that the drop is completed.
Dropping from remote machines
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
In order to support dropping of files from remote machines or to remote
machines, clients can first request the :rfc:`text/uri-list <2483>` MIME
In order to support dropping of files from remote machines,
clients can first request the :rfc:`text/uri-list <2483>` MIME
type to get a list of dropped URIs. For every URI in the list, they can
send the terminal emulator a data request of the form::

View File

@@ -25,6 +25,28 @@ static const size_t PRESENT_DATA_CAP = 64 * 1024 * 1024;
// It receives (window_id: int, data: bytes) and its return value is ignored.
static PyObject *g_dnd_test_write_func = NULL;
static const char*
machine_id(void) {
static bool done = false;
static char ans[512] = {0};
if (!done) {
done = true;
RAII_PyObject(mname, PyUnicode_DecodeFSDefault("kitty.machine_id"));
if (mname) {
RAII_PyObject(module, PyImport_Import(mname));
if (module) {
RAII_PyObject(func, PyObject_GetAttrString(module, "machine_id"));
if (func) {
RAII_PyObject(ret, PyObject_CallFunction(func, "s", "tty-dnd-protocol-machine-id"));
if (ret) snprintf(ans, sizeof(ans), "%s", PyUnicode_AsUTF8(ret));
}
}
}
if (PyErr_Occurred()) PyErr_Print();
}
return ans;
}
void
dnd_set_test_write_func(PyObject *func) {
Py_XDECREF(g_dnd_test_write_func);

24
kitty/machine_id.py Normal file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env python
# License: GPLv3 Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net>
import hashlib
import hmac
from contextlib import suppress
from functools import lru_cache
from kitty.constants import is_macos
@lru_cache(maxsize=8)
def machine_id(salt: str = '') -> str:
mid = b''
if is_macos:
from kitty.fast_data_types import cocoa_get_machine_id
mid = cocoa_get_machine_id().rstrip().encode()
else:
with suppress(OSError), open('/etc/machine-id', 'rb') as f:
mid = f.read().rstrip()
if not salt:
return mid.decode()
hmac_obj = hmac.new(salt.encode(), mid, hashlib.sha256)
return hmac_obj.hexdigest()