McRogueFace/src/PyMouse.h
John McCardle bb72040396 Migrate static PyTypeObject to inline, delete PyTypeCache workarounds
All 27 PyTypeObject declarations in namespace mcrfpydef headers changed
from `static` to `inline` (C++17), ensuring a single global instance
across translation units. This fixes the root cause of stale-type-pointer
segfaults where only the McRFPy_API.cpp copy was PyType_Ready'd.

Replaced ~20 PyTypeCache call sites and 2 PyRAII::PyTypeRef lookups with
direct &mcrfpydef::Type references. Deleted PyTypeCache.h/.cpp,
PyObjectUtils.h, and PyRAII.h (all were workarounds for the static bug).

228/228 tests pass.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-16 20:58:09 -05:00

50 lines
1.8 KiB
C++

#pragma once
#include "Common.h"
#include "Python.h"
// Singleton mouse state object
typedef struct {
PyObject_HEAD
// Track state for properties without SFML getters
bool cursor_visible;
bool cursor_grabbed;
} PyMouseObject;
class PyMouse
{
public:
// Python getters - query real-time mouse state via SFML
static PyObject* get_x(PyObject* self, void* closure);
static PyObject* get_y(PyObject* self, void* closure);
static PyObject* get_pos(PyObject* self, void* closure);
// Button state getters
static PyObject* get_left(PyObject* self, void* closure);
static PyObject* get_right(PyObject* self, void* closure);
static PyObject* get_middle(PyObject* self, void* closure);
// Cursor visibility/grab (read-write, tracked internally)
static PyObject* get_visible(PyObject* self, void* closure);
static int set_visible(PyObject* self, PyObject* value, void* closure);
static PyObject* get_grabbed(PyObject* self, void* closure);
static int set_grabbed(PyObject* self, PyObject* value, void* closure);
static PyObject* repr(PyObject* obj);
static int init(PyMouseObject* self, PyObject* args, PyObject* kwds);
static PyGetSetDef getsetters[];
};
namespace mcrfpydef {
inline PyTypeObject PyMouseType = {
.ob_base = {.ob_base = {.ob_refcnt = 1, .ob_type = NULL}, .ob_size = 0},
.tp_name = "mcrfpy.Mouse",
.tp_basicsize = sizeof(PyMouseObject),
.tp_itemsize = 0,
.tp_repr = PyMouse::repr,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = PyDoc_STR("Mouse state singleton for reading button/position state and controlling cursor visibility"),
.tp_getset = PyMouse::getsetters,
.tp_init = (initproc)PyMouse::init,
.tp_new = PyType_GenericNew,
};
}