Skip to content

Commit

Permalink
add "init-quit hook" for window module
Browse files Browse the repository at this point in the history
  • Loading branch information
yunline committed Oct 27, 2023
1 parent 018b27c commit 146bd31
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src_c/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ pg_init(PyObject *self, PyObject *_null)
const char *modnames[] = {
IMPPREFIX "display", /* Display first, this also inits event,time */
IMPPREFIX "joystick", IMPPREFIX "font", IMPPREFIX "freetype",
IMPPREFIX "mixer",
IMPPREFIX "mixer", IMPPREFIX "_window",
/* IMPPREFIX "_sdl2.controller", Is this required? Comment for now*/
NULL};

Expand Down Expand Up @@ -409,6 +409,7 @@ _pg_quit(void)
IMPPREFIX "freetype",
IMPPREFIX "font",
IMPPREFIX "joystick",
IMPPREFIX "_window",
IMPPREFIX "display", /* Display last, this also quits event,time */
NULL};

Expand Down
20 changes: 18 additions & 2 deletions src_c/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,20 @@ window_repr(pgWindowObject *self)
return PyUnicode_FromFormat("<Window(title='%s', id=%d)>", title, win_id);
}

static PyObject *
_window_internal_mod_init(PyObject *self, PyObject *_null)
{
SDL_AddEventWatch(_resize_event_watch, NULL);
Py_RETURN_NONE;
}

static PyObject *
_window_internal_mod_quit(PyObject *self, PyObject *_null)
{
SDL_DelEventWatch(_resize_event_watch, NULL);
Py_RETURN_NONE;
}

static PyMethodDef window_methods[] = {
{"destroy", (PyCFunction)window_destroy, METH_NOARGS,
DOC_SDL2_VIDEO_WINDOW_DESTROY},
Expand Down Expand Up @@ -1053,6 +1067,10 @@ static PyTypeObject pgWindow_Type = {
static PyMethodDef _window_methods[] = {
{"get_grabbed_window", (PyCFunction)get_grabbed_window, METH_NOARGS,
DOC_SDL2_VIDEO_GETGRABBEDWINDOW},
{"_internal_mod_init", (PyCFunction)_window_internal_mod_init, METH_NOARGS,
"auto initialize for _window module"},
{"_internal_mod_quit", (PyCFunction)_window_internal_mod_quit, METH_NOARGS,
"auto quit for _window module"},
{NULL, NULL, 0, NULL}};

MODINIT_DEFINE(_window)
Expand Down Expand Up @@ -1113,7 +1131,5 @@ MODINIT_DEFINE(_window)
return NULL;
}

SDL_AddEventWatch(_resize_event_watch, NULL);

return module;
}

0 comments on commit 146bd31

Please sign in to comment.