Skip to content

Commit

Permalink
Merge pull request #1843 from pygame/sdl2-screensaver-support
Browse files Browse the repository at this point in the history
display: Add support for controling screensaver state. part 2/2
  • Loading branch information
illume committed May 24, 2020
2 parents 6fcd4bc + 63b79f3 commit 3ea30cb
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 0 deletions.
2 changes: 2 additions & 0 deletions buildconfig/pygame-stubs/display.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ def get_caption() -> Tuple[str, str]: ...
def set_palette(palette: Sequence[_ColorValue]) -> None: ...
def get_num_displays() -> int: ...
def get_window_size() -> Tuple[int, int]: ...
def get_allow_screensaver() -> bool: ...
def set_allow_screensaver(value: bool) -> None: ...
48 changes: 48 additions & 0 deletions docs/reST/ref/display.rst
Original file line number Diff line number Diff line change
Expand Up @@ -571,4 +571,52 @@ required).

.. ## pygame.display.get_window_size ##
.. function:: get_allow_screensaver

| :sl:`Return whether the screensaver is allowed to run.`
| :sg:`get_allow_screensaver() -> bool`
Return whether screensaver is allowed to run whilst the app is running.
Default is False.
By default pygame does not allow the screensaver during game play.

.. note:: Some platforms do not have a screensaver or support
disabling the screensaver. Please see
:func:`pygame.display.set_allow_screensaver()` for
caveats with screensaver support.

.. versionadded:: 2.0

.. ## pygame.display.get_allow_screensaver ##
.. function:: set_allow_screensaver

| :sl:`Set whether the screensaver may run`
| :sg:`set_allow_screensaver(bool) -> None`
Change whether screensavers should be allowed whilst the app is running.
The default is False.
By default pygame does not allow the screensaver during game play.

If the screensaver has been disallowed due to this function, it will automatically
be allowed to run when :func:`pygame.quit()` is called.

It is possible to influence the default value via the environment variable
``SDL_HINT_VIDEO_ALLOW_SCREENSAVER``, which can be set to either ``0`` (disable)
or ``1`` (enable).

.. note:: Disabling screensaver is subject to platform support.
When platform support is absent, this function will
silently appear to work even though the screensaver state
is unchanged. The lack of feedback is due to SDL not
providing any supported method for determining whether
it supports changing the screensaver state.
``SDL_HINT_VIDEO_ALLOW_SCREENSAVER`` is available in SDL 2.0.2 or later.
SDL1.2 does not implement this.

.. versionadded:: 2.0


.. ## pygame.display.set_allow_screensaver ##
.. ## pygame.display ##
48 changes: 48 additions & 0 deletions src_c/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ static void
pg_do_set_icon(PyObject *surface);
static PyObject *pgDisplaySurfaceObject = NULL;
static int icon_was_set = 0;
static int _allow_screensaver = 0;

#else /* IS_SDLv2 */

Expand Down Expand Up @@ -2857,6 +2858,48 @@ pg_toggle_fullscreen(PyObject *self, PyObject *args)
}
#endif /* IS_SDLv1 */


static PyObject *
pg_get_allow_screensaver(PyObject *self) {
/* SDL_IsScreenSaverEnabled() unconditionally returns SDL_True if
* the video system is not initialized. Therefore we insist on
* the video being initialized before calling it.
*/
VIDEO_INIT_CHECK();
#if IS_SDLv2
return PyBool_FromLong(SDL_IsScreenSaverEnabled() == SDL_TRUE);
#else /* IS_SDLv1*/
return PyBool_FromLong(_allow_screensaver);
#endif /* IS_SDLv1*/
}

static PyObject *
pg_set_allow_screensaver(PyObject *self, PyObject *arg, PyObject *kwargs) {
int val = 1;
static char *keywords[] = {"value", NULL};

if (!PyArg_ParseTupleAndKeywords(arg, kwargs, "|i", keywords, &val)) {
return NULL;
}

VIDEO_INIT_CHECK();
if (val) {
#if IS_SDLv2
SDL_EnableScreenSaver();
#else
_allow_screensaver = 1;
#endif
} else {
#if IS_SDLv2
SDL_DisableScreenSaver();
#else
_allow_screensaver = 0;
#endif
}

Py_RETURN_NONE;
}

static PyMethodDef _pg_display_methods[] = {
{"__PYGAMEinit__", pg_display_autoinit, 1,
"auto initialize function for display."},
Expand Down Expand Up @@ -2914,6 +2957,11 @@ static PyMethodDef _pg_display_methods[] = {
{"gl_get_attribute", pg_gl_get_attribute, METH_VARARGS,
DOC_PYGAMEDISPLAYGLGETATTRIBUTE},

{"get_allow_screensaver", (PyCFunction)pg_get_allow_screensaver, METH_NOARGS,
DOC_PYGAMEDISPLAYGETALLOWSCREENSAVER},
{"set_allow_screensaver", (PyCFunction)pg_set_allow_screensaver, METH_VARARGS | METH_KEYWORDS,
DOC_PYGAMEDISPLAYSETALLOWSCREENSAVER},

{NULL, NULL, 0, NULL}};

#if IS_SDLv2
Expand Down
10 changes: 10 additions & 0 deletions src_c/doc/display_doc.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#define DOC_PYGAMEDISPLAYSETPALETTE "set_palette(palette=None) -> None\nSet the display color palette for indexed displays"
#define DOC_PYGAMEDISPLAYGETNUMDISPLAYS "get_num_displays() -> int\nReturn the number of displays"
#define DOC_PYGAMEDISPLAYGETWINDOWSIZE "get_window_size() -> tuple\nReturn the size of the window or screen"
#define DOC_PYGAMEDISPLAYGETALLOWSCREENSAVER "get_allow_screensaver() -> bool\nReturn whether the screensaver is allowed to run."
#define DOC_PYGAMEDISPLAYSETALLOWSCREENSAVER "set_allow_screensaver(bool) -> None\nSet whether the screensaver may run"


/* Docs in a comment... slightly easier to read. */
Expand Down Expand Up @@ -135,4 +137,12 @@ pygame.display.get_window_size
get_window_size() -> tuple
Return the size of the window or screen
pygame.display.get_allow_screensaver
get_allow_screensaver() -> bool
Return whether the screensaver is allowed to run.
pygame.display.set_allow_screensaver
set_allow_screensaver(bool) -> None
Set whether the screensaver may run
*/
9 changes: 9 additions & 0 deletions test/display_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,15 @@ def test_set_mode_scaled(self):
winsize[0] / surf.get_size()[0], winsize[1] / surf.get_size()[1]
)

def test_screensaver_support(self):
pygame.display.set_allow_screensaver(True)
self.assertTrue(pygame.display.get_allow_screensaver())
pygame.display.set_allow_screensaver(False)
self.assertFalse(pygame.display.get_allow_screensaver())
pygame.display.set_allow_screensaver()
self.assertTrue(pygame.display.get_allow_screensaver())


def todo_test_set_palette(self):

# __doc__ (as of 2008-08-02) for pygame.display.set_palette:
Expand Down

0 comments on commit 3ea30cb

Please sign in to comment.