~mfisch/powerd/pid-death

« back to all changes in this revision

Viewing changes to src/powerd.cpp

  • Committer: Tarmac
  • Author(s): Seth Forshee
  • Date: 2013-06-14 20:55:23 UTC
  • mfrom: (43.3.15 autodim)
  • Revision ID: tarmac-20130614205523-04l1e680llso5cc1
* Add control of screen brightness based on bright/dim display state requests
* Add auto-dimming of the screen. The timeout after which the screen will be dimmed is controlled by the dim-timeout setting.
* Fix bug in powerd-cli where bright state is used when dim is specified on the command line.

Approved by Matthew Fischer, PS Jenkins bot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
73
73
/* The real default for this is set in the gschema file, but set 60 here
74
74
 * as a sanity check */
75
75
static int activity_timeout = 60;
 
76
static int dim_timeout = 45;
 
77
static GSettings *powerd_settings = NULL;
 
78
 
76
79
static GMainLoop *main_loop = NULL;
77
80
static guint name_id;
78
81
 
84
87
 
85
88
static state button_state  = BUTTON_UP;
86
89
 
 
90
enum {
 
91
    SCREEN_STATE_OFF = 0,
 
92
    SCREEN_STATE_DIM,
 
93
    SCREEN_STATE_BRIGHT
 
94
};
 
95
 
 
96
/* Assume screen starts off when powerd starts */
 
97
int activity_timer_screen_state = SCREEN_STATE_OFF;
 
98
 
87
99
/*
88
100
 * Display request for activity timer
89
101
 *
108
120
 
109
121
 
110
122
gboolean activity_monitor(gpointer data);
 
123
void update_screen_state(int state);
111
124
 
112
125
gboolean call_shutdown(gpointer data)
113
126
{
126
139
 
127
140
gboolean activity_monitor(gpointer data)
128
141
{
129
 
    powerd_remove_display_request(activity_timer_req.cookie);
 
142
    int new_state;
 
143
    gboolean ret = FALSE;
 
144
 
 
145
    g_mutex_lock(&activity_timer_mutex);
 
146
 
 
147
    if (activity_timer_screen_state > SCREEN_STATE_OFF) {
 
148
        if (dim_timeout > 0 && activity_timeout > dim_timeout)
 
149
            new_state = activity_timer_screen_state - 1;
 
150
        else
 
151
            new_state = SCREEN_STATE_OFF;
 
152
 
 
153
        update_screen_state(new_state);
 
154
        if (new_state != SCREEN_STATE_OFF && activity_timer > 0)
 
155
            activity_timer = g_timeout_add_seconds(activity_timeout - dim_timeout,
 
156
                                                   activity_monitor, NULL);
 
157
    }
 
158
 
 
159
    g_mutex_unlock(&activity_timer_mutex);
130
160
    return FALSE;
131
161
}
132
162
 
173
203
    g_main_loop_quit(main_loop);
174
204
}
175
205
 
 
206
int update_screen_state_worker(gpointer data)
 
207
{
 
208
    int new_state = (int)data;
 
209
    int ret;
 
210
 
 
211
    if (new_state == activity_timer_screen_state)
 
212
        return 0;
 
213
 
 
214
    if (new_state == SCREEN_STATE_OFF) {
 
215
        powerd_remove_display_request(activity_timer_req.cookie);
 
216
    } else {
 
217
        if (new_state == SCREEN_STATE_DIM)
 
218
            activity_timer_req.brightness = POWERD_DISPLAY_BRIGHTNESS_DIM;
 
219
        else
 
220
            activity_timer_req.brightness = POWERD_DISPLAY_BRIGHTNESS_BRIGHT;
 
221
 
 
222
        if (activity_timer_screen_state == SCREEN_STATE_OFF)
 
223
            ret = powerd_add_display_request(&activity_timer_req);
 
224
        else
 
225
            ret = powerd_update_display_request(&activity_timer_req);
 
226
        
 
227
        if (ret)
 
228
            powerd_warn("Error adding display state request for activity timer");
 
229
    }
 
230
 
 
231
    activity_timer_screen_state = new_state;
 
232
    return 0;
 
233
}
 
234
 
 
235
void update_screen_state(int new_state)
 
236
{
 
237
    if (new_state < SCREEN_STATE_OFF || new_state > SCREEN_STATE_BRIGHT)
 
238
        return;
 
239
    powerd_run_mainloop_sync(update_screen_state_worker, (gpointer)new_state);
 
240
}
 
241
 
176
242
}   //namespace
177
243
 
178
244
void powerd_reset_activity_timer(int add)
179
245
{
 
246
    int timeout;
 
247
 
180
248
    g_mutex_lock(&activity_timer_mutex);
181
249
 
182
250
    if (activity_timer > 0) {
185
253
    }
186
254
 
187
255
    if (add) {
188
 
        if (!powerd_display_enabled() &&
189
 
            powerd_add_display_request(&activity_timer_req))
190
 
            powerd_warn("Error adding display state request for activity timer");
191
 
        activity_timer = g_timeout_add_seconds(activity_timeout, activity_monitor, NULL);
 
256
        if (dim_timeout > 0 && dim_timeout < activity_timeout)
 
257
            timeout = dim_timeout;
 
258
        else
 
259
            timeout = activity_timeout;
 
260
 
 
261
        update_screen_state(SCREEN_STATE_BRIGHT);
 
262
        activity_timer = g_timeout_add_seconds(timeout, activity_monitor, NULL);
192
263
    } else {
193
 
        powerd_remove_display_request(activity_timer_req.cookie);
 
264
        update_screen_state(SCREEN_STATE_OFF);
194
265
    }
195
266
 
196
267
    g_mutex_unlock(&activity_timer_mutex);
299
370
static int
300
371
get_activity_timeout_from_gsettings()
301
372
{
302
 
    GSettings *settings = NULL;
303
 
    GVariant *val = NULL;
304
 
    settings = g_settings_new("com.canonical.powerd");
305
 
    val = g_settings_get_value(settings, "activity-timeout");
306
 
    return g_variant_get_uint32(val);
 
373
    int ret;
 
374
    GVariant *val = NULL;
 
375
 
 
376
    if (!powerd_settings)
 
377
        powerd_settings = g_settings_new("com.canonical.powerd");
 
378
 
 
379
    val = g_settings_get_value(powerd_settings, "activity-timeout");
 
380
    ret = g_variant_get_uint32(val);
 
381
    g_variant_unref(val);
 
382
    return ret;
 
383
}
 
384
 
 
385
static int
 
386
get_dim_timeout_from_gsettings()
 
387
{
 
388
    int ret;
 
389
    GVariant *val = NULL;
 
390
 
 
391
    if (!powerd_settings)
 
392
        powerd_settings = g_settings_new("com.canonical.powerd");
 
393
 
 
394
    val = g_settings_get_value(powerd_settings, "dim-timeout");
 
395
    ret = (int)g_variant_get_uint32(val);
 
396
    g_variant_unref(val);
 
397
    return ret;
307
398
}
308
399
 
309
400
/* Must be first to run on main loop */
369
460
        NULL);
370
461
 
371
462
    activity_timeout = get_activity_timeout_from_gsettings();
 
463
    dim_timeout = get_dim_timeout_from_gsettings();
372
464
    powerd_debug("Activity Timeout is %d seconds\n", activity_timeout);
 
465
    powerd_debug("Auto-dim Timeout is %d seconds\n", dim_timeout);
373
466
 
374
467
    libsuspend_init(0);
375
468
    power_request_init();