aboutsummaryrefslogtreecommitdiff
path: root/src/mpris-bridge.c
blob: d85ec3dba7ad870c666f7ca592e3534d93f6b874 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* mpris-bridge.c generated by valac 0.9.7, the Vala compiler
 * generated from mpris-bridge.vala, do not modify */


#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>


#define TYPE_MPRIS_BRIDGE (mpris_bridge_get_type ())
#define MPRIS_BRIDGE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MPRIS_BRIDGE, MprisBridge))
#define MPRIS_BRIDGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MPRIS_BRIDGE, MprisBridgeClass))
#define IS_MPRIS_BRIDGE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MPRIS_BRIDGE))
#define IS_MPRIS_BRIDGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MPRIS_BRIDGE))
#define MPRIS_BRIDGE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MPRIS_BRIDGE, MprisBridgeClass))

typedef struct _MprisBridge MprisBridge;
typedef struct _MprisBridgeClass MprisBridgeClass;
typedef struct _MprisBridgePrivate MprisBridgePrivate;

#define TYPE_MPRIS_CONTROLLER (mpris_controller_get_type ())
#define MPRIS_CONTROLLER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MPRIS_CONTROLLER, MprisController))
#define MPRIS_CONTROLLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MPRIS_CONTROLLER, MprisControllerClass))
#define IS_MPRIS_CONTROLLER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MPRIS_CONTROLLER))
#define IS_MPRIS_CONTROLLER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MPRIS_CONTROLLER))
#define MPRIS_CONTROLLER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MPRIS_CONTROLLER, MprisControllerClass))

typedef struct _MprisController MprisController;
typedef struct _MprisControllerClass MprisControllerClass;

#define TYPE_MPRIS2_CONTROLLER (mpris2_controller_get_type ())
#define MPRIS2_CONTROLLER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MPRIS2_CONTROLLER, Mpris2Controller))
#define MPRIS2_CONTROLLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MPRIS2_CONTROLLER, Mpris2ControllerClass))
#define IS_MPRIS2_CONTROLLER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MPRIS2_CONTROLLER))
#define IS_MPRIS2_CONTROLLER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MPRIS2_CONTROLLER))
#define MPRIS2_CONTROLLER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MPRIS2_CONTROLLER, Mpris2ControllerClass))

typedef struct _Mpris2Controller Mpris2Controller;
typedef struct _Mpris2ControllerClass Mpris2ControllerClass;

#define MPRIS_BRIDGE_TYPE_MODE (mpris_bridge_mode_get_type ())
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define TYPE_PLAYER_CONTROLLER (player_controller_get_type ())
#define PLAYER_CONTROLLER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_PLAYER_CONTROLLER, PlayerController))
#define PLAYER_CONTROLLER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_PLAYER_CONTROLLER, PlayerControllerClass))
#define IS_PLAYER_CONTROLLER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_PLAYER_CONTROLLER))
#define IS_PLAYER_CONTROLLER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_PLAYER_CONTROLLER))
#define PLAYER_CONTROLLER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_PLAYER_CONTROLLER, PlayerControllerClass))

typedef struct _PlayerController PlayerController;
typedef struct _PlayerControllerClass PlayerControllerClass;

#define TRANSPORT_MENUITEM_TYPE_ACTION (transport_menuitem_action_get_type ())

struct _MprisBridge {
	GObject parent_instance;
	MprisBridgePrivate * priv;
};

struct _MprisBridgeClass {
	GObjectClass parent_class;
};

typedef enum  {
	MPRIS_BRIDGE_MODE_MPRIS_1,
	MPRIS_BRIDGE_MODE_MPRIS_2
} MprisBridgemode;

struct _MprisBridgePrivate {
	MprisController* mpris1_controller;
	Mpris2Controller* mpris2_controller;
	MprisBridgemode mode_in_use;
};

typedef enum  {
	TRANSPORT_MENUITEM_ACTION_PREVIOUS,
	TRANSPORT_MENUITEM_ACTION_PLAY_PAUSE,
	TRANSPORT_MENUITEM_ACTION_NEXT
} TransportMenuitemaction;


