shell.l 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. (
  2. (def ls (fn (do
  3. (split (load "/sd/") [0a])
  4. )))
  5. (struct task
  6. id 0
  7. name "untitled task"
  8. focused 0
  9. z 0
  10. needs-redraw 0
  11. redrawn 0
  12. surface (surface))
  13. (def draw-logo (fn ox oy (do
  14. (let c 0xff8e)
  15. (line fb (pt (+ ox 16) (- oy 38)) (pt (+ ox 16) (- oy 102)) c)
  16. (line fb (pt (+ ox 16) (- oy 102)) (pt (+ ox 80) (- oy 38)) c)
  17. (line fb (pt (+ ox 80) (- oy 38)) (pt (+ ox 80) (- oy 102)) c)
  18. (line fb (pt (+ ox 80) (- oy 102)) (pt (+ ox 144) (- oy 38)) c)
  19. (line fb (pt (+ ox 144) (- oy 38)) (pt (+ ox 144) (- oy 102)) c)
  20. (line fb (pt (+ ox 144) (- oy 102)) (pt (+ ox 208) (- oy 38)) c)
  21. (line fb (pt (+ ox 208) (- oy 38)) (pt (+ ox 208) (- oy 102)) c)
  22. (line fb (pt (+ ox 208) (- oy 102)) (pt (+ ox 272) (- oy 102)) c)
  23. )))
  24. (def desktop-task (fn (t task) (do
  25. (if (sget t needs-redraw) (do
  26. (clear)
  27. (draw-logo (- (/ (sget fb width) 2) 140) (/ (sget fb height) 2))
  28. (draw-logo (- (/ (sget fb width) 2) 139) (/ (sget fb height) 2))
  29. (draw-logo (- (/ (sget fb width) 2) 140) (+ 1 (/ (sget fb height) 2)))
  30. (send screen 0)
  31. ) 0)
  32. 0
  33. )))
  34. (def keyboard (open "/keyboard"))
  35. (import "/sd/os/mouse.l")
  36. (draw-logo (- (/ (sget fb width) 2) 140) (/ (sget fb height) 2))
  37. (blit-str fb unifont "hello" 100 100)
  38. (def tasks (list))
  39. (def new-tasks (list))
  40. (def add-task (fn task-func task-obj task-state (do
  41. (def tasks (cons (list task-func task-obj task-state) tasks))
  42. (list-size tasks)
  43. )))
  44. (def task-func (fn task-obj task-state (print "empty task-func")))
  45. (def paint-task (fn (t task) (do
  46. (if (sget t redrawn)
  47. (do
  48. (let task-surf (sget t surface))
  49. (let x (sget task-surf x))
  50. (let y (sget task-surf y))
  51. (let w (sget task-surf width))
  52. (let h (sget task-surf height))
  53. (if (and (gt w 0) (gt h 0)) (do
  54. (box task-surf 0 0 (- w 1) (- h 1) 0)
  55. (box task-surf 0 0 (- w 1) 20 0)
  56. (box task-surf (- w 16) (- h 16) (- w 1) (- h 1) 0)
  57. (blit-str task-surf unifont (sget t name) 2 2)
  58. (blit fb task-surf)
  59. ) 0)
  60. (sput t redrawn 0))
  61. 0)
  62. 0
  63. )))
  64. (def null-task (task))
  65. (sput null-task id 0) ; "null" task
  66. (def focused-task null-task)
  67. (def point-in-rect (fn px py x y x2 y2
  68. (if (gt px x)
  69. (if (gt py y)
  70. (if (lt px x2)
  71. (if (lt py y2)
  72. 1 0) 0) 0) 0)))
  73. (def mouse-resizing 0)
  74. (def focus-given 0)
  75. (def check-task-focus (fn (t task) (do
  76. (let surf (sget t surface))
  77. (let x (sget surf x))
  78. (let y (sget surf y))
  79. (let x2 (+ x (sget surf width)))
  80. (let y2 (+ y (sget surf height)))
  81. (let z (sget t z))
  82. (if (not mouse-dragging) (do
  83. (def mouse-resizing 0)
  84. (let already-focused (sget t focused))
  85. (if (point-in-rect mouse-x mouse-y x y x2 y2)
  86. (if (not focus-given)
  87. (do
  88. (def focus-given 1)
  89. (sput t focused 1)) 0)
  90. (sput t focused 0))) 0)
  91. (if mouse-dragging
  92. (if (sget t focused)
  93. (do
  94. (boxfill fb x y (- x2 x) (- y2 y) 0xffff)
  95. (if (or mouse-resizing (and (gt mouse-x (- x2 16)) (gt mouse-y (- y2 16))))
  96. (do ; resize
  97. (let nw (+ (sget surf width) mouse-dx))
  98. (let nh (+ (sget surf height) mouse-dy))
  99. (sput surf width nw)
  100. (sput surf height nh)
  101. (sput surf pitch (* 2 nw))
  102. (sput surf pixels (alloc (* 2 (* nw nh))))
  103. (sput t needs-redraw 1)
  104. (def mouse-resizing 1) ; sticky
  105. )
  106. (do ; move
  107. (sput surf x (+ mouse-dx x))
  108. (sput surf y (+ mouse-dy y))) )
  109. (sput t redrawn 1) ) 0) 0)
  110. 0
  111. )))
  112. (def focused-at-mouse (fn (t task) (do
  113. (let surf (sget t surface))
  114. (let x (sget surf x))
  115. (let y (sget surf y))
  116. (let x2 (+ x (sget surf width)))
  117. (let y2 (+ y (sget surf height)))
  118. (if (point-in-rect mouse-x mouse-y x y x2 y2)
  119. (sget t focused) 0)
  120. )))
  121. (def run-tasks (fn (do
  122. (let tl tasks)
  123. (let new-tl (list))
  124. (let i 0)
  125. (let highest-z-at-mouse 0)
  126. (def focus-given 0)
  127. (while (car tl) (do
  128. (let task-item (car tl))
  129. (let task-obj (car (cdr task-item)))
  130. (def focus-given (or focus-given (focused-at-mouse task-obj)))
  131. (let tl (cdr tl))
  132. ))
  133. (let tl tasks)
  134. (while (car tl) (do
  135. (let task-item (car tl))
  136. (def task-func (car task-item))
  137. (let task-obj (car (cdr task-item)))
  138. (let task-state (car (cdr (cdr task-item))))
  139. (check-task-focus task-obj)
  140. (task-func task-obj task-state)
  141. (paint-task task-obj)
  142. (let i (+ i 1))
  143. (let tl (cdr tl))
  144. ))
  145. )))
  146. ;(def zz (fn (import "/sd/tests/gtn.l")))
  147. (def cursor-blink 0)
  148. (def cursor-blink-delay 20)
  149. (import "/sd/os/repl.l")
  150. (import "/sd/os/editor.l")
  151. (def make-surface (fn x y w h (do
  152. (let surf (new surface))
  153. (sput surf pixels (alloc (* 2 (* w h))))
  154. (sput surf x x)
  155. (sput surf y y)
  156. (sput surf width w)
  157. (sput surf height h)
  158. (sput surf pitch (shl w 1))
  159. surf
  160. )))
  161. ;(add-task repl-task (repl-make 1 432 32 200 300))
  162. (def max-task-id 0)
  163. (def spawn-editor (fn x y title (do
  164. (let my-editor (new editor))
  165. (let my-editor-task (new task))
  166. (sput my-editor-task id (+ max-task-id 1))
  167. (sput my-editor-task name title)
  168. (sput my-editor buffer " ")
  169. (sput my-editor-task focused 0)
  170. (sput my-editor-task surface (make-surface x y 400 240))
  171. (add-task editor-task my-editor-task my-editor)
  172. (def mask-task-id (+ max-task-id 1))
  173. )))
  174. (def spawn-repl (fn x y title (do
  175. (let my-repl (new repl))
  176. (let my-task (new task))
  177. (sput my-task id (+ max-task-id 1))
  178. (sput my-task name title)
  179. ;(sput my-repl buffer " ")
  180. (sput my-task focused 0)
  181. (sput my-task surface (make-surface x y 400 240))
  182. (let s (sget my-task surface))
  183. (boxfill s 0 0 400 240 0xffff)
  184. (sput my-task redrawn 1)
  185. (add-task repl-task my-task my-repl)
  186. (def mask-task-id (+ max-task-id 1))
  187. )))
  188. (spawn-editor 32 32 "editor ")
  189. (spawn-repl 32 300 "repl ")
  190. (def mouse-task (new task))
  191. (sput mouse-task id (+ max-task-id 1))
  192. (sput mouse-task name "mouse")
  193. (add-task mouse-func mouse-task 0)
  194. (def launched 0)
  195. (def launcher-func (fn (t task) dummy (do
  196. (let f (sget t focused))
  197. (if (* f (* (not launched) mouse-btn)) (do
  198. (def launched 1)
  199. (spawn-editor 100 100 "new editor")
  200. ) 0)
  201. (if (not mouse-btn) (def launched 0) 0)
  202. 0
  203. )))
  204. (def launcher-task (new task))
  205. (sput launcher-task id (+ max-task-id 1))
  206. (sput launcher-task name "icon")
  207. (sput launcher-task surface (make-surface 0 0 32 32))
  208. (sput launcher-task redrawn 1)
  209. ;(add-task launcher-func launcher-task 0)
  210. (def main (fn (while 1 (do
  211. (run-tasks)
  212. (send screen 0)
  213. (gc)
  214. (def cursor-blink (% (+ cursor-blink 1) cursor-blink-delay))
  215. ))))
  216. (main)
  217. )