From bf862b8bdcbfc3f30c610ea822151ffd97da56d0 Mon Sep 17 00:00:00 2001 From: Marek Ratajczak Date: Tue, 19 Aug 2025 16:08:50 -0500 Subject: [PATCH 1/2] Update osxgl.m --- src/macosx/osxgl.m | 118 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) diff --git a/src/macosx/osxgl.m b/src/macosx/osxgl.m index 0a9147cf9b..29dc5e2148 100644 --- a/src/macosx/osxgl.m +++ b/src/macosx/osxgl.m @@ -65,6 +65,10 @@ #define MINIMUM_WIDTH 48 #define MINIMUM_HEIGHT 48 +/* by MAREK */ +#define FILE_URL_SIZE 254 +/* end by MAREK */ + /* Unsigned integer; data type only avaliable for OS X >= 10.5 */ #if MAC_OS_X_VERSION_MIN_REQUIRED < 1050 typedef unsigned int NSUInteger; @@ -212,6 +216,9 @@ @interface ALOpenGLView : NSOpenGLView /* This is passed onto the event functions so we know where the event came from */ ALLEGRO_DISPLAY* dpy_ptr; } +/* by MAREK */ +- (id)initWithFrame:(NSRect)frameRect; +/* end by MAREK */ -(void)setAllegroDisplay: (ALLEGRO_DISPLAY*) ptr; -(ALLEGRO_DISPLAY*) allegroDisplay; -(void) reshape; @@ -292,8 +299,119 @@ void _al_osx_mouse_was_installed(BOOL install) { }); } +/* by MAREK */ +@implementation NSString (Char) + +/* + * Convert a NSString to a char pointer +*/ +-(char *)toChar{ + const char* strUtf8 = [self UTF8String]; + size_t len = strlen(strUtf8) + 1; + char *toChar = malloc(len); + memcpy(toChar, strUtf8, len); + return toChar; +} +@end +/* end by MAREK */ + @implementation ALOpenGLView +/*by MAREK*/ +- (id)initWithFrame:(NSRect)frameRect +{ + self = [super initWithFrame:frameRect]; + if (self) { + // Register for file URL drops (e.g., images) + [self registerForDraggedTypes:@[NSPasteboardTypeFileURL]]; + // Or other types like NSStringPboardType, NSFileContentsPboardType, etc. + } + + NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]; + + // NSString *logPath = @"allegro5.log"; + // freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr); + + return self; +} + +// Drag-and-drop methods + +- (NSDragOperation)draggingEntered:(id)sender { + NSPasteboard *pasteboard = [sender draggingPasteboard]; + if ([[pasteboard types] containsObject:NSPasteboardTypeFileURL]) { + return NSDragOperationCopy; // Accept copy operation for files + } + return NSDragOperationNone; +} + +- (BOOL)prepareForDragOperation:(id)sender { + return YES; // Prepare to accept the drop +} + +- (BOOL)performDragOperation:(id)sender { + NSPasteboard *pasteboard = [sender draggingPasteboard]; + NSArray *classes = @[NSURL.class]; + NSDictionary *options = @{NSPasteboardURLReadingFileURLsOnlyKey: @YES}; + + int n=0; + + NSArray *fileURLs = [pasteboard readObjectsForClasses:classes options:options]; + if (fileURLs.count > 0) + { + // Handle multiple dropped files here + + for (NSURL *fileURL in fileURLs) + { + NSString *filePath = [fileURL path]; + unsigned long ul=[filePath length]; + NSLog(@"Dropped file: %@ len:%d", filePath, (int)ul); + const char *cfileURL= [filePath cStringUsingEncoding:NSUTF8StringEncoding]; + + // Process the file: e.g., load as texture, model, or data into OpenGL + // Example: If it's an image, use NSImage to load and upload to GL texture + // NSImage *image = [[NSImage alloc] initWithContentsOfURL:fileURL]; + // Then convert to OpenGL texture... + ALLEGRO_DISPLAY_OSX_WIN* dpy = (ALLEGRO_DISPLAY_OSX_WIN*) dpy_ptr; + NSWindow *window = dpy->win; + + NSPoint mousePos = [window mouseLocationOutsideOfEventStream]; + // mousePos.x and mousePos.y now contain the cursor's coordinates relative to the window's bottom-left corner. + + NSRect rc = [window frame]; + NSRect content = [window contentRectForFrameRect: rc]; + content = [self convertRectToBacking: content]; + ALLEGRO_EVENT_SOURCE *es = &dpy->parent.es; + + _al_event_source_lock(es); + ALLEGRO_EVENT event; + + event.drop.type = ALLEGRO_EVENT_DROP; + event.drop.timestamp = al_get_time(); + event.drop.x = (int)mousePos.x; + event.drop.y = (int)mousePos.y; + NSLog(@"Event with file (%d/%d) : %@", n+1, (int)fileURLs.count, filePath); + event.drop.text = (char*) cfileURL; + event.drop.is_file = true; + event.drop.row = n; + if ((n+1)<(int)fileURLs.count) event.drop.is_complete = false; + else event.drop.is_complete = true; + _al_event_source_emit_event(es, &event); + _al_event_source_unlock(es); + n++; + } + + return YES; + } + return NO; +} + +- (void)concludeDragOperation:(id)sender { + // Optional: Any cleanup after drop +} + +/* end by MAREK*/ + -(void) prepareOpenGL { [super prepareOpenGL]; From 64d32f8245e1487fa7c7c8da9322bd3b3dc109a0 Mon Sep 17 00:00:00 2001 From: Marek Ratajczak Date: Wed, 20 Aug 2025 18:08:14 -0500 Subject: [PATCH 2/2] Add files via upload --- src/macosx/osxgl.m | 65 +++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/src/macosx/osxgl.m b/src/macosx/osxgl.m index 29dc5e2148..b3be0ea620 100644 --- a/src/macosx/osxgl.m +++ b/src/macosx/osxgl.m @@ -15,7 +15,6 @@ * See readme.txt for copyright information. */ - #include "allegro5/allegro.h" #include "allegro5/allegro_opengl.h" #include "allegro5/internal/aintern.h" @@ -27,6 +26,7 @@ #include "allegro5/platform/aintosx.h" #include "./osxgl.h" #include "allegro5/allegro_osx.h" + #ifndef ALLEGRO_MACOSX #error something is wrong with the makefile #endif @@ -65,9 +65,11 @@ #define MINIMUM_WIDTH 48 #define MINIMUM_HEIGHT 48 + /* by MAREK */ -#define FILE_URL_SIZE 254 -/* end by MAREK */ +static char **cfileURLs; +int cfileURLs_n=0; +/* */ //by MAREK /* Unsigned integer; data type only avaliable for OS X >= 10.5 */ #if MAC_OS_X_VERSION_MIN_REQUIRED < 1050 @@ -216,9 +218,9 @@ @interface ALOpenGLView : NSOpenGLView /* This is passed onto the event functions so we know where the event came from */ ALLEGRO_DISPLAY* dpy_ptr; } -/* by MAREK */ +/* by MAREK* / - (id)initWithFrame:(NSRect)frameRect; -/* end by MAREK */ +/* */ //by MAREK -(void)setAllegroDisplay: (ALLEGRO_DISPLAY*) ptr; -(ALLEGRO_DISPLAY*) allegroDisplay; -(void) reshape; @@ -299,25 +301,9 @@ void _al_osx_mouse_was_installed(BOOL install) { }); } -/* by MAREK */ -@implementation NSString (Char) - -/* - * Convert a NSString to a char pointer -*/ --(char *)toChar{ - const char* strUtf8 = [self UTF8String]; - size_t len = strlen(strUtf8) + 1; - char *toChar = malloc(len); - memcpy(toChar, strUtf8, len); - return toChar; -} -@end -/* end by MAREK */ - @implementation ALOpenGLView -/*by MAREK*/ +/* by MAREK */ - (id)initWithFrame:(NSRect)frameRect { self = [super initWithFrame:frameRect]; @@ -327,8 +313,6 @@ - (id)initWithFrame:(NSRect)frameRect // Or other types like NSStringPboardType, NSFileContentsPboardType, etc. } - NSString *cachesDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0]; - // NSString *logPath = @"allegro5.log"; // freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding], "a+", stderr); @@ -361,6 +345,16 @@ - (BOOL)performDragOperation:(id)sender { { // Handle multiple dropped files here + //freeing filenames + if (cfileURLs_n>0) + { + for (int ni=0; ni)sender { event.drop.x = (int)mousePos.x; event.drop.y = (int)mousePos.y; NSLog(@"Event with file (%d/%d) : %@", n+1, (int)fileURLs.count, filePath); - event.drop.text = (char*) cfileURL; + //alocating memory for filename + cfileURLs[n]=malloc(strlen(cfileURL)+1); + //copying + strncpy(cfileURLs[n], cfileURL, strlen(cfileURL)+1); + //pointer will be unique and not destructed until next drop series + event.drop.text = cfileURLs[n]; event.drop.is_file = true; event.drop.row = n; if ((n+1)<(int)fileURLs.count) event.drop.is_complete = false; @@ -410,7 +409,7 @@ - (void)concludeDragOperation:(id)sender { // Optional: Any cleanup after drop } -/* end by MAREK*/ +/**/ //by MAREK -(void) prepareOpenGL { @@ -1764,6 +1763,10 @@ static void init_halt_events(ALLEGRO_DISPLAY_OSX_WIN *dpy) return; } + + ////// + //view->registerForDraggedTypes:@[NSPasteboardTypeFileURL]]; + ////// dpy->view = view; /* Hook up the view to its display */ [view setAllegroDisplay: &dpy->parent]; @@ -1994,6 +1997,16 @@ static void destroy_display(ALLEGRO_DISPLAY* d) al_free(d->vertex_cache); al_free(d); [pool drain]; + + /* by MAREK */ + //freeing filenames + if (cfileURLs_n>0) + { + for (int ni=0; ni