static gpointer mpris_bridge_parent_class = NULL;

GType mpris_bridge_get_type (void) G_GNUC_CONST;
GType mpris_controller_get_type (void) G_GNUC_CONST;
GType mpris2_controller_get_type (void) G_GNUC_CONST;
static GType mpris_bridge_mode_get_type (void) G_GNUC_UNUSED;
#define MPRIS_BRIDGE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_MPRIS_BRIDGE, MprisBridgePrivate))
enum  {
	MPRIS_BRIDGE_DUMMY_PROPERTY
};
GType player_controller_get_type (void) G_GNUC_CONST;
MprisBridge* mpris_bridge_new (PlayerController* ctrl);
MprisBridge* mpris_bridge_construct (GType object_type, PlayerController* ctrl);
Mpris2Controller* mpris2_controller_new (PlayerController* ctrl);
Mpris2Controller* mpris2_controller_construct (GType object_type, PlayerController* ctrl);
gboolean mpris2_controller_was_successfull (Mpris2Controller* self);
void mpris2_controller_initial_update (Mpris2Controller* self);
MprisController* mpris_controller_new (PlayerController* ctrl, const char* inter);
MprisController* mpris_controller_construct (GType object_type, PlayerController* ctrl, const char* inter);
gboolean mpris_bridge_connected (MprisBridge* self);
gboolean mpris_controller_connected (MprisController* self);
gboolean mpris2_controller_connected (Mpris2Controller* self);
GType transport_menuitem_action_get_type (void) G_GNUC_CONST;
void mpris_bridge_transport_update (MprisBridge* self, TransportMenuitemaction update);
void mpris_controller_transport_event (MprisController* self, TransportMenuitemaction command);
void mpris2_controller_transport_event (Mpris2Controller* self, TransportMenuitemaction command);
void mpris_bridge_expose (MprisBridge* self);
void mpris2_controller_expose (Mpris2Controller* self);
void mpris_bridge_set_track_position (MprisBridge* self, double pos);
void mpris_controller_set_position (MprisController* self, double position);
void mpris2_controller_set_position (Mpris2Controller* self, double position);
static void mpris_bridge_finalize (GObject* obj);



static GType mpris_bridge_mode_get_type (void) {
	static volatile gsize mpris_bridge_mode_type_id__volatile = 0;
	if (g_once_init_enter (&mpris_bridge_mode_type_id__volatile)) {
		static const GEnumValue values[] = {{MPRIS_BRIDGE_MODE_MPRIS_1, "MPRIS_BRIDGE_MODE_MPRIS_1", "mpris-1"}, {MPRIS_BRIDGE_MODE_MPRIS_2, "MPRIS_BRIDGE_MODE_MPRIS_2", "mpris-2"}, {0, NULL, NULL}};
		GType mpris_bridge_mode_type_id;
		mpris_bridge_mode_type_id = g_enum_register_static ("MprisBridgemode", values);
		g_once_init_leave (&mpris_bridge_mode_type_id__volatile, mpris_bridge_mode_type_id);
	}
	return mpris_bridge_mode_type_id__volatile;
}


MprisBridge* mpris_bridge_construct (GType object_type, PlayerController* ctrl) {
	MprisBridge * self;
	Mpris2Controller* _tmp0_;
	g_return_val_if_fail (ctrl != NULL, NULL);
	self = (MprisBridge*) g_object_new (object_type, NULL);
	self->priv->mpris2_controller = (_tmp0_ = mpris2_controller_new (ctrl), _g_object_unref0 (self->priv->mpris2_controller), _tmp0_);
	if (mpris2_controller_was_successfull (self->priv->mpris2_controller) == TRUE) {
		MprisController* _tmp1_;
		self->priv->mode_in_use = MPRIS_BRIDGE_MODE_MPRIS_2;
		self->priv->mpris1_controller = (_tmp1_ = NULL, _g_object_unref0 (self->priv->mpris1_controller), _tmp1_);
		mpris2_controller_initial_update (self->priv->mpris2_controller);
	} else {
		Mpris2Controller* _tmp2_;
		MprisController* _tmp3_;
		self->priv->mpris2_controller = (_tmp2_ = NULL, _g_object_unref0 (self->priv->mpris2_controller), _tmp2_);
		self->priv->mode_in_use = MPRIS_BRIDGE_MODE_MPRIS_1;
		self->priv->mpris1_controller = (_tmp3_ = mpris_controller_new (ctrl, "org.freedesktop.MediaPlayer"), _g_object_unref0 (self->priv->mpris1_controller), _tmp3_);
	}
	return self;
}


