avs_eelif.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. LICENSE
  3. -------
  4. Copyright 2005 Nullsoft, Inc.
  5. All rights reserved.
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. * Redistributions of source code must retain the above copyright notice,
  9. this list of conditions and the following disclaimer.
  10. * Redistributions in binary form must reproduce the above copyright notice,
  11. this list of conditions and the following disclaimer in the documentation
  12. and/or other materials provided with the distribution.
  13. * Neither the name of Nullsoft nor the names of its contributors may be used to
  14. endorse or promote products derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  18. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  21. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  22. OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include <windows.h>
  25. #include "../ns-eel2/ns-eel-int.h"
  26. #include "../ns-eel2/ns-eel-addfuncs.h"
  27. #include "avs_eelif.h"
  28. char last_error_string[1024];
  29. int g_log_errors;
  30. CRITICAL_SECTION g_eval_cs;
  31. static char *g_evallib_visdata;
  32. /////////////////////// begin AVS specific script functions
  33. static double NSEEL_CGEN_CALL getvis(unsigned char *visdata, int bc, int bw, int ch, int xorv)
  34. {
  35. int x;
  36. int accum=0;
  37. if (ch && ch != 1 && ch != 2) return 0.0;
  38. if (bw < 1) bw=1;
  39. bc-=bw/2;
  40. if (bc < 0)
  41. {
  42. bw+=bc;
  43. bc=0;
  44. }
  45. if (bc > 575) bc=575;
  46. if (bc+bw > 576) bw=576-bc;
  47. if (!ch)
  48. {
  49. for (x = 0; x < bw; x ++)
  50. {
  51. accum+=(visdata[bc]^xorv)-xorv;
  52. accum+=(visdata[bc+576]^xorv)-xorv;
  53. bc++;
  54. }
  55. return (double)accum / ((double)bw*255.0);
  56. }
  57. else
  58. {
  59. if (ch == 2) visdata+=576;
  60. for (x = 0; x < bw; x ++) accum+=(visdata[bc++]^xorv)-xorv;
  61. return (double)accum / ((double)bw*127.5);
  62. }
  63. }
  64. static double NSEEL_CGEN_CALL getspec_(void *_this, double *band, double *bandw, double *chan)
  65. {
  66. if (!g_evallib_visdata) return 0.0;
  67. return getvis((unsigned char *)g_evallib_visdata,(int)(*band*576.0),(int)(*bandw*576.0),(int)(*chan+0.5),0)*0.5;
  68. }
  69. static double NSEEL_CGEN_CALL getosc_(void *_this, double *band, double *bandw, double *chan)
  70. {
  71. if (!g_evallib_visdata) return 0.0;
  72. return getvis((unsigned char *)g_evallib_visdata+576*2,(int)(*band*576.0),(int)(*bandw*576.0),(int)(*chan+0.5),128);
  73. }
  74. static double NSEEL_CGEN_CALL gettime_(void *_this, double *sc)
  75. {
  76. int ispos;
  77. if ((ispos=(*sc > -1.001 && *sc < -0.999)) || (*sc > -2.001 && *sc < -1.999))
  78. {
  79. int pos=0;
  80. extern HWND hwnd_WinampParent;
  81. if (IsWindow(hwnd_WinampParent))
  82. {
  83. if (!SendMessageTimeout( hwnd_WinampParent, WM_USER,(WPARAM)!ispos,(LPARAM)105,SMTO_BLOCK,50,(LPDWORD)&pos)) pos=0;
  84. }
  85. if (!ispos) return (double)pos;
  86. return pos / 1000.0;
  87. }
  88. return GetTickCount()/1000.0 - *sc;
  89. }
  90. static double NSEEL_CGEN_CALL setmousepos_(void *_this, double *x, double *y)
  91. {
  92. //fucko: implement me
  93. return 0.0;
  94. }
  95. extern double DDraw_translatePoint(POINT p, int isY);
  96. static double NSEEL_CGEN_CALL getmouse_(void *_this, double *which)
  97. {
  98. int w=(int)(*which+0.5);
  99. if (w > 5)
  100. return (GetAsyncKeyState(w)&0x8000)?1.0:0.0;
  101. if (w == 1 || w == 2)
  102. {
  103. POINT p;
  104. GetCursorPos(&p);
  105. return DDraw_translatePoint(p,w==2);
  106. }
  107. if (w == 3) return (GetAsyncKeyState(MK_LBUTTON)&0x8000)?1.0:0.0;
  108. if (w == 4) return (GetAsyncKeyState(MK_RBUTTON)&0x8000)?1.0:0.0;
  109. if (w == 5) return (GetAsyncKeyState(MK_MBUTTON)&0x8000)?1.0:0.0;
  110. return 0.0;
  111. }
  112. /////////////////////// end AVS specific script functions
  113. void NSEEL_HOSTSTUB_EnterMutex()
  114. {
  115. EnterCriticalSection(&g_eval_cs);
  116. }
  117. void NSEEL_HOSTSTUB_LeaveMutex()
  118. {
  119. LeaveCriticalSection(&g_eval_cs);
  120. }
  121. void AVS_EEL_IF_init()
  122. {
  123. InitializeCriticalSection(&g_eval_cs);
  124. NSEEL_init();
  125. // todo: check to see that parameter orders are correct
  126. NSEEL_addfunctionex("getosc",3,(char *)_asm_generic3parm_retd,(char *)_asm_generic3parm_retd_end-(char *)_asm_generic3parm_retd,NSEEL_PProc_THIS,(void*)getosc_);
  127. NSEEL_addfunctionex("getspec",3,(char *)_asm_generic3parm_retd,(char *)_asm_generic3parm_retd_end-(char *)_asm_generic3parm_retd,NSEEL_PProc_THIS,(void*)getspec_);
  128. NSEEL_addfunctionex("gettime",1,(char *)_asm_generic1parm_retd,(char *)_asm_generic1parm_retd_end-(char *)_asm_generic1parm_retd,NSEEL_PProc_THIS,(void*)gettime_);
  129. NSEEL_addfunctionex("getkbmouse",1,(char *)_asm_generic1parm_retd,(char *)_asm_generic1parm_retd_end-(char *)_asm_generic1parm_retd,NSEEL_PProc_THIS,(void*)getmouse_);
  130. NSEEL_addfunctionex("setmousepos",2,(char *)_asm_generic2parm_retd,(char *)_asm_generic2parm_retd_end-(char *)_asm_generic2parm_retd,NSEEL_PProc_THIS,(void*)setmousepos_);
  131. }
  132. void AVS_EEL_IF_quit()
  133. {
  134. DeleteCriticalSection(&g_eval_cs);
  135. NSEEL_quit();
  136. }
  137. static void movestringover(char *str, int amount)
  138. {
  139. char tmp[1024+8];
  140. int l=(int)strlen(str);
  141. l=min(1024-amount-1,l);
  142. memcpy(tmp,str,l+1);
  143. while (l >= 0 && tmp[l]!='\n') l--;
  144. l++;
  145. tmp[l]=0;//ensure we null terminate
  146. memcpy(str+amount,tmp,l+1);
  147. }
  148. NSEEL_CODEHANDLE AVS_EEL_IF_Compile(void *context, char *code)
  149. {
  150. NSEEL_CODEHANDLE ret;
  151. EnterCriticalSection(&g_eval_cs);
  152. ret=NSEEL_code_compile((NSEEL_VMCTX)context,code,0);
  153. if (!ret)
  154. {
  155. if (g_log_errors)
  156. {
  157. char *expr = NSEEL_code_getcodeerror((NSEEL_VMCTX)context);
  158. if (expr)
  159. {
  160. int l=strlen(expr);
  161. if (l > 512) l=512;
  162. movestringover(last_error_string,l+2);
  163. memcpy(last_error_string,expr,l);
  164. last_error_string[l]='\r';
  165. last_error_string[l+1]='\n';
  166. }
  167. }
  168. }
  169. LeaveCriticalSection(&g_eval_cs);
  170. return ret;
  171. }
  172. void AVS_EEL_IF_Execute(NSEEL_CODEHANDLE handle, char visdata[2][2][576])
  173. {
  174. if (handle)
  175. {
  176. EnterCriticalSection(&g_eval_cs);
  177. g_evallib_visdata=(char*)visdata;
  178. NSEEL_code_execute((NSEEL_CODEHANDLE)handle);
  179. g_evallib_visdata=NULL;
  180. LeaveCriticalSection(&g_eval_cs);
  181. }
  182. }
  183. void AVS_EEL_IF_resetvars(NSEEL_VMCTX ctx)
  184. {
  185. NSEEL_VM_freeRAM(ctx);
  186. NSEEL_VM_remove_all_nonreg_vars(ctx);
  187. }