forked from wojtekmach/mix_install_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objc.exs
49 lines (40 loc) · 1.27 KB
/
objc.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Mix.install([
{:objc, github: "wojtekmach/objc"}
])
defmodule HelloObjc.MixProject do
use Mix.Project
def project do
[
app: :hello_objc,
version: "1.0.0"
]
end
end
defmodule HelloObjC do
use ObjC, compile: "-lobjc -framework AppKit"
defobjc(:hello, 0, ~S"""
#import "AppKit/AppKit.h"
extern int erl_drv_steal_main_thread(char *name, ErlNifTid *dtid, void* (*func)(void*), void* arg, ErlNifThreadOpts *opts);
extern int erl_drv_stolen_main_thread_join(ErlNifTid tid, void **respp);
ErlNifTid hello_thread;
void *hello_main_loop(void * _unused)
{
[NSAutoreleasePool new];
[NSApplication sharedApplication];
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp activateIgnoringOtherApps:YES];
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Hello from ObjC!"];
[alert addButtonWithTitle:@"OK"];
[alert runModal];
return NULL;
}
static ERL_NIF_TERM hello(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[])
{
erl_drv_steal_main_thread((char *)"hello", &hello_thread, hello_main_loop, (void *)NULL, NULL);
erl_drv_stolen_main_thread_join(hello_thread, NULL);
return enif_make_atom(env, "ok");
}
""")
end
:ok = HelloObjC.hello()