示例#1
0
int SDL_main(int argc, char *argv[])
{
  int i;
 
#ifdef __APPLE__
  void * pool = objc_msgSend((id)objc_lookUpClass("NSAutoreleasePool"), sel_getUid("alloc"));
  objc_msgSend(pool, sel_getUid("init"));
#endif
 
  hs_init(&argc, &argv);

#ifdef __GLASGOW_HASKELL__
  hs_add_root(__stginit_RectanglesMain);
#endif
 
  rectangles_main();
 
  hs_exit();

#ifdef __APPLE__
  objc_msgSend(pool, sel_getUid("release"));
#endif
  
  return 0;
}
示例#2
0
void ProcessImageLoad(const struct mach_header* mh, intptr_t slide)
{
	unsigned long size;
	std::vector<const char*> classNames;
	void* dataPtr = nullptr;

#ifdef OBJC_ABI_2
	const class_t** classes;
	ProcessProtocolsNew(mh, slide);

	classes = reinterpret_cast<const class_t**>(
		getsectdata(mh, SEG_OBJC_CLASSLIST_NEW, SECT_OBJC_CLASSLIST_NEW, &size)
	);
	if (classes)
	{
		classNames = ProcessClassesNew(mh, slide, classes, size);
		dataPtr = (void*)*classes;
	}

	ProcessCategoriesNew(mh, slide);
#else
	module_info* modinfo;
	ProcessProtocolsOld(mh, slide);

	modinfo = reinterpret_cast<module_info*>(
		getsectdata(mh, SEG_OBJC_MODINFO_OLD, SECT_OBJC_MODINFO_OLD, &size)
	);

	if (modinfo && modinfo->symtab)
	{
		classNames = ProcessClassesOld(mh, slide, modinfo);
		dataPtr = modinfo;
		ProcessCategoriesOld(mh, slide, modinfo);
	}
#endif

	UpdateClassRefs(mh);
	UpdateSelectors(mh, slide);

	// Do this only for dynamic libs (slide > 0)
	if (!classNames.empty() && slide > 0)
	{
		// Generate a NSFramework_XXXX class for GNUstep's NSBundle
		const char* path = dyld_image_path_containing_address(dataPtr);
		assert(path != nullptr);
		RegisterFramework(&classNames[0], classNames.size(), path);
	}

	static SEL selInit = sel_getUid("load");
	while (!g_pendingInitClasses.empty())
	{
		auto pair = g_pendingInitClasses.front();
		g_pendingInitClasses.pop();
		pair.second(reinterpret_cast<objc_object*>(pair.first), selInit);
	}
}
示例#3
0
void UpdateSelectors(const struct mach_header* mh, intptr_t slide)
{
	selref* sel_refs;
	msgref* msg_refs;
	unsigned long selsize, msgsize;

	sel_refs = reinterpret_cast<selref*>(
		getsectdata(mh, SEG_OBJC_SELREFS_NEW, SECT_OBJC_SELREFS_NEW, &selsize)
	);
	
	msg_refs = reinterpret_cast<msgref*>(
		getsectdata(mh, SEG_OBJC_MSGREFS_NEW, SECT_OBJC_MSGREFS_NEW, &msgsize)
	);
	
	if (!sel_refs)
	{
		sel_refs = reinterpret_cast<selref*>(
			getsectdata(mh, SEG_OBJC_SELREFS_OLD, SECT_OBJC_SELREFS_OLD, &selsize)
		);
	}

	if (sel_refs)
	{
		for (size_t i = 0; i < selsize / sizeof(selref); i++)
		{
			SEL native = sel_getUid(sel_refs[i].selName);
			LOG << "ObjC SEL fixup @" << (sel_refs+i) << ": " << sel_refs[i].sel << " -> " << native << std::endl;
			sel_refs[i].sel = native;
		}
	}
	if (msg_refs)
	{
		for (size_t i = 0; i < msgsize / sizeof(msgref); i++)
		{
			SEL native = sel_getUid(msg_refs[i].sel.selName);
			LOG << "ObjC msgref fixup @" << &msg_refs[i].sel.sel << ": " << msg_refs[i].sel.sel << " -> " << native << std::endl;
			msg_refs[i].sel.sel = native;
		}
	}
}
	bool NSGLWindow::loop()
	{
		// TODO : Can we pump events manually ?
		//NSEvent* event;
		id event;
		do
		{
// 			event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast]
// 					 inMode:NSDefaultRunLoopMode dequeue:YES];
			event = cmacs_next_event_msgSend(NSApp, sel_getUid("nextEventMatchingMask"), 0, 0, true);
            //Convert the cocoa events to something useful here and add them to your own event queue

			//[NSApp sendEvent: event];
		}
		while (event != nil);
		return true;
	}
