Over the weekend I was playing around with trying to make a minimal OSX platform layer. Im trying to avoid all the latency and overhead that the objective C runtime adds... This is what I got so far, its pretty quick, but it uses some "hidden" apis, so there might be some stability implications of doing it this way, but I'm thinking its probably not that big of a deal... Anyway here is some code:
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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | /* mkdir -p build/test.app/Contents/MacOS clang++ --std=c++11 -fno-exceptions -fno-rtti -mmacosx-version-min=10.9 -Wno-writable-strings -Wno-deprecated-declarations -framework OpenGL -framework Carbon -g gui8.cpp -o build/test.app/Contents/MacOS/test */ #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <ApplicationServices/ApplicationServices.h> #include <Carbon/Carbon.h> #include <OpenGL/CGLTypes.h> #include <OpenGL/CGLCurrent.h> #include <OpenGL/OpenGL.h> #include <OpenGL/gl.h> typedef int CGSConnectionID; typedef int CGSWindowID; typedef int CGSSurfaceID; typedef uint32_t _CGWindowID; extern "C" { typedef int CGSConnection; typedef int CGSWindow; typedef int CGSValue; extern CGSValue CGSCreateCStringNoCopy(const char str); extern char CGSCStringValue(CGSValue string); extern int CGSIntegerValue(CGSValue intVal); typedef enum _CGSWindowOrderingMode { kCGSOrderAbove = 1, // Window is ordered above target. kCGSOrderBelow = -1, // Window is ordered below target. kCGSOrderOut = 0 // Window is removed from the on-screen window list. } CGSWindowOrderingMode; typedef void *CGSRegion; typedef CGSRegion *CGSRegionRef; typedef CGSWindow *CGSWindowRef; extern CGError CGSNewWindow( CGSConnection cid, int, float, float, const CGSRegion, CGSWindowRef); extern CGError CGSNewRegionWithRect( const CGRect * rect, CGSRegionRef newRegion ); extern OSStatus CGSOrderWindow(CGSConnection cid, CGSWindow win, CGSWindowOrderingMode place, CGSWindow relativeToWindow /* nullable */); extern OSStatus CGSSetWindowProperty(const CGSConnection cid, CGSWindow wid, CGSValue key, CGSValue value); extern CGSConnectionID CGSMainConnectionID(void); extern CGError CGSAddSurface(CGSConnectionID cid, _CGWindowID wid, CGSSurfaceID *sid); extern CGError CGSSetSurfaceBounds(CGSConnectionID cid, _CGWindowID wid, CGSSurfaceID sid, CGRect rect); extern CGError CGSOrderSurface(CGSConnectionID cid, _CGWindowID wid, CGSSurfaceID sid, int a, int b); extern OSStatus CGSMoveWindow(const CGSConnection cid, const CGSWindow wid, CGPoint *point); extern CGLError CGLSetSurface(CGLContextObj gl, CGSConnectionID cid, CGSWindowID wid, CGSSurfaceID sid); extern Boolean ConvertEventRefToEventRecord(EventRef inEvent, EventRecord * outEvent); } #define kCGSBufferedBackingType 2 int main () { CGLContextObj cgl_context = NULL; CGSWindow window = 0; int width = 500, height = 500; CGPoint window_pos = { .x = 200, .y = 200 }; bool mouse_is_down = false; bool quit = false; CGSConnectionID connection_id = CGSMainConnectionID(); assert(connection_id); { CGSRegion region = NULL; CGRect r = CGRectMake(0,0, width, height); auto err1 = CGSNewRegionWithRect(&r, ®ion); assert(region); auto err2 = CGSNewWindow(connection_id, kCGSBufferedBackingType, window_pos.x, window_pos.y, region, &window); assert(window); auto err3 = CGSOrderWindow(connection_id, window, kCGSOrderAbove, 0); assert (err3 == kCGErrorSuccess); CGLPixelFormatAttribute attributes[] = { kCGLPFADoubleBuffer, kCGLPFAAccelerated, // Hardware rendering // kCGLPFARendererID, (CGLPixelFormatAttribute) kCGLRendererGenericFloatID, // Software rendering (CGLPixelFormatAttribute)0 }; CGLPixelFormatObj pix; GLint num; auto err4 = CGLChoosePixelFormat(attributes, &pix, &num); assert(err4 == kCGLNoError); // CGLErrorString(err1) assert(pix); CGLCreateContext(pix, NULL, &cgl_context); assert(cgl_context); CGLDestroyPixelFormat(pix); CGLSetCurrentContext(cgl_context); GLint v_sync_enabled = 1; CGLSetParameter(cgl_context, kCGLCPSwapInterval, &v_sync_enabled); CGSSurfaceID surface_id = 0; auto err5 = CGSAddSurface(connection_id, window, &surface_id); assert(err5 == kCGErrorSuccess); auto err6 = CGSSetSurfaceBounds(connection_id, window, surface_id, CGRectMake(0, 0, width, height)); assert(err6 == kCGErrorSuccess); auto err7 = CGSOrderSurface(connection_id, window, surface_id, 1, 0); assert(err7 == kCGErrorSuccess); auto err8 = CGLSetSurface(cgl_context, connection_id, window, surface_id); assert(err8 == kCGLNoError); GLint drawable = 0; CGLGetParameter(cgl_context, kCGLCPHasDrawable, &drawable); assert(drawable == 1); } assert(glGetError() == GL_NO_ERROR); CGPoint drag_starting_position; bool drag_started = false; while (!quit) { { EventTargetRef event_target = GetEventDispatcherTarget(); // No clue what the point of this is EventRef carbon_event_ref; while (ReceiveNextEvent(0, NULL, kEventDurationNoWait ,true, &carbon_event_ref)== noErr) { if (auto cg_event = CopyEventCGEvent(carbon_event_ref)) { char* event_type_string = NULL; auto cg_event_type = CGEventGetType(cg_event); switch (cg_event_type) { case kCGEventLeftMouseDown: { event_type_string = "kCGEventLeftMouseDown"; auto err3 = CGSOrderWindow(connection_id, window, kCGSOrderAbove, 0); assert (err3 == kCGErrorSuccess); mouse_is_down = true; } break; case kCGEventLeftMouseUp: { event_type_string = "kCGEventLeftMouseUp"; mouse_is_down = false; drag_started = false; } break; case kCGEventRightMouseDown: { event_type_string = "kCGEventRightMouseDown"; quit=true; } break; case kCGEventRightMouseUp: { event_type_string = "kCGEventRightMouseUp"; } break; case kCGEventMouseMoved: { event_type_string = "kCGEventMouseMoved"; } break; case kCGEventLeftMouseDragged: { event_type_string = "kCGEventLeftMouseDragged"; if (!drag_started) { drag_started = true; drag_starting_position = CGEventGetLocation(cg_event); } CGPoint mouse_pos = CGEventGetLocation(cg_event); window_pos.x = window_pos.x + mouse_pos.x - drag_starting_position.x; window_pos.y = window_pos.y + mouse_pos.y - drag_starting_position.y; drag_starting_position = CGEventGetLocation(cg_event); auto err = CGSMoveWindow(connection_id, window, &window_pos); } break; case kCGEventRightMouseDragged: { event_type_string = "kCGEventRightMouseDragged"; } break; case kCGEventKeyDown: { event_type_string = "kCGEventKeyDown"; } break; case kCGEventKeyUp: { event_type_string = "kCGEventKeyUp"; } break; case kCGEventFlagsChanged: { event_type_string = "kCGEventFlagsChanged"; } break; case kCGEventScrollWheel: { event_type_string = "kCGEventScrollWheel"; } break; case kCGEventTabletPointer: { event_type_string = "kCGEventTabletPointer"; } break; case kCGEventTabletProximity: { event_type_string = "kCGEventTabletProximity"; } break; case kCGEventOtherMouseDown: { event_type_string = "kCGEventOtherMouseDown"; } break; case kCGEventOtherMouseUp: { event_type_string = "kCGEventOtherMouseUp"; } break; case kCGEventOtherMouseDragged: { event_type_string = "kCGEventOtherMouseDragged"; } break; case kCGEventTapDisabledByTimeout: { event_type_string = "kCGEventTapDisabledByTimeout"; } break; case kCGEventTapDisabledByUserInput: { event_type_string = "kCGEventTapDisabledByUserInput"; } break; default: { event_type_string = "UNKNOWN"; } break; } printf("CG:Y %llu %s:%d ", CGEventGetTimestamp(cg_event), event_type_string, cg_event_type); CFRelease(cg_event); } else { printf("CG:N "); } char* what_str = NULL; EventRecord carbon_event; if (bool conv = ConvertEventRefToEventRecord(carbon_event_ref, &carbon_event)) { switch (carbon_event.what) { case nullEvent: what_str = "nullEvent"; break; case mouseDown: what_str = "mouseDown"; break; case mouseUp: what_str = "mouseUp"; break; case keyDown: what_str = "keyDown"; break; case autoKey: what_str = "autoKey"; break; case updateEvt: what_str = "updateEvt"; break; case diskEvt: what_str = "diskEvt"; break; case activateEvt: what_str = "activateEvt"; break; case osEvt: what_str = "osEvt"; break; case kHighLevelEvent: what_str = "kHighLevelEvent"; break; default: what_str = "UNKNOWN"; break; }; printf("evt: %u %s:%hu", carbon_event.when, what_str, carbon_event.what); if(conv && (carbon_event.what == kHighLevelEvent)) AEProcessAppleEvent(&carbon_event); } SendEventToEventTarget (carbon_event_ref, event_target); ReleaseEvent(carbon_event_ref); printf("\n"); } } glClearColor(mouse_is_down?1:0,1,0,1); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); static float a = 0; glRotatef(a * 1000, 0, 0, 1); // printf("a: %f\n", a); a= a + .001; glBegin(GL_QUADS); if (a>1.5) a=0; glColor4f(0,a,1,1); glVertex2f(0.25, 0.25); glVertex2f(0.75, 0.25); glVertex2f(0.75, 0.75); glVertex2f(0.25, 0.75); glEnd(); auto err1 = CGLFlushDrawable(cgl_context); assert(err1 == kCGLNoError); assert(glGetError() == GL_NO_ERROR); } CGLSetCurrentContext(NULL); CGLDestroyContext(cgl_context); } |
There is still a bit more needed in order to be a well behaved OS X app...