summaryrefslogtreecommitdiff
path: root/media-plugins/gkrellmpc/files/gkrellmpc-0.1_beta9-mt.patch
blob: 4de5688a2b5e0939a6784175e80fd212c26e6f38 (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
diff -uNr gkrellmpc-0.1_beta9/gkrellmpc.c gkrellmpc-0.1_beta9.mine/gkrellmpc.c
--- gkrellmpc-0.1_beta9/gkrellmpc.c	2005-01-05 22:33:16.000000000 +0300
+++ gkrellmpc-0.1_beta9.mine/gkrellmpc.c	2009-04-05 19:51:12.000000000 +0400
@@ -132,7 +132,7 @@
 	/* 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;
@@ -254,7 +254,7 @@
 	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();
 	}
 
@@ -428,7 +428,7 @@
 	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("");
 	}
diff -uNr gkrellmpc-0.1_beta9/mpd.c gkrellmpc-0.1_beta9.mine/mpd.c
--- gkrellmpc-0.1_beta9/mpd.c	2005-01-05 22:33:16.000000000 +0300
+++ gkrellmpc-0.1_beta9.mine/mpd.c	2009-04-05 20:30:38.000000000 +0400
@@ -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 @@
 	}
 
 	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 @@
 			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,40 @@
 		retval = FALSE;
 	}
 
-	if (retval) {
-		gkrellm_draw_decal_pixmap(mpc_panel, mpc_status_decal, D_MISC_LED1);
-		mpc_update_label("MPD");
-		mpc_update_songname("");
-	}
 
-	return(retval);
+err:	
+	pthread_mutex_unlock(&mpc_mutex);
+	return NULL;
+}
+
+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_unlock(&mpc_mutex);
 	return (TRUE);
 }
 
diff -uNr gkrellmpc-0.1_beta9/mpd.h gkrellmpc-0.1_beta9.mine/mpd.h
--- gkrellmpc-0.1_beta9/mpd.h	2005-01-05 22:33:16.000000000 +0300
+++ gkrellmpc-0.1_beta9.mine/mpd.h	2009-04-05 19:51:25.000000000 +0400
@@ -10,5 +10,6 @@
 gboolean     mpc_mpd_do(gchar *);
 GHashTable * mpc_mpd_get(gchar *);
 GPtrArray  * mpc_mpd_get_clumps(gchar *, gboolean);
+gboolean     mpc_mpd_connected();
 
 #endif