示例#5
0
void ProcessImageLoad(const struct mach_header* mh, intptr_t slide)
{
	unsigned long size;

#ifdef OBJC_ABI_2
	const class_t** classes;
	ProcessProtocolsNew(mh, slide);

	classes = reinterpret_cast<const class_t**>(
		getsectdata(mh, SEG_OBJC_CLASSLIST_NEW, SECT_OBJC_CLASSLIST_NEW, &size)
	);
	if (classes)
		ProcessClassesNew(mh, slide, classes, size);

	ProcessCategoriesNew(mh, slide);
#else
	module_info* modinfo;
	ProcessProtocolsOld(mh, slide);

	modinfo = reinterpret_cast<module_info*>(
		getsectdata(mh, SEG_OBJC_MODINFO_OLD, SECT_OBJC_MODINFO_OLD, &size)
	);

	if (modinfo && modinfo->symtab)
	{
		ProcessClassesOld(mh, slide, modinfo);
		ProcessCategoriesOld(mh, slide, modinfo);
	}
#endif

	UpdateClassRefs(mh);
	UpdateSelectors(mh, slide);

	static SEL selInit = sel_getUid("load");
	while (!g_pendingInitClasses.empty())
	{
		auto pair = g_pendingInitClasses.front();
		g_pendingInitClasses.pop();
		pair.second(reinterpret_cast<objc_object*>(pair.first), selInit);
	}
}
示例#6
0
WEAK void __halide_print(void *user_context, const char *str) {
    // Buy an autorelease pool because this is not perf critical and it is the
    // really safe thing to do.
    objc_id pool =
        objc_msgSend(objc_msgSend(objc_getClass("NSAutoreleasePool"),
                                  sel_getUid("alloc")), sel_getUid("init"));
    objc_id ns_str =
        objc_msgSend(objc_msgSend(objc_getClass("NSString"),
                                  sel_getUid("alloc")),
                     sel_getUid("initWithUTF8String:"), str);

    NSLog(ns_str);

    objc_msgSend(ns_str, sel_getUid("release"));

    objc_msgSend(pool, sel_getUid("drain"));
}
示例#7
0
SEL sel_registerName(const char *cString) {
    return sel_getUid(cString);
}
	bool NSGLWindow::initialize()
	{
		// Strong reference to the class of the AppDelegate (same as [AppDelegate class])
		static Class appDelClass = nullptr;
		if (not appDelClass)
		{
			appDelClass = objc_allocateClassPair((Class)objc_getClass("NSObject"), "AppDelegate", 0);
			class_addMethod(appDelClass, sel_getUid("applicationDidFinishLaunching:"), (IMP)OnAppDidFinishLaunching, "i@:@");
			objc_registerClassPair(appDelClass);
		}

		// Tell the runtime to create a new class, a subclass of 'NSOpenGLView' named 'YView'.
		static Class viewClass = nullptr;
		if (not viewClass)
		{
			viewClass = objc_allocateClassPair((Class)objc_getClass("NSOpenGLView"), "YView", 0);

			// Tell the runtime to add functions for various events to our custom view.
			class_addMethod(viewClass, sel_getUid("drawRect:"), (IMP)NSGLWindow::OnDrawRect, "v@:");
			class_addMethod(viewClass, sel_getUid("mouseDown:"), (IMP)NSGLWindow::OnMouseDown, "v@:");
			//class_addMethod(viewClass, sel_getUid("close:"), (IMP)NSGLWindow::OnWindowClose, "v@:");

			// And again, we tell the runtime that this class is now valid to be used.
			// At this point, the application should run and display the screenshot shown below.
			objc_registerClassPair(viewClass);
		}

		cmacs_simple_msgSend((id)objc_getClass("NSApplication"), sel_getUid("sharedApplication"));
		if (not NSApp)
		{
			std::cerr << "Failed to initialized NSApplication...  terminating..." << std::endl;
			return false;
		}

		// Create the app delegate
		static AppDelegate* appDelObj = nullptr;
		if (not appDelObj)
		{
			appDelObj = (AppDelegate*)cmacs_simple_msgSend((id)objc_getClass("AppDelegate"), sel_getUid("alloc"));
			appDelObj = (AppDelegate*)cmacs_simple_msgSend((id)appDelObj, sel_getUid("init"));

			cmacs_void_msgSend1(NSApp, sel_getUid("setDelegate:"), appDelObj);

			// Launch main loop
			//cmacs_void_msgSend(NSApp, sel_getUid("run"));
		}

		appDelObj->window = cmacs_simple_msgSend((id)objc_getClass("NSOpenGLWindow"), sel_getUid("alloc"));

		/// Create an instance of the window.
		appDelObj->window = cmacs_window_init_msgSend(appDelObj->window, sel_getUid("initWithContentRect:styleMask:backing:defer:"), (CMRect){ {0,0},{1024,460}},
			(NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask | NSMiniaturizableWindowMask), 0, false);
	    
		/// Create an instance of our view class.
		///
		/// Relies on the view having declared a constructor that allocates a class pair for it.
		id view = cmacs_rect_msgSend1(cmacs_simple_msgSend((id)objc_getClass("YView"), sel_getUid("alloc")), sel_getUid("initWithFrame:"), (CMRect){ {0, 0}, {640, 480} });

		// here we simply add the view to the window.
		cmacs_void_msgSend1(appDelObj->window, sel_getUid("setContentView:"), view);
		cmacs_simple_msgSend(appDelObj->window, sel_getUid("becomeFirstResponder"));

		// Shows our window in the bottom-left hand corner of the screen.
		//cmacs_void_msgSend1(appDelObj->window, sel_getUid("makeKeyAndOrderFront:"), appDelObj);

		return true;
	}
示例#9
0
void call_objc_obj(REAL mflops) {
    objc_msgSend(my_objc_obj, sel_getUid("updateFlops:"), mflops);	
}