-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.c
240 lines (169 loc) · 4 KB
/
control.c
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
#include "control.h"
#include "output.h"
#include "window.h"
#include <glib.h>
#include <string.h>
#include <signal.h>
static GString *filename = NULL;
static GPid pid;
static guint timerid;
static GIOChannel *input, *output, *error;
static gint volumelevel;
static State status = STOPPED;
static void Write_To_Input(char *string)
{
gsize written;
g_io_channel_write_chars(input, string, strlen(string), &written, NULL);
g_io_channel_flush(input, NULL);
}
static void Request_Time()
{
Write_To_Input("get_time_pos\n");
}
static void Request_Lenght()
{
Write_To_Input("get_time_length\n");
}
static void Request_Video_Codec()
{
Write_To_Input("get_video_codec\n");
}
static void Request_Size()
{
Write_To_Input("get_property width\n");
Write_To_Input("get_property height\n");
}
static gboolean Timer(gpointer data)
{
Request_Time();
return TRUE;
}
static void Create_Channels(gint inputfd, gint outputfd, gint errorfd)
{
input = g_io_channel_unix_new(inputfd);
output = g_io_channel_unix_new(outputfd);
error = g_io_channel_unix_new(errorfd);
g_io_channel_set_flags(input, G_IO_FLAG_NONBLOCK, NULL);
g_io_channel_set_encoding(output, NULL, NULL);
}
static void Shutdown_Channels()
{
g_io_channel_shutdown(input, TRUE, NULL);
g_io_channel_shutdown(output, TRUE, NULL);
g_io_channel_shutdown(error, TRUE, NULL);
}
static void Ended_Playing(GPid pid, gint ret, gpointer data)
{
if (status == PLAYING && ret == 0) // El fichero se reprodujo hasta el final
{
Output_Stop_Checking();
g_source_remove(timerid);
Shutdown_Channels();
status = STOPPED;
Window_Play_Next();
}
g_spawn_close_pid(pid);
Window_Change_To_List();
}
static void Refresh_Volume_Level()
{
GString *tmp = g_string_new(NULL);
g_string_printf(tmp, "volume %d 1\n", volumelevel * 10);
Write_To_Input(tmp->str);
g_string_free(tmp, TRUE);
}
void Control_Initialize()
{
filename = g_string_new(NULL);
}
void Control_Terminate()
{
g_string_free(filename, TRUE);
}
void Control_Load_File(gchar *pathname)
{
g_string_assign(filename, pathname);
}
void Control_Play()
{
gint inputfd, outputfd, errorfd;
GString *line = g_string_new(NULL);
g_string_printf(line, "/usr/bin/mplayer -slave -quiet -mixer-channel vol "
"-osdlevel 0 -wid %d", Window_Get_MPlayer_Window_ID());
gchar **argv = g_strsplit(line->str, " ", 0);
gint n = g_strv_length(argv);
gchar **tmp = g_memdup(argv, n * sizeof(gchar *));
tmp = g_realloc(tmp, (n + 2) * sizeof(gchar *));
tmp[n] = filename->str;
tmp[n + 1] = NULL;
g_spawn_async_with_pipes(NULL, tmp, NULL, G_SPAWN_LEAVE_DESCRIPTORS_OPEN |
G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, &inputfd,
&outputfd, &errorfd, NULL);
Create_Channels(inputfd, outputfd, errorfd);
Refresh_Volume_Level();
Output_Start_Checking(output);
Request_Size();
Request_Video_Codec();
Request_Lenght();
Request_Time();
timerid = g_timeout_add(REQUEST_TIME, Timer, NULL);
g_child_watch_add(pid, Ended_Playing, NULL);
status = PLAYING;
g_free(tmp);
g_strfreev(argv);
g_string_free(line, TRUE);
}
void Control_Pause(gboolean pause)
{
if (pause)
{
g_source_remove(timerid);
Write_To_Input("pause\n");
status = PAUSED;
}
else
{
Write_To_Input("pause\n");
Refresh_Volume_Level();
timerid = g_timeout_add(REQUEST_TIME, Timer, NULL);
status = PLAYING;
}
}
void Control_Stop()
{
Output_Stop_Checking();
g_source_remove(timerid);
kill(pid, SIGTERM);
Shutdown_Channels();
status = STOPPED;
}
State Control_Get_State()
{
return status;
}
void Control_Set_Volume_Level(gint level)
{
volumelevel = level;
if (status == PLAYING)
{
Refresh_Volume_Level();
}
}
void Control_Set_Position(gint position)
{
GString *tmp = g_string_new(NULL);
g_string_printf(tmp, "seek %d 2\n", position);
Write_To_Input(tmp->str);
if (status == PAUSED)
{
Refresh_Volume_Level();
timerid = g_timeout_add(REQUEST_TIME, Timer, NULL);
}
Request_Time();
status = PLAYING;
g_string_free(tmp, TRUE);
}
void Control_Set_Full_Screen()
{
Write_To_Input("vo_fullscreen 1\n");
}