You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

itunes.dpr 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. {*
  2. * winamp.dpr -> Code for winamp.dll for DMDirc
  3. * DMDirc - Open Source IRC Client
  4. * Copyright (c) 2006-2017 DMDirc Developers
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *}
  24. // References:
  25. // http://www.informit.com/articles/article.aspx?p=130494&seqNum=3
  26. // http://developer.apple.com/sdk/itunescomsdk.html
  27. library winamp;
  28. uses
  29. windows, messages, SysUtils, Classes, ComObj, ActiveX;
  30. var
  31. oldExitProc: Pointer;
  32. function getITunes(): Variant;
  33. begin
  34. if FindWindow('iTunes', nil) <> 0 then begin
  35. Result := CreateOLEObject('iTunes.Application');
  36. end
  37. else Raise Exception.Create('iTunes is not running');
  38. end;
  39. // Freepascal sucks.
  40. // In Delphi I could just do V.Foo to call function Foo, but FPC hasn't added
  41. // this functionality yet, so yay for horrible callOLEFunction(V, 'Foo'); to
  42. // do the exact same thing!
  43. function callOLEFunction(V: IDispatch; S: WideString): Variant;
  44. const
  45. GUID_NULL: TGUID = '{00000000-0000-0000-0000-000000000000}';
  46. DEFAULT_PARAMS: TDispParams = ();
  47. var
  48. mres: HRESULT;
  49. invres: Variant;
  50. id: Integer;
  51. begin
  52. mres := V.GetIDsOfNames(GUID_NULL, @S, 1, locale_system_default, @id);
  53. olecheck(mres);
  54. mres := V.Invoke(id, GUID_NULL, locale_system_default, DISPATCH_PROPERTYGET, DEFAULT_PARAMS, @invres, nil, nil);
  55. olecheck(mres);
  56. Result := invres;
  57. end;
  58. function getPlayState(data: PChar):integer; stdcall;
  59. var
  60. V: Variant;
  61. B: array[0..255] of char;
  62. state: Integer;
  63. begin
  64. result := 1;
  65. try
  66. V := getITunes();
  67. state := callOLEFunction(V, 'PlayerState');
  68. if state = 0 then begin
  69. try
  70. callOLEFunction(V, 'PlayerPosition');
  71. B := 'Paused';
  72. except
  73. B := 'Stopped';
  74. end
  75. end
  76. else if state = 1 then B := 'Playing';
  77. result := 0;
  78. except
  79. on E : Exception do B:= 'Unable to find iTunes Application ('+E.ClassName+'/'+E.Message+')';
  80. end;
  81. StrCopy(data,B);
  82. end;
  83. function getArtist(data: PChar):integer; stdcall;
  84. var
  85. V: Variant;
  86. B: array[0..255] of char;
  87. begin
  88. result := 1;
  89. try
  90. V := getITunes();
  91. V := callOLEFunction(V, 'CurrentTrack');
  92. B := String(callOLEFunction(V, 'Artist'));
  93. result := 0;
  94. except
  95. on E : Exception do B:= 'Unable to find iTunes Application ('+E.ClassName+'/'+E.Message+')';
  96. end;
  97. StrCopy(data,B);
  98. end;
  99. function getTitle(data: PChar):integer; stdcall;
  100. var
  101. V: Variant;
  102. B: array[0..255] of char;
  103. kind: Integer;
  104. begin
  105. result := 1;
  106. try
  107. V := getITunes();
  108. V := callOLEFunction(V, 'CurrentTrack');
  109. kind := callOLEFunction(V, 'Kind');
  110. if kind = 3 then begin
  111. V := getITunes();
  112. B := String(callOLEFunction(V, 'CurrentStreamTitle'));
  113. end
  114. else begin
  115. B := String(callOLEFunction(V, 'Name'));
  116. end;
  117. result := 0;
  118. except
  119. on E : Exception do B:= 'Unable to find iTunes Application ('+E.ClassName+'/'+E.Message+')';
  120. end;
  121. StrCopy(data,B);
  122. end;
  123. function getAlbum(data: PChar):integer; stdcall;
  124. var
  125. V: Variant;
  126. B: array[0..255] of char;
  127. begin
  128. result := 1;
  129. try
  130. V := getITunes();
  131. V := callOLEFunction(V, 'CurrentTrack');
  132. B := String(callOLEFunction(V, 'Album'));
  133. result := 0;
  134. except
  135. on E : Exception do B:= 'Unable to find iTunes Application ('+E.ClassName+'/'+E.Message+')';
  136. end;
  137. StrCopy(data,B);
  138. end;
  139. function getLength(data: PChar):integer; stdcall;
  140. var
  141. V: Variant;
  142. B: array[0..255] of char;
  143. begin
  144. result := 1;
  145. try
  146. V := getITunes();
  147. V := callOLEFunction(V, 'CurrentTrack');
  148. B := String(callOLEFunction(V, 'Duration'));
  149. result := 0;
  150. except
  151. on E : Exception do B:= 'Unable to find iTunes Application ('+E.ClassName+'/'+E.Message+')';
  152. end;
  153. StrCopy(data,B);
  154. end;
  155. function getTime(data: PChar):integer; stdcall;
  156. var
  157. V: Variant;
  158. B: array[0..255] of char;
  159. begin
  160. result := 1;
  161. try
  162. V := getITunes();
  163. B := String(callOLEFunction(V, 'PlayerPosition'));
  164. result := 0;
  165. except
  166. on E : Exception do B:= 'Unable to find iTunes Application ('+E.ClassName+'/'+E.Message+')';
  167. end;
  168. StrCopy(data,B);
  169. end;
  170. function getFormat(data: PChar):integer; stdcall;
  171. var
  172. B: array[0..255] of char;
  173. begin
  174. Result := 0;
  175. B := 'Unknown';
  176. StrCopy(data,B);
  177. end;
  178. function getBitrate(data: PChar):integer; stdcall;
  179. var
  180. V: Variant;
  181. B: array[0..255] of char;
  182. begin
  183. result := 1;
  184. try
  185. V := getITunes();
  186. V := callOLEFunction(V, 'CurrentTrack');
  187. B := String(callOLEFunction(V, 'BitRate'));
  188. result := 0;
  189. except
  190. on E : Exception do B:= 'Unable to find iTunes Application ('+E.ClassName+'/'+E.Message+')';
  191. end;
  192. StrCopy(data,B);
  193. end;
  194. exports getPlayState, getArtist, getTitle, getAlbum, getLength, getTime, getFormat, getBitrate;
  195. procedure myExitProc;
  196. begin
  197. CoUnInitialize();
  198. ExitProc := oldExitProc;
  199. end;
  200. begin
  201. CoInitialize(nil);
  202. oldExitProc := ExitProc;
  203. ExitProc := @myExitProc;
  204. end.