MainWindow.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // Copyright (c) 2009 Openmoko Inc.
  2. //
  3. // Authors Daniel Mack
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #import "MainWindow.h"
  18. #include <wikilib.h>
  19. #include <guilib.h>
  20. #include <input.h>
  21. char *framebuffer;
  22. /* wikireader glue level */
  23. void fb_refresh(void)
  24. {
  25. MainWindow *window = (MainWindow *) [NSApp mainWindow];
  26. [window refreshDisplay];
  27. }
  28. int wl_input_wait(struct wl_input_event *ev)
  29. {
  30. MainWindow *window = (MainWindow *) [NSApp mainWindow];
  31. NSCondition *condition = [window getCondition];
  32. NSPoint event_location, local_point;
  33. NSSize view_size;
  34. ev->type = -1;
  35. do {
  36. [condition lock];
  37. [condition wait];
  38. [condition unlock];
  39. NSEvent *currentEvent = [NSApp currentEvent];
  40. switch ([currentEvent type]) {
  41. case NSKeyUp:
  42. case NSKeyDown:
  43. ev->type = WL_INPUT_EV_TYPE_KEYBOARD;
  44. ev->key_event.keycode = [[currentEvent characters] characterAtIndex: 0];
  45. if (ev->key_event.keycode == '?')
  46. ev->key_event.keycode = [currentEvent keyCode];
  47. ev->key_event.value = ([currentEvent type] == NSKeyUp) ? 0 : 1;
  48. break;
  49. case NSLeftMouseUp:
  50. case NSLeftMouseDown:
  51. ev->type = WL_INPUT_EV_TYPE_TOUCH;
  52. event_location = [currentEvent locationInWindow];
  53. local_point = [[window imageView] convertPoint:event_location fromView:nil];
  54. view_size = [[window imageView] bounds].size;
  55. ev->touch_event.x = local_point.x;
  56. ev->touch_event.y = view_size.height - local_point.y;
  57. ev->touch_event.value = ([currentEvent type] == NSLeftMouseUp) ? 0 : 1;
  58. break;
  59. }
  60. } while (ev->type == -1);
  61. return 0;
  62. }
  63. @implementation MainWindow
  64. - (void)keyDown:(NSEvent *)event
  65. {
  66. [condition lock];
  67. [condition signal];
  68. [condition unlock];
  69. }
  70. - (void)mouseDownInDisplay:(NSEvent *) event
  71. {
  72. [condition lock];
  73. [condition signal];
  74. [condition unlock];
  75. }
  76. - (NSCondition *) getCondition
  77. {
  78. return condition;
  79. }
  80. - (NSImageView *) imageView
  81. {
  82. return imageView;
  83. }
  84. - (void) refreshDisplay
  85. {
  86. NSInteger x, y;
  87. for (x = 0; x < FRAMEBUFFER_WIDTH; x++)
  88. for (y = 0; y < FRAMEBUFFER_HEIGHT; y++) {
  89. NSUInteger val = guilib_get_pixel(x, y) * 0xff;
  90. NSUInteger rgba[4] = { val, val, val, 0 };
  91. [imageRep setPixel: rgba atX: x y: y];
  92. }
  93. [imageView setNeedsDisplay];
  94. }
  95. - (void) wikiLibThread : (id) param
  96. {
  97. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  98. wikilib_init();
  99. guilib_init();
  100. wikilib_run();
  101. [pool release];
  102. }
  103. - (void) awakeFromNib
  104. {
  105. framebuffer = (char *) malloc(FRAMEBUFFER_SIZE);
  106. if ([imageView frame].size.width != FRAMEBUFFER_WIDTH ||
  107. [imageView frame].size.height != FRAMEBUFFER_HEIGHT) {
  108. printf("ERROR! guilib's framebuffer size does not match canvas size!\n");
  109. return;
  110. }
  111. imageRep = [[NSBitmapImageRep alloc]
  112. initWithBitmapDataPlanes: NULL
  113. pixelsWide: (NSInteger) [imageView frame].size.width
  114. pixelsHigh: (NSInteger) [imageView frame].size.height
  115. bitsPerSample: 8
  116. samplesPerPixel: 3
  117. hasAlpha: NO
  118. isPlanar: NO
  119. colorSpaceName: @"NSCalibratedRGBColorSpace"
  120. bytesPerRow: (NSInteger) [imageView frame].size.width * 3
  121. bitsPerPixel: 24 ];
  122. [imageRep setSize: [imageView frame].size];
  123. NSImage *image = [[NSImage alloc] initWithSize: [imageView frame].size];
  124. [image addRepresentation: imageRep];
  125. [imageView setImage: image];
  126. condition = [[NSCondition alloc] init];
  127. frameBuffer = (unsigned char *) malloc(FRAMEBUFFER_WIDTH * FRAMEBUFFER_HEIGHT);
  128. memset(frameBuffer, 0, FRAMEBUFFER_WIDTH * FRAMEBUFFER_HEIGHT);
  129. [[NSNotificationCenter defaultCenter] addObserver:self
  130. selector:@selector(mouseDownInDisplay:)
  131. name:@"mouseEvent" object:imageView];
  132. }
  133. // NSApplication delegates
  134. - (void)applicationDidBecomeActive: (NSNotification *) aNotification
  135. {
  136. [NSThread detachNewThreadSelector: @selector(wikiLibThread:)
  137. toTarget: [NSApp mainWindow]
  138. withObject: nil];
  139. }
  140. // GUI callbacks
  141. - (IBAction) buttonPressed: (id) sender
  142. {
  143. unsigned int code;
  144. switch ([sender tag]) {
  145. case 0:
  146. code = WL_INPUT_KEY_SEARCH;
  147. break;
  148. case 1:
  149. code = WL_INPUT_KEY_TREE;
  150. break;
  151. case 2:
  152. code = WL_INPUT_KEY_RANDOM;
  153. break;
  154. default:
  155. return;
  156. }
  157. NSEvent *curr = [NSApp currentEvent];
  158. NSEvent *ev = [NSEvent keyEventWithType: NSKeyDown
  159. location: [curr locationInWindow]
  160. modifierFlags: 0
  161. timestamp: [curr timestamp]
  162. windowNumber: [curr windowNumber]
  163. context: [curr context]
  164. characters: @"?"
  165. charactersIgnoringModifiers: nil
  166. isARepeat: NO
  167. keyCode: code ];
  168. [NSApp postEvent: ev atStart: YES];
  169. }
  170. @end