summaryrefslogtreecommitdiff
path: root/src/bin/engine_gl_cocoa.m
blob: a6878d7b92fca12b4cf237fdca444288f4672907 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* NOTE: If you're using Expedite for performance testing,
   set "Disable Beam Synchronization" in GL_Cocoa Debug. */

#include "main.h"

#include <Cocoa/Cocoa.h>
#include <Evas_Engine_GL_Cocoa.h>

static NSView * evas_view;
static NSWindow * main_window;


int
engine_gl_cocoa_args(int argc, char **argv)
{
   Evas_Engine_Info_GL_Cocoa *einfo;
   int i;
   int ok = 0;

   /*
   for (i = 1; i < argc; i++)
   {
      if ((!strcmp(argv[i], "-e")) && (i < (argc - 1)))
      {
         i++;
         if (!strcmp(argv[i], "gl-cocoa")) ok = 1;
      }
   }
   if (!ok) return 0;
   */

   evas_output_method_set(evas, evas_render_method_lookup("gl_cocoa"));
   einfo = (Evas_Engine_Info_GL_Cocoa *)evas_engine_info_get(evas);
   if (!einfo)
   {
      printf("Evas is a goat and can't run GL_COCOA ... or .... it's me the goat\n");
      return 0;
   }

   // Set up the Cocoa runtime
   [[NSAutoreleasePool alloc] init];
   [NSApplication sharedApplication];

   // Register ourselves as a full-fledged Cocoa app, instead of a NSUIElement.
   // This gives benefits like window focus and a dock icon!
   ProcessSerialNumber psn = { 0, kCurrentProcess };
   TransformProcessType (&psn, kProcessTransformToForegroundApplication);

   [NSApp finishLaunching];
   // Create our main window, and embed an EvasGLView in it
   main_window = [[NSWindow alloc] initWithContentRect:NSMakeRect(20,500,win_w,win_h) styleMask:(NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask) backing:NSBackingStoreBuffered defer:NO screen:nil];
   [main_window makeKeyAndOrderFront:NSApp];
   [main_window setTitle:@"Expedite"];
   [main_window makeMainWindow];
   [main_window setAcceptsMouseMovedEvents:YES];
   [NSApp activateIgnoringOtherApps:YES];

   evas_output_size_set(evas, win_w, win_h);
   evas_output_viewport_set(evas, 0, 0, win_w, win_h);

   einfo->window = main_window;
	
   //einfo->info.context = [evas_view context];
   if (!evas_engine_info_set(evas, (Evas_Engine_Info *) einfo))
     {
	printf("Evas can not setup the informations of the GL_Cocoa Engine\n");
        exit(-1);
     }

   evas_view = (NSView*)einfo->view;
   [[main_window contentView] addSubview:evas_view];
   [evas_view display];

   evas_damage_rectangle_add(evas,0,0, win_w ,win_h);
   return 1;
}

