/ob_mci.cpp

https://github.com/dadymax/BrainBay · C++ · 329 lines · 233 code · 60 blank · 36 comment · 42 complexity · 8217de94d5cfcf4cd0b459609ffb9a47 MD5 · raw file

  1. /* -----------------------------------------------------------------------------
  2. BrainBay Version 1.8, GPL 2003-2011, contact: chris@shifz.org
  3. MODULE: OB_MCI.CPP: definitions for the Multimedia-Player-Object
  4. Author: Chris Veigl
  5. This Object can open standard Multimedia files and play them in an extra window
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; See the
  9. GNU General Public License for more details.
  10. -----------------------------------------------------------------------------*/
  11. #include "brainBay.h"
  12. #include "ob_mci.h"
  13. LRESULT CALLBACK MCIDlgHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  14. {
  15. char szFileName[MAX_PATH];
  16. MCIOBJ * st;
  17. st = (MCIOBJ *) actobject;
  18. if ((st==NULL)||(st->type!=OB_MCIPLAYER)) return(FALSE);
  19. switch( message )
  20. {
  21. case WM_INITDIALOG:
  22. SetDlgItemText(hDlg, IDC_MCIFILE, st->mcifile);
  23. SCROLLINFO lpsi;
  24. lpsi.cbSize=sizeof(SCROLLINFO);
  25. lpsi.fMask=SIF_RANGE|SIF_POS;
  26. lpsi.nMin=4; lpsi.nMax=5000;
  27. SetScrollInfo(GetDlgItem(hDlg,IDC_SPEEDUPDATEBAR),SB_CTL,&lpsi, TRUE);
  28. SetScrollPos(GetDlgItem(hDlg,IDC_SPEEDUPDATEBAR), SB_CTL,st->upd_speed, TRUE);
  29. SetDlgItemInt(hDlg, IDC_UPDATESPEED, st->upd_speed, FALSE);
  30. SetDlgItemInt(hDlg, IDC_POS_CENTER, st->pos_center, FALSE);
  31. CheckDlgButton(hDlg, IDC_PLAY_ONCE, st->play_once);
  32. return TRUE;
  33. case WM_CLOSE:
  34. EndDialog(hDlg, LOWORD(wParam));
  35. break;
  36. case WM_COMMAND:
  37. switch (LOWORD(wParam))
  38. {
  39. case IDC_OPEN:
  40. if (!(strcmp(st->mcifile,"none")))
  41. {
  42. strcpy(szFileName,GLOBAL.resourcepath);
  43. strcat(szFileName,"MOVIES\\*.avi");
  44. } else strcpy (szFileName,st->mcifile);
  45. if (open_file_dlg(hDlg, szFileName, FT_AVI, OPEN_LOAD))
  46. {
  47. st->playing=FALSE;
  48. strcpy(st->mcifile,szFileName);
  49. SetDlgItemText(hDlg, IDC_MCIFILE, st->mcifile);
  50. }
  51. InvalidateRect(hDlg,NULL,FALSE);
  52. break;
  53. case IDC_PLAY_ONCE:
  54. st->play_once=IsDlgButtonChecked(hDlg,IDC_PLAY_ONCE);
  55. break;
  56. case IDC_LOAD:
  57. if (st->m_video) { MCIWndStop(st->m_video); MCIWndDestroy(st->m_video); }
  58. st->m_video = MCIWndCreate(ghWndMain, hInst,WS_VISIBLE|WS_THICKFRAME|MCIWNDF_NOERRORDLG,st->mcifile);
  59. if (!st->m_video) report_error ("Cannot open MCI File");
  60. else
  61. {
  62. RECT prc;
  63. MCIWndSetZoom(st->m_video,100);
  64. MCIWndGetSource(st->m_video,&prc);
  65. MCIWndSetTimeFormat(st->m_video ,"ms");
  66. MCIWndSetActiveTimer(st->m_video,500);
  67. if ((!strstr(st->mcifile,".mp3")) && (!strstr(st->mcifile,".wav")))
  68. SetWindowPos(st->m_video,HWND_TOPMOST,st->left,st->top,st->left+prc.right-prc.left,st->top+prc.bottom-prc.top,SWP_SHOWWINDOW);
  69. else ShowWindow(st->m_video,FALSE);
  70. }
  71. break;
  72. case IDC_MCIPLAY:
  73. if (st->m_video)
  74. {
  75. MCIWndSetSpeed(st->m_video,st->speed);
  76. MCIWndPlay(st->m_video);
  77. st->playing=TRUE;
  78. }
  79. break;
  80. case IDC_MCISTOP:
  81. if (st->m_video) { MCIWndStop(st->m_video);} // MCIWndDestroy(st->m_video); }
  82. st->playing=FALSE;
  83. break;
  84. case IDC_MCIPLUS:
  85. st->speed+=50;
  86. if (st->m_video) {
  87. MCIWndSetSpeed(st->m_video,st->speed); //MCIWndStep(st->m_video,2);
  88. MCIWndPlay(st->m_video); }
  89. break;
  90. case IDC_MCIMINUS:
  91. st->speed-=50;
  92. if (st->m_video) { MCIWndSetSpeed(st->m_video,st->speed); //MCIWndStep(st->m_video,2);
  93. MCIWndPlay(st->m_video);
  94. }
  95. break;
  96. case IDC_POS_CENTER:
  97. st->pos_center=GetDlgItemInt(hDlg,IDC_POS_CENTER,0,FALSE);
  98. break;
  99. }
  100. break;
  101. case WM_HSCROLL:
  102. {
  103. int nNewPos;
  104. nNewPos = get_scrollpos(wParam,lParam);
  105. if (lParam == (long) GetDlgItem(hDlg,IDC_SPEEDUPDATEBAR))
  106. {
  107. SetDlgItemInt(hDlg, IDC_UPDATESPEED, nNewPos, TRUE);
  108. st->upd_speed=nNewPos;
  109. }
  110. break;
  111. }
  112. case WM_SIZE:
  113. case WM_MOVE: update_toolbox_position(hDlg);
  114. break;
  115. }
  116. return FALSE;
  117. }
  118. //
  119. // Object Implementation
  120. //
  121. MCIOBJ::MCIOBJ(int num) : BASE_CL()
  122. {
  123. inports = 5;
  124. outports = 0;
  125. strcpy(in_ports[0].in_name,"play");
  126. strcpy(in_ports[1].in_name,"vol");
  127. strcpy(in_ports[2].in_name,"speed");
  128. strcpy(in_ports[3].in_name,"step");
  129. strcpy(in_ports[4].in_name,"position");
  130. width=75;
  131. left=250;right=500;top=100;bottom=200;
  132. input=0;m_video=0;speed=1000; volume=1000;
  133. play_once=FALSE;
  134. updatestep=0;step=0;
  135. volume=1000;
  136. upd_speed=200;
  137. pos_center=0;pos_change=0;
  138. prevtime=0;
  139. play=INVALID_VALUE; playing=FALSE; resetted=FALSE;
  140. strcpy(mcifile,"none");
  141. //displayWnd=create_AVI_Window(left,right,top,bottom,&GLRC);
  142. }
  143. void MCIOBJ::session_stop(void)
  144. {
  145. if (m_video) MCIWndStop(m_video);
  146. playing=FALSE;
  147. }
  148. void MCIOBJ::make_dialog(void)
  149. {
  150. display_toolbox(hDlg=CreateDialog(hInst, (LPCTSTR)IDD_MCIPROPBOX, ghWndStatusbox, (DLGPROC)MCIDlgHandler));
  151. }
  152. void MCIOBJ::load(HANDLE hFile)
  153. {
  154. load_object_basics(this);
  155. load_property("mci-file",P_STRING,mcifile);
  156. load_property("wnd-top",P_INT,&top);
  157. load_property("wnd-bottom",P_INT,&bottom);
  158. load_property("wnd-left",P_INT,&left);
  159. load_property("wnd-right",P_INT,&right);
  160. load_property("upd_speed",P_INT,&upd_speed);
  161. load_property("pos_center",P_INT,&pos_center);
  162. load_property("play_once",P_INT,&play_once);
  163. if (strcmp(mcifile,"none"))
  164. {
  165. char szFileName[MAX_PATH] = "";
  166. strcpy(szFileName,mcifile);
  167. // m_video = MCIWndCreate(ghWndMain, hInst,WS_VISIBLE|WS_THICKFRAME|MCIWNDF_NOMENU|MCIWNDF_NOPLAYBAR|MCIWNDF_NOERRORDLG,mcifile);
  168. m_video = MCIWndCreate(ghWndMain, hInst,WS_VISIBLE|WS_THICKFRAME|MCIWNDF_NOERRORDLG,mcifile);
  169. if (!m_video) report_error ("Cannot open MCI File");
  170. else
  171. {
  172. MCIWndSetTimeFormat(m_video ,"ms");
  173. MCIWndSetActiveTimer(m_video,500);
  174. if ((!strstr(mcifile,".mp3")) && (!strstr(mcifile,".wav"))) SetWindowPos(m_video,HWND_TOPMOST,left,top,right-left,bottom-top,SWP_SHOWWINDOW);
  175. else ShowWindow(m_video,FALSE);
  176. }
  177. }
  178. }
  179. void MCIOBJ::save(HANDLE hFile)
  180. {
  181. WINDOWPLACEMENT wndpl;
  182. if (m_video)
  183. {
  184. GetWindowPlacement(m_video, &wndpl);
  185. left=wndpl.rcNormalPosition.left;
  186. right=wndpl.rcNormalPosition.right;
  187. top=wndpl.rcNormalPosition.top;
  188. bottom=wndpl.rcNormalPosition.bottom;
  189. }
  190. save_object_basics(hFile,this);
  191. save_property(hFile,"mci-file",P_STRING,mcifile);
  192. save_property(hFile,"wnd-top",P_INT,&top);
  193. save_property(hFile,"wnd-bottom",P_INT,&bottom);
  194. save_property(hFile,"wnd-left",P_INT,&left);
  195. save_property(hFile,"wnd-right",P_INT,&right);
  196. save_property(hFile,"upd_speed",P_INT,&upd_speed);
  197. save_property(hFile,"pos_center",P_INT,&pos_center);
  198. save_property(hFile,"play_once",P_INT,&play_once);
  199. }
  200. void MCIOBJ::incoming_data(int port, float value)
  201. {
  202. switch (port)
  203. {
  204. case 0: play=(int)value; break;
  205. case 1: volume=(int)value;break;
  206. case 2: speed=(int)value; break;
  207. case 3: step=(int)value; break;
  208. case 4: pos_change=(int)value; break;
  209. }
  210. }
  211. void MCIOBJ::work(void)
  212. {
  213. // InvalidateRect(displayWnd,NULL,FALSE);
  214. if (GLOBAL.fly) return;
  215. if (!m_video) return;
  216. if (MCIWndGetPosition(m_video)==MCIWndGetLength(m_video)) playing=FALSE;
  217. if ((play!=INVALID_VALUE) && (m_video) && (!playing))
  218. {
  219. if ((!play_once)||(resetted)) { MCIWndPlay(m_video); playing = TRUE; }
  220. resetted=FALSE;
  221. }
  222. if (play==INVALID_VALUE)
  223. {
  224. resetted=TRUE;
  225. if ((m_video) && (playing))
  226. { MCIWndStop(m_video); playing=FALSE; }
  227. }
  228. acttime=TIMING.acttime-prevtime;
  229. if (((float)acttime/(float)TIMING.pcfreq*1000)>(float)upd_speed)
  230. {
  231. prevtime=TIMING.acttime;
  232. if (actspeed!=speed) { actspeed=speed; MCIWndSetSpeed(m_video, speed); }
  233. if (actvolume!=volume) { actvolume=volume; MCIWndSetVolume(m_video, volume); }
  234. if (step) { MCIWndStep(m_video,step); step=0;}
  235. if (pos_change) {MCIWndSeek(m_video,pos_center+pos_change); pos_change=0;}
  236. if (MCIWndGetPosition(m_video)==MCIWndGetLength(m_video))
  237. {
  238. //MCIWndPlay(m_video);
  239. MCIWndSeek(m_video,1);
  240. }
  241. }
  242. }
  243. MCIOBJ::~MCIOBJ()
  244. {
  245. if (m_video) {
  246. MCIWndStop(m_video);
  247. MCIWndClose(m_video);
  248. Sleep(1000);
  249. MCIWndDestroy(m_video); }
  250. }
  251. // MCIWndHome(st->m_video);
  252. // MCIWndResume(st->m_video);
  253. // MCIWndPause(st->m_video);
  254. // MCIWndStop(st->m_video);
  255. // MCIWndDestroy(st->m_video);
  256. // MCIWndSetVolume(st->m_video, m_Volume);
  257. // MCIWndSeek(st->m_video,SeekPos);
  258. // MCIWndPlay(st->m_video);
  259. // MCIWndSetZoom(st->m_video,150);
  260. // Length=MCIWndGetLength(st->m_video);
  261. // SetWindowPos(hWnd,HWND_TOPMOST,re.bottom,re.left,re.right,re.top,SWP_SHOWWINDOW);
  262. // SeekPos=MCIWndGetPosition(st->m_video);
  263. // if(SeekPos==Length)
  264. // MCIWndSetSpeed(st->m_video,1000);