Get reading from clipboard working
This commit is contained in:
+8
-3
@@ -2,10 +2,10 @@
|
||||
# License: GPLv3 Copyright: 2022, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
import io
|
||||
from typing import IO, Callable, Dict, Union
|
||||
from typing import IO, Callable, Dict, Union, List
|
||||
|
||||
from .constants import supports_primary_selection
|
||||
from .fast_data_types import GLFW_CLIPBOARD, get_boss, set_clipboard_data_types
|
||||
from .fast_data_types import GLFW_CLIPBOARD, get_boss, set_clipboard_data_types, get_clipboard_mime
|
||||
|
||||
DataType = Union[bytes, 'IO[bytes]']
|
||||
|
||||
@@ -26,7 +26,12 @@ class Clipboard:
|
||||
set_clipboard_data_types(self.clipboard_type, tuple(self.data))
|
||||
|
||||
def get_text(self) -> str:
|
||||
raise NotImplementedError('TODO: Implement this')
|
||||
parts: List[bytes] = []
|
||||
self.get_mime("text/plain", parts.append)
|
||||
return b''.join(parts).decode('utf-8', 'replace')
|
||||
|
||||
def get_mime(self, mime: str, output: Callable[[bytes], None]) -> None:
|
||||
get_clipboard_mime(self.clipboard_type, mime, output)
|
||||
|
||||
def __call__(self, mime: str) -> Callable[[], bytes]:
|
||||
data = self.data.get(mime, b'')
|
||||
|
||||
@@ -1473,3 +1473,4 @@ def set_use_os_log(yes: bool) -> None: ...
|
||||
def get_docs_ref_map() -> bytes: ...
|
||||
def clearenv() -> None: ...
|
||||
def set_clipboard_data_types(ct: int, mime_types: Tuple[str, ...]) -> None: ...
|
||||
def get_clipboard_mime(ct: int, mime: Optional[str], callback: Callable[[bytes], None]) -> None: ...
|
||||
|
||||
Generated
+3
@@ -368,6 +368,9 @@ load_glfw(const char* path) {
|
||||
*(void **) (&glfwSetClipboardDataTypes_impl) = dlsym(handle, "glfwSetClipboardDataTypes");
|
||||
if (glfwSetClipboardDataTypes_impl == NULL) fail("Failed to load glfw function glfwSetClipboardDataTypes with error: %s", dlerror());
|
||||
|
||||
*(void **) (&glfwGetClipboard_impl) = dlsym(handle, "glfwGetClipboard");
|
||||
if (glfwGetClipboard_impl == NULL) fail("Failed to load glfw function glfwGetClipboard with error: %s", dlerror());
|
||||
|
||||
*(void **) (&glfwGetTime_impl) = dlsym(handle, "glfwGetTime");
|
||||
if (glfwGetTime_impl == NULL) fail("Failed to load glfw function glfwGetTime with error: %s", dlerror());
|
||||
|
||||
|
||||
Generated
+5
@@ -1467,6 +1467,7 @@ typedef enum {
|
||||
GLFW_CLIPBOARD, GLFW_PRIMARY_SELECTION
|
||||
} GLFWClipboardType;
|
||||
typedef GLFWDataChunk (* GLFWclipboarditerfun)(const char *mime_type, void *iter, GLFWClipboardType ctype);
|
||||
typedef bool (* GLFWclipboardwritedatafun)(void *object, const char *data, size_t sz);
|
||||
|
||||
/*! @brief Video mode type.
|
||||
*
|
||||
@@ -2096,6 +2097,10 @@ typedef void (*glfwSetClipboardDataTypes_func)(GLFWClipboardType, const char* co
|
||||
GFW_EXTERN glfwSetClipboardDataTypes_func glfwSetClipboardDataTypes_impl;
|
||||
#define glfwSetClipboardDataTypes glfwSetClipboardDataTypes_impl
|
||||
|
||||
typedef void (*glfwGetClipboard_func)(GLFWClipboardType, const char*, GLFWclipboardwritedatafun, void*);
|
||||
GFW_EXTERN glfwGetClipboard_func glfwGetClipboard_impl;
|
||||
#define glfwGetClipboard glfwGetClipboard_impl
|
||||
|
||||
typedef monotonic_t (*glfwGetTime_func)(void);
|
||||
GFW_EXTERN glfwGetTime_func glfwGetTime_impl;
|
||||
#define glfwGetTime glfwGetTime_impl
|
||||
|
||||
+30
-13
@@ -1590,16 +1590,10 @@ get_clipboard_data(const char *mime_type, void *iter, GLFWClipboardType ct) {
|
||||
if (global_state.boss == NULL) return ans;
|
||||
if (iter == NULL) {
|
||||
PyObject *c = PyObject_GetAttrString(global_state.boss, ct == GLFW_PRIMARY_SELECTION ? "primary_selection" : "clipboard");
|
||||
if (c == NULL) {
|
||||
PyErr_Print();
|
||||
return ans;
|
||||
}
|
||||
if (c == NULL) { return ans; }
|
||||
PyObject *i = PyObject_CallFunction(c, "s", mime_type);
|
||||
Py_DECREF(c);
|
||||
if (!i) {
|
||||
PyErr_Print();
|
||||
return ans;
|
||||
}
|
||||
if (!i) { return ans; }
|
||||
ans.iter = i;
|
||||
return ans;
|
||||
}
|
||||
@@ -1621,14 +1615,36 @@ set_clipboard_data_types(PyObject *self UNUSED, PyObject *args) {
|
||||
PyObject *mta;
|
||||
int ctype;
|
||||
if (!PyArg_ParseTuple(args, "iO!", &ctype, &PyTuple_Type, &mta)) return NULL;
|
||||
const char **mime_types = calloc(PyTuple_GET_SIZE(mta), sizeof(char*));
|
||||
if (!mime_types) return PyErr_NoMemory();
|
||||
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(mta); i++) mime_types[i] = PyUnicode_AsUTF8(PyTuple_GET_ITEM(mta, i));
|
||||
glfwSetClipboardDataTypes(ctype, mime_types, PyTuple_GET_SIZE(mta), get_clipboard_data);
|
||||
free(mime_types);
|
||||
if (glfwSetClipboardDataTypes) {
|
||||
const char **mime_types = calloc(PyTuple_GET_SIZE(mta), sizeof(char*));
|
||||
if (!mime_types) return PyErr_NoMemory();
|
||||
for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(mta); i++) mime_types[i] = PyUnicode_AsUTF8(PyTuple_GET_ITEM(mta, i));
|
||||
glfwSetClipboardDataTypes(ctype, mime_types, PyTuple_GET_SIZE(mta), get_clipboard_data);
|
||||
free(mime_types);
|
||||
} else log_error("GLFW not initialized cannot set clipboard data");
|
||||
if (PyErr_Occurred()) return NULL;
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
static bool
|
||||
write_clipboard_data(void *callback, const char *data, size_t sz) {
|
||||
Py_ssize_t z = sz;
|
||||
PyObject *ret = PyObject_CallFunction(callback, "y#", data, z);
|
||||
bool ok = false;
|
||||
if (ret != NULL) { ok = true; Py_DECREF(ret); }
|
||||
return ok;
|
||||
}
|
||||
|
||||
static PyObject*
|
||||
get_clipboard_mime(PyObject *self UNUSED, PyObject *args) {
|
||||
int ctype;
|
||||
const char *mime;
|
||||
PyObject *callback;
|
||||
if (!PyArg_ParseTuple(args, "izO", &ctype, &mime, &callback)) return NULL;
|
||||
glfwGetClipboard(ctype, mime, write_clipboard_data, callback);
|
||||
if (PyErr_Occurred()) return NULL;
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
// Boilerplate {{{
|
||||
|
||||
static PyMethodDef module_methods[] = {
|
||||
@@ -1636,6 +1652,7 @@ static PyMethodDef module_methods[] = {
|
||||
{"create_os_window", (PyCFunction)(void (*) (void))(create_os_window), METH_VARARGS | METH_KEYWORDS, NULL},
|
||||
METHODB(set_default_window_icon, METH_VARARGS),
|
||||
METHODB(set_clipboard_data_types, METH_VARARGS),
|
||||
METHODB(get_clipboard_mime, METH_VARARGS),
|
||||
METHODB(toggle_secure_input, METH_NOARGS),
|
||||
METHODB(get_content_scale_for_window, METH_NOARGS),
|
||||
METHODB(ring_bell, METH_NOARGS),
|
||||
|
||||
Reference in New Issue
Block a user