MprisBridge* mpris_bridge_new (PlayerController* ctrl) {
	return mpris_bridge_construct (TYPE_MPRIS_BRIDGE, ctrl);
}


gboolean mpris_bridge_connected (MprisBridge* self) {
	gboolean result = FALSE;
	g_return_val_if_fail (self != NULL, FALSE);
	if (self->priv->mode_in_use == MPRIS_BRIDGE_MODE_MPRIS_1) {
		result = mpris_controller_connected (self->priv->mpris1_controller);
		return result;
	} else {
		if (self->priv->mode_in_use == MPRIS_BRIDGE_MODE_MPRIS_2) {
			result = mpris2_controller_connected (self->priv->mpris2_controller);
			return result;
		}
	}
	result = FALSE;
	return result;
}


void mpris_bridge_transport_update (MprisBridge* self, TransportMenuitemaction update) {
	g_return_if_fail (self != NULL);
	if (self->priv->mode_in_use == MPRIS_BRIDGE_MODE_MPRIS_1) {
		mpris_controller_transport_event (self->priv->mpris1_controller, update);
	} else {
		if (self->priv->mode_in_use == MPRIS_BRIDGE_MODE_MPRIS_2) {
			mpris2_controller_transport_event (self->priv->mpris2_controller, update);
		}
	}
}


void mpris_bridge_expose (MprisBridge* self) {
	g_return_if_fail (self != NULL);
	if (self->priv->mode_in_use == MPRIS_BRIDGE_MODE_MPRIS_2) {
		mpris2_controller_expose (self->priv->mpris2_controller);
	} else {
		g_warning ("mpris-bridge.vala:56: MPRIS1 clients don't have the ability to raise/e" \
"xpose the client");
	}
}


void mpris_bridge_set_track_position (MprisBridge* self, double pos) {
	g_return_if_fail (self != NULL);
	if (self->priv->mode_in_use == MPRIS_BRIDGE_MODE_MPRIS_1) {
		mpris_controller_set_position (self->priv->mpris1_controller, pos);
	} else {
		if (self->priv->mode_in_use == MPRIS_BRIDGE_MODE_MPRIS_2) {
			mpris2_controller_set_position (self->priv->mpris2_controller, pos);
		}
	}
}


static void mpris_bridge_class_init (MprisBridgeClass * klass) {
	mpris_bridge_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (MprisBridgePrivate));
	G_OBJECT_CLASS (klass)->finalize = mpris_bridge_finalize;
}


static void mpris_bridge_instance_init (MprisBridge * self) {
	self->priv = MPRIS_BRIDGE_GET_PRIVATE (self);
}


static void mpris_bridge_finalize (GObject* obj) {
	MprisBridge * self;
	self = MPRIS_BRIDGE (obj);
	_g_object_unref0 (self->priv->mpris1_controller);
	_g_object_unref0 (self->priv->mpris2_controller);
	G_OBJECT_CLASS (mpris_bridge_parent_class)->finalize (obj);
}


GType mpris_bridge_get_type (void) {
	static volatile gsize mpris_bridge_type_id__volatile = 0;
	if (g_once_init_enter (&mpris_bridge_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (MprisBridgeClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) mpris_bridge_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (MprisBridge), 0, (GInstanceInitFunc) mpris_bridge_instance_init, NULL };
		GType mpris_bridge_type_id;
		mpris_bridge_type_id = g_type_register_static (G_TYPE_OBJECT, "MprisBridge", &g_define_type_info, 0);
		g_once_init_leave (&mpris_bridge_type_id__volatile, mpris_bridge_type_id);
	}
	return mpris_bridge_type_id__volatile;
}