Hacking Mac OSX Cocoa API from Perl

5,101 views
4,942 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,101
On SlideShare
0
From Embeds
0
Number of Embeds
2,508
Actions
Shares
0
Downloads
9
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Hacking Mac OSX Cocoa API from Perl

  1. 1. • #import <Foundation/Foundation.h> int main(int argc, char** argv) { NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; // Objective-C code here NSLog(@”Hello World!”); [pool drain]; return 0; }
  2. 2. • #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" XS(func) { dXSARGS; // code here XSRETURN(0); } XS(boot_Foo) { newXS("Foo::xs_function", func, __FILE__); }
  3. 3. • package Foo; use strict; use XSLoader; XSLoader::load __PACKAGE__, $VERSION; 1;
  4. 4. • use Foo; Foo::xs_function();
  5. 5. • use inc::Module::Install; # some basic descriptions here use_ppport 3.19; WriteAll;
  6. 6. • #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h" // undefine Move macro, this is conflict to Mac OS X QuickDraw API. #undef Move #import <Foundation/Foundation.h> XS(hello) { dXSARGS; NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; NSLog(@"Hello!"); [pool drain]; XSRETURN(0); } XS(boot_Hello) { newXS("Hello::hello", hello, __FILE__); }
  7. 7. • #include "EXTERN.h" #include "perl.h" #include "XSUB.h" #include "ppport.h"
  8. 8. • #undef Move
  9. 9. • XS(function) { dXSARGS; // code here XSRETURN(0); }
  10. 10. • XS(function) { dXSARGS; // code here ST(0) = some_sv; XSRETURN(1); }
  11. 11. • XS(function) { dXSARGS; // code here ST(0) = some_sv; ST(1) = some_sv2; XSRETURN(2); }
  12. 12. • XS(function) { dXSARGS; SV* sv_args1 = ST(0); SV* sv_args2 = ST(1); // code here ST(0) = some_sv; ST(1) = some_sv2; XSRETURN(2); }
  13. 13. • XS(function) { dXSARGS; if (items < 2) { Perl_croak(aTHX_ "Usage: function($args1, $args2)"); } SV* sv_args1 = ST(0); SV* sv_args2 = ST(1); // code here ST(0) = some_sv; ST(1) = some_sv2; XSRETURN(2); }
  14. 14. STRLEN len;char* c = SvPV(sv, len);NSString* str = [NSString stringWithUTF8String:c];SV* sv = sv_2mortal(newSV(0));sv_setpv(sv, [str UTF8String]);
  15. 15. NSNumber* n;if (SvNOKp(sv)) { n = [NSNumber numberWithDouble:(double)SvNVX(sv))];}else if (SvIOK_UV(sv)) { n = [NSNumber numberWithDouble:(double)SvUV(sv))];}else if (SvIOKp(sv)) { n = [NSNumber numberWithDouble:(double)SvIV(sv))];}SV* sv = sv_2mortal(newSVnv([n doubleValue]));
  16. 16. • use Cocoa::Growl :all; my $installed = growl_installed(); my $running = growl_running();
  17. 17. • use Cocoa::Growl :all; # register application growl_register( app => My growl script, icon => /path/to/icon.png, # or http://urlto/icon notifications => [qw(Notification1 Notification2)], );
  18. 18. • use Cocoa::Growl :all; # show growl notification growl_notify( name => Notification1, title => Hello!, description => Growl world!, );
  19. 19. • use Cocoa::EventLoop; use Cocoa::Growl :all; growl_register( name => test script, notifications => [test notification], ); my $wait = 1; growl_notify( name => test notification, title => Hello, description => Growl World!, on_click => sub { warn click; $wait = 0; }, on_timeout => sub { warn timeout; $want = 0; }, ); Cocoa::EventLoop->run_while(0.1) while unless $wait;
  20. 20. use AnyEvent;use Cocoa::EventLoop;# then all anyevent based api useCocoa::EventLoop!
  21. 21. • use AnyEvent; use Cocoa::EventLoop; use Cocoa::Growl; my $cv = AE::cv; growl_notify( name => test notification, title => Hello, description => Growl World!, on_click => sub { warn ‘click’; $cv->send; }, on_timeout => sub { warn ‘timeout’; $cv->send; }, ); $cv->recv;

×