File: music_player.c

package info (click to toggle)
keytouch 2.3.2-2.1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 4,480 kB
  • ctags: 1,475
  • sloc: ansic: 9,591; sh: 4,008; makefile: 466; perl: 75
file content (363 lines) | stat: -rw-r--r-- 8,196 bytes parent folder | download | duplicates (2)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*---------------------------------------------------------------------------------
Name               : music_player.c
Author             : Marvin Raaijmakers
Description        : Plugin for keyTouch that controls various music players.
Date of last change: 26-Jan-2007

    Copyright (C) 2007 Marvin Raaijmakers

    This program 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 2
    of the License, or any later version.
    
    This program 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 this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

    You can contact me at: marvinr(at)users(dot)sf(dot)net
    (replace (at) by @ and (dot) by .)
-----------------------------------------------------------------------------------*/
#define _GNU_SOURCE
#include <stdlib.h>
#include <unistd.h>
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <ctype.h>
#include <string.h>

#include <plugin.h>

#define NUM_PLAYERS            5
#define MAX_STAT_FILENAME_SIZE 20
#define MAX_PROCESS_NAME_SIZE  20

void play_pause (KTPreferences *preferences);
void stop (KTPreferences *preferences);
void next (KTPreferences *preferences);
void previous (KTPreferences *preferences);

typedef enum {
	PCMD_PLAY_PAUSE,
	PCMD_STOP,
	PCMD_PREVIOUS,
	PCMD_NEXT
} PLAYER_CMD;


static void run (char *command);
static char str_only_digits (char *str);
static void do_player_command (PLAYER_CMD cmd);
static void control_rhythmbox (char *rhythmbox_command, char *rhythmbox_client_command);

static void ctrl_xmms (PLAYER_CMD cmd);
static void ctrl_amarok (PLAYER_CMD cmd);
static void ctrl_rhythmbox (PLAYER_CMD cmd);
static void ctrl_audacious (PLAYER_CMD cmd);
static void ctrl_mpd (PLAYER_CMD cmd);


typedef struct {
	const char *name; /* The process name of the player */
	void (*player_ctrl) (PLAYER_CMD); /* The function that controls the player */
} PLAYER;

const PLAYER player[NUM_PLAYERS] = {
	{"xmms", ctrl_xmms},
	{"amarokapp", ctrl_amarok},
	{"rhythmbox", ctrl_rhythmbox},
	{"audacious", ctrl_audacious},
	{"mpd", ctrl_mpd}
};


KeytouchPlugin plugin_struct = {
	{"Music player", "Marvin Raaijmakers", "GPL 2", "1.0",
	 "This plugin controls the following music players:\n"
	 "- XMMS\n"
	 "- Amarok\n"
	 "- Rhythmbox\n"
	 "- Audacious\n"
	 "- MPD"},
	"music_player.so",
	4,
	{{"Play/Pause", KTPluginFunctionType_Function, {.function = play_pause}},
	 {"Stop",       KTPluginFunctionType_Function, {.function = stop}},
	 {"Previous",   KTPluginFunctionType_Function, {.function = previous}},
	 {"Next",       KTPluginFunctionType_Function, {.function = next}},
	}
};


void
run (char *command)
{
	if (fork() == 0)
	{
		execlp ("sh", "sh", "-c", command, NULL);
	}
}



void
ctrl_xmms (PLAYER_CMD cmd)
{
	switch (cmd)
	{
		case PCMD_PLAY_PAUSE:
			run ("xmms --play-pause");
			break;
		case PCMD_STOP:
			run ("xmms --stop");
			break;
		case PCMD_PREVIOUS:
			run ("xmms --rew");
			break;
		case PCMD_NEXT:
			run ("xmms --fwd");
			break;
	}
}

void
ctrl_amarok (PLAYER_CMD cmd)
{
	switch (cmd)
	{
		case PCMD_PLAY_PAUSE:
			run ("amarok --play-pause");
			break;
		case PCMD_STOP:
			run ("amarok --stop");
			break;
		case PCMD_PREVIOUS:
			run ("amarok --previous");
			break;
		case PCMD_NEXT:
			run ("amarok --next");
			break;
	}
}


void
control_rhythmbox (	char	*rhythmbox_command,
			char	*rhythmbox_client_command )
/*
Description:
	This functions creates a client process that runs 'rhythmbox_client_command' when the
	program "rhythmbox-client" is installed and otherwise 'rhythmbox_command'.
*/
{
	static Boolean use_client;
	static Boolean initialized = FALSE;
	
	if (!initialized)
	{
		initialized = TRUE;
		use_client = (system( "which rhythmbox-client" ) == 0);
	}
	if (fork() == 0)
	{
		execlp ("sh", "sh", "-c", use_client ? rhythmbox_client_command : rhythmbox_command, NULL);
		exit (EXIT_SUCCESS);
	}
}

