PolycodeView.mm 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #import "PolycodeView.h"
  20. @implementation PolycodeView
  21. @synthesize viewReady;
  22. @synthesize mouseX;
  23. @synthesize mouseY;
  24. - (id)initWithFrame:(NSRect)frameRect pixelFormat:(NSOpenGLPixelFormat *)format
  25. {
  26. self = [super initWithFrame:frameRect pixelFormat:format];
  27. if(self) {
  28. glSizeX = 0;
  29. glSizeY = 0;
  30. viewReady = NO;
  31. currentCursor = NULL;
  32. contextLock = [[NSLock alloc] init];
  33. }
  34. return self;
  35. }
  36. - (void) awakeFromNib {
  37. contextLock = [[NSLock alloc] init];
  38. [[self window] setAcceptsMouseMovedEvents:YES];
  39. [[self window] makeFirstResponder:self];
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowResized:) name:NSWindowDidResizeNotification object:[self window]];
  41. [self setAutoresizesSubviews: YES];
  42. }
  43. -(void) viewDidMoveToWindow
  44. {
  45. [super viewDidMoveToWindow];
  46. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignMain:) name:NSWindowDidResignKeyNotification object:[self window]];
  47. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidResignMain:) name:NSWindowDidResignMainNotification object:[self window]];
  48. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeMain:) name:NSWindowDidBecomeKeyNotification object:[self window]];
  49. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowDidBecomeMain:) name:NSWindowDidBecomeMainNotification object:[self window]];
  50. viewReady = YES;
  51. }
  52. - (void)windowDidResignMain:(NSNotification *)notification
  53. {
  54. if(core == NULL)
  55. return;
  56. core->lockMutex(core->eventMutex);
  57. CocoaEvent newEvent;
  58. newEvent.eventGroup = CocoaEvent::FOCUS_EVENT;
  59. newEvent.eventCode = Core::EVENT_LOST_FOCUS;
  60. core->cocoaEvents.push_back(newEvent);
  61. core->unlockMutex(core->eventMutex);
  62. }
  63. - (void)windowDidBecomeMain:(NSNotification *)notification
  64. {
  65. if(core == NULL)
  66. return;
  67. core->lockMutex(core->eventMutex);
  68. CocoaEvent newEvent;
  69. newEvent.eventGroup = CocoaEvent::FOCUS_EVENT;
  70. newEvent.eventCode = Core::EVENT_GAINED_FOCUS;
  71. core->cocoaEvents.push_back(newEvent);
  72. core->unlockMutex(core->eventMutex);
  73. }
  74. - (void) initKeymap {
  75. keymap[0x00] = KEY_a;
  76. keymap[0x01] = KEY_s;
  77. keymap[0x02] = KEY_d;
  78. keymap[0x03] = KEY_f;
  79. keymap[0x05] = KEY_g;
  80. keymap[0x04] = KEY_h;
  81. keymap[0x06] = KEY_z;
  82. keymap[0x07] = KEY_x;
  83. keymap[0x08] = KEY_c;
  84. keymap[0x09] = KEY_v;
  85. keymap[0x0B] = KEY_b;
  86. keymap[0x0C] = KEY_q;
  87. keymap[0x0D] = KEY_w;
  88. keymap[0x0E] = KEY_e;
  89. keymap[0x0F] = KEY_r;
  90. keymap[0x10] = KEY_y;
  91. keymap[0x11] = KEY_t;
  92. keymap[0x12] = KEY_1;
  93. keymap[0x13] = KEY_2;
  94. keymap[0x14] = KEY_3;
  95. keymap[0x15] = KEY_4;
  96. keymap[0x16] = KEY_6;
  97. keymap[0x17] = KEY_5;
  98. keymap[0x18] = KEY_EQUALS;
  99. keymap[0x19] = KEY_9;
  100. keymap[0x1A] = KEY_7;
  101. keymap[0x1B] = KEY_MINUS;
  102. keymap[0x1C] = KEY_8;
  103. keymap[0x1D] = KEY_0;
  104. keymap[0x1E] = KEY_RIGHTBRACKET;
  105. keymap[0x1F] = KEY_o;
  106. keymap[0x20] = KEY_u;
  107. keymap[0x21] = KEY_LEFTBRACKET;
  108. keymap[0x22] = KEY_i;
  109. keymap[0x23] = KEY_p;
  110. keymap[0x25] = KEY_l;
  111. keymap[0x26] = KEY_j;
  112. keymap[0x27] = KEY_QUOTE;
  113. keymap[0x28] = KEY_k;
  114. keymap[0x29] = KEY_SEMICOLON;
  115. keymap[0x2A] = KEY_BACKSLASH;
  116. keymap[0x2B] = KEY_COMMA;
  117. keymap[0x2C] = KEY_SLASH;
  118. keymap[0x2D] = KEY_n;
  119. keymap[0x2E] = KEY_m;
  120. keymap[0x2F] = KEY_PERIOD;
  121. // keymap[0x32] = KEY_GRAVE;
  122. keymap[0x41] = KEY_KP_PERIOD;
  123. keymap[0x43] = KEY_KP_MULTIPLY;
  124. keymap[0x45] = KEY_KP_PLUS;
  125. // keymap[0x47] = KEY_KeypadClear;
  126. keymap[0x4B] = KEY_KP_DIVIDE;
  127. keymap[0x4C] = KEY_KP_ENTER;
  128. keymap[0x4E] = KEY_KP_MINUS;
  129. keymap[0x51] = KEY_KP_EQUALS;
  130. keymap[0x52] = KEY_KP0;
  131. keymap[0x53] = KEY_KP1;
  132. keymap[0x54] = KEY_KP2;
  133. keymap[0x55] = KEY_KP3;
  134. keymap[0x56] = KEY_KP4;
  135. keymap[0x57] = KEY_KP5;
  136. keymap[0x58] = KEY_KP6;
  137. keymap[0x59] = KEY_KP7;
  138. keymap[0x5B] = KEY_KP8;
  139. keymap[0x5C] = KEY_KP9;
  140. keymap[0x24] = KEY_RETURN;
  141. keymap[0x30] = KEY_TAB;
  142. keymap[0x31] = KEY_SPACE;
  143. keymap[0x33] = KEY_BACKSPACE;
  144. keymap[0x35] = KEY_ESCAPE;
  145. keymap[0x37] = KEY_LSUPER;
  146. keymap[0x36] = KEY_RSUPER;
  147. keymap[0x38] = KEY_LSHIFT;
  148. keymap[0x39] = KEY_CAPSLOCK;
  149. keymap[0x3A] = KEY_LALT;
  150. keymap[0x3D] = KEY_RALT;
  151. keymap[0x3B] = KEY_LCTRL;
  152. keymap[0x3C] = KEY_RSHIFT;
  153. keymap[0x3E] = KEY_RCTRL;
  154. keymap[0x3F] = KEY_COMPOSE;
  155. // keymap[0x40] = KEY_F17;
  156. // keymap[0x48] = KEY_VolumeUp;
  157. // keymap[0x49] = KEY_VolumeDown;
  158. // keymap[0x4A] = KEY_Mute;
  159. // keymap[0x4F] = KEY_F18;
  160. // keymap[0x50] = KEY_F19;
  161. // keymap[0x5A] = KEY_F20;
  162. keymap[0x60] = KEY_F5;
  163. keymap[0x61] = KEY_F6;
  164. keymap[0x62] = KEY_F7;
  165. keymap[0x63] = KEY_F3;
  166. keymap[0x64] = KEY_F8;
  167. keymap[0x65] = KEY_F9;
  168. keymap[0x67] = KEY_F11;
  169. keymap[0x69] = KEY_F13;
  170. // keymap[0x6A] = KEY_F16;
  171. keymap[0x6B] = KEY_F14;
  172. keymap[0x6D] = KEY_F10;
  173. keymap[0x6F] = KEY_F12;
  174. keymap[0x71] = KEY_F15;
  175. keymap[0x72] = KEY_HELP;
  176. keymap[0x73] = KEY_HOME;
  177. keymap[0x74] = KEY_PAGEUP;
  178. keymap[0x75] = KEY_DELETE;
  179. keymap[0x76] = KEY_F4;
  180. keymap[0x77] = KEY_END;
  181. keymap[0x78] = KEY_F2;
  182. keymap[0x79] = KEY_PAGEDOWN;
  183. keymap[0x7A] = KEY_F1;
  184. keymap[0x7B] = KEY_LEFT;
  185. keymap[0x7C] = KEY_RIGHT;
  186. keymap[0x7D] = KEY_DOWN;
  187. keymap[0x7E] = KEY_UP;
  188. // keymap[0x0A] = KEY_ISO_Section;
  189. // keymap[0x5D] = KEY_JIS_Yen;
  190. // keymap[0x5E] = KEY_JIS_Underscore;
  191. // keymap[0x5F] = KEY_JIS_KeypadComma;
  192. // keymap[0x66] = KEY_JIS_Eisu;
  193. // keymap[0x68] = KEY_JIS_Kana;
  194. }
  195. -(void) setCurrentCursor: (NSCursor*) newCursor
  196. {
  197. currentCursor = newCursor;
  198. }
  199. - (void)resetCursorRects
  200. {
  201. if(currentCursor)
  202. [self addCursorRect:[self bounds] cursor:currentCursor];
  203. }
  204. - (void) setCore: (CocoaCore*) newCore {
  205. [self initKeymap];
  206. core = newCore;
  207. }
  208. - (BOOL) lockFocusIfCanDraw {
  209. [contextLock lock];
  210. BOOL retVal = [super lockFocusIfCanDraw];
  211. [contextLock unlock];
  212. return retVal;
  213. }
  214. - (void) update {
  215. [contextLock lock];
  216. [super update];
  217. [contextLock unlock];
  218. }
  219. - (void) lockContext {
  220. [contextLock lock];
  221. }
  222. - (void) unlockContext {
  223. [contextLock unlock];
  224. }
  225. - (void)prepareOpenGL {
  226. contextReady = YES;
  227. }
  228. - (BOOL) isContextReady {
  229. return contextReady;
  230. }
  231. - (void)setOpenGLContext:(NSOpenGLContext *)context {
  232. contextReady = NO;
  233. [super setOpenGLContext:context];
  234. }
  235. - (BOOL)acceptsFirstResponder
  236. {
  237. return YES;
  238. }
  239. // ---------------------------------
  240. - (BOOL)becomeFirstResponder
  241. {
  242. return YES;
  243. }
  244. // ---------------------------------
  245. - (BOOL)resignFirstResponder
  246. {
  247. return YES;
  248. }
  249. - (BOOL)acceptsFirstMouse:(NSEvent *)theEvent
  250. {
  251. return YES;
  252. }
  253. - (BOOL)mouseDownCanMoveWindow
  254. {
  255. return NO;
  256. }
  257. - (void) drawRect:(NSRect)rect {
  258. if(glSizeX != (int)[self frame].size.width || glSizeY != (int)[self frame].size.height) {
  259. glSizeX = (int)[self frame].size.width;
  260. glSizeY = (int)[self frame].size.height;
  261. if(core != NULL) {
  262. core->resizeTo(glSizeX, glSizeY);
  263. }
  264. }
  265. }
  266. -(void) windowResized: (NSNotification *) notification {
  267. }
  268. -(void) dealloc {
  269. [[NSNotificationCenter defaultCenter] removeObserver:self];
  270. [super dealloc];
  271. }
  272. - (void) resizeWithOldSuperviewSize: (NSSize) oldSize {
  273. [super resizeWithOldSuperviewSize: oldSize];
  274. // if(core == NULL)
  275. // return;
  276. // core->setVideoMode(oldSize.width, oldSize.height, core->isFullscreen(), core->getAALevel());
  277. }
  278. // INPUT
  279. - (void) otherMouseDown:(NSEvent *) event
  280. {
  281. if(core == NULL)
  282. return;
  283. core->lockMutex(core->eventMutex);
  284. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  285. CocoaEvent newEvent;
  286. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  287. newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
  288. newEvent.mouseButton = CoreInput::MOUSE_BUTTON3;
  289. newEvent.mouseX = mouseLoc.x;
  290. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  291. core->cocoaEvents.push_back(newEvent);
  292. core->unlockMutex(core->eventMutex);
  293. }
  294. - (void) otherMouseUp:(NSEvent *) event
  295. {
  296. if(core == NULL)
  297. return;
  298. core->lockMutex(core->eventMutex);
  299. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  300. CocoaEvent newEvent;
  301. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  302. newEvent.eventCode = InputEvent::EVENT_MOUSEUP;
  303. newEvent.mouseButton = CoreInput::MOUSE_BUTTON3;
  304. newEvent.mouseX = mouseLoc.x;
  305. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  306. core->cocoaEvents.push_back(newEvent);
  307. core->unlockMutex(core->eventMutex);
  308. }
  309. - (void) rightMouseDown:(NSEvent *) event
  310. {
  311. if(core == NULL)
  312. return;
  313. core->lockMutex(core->eventMutex);
  314. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  315. CocoaEvent newEvent;
  316. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  317. newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
  318. newEvent.mouseButton = CoreInput::MOUSE_BUTTON2;
  319. newEvent.mouseX = mouseLoc.x;
  320. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  321. core->cocoaEvents.push_back(newEvent);
  322. core->unlockMutex(core->eventMutex);
  323. }
  324. - (void) rightMouseUp:(NSEvent *) event
  325. {
  326. if(core == NULL)
  327. return;
  328. core->lockMutex(core->eventMutex);
  329. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  330. CocoaEvent newEvent;
  331. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  332. newEvent.eventCode = InputEvent::EVENT_MOUSEUP;
  333. newEvent.mouseButton = CoreInput::MOUSE_BUTTON2;
  334. newEvent.mouseX = mouseLoc.x;
  335. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  336. core->cocoaEvents.push_back(newEvent);
  337. core->unlockMutex(core->eventMutex);
  338. }
  339. - (void)scrollWheel:(NSEvent*) event {
  340. if(core == NULL || [event deltaY] == 0)
  341. return;
  342. core->lockMutex(core->eventMutex);
  343. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  344. CocoaEvent newEvent;
  345. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  346. if([event deltaY] > 0)
  347. newEvent.eventCode = InputEvent::EVENT_MOUSEWHEEL_UP;
  348. else
  349. newEvent.eventCode = InputEvent::EVENT_MOUSEWHEEL_DOWN;
  350. newEvent.mouseX = mouseLoc.x;
  351. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  352. core->cocoaEvents.push_back(newEvent);
  353. core->unlockMutex(core->eventMutex);
  354. }
  355. - (void) mouseDown:(NSEvent *) event
  356. {
  357. if(core == NULL)
  358. return;
  359. core->lockMutex(core->eventMutex);
  360. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  361. CocoaEvent newEvent;
  362. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  363. newEvent.eventCode = InputEvent::EVENT_MOUSEDOWN;
  364. newEvent.mouseButton = CoreInput::MOUSE_BUTTON1;
  365. newEvent.mouseX = mouseLoc.x;
  366. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  367. mouseX = mouseLoc.x;
  368. mouseY = mouseLoc.y;
  369. core->cocoaEvents.push_back(newEvent);
  370. core->unlockMutex(core->eventMutex);
  371. }
  372. - (void) mouseUp:(NSEvent *) event
  373. {
  374. if(core == NULL)
  375. return;
  376. core->lockMutex(core->eventMutex);
  377. NSPoint mouseLoc = [self convertPoint:[event locationInWindow] fromView:self];
  378. CocoaEvent newEvent;
  379. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  380. newEvent.eventCode = InputEvent::EVENT_MOUSEUP;
  381. newEvent.mouseButton = CoreInput::MOUSE_BUTTON1;
  382. newEvent.mouseX = mouseLoc.x;
  383. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  384. mouseX = mouseLoc.x;
  385. mouseY = mouseLoc.y;
  386. core->cocoaEvents.push_back(newEvent);
  387. core->unlockMutex(core->eventMutex);
  388. }
  389. - (void)flagsChanged:(NSEvent *)theEvent
  390. {
  391. if(core == NULL)
  392. return;
  393. if([theEvent keyCode] == 0) {
  394. return;
  395. }
  396. core->lockMutex(core->eventMutex);
  397. CocoaEvent newEvent;
  398. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  399. newEvent.keyCode = keymap[[theEvent keyCode]];
  400. switch(newEvent.keyCode) {
  401. case Polycode::KEY_LALT:
  402. if (([theEvent modifierFlags] & 0x80120) == 0x80120) {
  403. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  404. } else {
  405. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  406. }
  407. break;
  408. case Polycode::KEY_RALT:
  409. if (([theEvent modifierFlags] & 0x80140) == 0x80140) {
  410. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  411. } else {
  412. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  413. }
  414. break;
  415. case Polycode::KEY_LSHIFT:
  416. if (([theEvent modifierFlags] & 0x20102) == 0x20102) {
  417. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  418. } else {
  419. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  420. }
  421. break;
  422. case Polycode::KEY_RSHIFT:
  423. if (([theEvent modifierFlags] & 0x20104) == 0x20104) {
  424. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  425. } else {
  426. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  427. }
  428. break;
  429. case Polycode::KEY_LCTRL:
  430. if (([theEvent modifierFlags] & 0x40101) == 0x40101) {
  431. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  432. } else {
  433. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  434. }
  435. break;
  436. case Polycode::KEY_RCTRL:
  437. // no right control on my mac :)
  438. break;
  439. case Polycode::KEY_LSUPER:
  440. if (([theEvent modifierFlags] & 0x100108) == 0x100108) {
  441. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  442. } else {
  443. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  444. }
  445. break;
  446. case Polycode::KEY_RSUPER:
  447. if (([theEvent modifierFlags] & 0x100110) == 0x100110) {
  448. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  449. } else {
  450. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  451. }
  452. break;
  453. }
  454. newEvent.unicodeChar = 0;
  455. core->cocoaEvents.push_back(newEvent);
  456. core->unlockMutex(core->eventMutex);
  457. }
  458. - (void)keyDown: (NSEvent*) theEvent
  459. {
  460. if(core == NULL)
  461. return;
  462. core->lockMutex(core->eventMutex);
  463. CocoaEvent newEvent;
  464. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  465. newEvent.eventCode = InputEvent::EVENT_KEYDOWN;
  466. newEvent.keyCode = keymap[[theEvent keyCode]];
  467. NSString *chars = [theEvent characters];
  468. unsigned int numChars = [chars length];
  469. // NSLog(@"CHARS: %@", [chars characterAtIndex:0]);
  470. if(numChars > 0) {
  471. newEvent.unicodeChar = [chars characterAtIndex:0];
  472. } else {
  473. newEvent.unicodeChar = 0;
  474. }
  475. core->cocoaEvents.push_back(newEvent);
  476. core->unlockMutex(core->eventMutex);
  477. }
  478. - (void)keyUp: (NSEvent*) theEvent
  479. {
  480. if(core == NULL)
  481. return;
  482. core->lockMutex(core->eventMutex);
  483. CocoaEvent newEvent;
  484. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  485. newEvent.eventCode = InputEvent::EVENT_KEYUP;
  486. newEvent.keyCode = keymap[[theEvent keyCode]];
  487. NSString *chars = [theEvent characters];
  488. unsigned int numChars = [chars length];
  489. if(numChars > 0)
  490. newEvent.unicodeChar = [chars characterAtIndex:0];
  491. else
  492. newEvent.unicodeChar = 0;
  493. core->cocoaEvents.push_back(newEvent);
  494. core->unlockMutex(core->eventMutex);
  495. }
  496. - (void)mouseDragged:(NSEvent *)theEvent
  497. {
  498. if(core == NULL)
  499. return;
  500. core->lockMutex(core->eventMutex);
  501. NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:self];
  502. CocoaEvent newEvent;
  503. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  504. newEvent.eventCode = InputEvent::EVENT_MOUSEMOVE;
  505. newEvent.mouseX = mouseLoc.x;
  506. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  507. core->cocoaEvents.push_back(newEvent);
  508. core->unlockMutex(core->eventMutex);
  509. }
  510. - (void)mouseMoved:(NSEvent *)theEvent
  511. {
  512. if(core == NULL)
  513. return;
  514. core->lockMutex(core->eventMutex);
  515. NSPoint mouseLoc = [self convertPoint:[theEvent locationInWindow] fromView:self];
  516. CocoaEvent newEvent;
  517. newEvent.eventGroup = CocoaEvent::INPUT_EVENT;
  518. newEvent.eventCode = InputEvent::EVENT_MOUSEMOVE;
  519. newEvent.mouseX = mouseLoc.x;
  520. newEvent.mouseY = core->getYRes() - mouseLoc.y;
  521. mouseX = mouseLoc.x;
  522. mouseY = mouseLoc.y;
  523. core->cocoaEvents.push_back(newEvent);
  524. core->unlockMutex(core->eventMutex);
  525. }
  526. @end