Did a flat import of OS X files for now. Need to really clean this up.

This commit is contained in:
Pietro Gagliardi 2015-04-28 13:49:20 -04:00
parent 123db608cd
commit ca487ad18d
7 changed files with 247 additions and 0 deletions

18
new/darwin/GNUmakeinc.mk Normal file
View File

@ -0,0 +1,18 @@
# 28 april 2015
osMFILES = \
darwin/alloc.m \
darwin/init.m \
darwin/main.m \
darwin/menu.m \
darwin/text.m \
darwin/util.m
osHFILES = \
darwin/uipriv_darwin.h
osCFLAGS = -mmacosx-version-min=10.7 -DMACOSX_DEPLOYMENT_TARGET=10.7
osLDFLAGS = -mmacosx-version-min=10.7 -lobjc -framework Foundation -framework AppKit
osLIBSUFFIX = .dylib
osEXESUFFIX =

44
new/darwin/alloc.m Normal file
View File

@ -0,0 +1,44 @@
// 4 december 2014
#import <stdio.h>
#import "uipriv_darwin.h"
void *uiAlloc(size_t size, const char *type)
{
void *out;
out = malloc(size);
if (out == NULL) {
fprintf(stderr, "memory exhausted in uiAlloc() allocating %s\n", type);
abort();
}
memset(out, 0, size);
if (options.debugLogAllocations)
fprintf(stderr, "%p alloc %s\n", out, type);
return out;
}
void *uiRealloc(void *p, size_t size, const char *type)
{
void *out;
if (p == NULL)
return uiAlloc(size, type);
out = realloc(p, size);
if (out == NULL) {
fprintf(stderr, "memory exhausted in uiRealloc() reallocating %s\n", type);
abort();
}
// TODO zero the extra memory
if (options.debugLogAllocations)
fprintf(stderr, "%p realloc %p\n", p, out);
return out;
}
void uiFree(void *p)
{
if (p == NULL)
return;
free(p);
if (options.debugLogAllocations)
fprintf(stderr, "%p free\n", p);
}

70
new/darwin/init.m Normal file
View File

@ -0,0 +1,70 @@
// 6 april 2015
#import "uipriv_darwin.h"
@interface uiApplication : NSApplication
@end
@implementation uiApplication
// hey look! we're overriding terminate:!
// we're going to make sure we can go back to main() whether Cocoa likes it or not!
// and just how are we going to do that, hm?
// (note: this is called after applicationShouldTerminate:)
- (void)terminate:(id)sender
{
// yes that's right folks: DO ABSOLUTELY NOTHING.
// the magic is [NSApp run] will just... stop.
// for debugging
NSLog(@"in terminate:");
}
@end
@interface uiAppDelegate : NSObject <NSApplicationDelegate>
@end
@implementation uiAppDelegate
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app
{
// for debugging
NSLog(@"in applicationShouldTerminate:");
return NSTerminateNow;
}
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)app
{
return NO;
}
@end
// we are not in control of the actual lifetimes and refcounts of NSViews (see http://stackoverflow.com/a/29523141/3408572)
// when we're done with a view, it'll be added as a subview of this one, and this one will be released on application shutdown
// we need this separate view because it's possible for controls to have no parent but still be alive
NSView *destroyedControlsView;
uiInitOptions options;
const char *uiInit(uiInitOptions *o)
{
options = *o;
[uiApplication sharedApplication];
// don't check for a NO return; something (launch services?) causes running from application bundles to always return NO when asking to change activation policy, even if the change is to the same activation policy!
// see https://github.com/andlabs/ui/issues/6
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp setDelegate:[uiAppDelegate new]];
// always do this so we always have an application menu
[NSApp setMainMenu:makeMenubar()];
// we can use a stock NSView for this
destroyedControlsView = [[NSView alloc] initWithFrame:NSZeroRect];
return NULL;
}
void uiFreeInitError(const char *err)
{
}

25
new/darwin/main.m Normal file
View File

@ -0,0 +1,25 @@
// 6 april 2015
#import "uipriv_darwin.h"
void uiMain(void)
{
[NSApp run];
}
void uiQuit(void)
{
NSEvent *e;
[NSApp stop:NSApp];
// stop: won't register until another event has passed; let's synthesize one
e = [NSEvent otherEventWithType:NSApplicationDefined
location:NSZeroPoint
modifierFlags:0
timestamp:[[NSProcessInfo processInfo] systemUptime]
windowNumber:0
context:[NSGraphicsContext currentContext]
subtype:0
data1:0
data2:0];
[NSApp postEvent:e atStart:NO]; // let pending events take priority (this is what PostQuitMessage() on Windows does so we have to do it here too for parity; thanks to mikeash in irc.freenode.net/#macdev for confirming that this parameter should indeed be NO)
}

19
new/darwin/text.m Normal file
View File

@ -0,0 +1,19 @@
// 10 april 2015
#import "uipriv_darwin.h"
char *uiDarwinNSStringToText(NSString *s)
{
char *out;
out = strdup([s UTF8String]);
if (out == NULL) {
fprintf(stderr, "memory exhausted in uiDarwinNSStringToText()\n");
abort();
}
return out;
}
void uiFreeText(char *s)
{
free(s);
}

View File

@ -0,0 +1,46 @@
// 6 january 2015
#define MAC_OS_X_VERSION_MIN_REQUIRED MAC_OS_X_VERSION_10_7
#define MAC_OS_X_VERSION_MAX_ALLOWED MAC_OS_X_VERSION_10_7
#import <Cocoa/Cocoa.h>
#import "../uipriv.h"
#import "../ui_darwin.h"
#define toNSString(str) [NSString stringWithUTF8String:(str)]
#define fromNSString(str) [(str) UTF8String]
#define uiLogObjCClassAllocations \
+ (id)alloc \
{ \
id thing; \
thing = [super alloc]; \
if (options.debugLogAllocations) \
fprintf(stderr, "%p alloc %s\n", thing, [[self className] UTF8String]); \
return thing; \
} \
- (void)dealloc \
{ \
[super dealloc]; \
if (options.debugLogAllocations) \
fprintf(stderr, "%p free\n", self); \
}
#define VIEW(c) uiControlHandle(uiControl(c))
// init.m
extern NSView *destroyedControlsView;
// util.m
extern void setStandardControlFont(NSControl *);
extern void disableAutocorrect(NSTextView *);
// parent.m
// These are based on measurements from Interface Builder.
// These seem to be based on Auto Layout constants, but I don't see an API that exposes these...
#define macXMargin 20
#define macYMargin 20
// entry.m
extern void finishNewTextField(NSTextField *, BOOL);
// menu.m
extern NSMenu *makeMenubar(void);

25
new/darwin/util.m Normal file
View File

@ -0,0 +1,25 @@
// 7 april 2015
#import "uipriv_darwin.h"
// also fine for NSCells and NSTexts (NSTextViews)
void setStandardControlFont(NSControl *control)
{
[control setFont:[NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]]];
}
void disableAutocorrect(NSTextView *tv)
{
[tv setEnabledTextCheckingTypes:0];
[tv setAutomaticDashSubstitutionEnabled:NO];
// don't worry about automatic data detection; it won't change stringValue (thanks pretty_function in irc.freenode.net/#macdev)
[tv setAutomaticSpellingCorrectionEnabled:NO];
[tv setAutomaticTextReplacementEnabled:NO];
[tv setAutomaticQuoteSubstitutionEnabled:NO];
[tv setAutomaticLinkDetectionEnabled:NO];
[tv setSmartInsertDeleteEnabled:NO];
}
void complain(const char *fmt, ...)
{
// TODO
}