diff --git a/lab/hello-cocoa.ss b/lab/hello-cocoa.ss new file mode 100755 index 0000000..b3d3d68 --- /dev/null +++ b/lab/hello-cocoa.ss @@ -0,0 +1,74 @@ +#!/usr/bin/env ikarus --r6rs-script + +(import (ikarus) (objc) (Cocoa) (Cocoa helpers)) + +(define (nsstring x) + [$ [$ NSString alloc] + initWithCharactersNoCopy: x + length: (string-length x) + freeWhenDone: #t]) + +(define pool [$ [$ NSAutoreleasePool alloc] init]) + +(make-app) +[$ NSApplication sharedApplication] + +(define (setup-menu app-name) + (define (make-menu title) + [$ [$ NSMenu alloc] initWithTitle: (nsstring title)]) + [$ NSApp setMainMenu: (make-menu "")] + (let ([apple-menu (make-menu "")]) + (define (add-item name key action mod) + (let ([x [$ apple-menu + addItemWithTitle: (nsstring name) + action: (get-selector action) + keyEquivalent: (nsstring key)]]) + (when mod + [$ x setKeyEquivalentModifierMask: mod]))) + (define (add-separator) + [$ apple-menu addItem: [$ NSMenuItem separatorItem]]) + (add-item (string-append "About " app-name) "" + "orderFrontStandardAboutPanel:" #f) + (add-separator) + (add-item (string-append "Hide " app-name) "h" "hide:" #f) + (add-item "Hide Others" "h" "hideOtherApplications:" + (bitwise-ior NSAlternateKeyMask NSCommandKeyMask)) + (add-item "Show All" "" "unhideAllApplications:" #f) + (add-separator) + (add-item (string-append "Quit " app-name) "q" "terminate:" #f) + (let ([menu-item + [$ [$ NSMenuItem alloc] + initWithTitle: (nsstring "") + action: #f + keyEquivalent: (nsstring "")]]) + [$ menu-item setSubmenu: apple-menu] + [$ [$ NSApp mainMenu] addItem: menu-item] + [$ NSApp setAppleMenu: apple-menu] + [$ menu-item release]) + [$ apple-menu release])) + +(setup-menu "Hello Ikarus") + +(define style + (bitwise-ior + NSTitledWindowMask + NSClosableWindowMask + NSResizableWindowMask + NSMiniaturizableWindowMask)) + +(define backing NSBackingStoreBuffered) + +(define win + [$ [$ NSWindow alloc] + initWithContentRect: '#(#(50 50) #(600 400)) + styleMask: style + backing: backing + defer: #f]) + +[$ win setTitle: (nsstring "Hello Ikarus")] +[$ win makeKeyAndOrderFront: win] + +[$ NSApp run] +[$ pool release] + +(printf "back!\n") diff --git a/lab/objc/hello-cocoa.ss b/lab/objc/hello-cocoa.ss deleted file mode 100755 index b4c9f86..0000000 --- a/lab/objc/hello-cocoa.ss +++ /dev/null @@ -1,36 +0,0 @@ -#!/usr/bin/env ikarus --r6rs-script - -(import (ikarus) (objc) (Cocoa)) - -(define pool [$ [$ NSAutoreleasePool alloc] init]) - -[$ NSApplication sharedApplication] - -(define style - (bitwise-ior - NSTitledWindowMask - NSClosableWindowMask - NSResizableWindowMask - NSMiniaturizableWindowMask)) - -(define backing NSBackingStoreBuffered) - -(define win - [$ [$ NSWindow alloc] - initWithContentRect: '#(#(50 50) #(600 400)) - styleMask: style - backing: backing - defer: #f]) - -(define (nsstring x) - [$ [$ NSString alloc] - initWithCharactersNoCopy: x - length: (string-length x) - freeWhenDone: #t]) - -[$ win setTitle: (nsstring "Hello Ikarus")] - -[$ win makeKeyAndOrderFront: win] - -[$ NSApp run] -[$ pool release] diff --git a/lab/objc/Cocoa.ss b/lib/Cocoa.ss similarity index 81% rename from lab/objc/Cocoa.ss rename to lib/Cocoa.ss index 2b573c6..6efc0ca 100755 --- a/lab/objc/Cocoa.ss +++ b/lib/Cocoa.ss @@ -16,6 +16,8 @@ (define-class NSWindow) (define-class NSApplication) (define-class NSString) + (define-class NSMenu) + (define-class NSMenuItem) (define-object NSApp Cocoa) (define NSBorderlessWindowMask #b000000000) @@ -48,5 +50,19 @@ (define NSMacOSRomanStringEncoding 30) (define NSProprietaryStringEncoding 65536) ; /* Installation-specific encoding */ + (define NSAlphaShiftKeyMask (sll 1 16)) + (define NSShiftKeyMask (sll 1 17)) + (define NSControlKeyMask (sll 1 18)) + (define NSAlternateKeyMask (sll 1 19)) + (define NSCommandKeyMask (sll 1 20)) + (define NSNumericPadKeyMask (sll 1 21)) + (define NSHelpKeyMask (sll 1 22)) + (define NSFunctionKeyMask (sll 1 23)) + (define NSDeviceIndependentModifierFlagsMask #xffff0000) + + )) + + + diff --git a/lib/Cocoa/helpers.ss b/lib/Cocoa/helpers.ss new file mode 100644 index 0000000..c27b36e --- /dev/null +++ b/lib/Cocoa/helpers.ss @@ -0,0 +1,24 @@ +(library (Cocoa helpers) + (export make-app) + (import (ikarus) (ikarus system $foreign)) + + (define (make-app) + (define kProcessTransformToForegroundApplication 1) + (define self (dlopen #f)) + (define get-current-process + ((make-ffi 'void '(pointer)) + (dlsym self "GetCurrentProcess"))) + (define transform-process-type + ((make-ffi 'void '(pointer sint32)) + (dlsym self "TransformProcessType"))) + (define set-front-process + ((make-ffi 'void '(pointer)) + (dlsym self "SetFrontProcess"))) + (let ([p (malloc 16)]) + (get-current-process p) + (transform-process-type p kProcessTransformToForegroundApplication) + (set-front-process p) + (free p))) +) + + diff --git a/lib/Makefile.am b/lib/Makefile.am index d09c41a..17af419 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -1,7 +1,9 @@ libstreamsdir=$(pkglibdir)/streams dist_libstreams_DATA=streams/primitive.ss streams/derived.ss +libCocoadir=$(pkglibdir)/Cocoa +dist_libCocoa_DATA=Cocoa/helpers.ss dist_pkglib_DATA= streams.ss match.ss pregexp.ss gl.ss glut.ss \ - ypsilon-compat.ikarus.ss ypsilon-compat.ypsilon.ss - + ypsilon-compat.ikarus.ss ypsilon-compat.ypsilon.ss \ + objc.ss Cocoa.ss diff --git a/lib/Makefile.in b/lib/Makefile.in index e21a9d8..69976c8 100644 --- a/lib/Makefile.in +++ b/lib/Makefile.in @@ -34,8 +34,9 @@ build_triplet = @build@ host_triplet = @host@ target_triplet = @target@ subdir = lib -DIST_COMMON = $(dist_libstreams_DATA) $(dist_pkglib_DATA) \ - $(srcdir)/Makefile.am $(srcdir)/Makefile.in +DIST_COMMON = $(dist_libCocoa_DATA) $(dist_libstreams_DATA) \ + $(dist_pkglib_DATA) $(srcdir)/Makefile.am \ + $(srcdir)/Makefile.in ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 am__aclocal_m4_deps = $(top_srcdir)/configure.ac am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ @@ -51,11 +52,13 @@ am__vpath_adj = case $$p in \ *) f=$$p;; \ esac; am__strip_dir = `echo $$p | sed -e 's|^.*/||'`; -am__installdirs = "$(DESTDIR)$(libstreamsdir)" \ - "$(DESTDIR)$(pkglibdir)" +am__installdirs = "$(DESTDIR)$(libCocoadir)" \ + "$(DESTDIR)$(libstreamsdir)" "$(DESTDIR)$(pkglibdir)" +dist_libCocoaDATA_INSTALL = $(INSTALL_DATA) dist_libstreamsDATA_INSTALL = $(INSTALL_DATA) dist_pkglibDATA_INSTALL = $(INSTALL_DATA) -DATA = $(dist_libstreams_DATA) $(dist_pkglib_DATA) +DATA = $(dist_libCocoa_DATA) $(dist_libstreams_DATA) \ + $(dist_pkglib_DATA) DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) ACLOCAL = @ACLOCAL@ AMTAR = @AMTAR@ @@ -160,8 +163,11 @@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ libstreamsdir = $(pkglibdir)/streams dist_libstreams_DATA = streams/primitive.ss streams/derived.ss +libCocoadir = $(pkglibdir)/Cocoa +dist_libCocoa_DATA = Cocoa/helpers.ss dist_pkglib_DATA = streams.ss match.ss pregexp.ss gl.ss glut.ss \ - ypsilon-compat.ikarus.ss ypsilon-compat.ypsilon.ss + ypsilon-compat.ikarus.ss ypsilon-compat.ypsilon.ss \ + objc.ss Cocoa.ss all: all-am @@ -195,6 +201,23 @@ $(top_srcdir)/configure: $(am__configure_deps) cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh $(ACLOCAL_M4): $(am__aclocal_m4_deps) cd $(top_builddir) && $(MAKE) $(AM_MAKEFLAGS) am--refresh +install-dist_libCocoaDATA: $(dist_libCocoa_DATA) + @$(NORMAL_INSTALL) + test -z "$(libCocoadir)" || $(MKDIR_P) "$(DESTDIR)$(libCocoadir)" + @list='$(dist_libCocoa_DATA)'; for p in $$list; do \ + if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \ + f=$(am__strip_dir) \ + echo " $(dist_libCocoaDATA_INSTALL) '$$d$$p' '$(DESTDIR)$(libCocoadir)/$$f'"; \ + $(dist_libCocoaDATA_INSTALL) "$$d$$p" "$(DESTDIR)$(libCocoadir)/$$f"; \ + done + +uninstall-dist_libCocoaDATA: + @$(NORMAL_UNINSTALL) + @list='$(dist_libCocoa_DATA)'; for p in $$list; do \ + f=$(am__strip_dir) \ + echo " rm -f '$(DESTDIR)$(libCocoadir)/$$f'"; \ + rm -f "$(DESTDIR)$(libCocoadir)/$$f"; \ + done install-dist_libstreamsDATA: $(dist_libstreams_DATA) @$(NORMAL_INSTALL) test -z "$(libstreamsdir)" || $(MKDIR_P) "$(DESTDIR)$(libstreamsdir)" @@ -266,7 +289,7 @@ check-am: all-am check: check-am all-am: Makefile $(DATA) installdirs: - for dir in "$(DESTDIR)$(libstreamsdir)" "$(DESTDIR)$(pkglibdir)"; do \ + for dir in "$(DESTDIR)$(libCocoadir)" "$(DESTDIR)$(libstreamsdir)" "$(DESTDIR)$(pkglibdir)"; do \ test -z "$$dir" || $(MKDIR_P) "$$dir"; \ done install: install-am @@ -311,7 +334,7 @@ info: info-am info-am: -install-data-am: install-dist_libstreamsDATA +install-data-am: install-dist_libCocoaDATA install-dist_libstreamsDATA install-dvi: install-dvi-am @@ -345,21 +368,23 @@ ps: ps-am ps-am: -uninstall-am: uninstall-dist_libstreamsDATA uninstall-dist_pkglibDATA +uninstall-am: uninstall-dist_libCocoaDATA \ + uninstall-dist_libstreamsDATA uninstall-dist_pkglibDATA .MAKE: install-am install-strip .PHONY: all all-am check check-am clean clean-generic distclean \ distclean-generic distdir dvi dvi-am html html-am info info-am \ install install-am install-data install-data-am \ - install-dist_libstreamsDATA install-dist_pkglibDATA \ - install-dvi install-dvi-am install-exec install-exec-am \ - install-html install-html-am install-info install-info-am \ - install-man install-pdf install-pdf-am install-ps \ - install-ps-am install-strip installcheck installcheck-am \ - installdirs maintainer-clean maintainer-clean-generic \ - mostlyclean mostlyclean-generic pdf pdf-am ps ps-am uninstall \ - uninstall-am uninstall-dist_libstreamsDATA \ + install-dist_libCocoaDATA install-dist_libstreamsDATA \ + install-dist_pkglibDATA install-dvi install-dvi-am \ + install-exec install-exec-am install-html install-html-am \ + install-info install-info-am install-man install-pdf \ + install-pdf-am install-ps install-ps-am install-strip \ + installcheck installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-generic pdf \ + pdf-am ps ps-am uninstall uninstall-am \ + uninstall-dist_libCocoaDATA uninstall-dist_libstreamsDATA \ uninstall-dist_pkglibDATA # Tell versions [3.59,3.63) of GNU make to not export all variables. diff --git a/lab/objc/objc.ss b/lib/objc.ss similarity index 96% rename from lab/objc/objc.ss rename to lib/objc.ss index 32a624b..65f27d6 100644 --- a/lab/objc/objc.ss +++ b/lib/objc.ss @@ -5,12 +5,12 @@ define-class define-object string->char* + get-selector $) (import (ikarus) (ikarus system $foreign) - (except (ypsilon-compat) format) - ) + (except (ypsilon-compat) format)) (define ptrsize 4) @@ -79,13 +79,15 @@ (define (bv->u8* x) (let ([n (bytevector-length x)]) - (let ([p (malloc n)]) - (let f ([i 0]) - (cond - [(= i n) p] - [else - (pointer-set-char p i (bytevector-s8-ref x i)) - (f (+ i 1))]))))) + (if (= n 0) + (integer->pointer 0) + (let ([p (malloc n)]) + (let f ([i 0]) + (cond + [(= i n) p] + [else + (pointer-set-char p i (bytevector-s8-ref x i)) + (f (+ i 1))])))))) (define (char*->string x) @@ -301,7 +303,7 @@ (error 'symbol->selector "undefined selector" x))) -(define (make-signature str) +(define (make-signature method-name str) (define who 'make-signature) (let ([n (string-length str)]) (define (scan i c) @@ -376,6 +378,7 @@ (define (convert-incoming t x) (case t [(object) (make-object x)] + [(char) x] [(void) (void)] [else (error 'convert-incoming "invalid type" t)])) @@ -401,6 +404,7 @@ [(selector) (cond [(selector? x) (selector-ptr x)] + [(not x) (integer->pointer 0)] [else (error 'convert-output "not a selector" x)])] [(object) (cond @@ -449,7 +453,7 @@ [else (error 'send-message "not an object" x)])]) (unless method (error 'send-message "undefined method" method-name)) - (let ([sig (make-signature (method-types method))] + (let ([sig (make-signature method-name (method-types method))] [mptr (method-pointer method)]) (call-with-sig sig mptr (cons* x selector args))))))