From 49be15ac7bcae04459d89b0488c09ed7e875f57b Mon Sep 17 00:00:00 2001
From: Michael Webster <miketwebster@gmail.com>
Date: Fri, 19 Jul 2024 11:05:16 -0400
Subject: [PATCH] osk_virtkey.c: Don't use legacy Py_UNICODE for converting
 utf8 to keysyms.

The 'u' format is removed in python 3.12, switch to using
PyUnicode_AsWideCharString as recommended.

Fixes linuxmint/mint22-beta#22.
---
 Onboard/osk/osk_module.h  |  2 ++
 Onboard/osk/osk_virtkey.c | 19 ++++++++++++++-----
 2 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/Onboard/osk/osk_module.h b/Onboard/osk/osk_module.h
index f611f7b..d049df4 100644
--- a/Onboard/osk/osk_module.h
+++ b/Onboard/osk/osk_module.h
@@ -22,6 +22,8 @@
 #ifndef __OSK_MODULE__
 #define __OSK_MODULE__
 
+#define PY_SSIZE_T_CLEAN
+
 #include <Python.h>
 #include <structmember.h>
 
diff --git a/Onboard/osk/osk_virtkey.c b/Onboard/osk/osk_virtkey.c
index 87eb5bf..f054a5c 100644
--- a/Onboard/osk/osk_virtkey.c
+++ b/Onboard/osk/osk_virtkey.c
@@ -1461,14 +1461,23 @@ osk_virtkey_keycode_from_keysym (PyObject *self, PyObject *args)
 static PyObject *
 osk_virtkey_keysym_from_unicode (PyObject *self, PyObject *args)
 {
-    long keysym;
-    Py_UNICODE* ucs;
+    PyObject *utf_obj;
+    wchar_t *str = NULL;
 
-    if (!PyArg_ParseTuple (args, "u", &ucs))
+    if (!PyArg_ParseTuple (args, "O", &utf_obj))
         return NULL;
 
-    keysym = ucs2keysym(*ucs);
-    return PyLong_FromLong (keysym);
+    str = PyUnicode_AsWideCharString(utf_obj, NULL);
+    if (str != NULL)
+    {
+        long keysym;
+        keysym = ucs2keysym(str[0]);
+        PyMem_Free (str);
+
+        return PyLong_FromLong (keysym);
+    }
+
+    return NULL;
 }
 
 static PyObject *
-- 
GitLab

