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;
215 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
217 if (d->sink && pa_sink_check_suspend(d->sink) <= 0 && !(d->sink->suspend_cause & PA_SUSPEND_IDLE)) {
218 pa_log_info("Sink %s idle for too long, suspending ...", d->sink->name);
219 pa_sink_suspend(d->sink, true, PA_SUSPEND_IDLE);
220 pa_core_maybe_vacuum(d->userdata->core);
222 UPDATE_PM_UNLOCK(d->userdata->pm_state, PM_TYPE_SINK);
223 if(!(d->userdata->pm_state)) {
224 ret = pm_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
226 pa_log_info("sink pm_unlock_state success [%d]", ret);
228 pa_log_error("sink pm_unlock_state failed [%d]", ret);
230 #endif /* USE_PM_LOCK */
233 if (d->source && pa_source_check_suspend(d->source) <= 0 && !(d->source->suspend_cause & PA_SUSPEND_IDLE)) {
234 pa_log_info("Source %s idle for too long, suspending ...", d->source->name);
235 pa_source_suspend(d->source, true, PA_SUSPEND_IDLE);
236 pa_core_maybe_vacuum(d->userdata->core);
239 UPDATE_PM_UNLOCK(d->userdata->pm_state, PM_TYPE_SOURCE);
240 if(!(d->userdata->pm_state)) {
241 ret = pm_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
243 pa_log_info("source pm_unlock_state success [%d]", ret);
245 pa_log_error("source pm_unlock_state failed [%d]", ret);
247 #endif /* USE_PM_LOCK */
251 static void restart(struct device_info *d) {
255 pa_assert(d->sink || d->source);
257 d->last_use = now = pa_rtclock_now();
258 pa_core_rttime_restart(d->userdata->core, d->time_event, now + d->timeout);
261 pa_log_debug("Sink %s becomes idle, timeout in %" PRIu64 " seconds.", d->sink->name, d->timeout / PA_USEC_PER_SEC);
263 pa_log_debug("Source %s becomes idle, timeout in %" PRIu64 " seconds.", d->source->name, d->timeout / PA_USEC_PER_SEC);
266 static void resume(struct device_info *d) {
271 d->userdata->core->mainloop->time_restart(d->time_event, NULL);
275 UPDATE_PM_LOCK(d->userdata->pm_state, PM_TYPE_SINK);
276 ret = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
278 pa_log_info("sink pm_lock_state success [%d]", ret);
280 pa_log_error("sink pm_lock_state failed [%d]", ret);
282 #endif /* USE_PM_LOCK */
283 pa_log_debug("Sink %s becomes busy.", d->sink->name);
284 pa_sink_suspend(d->sink, false, PA_SUSPEND_IDLE);
289 UPDATE_PM_LOCK(d->userdata->pm_state, PM_TYPE_SOURCE);
290 ret = pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
292 pa_log_info("source pm_lock_state success [%d]", ret);
294 pa_log_error("source pm_lock_state failed [%d]", ret);
296 #endif /* USE_PM_LOCK */
297 pa_log_debug("Source %s becomes busy.", d->source->name);
298 pa_source_suspend(d->source, false, PA_SUSPEND_IDLE);
302 static pa_hook_result_t sink_input_fixate_hook_cb(pa_core *c, pa_sink_input_new_data *data, struct userdata *u) {
303 struct device_info *d;
309 /* We need to resume the audio device here even for
310 * PA_SINK_INPUT_START_CORKED, since we need the device parameters
311 * to be fully available while the stream is set up. */
313 if ((d = pa_hashmap_get(u->device_infos, data->sink)))
319 static pa_hook_result_t source_output_fixate_hook_cb(pa_core *c, pa_source_output_new_data *data, struct userdata *u) {
320 struct device_info *d;
326 if (data->source->monitor_of)
327 d = pa_hashmap_get(u->device_infos, data->source->monitor_of);
329 d = pa_hashmap_get(u->device_infos, data->source);
337 static pa_hook_result_t sink_input_unlink_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
339 pa_sink_input_assert_ref(s);
345 if (pa_sink_check_suspend(s->sink) <= 0) {
346 struct device_info *d;
347 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
354 static pa_hook_result_t source_output_unlink_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
355 struct device_info *d = NULL;
358 pa_source_output_assert_ref(s);
364 if (s->source->monitor_of) {
365 if (pa_sink_check_suspend(s->source->monitor_of) <= 0)
366 d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
368 if (pa_source_check_suspend(s->source) <= 0)
369 d = pa_hashmap_get(u->device_infos, s->source);
378 static pa_hook_result_t sink_input_move_start_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
379 struct device_info *d;
382 pa_sink_input_assert_ref(s);
385 if (pa_sink_check_suspend(s->sink) <= 1)
386 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
392 static pa_hook_result_t sink_input_move_finish_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
393 struct device_info *d;
394 pa_sink_input_state_t state;
397 pa_sink_input_assert_ref(s);
400 state = pa_sink_input_get_state(s);
401 if (state != PA_SINK_INPUT_RUNNING && state != PA_SINK_INPUT_DRAINED)
404 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
410 static pa_hook_result_t source_output_move_start_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
411 struct device_info *d = NULL;
414 pa_source_output_assert_ref(s);
417 if (s->source->monitor_of) {
418 if (pa_sink_check_suspend(s->source->monitor_of) <= 1)
419 d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
421 if (pa_source_check_suspend(s->source) <= 1)
422 d = pa_hashmap_get(u->device_infos, s->source);
431 static pa_hook_result_t source_output_move_finish_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
432 struct device_info *d;
435 pa_source_output_assert_ref(s);
438 if (pa_source_output_get_state(s) != PA_SOURCE_OUTPUT_RUNNING)
441 if (s->source->monitor_of)
442 d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
444 d = pa_hashmap_get(u->device_infos, s->source);
452 static pa_hook_result_t sink_input_state_changed_hook_cb(pa_core *c, pa_sink_input *s, struct userdata *u) {
453 struct device_info *d;
454 pa_sink_input_state_t state;
457 pa_sink_input_assert_ref(s);
460 state = pa_sink_input_get_state(s);
461 if (state == PA_SINK_INPUT_RUNNING || state == PA_SINK_INPUT_DRAINED)
462 if ((d = pa_hashmap_get(u->device_infos, s->sink)))
468 static pa_hook_result_t source_output_state_changed_hook_cb(pa_core *c, pa_source_output *s, struct userdata *u) {
470 pa_source_output_assert_ref(s);
473 if (pa_source_output_get_state(s) == PA_SOURCE_OUTPUT_RUNNING) {
474 struct device_info *d;
476 if (s->source->monitor_of)
477 d = pa_hashmap_get(u->device_infos, s->source->monitor_of);
479 d = pa_hashmap_get(u->device_infos, s->source);
488 static pa_hook_result_t device_new_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
489 struct device_info *d;
492 const char *timeout_str;
497 pa_object_assert_ref(o);
500 source = pa_source_isinstance(o) ? PA_SOURCE(o) : NULL;
501 sink = pa_sink_isinstance(o) ? PA_SINK(o) : NULL;
503 /* Never suspend monitors */
504 if (source && source->monitor_of)
507 pa_assert(source || sink);
509 timeout_str = pa_proplist_gets(sink ? sink->proplist : source->proplist, "module-suspend-on-idle.timeout");
510 if (timeout_str && pa_atoi(timeout_str, &timeout) >= 0)
511 timeout_valid = true;
513 timeout_valid = false;
515 if (timeout_valid && timeout < 0)
518 d = pa_xnew(struct device_info, 1);
520 d->source = source ? pa_source_ref(source) : NULL;
521 d->sink = sink ? pa_sink_ref(sink) : NULL;
522 d->time_event = pa_core_rttime_new(c, PA_USEC_INVALID, timeout_cb, d);
525 d->timeout = timeout * PA_USEC_PER_SEC;
527 d->timeout = d->userdata->timeout;
529 pa_hashmap_put(u->device_infos, o, d);
531 if ((d->sink && pa_sink_check_suspend(d->sink) <= 0) ||
532 (d->source && pa_source_check_suspend(d->source) <= 0))
538 static void device_info_free(struct device_info *d) {
542 pa_source_unref(d->source);
544 pa_sink_unref(d->sink);
546 d->userdata->core->mainloop->time_free(d->time_event);
551 static pa_hook_result_t device_unlink_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
552 struct device_info *d;
555 pa_object_assert_ref(o);
558 if ((d = pa_hashmap_remove(u->device_infos, o)))
564 static pa_hook_result_t device_state_changed_hook_cb(pa_core *c, pa_object *o, struct userdata *u) {
565 struct device_info *d;
568 pa_object_assert_ref(o);
571 if (!(d = pa_hashmap_get(u->device_infos, o)))
574 if (pa_sink_isinstance(o)) {
575 pa_sink *s = PA_SINK(o);
576 pa_sink_state_t state = pa_sink_get_state(s);
578 if (pa_sink_check_suspend(s) <= 0)
579 if (PA_SINK_IS_OPENED(state))
582 } else if (pa_source_isinstance(o)) {
583 pa_source *s = PA_SOURCE(o);
584 pa_source_state_t state = pa_source_get_state(s);
586 if (pa_source_check_suspend(s) <= 0)
587 if (PA_SOURCE_IS_OPENED(state))
594 int pa__init(pa_module*m) {
595 pa_modargs *ma = NULL;
597 uint32_t timeout = 5;
604 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
605 pa_log("Failed to parse module arguments.");
609 if (pa_modargs_get_value_u32(ma, "timeout", &timeout) < 0) {
610 pa_log("Failed to parse timeout value.");
614 m->userdata = u = pa_xnew(struct userdata, 1);
616 u->timeout = timeout * PA_USEC_PER_SEC;
617 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);
620 #endif /* USE_PM_LOCK */
622 PA_IDXSET_FOREACH(sink, m->core->sinks, idx)
623 device_new_hook_cb(m->core, PA_OBJECT(sink), u);
625 PA_IDXSET_FOREACH(source, m->core->sources, idx)
626 device_new_hook_cb(m->core, PA_OBJECT(source), u);
628 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);
629 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);
630 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);
631 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);
632 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);
633 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);
635 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);
636 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);
637 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);
638 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);
639 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);
640 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);
641 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);
642 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);
643 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);
644 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);
657 void pa__done(pa_module*m) {
667 if (u->sink_new_slot)
668 pa_hook_slot_free(u->sink_new_slot);
669 if (u->sink_unlink_slot)
670 pa_hook_slot_free(u->sink_unlink_slot);
671 if (u->sink_state_changed_slot)
672 pa_hook_slot_free(u->sink_state_changed_slot);
674 if (u->source_new_slot)
675 pa_hook_slot_free(u->source_new_slot);
676 if (u->source_unlink_slot)
677 pa_hook_slot_free(u->source_unlink_slot);
678 if (u->source_state_changed_slot)
679 pa_hook_slot_free(u->source_state_changed_slot);
681 if (u->sink_input_new_slot)
682 pa_hook_slot_free(u->sink_input_new_slot);
683 if (u->sink_input_unlink_slot)
684 pa_hook_slot_free(u->sink_input_unlink_slot);
685 if (u->sink_input_move_start_slot)
686 pa_hook_slot_free(u->sink_input_move_start_slot);
687 if (u->sink_input_move_finish_slot)
688 pa_hook_slot_free(u->sink_input_move_finish_slot);
689 if (u->sink_input_state_changed_slot)
690 pa_hook_slot_free(u->sink_input_state_changed_slot);
692 if (u->source_output_new_slot)
693 pa_hook_slot_free(u->source_output_new_slot);
694 if (u->source_output_unlink_slot)
695 pa_hook_slot_free(u->source_output_unlink_slot);
696 if (u->source_output_move_start_slot)
697 pa_hook_slot_free(u->source_output_move_start_slot);
698 if (u->source_output_move_finish_slot)
699 pa_hook_slot_free(u->source_output_move_finish_slot);
700 if (u->source_output_state_changed_slot)
701 pa_hook_slot_free(u->source_output_state_changed_slot);
703 pa_hashmap_free(u->device_infos);