1 /* $Id$ */
2 /* Copyright (c) 2006-2013 Pierre Pronchery <khorben@defora.org> */
3 /* This file is part of DeforaOS Desktop Player */
4 /* This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, version 3 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>. */
15 
16 
17 
18 #include <sys/wait.h>
19 #include <signal.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <locale.h>
24 #include <libintl.h>
25 #include "player.h"
26 #include "../config.h"
27 #define _(string) gettext(string)
28 
29 /* constants */
30 #ifndef PREFIX
31 # define PREFIX		"/usr/local"
32 #endif
33 #ifndef DATADIR
34 # define DATADIR	PREFIX "/share"
35 #endif
36 #ifndef LOCALEDIR
37 # define LOCALEDIR	DATADIR "/locale"
38 #endif
39 
40 
41 /* public */
42 /* variables */
43 Player * player;
44 
45 
46 /* private */
47 /* functions */
48 /* usage */
_usage(void)49 static int _usage(void)
50 {
51 	fputs(_("Usage: player [file...]\n"), stderr);
52 	return 1;
53 }
54 
55 
56 /* public */
57 /* functions */
58 /* main */
59 static void _main_signal(void);
60 
main(int argc,char * argv[])61 int main(int argc, char * argv[])
62 {
63 	int o;
64 	int i;
65 
66 	if(setlocale(LC_ALL, "") == NULL)
67 		player_error(NULL, "setlocale", 1);
68 	bindtextdomain(PACKAGE, LOCALEDIR);
69 	textdomain(PACKAGE);
70 	gtk_init(&argc, &argv);
71 	while((o = getopt(argc, argv, "")) != -1)
72 		switch(o)
73 		{
74 			default:
75 				return _usage();
76 		}
77 	_main_signal();
78 	if((player = player_new()) == NULL)
79 		return 2;
80 	if(optind < argc)
81 		player_open(player, argv[optind]);
82 	for(i = optind + 1; i < argc; i++)
83 		player_playlist_add(player, argv[i]);
84 	gtk_main();
85 	player_delete(player);
86 	return 0;
87 }
88 
89 static void _signal_handler(int signum);
90 
_main_signal(void)91 static void _main_signal(void)
92 	/* handle mplayer death; should be done in Player as a callback but
93 	 * would potentially conflict with other Player instances */
94 {
95 	struct sigaction sa;
96 
97 	memset(&sa, 0, sizeof(sa));
98 	sa.sa_handler = _signal_handler;
99 	sigfillset(&sa.sa_mask);
100 	if(sigaction(SIGCHLD, &sa, NULL) == -1)
101 		fputs("player: SIGCHLD: Not handled\n", stderr);
102 }
103 
_signal_handler(int signum)104 static void _signal_handler(int signum)
105 {
106 	pid_t pid;
107 	int status;
108 
109 	if(signum != SIGCHLD)
110 		return;
111 	if(player_sigchld(player) == 0)
112 		return;
113 	if((pid = waitpid(-1, &status, WNOHANG)) == -1)
114 	{
115 		player_error(NULL, "waitpid", 1);
116 		return;
117 	}
118 	if(pid == 0)
119 		return;
120 	fputs("player: ", stderr);
121 	if(WIFEXITED(status))
122 		fprintf(stderr, "%s%d%s%u\n", "child ", pid,
123 				": exited with code ", WEXITSTATUS(status));
124 	else
125 		fprintf(stderr, "%d%s", pid, ": Unknown state\n");
126 }
127