Multi copy from other applications

master
Allen Webster 2017-11-09 19:47:57 -05:00
parent bcff1f1f9f
commit d04dd8879b
2 changed files with 325 additions and 313 deletions

View File

@ -695,7 +695,7 @@ CUSTOM_COMMAND_SIG(setup_new_project){
if (sh_script != 0){ if (sh_script != 0){
fprintf(sh_script, "#!/bin/bash\n\n"); fprintf(sh_script, "#!/bin/bash\n\n");
fprintf(sh_script, get_code_home); fprintf(sh_script, "%s", get_code_home);
fprintf(sh_script, "OPTS=%.*s\n", fprintf(sh_script, "OPTS=%.*s\n",
default_flags_sh.size, default_flags_sh.str); default_flags_sh.size, default_flags_sh.str);

View File

@ -43,22 +43,22 @@
void void
osx_post_to_clipboard(char *str){ osx_post_to_clipboard(char *str){
NSPasteboard *board = [NSPasteboard generalPasteboard]; NSPasteboard *board = [NSPasteboard generalPasteboard];
NSString *utf8_type = @"public.utf8-plain-text"; NSString *utf8_type = @"public.utf8-plain-text";
NSArray<NSString*> *typesArray = [NSArray arrayWithObjects: utf8_type, nil]; NSArray<NSString*> *typesArray = [NSArray arrayWithObjects: utf8_type, nil];
[board declareTypes:typesArray owner:nil]; [board declareTypes:typesArray owner:nil];
NSString *paste_string = [NSString stringWithUTF8String:str]; NSString *paste_string = [NSString stringWithUTF8String:str];
[board setString:paste_string forType:utf8_type]; [board setString:paste_string forType:utf8_type];
osx_objc.just_posted_to_clipboard = true; osx_objc.just_posted_to_clipboard = true;
} }
void void
osx_error_dialogue(char *str){ osx_error_dialogue(char *str){
NSAlert *alert = [[NSAlert alloc] init]; NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"]; [alert addButtonWithTitle:@"OK"];
NSString *text = [NSString stringWithUTF8String:str]; NSString *text = [NSString stringWithUTF8String:str];
[alert setMessageText:text]; [alert setMessageText:text];
[alert setAlertStyle:NSCriticalAlertStyle]; [alert setAlertStyle:NSCriticalAlertStyle];
[alert runModal]; [alert runModal];
} }
// //
@ -69,11 +69,12 @@ osx_error_dialogue(char *str){
@end @end
@interface My4coderView : NSOpenGLView{ @interface My4coderView : NSOpenGLView{
@public @public
//CVDisplayLinkRef displayLink; //CVDisplayLinkRef displayLink;
} }
- (void)requestDisplay; - (void)requestDisplay;
- (void)checkClipboard;
- (CVReturn)getFrame; - (CVReturn)getFrame;
- (void)drawRect:(NSRect)bounds; - (void)drawRect:(NSRect)bounds;
@end @end
@ -83,57 +84,57 @@ static DISPLINK_SIG(osx_display_link);
@implementation My4coderView @implementation My4coderView
- (void)keyDown:(NSEvent *)event{ - (void)keyDown:(NSEvent *)event{
NSString *real = [event charactersIgnoringModifiers]; NSString *real = [event charactersIgnoringModifiers];
NSString *with_mods = [event characters]; NSString *with_mods = [event characters];
b32 is_dead_key = false; b32 is_dead_key = false;
if (real && !with_mods){ if (real && !with_mods){
is_dead_key = true; is_dead_key = true;
} }
OSX_Keyboard_Modifiers mods = {0}; OSX_Keyboard_Modifiers mods = {0};
NSEventModifierFlags flags = [NSEvent modifierFlags]; NSEventModifierFlags flags = [NSEvent modifierFlags];
mods.shift = ((flags & NSEventModifierFlagShift) != 0); mods.shift = ((flags & NSEventModifierFlagShift) != 0);
mods.command = ((flags & NSEventModifierFlagCommand) != 0); mods.command = ((flags & NSEventModifierFlagCommand) != 0);
mods.control = ((flags & NSEventModifierFlagControl) != 0); mods.control = ((flags & NSEventModifierFlagControl) != 0);
mods.option = ((flags & NSEventModifierFlagOption) != 0); mods.option = ((flags & NSEventModifierFlagOption) != 0);
mods.caps = ((flags & NSEventModifierFlagCapsLock) != 0); mods.caps = ((flags & NSEventModifierFlagCapsLock) != 0);
u32 length = real.length; u32 length = real.length;
for (u32 i = 0; i < length; ++i){ for (u32 i = 0; i < length; ++i){
unichar c = [real characterAtIndex:i]; unichar c = [real characterAtIndex:i];
osx_character_input(c, mods); osx_character_input(c, mods);
} }
} }
- (void)mouseDown:(NSEvent*)event{ - (void)mouseDown:(NSEvent*)event{
NSPoint m = [event locationInWindow]; NSPoint m = [event locationInWindow];
osx_mouse(m.x, m.y, MouseType_Press); osx_mouse(m.x, m.y, MouseType_Press);
} }
- (void)mouseDragged:(NSEvent*)event{ - (void)mouseDragged:(NSEvent*)event{
NSPoint m = [event locationInWindow]; NSPoint m = [event locationInWindow];
osx_mouse(m.x, m.y, MouseType_Move); osx_mouse(m.x, m.y, MouseType_Move);
} }
- (void)mouseMoved:(NSEvent*)event{ - (void)mouseMoved:(NSEvent*)event{
NSPoint m = [event locationInWindow]; NSPoint m = [event locationInWindow];
osx_mouse(m.x, m.y, MouseType_Move); osx_mouse(m.x, m.y, MouseType_Move);
} }
- (void)mouseUp:(NSEvent*)event{ - (void)mouseUp:(NSEvent*)event{
NSPoint m = [event locationInWindow]; NSPoint m = [event locationInWindow];
osx_mouse(m.x, m.y, MouseType_Release); osx_mouse(m.x, m.y, MouseType_Release);
} }
- (void)scrollWheel:(NSEvent*)event{ - (void)scrollWheel:(NSEvent*)event{
float dx = event.scrollingDeltaX; float dx = event.scrollingDeltaX;
float dy = event.scrollingDeltaY; float dy = event.scrollingDeltaY;
osx_mouse_wheel(dx, dy); osx_mouse_wheel(dx, dy);
} }
- (BOOL)windowShouldClose:(NSWindow*)sender{ - (BOOL)windowShouldClose:(NSWindow*)sender{
osx_try_to_close(); osx_try_to_close();
return(NO); return(NO);
} }
- (void)requestDisplay{ - (void)requestDisplay{
@ -141,45 +142,56 @@ static DISPLINK_SIG(osx_display_link);
[self setNeedsDisplayInRect:rect]; [self setNeedsDisplayInRect:rect];
} }
static i32 did_update_for_clipboard = true;
- (void)checkClipboard{
NSPasteboard *board = [NSPasteboard generalPasteboard];
if (board.changeCount != osx_objc.prev_clipboard_change_count && did_update_for_clipboard){
[self requestDisplay];
did_update_for_clipboard = false;
}
}
- (CVReturn)getFrame{ - (CVReturn)getFrame{
did_update_for_clipboard = true;
@autoreleasepool @autoreleasepool
{ {
if (osx_objc.running){ if (osx_objc.running){
osx_objc.has_clipboard_item = false; osx_objc.has_clipboard_item = false;
NSPasteboard *board = [NSPasteboard generalPasteboard]; NSPasteboard *board = [NSPasteboard generalPasteboard];
if (board.changeCount != osx_objc.prev_clipboard_change_count){ if (board.changeCount != osx_objc.prev_clipboard_change_count){
if (!osx_objc.just_posted_to_clipboard){ if (!osx_objc.just_posted_to_clipboard){
NSString *utf8_type = @"public.utf8-plain-text"; NSString *utf8_type = @"public.utf8-plain-text";
NSArray *array = [NSArray arrayWithObjects: utf8_type, nil]; NSArray *array = [NSArray arrayWithObjects: utf8_type, nil];
NSString *has_string = [board availableTypeFromArray:array]; NSString *has_string = [board availableTypeFromArray:array];
if (has_string != nil){ if (has_string != nil){
NSData *data = [board dataForType: utf8_type]; NSData *data = [board dataForType: utf8_type];
if (data != nil){ if (data != nil){
u32 copy_length = data.length; u32 copy_length = data.length;
if (copy_length > 0){ if (copy_length > 0){
// TODO(allen): Grow clipboard memory if needed. // TODO(allen): Grow clipboard memory if needed.
if (copy_length+1 < osx_objc.clipboard_max){ if (copy_length+1 < osx_objc.clipboard_max){
osx_objc.clipboard_size = copy_length; osx_objc.clipboard_size = copy_length;
[data getBytes: osx_objc.clipboard_data length: copy_length]; [data getBytes: osx_objc.clipboard_data length: copy_length];
((char*)osx_objc.clipboard_data)[copy_length] = 0; ((char*)osx_objc.clipboard_data)[copy_length] = 0;
osx_objc.has_clipboard_item = true; osx_objc.has_clipboard_item = true;
} }
} }
} }
} }
} }
else{ else{
osx_objc.just_posted_to_clipboard = false; osx_objc.just_posted_to_clipboard = false;
} }
osx_objc.prev_clipboard_change_count = board.changeCount; osx_objc.prev_clipboard_change_count = board.changeCount;
} }
CGLLockContext([[self openGLContext] CGLContextObj]); CGLLockContext([[self openGLContext] CGLContextObj]);
[[self openGLContext] makeCurrentContext]; [[self openGLContext] makeCurrentContext];
osx_step(); osx_step();
[[self openGLContext] flushBuffer]; [[self openGLContext] flushBuffer];
CGLUnlockContext([[self openGLContext] CGLContextObj]); CGLUnlockContext([[self openGLContext] CGLContextObj]);
} }
} }
return kCVReturnSuccess; return kCVReturnSuccess;
} }
@ -187,7 +199,7 @@ static DISPLINK_SIG(osx_display_link);
- (void)reshape - (void)reshape
{ {
[super reshape]; [super reshape];
NSRect rect = [self bounds]; NSRect rect = [self bounds];
osx_resize(rect.size.width, rect.size.height); osx_resize(rect.size.width, rect.size.height);
} }
@ -197,35 +209,35 @@ static DISPLINK_SIG(osx_display_link);
if(osx_objc.running){ if(osx_objc.running){
return; return;
} }
[self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; [self setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
NSOpenGLPixelFormatAttribute attrs[] = { NSOpenGLPixelFormatAttribute attrs[] = {
NSOpenGLPFAOpenGLProfile, NSOpenGLPFAOpenGLProfile,
//NSOpenGLProfileVersion3_2Core, //NSOpenGLProfileVersion3_2Core,
NSOpenGLProfileVersionLegacy, NSOpenGLProfileVersionLegacy,
NSOpenGLPFAColorSize, NSOpenGLPFAColorSize,
24, 24,
NSOpenGLPFAAlphaSize, NSOpenGLPFAAlphaSize,
8, 8,
NSOpenGLPFAAccelerated, NSOpenGLPFAAccelerated,
NSOpenGLPFADoubleBuffer, NSOpenGLPFADoubleBuffer,
0 0
}; };
NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs]; NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
if(format == nil){ if(format == nil){
fprintf(stderr, "Error creating OpenGLPixelFormat\n"); fprintf(stderr, "Error creating OpenGLPixelFormat\n");
exit(1); exit(1);
} }
NSOpenGLContext* context = [[NSOpenGLContext alloc] initWithFormat:format shareContext:nil]; NSOpenGLContext* context = [[NSOpenGLContext alloc] initWithFormat:format shareContext:nil];
[self setPixelFormat:format]; [self setPixelFormat:format];
[self setOpenGLContext:context]; [self setOpenGLContext:context];
[context makeCurrentContext]; [context makeCurrentContext];
osx_objc.running = true; osx_objc.running = true;
} }
@ -236,7 +248,7 @@ static DISPLINK_SIG(osx_display_link);
{ {
return nil; return nil;
} }
[self init_gl]; [self init_gl];
return self; return self;
} }
@ -253,9 +265,9 @@ static DISPLINK_SIG(osx_display_link);
- (void)prepareOpenGL - (void)prepareOpenGL
{ {
[super prepareOpenGL]; [super prepareOpenGL];
[[self openGLContext] makeCurrentContext]; [[self openGLContext] makeCurrentContext];
GLint swapInt = 1; GLint swapInt = 1;
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
} }
@ -310,18 +322,18 @@ typedef struct File_Table_Entry{
} File_Table_Entry; } File_Table_Entry;
typedef struct File_Change_Queue{ typedef struct File_Change_Queue{
i32 kq; i32 kq;
File_Table_Entry *table; File_Table_Entry *table;
i32 table_count; i32 table_count;
i32 table_size; i32 table_size;
} File_Change_Queue; } File_Change_Queue;
static File_Change_Queue file_change_queue = {0}; static File_Change_Queue file_change_queue = {0};
void* void*
osx_file_name_prefixed_length(char *name){ osx_file_name_prefixed_length(char *name){
i32 len = 0; i32 len = 0;
for (; name[len] != 0; ++len); for (; name[len] != 0; ++len);
char *name_stored = (char*)malloc(4 + l_round_up_u32(len, 4)); char *name_stored = (char*)malloc(4 + l_round_up_u32(len, 4));
*(i32*)name_stored = len; *(i32*)name_stored = len;
@ -331,181 +343,181 @@ osx_file_name_prefixed_length(char *name){
b32 b32
osx_name_prefixed_match(void *a, void *b){ osx_name_prefixed_match(void *a, void *b){
b32 result = false; b32 result = false;
i32 *len_a = (i32*)a; i32 *len_a = (i32*)a;
i32 *len_b = (i32*)b; i32 *len_b = (i32*)b;
if (*len_a == *len_b){ if (*len_a == *len_b){
char *str_a = (char*)(len_a + 1); char *str_a = (char*)(len_a + 1);
char *str_b = (char*)(len_b + 1); char *str_b = (char*)(len_b + 1);
if (strncmp(str_a, str_b, *len_a) == 0){ if (strncmp(str_a, str_b, *len_a) == 0){
result = true; result = true;
} }
} }
return(result); return(result);
} }
File_Table_Entry* File_Table_Entry*
osx_file_listener_lookup_and_return_pointer(u64 hash, void *name){ osx_file_listener_lookup_and_return_pointer(u64 hash, void *name){
File_Table_Entry *result = 0; File_Table_Entry *result = 0;
i32 index = (i32)(hash % file_change_queue.table_size); i32 index = (i32)(hash % file_change_queue.table_size);
i32 first_index = index; i32 first_index = index;
for (;;){ for (;;){
File_Table_Entry *entry = &file_change_queue.table[index]; File_Table_Entry *entry = &file_change_queue.table[index];
if (entry->hash == hash){ if (entry->hash == hash){
if (osx_name_prefixed_match(name, entry->name)){ if (osx_name_prefixed_match(name, entry->name)){
result = entry; result = entry;
break; break;
} }
} }
if (entry->name == 0){ if (entry->name == 0){
break; break;
} }
index = (index + 1)%file_change_queue.table_size; index = (index + 1)%file_change_queue.table_size;
if (index == first_index){ if (index == first_index){
break; break;
} }
} }
return(result); return(result);
} }
b32 b32
osx_file_listener_lookup(u64 hash, void *name, i32 *fd_out){ osx_file_listener_lookup(u64 hash, void *name, i32 *fd_out){
b32 found = false; b32 found = false;
File_Table_Entry *entry = osx_file_listener_lookup_and_return_pointer(hash, name); File_Table_Entry *entry = osx_file_listener_lookup_and_return_pointer(hash, name);
if (entry != 0){ if (entry != 0){
found = true; found = true;
if (fd_out != 0){ if (fd_out != 0){
*fd_out = entry->fd; *fd_out = entry->fd;
} }
} }
return(found); return(found);
} }
b32 b32
osx_file_listener_lookup_and_delete(u64 hash, void *name, i32 *fd_out){ osx_file_listener_lookup_and_delete(u64 hash, void *name, i32 *fd_out){
b32 found = false; b32 found = false;
File_Table_Entry *entry = osx_file_listener_lookup_and_return_pointer(hash, name); File_Table_Entry *entry = osx_file_listener_lookup_and_return_pointer(hash, name);
if (entry != 0){ if (entry != 0){
found = true; found = true;
if (fd_out != 0){ if (fd_out != 0){
*fd_out = entry->fd; *fd_out = entry->fd;
} }
memset(entry, 0, sizeof(*entry)); memset(entry, 0, sizeof(*entry));
entry->name = (void*)1; entry->name = (void*)1;
} }
return(found); return(found);
} }
typedef struct Hash_Result{ typedef struct Hash_Result{
b32 is_in_table; b32 is_in_table;
b32 was_already_in_table; b32 was_already_in_table;
} Hash_Result; } Hash_Result;
u64 u64
osx_file_hash(void *name){ osx_file_hash(void *name){
u32 count = *(u32*)(name); u32 count = *(u32*)(name);
char *str = (char*)name + 4; char *str = (char*)name + 4;
u64 hash = 0; u64 hash = 0;
u64 state = count; u64 state = count;
u64 inc = 1 + 2*count; u64 inc = 1 + 2*count;
for (u32 i = 0; i <= count; ++i){ for (u32 i = 0; i <= count; ++i){
u64 old_state = state; u64 old_state = state;
state = state*6364136223846783005ULL + inc; state = state*6364136223846783005ULL + inc;
u32 xorshifted = ((old_state >> 18u) ^ old_state) >> 27u; u32 xorshifted = ((old_state >> 18u) ^ old_state) >> 27u;
u32 rot = old_state >> 59u; u32 rot = old_state >> 59u;
hash = (hash << 3) + (hash & 1) + ((xorshifted >> rot) | (xorshifted << ((-rot) & 31))); hash = (hash << 3) + (hash & 1) + ((xorshifted >> rot) | (xorshifted << ((-rot) & 31)));
if (i < count){ if (i < count){
inc = 1 + 2*(((inc - 1) << 7) | (u8)str[i]); inc = 1 + 2*(((inc - 1) << 7) | (u8)str[i]);
} }
} }
return(hash); return(hash);
} }
Hash_Result Hash_Result
osx_file_listener_hash(u64 hash, void *name, i32 fd){ osx_file_listener_hash(u64 hash, void *name, i32 fd){
Hash_Result result = {0}; Hash_Result result = {0};
if (osx_file_listener_lookup(hash, name, 0)){ if (osx_file_listener_lookup(hash, name, 0)){
result.is_in_table = true; result.is_in_table = true;
result.was_already_in_table = true; result.was_already_in_table = true;
} }
else if (file_change_queue.table_count * 6 < file_change_queue.table_size * 5){ else if (file_change_queue.table_count * 6 < file_change_queue.table_size * 5){
i32 index = (i32)(hash % file_change_queue.table_size); i32 index = (i32)(hash % file_change_queue.table_size);
i32 first_index = index; i32 first_index = index;
for (;;){ for (;;){
File_Table_Entry *entry = &file_change_queue.table[index]; File_Table_Entry *entry = &file_change_queue.table[index];
if (entry->name == 0 || entry->name == (void*)1){ if (entry->name == 0 || entry->name == (void*)1){
entry->hash = hash; entry->hash = hash;
entry->name = name; entry->name = name;
entry->fd = fd; entry->fd = fd;
result.is_in_table = true; result.is_in_table = true;
++file_change_queue.table_count; ++file_change_queue.table_count;
break; break;
} }
index = (index + 1)%file_change_queue.table_size; index = (index + 1)%file_change_queue.table_size;
if (index == first_index){ if (index == first_index){
break; break;
} }
} }
if (!result.is_in_table){ if (!result.is_in_table){
fprintf(stdout, "file change listener table error: could not find a free slot in the table\n"); fprintf(stdout, "file change listener table error: could not find a free slot in the table\n");
} }
} }
return(result); return(result);
} }
Hash_Result Hash_Result
osx_file_listener_hash_bundled(File_Table_Entry entry){ osx_file_listener_hash_bundled(File_Table_Entry entry){
Hash_Result result = osx_file_listener_hash(entry.hash, entry.name, entry.fd); Hash_Result result = osx_file_listener_hash(entry.hash, entry.name, entry.fd);
return(result); return(result);
} }
void void
osx_file_listener_grow_table(i32 table_size){ osx_file_listener_grow_table(i32 table_size){
if (file_change_queue.table_size < table_size){ if (file_change_queue.table_size < table_size){
File_Table_Entry *old_table = file_change_queue.table; File_Table_Entry *old_table = file_change_queue.table;
i32 old_size = file_change_queue.table_size; i32 old_size = file_change_queue.table_size;
file_change_queue.table = (File_Table_Entry*)osx_allocate(table_size*sizeof(File_Table_Entry)); file_change_queue.table = (File_Table_Entry*)osx_allocate(table_size*sizeof(File_Table_Entry));
memset(file_change_queue.table, 0, table_size*sizeof(File_Table_Entry)); memset(file_change_queue.table, 0, table_size*sizeof(File_Table_Entry));
file_change_queue.table_size = table_size; file_change_queue.table_size = table_size;
for (i32 i = 0; i < old_size; ++i){ for (i32 i = 0; i < old_size; ++i){
void *name = file_change_queue.table[i].name; void *name = file_change_queue.table[i].name;
if (name != 0 && name != (void*)1){ if (name != 0 && name != (void*)1){
osx_file_listener_hash_bundled(file_change_queue.table[i]); osx_file_listener_hash_bundled(file_change_queue.table[i]);
} }
} }
if (old_table != 0){ if (old_table != 0){
osx_free(old_table, old_size*sizeof(File_Table_Entry)); osx_free(old_table, old_size*sizeof(File_Table_Entry));
} }
} }
} }
void void
osx_file_listener_double_table(){ osx_file_listener_double_table(){
osx_file_listener_grow_table(file_change_queue.table_size*2); osx_file_listener_grow_table(file_change_queue.table_size*2);
} }
b32 b32
osx_file_listener_hash_always(u64 hash, void *name, i32 fd){ osx_file_listener_hash_always(u64 hash, void *name, i32 fd){
b32 was_already_in_table = false; b32 was_already_in_table = false;
Hash_Result result = osx_file_listener_hash(hash, name, fd); Hash_Result result = osx_file_listener_hash(hash, name, fd);
if (result.was_already_in_table){ if (result.was_already_in_table){
was_already_in_table = true; was_already_in_table = true;
} }
else if (!result.is_in_table){ else if (!result.is_in_table){
osx_file_listener_double_table(); osx_file_listener_double_table();
osx_file_listener_hash(hash, name, fd); osx_file_listener_hash(hash, name, fd);
} }
return(was_already_in_table); return(was_already_in_table);
} }
void void
@ -521,40 +533,40 @@ osx_add_file_listener(char *dir_name){
if (file_change_queue.kq < 0){ if (file_change_queue.kq < 0){
return; return;
} }
//fprintf(stdout, "ADD_FILE_LISTENER: %s\n", dir_name); //fprintf(stdout, "ADD_FILE_LISTENER: %s\n", dir_name);
i32 fd = open(dir_name, O_EVTONLY); i32 fd = open(dir_name, O_EVTONLY);
if (fd <= 0){ if (fd <= 0){
fprintf(stdout, "could not open fd for %s\n", dir_name); fprintf(stdout, "could not open fd for %s\n", dir_name);
return; return;
} }
// TODO(allen): Decide what to do about these darn string mallocs. // TODO(allen): Decide what to do about these darn string mallocs.
void *name_stored = osx_file_name_prefixed_length(dir_name); void *name_stored = osx_file_name_prefixed_length(dir_name);
struct kevent new_kevent; struct kevent new_kevent;
EV_SET(&new_kevent, fd, EVFILT_VNODE, EV_ADD|EV_CLEAR, NOTE_DELETE|NOTE_WRITE, 0, name_stored); EV_SET(&new_kevent, fd, EVFILT_VNODE, EV_ADD|EV_CLEAR, NOTE_DELETE|NOTE_WRITE, 0, name_stored);
struct timespec t = {0}; struct timespec t = {0};
kevent(file_change_queue.kq, &new_kevent, 1, 0, 0, &t); kevent(file_change_queue.kq, &new_kevent, 1, 0, 0, &t);
b32 was_already_in_table = osx_file_listener_hash_always(osx_file_hash(name_stored), name_stored, fd); b32 was_already_in_table = osx_file_listener_hash_always(osx_file_hash(name_stored), name_stored, fd);
if (was_already_in_table){ if (was_already_in_table){
free(name_stored); free(name_stored);
} }
DBG_POINT(); DBG_POINT();
} }
void void
osx_remove_file_listener(char *dir_name){ osx_remove_file_listener(char *dir_name){
void *name = osx_file_name_prefixed_length(dir_name); void *name = osx_file_name_prefixed_length(dir_name);
i32 fd; i32 fd;
if (osx_file_listener_lookup_and_delete(osx_file_hash(name), name, &fd)){ if (osx_file_listener_lookup_and_delete(osx_file_hash(name), name, &fd)){
close(fd); close(fd);
} }
free(name); free(name);
} }
i32 i32
@ -562,7 +574,7 @@ osx_get_file_change_event(char *buffer, i32 max, i32 *size){
if (file_change_queue.kq < 0){ if (file_change_queue.kq < 0){
return 0; return 0;
} }
i32 result = 0; i32 result = 0;
struct timespec t = {0}; struct timespec t = {0};
struct kevent event_out; struct kevent event_out;
@ -587,7 +599,7 @@ osx_get_file_change_event(char *buffer, i32 max, i32 *size){
} }
} }
} }
return(result); return(result);
} }
@ -637,15 +649,10 @@ NSWindow* window = 0;
void void
osx_schedule_step(void){ osx_schedule_step(void){
//DBG_POINT();
#if 1
[NSTimer scheduledTimerWithTimeInterval: 0.0 [NSTimer scheduledTimerWithTimeInterval: 0.0
target: view target: view
selector: @selector(requestDisplay) selector: @selector(requestDisplay)
userInfo: nil repeats:NO]; userInfo: nil repeats:NO];
#else
[view requestDisplay];
#endif
} }
void void
@ -666,46 +673,51 @@ osx_close_app(void){
int int
main(int argc, char **argv){ main(int argc, char **argv){
memset(&osx_objc, 0, sizeof(osx_objc)); memset(&osx_objc, 0, sizeof(osx_objc));
umem clipboard_size = MB(4); umem clipboard_size = MB(4);
osx_objc.clipboard_data = osx_allocate(clipboard_size); osx_objc.clipboard_data = osx_allocate(clipboard_size);
osx_objc.clipboard_max = clipboard_size; osx_objc.clipboard_max = clipboard_size;
osx_objc.argc = argc; osx_objc.argc = argc;
osx_objc.argv = argv; osx_objc.argv = argv;
osx_file_listener_init(); osx_file_listener_init();
@autoreleasepool{ @autoreleasepool{
NSApplication *app = [NSApplication sharedApplication]; NSApplication *app = [NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular]; [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[app setDelegate:[[AppDelegate alloc] init]]; [app setDelegate:[[AppDelegate alloc] init]];
NSRect screenRect = [[NSScreen mainScreen] frame]; NSRect screenRect = [[NSScreen mainScreen] frame];
float w = 800.f; float w = 800.f;
float h = 600.f; float h = 600.f;
NSRect frame = NSMakeRect((screenRect.size.width - w) * 0.5, (screenRect.size.height - h) * 0.5, w, h); NSRect frame = NSMakeRect((screenRect.size.width - w) * 0.5, (screenRect.size.height - h) * 0.5, w, h);
u32 flags = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask; u32 flags = NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask | NSResizableWindowMask;
window = [[NSWindow alloc] initWithContentRect:frame styleMask:flags backing:NSBackingStoreBuffered defer:NO]; window = [[NSWindow alloc] initWithContentRect:frame styleMask:flags backing:NSBackingStoreBuffered defer:NO];
[window setAcceptsMouseMovedEvents:YES]; [window setAcceptsMouseMovedEvents:YES];
view = [[My4coderView alloc] init]; view = [[My4coderView alloc] init];
[view setFrame:[[window contentView] bounds]]; [view setFrame:[[window contentView] bounds]];
[view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; [view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[[window contentView] addSubview:view]; [[window contentView] addSubview:view];
[window setMinSize:NSMakeSize(100, 100)]; [window setMinSize:NSMakeSize(100, 100)];
[window setTitle:@WINDOW_NAME]; [window setTitle:@WINDOW_NAME];
[window makeKeyAndOrderFront:nil]; [window makeKeyAndOrderFront:nil];
osx_init(); [NSTimer scheduledTimerWithTimeInterval: 0.5
target: view
[NSApp run]; selector: @selector(checkClipboard)
} userInfo: nil repeats:YES];
return(0); osx_init();
[NSApp run];
}
return(0);
} }
// BOTTOM // BOTTOM