-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLive.lua
More file actions
executable file
·144 lines (127 loc) · 4.1 KB
/
Copy pathLLive.lua
File metadata and controls
executable file
·144 lines (127 loc) · 4.1 KB
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
#!/usr/bin/env luajit
--[[----------------------------------------------------------------------------
live.lua
Luce Live Coding
@alias meta
@author Christophe Berbizier (cberbizier@peersuasive.com)
@license GPLv3
@copyright
(c) 2014, Peersuasive Technologies
------------------------------------------------------------------------------]]
local LUCE_EVENT = "PSM.EVENTS.LUCE.RELOAD "
local LUCE_ERROR = "PSM.EVENTS.LUCE.ERROR "
local port = 20087
local zmq = require"lzmq"
local zpoller = require"lzmq.poller"
local assert = zmq.assert
local htime = require"htime"
local htime = function() local s, u, n = htime.time(); return s..u end
local function printf(msg, ...)
print(string.format(msg, ...))
end
local app, luce = require"luce.LApplication"("Luce Live Coding", ".", ...) -- set prog to "."
local context = zmq.context(1)
local poller = zpoller.new(1)
local listen = context:socket(zmq.SUB)
listen:set_subscribe(LUCE_EVENT)
listen:set_subscribe(LUCE_ERROR)
assert( listen:bind("tcp://127.0.0.1:"..port), "Can't bind socket" )
local function MainWindow(params)
local app, luce = app, luce
local Colours = luce.Colours
local wsize = {250,40}
local dw = luce:Document("Luce Live Coding: dw")
local mc = luce:MainComponent("Luce Live Coding: mc")
mc:paint(function(g)
g:setFont(12.0)
g:setColour( Colours.grey )
g:drawText("Luce Live Coding - waiting for input",
mc:getLocalBounds(), luce.JustificationType.centred, true);
end)
local K = string.byte
local kc = setmetatable(luce.KeyPress.KeyCodes,{__index=function()return 0 end})
dw:keyPressed(function(k)
local k, m = k:getKeyCode(), k:getModifiers()
if (k==K"Q" or k==K"q") and m:isCommandDown() then
app:exit(0)
elseif (k==K"w" or k==K"W") and (m:isCommandDown() ) then
app:exit(0)
else
return false
end
return true
end)
mc:setSize(wsize)
dw:setContentOwned(mc, true)
dw:setBounds{0,0,wsize[1], wsize[2]}
dw:setVisible(true)
return dw
end
local res = 0
local force = false
local current = nil
local previous_control = ""
local remote_control = nil
local ms = 0
local function cb(socket)
local what = socket:recv()
local data = socket:recv()
local rcontrol = socket:recv()
--ms = tonumber(socket:recv()) * 1000
local itv = socket:recv()
ms = tonumber(itv or 0) * 1000
local force = socket:recv()
force = (force ~= "false") and (force ~= "0")
if(what == LUCE_ERROR)then
print(string.format("***** %s *****", os.date()))
print(string.format("ERROR: %s", data))
return
end
if not(force) and (data == current)then
print"D: (no change)"
return
end
current = data
local chunk, e = loadstring(data)
print(string.format("***** %s *****", os.date()))
if not(chunk) then
print(string.format("ERROR: %s", e))
else
if(rcontrol ~= "")then
if not(previous_control==rcontrol)then
local rcontrol_chunk, err = loadstring(rcontrol)
if not(rcontrol_chunk)then
print(string.format("ERROR: %s", err))
return
end
print("D: reloading control")
previous_control = rcontrol
remote_control = rcontrol_chunk
else
print("D: NOT reloading control")
end
end
local r, status, err = pcall(luce.reload, luce, chunk)
if not(r) then
print(string.format("ERROR: %s", status))
elseif not (status) then
print(string.format("ERROR: %s", err))
else
print("OK")
end
end
end
poller:add(listen, zmq.POLLIN, cb)
local lastTime, now = htime(), 0
res = app:start(MainWindow, { function(...)
poller:poll(0)
local now = htime()
local control = remote_control
if (control) and ((ms==0) or (ms>0 and ms < (now - lastTime))) then
control()
lastTime = now
end
end, 1})
listen:close()
context:term()
return res