~ubuntu-branches/ubuntu/trusty/powerd/trusty

« back to all changes in this revision

Viewing changes to src/display-request.c

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release, Matthew Fischer, Ubuntu daily release
  • Date: 2013-07-20 02:01:50 UTC
  • mfrom: (1.1.12)
  • Revision ID: package-import@ubuntu.com-20130720020150-dkohn1ywvoinuwwh
Tags: 0.13+13.10.20130720-0ubuntu1
[ Matthew Fischer ]
* Simplify the API by dropping the special brightness setting and
  instead make it a flag.

[ Ubuntu daily release ]
* Automatic snapshot from revision 72

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
 
41
41
static struct {
42
42
    unsigned int state[POWERD_NUM_DISPLAY_STATES];
43
 
    unsigned int brightness[POWERD_NUM_DISPLAY_BRIGHTNESS_STATES];
44
43
    unsigned int flags[POWERD_NUM_DISPLAY_FLAGS];
45
44
} display_state_count;
46
45
 
47
46
static struct powerd_display_request internal_state = {
48
47
    .state = POWERD_DISPLAY_STATE_DONT_CARE,
49
 
    .brightness = POWERD_DISPLAY_BRIGHTNESS_DONT_CARE,
50
48
    .flags = 0,
51
49
};
52
50
 
65
63
    else
66
64
        new_state.state = POWERD_DISPLAY_STATE_DONT_CARE;
67
65
 
68
 
    new_state.brightness = POWERD_DISPLAY_BRIGHTNESS_DONT_CARE;
69
 
    for (i = POWERD_NUM_DISPLAY_BRIGHTNESS_STATES - 1; i > 0; i--) {
70
 
        if (display_state_count.brightness[i] > 0) {
71
 
            new_state.brightness = i;
72
 
            break;
73
 
        }
74
 
    }
75
 
 
76
66
    for (i = 0; i < POWERD_NUM_DISPLAY_FLAGS; i++) {
77
67
        if (display_state_count.flags[i] > 0)
78
68
            new_state.flags |= (1U << i);
83
73
        return;
84
74
    }
85
75
 
86
 
    powerd_debug("Internal state updated: state %d brightness %d flags 0x%08x",
87
 
                 new_state.state, new_state.brightness, new_state.flags);
 
76
    powerd_debug("Internal state updated: state %d flags 0x%08x",
 
77
                 new_state.state, new_state.flags);
88
78
    internal_state = new_state;
89
79
    powerd_set_display_state(&internal_state);
90
80
    powerd_display_state_signal_emit(&internal_state);
96
86
    int i;
97
87
 
98
88
    display_state_count.state[req->state]++;
99
 
    display_state_count.brightness[req->brightness]++;
100
89
 
101
90
    flags = req->flags;
