xuimenu.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include <precomp.h>
  2. #include "xuimenu.h"
  3. #include <api/service/svcs/svc_action.h>
  4. #define MENU_TIMER_CHECK 0x100
  5. #define DC_MENU_CHAIN 0x101
  6. #define DC_MENU_REENABLE 0x102
  7. #define DC_MENU_INIT 0x103
  8. #define DC_MENU_NEXT 0x104
  9. #define DC_MENU_PREV 0x105
  10. #define DC_MENU_OPENACTION 0x106
  11. #define MENU_TIMER_POPKBDLOCK 0x107
  12. #define MENU_TIMER_DELAY 5
  13. extern HINSTANCE hInstance;
  14. const wchar_t MenuXuiObjectStr[] = L"Menu"; // This is the xml tag
  15. char MenuXuiSvcName[] = "Menu xui object";
  16. static XuiMenu *xuimenu_hookingMenu = NULL;
  17. static HHOOK hhook=NULL;
  18. static HHOOK hhook_menuselect=NULL;
  19. static int hookusercount = 0;
  20. XMLParamPair XuiMenu::params[] = {
  21. {MENU_DOWNID, L"DOWN"},
  22. {MENU_HOVERID, L"HOVER"},
  23. {MENU_MENU, L"MENU"},
  24. {MENU_MENUGROUP, L"MENUGROUP"},
  25. {MENU_NEXT, L"NEXT"},
  26. {MENU_NORMALID, L"NORMAL"},
  27. {MENU_PREV, L"PREV"},
  28. };
  29. XuiMenu::XuiMenu() {
  30. timerset = 0;
  31. disablespawn = 0;
  32. nextinchain = NULL;
  33. normal = NULL;
  34. down = NULL;
  35. isspawned = 0;
  36. inarea = 0;
  37. kbdhook = 0;
  38. kbdlocktimer = 0;
  39. submenu_isselected = 0;
  40. submenu_selectedbymouse = 0;
  41. submenu_selected = NULL;
  42. first_hmenu = NULL;
  43. cur_hmenu = NULL;
  44. getScriptObject()->vcpu_setInterface(xuiMenuGuid, (void *)static_cast<XuiMenu *>(this));
  45. xuihandle = newXuiHandle();
  46. CreateXMLParameters(xuihandle);
  47. hover = NULL;
  48. orig_x = orig_y = 0;
  49. menu_parent = NULL;
  50. }
  51. void XuiMenu::CreateXMLParameters(int master_handle)
  52. {
  53. //XUIMENU_PARENT::CreateXMLParameters(master_handle);
  54. int numParams = sizeof(params) / sizeof(params[0]);
  55. hintNumberOfParams(xuihandle, numParams);
  56. for (int i = 0;i < numParams;i++)
  57. addParam(xuihandle, params[i], XUI_ATTRIBUTE_IMPLIED);
  58. }
  59. XuiMenu::~XuiMenu() {
  60. if (timerset) killTimer(MENU_TIMER_CHECK);
  61. if (kbdlocktimer) { killTimer(MENU_TIMER_POPKBDLOCK); WASABI_API_WND->popKeyboardLock(); } // do not set kbdlocktimer to 0, or stopKbdHook will start a timer
  62. stopKbdHook();
  63. }
  64. int XuiMenu::onInit() {
  65. XUIMENU_PARENT::onInit();
  66. postDeferredCallback(DC_MENU_INIT);
  67. return 1;
  68. }
  69. int XuiMenu::setXuiParam(int _xuihandle, int xmlattrid, const wchar_t *name, const wchar_t *value) {
  70. if (xuihandle == _xuihandle) {
  71. switch (xmlattrid) {
  72. case MENU_MENUGROUP: setMenuGroup(value); return 1;
  73. case MENU_MENU: setMenu(value); return 1;
  74. case MENU_NORMALID: setNormalId(value); return 1;
  75. case MENU_DOWNID: setDownId(value); return 1;
  76. case MENU_HOVERID: setHoverId(value); return 1;
  77. case MENU_NEXT: next = value; return 1;
  78. case MENU_PREV: prev = value; return 1;
  79. }
  80. }
  81. return XUIMENU_PARENT::setXuiParam(_xuihandle, xmlattrid, name, value);
  82. }
  83. void XuiMenu::setMenu(const wchar_t *m)
  84. {
  85. menuid = m;
  86. return;
  87. }
  88. void XuiMenu::setMenuGroup(const wchar_t *mg) {
  89. menugroup = mg;
  90. return;
  91. }
  92. void XuiMenu::setNormalId(const wchar_t *id) {
  93. normalid = id;
  94. updateObjects();
  95. }
  96. void XuiMenu::setDownId(const wchar_t *id) {
  97. downid = id;
  98. updateObjects();
  99. }
  100. void XuiMenu::setHoverId(const wchar_t *id) {
  101. hoverid = id;
  102. updateObjects();
  103. }
  104. int XuiMenu::onLeftButtonDown(int x, int y) {
  105. XUIMENU_PARENT::onLeftButtonDown(x, y);
  106. if (!disablespawn)
  107. spawnMenu();
  108. return 1;
  109. }
  110. void XuiMenu::timerCallback(int c) {
  111. if (c == MENU_TIMER_CHECK) {
  112. timerCheck();
  113. } else if (c == MENU_TIMER_POPKBDLOCK) {
  114. kbdlocktimer = 0;
  115. killTimer(MENU_TIMER_POPKBDLOCK);
  116. WASABI_API_WND->popKeyboardLock();
  117. }
  118. else XUIMENU_PARENT::timerCallback(c);
  119. }
  120. void XuiMenu::timerCheck() {
  121. POINT pt;
  122. Wasabi::Std::getMousePos((long*)&pt.x, (long*)&pt.y);
  123. if (pt.x == orig_x && pt.y == orig_y) return;
  124. nextinchain = NULL;
  125. ifc_window *w = WASABI_API_WND->rootWndFromPoint(&pt);
  126. if (w != NULL) {
  127. XuiMenu *x = static_cast<XuiMenu *>(w->getInterface(xuiMenuGuid));
  128. switchToMenu(x);
  129. }
  130. }
  131. void XuiMenu::switchToMenu(XuiMenu *x) {
  132. if (x != NULL && x != this && x != nextinchain)
  133. {
  134. if (WCSCASEEQLSAFE(x->getMenuGroup(), getMenuGroup()))
  135. {
  136. killTimer(MENU_TIMER_CHECK);
  137. timerset = 0;
  138. nextinchain = x;
  139. stopKbdHook();
  140. cancelMenu();
  141. }
  142. }
  143. }
  144. void XuiMenu::spawnMenu(int monitor) {
  145. Wasabi::Std::getMousePos(&orig_x, &orig_y);
  146. isspawned = 1;
  147. onOpenMenu();
  148. if (monitor) { setTimer(MENU_TIMER_CHECK, MENU_TIMER_DELAY); timerset = 1; startKbdHook(); }
  149. StringW actionstr = StringPrintfW(L"MENU:%s", menuid);
  150. svc_action *act = ActionEnum(actionstr).getNext();
  151. if (act) {
  152. RECT r;
  153. getClientRect(&r);
  154. clientToScreen(&r);
  155. act->onAction(actionstr, NULL, r.left, r.bottom, NULL, 0, this);
  156. SvcEnum::release(act);
  157. }
  158. if (monitor) {
  159. if (timerset) { killTimer(MENU_TIMER_CHECK); timerset = 0; }
  160. stopKbdHook();
  161. }
  162. disablespawn = 1;
  163. if (nextinchain) {
  164. postDeferredCallback(DC_MENU_CHAIN);
  165. }
  166. postDeferredCallback(DC_MENU_REENABLE);
  167. isspawned = 0;
  168. onCloseMenu();
  169. }
  170. int XuiMenu::onDeferredCallback(intptr_t p1, intptr_t p2) {
  171. if (p1 == DC_MENU_CHAIN) {
  172. XuiMenu *x = nextinchain;
  173. nextinchain = NULL;
  174. x->spawnMenu();
  175. } else if (p1 == DC_MENU_REENABLE) {
  176. disablespawn = 0;
  177. } else if (p1 == DC_MENU_INIT) {
  178. updateObjects();
  179. } else if (p1 == DC_MENU_NEXT) {
  180. _nextMenu();
  181. } else if (p1 == DC_MENU_PREV) {
  182. _previousMenu();
  183. } else if (p1 == DC_MENU_OPENACTION) {
  184. spawnMenu();
  185. WASABI_API_WND->kbdReset();
  186. } else return XUIMENU_PARENT::onDeferredCallback(p1, p2);
  187. return 1;
  188. }
  189. void XuiMenu::cancelMenu() {
  190. #ifdef WIN32
  191. PostMessage(gethWnd(), WM_LBUTTONDOWN, 0, 0xdeadc0de);
  192. PostMessage(gethWnd(), WM_LBUTTONUP, 0, 0xdeadc0de);
  193. #else
  194. #error port me! you should close that menu which is in its own message pump now (oh and btw, you don't know shit about the menu itself because it's spawned by an action). have fun!
  195. #endif
  196. }
  197. void XuiMenu::onOpenMenu() {
  198. updateObjects();
  199. script_onOpenMenu();
  200. }
  201. void XuiMenu::onCloseMenu() {
  202. updateObjects();
  203. WASABI_API_WND->kbdReset();
  204. script_onCloseMenu();
  205. }
  206. void XuiMenu::updateObjects()
  207. {
  208. normal = findObject(normalid);
  209. down = findObject(downid);
  210. hover = findObject(hoverid);
  211. if (normal) normal->guiobject_getRootWnd()->setVisible(!isspawned);
  212. if (down) down->guiobject_getRootWnd()->setVisible(isspawned);
  213. if (hover) hover->guiobject_getRootWnd()->setVisible(!isspawned && inarea);
  214. }
  215. void XuiMenu::onEnterArea() {
  216. XUIMENU_PARENT::onEnterArea();
  217. inarea = 1;
  218. updateObjects();
  219. }
  220. void XuiMenu::onLeaveArea() {
  221. XUIMENU_PARENT::onLeaveArea();
  222. inarea = 0;
  223. updateObjects();
  224. }
  225. int XuiMenu::onAction(const wchar_t *action, const wchar_t *param, int x, int y, intptr_t p1, intptr_t p2, void *data, size_t datalen, ifc_window *source)
  226. {
  227. if (!_wcsicmp(action, L"open")) { openAction(); }
  228. //else if (STRCASEEQL(action, "preopen")) { inarea = 1; updateObjects(); startKbdHook(); }
  229. else if (!_wcsicmp(action, L"close")) cancelMenu();
  230. else return XUIMENU_PARENT::onAction(action, param, x, y, p1, p2, data, datalen, source);
  231. return 1;
  232. }
  233. LRESULT CALLBACK xuimenu_KeyboardProc(int code, WPARAM wParam, LPARAM lParam) {
  234. if (code >= 0 && xuimenu_hookingMenu != NULL) {
  235. if (code != HC_NOREMOVE && !(lParam & (1 << 31))) {
  236. switch (wParam) {
  237. case VK_LEFT: xuimenu_hookingMenu->onTrappedLeft(); break;
  238. case VK_RIGHT: xuimenu_hookingMenu->onTrappedRight(); break;
  239. //case VK_ESCAPE: DebugString("Escape trapped!\n"); break;
  240. }
  241. }
  242. }
  243. if (hhook) return CallNextHookEx(hhook, code, wParam, lParam);
  244. return 1;
  245. }
  246. LRESULT CALLBACK xuimenu_msgProc(int code, WPARAM wParam, LPARAM lParam) {
  247. if (code >= 0 && xuimenu_hookingMenu != NULL) {
  248. MSG *msg = (MSG *)lParam;
  249. if (msg->message == WM_MENUSELECT) {
  250. xuimenu_hookingMenu->onMenuSelect(msg->hwnd, (HMENU)msg->lParam, LOWORD(msg->wParam), HIWORD(msg->wParam));
  251. }
  252. }
  253. if (hhook_menuselect) return CallNextHookEx(hhook_menuselect, code, wParam, lParam);
  254. return 1;
  255. }
  256. void XuiMenu::startKbdHook() {
  257. if (kbdhook) return;
  258. if (kbdlocktimer) { killTimer(MENU_TIMER_POPKBDLOCK); kbdlocktimer = 0; }
  259. else WASABI_API_WND->pushKeyboardLock();
  260. xuimenu_hookingMenu = this;
  261. if (hhook == NULL) {
  262. hhook = SetWindowsHookEx(WH_KEYBOARD, xuimenu_KeyboardProc, 0, GetCurrentThreadId());
  263. hhook_menuselect = SetWindowsHookEx(WH_MSGFILTER, xuimenu_msgProc, 0, GetCurrentThreadId());
  264. hookusercount = 1;
  265. } else hookusercount++;
  266. kbdhook = 1;
  267. }
  268. void XuiMenu::stopKbdHook() {
  269. if (!kbdhook) return;
  270. if (nextinchain) {
  271. if (!kbdlocktimer) {
  272. kbdlocktimer = 1;
  273. setTimer(MENU_TIMER_POPKBDLOCK, 500);
  274. }
  275. } else WASABI_API_WND->popKeyboardLock();
  276. if (xuimenu_hookingMenu == this) xuimenu_hookingMenu = NULL;
  277. first_hmenu = NULL;
  278. submenu_isselected = 0;
  279. submenu_selectedbymouse = 0;
  280. submenu_selected = NULL;
  281. cur_hmenu = NULL;
  282. menu_parent = NULL;
  283. if (--hookusercount == 0) {
  284. UnhookWindowsHookEx(hhook_menuselect);
  285. UnhookWindowsHookEx(hhook);
  286. hhook = NULL;
  287. hhook_menuselect = NULL;
  288. }
  289. kbdhook = 0;
  290. }
  291. void XuiMenu::onMenuSelect(HWND hwnd, HMENU menu, int item, int flags) {
  292. if (first_hmenu == NULL) first_hmenu = menu;
  293. cur_hmenu = menu;
  294. menu_parent = hwnd;
  295. // DebugString("Menu Item Selected! HMENU = %X, item = %d, flags = %d, submenu = %X\n", menu, item, flags, flags & MF_POPUP);
  296. if (flags & MF_POPUP) { submenu_isselected = 1; submenu_selected = GetSubMenu(menu, item); submenu_selectedbymouse = flags & MF_MOUSESELECT; }
  297. else { submenu_isselected = 0; submenu_selected = NULL; submenu_selectedbymouse = 0; }
  298. }
  299. void XuiMenu::onTrappedLeft() {
  300. if (cur_hmenu == first_hmenu)
  301. xuimenu_hookingMenu->previousMenu();
  302. }
  303. void XuiMenu::onTrappedRight() {
  304. if (!submenu_isselected)
  305. nextMenu();
  306. else if (submenu_selectedbymouse)
  307. PostMessage(menu_parent, WM_KEYDOWN, VK_DOWN, 0);
  308. }
  309. void XuiMenu::openAction() {
  310. postDeferredCallback(DC_MENU_OPENACTION);
  311. }
  312. void XuiMenu::previousMenu() {
  313. postDeferredCallback(DC_MENU_PREV);
  314. }
  315. void XuiMenu::nextMenu() {
  316. postDeferredCallback(DC_MENU_NEXT);
  317. }
  318. void XuiMenu::_nextMenu() {
  319. GuiObject *o = findObject(next);
  320. if (o) {
  321. XuiMenu *menu = static_cast<XuiMenu*>(o->guiobject_getScriptObject()->vcpu_getInterface(xuiMenuGuid));
  322. if (menu) switchToMenu(menu);
  323. }
  324. }
  325. void XuiMenu::_previousMenu() {
  326. GuiObject *o = findObject(prev);
  327. if (o) {
  328. XuiMenu *menu = static_cast<XuiMenu*>(o->guiobject_getScriptObject()->vcpu_getInterface(xuiMenuGuid));
  329. if (menu) switchToMenu(menu);
  330. }
  331. }