2 This file is part of PulseAudio.
4 Copyright 2006 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <pulse/xmalloc.h>
27 #include <pulse/timeval.h>
28 #include <pulse/rtclock.h>
30 #include <pulsecore/core.h>
31 #include <pulsecore/core-util.h>
32 #include <pulsecore/sink-input.h>
33 #include <pulsecore/source-output.h>
34 #include <pulsecore/modargs.h>
35 #include <pulsecore/log.h>
37 #include <pulsecore/namereg.h>
38 #include "module-suspend-on-idle-symdef.h"
39 //move to configure.ac
40 //#define USE_PM_LOCK /* Enable as default */
43 #include <sys/types.h>
44 #include <sys/socket.h>
49 #include <linux/limits.h>
51 #define SOCK_PATH "/tmp/pm_sock"
52 #define SHIFT_UNLOCK 4
53 #define SHIFT_UNLOCK_PARAMETER 12
54 #define SHIFT_CHANGE_STATE 8
55 #define SHIFT_HOLD_KEY_BLOCK 16
56 #define SHIFT_CHANGE_TIMEOUT 20
57 #define TIMEOUT_RESET_BIT 0x80
59 #define LCD_NORMAL 0x1 /**< NORMAL state */
60 #define LCD_DIM 0x2 /**< LCD dimming state */
61 #define LCD_OFF 0x4 /**< LCD off state */
62 #define SUSPEND 0x8 /**< Sleep state */
63 #define POWER_OFF 0x16 /**< Sleep state */
64 #define SETALL (LCD_DIM | LCD_OFF | LCD_NORMAL) /*< select all state - not supported yet */
66 /* parameters for pm_lock_state() */
67 #define STAY_CUR_STATE 0x0
68 #define GOTO_STATE_NOW 0x1
69 #define HOLD_KEY_BLOCK 0x2
71 /* paramters for pm_unlcok_state() - details are described at 162 line */
72 #define PM_SLEEP_MARGIN 0x0 /**< keep guard time for unlock */
73 #define PM_RESET_TIMER 0x1 /**< reset timer for unlock */
74 #define PM_KEEP_TIMER 0x2 /**< keep timer for unlock */
80 unsigned int timeout2;
83 #endif /* USE_PM_LOCK */
85 PA_MODULE_AUTHOR("Lennart Poettering");
86 PA_MODULE_DESCRIPTION("When a sink/source is idle for too long, suspend it");
87 PA_MODULE_VERSION(PACKAGE_VERSION);
88 PA_MODULE_LOAD_ONCE(true);
89 PA_MODULE_USAGE("timeout=<timeout>");
91 static const char* const valid_modargs[] = {
97 #define PM_TYPE_SINK 0x01
98 #define PM_TYPE_SOURCE 0x02
100 #define UPDATE_PM_LOCK(current,type) (current |= type)
101 #define UPDATE_PM_UNLOCK(current,type) (current &= ~type)
102 #endif /* USE_PM_LOCK */
106 pa_hashmap *device_infos;
112 *sink_state_changed_slot,
113 *source_state_changed_slot;
116 *sink_input_new_slot,
117 *source_output_new_slot,
118 *sink_input_unlink_slot,
119 *source_output_unlink_slot,
120 *sink_input_move_start_slot,
121 *source_output_move_start_slot,
122 *sink_input_move_finish_slot,
123 *source_output_move_finish_slot,
124 *sink_input_state_changed_slot,
125 *source_output_state_changed_slot;
128 #endif /* USE_PM_LOCK */
132 struct userdata *userdata;
136 pa_time_event *time_event;
141 static int send_msg(unsigned int s_bits, unsigned int timeout, unsigned int timeout2)
146 struct sockaddr_un remote;
151 p.timeout2 = timeout2;
153 sock = socket(AF_UNIX, SOCK_DGRAM, 0);
158 remote.sun_family = AF_UNIX;
159 if(strlen(SOCK_PATH) >= sizeof(remote.sun_path)) {
162 strncpy(remote.sun_path, SOCK_PATH, sizeof(remote.sun_path));
164 rc = sendto(sock, (void *)&p, sizeof(p), 0, (struct sockaddr *)&remote,
165 sizeof(struct sockaddr_un));
171 static int pm_lock_state(unsigned int s_bits, unsigned int flag,
172 unsigned int timeout)
182 if (flag & GOTO_STATE_NOW)
183 /* if the flag is true, go to the locking state directly */
184 s_bits = s_bits | (s_bits << SHIFT_CHANGE_STATE);
185 if (flag & HOLD_KEY_BLOCK)
186 s_bits = s_bits | (1 << SHIFT_HOLD_KEY_BLOCK);
188 return send_msg(s_bits, timeout, 0);
191 static int pm_unlock_state(unsigned int s_bits, unsigned int flag)
202 s_bits = (s_bits << SHIFT_UNLOCK);
203 s_bits = (s_bits | (flag << SHIFT_UNLOCK_PARAMETER));
204 return send_msg(s_bits, 0, 0);
209 static void timeout_cb(pa_mainloop_api*a, pa_time_event* e, const struct timeval *t, void *userdata) {
210 struct device_info *d = userdata;
218 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
220 if (d->sink && pa_sink_check_suspend(d->sink) <= 0 && !(d->sink->suspend_cause & PA_SUSPEND_IDLE)) {
221 pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name);
222 pa_sink_suspend(d->sink, true, PA_SUSPEND_IDLE);
223 pa_core_maybe_vacuum(d->userdata->core);
225 UPDATE_PM_UNLOCK(d->userdata->pm_state, PM_TYPE_SINK);
226 if(!(d->userdata->pm_state)) {
227 ret = pm_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
229 pa_log_info("sink pm_unlock_state success [%d]", ret);
231 pa_log_error("sink pm_unlock_state failed [%d]", ret);
233 #endif /* USE_PM_LOCK */
236 if (d->source && pa_source_check_suspend(d->source) <= 0 && !(d->source->suspend_cause & PA_SUSPEND_IDLE)) {
237 pa_log_info("Source %s idle for too long, suspending ...", d->source->name);
238 pa_source_suspend(d->source, true, PA_SUSPEND_IDLE);
239 pa_core_maybe_vacuum(d->userdata->core);
242 UPDATE_PM_UNLOCK(d->userdata->pm_state, PM_TYPE_SOURCE);
243 if(!(d->userdata->pm_state)) {
244 ret = pm_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
246 pa_log_info("source pm_unlock_state success [%d]", ret);
248 pa_log_error("source pm_unlock_state failed [%d]", ret);
250 #endif /* USE_PM_LOCK */
254 static void restart(struct device_info *d) {
258 pa_assert(d->sink || d->source);
260 d->last_use = now = pa_rtclock_now();
261 pa_core_rttime_restart(d->userdata->core, d->time_event, now + d->timeout);
264 pa_log_debug("Sink %s becomes idle, timeout in %" PRIu64 " seconds.", d->sink->name, d->timeout / PA_USEC_PER_SEC);
266 pa_log_debug("Source %s becomes idle, timeout in %" PRIu64 " seconds.", d->source->name, d->timeout / PA_USEC_PER_SEC);
269 static void resume(struct device_info *d) {
276 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
280 UPDATE_PM_LOCK(d->userdata->pm_state, PM_TYPE_SINK);
281 ret = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
283 pa_log_info("sink pm_lock_state success [%d]", ret);
285 pa_log_error("sink pm_lock_state failed [%d]", ret);
287 #endif /* USE_PM_LOCK */
288 pa_log_debug("Sink %s becomes busy.", d->sink->name);
289 pa_sink_suspend(d->sink, false, PA_SUSPEND_IDLE);
294 UPDATE_PM_LOCK(d->userdata->pm_state, PM_TYPE_SOURCE);
295 ret = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
297 pa_log_info("source pm_lock_state success [%d]", ret);
299 pa_log_error("source pm_lock_state failed [%d]", ret);
301 #endif /* USE_PM_LOCK */
302 pa_log_debug("Source %s becomes busy.", d->source->name);
303 pa_source_suspend(d->source, false, PA_SUSPEND_IDLE);
307 static pa_hook_result_t sink_input_fixate_hook_cb(pa_core *c, pa_sink_input_new_data *data, struct userdata *u) {
308 struct device_info *d;
314 /* We need to resume the audio device here even for
315 * PA_SINK_INPUT_START_CORKED, since we need the device parameters
316 * to be fully available while the stream is set up. */
318 if ((d = pa_hashmap_get(u->device_infos, data->sink)))
324 static pa_hook_result_t source_output_fixate_hook_cb(pa_core *c, pa_source_output_new_data *data, struct userdata *u) {
325 struct device_info *d;
331 if (data->source->monitor_of)
332 d = pa_hashmap_get(u->device_infos, data->source->monitor_of);
334 d = pa_hashmap_get(u->device_infos, data->source);
342 static pa_hook_result_t sink_input_unlink_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
344 pa_sink_input_assert_ref(s);
350 if (pa_sink_check_suspend(s->sink) <= 0) {
351 struct device_info *d;
352 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
359 static pa_hook_result_t source_output_unlink_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
360 struct device_info *d = NULL;
363 pa_source_output_assert_ref(s);
369 if (s->source->monitor_of) {
370 if (pa_sink_check_suspend(s->source->monitor_of) <= 0)
371 d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
373 if (pa_source_check_suspend(s->source) <= 0)
374 d = pa_hashmap_get(u->device_infos, s->source);
383 static pa_hook_result_t sink_input_move_start_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
384 struct device_info *d;
387 pa_sink_input_assert_ref(s);
390 if (pa_sink_check_suspend(s->sink) <= 1)
391 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
397 static pa_hook_result_t sink_input_move_finish_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
398 struct device_info *d;
399 pa_sink_input_state_t state;
402 pa_sink_input_assert_ref(s);
405 state = pa_sink_input_get_state(s);
406 if (state != PA_SINK_INPUT_RUNNING && state != PA_SINK_INPUT_DRAINED)
409 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
415 static pa_hook_result_t source_output_move_start_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
416 struct device_info *d = NULL;
419 pa_source_output_assert_ref(s);
422 if (s->source->monitor_of) {
423 if (pa_sink_check_suspend(s->source->monitor_of) <= 1)
424 d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
426 if (pa_source_check_suspend(s->source) <= 1)
427 d = pa_hashmap_get(u->device_infos, s->source);
436 static pa_hook_result_t source_output_move_finish_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
437 struct device_info *d;
440 pa_source_output_assert_ref(s);
443 if (pa_source_output_get_state(s) != PA_SOURCE_OUTPUT_RUNNING)
446 if (s->source->monitor_of)
447 d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
449 d = pa_hashmap_get(u->device_infos, s->source);
457 static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
458 struct device_info *d;
459 pa_sink_input_state_t state;
462 pa_sink_input_assert_ref(s);
465 state = pa_sink_input_get_state(s);
466 if (state == PA_SINK_INPUT_RUNNING || state == PA_SINK_INPUT_DRAINED)
467 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
473 static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
475 pa_source_output_assert_ref(s);
478 if (pa_source_output_get_state(s) == PA_SOURCE_OUTPUT_RUNNING) {
479 struct device_info *d;
481 if (s->source->monitor_of)
482 d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
484 d = pa_hashmap_get(u->device_infos, s->source);
493 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
494 struct device_info *d;
497 const char *timeout_str;
502 pa_object_assert_ref(o);
505 source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL;
506 sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL;
508 /* Never suspend monitors */
509 if (source && source->monitor_of)
512 pa_assert(source || sink);
514 timeout_str = pa_proplist_gets(sink ? sink->proplist : source->proplist, "module-suspend-on-idle.timeout");
515 if (timeout_str && pa_atoi(timeout_str, &timeout) >= 0)
516 timeout_valid = true;
518 timeout_valid = false;
520 if (timeout_valid && timeout < 0)
523 d = pa_xnew(struct device_info, 1);
525 d->source = source ? pa_source_ref(source) : NULL;
526 d->sink = sink ? pa_sink_ref(sink) : NULL;
527 d->time_event = pa_core_rttime_new(c, PA_USEC_INVALID, timeout_cb, d);
530 d->timeout = timeout * PA_USEC_PER_SEC;
532 d->timeout = d->userdata->timeout;
534 pa_hashmap_put(u->device_infos, o, d);
536 if ((d->sink && pa_sink_check_suspend(d->sink) <= 0) ||
537 (d->source && pa_source_check_suspend(d->source) <= 0))
543 static void device_info_free(struct device_info *d) {
547 pa_source_unref(d->source);
549 pa_sink_unref(d->sink);
551 d->userdata->core->mainloop->time_free(d->time_event);
556 static pa_hook_result_t device_unlink_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
557 struct device_info *d;
560 pa_object_assert_ref(o);
563 if ((d = pa_hashmap_remove(u->device_infos, o)))
569 static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
570 struct device_info *d;
573 pa_object_assert_ref(o);
576 if (!(d = pa_hashmap_get(u->device_infos, o)))
579 if (pa_sink_isinstance(o)) {
580 pa_sink *s = PA_SINK(o);
581 pa_sink_state_t state = pa_sink_get_state(s);
583 if (pa_sink_check_suspend(s) <= 0)
584 if (PA_SINK_IS_OPENED(state))
587 } else if (pa_source_isinstance(o)) {
588 pa_source *s = PA_SOURCE(o);
589 pa_source_state_t state = pa_source_get_state(s);
591 if (pa_source_check_suspend(s) <= 0)
592 if (PA_SOURCE_IS_OPENED(state))
599 int pa__init(pa_module*m) {
600 pa_modargs *ma = NULL;
602 uint32_t timeout = 5;
609 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
610 pa_log("Failed to parse module arguments.");
614 if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
615 pa_log("Failed to parse timeout value.");
619 m->userdata = u = pa_xnew(struct userdata, 1);
621 u->timeout = timeout * PA_USEC_PER_SEC;
622 u->device_infos = pa_hashmap_new_full(pa_idxset_trivial_hash_func, pa_idxset_trivial_compare_func, NULL, (pa_free_cb_t) device_info_free);
625 #endif /* USE_PM_LOCK */
627 PA_IDXSET_FOREACH(sink, m->core->sinks, idx)
628 device_new_hook_cb(m->core, PA_OBJECT(sink), u);
630 PA_IDXSET_FOREACH(source, m->core->sources, idx)
631 device_new_hook_cb(m->core, PA_OBJECT(source), u);
633 u->sink_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) device_new_hook_cb, u);
634 u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_NORMAL, (pa_hook_cb_t) device_new_hook_cb, u);
635 u->sink_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) device_unlink_hook_cb, u);
636 u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) device_unlink_hook_cb, u);
637 u->sink_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) device_state_changed_hook_cb, u);
638 u->source_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) device_state_changed_hook_cb, u);
640 u->sink_input_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_FIXATE], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_fixate_hook_cb, u);
641 u->source_output_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_FIXATE], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_fixate_hook_cb, u);
642 u->sink_input_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_unlink_hook_cb, u);
643 u->source_output_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_UNLINK_POST], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_unlink_hook_cb, u);
644 u->sink_input_move_start_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_START], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_move_start_hook_cb, u);
645 u->source_output_move_start_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_START], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_move_start_hook_cb, u);
646 u->sink_input_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_MOVE_FINISH], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_move_finish_hook_cb, u);
647 u->source_output_move_finish_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_MOVE_FINISH], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_move_finish_hook_cb, u);
648 u->sink_input_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_INPUT_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) sink_input_state_changed_hook_cb, u);
649 u->source_output_state_changed_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_OUTPUT_STATE_CHANGED], PA_HOOK_NORMAL, (pa_hook_cb_t) source_output_state_changed_hook_cb, u);
662 void pa__done(pa_module*m) {
672 if (u->sink_new_slot)
673 pa_hook_slot_free(u->sink_new_slot);
674 if (u->sink_unlink_slot)
675 pa_hook_slot_free(u->sink_unlink_slot);
676 if (u->sink_state_changed_slot)
677 pa_hook_slot_free(u->sink_state_changed_slot);
679 if (u->source_new_slot)
680 pa_hook_slot_free(u->source_new_slot);
681 if (u->source_unlink_slot)
682 pa_hook_slot_free(u->source_unlink_slot);
683 if (u->source_state_changed_slot)
684 pa_hook_slot_free(u->source_state_changed_slot);
686 if (u->sink_input_new_slot)
687 pa_hook_slot_free(u->sink_input_new_slot);
688 if (u->sink_input_unlink_slot)
689 pa_hook_slot_free(u->sink_input_unlink_slot);
690 if (u->sink_input_move_start_slot)
691 pa_hook_slot_free(u->sink_input_move_start_slot);
692 if (u->sink_input_move_finish_slot)
693 pa_hook_slot_free(u->sink_input_move_finish_slot);
694 if (u->sink_input_state_changed_slot)
695 pa_hook_slot_free(u->sink_input_state_changed_slot);
697 if (u->source_output_new_slot)
698 pa_hook_slot_free(u->source_output_new_slot);
699 if (u->source_output_unlink_slot)
700 pa_hook_slot_free(u->source_output_unlink_slot);
701 if (u->source_output_move_start_slot)
702 pa_hook_slot_free(u->source_output_move_start_slot);
703 if (u->source_output_move_finish_slot)
704 pa_hook_slot_free(u->source_output_move_finish_slot);
705 if (u->source_output_state_changed_slot)
706 pa_hook_slot_free(u->source_output_state_changed_slot);
708 pa_hashmap_free(u->device_infos);