File: external.c

package info (click to toggle)
ebview 0.3.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 4,040 kB
  • sloc: ansic: 35,577; sh: 10,714; xml: 1,771; makefile: 607; yacc: 291
file content (227 lines) | stat: -rw-r--r-- 5,189 bytes parent folder | download | duplicates (8)
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
/*  Copyright (C) 2001-2004  Kenichi Suto
 *
 *  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
 *  (at your option) 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "defs.h"
#include "global.h"
#include "eb.h"
#include <pthread.h>

#ifndef __WIN32__
#include <sys/wait.h>
#endif

#define USE_EXEC 1

gint launch_external(gchar *cmd, gboolean wait){

#ifdef __WIN32__
	PROCESS_INFORMATION pinfo;
	STARTUPINFO sinfo;
#else
	pid_t pid;
	gchar *words[128];
	gint status;
#endif

#ifdef __WIN32__
	ZeroMemory(&sinfo, sizeof(sinfo));
	sinfo.cb = sizeof(sinfo);

	LOG(LOG_DEBUG, "Lanuching external command : %s", cmd);
	if(!CreateProcess(NULL, cmd, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &sinfo, &pinfo)){
		LOG(LOG_CRITICAL, "Failed to execute command : %s", cmd);
		return(0);
	}
	CloseHandle(pinfo.hThread);
	if(wait == TRUE)
		WaitForSingleObject(pinfo.hProcess, INFINITE);
	CloseHandle(pinfo.hProcess);
	return(0);
#else
	switch(pid = fork()){
	case -1:
		LOG(LOG_CRITICAL, "fork : %s", strerror(errno));
		exit(1);
	case  0:
		LOG(LOG_DEBUG, "Lanuching external command : %s", cmd);

#ifdef USE_EXEC
		split_word(cmd, words);

		if(execvp(words[0], words) == -1){
		LOG(LOG_CRITICAL, "exec : %s", strerror(errno));
			free_words(words);
			_exit(100);
			break;
		}
		free_words(words);
#else
		system(cmd);
#endif
		_exit(0);
		
		break;
	default:
		if(wait == TRUE) {
			waitpid(pid, &status, 0);
			LOG(LOG_DEBUG, "Child exited with status %d\n", WEXITSTATUS(status));
			if(WIFEXITED(status) == 0){
				LOG(LOG_WARNING, "Child exited abnormally with status %d\n", WEXITSTATUS(status));
				return(WEXITSTATUS(status));
			}
		}
	}
	return(0);
#endif
}

#ifdef __WIN32__
static gchar *g_filename=NULL;
static pthread_t tid;

static void *play_background(void *arg)
{
	MCI_OPEN_PARMS open_parms;
	MCI_PLAY_PARMS play_parms;
	MCI_GENERIC_PARMS parms;

	open_parms.wDeviceID = 0;
	open_parms.lpstrDeviceType = "waveaudio";
	open_parms.lpstrElementName = g_filename;
	if(mciSendCommand(0,MCI_OPEN,MCI_WAIT|MCI_OPEN_TYPE|MCI_OPEN_ELEMENT,(DWORD)&open_parms) == 0){
		play_parms.dwFrom = 0;
		mciSendCommand(open_parms.wDeviceID,MCI_PLAY,MCI_WAIT|MCI_FROM,(DWORD)&play_parms);
		mciSendCommand(open_parms.wDeviceID,MCI_STOP,MCI_WAIT,(DWORD)&parms);
		mciSendCommand(open_parms.wDeviceID,MCI_CLOSE,MCI_WAIT,(DWORD)NULL);
	}
}
#endif

void play_multimedia(gchar *filename, gint type)
{
	gchar cmd[512];
	gchar *p;
	gchar *template;

	LOG(LOG_DEBUG, "IN : play_multimedia(%s, %d)", filename, type);

	switch(type){
	case TAG_TYPE_MOVIE:
		template = strdup(mpeg_template);
		break;
	case TAG_TYPE_SOUND:
#ifdef __WIN32__
		if(bplay_sound_internally){
			pthread_attr_t thread_attr;
			gint rc;

			if(g_filename)
				g_free(g_filename);
			g_filename = g_strdup(filename);

			pthread_attr_init (&thread_attr) ;
			pthread_attr_setstacksize (&thread_attr, 512*1024) ;

			LOG(LOG_DEBUG, "thread_create");
			rc = pthread_create(&tid, &thread_attr, play_background, (void *)NULL);
			if(rc != 0){
				LOG(LOG_CRITICAL, "pthread_create: %s", strerror(errno));
				LOG(LOG_DEBUG, "OUT : thread_search()");
				exit(1);
			}
			LOG(LOG_DEBUG, "thread_created");
			pthread_attr_destroy(&thread_attr);

			return;
		}
#endif
		template = strdup(wave_template);
		break;
	default:
		return;
		break;
	}

	if((template == NULL) || 
	   (strlen(template) == 0)){
#ifdef __WIN32__
		HINSTANCE hi;
		hi = ShellExecute(NULL, "open", filename, NULL, NULL, SW_SHOWNORMAL);
		if(hi <= 32){
			LOG(LOG_CRITICAL, "ShellExecute() failed : %d", hi);
		}
#else
		if(template)
			g_free(template);
#endif
		LOG(LOG_DEBUG, "OUT : play_multimedia()");
		return;
	}

	p = strstr(template, "%f");
	if(p != NULL){
		*p = '%';
		p++;
		*p = 's';
	}

	sprintf(cmd, template, filename);

	launch_external(cmd, FALSE);

	free(template);

	LOG(LOG_DEBUG, "OUT : play_multimedia()");
}

void launch_web_browser(gchar *url){
	gchar *p;
	gchar *template;
	gchar cmd[512];


	LOG(LOG_DEBUG, "IN : launch_web_browser()");

	if((browser_template == NULL) || 
	   (strlen(browser_template) == 0)){
#ifdef __WIN32__
		HINSTANCE hi;
		hi = ShellExecute(NULL, "open", url, NULL, NULL, SW_SHOWNORMAL);
		if(hi <= 32){
			LOG(LOG_CRITICAL, "ShellExecute() failed : %d", hi);
		}
#else
		LOG(LOG_CRITICAL, _("Web browser not set"));
#endif
		return;
	}

	template = strdup(browser_template);
	p = strstr(template, "%f");
	if(p != NULL){
	  *p = '%';
	  p++;
	  *p = 's';
	}
	sprintf(cmd, template, url);

	launch_external(cmd, FALSE);
	g_free(template);

	LOG(LOG_DEBUG, "OUT : launch_web_browser()");
}