Debian Codegraph @

plugins/wlx/WlxMplayer/src/wlxMplayer.lpr

raw
1 {
2 WlxMplayer
3 -------------------------------------------------------------------------
4 This is WLX (Lister) plugin for Double Commander.
5
6 Copyright (C) 2008 Dmitry Kolomiets ([email protected])
7 Class TExProcess used in plugin was written by Anton Rjeshevsky.
8 Gtk2 and Qt support were added by Koblov Alexander ([email protected])
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 }
24
25 library wlxMplayer;
26
27 {$mode objfpc}{$H+}
28 {$include calling.inc}
29
30 {$IF NOT (DEFINED(LCLGTK) or DEFINED(LCLGTK2) or DEFINED(LCLQT) or DEFINED(LCLQT5) or DEFINED(LCLQT6))}
31 {$DEFINE LCLGTK2}
32 {$ENDIF}
33
34 uses
35 {$IFDEF UNIX}
36 cthreads,
37 {$IFNDEF HEAPTRC}
38 cmem,
39 {$ENDIF}
40 {$ENDIF}
41 Classes,
42 sysutils,
43 x,
44 {$IFDEF LCLGTK} gtk, gdk, glib, {$ENDIF}
45 {$IFDEF LCLGTK2} gtk2, gdk2, glib2, gdk2x, {$ENDIF}
46 {$IFDEF LCLQT} qt4, {$ENDIF}
47 {$IFDEF LCLQT5} qt5, {$ENDIF}
48 {$IFDEF LCLQT6} qt6, {$ENDIF}
49 process,
50 math,
51 WLXPlugin;
52
53
54
55 type
56 { TExProcess }
57
58 TExProcess = class
59 protected
60 p: TProcess;
61 s: string;
62 function _GetExitStatus(): integer;
63 public
64 RezList:TStringList;
65 constructor Create(commandline: string);
66 procedure Execute;
67 destructor Destroy;
68 procedure OnReadLn(str: string);
69 property ExitStatus: integer read _GetExitStatus;
70 end;
71
72 const buf_len = 3000;
73
74
75 { TExProcess }
76
77 function TExProcess._GetExitStatus(): integer;
78 begin
79 Result:=p.ExitStatus;
80 end;
81
82 constructor TExProcess.Create(commandline: string);
83 begin
84 RezList:=TStringList.Create;
85 s:='';
86 p:=TProcess.Create(nil);
87 p.CommandLine:=commandline;
88 p.Options:=[poUsePipes,poNoConsole];
89 end;
90
91 procedure TExProcess.Execute;
92 var
93 buf: string;
94 i, j, c, n: integer;
95 begin
96 p.Execute;
97 repeat
98 SetLength(buf, buf_len);
99 SetLength(buf, p.output.Read(buf[1], length(buf))); //waits for the process output
100 // cut the incoming stream to lines:
101 s:=s + buf; //add to the accumulator
102 repeat //detect the line breaks and cut.
103 i:=Pos(#13, s);
104 j:=Pos(#10, s);
105 if i=0 then i:=j;
106 if j=0 then j:=i;
107 if j = 0 then Break; //there are no complete lines yet.
108 OnReadLn(Copy(s, 1, min(i, j) - 1)); //return the line without the CR/LF characters
109 s:=Copy(s, max(i, j) + 1, length(s) - max(i, j)); //remove the line from accumulator
110 until false;
111 until buf = '';
112 if s <> '' then OnReadLn(s);
113 end;
114
115 destructor TExProcess.Destroy;
116 begin
117 RezList.Free;
118 p.Free;
119 end;
120
121 procedure TExProcess.OnReadLn(str: string);
122 begin
123 RezList.Add(str);
124 end;
125
126
127 type
128
129 //Class implementing mplayer control
130 { TMPlayer }
131
132 TMPlayer=class(TThread)
133 public
134 //---------------------
135 hWidget:THandle; //the integrable widget
136 fileName:string; //filename
137 xid:TWindow; //X window handle
138 pr:TProcess; //mplayer's process
139 pmplayer:string; //path to mplayer
140 //---------------------
141 constructor Create(APlayerPath, AFilename: String);
142 destructor destroy; override;
143 procedure SetParentWidget(AWidget:thandle);
144 protected
145 procedure Execute; override;
146 private
147
148 end;
149
150 { TMPlayer }
151
152 constructor TMPlayer.Create(APlayerPath, AFilename: String);
153 begin
154 inherited Create(True);
155 filename:= '"' + AFilename + '"';
156 pmplayer:= APlayerPath + ' ';
157 WriteLn('wlxMPlayer: found mplayer in - ' + pmplayer);
158 end;
159
160 destructor TMPlayer.destroy;
161 begin
162 if pr.Running then
163 pr.Terminate(0);
164 pr.Free;
165 {$IF DEFINED(LCLQT) or DEFINED(LCLQT5) or DEFINED(LCLQT6)}
166 QWidget_Destroy(QWidgetH(hWidget));
167 {$ELSE}
168 gtk_widget_destroy(PGtkWidget(hWidget));
169 {$ENDIF}
170 inherited destroy;
171 end;
172
173 procedure TMPlayer.SetParentWidget(AWidget: THandle);
174 {$IF DEFINED(LCLQT) or DEFINED(LCLQT5) or DEFINED(LCLQT6)}
175 begin
176 hWidget:= THandle(QWidget_create(QWidgetH(AWidget)));
177 QWidget_show(QWidgetH(hWidget));
178 xid:= QWidget_winId(QWidgetH(hWidget));
179 end;
180 {$ELSE}
181 var
182 widget,
183 mySocket: PGtkWidget; //the socket
184 begin
185 widget := PGtkWidget(AWidget);
186 mySocket := gtk_socket_new;
187
188 gtk_container_add(GTK_CONTAINER(widget), mySocket);
189
190 gtk_widget_show(mySocket);
191 gtk_widget_show(widget);
192
193 gtk_widget_realize(mySocket);
194
195 {$IFDEF LCLGTK}
196 xid:= (PGdkWindowPrivate(mySocket^.window))^.xwindow;
197 {$ENDIF}
198 {$IFDEF LCLGTK2}
199 xid:= GDK_WINDOW_XID(mySocket^.window);
200 {$ENDIF}
201 hWidget:= THandle(mySocket);
202 end;
203 {$ENDIF}
204
205
206 procedure TMPlayer.Execute;
207 begin
208 pr:=TProcess.Create(nil);
209 pr.Options := Pr.Options + [poWaitOnExit,poNoConsole{,poUsePipes}]; //mplayer stops if poUsePipes used.
210 pr.CommandLine:=pmplayer+fileName+' -wid '+IntToStr(xid);
211 WriteLn(pr.CommandLine);
212 pr.Execute;
213 end;
214
215
216
217 //Custom class contains info for plugin windows
218 type
219
220 { TPlugInfo }
221
222 TPlugInfo = class
223 private
224 fControls:TStringList;
225 public
226 fFileToLoad:string;
227 fShowFlags:integer;
228 //etc
229 constructor Create;
230 destructor Destroy; override;
231 function AddControl(AItem: TMPlayer):integer;
232 end;
233
234 { TPlugInfo }
235
236 constructor TPlugInfo.Create;
237 begin
238 fControls:=TStringlist.Create;
239 end;
240
241 destructor TPlugInfo.Destroy;
242 begin
243 while fControls.Count>0 do
244 begin
245 TMPlayer(fControls.Objects[0]).Free;
246 fControls.Delete(0);
247 end;
248 inherited Destroy;
249 end;
250
251 function TPlugInfo.AddControl(AItem: TMPlayer): integer;
252 begin
253 fControls.AddObject(inttostr(PtrUInt(AItem)),TObject(AItem));
254 end;
255
256 {Plugin main part}
257
258 var List:TStringList;
259
260 function ListLoad(ParentWin: THandle; FileToLoad: PChar; ShowFlags: Integer): THandle; dcpcall;
261 var
262 pf: TExProcess;
263 sPlayerPath: String;
264 p: TMPlayer;
265 begin
266 pf:= TExProcess.Create('which mplayer');
267 try
268 pf.Execute;
269 if (pf.RezList.Count <> 0) then
270 sPlayerPath:= pf.RezList[0]
271 else
272 WriteLn('wlxMPlayer: mplayer not found!');
273 finally
274 pf.Free;
275 end;
276
277 if sPlayerPath = EmptyStr then Exit(wlxInvalidHandle);
278
279 p:= TMPlayer.Create(sPlayerPath, string(FileToLoad));
280 p.SetParentWidget(ParentWin);
281
282 // Create list if none
283 if not Assigned(List) then
284 List:= TStringList.Create;
285
286 // Add to list new plugin window and it's info
287 List.AddObject(IntToStr(PtrInt(p.hWidget)), TPlugInfo.Create);
288 with TPlugInfo(List.Objects[List.Count-1]) do
289 begin
290 fFileToLoad:= FileToLoad;
291 fShowFlags:= ShowFlags;
292 AddControl(p);
293 end;
294
295 Result:= p.hWidget;
296
297 p.Resume;
298 end;
299
300 procedure ListCloseWindow(ListWin:thandle); dcpcall;
301 var Index:integer; s:string;
302 begin
303
304 if assigned(List) then
305 begin
306 writeln('ListCloseWindow quit, List Item count: '+inttostr(List.Count));
307 s:=IntToStr(ListWin);
308 Index:=List.IndexOf(s);
309 if Index>-1 then
310 begin
311 TPlugInfo(List.Objects[index]).Free;
312 List.Delete(Index);
313 writeln('List item n: '+inttostr(Index)+' Deleted');
314 end;
315
316 //Free list if it has zero items
317 If List.Count=0 then FreeAndNil(List);
318 end;
319
320 end;
321
322 procedure ListGetDetectString(DetectString:pchar;maxlen:integer); dcpcall;
323 begin
324 StrLCopy(DetectString, '(EXT="AVI")|(EXT="MKV")|(EXT="FLV")|(EXT="MPG")|(EXT="MPEG")|(EXT="MP4")|(EXT="VOB")', maxlen);
325 end;
326
327 exports
328 ListLoad,
329 ListCloseWindow,
330 ListGetDetectString;
331
332 {$R *.res}
333
334 begin
335 end.
336