1 /*
2  * Copyright (C) 2014 Michal Ratajsky <michal.ratajsky@gmail.com>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the licence, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <glib.h>
19 #include <glib-object.h>
20 
21 #include "matemixer-device.h"
22 #include "matemixer-enums.h"
23 #include "matemixer-enum-types.h"
24 #include "matemixer-switch.h"
25 #include "matemixer-device-switch.h"
26 
27 /**
28  * SECTION:matemixer-device-switch
29  * @include: libmatemixer/matemixer.h
30  */
31 
32 struct _MateMixerDeviceSwitchPrivate
33 {
34     MateMixerDevice          *device;
35     MateMixerDeviceSwitchRole role;
36 };
37 
38 enum {
39     PROP_0,
40     PROP_ROLE,
41     PROP_DEVICE,
42     N_PROPERTIES
43 };
44 
45 static GParamSpec *properties[N_PROPERTIES] = { NULL, };
46 
47 static void mate_mixer_device_switch_get_property (GObject                    *object,
48                                                    guint                       param_id,
49                                                    GValue                     *value,
50                                                    GParamSpec                 *pspec);
51 static void mate_mixer_device_switch_set_property (GObject                    *object,
52                                                    guint                       param_id,
53                                                    const GValue               *value,
54                                                    GParamSpec                 *pspec);
55 
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE(MateMixerDeviceSwitch,mate_mixer_device_switch,MATE_MIXER_TYPE_SWITCH)56 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (MateMixerDeviceSwitch, mate_mixer_device_switch, MATE_MIXER_TYPE_SWITCH)
57 
58 static void
59 mate_mixer_device_switch_class_init (MateMixerDeviceSwitchClass *klass)
60 {
61     GObjectClass *object_class;
62 
63     object_class = G_OBJECT_CLASS (klass);
64     object_class->get_property = mate_mixer_device_switch_get_property;
65     object_class->set_property = mate_mixer_device_switch_set_property;
66 
67     properties[PROP_ROLE] =
68         g_param_spec_enum ("role",
69                            "Role",
70                            "Role of the switch",
71                            MATE_MIXER_TYPE_DEVICE_SWITCH_ROLE,
72                            MATE_MIXER_DEVICE_SWITCH_ROLE_UNKNOWN,
73                            G_PARAM_READWRITE |
74                            G_PARAM_CONSTRUCT_ONLY |
75                            G_PARAM_STATIC_STRINGS);
76 
77     properties[PROP_DEVICE] =
78         g_param_spec_object ("device",
79                              "Device",
80                              "Device owning the switch",
81                              MATE_MIXER_TYPE_DEVICE,
82                              G_PARAM_READWRITE |
83                              G_PARAM_CONSTRUCT_ONLY |
84                              G_PARAM_STATIC_STRINGS);
85 
86     g_object_class_install_properties (object_class, N_PROPERTIES, properties);
87 }
88 
89 static void
mate_mixer_device_switch_get_property(GObject * object,guint param_id,GValue * value,GParamSpec * pspec)90 mate_mixer_device_switch_get_property (GObject    *object,
91                                        guint       param_id,
92                                        GValue     *value,
93                                        GParamSpec *pspec)
94 {
95     MateMixerDeviceSwitch *swtch;
96 
97     swtch = MATE_MIXER_DEVICE_SWITCH (object);
98 
99     switch (param_id) {
100     case PROP_ROLE:
101         g_value_set_enum (value, swtch->priv->role);
102         break;
103     case PROP_DEVICE:
104         g_value_set_object (value, swtch->priv->device);
105         break;
106     default:
107         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
108         break;
109     }
110 }
111 
112 static void
mate_mixer_device_switch_set_property(GObject * object,guint param_id,const GValue * value,GParamSpec * pspec)113 mate_mixer_device_switch_set_property (GObject      *object,
114                                        guint         param_id,
115                                        const GValue *value,
116                                        GParamSpec   *pspec)
117 {
118     MateMixerDeviceSwitch *swtch;
119 
120     swtch = MATE_MIXER_DEVICE_SWITCH (object);
121 
122     switch (param_id) {
123     case PROP_ROLE:
124         swtch->priv->role = g_value_get_enum (value);
125         break;
126     case PROP_DEVICE:
127         /* Construct-only object */
128         swtch->priv->device = g_value_get_object (value);
129 
130         if (swtch->priv->device != NULL)
131             g_object_add_weak_pointer (G_OBJECT (swtch->priv->device),
132                                        (gpointer *) &swtch->priv->device);
133         break;
134     default:
135         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
136         break;
137     }
138 }
139 
140 static void
mate_mixer_device_switch_init(MateMixerDeviceSwitch * swtch)141 mate_mixer_device_switch_init (MateMixerDeviceSwitch *swtch)
142 {
143     swtch->priv = mate_mixer_device_switch_get_instance_private (swtch);
144 }
145 
146 /**
147  * mate_mixer_device_switch_get_role:
148  * @swtch: a #MateMixerDeviceSwitch
149  *
150  * Gets the role of the switch. The role identifies the purpose of the switch.
151  *
152  * Returns: the switch role.
153  */
154 MateMixerDeviceSwitchRole
mate_mixer_device_switch_get_role(MateMixerDeviceSwitch * swtch)155 mate_mixer_device_switch_get_role (MateMixerDeviceSwitch *swtch)
156 {
157     g_return_val_if_fail (MATE_MIXER_IS_DEVICE_SWITCH (swtch), MATE_MIXER_DEVICE_SWITCH_ROLE_UNKNOWN);
158 
159     return swtch->priv->role;
160 }
161 
162 /**
163  * mate_mixer_device_switch_get_device:
164  * @swtch: a #MateMixerDeviceSwitch
165  */
166 MateMixerDevice *
mate_mixer_device_switch_get_device(MateMixerDeviceSwitch * swtch)167 mate_mixer_device_switch_get_device (MateMixerDeviceSwitch *swtch)
168 {
169     g_return_val_if_fail (MATE_MIXER_IS_DEVICE_SWITCH (swtch), NULL);
170 
171     return swtch->priv->device;
172 }
173