summaryrefslogtreecommitdiff
blob: 42a5848a44cb817e9bbeaca9f100aaf261d824d4 (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
diff --git a/gkrellmpc.c b/gkrellmpc.c
index eb28982..08a3fb3 100644
--- a/gkrellmpc.c
+++ b/gkrellmpc.c
@@ -140,7 +140,7 @@ void mpc_create_plugin (GtkWidget *vbox, gint first_create) {
 	/* Create the status decal */
 	mpc_status_decal = gkrellm_create_decal_pixmap(mpc_panel, gkrellm_decal_misc_pixmap(), gkrellm_decal_misc_mask(), N_MISC_DECALS, style, 0, t);
 	mpc_status_decal->x = w - mpc_status_decal->w;
-	gkrellm_draw_decal_pixmap(mpc_panel, mpc_status_decal, (mpc_mpd ? D_MISC_LED1 : D_MISC_LED0));
+	gkrellm_draw_decal_pixmap(mpc_panel, mpc_status_decal, (mpc_mpd_connected() ? D_MISC_LED1 : D_MISC_LED0));
 
 	/* Update t */
 	t += mpc_label_decal->h > mpc_status_decal->h ? mpc_label_decal->h : mpc_status_decal->h;
@@ -279,7 +279,7 @@ void mpc_update_plugin () {
 	static gint	x_scroll;
 
 	/* Try to connect to mpd */
-	if (!mpc_mpd && mpc_ticker->ten_second_tick) {
+	if (!mpc_mpd_connected() && mpc_ticker->ten_second_tick) {
 		mpc_mpd_connect();
 	}
 
@@ -457,7 +457,7 @@ void mpc_sync_with_mpd() {
 	status = mpc_mpd_get("status\n");
 	currentsong = mpc_mpd_get("currentsong\n");
 
-	if (!mpc_mpd) {
+	if (!mpc_mpd_connected()) {
 		mpc_update_label(_("NO MPD"));
 		mpc_update_songname("");
 		gtk_tooltips_set_tip(mpc_tooltip, mpc_panel->drawing_area, _("MPD is not running"), NULL);
diff --git a/mpd.c b/mpd.c
index 5918416..1d9ea6a 100644
--- a/mpd.c
+++ b/mpd.c
@@ -12,18 +12,32 @@
 #include <sys/socket.h>
 #include <netdb.h>
 
+#include <errno.h>
+#include <pthread.h>
+
 GIOChannel   * mpc_mpd = NULL;
+pthread_mutex_t mpc_mutex = { { 0, 0, 0, PTHREAD_MUTEX_RECURSIVE_NP, 0, { 0 } } }; //PTHREAD_MUTEX_INITIALIZER;
+
+gboolean mpc_mpd_connected() {
+	if(pthread_mutex_trylock(&mpc_mutex)){
+		return (FALSE);
+	}
+	pthread_mutex_unlock(&mpc_mutex);
+	return (gboolean)mpc_mpd;
+}
 
 /*
  * Connects to the MPD server, sets up the mpd object, sets the status decal to ON
  */
-gboolean mpc_mpd_connect() {
+void* mpc_mpd_connect_worker(void* arg) {
 	int sockfd;
 	struct hostent *server;
 	struct sockaddr_in serv_addr;
 	gchar * line;
 	gchar ** parts;
 	gboolean retval;
+	
+	pthread_mutex_lock(&mpc_mutex);
 
 	if (mpc_mpd) {
 		/*
@@ -33,11 +47,11 @@ gboolean mpc_mpd_connect() {
 	}
 
 	if (!mpc_conf_hostname || !mpc_conf_port) {
-		return (FALSE);
+		goto err;
 	}
 
-	if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) return(FALSE);
-	if (!(server = gethostbyname(mpc_conf_hostname))) return(FALSE);
+	if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1) goto err;
+	if (!(server = gethostbyname(mpc_conf_hostname))) goto err;
 
 	bzero((char *) &serv_addr, sizeof(serv_addr));
 	serv_addr.sin_family = AF_INET;
@@ -46,7 +60,7 @@ gboolean mpc_mpd_connect() {
 			server->h_length);
 	serv_addr.sin_port = htons(mpc_conf_port);
 
-	if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) return(FALSE);
+	if (connect(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)) < 0) goto err;
 	
 	/* Getup the mpd object */
 	mpc_mpd = g_io_channel_unix_new(sockfd);
@@ -72,29 +86,39 @@ gboolean mpc_mpd_connect() {
 		retval = FALSE;
 	}
 
-	if (retval) {
-		gkrellm_draw_decal_pixmap(mpc_panel, mpc_status_decal, D_MISC_LED1);
-		mpc_update_label(_("MPD"));
-		mpc_update_songname("");
-	}
+err:	
+	pthread_mutex_unlock(&mpc_mutex);
+	return NULL;
+}
 
-	return(retval);
+gboolean mpc_mpd_connect() {
+	pthread_attr_t attr;
+	pthread_t thread_id;
+	
+	if(pthread_mutex_trylock(&mpc_mutex)){
+		return (FALSE);
+	}
+	
+	pthread_attr_init(&attr);
+	pthread_create(&thread_id, &attr, mpc_mpd_connect_worker, NULL);
+	
+	pthread_mutex_unlock(&mpc_mutex);
+	
+	return (FALSE);
 }
 
 /*
  * Disconnects from MPD, destroys the mpd object, sets the status decal to off
  */
 gboolean mpc_mpd_disconnect() {
-
+	pthread_mutex_lock(&mpc_mutex);
 	if (mpc_mpd) {
 		g_io_channel_shutdown(mpc_mpd, FALSE, NULL);
 		free(mpc_mpd);
 		mpc_mpd = NULL;
 	}
 	
-	gkrellm_draw_decal_pixmap(mpc_panel, mpc_status_decal, D_MISC_LED0);
-	mpc_update_label(_("NO MPD"));
-	mpc_update_songname("");
+	pthread_mutex_lock(&mpc_mutex);
 	return (TRUE);
 }
 
diff --git a/mpd.h b/mpd.h
index efcb9f6..c6942c4 100644
--- a/mpd.h
+++ b/mpd.h
@@ -10,5 +10,6 @@ gboolean     mpc_mpd_disconnect();
 gboolean     mpc_mpd_do(gchar *);
 GHashTable * mpc_mpd_get(gchar *);
 GPtrArray  * mpc_mpd_get_clumps(gchar *, gboolean);
+gboolean     mpc_mpd_connected();
 
 #endif