102
91
    for (i = 0; flags && i < POWERD_NUM_DISPLAY_FLAGS; flags >>= 1, i++) {
113
102
    if (display_state_count.state[req->state] > 0)
114
103
        display_state_count.state[req->state]--;
115
104
 
116
 
    if (display_state_count.brightness[req->brightness] > 0)
117
 
        display_state_count.brightness[req->brightness]--;
118
 
 
119
105
    flags = req->flags;
120
106
    for (i = 0; flags && i < POWERD_NUM_DISPLAY_FLAGS; flags >>= 1, i++) {
121
107
        if ((flags & 1) && display_state_count.flags[i] > 0)
145
131
    __add_request(new_req);
146
132
 
147
133
    ireq->req.state = new_req->state;
148
 
    ireq->req.brightness = new_req->brightness;
149
134
    ireq->req.flags = new_req->flags;
150
135
 
151
136
    update_internal_state();
207
192
        return -EINVAL;
208
193
    }
209
194
 
210
 
    if ((unsigned)request->brightness >= POWERD_NUM_DISPLAY_BRIGHTNESS_STATES) {
211
 
        powerd_warn("Invalid display brightness requested: %d",
212
 
                    request->brightness);
213
 
        return -EINVAL;
214
 
    }
215
 
 
216
195
    /*
217
196
     * XXX: This will warn if we get up to 32 flags, but in that
218
197
     * case the check should just be removed.
276
255
 
277
256
gboolean handle_add_display_request(PowerdSource *obj,
278
257
                                    GDBusMethodInvocation *invocation,
279
 
                                    int state, int brightness, guint32 flags)
 
258
                                    int state, guint32 flags)
280
259
{
281
260
    struct powerd_display_request req;
282
261
    const char *owner;
286
265
    memset(&req, 0, sizeof(req));
287
266
 
288
267
    owner = g_dbus_method_invocation_get_sender(invocation);
289
 
    powerd_debug("%s from %s: state %d brightness %d flags %#08x",
290
 
                 __func__, owner, state, brightness, flags);
 
268
    powerd_debug("%s from %s: state %d flags %#08x",
 
269
                 __func__, owner, state, flags);
291
270
 
292
271
    req.state = state;
293
 
    req.brightness = brightness;
294
272
    req.flags = flags;
295
273
 
296
274
    ret = __powerd_add_display_request(&req, owner);
311
289
gboolean handle_update_display_request(PowerdSource *obj,
312
290
                                       GDBusMethodInvocation *invocation,
313
291
                                       const gchar *ext_cookie, int state,
314
 
                                       int brightness, guint32 flags)
 
292
                                       guint32 flags)
315
293
{
316
294
    struct powerd_display_request req;
317
295
    uuid_t cookie;
319
297
 
320
298
    memset(&req, 0, sizeof(req));
321
299
 
322
 
    powerd_debug("%s from %s: cookie: %s state %d brightness %d flags %#08x",
 
300
    powerd_debug("%s from %s: cookie: %s state %d flags %#08x",
323
301
                 __func__, g_dbus_method_invocation_get_sender(invocation),
324
 
                 ext_cookie, state, brightness, flags);
 
302
                 ext_cookie, state, flags);
325
303
 
326
304
    if (uuid_parse(ext_cookie, cookie)) {
327
305
        g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
333
311
 
334
312
    memcpy(req.cookie, cookie, sizeof(uuid_t));
335
313
    req.state = state;
336
 
    req.brightness = brightness;
337
314
    req.flags = flags;
338
315
 
339
316
    ret = powerd_update_display_request(&req);
387
364
    char cookie_str[UUID_STR_LEN];
388
365
 
389
366
    uuid_unparse(req->cookie, cookie_str);
390
 
    powerd_debug("  owner: %s, cookie: %s, state: %d, brightness: %d, flags: %#08x",
391
 
                 ireq->owner, cookie_str, req->state, req->brightness,
392
 
                 req->flags);
 
367
    powerd_debug("  owner: %s, cookie: %s, state: %d, flags: %#08x",
 
368
                 ireq->owner, cookie_str, req->state, req->flags);
393
369
 
394
 
    g_variant_builder_add(builder, "(siiu)", ireq->owner, req->state,
395
 
                          req->brightness, req->flags);
 
370
    g_variant_builder_add(builder, "(siu)", ireq->owner, req->state, req->flags);
396
371
}
397
372
 
398
373
static int build_display_request_list(GVariantBuilder *builder)
409
384
    GVariantBuilder *builder;
410
385
    int count;
411
386
 
412
 
    builder = g_variant_builder_new(G_VARIANT_TYPE("a(siiu)"));
 
387
    builder = g_variant_builder_new(G_VARIANT_TYPE("a(siu)"));
413
388
    count = build_display_request_list(builder);
414
389
    if (count > 0) {
415
390
        list = g_variant_builder_end(builder);
416
391
    } else {
417
392
        g_variant_builder_clear(builder);
418
 
        list = g_variant_new_array(G_VARIANT_TYPE("(siiu)"), NULL, 0);
 
393
        list = g_variant_new_array(G_VARIANT_TYPE("(siu)"), NULL, 0);
419
394
    }
420
395
 
421
396
    tuple = g_variant_new_tuple(&list, 1);