2018-11-09 16:30:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 Purism SPC
|
|
|
|
*
|
|
|
|
* This file is part of Calls.
|
|
|
|
*
|
|
|
|
* Calls is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Calls is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Calls. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Author: Bob Ham <bob.ham@puri.sm>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
*
|
|
|
|
*/
|
2021-06-18 08:55:47 +00:00
|
|
|
#define G_LOG_DOMAIN "CallsRinger"
|
2018-11-09 16:30:40 +00:00
|
|
|
|
|
|
|
#include "calls-ringer.h"
|
2020-03-18 11:53:13 +00:00
|
|
|
#include "calls-manager.h"
|
2018-11-09 16:30:40 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <glib-object.h>
|
|
|
|
|
2020-05-20 14:14:23 +00:00
|
|
|
#define LIBFEEDBACK_USE_UNSTABLE_API
|
|
|
|
#include <libfeedback.h>
|
|
|
|
|
2018-11-09 16:30:40 +00:00
|
|
|
|
2021-10-14 08:38:30 +00:00
|
|
|
struct _CallsRinger {
|
2020-03-18 11:51:49 +00:00
|
|
|
GObject parent_instance;
|
2018-11-09 16:30:40 +00:00
|
|
|
|
2021-10-14 09:41:18 +00:00
|
|
|
GList *calls;
|
2020-05-20 14:14:23 +00:00
|
|
|
LfbEvent *event;
|
2021-10-14 09:41:18 +00:00
|
|
|
gboolean playing;
|
2018-11-09 16:30:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
G_DEFINE_TYPE (CallsRinger, calls_ringer, G_TYPE_OBJECT);
|
|
|
|
|
|
|
|
static void
|
2020-05-20 14:14:23 +00:00
|
|
|
on_event_triggered (LfbEvent *event,
|
|
|
|
GAsyncResult *res,
|
|
|
|
CallsRinger *self)
|
2018-11-09 16:30:40 +00:00
|
|
|
{
|
2020-05-20 14:14:23 +00:00
|
|
|
g_autoptr (GError) err = NULL;
|
|
|
|
g_return_if_fail (LFB_IS_EVENT (event));
|
|
|
|
g_return_if_fail (CALLS_IS_RINGER (self));
|
|
|
|
|
|
|
|
if (lfb_event_trigger_feedback_finish (event, res, &err))
|
2021-10-14 08:38:30 +00:00
|
|
|
self->playing = TRUE;
|
2020-05-20 14:14:23 +00:00
|
|
|
else
|
2021-10-14 08:38:30 +00:00
|
|
|
g_warning ("Failed to trigger feedback for '%s': %s",
|
|
|
|
lfb_event_get_event (event), err->message);
|
|
|
|
|
2020-05-20 14:14:23 +00:00
|
|
|
g_object_unref (self);
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 08:47:54 +00:00
|
|
|
static void
|
2021-10-14 09:41:18 +00:00
|
|
|
start (CallsRinger *self,
|
|
|
|
gboolean quiet)
|
2018-11-09 16:30:40 +00:00
|
|
|
{
|
2020-05-20 14:14:23 +00:00
|
|
|
g_return_if_fail (self->playing == FALSE);
|
2018-11-09 16:30:40 +00:00
|
|
|
|
2021-10-14 08:38:30 +00:00
|
|
|
if (self->event) {
|
2021-10-14 09:41:18 +00:00
|
|
|
if (quiet)
|
2021-10-14 08:38:30 +00:00
|
|
|
lfb_event_set_feedback_profile (self->event, "quiet");
|
|
|
|
|
|
|
|
g_object_ref (self);
|
|
|
|
lfb_event_trigger_feedback_async (self->event,
|
|
|
|
NULL,
|
|
|
|
(GAsyncReadyCallback)on_event_triggered,
|
|
|
|
self);
|
|
|
|
}
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 09:41:18 +00:00
|
|
|
|
2018-11-09 16:30:40 +00:00
|
|
|
static void
|
2020-05-20 14:14:23 +00:00
|
|
|
on_event_feedback_ended (LfbEvent *event,
|
|
|
|
GAsyncResult *res,
|
|
|
|
CallsRinger *self)
|
2018-11-09 16:30:40 +00:00
|
|
|
{
|
2020-05-20 14:14:23 +00:00
|
|
|
g_autoptr (GError) err = NULL;
|
2021-10-14 08:38:30 +00:00
|
|
|
|
2020-05-20 14:14:23 +00:00
|
|
|
g_return_if_fail (LFB_IS_EVENT (event));
|
|
|
|
g_return_if_fail (CALLS_IS_RINGER (self));
|
|
|
|
|
|
|
|
if (lfb_event_end_feedback_finish (event, res, &err))
|
2021-10-14 08:38:30 +00:00
|
|
|
self->playing = FALSE;
|
2020-05-20 14:14:23 +00:00
|
|
|
else
|
2021-10-14 08:38:30 +00:00
|
|
|
g_warning ("Failed to end feedback for '%s': %s",
|
|
|
|
lfb_event_get_event (event), err->message);
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 09:41:18 +00:00
|
|
|
|
2018-11-09 16:30:40 +00:00
|
|
|
static void
|
2021-10-14 08:38:30 +00:00
|
|
|
on_feedback_ended (LfbEvent *event,
|
2020-05-20 14:14:23 +00:00
|
|
|
CallsRinger *self)
|
2018-11-09 16:30:40 +00:00
|
|
|
{
|
2020-05-20 14:14:23 +00:00
|
|
|
g_debug ("Feedback ended");
|
|
|
|
self->playing = FALSE;
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
2021-10-14 09:41:18 +00:00
|
|
|
|
2018-11-09 16:30:40 +00:00
|
|
|
static void
|
|
|
|
stop (CallsRinger *self)
|
|
|
|
{
|
|
|
|
g_debug ("Stopping ringtone");
|
2020-05-20 14:14:23 +00:00
|
|
|
lfb_event_end_feedback_async (self->event,
|
|
|
|
NULL,
|
|
|
|
(GAsyncReadyCallback)on_event_feedback_ended,
|
|
|
|
self);
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-14 09:41:18 +00:00
|
|
|
static inline gboolean
|
|
|
|
is_ring_state (CallsCallState state)
|
2018-11-09 16:30:40 +00:00
|
|
|
{
|
2021-10-14 09:41:18 +00:00
|
|
|
switch (state) {
|
|
|
|
case CALLS_CALL_STATE_INCOMING:
|
|
|
|
case CALLS_CALL_STATE_WAITING:
|
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
2021-10-14 08:38:30 +00:00
|
|
|
}
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline gboolean
|
2021-10-14 09:41:18 +00:00
|
|
|
is_active_state (CallsCallState state)
|
2018-11-09 16:30:40 +00:00
|
|
|
{
|
2021-10-14 08:38:30 +00:00
|
|
|
switch (state) {
|
2021-10-14 09:41:18 +00:00
|
|
|
case CALLS_CALL_STATE_ACTIVE:
|
|
|
|
case CALLS_CALL_STATE_DIALING:
|
|
|
|
case CALLS_CALL_STATE_ALERTING:
|
|
|
|
case CALLS_CALL_STATE_HELD:
|
2021-10-14 08:38:30 +00:00
|
|
|
return TRUE;
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-14 09:41:18 +00:00
|
|
|
static gboolean
|
|
|
|
has_active_call (CallsRinger *self)
|
2018-11-09 16:30:40 +00:00
|
|
|
{
|
2021-10-14 09:41:18 +00:00
|
|
|
g_assert (CALLS_IS_RINGER (self));
|
2018-11-09 16:30:40 +00:00
|
|
|
|
2021-10-14 09:41:18 +00:00
|
|
|
for (GList *node = self->calls; node != NULL; node = node->next) {
|
|
|
|
CallsCall *call = node->data;
|
2018-11-09 16:30:40 +00:00
|
|
|
|
2021-10-14 09:41:18 +00:00
|
|
|
if (is_active_state (calls_call_get_state (call)))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-11-09 16:30:40 +00:00
|
|
|
|
|
|
|
|
2021-10-14 09:41:18 +00:00
|
|
|
static gboolean
|
|
|
|
has_incoming_call (CallsRinger *self)
|
|
|
|
{
|
|
|
|
g_assert (CALLS_IS_RINGER (self));
|
|
|
|
|
|
|
|
for (GList *node = self->calls; node != NULL; node = node->next) {
|
|
|
|
CallsCall *call = node->data;
|
|
|
|
|
|
|
|
if (is_ring_state (calls_call_get_state (call)))
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
return FALSE;
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2021-10-14 09:41:18 +00:00
|
|
|
update_ring (CallsRinger *self)
|
2018-11-09 16:30:40 +00:00
|
|
|
{
|
2021-10-14 09:41:18 +00:00
|
|
|
g_assert (CALLS_IS_RINGER (self));
|
2021-03-12 17:21:41 +00:00
|
|
|
|
2021-10-14 09:41:18 +00:00
|
|
|
if (!self->event) {
|
|
|
|
g_debug ("Can't ring because libfeedback is not initialized");
|
|
|
|
return;
|
|
|
|
}
|
2018-11-09 16:30:40 +00:00
|
|
|
|
2021-10-14 09:41:18 +00:00
|
|
|
if (has_incoming_call (self))
|
|
|
|
start (self, has_active_call (self));
|
|
|
|
else
|
|
|
|
stop (self);
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2021-10-14 08:38:30 +00:00
|
|
|
call_added_cb (CallsRinger *self,
|
2021-10-14 09:41:18 +00:00
|
|
|
CallsCall *call)
|
2018-11-09 16:30:40 +00:00
|
|
|
{
|
2021-10-14 09:41:18 +00:00
|
|
|
g_assert (CALLS_IS_RINGER (self));
|
|
|
|
g_assert (CALLS_IS_CALL (call));
|
|
|
|
|
|
|
|
self->calls = g_list_append (self->calls, call);
|
2020-03-18 11:53:13 +00:00
|
|
|
|
|
|
|
g_signal_connect_swapped (call,
|
|
|
|
"state-changed",
|
2021-10-14 09:41:18 +00:00
|
|
|
G_CALLBACK (update_ring),
|
2020-03-18 11:53:13 +00:00
|
|
|
self);
|
2021-10-14 09:41:18 +00:00
|
|
|
update_ring (self);
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2021-10-14 08:38:30 +00:00
|
|
|
call_removed_cb (CallsRinger *self,
|
|
|
|
CallsCall *call)
|
2018-11-09 16:30:40 +00:00
|
|
|
{
|
2021-10-14 09:41:18 +00:00
|
|
|
self->calls = g_list_remove (self->calls, call);
|
2018-11-09 16:30:40 +00:00
|
|
|
|
2020-03-18 11:53:13 +00:00
|
|
|
g_signal_handlers_disconnect_by_data (call, self);
|
2021-10-14 09:41:18 +00:00
|
|
|
|
|
|
|
update_ring (self);
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
2020-03-18 11:53:13 +00:00
|
|
|
calls_ringer_init (CallsRinger *self)
|
2018-11-09 16:30:40 +00:00
|
|
|
{
|
2021-02-10 23:38:10 +00:00
|
|
|
g_autoptr (GError) err = NULL;
|
2020-05-20 14:14:23 +00:00
|
|
|
|
2021-10-14 08:38:30 +00:00
|
|
|
if (lfb_init (APP_ID, &err)) {
|
|
|
|
self->event = lfb_event_new ("phone-incoming-call");
|
|
|
|
/* Let feedbackd do the loop */
|
|
|
|
lfb_event_set_timeout (self->event, 0);
|
|
|
|
g_signal_connect (self->event,
|
|
|
|
"feedback-ended",
|
|
|
|
G_CALLBACK (on_feedback_ended),
|
|
|
|
self);
|
|
|
|
} else {
|
|
|
|
g_warning ("Failed to init libfeedback: %s", err->message);
|
|
|
|
}
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
constructed (GObject *object)
|
|
|
|
{
|
2020-03-18 11:53:13 +00:00
|
|
|
g_autoptr (GList) calls = NULL;
|
|
|
|
GList *c;
|
2018-11-09 16:30:40 +00:00
|
|
|
CallsRinger *self = CALLS_RINGER (object);
|
|
|
|
|
2020-03-18 11:53:13 +00:00
|
|
|
g_signal_connect_swapped (calls_manager_get_default (),
|
|
|
|
"call-add",
|
|
|
|
G_CALLBACK (call_added_cb),
|
|
|
|
self);
|
2018-11-09 16:30:40 +00:00
|
|
|
|
2020-03-18 11:53:13 +00:00
|
|
|
g_signal_connect_swapped (calls_manager_get_default (),
|
|
|
|
"call-remove",
|
|
|
|
G_CALLBACK (call_removed_cb),
|
|
|
|
self);
|
2018-11-09 16:30:40 +00:00
|
|
|
|
2020-03-18 11:53:13 +00:00
|
|
|
calls = calls_manager_get_calls (calls_manager_get_default ());
|
|
|
|
for (c = calls; c != NULL; c = c->next) {
|
|
|
|
call_added_cb (self, c->data);
|
|
|
|
}
|
2018-11-09 16:30:40 +00:00
|
|
|
|
2020-03-18 11:53:13 +00:00
|
|
|
G_OBJECT_CLASS (calls_ringer_parent_class)->constructed (object);
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-23 14:22:31 +00:00
|
|
|
static void
|
2020-05-20 14:14:23 +00:00
|
|
|
dispose (GObject *object)
|
2019-09-23 14:22:31 +00:00
|
|
|
{
|
|
|
|
CallsRinger *self = CALLS_RINGER (object);
|
|
|
|
|
2021-10-14 08:38:30 +00:00
|
|
|
if (self->event) {
|
|
|
|
g_clear_object (&self->event);
|
|
|
|
lfb_uninit ();
|
|
|
|
}
|
2019-09-23 14:22:31 +00:00
|
|
|
|
2020-05-20 14:14:23 +00:00
|
|
|
G_OBJECT_CLASS (calls_ringer_parent_class)->dispose (object);
|
2019-09-23 14:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-09 16:30:40 +00:00
|
|
|
static void
|
|
|
|
calls_ringer_class_init (CallsRingerClass *klass)
|
|
|
|
{
|
|
|
|
GObjectClass *object_class = G_OBJECT_CLASS (klass);
|
|
|
|
|
|
|
|
object_class->constructed = constructed;
|
2020-05-20 14:14:23 +00:00
|
|
|
object_class->dispose = dispose;
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CallsRinger *
|
2020-03-18 11:53:13 +00:00
|
|
|
calls_ringer_new (void)
|
2018-11-09 16:30:40 +00:00
|
|
|
{
|
2020-03-18 11:53:13 +00:00
|
|
|
return g_object_new (CALLS_TYPE_RINGER, NULL);
|
2018-11-09 16:30:40 +00:00
|
|
|
}
|