<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 084console.mu</title>
<meta name="Generator" content="Vim/7.3">
<meta name="plugin-version" content="vim7.3_v6">
<meta name="syntax" content="none">
<meta name="settings" content="use_css">
<style type="text/css">
<!--
pre { font-family: monospace; color: #eeeeee; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color: #080808; }
.muControl { color: #c0a020; }
.Delimiter { color: #800080; }
.Special { color: #c00000; }
.Constant { color: #00a0a0; }
.muRecipe { color: #ff8700; }
.muData { color: #ffff00; }
.Comment { color: #9090ff; }
-->
</style>
</head>
<body>
<pre>
<span class="Comment"># Wrappers around interaction primitives that take a potentially fake object</span>
<span class="Comment"># and are thus easier to test.</span>
<span class="muData">exclusive-container</span> event [
text:character
keycode:number <span class="Comment"># keys on keyboard without a unicode representation</span>
touch:touch-event <span class="Comment"># mouse, track ball, etc.</span>
resize:resize-event
<span class="Comment"># update the assume-console handler if you add more variants</span>
]
<span class="muData">container</span> touch-event [
type:number
row:number
column:number
]
<span class="muData">container</span> resize-event [
width:number
height:number
]
<span class="muData">container</span> console [
current-event-index:number
events:address:shared:array:event
]
<span class="muRecipe">def</span> new-fake-console events:address:shared:array:event<span class="muRecipe"> -> </span>result:address:shared:console [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
result:address:shared:console<span class="Special"> <- </span>new <span class="Constant">console:type</span>
buf:address:address:shared:array:event<span class="Special"> <- </span>get-address *result, <span class="Constant">events:offset</span>
*buf<span class="Special"> <- </span>copy events
idx:address:number<span class="Special"> <- </span>get-address *result, <span class="Constant">current-event-index:offset</span>
*idx<span class="Special"> <- </span>copy <span class="Constant">0</span>
]
<span class="muRecipe">def</span> read-event console:address:shared:console<span class="muRecipe"> -> </span>result:event, console:address:shared:console, found?:boolean, quit?:boolean [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> console
current-event-index:address:number<span class="Special"> <- </span>get-address *console, <span class="Constant">current-event-index:offset</span>
buf:address:shared:array:event<span class="Special"> <- </span>get *console, <span class="Constant">events:offset</span>
<span class="Delimiter">{</span>
max:number<span class="Special"> <- </span>length *buf
done?:boolean<span class="Special"> <- </span>greater-or-equal *current-event-index, max
<span class="muControl">break-unless</span> done?
dummy:address:shared:event<span class="Special"> <- </span>new <span class="Constant">event:type</span>
<span class="muControl">return</span> *dummy, console/same-as-ingredient:<span class="Constant">0</span>, <span class="Constant">1/found</span>, <span class="Constant">1/quit</span>
<span class="Delimiter">}</span>
result<span class="Special"> <- </span>index *buf, *current-event-index
*current-event-index<span class="Special"> <- </span>add *current-event-index, <span class="Constant">1</span>
<span class="muControl">return</span> result, console/same-as-ingredient:<span class="Constant">0</span>, <span class="Constant">1/found</span>, <span class="Constant">0/quit</span>
<span class="Delimiter">}</span>
switch <span class="Comment"># real event source is infrequent; avoid polling it too much</span>
result:event, found?:boolean<span class="Special"> <- </span>check-for-interaction
<span class="muControl">return</span> result, console/same-as-ingredient:<span class="Constant">0</span>, found?, <span class="Constant">0/quit</span>
]
<span class="Comment"># variant of read-event for just keyboard events. Discards everything that</span>
<span class="Comment"># isn't unicode, so no arrow keys, page-up/page-down, etc. But you still get</span>
<span class="Comment"># newlines, tabs, ctrl-d..</span>
<span class="muRecipe">def</span> read-key console:address:shared:console<span class="muRecipe"> -> </span>result:character, console:address:shared:console, found?:boolean, quit?:boolean [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
x:event, console, found?:boolean, quit?:boolean<span class="Special"> <- </span>read-event console
<span class="muControl">return-if</span> quit?, <span class="Constant">0</span>, console/same-as-ingredient:<span class="Constant">0</span>, found?, quit?
<span class="muControl">return-unless</span> found?, <span class="Constant">0</span>, console/same-as-ingredient:<span class="Constant">0</span>, found?, quit?
c:address:character<span class="Special"> <- </span>maybe-convert x, <span class="Constant">text:variant</span>
<span class="muControl">return-unless</span> c, <span class="Constant">0</span>, console/same-as-ingredient:<span class="Constant">0</span>, <span class="Constant">0/found</span>, <span class="Constant">0/quit</span>
<span class="muControl">return</span> *c, console/same-as-ingredient:<span class="Constant">0</span>, <span class="Constant">1/found</span>, <span class="Constant">0/quit</span>
]
<span class="muRecipe">def</span> send-keys-to-channel console:address:shared:console, chan:address:shared:channel, screen:address:shared:screen<span class="muRecipe"> -> </span>console:address:shared:console, chan:address:shared:channel, screen:address:shared:screen [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
c:character, console, found?:boolean, quit?:boolean<span class="Special"> <- </span>read-key console
<span class="muControl">loop-unless</span> found?
<span class="muControl">break-if</span> quit?
assert c, <span class="Constant">[invalid event, expected text]</span>
screen<span class="Special"> <- </span>print screen, c
chan<span class="Special"> <- </span>write chan, c
<span class="muControl">loop</span>
<span class="Delimiter">}</span>
]
<span class="muRecipe">def</span> wait-for-event console:address:shared:console<span class="muRecipe"> -> </span>console:address:shared:console [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
_, console, found?:boolean<span class="Special"> <- </span>read-event console
<span class="muControl">loop-unless</span> found?
<span class="Delimiter">}</span>
]
<span class="Comment"># use this helper to skip rendering if there's lots of other events queued up</span>
<span class="muRecipe">def</span> has-more-events? console:address:shared:console<span class="muRecipe"> -> </span>result:boolean [
<span class="Constant">local-scope</span>
<span class="Constant">load-ingredients</span>
<span class="Delimiter">{</span>
<span class="muControl">break-unless</span> console
<span class="Comment"># fake consoles should be plenty fast; never skip</span>
<span class="muControl">return</span> <span class="Constant">0/false</span>
<span class="Delimiter">}</span>
result<span class="Special"> <- </span>interactions-left?
]
</pre>
</body>
</html>