void
engine_gl_cocoa_loop(void)
{
   NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0.001];

   NSEvent *event = [NSApp nextEventMatchingMask:NSAnyEventMask
                                       untilDate:date
                                          inMode:NSDefaultRunLoopMode
                                         dequeue:YES];
   [date release];

   if (!event) return;

   if([event type] == NSMouseMoved || [event type] == NSLeftMouseDragged || [event type] == NSRightMouseDragged)
   {
      evas_event_feed_mouse_move(evas, [event locationInWindow].x, [event locationInWindow].y, 0, NULL);

      // Also notify on entering or leaving the window
      NSPoint mouseLoc = [[event window] convertBaseToScreen:[event locationInWindow]];

      if(NSPointInRect(mouseLoc, [[event window] frame]))
         evas_event_feed_mouse_in(evas, 0, NULL);
      else
         evas_event_feed_mouse_out(evas, 0, NULL);

      [NSApp sendEvent:event];
   }
   else if([event type] == NSLeftMouseDown)
   {
      evas_event_feed_mouse_move(evas, [event locationInWindow].x, [event locationInWindow].y, 0, NULL);
      evas_event_feed_mouse_down(evas, 1, EVAS_BUTTON_NONE, 0, NULL);

      [NSApp sendEvent:event];
   }
   else if([event type] == NSLeftMouseUp)
   {
      evas_event_feed_mouse_move(evas, [event locationInWindow].x, [event locationInWindow].y, 0, NULL);
      evas_event_feed_mouse_up(evas, 1, EVAS_BUTTON_NONE, 0, NULL);

      [NSApp sendEvent:event];
   }
   else if([event type] == NSRightMouseDown)
   {
      evas_event_feed_mouse_move(evas, [event locationInWindow].x, [event locationInWindow].y, 0, NULL);
      evas_event_feed_mouse_down(evas, 3, EVAS_BUTTON_NONE, 0, NULL);
      [NSApp sendEvent:event];
   }
   else if([event type] == NSRightMouseUp)
   {
      evas_event_feed_mouse_move(evas, [event locationInWindow].x, [event locationInWindow].y, 0, NULL);
      evas_event_feed_mouse_up(evas, 3, EVAS_BUTTON_NONE, 0, NULL);
      [NSApp sendEvent:event];
   }
   else if([event type] == NSKeyDown)
   {
      NSString * chars = [event charactersIgnoringModifiers];

      switch([chars characterAtIndex:0])
      {
         case NSLeftArrowFunctionKey:
            evas_event_feed_key_down(evas, "Left", "Left", NULL, NULL, 0, NULL);
            break;
         case NSRightArrowFunctionKey:
            evas_event_feed_key_down(evas, "Right", "Right", NULL, NULL, 0, NULL);
            break;
         case NSEnterCharacter:
         case NSCarriageReturnCharacter:
            evas_event_feed_key_down(evas, "Return", "Return", NULL, NULL, 0, NULL);
            break;
         case 's':
         case '\e':
            evas_event_feed_key_down(evas, "Escape", "Escape", NULL, NULL, 0, NULL);
            break;
         case 'q':
            evas_event_feed_key_down(evas, "q", "q", NULL, NULL, 0, NULL);
            break;
      }

      [chars release];
   }
   else if([event type] == NSKeyUp)
   {
      NSString * chars = [event charactersIgnoringModifiers];

      switch([chars characterAtIndex:0])
      {
         case NSLeftArrowFunctionKey:
            evas_event_feed_key_up(evas, "Left", "Left", NULL, NULL, 0, NULL);
            break;
         case NSRightArrowFunctionKey:
            evas_event_feed_key_up(evas, "Right", "Right", NULL, NULL, 0, NULL);
            break;
         case NSEnterCharacter:
         case NSCarriageReturnCharacter:
            evas_event_feed_key_up(evas, "Return", "Return", NULL, NULL, 0, NULL);
            break;
         case '\e':
            evas_event_feed_key_up(evas, "Escape", "Escape", NULL, NULL, 0, NULL);
            break;
         case 'q':
            if (evas_key_lock_is_set(evas_key_lock_get(evas), "Caps_Lock"))
              evas_event_feed_key_up(evas, "Q", "Q", NULL, NULL, 0, NULL);
            else
              evas_event_feed_key_up(evas, "q", "q", NULL, NULL, 0, NULL);
            break;
      }

      [chars release];
   }
   else if([event type] == NSFlagsChanged)
   {
      int flags = [event modifierFlags];

      // Turn special key flags on
      if(flags & NSShiftKeyMask)
         evas_key_modifier_on(evas, "Shift");
      else if(flags & NSControlKeyMask)
         evas_key_modifier_on(evas, "Control");
      else if(flags & NSAlternateKeyMask)
         evas_key_modifier_on(evas, "Alt");
      else if(flags & NSCommandKeyMask)
         evas_key_modifier_on(evas, "Super");
      else if(flags & NSAlphaShiftKeyMask)
      {
         if (evas_key_lock_is_set(evas_key_lock_get(evas), "Caps_Lock"))
           evas_key_lock_off(evas, "Caps_Lock");
         else
           evas_key_lock_on(evas, "Caps_Lock");
      }

      // Turn special key flags off
      if(!(flags & NSShiftKeyMask))
         evas_key_modifier_off(evas, "Shift");
      else if(!(flags & NSControlKeyMask))
         evas_key_modifier_off(evas, "Control");
      else if(!(flags & NSAlternateKeyMask))
         evas_key_modifier_off(evas, "Alt");
      else if(!(flags & NSCommandKeyMask))
         evas_key_modifier_off(evas, "Super");
   }
   else
   {
      [NSApp sendEvent:event];
   }

   [event release];
   [NSAutoreleasePool release];
}

void
engine_gl_cocoa_shutdown(void)
{
  /* If someone knows what to do here... */
}