Unconditionally save instant.xml on session-close
[ardour.git] / gtk2_ardour / screensaver.cc
1 /*
2  * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include "utils.h"
20
21 #ifdef PLATFORM_WINDOWS
22 /* http://www.blackwasp.co.uk/DisableScreensaver.aspx */
23 #include <windows.h>
24
25 void
26 ARDOUR_UI_UTILS::inhibit_screensaver (bool inhibit)
27 {
28         if (inhibit) {
29                 SetThreadExecutionState (ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED | ES_CONTINUOUS);
30         } else {
31                 SetThreadExecutionState (ES_CONTINUOUS);
32         }
33 }
34
35 #elif defined __APPLE__
36
37 #include <IOKit/pwr_mgt/IOPMLib.h>
38
39 static IOReturn success = kIOReturnError;
40 static IOPMAssertionID assertion_id;
41
42 void
43 ARDOUR_UI_UTILS::inhibit_screensaver (bool inhibit)
44 {
45         if (inhibit == (success == kIOReturnSuccess)) {
46                 return;
47         }
48
49         if (inhibit) {
50                 /* kIOPMAssertionTypeNoDisplaySleep prevents display sleep,
51                  * kIOPMAssertionTypeNoIdleSleep prevents idle sleep
52                  */
53 #ifdef __ppc__ /* OS X 10.5 compat API */
54                 success = IOPMAssertionCreate (kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, &assertion_id);
55 #else
56                 static const CFStringRef name = CFSTR("Ardour DAW");
57                 success = IOPMAssertionCreateWithName (kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, name, &assertion_id);
58 #endif
59         } else {
60                 if (kIOReturnSuccess == IOPMAssertionRelease (assertion_id)) {
61                         success = kIOReturnError; // mark as inactive
62                 }
63         }
64 }
65
66 #else /* Linux / X11 */
67
68 #include <gtkmm.h>
69 #include "ardour/system_exec.h"
70
71 static sigc::connection glib_timer;
72
73 static bool
74 xdg_screensaver_reset ()
75 {
76         char** args = (char**) malloc (5 * sizeof(char*));
77         args[0] = strdup ("/bin/sh");
78         args[1] = strdup ("-c");
79         args[2] = strdup ("xdg-screensaver");
80         args[3] = strdup ("reset");
81         args[4] = 0;
82
83         ARDOUR::SystemExec xdg_exec ("/bin/sh", args);
84         if (xdg_exec.start ()) {
85                 return false;
86         }
87         xdg_exec.wait ();
88         return true; /* keep going */
89 }
90
91 void
92 ARDOUR_UI_UTILS::inhibit_screensaver (bool inhibit)
93 {
94         glib_timer.disconnect ();
95         if (inhibit) {
96                 xdg_screensaver_reset ();
97                 glib_timer = Glib::signal_timeout().connect_seconds (sigc::ptr_fun (&xdg_screensaver_reset), 45, Glib::PRIORITY_DEFAULT_IDLE);
98         }
99 }
100
101 #endif