-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdemo-app.lisp
376 lines (312 loc) · 13.8 KB
/
demo-app.lisp
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
(defpackage :demo-app
(:use :cl :objc-runtime)
(:export
#:get-method-names))
(in-package :demo-app)
(named-readtables:in-readtable :objc-readtable)
(cffi:defcallback exception-handler :void ((exception :pointer))
(with-selectors (reason)
(format t "~&Exxception: ~a~%" [exception reason])
(values)))
(defun init-window (window rect a b c)
(format t "~&got rect: ~s" rect)
(cffi:foreign-funcall "objc_msgSend"
:pointer window
:pointer @(initWithContentRect:)
:pointer window
(:struct objc-runtime::ns-rect) rect
:char a
:char b
:boolean c
:pointer))
(defmacro selector-lambda (selector &rest args)
`(lambda (receiver)
[receiver ,selector ,@args]))
(defun init-with-frame (thing rect)
(format t "~&got rect: ~s" rect)
(cffi:foreign-funcall "objc_msgSend"
:pointer thing
:pointer @(initWithFrame:)
(:struct objc-runtime::ns-rect) rect
:pointer))
(cffi:defcfun (print-rect "printRect")
:void
(rect (:struct objc-runtime:ns-rect)))
#+(or)
(cffi:defcfun (set-uncaught-exception-handler "set_uncaught_exception_handler"
:library objc-runtime::expose-stuff)
:void
(cb :pointer))
(defun value-for-key (thing key)
(with-selectors ((vfk "valueForKey:"))
(let ((key (objc-runtime::make-nsstring key)))
[thing vfk :string key])))
(defun call-with-rect (x y w h cb)
(check-type x real)
(check-type y real)
(check-type w real)
(check-type h real)
(cffi:with-foreign-object (rect '(:struct objc-runtime::ns-rect))
(cffi:with-foreign-slots (((:pointer ns-rect-origin) (:pointer ns-rect-size))
rect (:struct objc-runtime::ns-rect))
(cffi:with-foreign-slots ((ns-point-x ns-point-y) ns-rect-origin (:struct ns-point))
(setf ns-point-x (coerce x 'double-float)
ns-point-y (coerce y 'double-float)))
(cffi:with-foreign-slots ((ns-size-width ns-size-height)
ns-rect-size (:struct ns-size))
(setf ns-size-width (coerce w 'double-float)
ns-size-height (coerce h 'double-float))))
(funcall cb rect)))
(defun call-with-point (x y cb)
(check-type x real)
(check-type y real)
(cffi:with-foreign-object (point '(:struct ns-point))
(cffi:with-foreign-slots ((ns-point-x ns-point-y) point (:struct ns-point))
(setf ns-point-x (coerce x 'double-float)
ns-point-y (coerce y 'double-float)))
(funcall cb point)))
(defmacro with-rect ((rect (x y) (w h)) &body body)
`(call-with-rect ,x ,y ,w ,h
(lambda (,rect)
,@body)))
(defmacro with-point ((point (x y)) &body body)
`(call-with-point ,x ,y
(lambda (,point)
,@body)))
(defun make-rect (x y w h)
(check-type x real)
(check-type y real)
(check-type w real)
(check-type h real)
(cffi:convert-to-foreign `(ns-rect-origin
(objc-runtime:ns-point-x
,(coerce x 'double-float)
objc-runtime:ns-point-y
,(coerce y 'double-float))
ns-rect-size
(objc-runtime:ns-size-width
,(coerce w 'double-float)
objc-runtime:ns-size-height
,(coerce h 'double-float)))
'(:struct objc-runtime:ns-rect)))
(defun show-alert (message &optional (informative-text "Informative Text!"))
(let ((alert [[#@NSAlert @(alloc)] @(init)]))
[alert @(setMessageText:) :pointer (objc-runtime::make-nsstring message)]
[alert @(setInformativeText:) :pointer (objc-runtime::make-nsstring informative-text)]
[alert @(addButtonWithTitle:) :pointer @"OK"]
[alert @(addButtonWithTitle:) :pointer @"Cancel"]
[alert @(runModal)]))
(cffi:defcallback do-things-action :void ((a :pointer) (b :pointer) (sender :pointer))
(declare (ignore a b sender))
(show-alert "Starting Swank"
"Loading Quicklisp from ~/quicklisp/setup.lisp + starting swank")
(load "~/quicklisp/setup.lisp")
(funcall (intern "QUICKLOAD" (find-package :QL)) :swank)
(funcall (intern "CREATE-SERVER" (find-package :swank)) :port 5060 :dont-close t)
(show-alert "Started swank on 5060"))
(cffi:defcallback alert-action :void ((a :pointer) (b :pointer) (sender :pointer))
(declare (ignore a b sender))
(show-alert "Hello There!"))
(cffi:defcallback profit-action :void ((a :pointer) (b :pointer) (sender :pointer))
(declare (ignore a b sender))
(show-alert "That Was Profitable!"))
(defun alloc-init (cls)
[[cls @(alloc)] @(init)])
(defun make-button-delegate (button cb)
(objc-runtime.data-extractors:objc-typecase button
(#@NSButton (let ((my-class (objc-runtime::objc-allocate-class-pair #@NSObject "ButtonDel" 0)))
(objc-runtime::class-add-method my-class @(doMagic) cb "v@:@")
(fw.lu:prog1-bind (result (alloc-init my-class))
[button @(setTarget:) :pointer result]
[button @(setAction:) :pointer @(doMagic)])))
(t (format t "~&The button is not a button~%"))))
(defun make-app-delegate-class (outlets)
(let ((app-delegate-class (objc-runtime::objc-allocate-class-pair
#@NSObject "AppDelegate" 0)))
(objc-runtime:add-pointer-ivar app-delegate-class "window")
(objc-runtime:add-pointer-ivar app-delegate-class "delegate")
(loop for outlet in outlets do
(objc-runtime:add-pointer-ivar app-delegate-class outlet))
app-delegate-class))
(defun make-app-delegate-class-with-props (foo outlets)
(let ((app-delegate-class (objc-runtime::objc-allocate-class-pair
#@NSObject foo 0)))
(objc-runtime:add-pointer-ivar app-delegate-class "window")
(objc-runtime:add-pointer-ivar app-delegate-class "delegate")
(loop for outlet in outlets do
(objc-runtime:add-pointer-ivar app-delegate-class outlet))
app-delegate-class))
(defun load-nib (name)
;; find and activate the nib
(let* ((bundle [#@NSBundle @(mainBundle)])
(nib [[#@NSNib @(alloc)] @(initWithNibNamed:bundle:)
:pointer (objc-runtime::make-nsstring name)
:pointer bundle]))
(cffi:with-foreign-object (p :pointer)
;; TODO: is dropping p a problem here? The docs say something relevant.
;; must investigate.
[nib @(instantiateWithOwner:topLevelObjects:)
:pointer objc-runtime::ns-app
:pointer p])))
;#+null
(defun main ()
#+sbcl
(sb-int:set-floating-point-modes :traps '())
(load "~/quicklisp/setup.lisp")
(funcall (intern "QUICKLOAD"
(find-package :QL))
:swank)
#+nil
(funcall (intern "CREATE-SERVER"
(find-package :swank))
:port 5060
:dont-close t)
(trivial-main-thread:with-body-in-main-thread (:blocking t)
[#@NSAutoreleasePool @(new)]
[#@NSApplication @(sharedApplication)]
#+nil
[objc-runtime::ns-app @(setActivationPolicy:) :int 0]
;; Setup the app delegate class. We register this one because it's useful
;; When debugging via something like lldb
(objc-runtime::objc-register-class-pair
(make-app-delegate-class '("actionButton"
"alertButton"
"profitButton")))
(load-nib "MainMenu.nib")
(let ((app-delegate [objc-runtime::ns-app @(delegate)]))
(make-button-delegate (value-for-key app-delegate "actionButton")
(cffi:callback do-things-action))
(make-button-delegate (value-for-key app-delegate "alertButton")
(cffi:callback alert-action))
(make-button-delegate (value-for-key app-delegate "profitButton")
(cffi:callback profit-action)))
[objc-runtime::ns-app @(activateIgnoringOtherApps:) :boolean t]
[objc-runtime::ns-app @(run)]))
(defclass application-shim ()
((%main-view :initarg :main-view :accessor main-view)))
(cffi:defcfun (%set-string-value "objc_msgSend")
:void
(cls objc-runtime::o-class)
(sel objc-runtime::o-selector)
(value :pointer))
(defun set-string-value (control string)
(prog1 control
(%set-string-value control @(setStringValue:)
(objc-runtime:make-nsstring string))))
(defun label (text)
(let ((view [[#@NSTextField @(alloc)] @(init)]))
(prog1 view
(set-string-value view text))))
(defun button (title)
(trivial-main-thread:with-body-in-main-thread (:blocking t)
[#@NSButton @(buttonWithTitle:target:action:)
:pointer (objc-runtime:make-nsstring title)
:pointer #@NSButton
:pointer @(alloc)]))
(defun init-in-main-thread (instance)
(prog1 instance
[instance @(performSelectorOnMainThread:withObject:waitUntilDone:)
:pointer @(init)
:pointer (cffi:null-pointer)
:bool t]))
(defvar *application-shim*
(make-instance 'application-shim))
(defun wait-for-events ()
(let ((event [objc-runtime::ns-app @(nextEventMatchingMask:untilDate:inMode:dequeue:)
:unsigned-long 18446744073709551615
:pointer [#@NSDate @(distantFuture)]
:pointer @"kCFRunLoopDefaultMode"
:int 1]))
[objc-runtime::ns-app @(sendEvent:) :pointer event]
event))
(defun tick ()
(wait-for-events))
(defun task-thread ()
(bt:make-thread (lambda ()
#+(or)
(trivial-main-thread:with-body-in-main-thread (:blocking t)
[#@NSEvent @(stopPeriodicEvents)]
[#@NSEvent @(startPeriodicEventsAfterDelay:withPeriod:) :double 0.0d0 :double 0.01d0])
(loop
(trivial-main-thread:with-body-in-main-thread (:blocking t)
(tick))))
:name "Cocoa Event Loop Feeder"))
;;#+nil
(defun old-main ()
(trivial-main-thread:with-body-in-main-thread (:blocking nil)
#+sbcl
(sb-int:set-floating-point-modes :traps '())
[#@NSAutoreleasePool @(new)]
[#@NSApplication @(sharedApplication)]
(format t "~&app: ~s~%" objc-runtime::ns-app)
#+nil
[objc-runtime::ns-app @(setActivationPolicy) :int 0]
(let* ((application-name [[#@NSProcessInfo @(processInfo)] @(processName)]))
(let* ((menubar [[#@NSMenu @(new)] @(autorelease)])
(app-menu-item [[#@NSMenuItem @(new)] @(autorelease)])
(app-menu [[#@NSMenu @(new)] @(autorelease)])
(quit-name @"Quit")
(key @"q")
(quit-menu-item
[[[#@NSMenuItem @(alloc)]
@(initWithTitle:action:keyEquivalent:) :pointer quit-name :pointer @(terminate?) :string key]
@(autorelease)]))
[menubar @(addItem:) :pointer app-menu-item]
[app-menu @(addItem:) :pointer quit-menu-item]
[app-menu-item @(setSubmenu:) :pointer app-menu]
[objc-runtime::ns-app @(setMainMenu:) :pointer menubar] )
(setf (main-view *application-shim*)
[#@NSStackView @(stackViewWithViews:)
:pointer [[#@NSArray @(alloc)] @(init)]])
(with-point (p (20 20))
(let* ((foreign-rect (make-rect 10 10 120 120))
(the-window (init-window [#@NSWindow @(alloc)] foreign-rect 15 2 nil)))
[(value-for-key the-window "contentView") @(addSubview:) :pointer (main-view *application-shim*)]
[the-window @(cascadeTopLeftFromPoint:) :pointer p]
[the-window @(setTitle:) :pointer application-name]
[the-window @(makeKeyAndOrderFront:) :pointer (cffi:null-pointer)]
[ objc-runtime::ns-app @(activateIgnoringOtherApps:) :boolean t]
(task-thread))))))
(cffi:defcfun (%get-view-frame "objc_msgSend_stret")
:void
(out (:pointer (:struct objc-runtime:ns-rect)))
(class :pointer)
(sel :pointer))
(cffi:defcfun (%init-with-frame "objc_msgSend")
:pointer
(class :pointer)
(sel :pointer)
(frame (:struct objc-runtime:ns-rect)))
(defmacro new-msg-send (selector ((&rest arg-types) return-type))
(let ((arg-syms (mapcar (lambda (_) (gensym (symbol-name _)))
arg-types)))
`(lambda ,(cons 'target arg-syms)
(cffi:foreign-funcall "objc_msgSend"
:pointer target
:pointer ,selector
,@(mapcan #'list arg-types arg-syms)
,return-type))))
(defmacro make-view-dictionary (&rest objc-values)
(alexandria:with-gensyms (selector)
`(let ((,selector (new-msg-send @(dictionaryWithObjectsAndKeys:)
((,@(mapcar (lambda (_) _ :pointer) objc-values) :pointer)
:pointer))))
(funcall ,selector ,@objc-values (cffi:null-pointer)))))
#+(or)
(defun text-view (parent-view)
(let ((text-view [#@NSTextView @(alloc)]))
(trivial-main-thread:with-body-in-main-thread (:blocking nil)
(cffi:with-foreign-pointer (v (cffi:foreign-type-size '(:struct objc-runtime::ns-rect)))
(%get-view-frame v *window-view* @(frame))
(init-with-frame *text-view* v))
[*window-view* @(addSubview:) :pointer *text-view*]?)
(defparameter *view-dictionary*
)
))
#+(or)
(progn
(defparameter *window-view*
[(main-view *application-shim*) @(superview)])
(trivial-main-thread:with-body-in-main-thread (:blocking nil)
[(main-view *application-shim*) @(removeFromSuperview)]?
))