-rw-r--r-- | core/launcher/screensaver.cpp | 97 |
1 files changed, 77 insertions, 20 deletions
diff --git a/core/launcher/screensaver.cpp b/core/launcher/screensaver.cpp index b9bb64c..182084d 100644 --- a/core/launcher/screensaver.cpp +++ b/core/launcher/screensaver.cpp | |||
@@ -25,246 +25,303 @@ OpieScreenSaver::OpieScreenSaver ( ) | |||
25 | m_enable_lightoff_ac = false; | 25 | m_enable_lightoff_ac = false; |
26 | m_enable_suspend_ac = false; | 26 | m_enable_suspend_ac = false; |
27 | m_onlylcdoff_ac = false; | 27 | m_onlylcdoff_ac = false; |
28 | 28 | ||
29 | m_use_light_sensor = false; | 29 | m_use_light_sensor = false; |
30 | m_backlight_sensor = -1; | 30 | m_backlight_sensor = -1; |
31 | ::memset ( m_sensordata, 0xff, LS_Count * sizeof( m_sensordata [0] )); | 31 | ::memset ( m_sensordata, 0xff, LS_Count * sizeof( m_sensordata [0] )); |
32 | 32 | ||
33 | m_lcd_status = true; | 33 | m_lcd_status = true; |
34 | 34 | ||
35 | m_backlight_normal = -1; | 35 | m_backlight_normal = -1; |
36 | m_backlight_current = -1; | 36 | m_backlight_current = -1; |
37 | m_backlight_forcedoff = false; | 37 | m_backlight_forcedoff = false; |
38 | 38 | ||
39 | m_on_ac = false; | 39 | m_on_ac = false; |
40 | 40 | ||
41 | m_level = -1; | 41 | m_level = -1; |
42 | 42 | ||
43 | // Make sure the LCD is in fact on, (if opie was killed while the LCD is off it would still be off) | 43 | // Make sure the LCD is in fact on, (if opie was killed while the LCD is off it would still be off) |
44 | ODevice::inst ( )-> setDisplayStatus ( true ); | 44 | ODevice::inst ( )-> setDisplayStatus ( true ); |
45 | setBacklight ( -1 ); | 45 | setBacklight ( -1 ); |
46 | } | 46 | } |
47 | 47 | ||
48 | 48 | ||
49 | /** | ||
50 | * Stops the screen saver | ||
51 | */ | ||
49 | void OpieScreenSaver::restore() | 52 | void OpieScreenSaver::restore() |
50 | { | 53 | { |
51 | m_level = -1; | 54 | m_level = -1; |
52 | 55 | ||
53 | if ( !m_lcd_status ) { // We must have turned it off | 56 | if ( !m_lcd_status ) { // We must have turned it off |
54 | ODevice::inst ( ) -> setDisplayStatus ( true ); | 57 | ODevice::inst ( ) -> setDisplayStatus ( true ); |
55 | m_lcd_status = true; | 58 | m_lcd_status = true; |
56 | } | 59 | } |
57 | 60 | ||
58 | setBacklightInternal ( -1 ); | 61 | setBacklightInternal ( -1 ); |
59 | } | 62 | } |
60 | 63 | ||
61 | 64 | ||
65 | /** | ||
66 | * Starts the screen saver | ||
67 | * | ||
68 | * @param level what level of screen saving should happen (0=lowest non-off, 1=off, | ||
69 | * 2=suspend whole machine) | ||
70 | * @returns true on success | ||
71 | */ | ||
62 | bool OpieScreenSaver::save( int level ) | 72 | bool OpieScreenSaver::save( int level ) |
63 | { | 73 | { |
64 | m_level = level; | 74 | m_level = level; |
65 | 75 | ||
66 | switch ( level ) { | 76 | switch ( level ) { |
67 | case 0: | 77 | case 0: |
68 | if (( m_on_ac && m_enable_dim_ac ) || | 78 | if (( m_on_ac && m_enable_dim_ac ) || |
69 | ( !m_on_ac && m_enable_dim )) { | 79 | ( !m_on_ac && m_enable_dim )) { |
70 | if (( m_disable_suspend > 0 ) && ( m_backlight_current > 1 ) && !m_use_light_sensor ) | 80 | if (( m_disable_suspend > 0 ) && ( m_backlight_current > 1 ) && !m_use_light_sensor ) |
71 | setBacklightInternal ( 1 ); // lowest non-off | 81 | setBacklightInternal ( 1 ); // lowest non-off |
72 | } | 82 | } |
73 | return true; | 83 | return true; |
74 | break; | 84 | break; |
75 | 85 | ||
76 | case 1: | 86 | case 1: |
77 | if (( m_on_ac && m_enable_lightoff_ac ) || | 87 | if (( m_on_ac && m_enable_lightoff_ac ) || |
78 | ( !m_on_ac && m_enable_lightoff )) { | 88 | ( !m_on_ac && m_enable_lightoff )) { |
79 | if ( m_disable_suspend > 1 ) | 89 | if ( m_disable_suspend > 1 ) |
80 | setBacklightInternal ( 0 ); // off | 90 | setBacklightInternal ( 0 ); // off |
81 | } | 91 | } |
82 | return true; | 92 | return true; |
83 | break; | 93 | break; |
84 | 94 | ||
85 | case 2: | 95 | case 2: |
86 | if (( m_on_ac && !m_enable_suspend_ac ) || | 96 | if (( m_on_ac && !m_enable_suspend_ac ) || |
87 | ( !m_on_ac && !m_enable_suspend )) { | 97 | ( !m_on_ac && !m_enable_suspend )) { |
88 | return true; | 98 | return true; |
89 | } | 99 | } |
90 | 100 | ||
91 | if (( m_on_ac && m_onlylcdoff_ac ) || | 101 | if (( m_on_ac && m_onlylcdoff_ac ) || |
92 | ( !m_on_ac && m_onlylcdoff )) { | 102 | ( !m_on_ac && m_onlylcdoff )) { |
93 | ODevice::inst ( ) -> setDisplayStatus ( false ); | 103 | ODevice::inst ( ) -> setDisplayStatus ( false ); |
94 | m_lcd_status = false; | 104 | m_lcd_status = false; |
95 | return true; | 105 | return true; |
96 | } | 106 | } |
97 | 107 | ||
98 | // We're going to suspend the whole machine | 108 | // We're going to suspend the whole machine |
99 | 109 | ||
100 | if (( m_disable_suspend > 2 ) && !Network::networkOnline ( )) { | 110 | if (( m_disable_suspend > 2 ) && !Network::networkOnline ( )) { |
111 | // TODO: why is this key F34 hard coded? -- schurig | ||
112 | // Does this now only work an devices with a ODevice::filter? | ||
101 | QWSServer::sendKeyEvent( 0xffff, Qt::Key_F34, FALSE, TRUE, FALSE ); | 113 | QWSServer::sendKeyEvent( 0xffff, Qt::Key_F34, FALSE, TRUE, FALSE ); |
102 | return true; | 114 | return true; |
103 | } | 115 | } |
104 | 116 | ||
105 | break; | 117 | break; |
106 | } | 118 | } |
107 | return false; | 119 | return false; |
108 | } | 120 | } |
109 | 121 | ||
110 | 122 | ||
111 | void OpieScreenSaver::setIntervals ( int i1, int i2, int i3 ) | 123 | /** |
124 | * Set intervals in seconds for automatic dimming, light off and suspend | ||
125 | * | ||
126 | * This function also sets the member variables m_m_enable_dim[_ac], | ||
127 | * m_enable_lightoff[_ac], m_enable_suspend[_ac], m_onlylcdoff[_ac] | ||
128 | * | ||
129 | * @param dim time in seconds to dim, -1 to read value from config file, | ||
130 | * 0 to disable | ||
131 | * @param lightoff time in seconds to turn LCD backlight off, -1 to | ||
132 | * read value from config file, 0 to disable | ||
133 | * @param suspend time in seconds to do an APM suspend, -1 to | ||
134 | * read value from config file, 0 to disable | ||
135 | */ | ||
136 | void OpieScreenSaver::setIntervals ( int dim, int lightoff, int suspend ) | ||
112 | { | 137 | { |
113 | Config config ( "apm" ); | 138 | Config config ( "apm" ); |
114 | config. setGroup ( m_on_ac ? "AC" : "Battery" ); | 139 | config. setGroup ( m_on_ac ? "AC" : "Battery" ); |
115 | 140 | ||
116 | int v[ 4 ]; | 141 | int v[ 4 ]; |
117 | if ( i1 < 0 ) | 142 | if ( dim < 0 ) |
118 | i1 = config. readNumEntry ( "Dim", m_on_ac ? 60 : 30 ); | 143 | dim = config. readNumEntry ( "Dim", m_on_ac ? 60 : 30 ); |
119 | if ( i2 < 0 ) | 144 | if ( lightoff < 0 ) |
120 | i2 = config. readNumEntry ( "LightOff", m_on_ac ? 120 : 20 ); | 145 | lightoff = config. readNumEntry ( "LightOff", m_on_ac ? 120 : 20 ); |
121 | if ( i3 < 0 ) | 146 | if ( suspend < 0 ) |
122 | i3 = config. readNumEntry ( "Suspend", m_on_ac ? 0 : 60 ); | 147 | suspend = config. readNumEntry ( "Suspend", m_on_ac ? 0 : 60 ); |
123 | 148 | ||
124 | if ( m_on_ac ) { | 149 | if ( m_on_ac ) { |
125 | m_enable_dim_ac = ( i1 > 0 ); | 150 | m_enable_dim_ac = ( dim > 0 ); |
126 | m_enable_lightoff_ac = ( i2 > 0 ); | 151 | m_enable_lightoff_ac = ( lightoff > 0 ); |
127 | m_enable_suspend_ac = ( i3 > 0 ); | 152 | m_enable_suspend_ac = ( suspend > 0 ); |
128 | m_onlylcdoff_ac = config.readBoolEntry ( "LcdOffOnly", false ); | 153 | m_onlylcdoff_ac = config.readBoolEntry ( "LcdOffOnly", false ); |
129 | } | 154 | } |
130 | else { | 155 | else { |
131 | m_enable_dim = ( i1 > 0 ); | 156 | m_enable_dim = ( dim > 0 ); |
132 | m_enable_lightoff = ( i2 > 0 ); | 157 | m_enable_lightoff = ( lightoff > 0 ); |
133 | m_enable_suspend = ( i3 > 0 ); | 158 | m_enable_suspend = ( suspend > 0 ); |
134 | m_onlylcdoff = config.readBoolEntry ( "LcdOffOnly", false ); | 159 | m_onlylcdoff = config.readBoolEntry ( "LcdOffOnly", false ); |
135 | } | 160 | } |
136 | 161 | ||
137 | //qDebug("screen saver intervals: %d %d %d", i1, i2, i3); | 162 | //qDebug("screen saver intervals: %d %d %d", dim, lightoff, suspend); |
138 | 163 | ||
139 | v [ 0 ] = QMAX( 1000 * i1, 100 ); | 164 | v [ 0 ] = QMAX( 1000 * dim, 100 ); |
140 | v [ 1 ] = QMAX( 1000 * i2, 100 ); | 165 | v [ 1 ] = QMAX( 1000 * lightoff, 100 ); |
141 | v [ 2 ] = QMAX( 1000 * i3, 100 ); | 166 | v [ 2 ] = QMAX( 1000 * suspend, 100 ); |
142 | v [ 3 ] = 0; | 167 | v [ 3 ] = 0; |
143 | 168 | ||
144 | if ( !i1 && !i2 && !i3 ) | 169 | if ( !dim && !lightoff && !suspend ) |
145 | QWSServer::setScreenSaverInterval( 0 ); | 170 | QWSServer::setScreenSaverInterval( 0 ); |
146 | else | 171 | else |
147 | QWSServer::setScreenSaverIntervals( v ); | 172 | QWSServer::setScreenSaverIntervals( v ); |
148 | } | 173 | } |
149 | 174 | ||
150 | 175 | ||
176 | /** | ||
177 | * Set suspend time. Will read the dim and lcd-off times from the config file. | ||
178 | * | ||
179 | * @param suspend time in seconds to go into APM suspend, -1 to | ||
180 | * read value from config file, 0 to disable | ||
181 | */ | ||
151 | void OpieScreenSaver::setInterval ( int interval ) | 182 | void OpieScreenSaver::setInterval ( int interval ) |
152 | { | 183 | { |
153 | setIntervals ( -1, -1, interval ); | 184 | setIntervals ( -1, -1, interval ); |
154 | } | 185 | } |
155 | 186 | ||
156 | 187 | ||
157 | void OpieScreenSaver::setMode ( int mode ) | 188 | void OpieScreenSaver::setMode ( int mode ) |
158 | { | 189 | { |
159 | if ( mode > m_disable_suspend ) | 190 | if ( mode > m_disable_suspend ) |
160 | setInterval ( -1 ); | 191 | setInterval ( -1 ); |
161 | m_disable_suspend = mode; | 192 | m_disable_suspend = mode; |
162 | } | 193 | } |
163 | 194 | ||
164 | 195 | ||
196 | /** | ||
197 | * Set display brightness | ||
198 | * | ||
199 | * Get's default values for backlight and light sensor from config file. | ||
200 | * | ||
201 | * @param bright desired brighness (-1 to use automatic sensor data or value | ||
202 | * from config file, -2 to toggle backlight on and off, -3 to | ||
203 | * force backlight off) | ||
204 | */ | ||
165 | void OpieScreenSaver::setBacklight ( int bright ) | 205 | void OpieScreenSaver::setBacklight ( int bright ) |
166 | { | 206 | { |
167 | // Read from config | 207 | // Read from config |
168 | Config config ( "apm" ); | 208 | Config config ( "apm" ); |
169 | config. setGroup ( m_on_ac ? "AC" : "Battery" ); | 209 | config. setGroup ( m_on_ac ? "AC" : "Battery" ); |
170 | m_backlight_normal = config. readNumEntry ( "Brightness", m_on_ac ? 255 : 127 ); | 210 | m_backlight_normal = config. readNumEntry ( "Brightness", m_on_ac ? 255 : 127 ); |
171 | |||
172 | m_use_light_sensor = config. readBoolEntry ( "LightSensor", false ); | 211 | m_use_light_sensor = config. readBoolEntry ( "LightSensor", false ); |
173 | 212 | ||
174 | //qDebug ( "setBacklight: %d (norm: %d) (ls: %d)", bright, m_backlight_normal, m_use_light_sensor ? 1 : 0 ); | 213 | //qDebug ( "setBacklight: %d (norm: %d) (ls: %d)", bright, m_backlight_normal, m_use_light_sensor ? 1 : 0 ); |
175 | 214 | ||
176 | killTimers ( ); | 215 | killTimers ( ); |
177 | if (( bright < 0 ) && m_use_light_sensor ) { | 216 | if (( bright < 0 ) && m_use_light_sensor ) { |
178 | QStringList sl = config. readListEntry ( "LightSensorData", ';' ); | 217 | QStringList sl = config. readListEntry ( "LightSensorData", ';' ); |
179 | 218 | ||
180 | m_sensordata [LS_SensorMin] = 40; | 219 | m_sensordata [LS_SensorMin] = 40; |
181 | m_sensordata [LS_SensorMax] = 215; | 220 | m_sensordata [LS_SensorMax] = 215; |
182 | m_sensordata [LS_LightMin] = 1; | 221 | m_sensordata [LS_LightMin] = 1; |
183 | m_sensordata [LS_LightMax] = 255; | 222 | m_sensordata [LS_LightMax] = 255; |
184 | m_sensordata [LS_Steps] = 12; | 223 | m_sensordata [LS_Steps] = 12; |
185 | m_sensordata [LS_Interval] = 2000; | 224 | m_sensordata [LS_Interval] = 2000; |
186 | 225 | ||
187 | for ( uint i = 0; i < LS_Count; i++ ) { | 226 | for ( uint i = 0; i < LS_Count; i++ ) { |
188 | if ( i < sl. count ( )) | 227 | if ( i < sl. count ( )) |
189 | m_sensordata [i] = sl [i]. toInt ( ); | 228 | m_sensordata [i] = sl [i]. toInt ( ); |
190 | } | 229 | } |
191 | if ( m_sensordata [LS_Steps] < 2 ) // sanity check to avoid SIGFPE | 230 | if ( m_sensordata [LS_Steps] < 2 ) // sanity check to avoid SIGFPE |
192 | m_sensordata [LS_Steps] = 2; | 231 | m_sensordata [LS_Steps] = 2; |
193 | 232 | ||
194 | timerEvent ( 0 ); | 233 | timerEvent ( 0 ); |
195 | startTimer ( m_sensordata [LS_Interval] ); | 234 | startTimer ( m_sensordata [LS_Interval] ); |
196 | } | 235 | } |
197 | 236 | ||
198 | setBacklightInternal ( bright ); | 237 | setBacklightInternal ( bright ); |
199 | } | 238 | } |
200 | 239 | ||
201 | 240 | ||
241 | /** | ||
242 | * Internal brightness setting method | ||
243 | * | ||
244 | * Get's default values for backlight and light sensor from config file. | ||
245 | * | ||
246 | * @param bright desired brighness (-1 to use automatic sensor data or value | ||
247 | * from config file, -2 to toggle backlight on and off, -3 to | ||
248 | * force backlight off) | ||
249 | */ | ||
202 | void OpieScreenSaver::setBacklightInternal ( int bright ) | 250 | void OpieScreenSaver::setBacklightInternal ( int bright ) |
203 | { | 251 | { |
204 | if ( bright == -3 ) { | 252 | if ( bright == -3 ) { |
205 | // Forced on | 253 | // Forced on |
206 | m_backlight_forcedoff = false; | 254 | m_backlight_forcedoff = false; |
207 | bright = -1; | 255 | bright = -1; |
208 | } | 256 | } |
209 | if ( m_backlight_forcedoff && bright != -2 ) | 257 | if ( m_backlight_forcedoff && bright != -2 ) |
210 | return ; | 258 | return ; |
211 | if ( bright == -2 ) { | 259 | if ( bright == -2 ) { |
212 | // Toggle between off and on | 260 | // Toggle between off and on |
213 | bright = m_backlight_current ? 0 : -1; | 261 | bright = m_backlight_current ? 0 : -1; |
214 | m_backlight_forcedoff = !bright; | 262 | m_backlight_forcedoff = !bright; |
215 | } | 263 | } |
216 | if ( bright == -1 ) | 264 | if ( bright == -1 ) |
217 | bright = m_use_light_sensor ? m_backlight_sensor : m_backlight_normal; | 265 | bright = m_use_light_sensor ? m_backlight_sensor : m_backlight_normal; |
218 | 266 | ||
219 | if ( bright != m_backlight_current ) { | 267 | if ( bright != m_backlight_current ) { |
220 | ODevice::inst ( )-> setDisplayBrightness ( bright ); | 268 | ODevice::inst ( )-> setDisplayBrightness ( bright ); |
221 | m_backlight_current = bright; | 269 | m_backlight_current = bright; |
222 | } | 270 | } |
223 | } | 271 | } |
224 | 272 | ||
225 | 273 | ||
274 | /** | ||
275 | * Timer event used for automatic setting the backlight according to a light sensor | ||
276 | * and to set the default brightness | ||
277 | */ | ||
226 | void OpieScreenSaver::timerEvent ( QTimerEvent * ) | 278 | void OpieScreenSaver::timerEvent ( QTimerEvent * ) |
227 | { | 279 | { |
228 | int s = ODevice::inst ( )-> readLightSensor ( ) * 256 / ODevice::inst ( )-> lightSensorResolution ( ); | 280 | int s = ODevice::inst ( )-> readLightSensor ( ) * 256 / ODevice::inst ( )-> lightSensorResolution ( ); |
229 | 281 | ||
230 | if ( s < m_sensordata [LS_SensorMin] ) | 282 | if ( s < m_sensordata [LS_SensorMin] ) |
231 | m_backlight_sensor = m_sensordata [LS_LightMax]; | 283 | m_backlight_sensor = m_sensordata [LS_LightMax]; |
232 | else if ( s >= m_sensordata [LS_SensorMax] ) | 284 | else if ( s >= m_sensordata [LS_SensorMax] ) |
233 | m_backlight_sensor = m_sensordata [LS_LightMin]; | 285 | m_backlight_sensor = m_sensordata [LS_LightMin]; |
234 | else { | 286 | else { |
235 | int dx = m_sensordata [LS_SensorMax] - m_sensordata [LS_SensorMin]; | 287 | int dx = m_sensordata [LS_SensorMax] - m_sensordata [LS_SensorMin]; |
236 | int dy = m_sensordata [LS_LightMax] - m_sensordata [LS_LightMin]; | 288 | int dy = m_sensordata [LS_LightMax] - m_sensordata [LS_LightMin]; |
237 | 289 | ||
238 | int stepno = ( s - m_sensordata [LS_SensorMin] ) * m_sensordata [LS_Steps] / dx; // dx is never 0 | 290 | int stepno = ( s - m_sensordata [LS_SensorMin] ) * m_sensordata [LS_Steps] / dx; // dx is never 0 |
239 | 291 | ||
240 | m_backlight_sensor = m_sensordata [LS_LightMax] - dy * stepno / ( m_sensordata [LS_Steps] - 1 ); | 292 | m_backlight_sensor = m_sensordata [LS_LightMax] - dy * stepno / ( m_sensordata [LS_Steps] - 1 ); |
241 | } | 293 | } |
242 | 294 | ||
243 | //qDebug ( "f(%d) = %d [%d - %d] -> [%d - %d] / %d", s, m_backlight_sensor, m_sensordata [LS_SensorMin], m_sensordata [LS_SensorMax], m_sensordata [LS_LightMin], m_sensordata [LS_LightMax], m_sensordata [LS_Steps] ); | 295 | //qDebug ( "f(%d) = %d [%d - %d] -> [%d - %d] / %d", s, m_backlight_sensor, m_sensordata [LS_SensorMin], m_sensordata [LS_SensorMax], m_sensordata [LS_LightMin], m_sensordata [LS_LightMax], m_sensordata [LS_Steps] ); |
244 | 296 | ||
245 | if ( m_level <= 0 ) | 297 | if ( m_level <= 0 ) |
246 | setBacklightInternal ( -1 ); | 298 | setBacklightInternal ( -1 ); |
247 | } | 299 | } |
248 | 300 | ||
249 | 301 | ||
302 | /** | ||
303 | * Like ODevice::setDisplayStatus(), but keep current state in m_lcd_status. | ||
304 | */ | ||
250 | void OpieScreenSaver::setDisplayState ( bool on ) | 305 | void OpieScreenSaver::setDisplayState ( bool on ) |
251 | { | 306 | { |
252 | if ( m_lcd_status != on ) { | 307 | if ( m_lcd_status != on ) { |
253 | ODevice::inst ( ) -> setDisplayStatus ( on ); | 308 | ODevice::inst ( ) -> setDisplayStatus ( on ); |
254 | m_lcd_status = on; | 309 | m_lcd_status = on; |
255 | } | 310 | } |
256 | } | 311 | } |
257 | 312 | ||
258 | 313 | ||
314 | /** | ||
315 | * Set display to default ac/battery settings when power status changed. | ||
316 | */ | ||
259 | void OpieScreenSaver::powerStatusChanged ( PowerStatus ps ) | 317 | void OpieScreenSaver::powerStatusChanged ( PowerStatus ps ) |
260 | { | 318 | { |
261 | bool newonac = ( ps. acStatus ( ) == PowerStatus::Online ); | 319 | bool newonac = ( ps. acStatus ( ) == PowerStatus::Online ); |
262 | 320 | ||
263 | if ( newonac != m_on_ac ) { | 321 | if ( newonac != m_on_ac ) { |
264 | m_on_ac = newonac; | 322 | m_on_ac = newonac; |
265 | setInterval ( -1 ); | 323 | setInterval ( -1 ); |
266 | setBacklight ( -1 ); | 324 | setBacklight ( -1 ); |
267 | restore ( ); | 325 | restore ( ); |
268 | } | 326 | } |
269 | } | 327 | } |
270 | |||