void
ctrl_rhythmbox (PLAYER_CMD cmd)
{
	switch (cmd)
	{
		case PCMD_PLAY_PAUSE:
			control_rhythmbox ("rhythmbox --play-pause", "rhythmbox-client --play-pause");
			break;
		case PCMD_STOP:
			control_rhythmbox ("rhythmbox --stop", "rhythmbox-client --stop");
			break;
		case PCMD_PREVIOUS:
			control_rhythmbox ("rhythmbox --previous", "rhythmbox-client --previous");
			break;
		case PCMD_NEXT:
			control_rhythmbox ("rhythmbox --next", "rhythmbox-client --next");
			break;
	}
}

void
ctrl_audacious (PLAYER_CMD cmd)
{
	switch (cmd)
	{
		case PCMD_PLAY_PAUSE:
			run ("audacious -t");
			break;
		case PCMD_STOP:
			run ("audacious --stop");
			break;
		case PCMD_PREVIOUS:
			run ("audacious --rew");
			break;
		case PCMD_NEXT:
			run ("audacious --fwd");
			break;
	}
}

void
ctrl_mpd (PLAYER_CMD cmd)
{
	switch (cmd)
	{
		case PCMD_PLAY_PAUSE:
			run ("mpc toggle");
			break;
		case PCMD_STOP:
			run ("mpc stop");
			break;
		case PCMD_PREVIOUS:
			run ("mpc prev");
			break;
		case PCMD_NEXT:
			run ("mpc next");
			break;
	}
}


char
str_only_digits (char *str)
{
	while (*str && isdigit(*str))
	{
		str++;
	}
	return *str == '\0';
}


void
do_player_command (PLAYER_CMD cmd)
/*
Input:
  cmd     - The command to be executed
Global:
  player  - Array containing the names of the supported players and a pointer to the
            function for controlling that player.
Description:
  For each music player in the 'player' array do_player_command() checks if that
  player is being runned by the current user. If it is then the 'player_ctrl'
  function of that player is being called with 'cmd' as its argument.
*/
{
	DIR *dp;
	struct dirent *ep;
	struct stat dir_stat;
	FILE *stat_file;
	int  uid,
	     i;
	char *working_dir,
	     stat_file_name[MAX_STAT_FILENAME_SIZE],
	     process_name[MAX_PROCESS_NAME_SIZE],
	     c;
	
	uid = getuid();
	working_dir = get_current_dir_name();
	if (working_dir == NULL)
	{
		/* There is not enough memory */
		return;
	}
	chdir ("/proc");
	dp = opendir (".");
	if (dp != NULL)
	{
		/* We are going the check every process that has a directory in /proc/
		 * and is being executed by the current user.
		 */
		while (ep = readdir (dp))
		{
			if (str_only_digits (ep->d_name))
			{
				stat (ep->d_name, &dir_stat);
				if (dir_stat.st_uid == uid) /* Does the user own this directory? */
				{
					snprintf (stat_file_name, MAX_STAT_FILENAME_SIZE, "%s/stat", ep->d_name);
					stat_file = fopen (stat_file_name, "r");
					if (stat_file)
					{
						/* Read from stat_file until we read a '(' or EOF */
						do {
							c = fgetc (stat_file);
						} while (c != EOF && c != '(');
						if (c != EOF)
						{
							/* Initialise process_name in case we don't read anything */
							process_name[0] = '\0';
							/* Read the process name from stat_file and put
							 * it in process_name */
							for (i = 0;
							     i < MAX_PROCESS_NAME_SIZE-1 &&
							      (c = fgetc (stat_file)) != ')' && c != EOF;
							     i++)
							{
								process_name[i] = c;
							}
							process_name[i] = '\0';
							
							/* OK we've got the name now check if it is a player: */
							for (i = 0; i < NUM_PLAYERS; i++)
							{
								if (strcmp (process_name, player[i].name) == 0)
								{
									/* Do the action */
									(*player[i].player_ctrl) (cmd);
									break;
								}
							}
						}
						fclose (stat_file);
					}
				}
			}
		}
		(void) closedir (dp);
	}
	chdir (working_dir);
	free (working_dir);
}


void
play_pause (KTPreferences *preferences)
{
	do_player_command (PCMD_PLAY_PAUSE);
}

void
stop (KTPreferences *preferences)
{
	do_player_command (PCMD_STOP);
}

void
next (KTPreferences *preferences)
{
	do_player_command (PCMD_NEXT);
}

void
previous (KTPreferences *preferences)
{
	do_player_command (PCMD_PREVIOUS);
}