-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmrl-shell
executable file
·367 lines (307 loc) · 9.3 KB
/
mrl-shell
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
#!/usr/bin/env luajit
-- a traditional window manager, titlebar-draggable, maximizable and resizable
-- windows
-- redraw of single windows cause loss of events elsewhere
local S = require 'syscall'
function which(command)
local ret = ''
local PATH = '.:' .. S.getenv('PATH')
for str in string.gmatch(PATH, "([^:]+)") do
if S.stat(str .. '/' .. command) then
local cmpstr = str
if cmpstr == '.' then cmpstr = S.getcwd() end
return cmpstr .. '/' .. command
end
end
return nil
end
if false then
-- using the restarter on the host is only advisable
-- when all clients already are launched, since the use
-- of environment variables by the restarter mechanism
-- interacts badly with launching clients of an mmm compositor.
S.setenv('MRG_RESTARTER','yes')
S.setenv('MRG_BACKEND','mmm')
end
local tiled = false
local os = require('os')
local string = require('string')
local Mrg = require('mrg')
local Math = require('math')
local mrg = Mrg.new(-1, -1);
local mrg = Mrg.new(1024, 768);
local host = mrg:host_new("/tmp/mrg")
local notifications = {
-- {text='welcome to microraptor gui'},
}
local show_apps = false;
local applications = {
{text='applications', cb=function() show_apps=false mrg:queue_draw(nil) end },
{text='toggle tiled', cb=function() tiled=not tiled mrg:queue_draw(nil) end },
{text='filesystem', command=which('mrl-view') .. ' `pwd` &'},
{text='help', command='mrg browser &'},
{text='terminal', command='mrg terminal&'},
{text='flipping game', command=which('mrl-flipgame') .. ' &'},
{text='paddlewar', command=which('mrl-paddlewar') .. ' &'},
}
function string.has_prefix(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
local css = [[
document {background-color:#111;}
notifications {
background-color: #000d;
padding: 0.5em;
padding-top: 0;
border: 2px solid white;
display: block;
width: 20em;
margin-left: auto;
margin-right: auto;
}
notification {
color: white;
margin-bottom: 0.1em;
display: block;
}
apps {
background-color: #000d;
padding: 0.5em;
padding-top: 0;
border: 2px solid white;
width: 10em;
display: block;
}
application {
color: white;
margin-bottom: 1.4em;
display: block;
}
]];
function table.copy(t)
local u = { }
for k, v in pairs(t) do u[k] = v end
return setmetatable(u, getmetatable(t))
end
function table.reverse(t)
local l = table.getn(t) -- table length
local j = l
for i = 1, l / 2 do
t[i], t[j] = t[j], t[i]
j = j - 1
end
end
--[[
local mrg2 = Mrg.new(200, 200, "mem")
mrg2:set_title ("task list")
mrg2:set_ui(function(mrg)
mrg:print("fnord")
end)
host:add_client_mrg(mrg2, 40, 40)
host:add_client_mrg(mrg2, 40, 140)
]]
function toggle_fullscreen(event)
event.mrg:set_fullscreen(not event.mrg:is_fullscreen())
show_apps = false
end
mrg:set_ui(
function()
local cr = mrg:cr()
local em = mrg:em()
mrg:start("host")
mrg:set_xy(0,0)
mrg:start("header")
mrg:start("time")
mrg:print(os.date("%a %d %b %H:%M"))
mrg:close()
mrg:start("applications")
mrg:text_listen(Mrg.PRESS, function(event)
show_apps = true
mrg:queue_draw(nil)
end)
mrg:print("applications")
mrg:text_listen_done()
mrg:close()
mrg:close()
local old_focused = host:focused()
-- host:set_focused(nil) -- (render_sloppy sets focused as part of rendering)
local clients = host:clients()
local x = 0
local y = 40
local w = 160
local h = 120
em = mrg:em()
for i, client in ipairs(clients) do
local x, y = client:xy()
local w, h = client:size()
x = Math.floor(x)
y = Math.floor(y)
client:render(mrg, x, y)
em = mrg:em()
if not client:get_value('borderless') then
if old_focused == client then
mrg:start_with_style('client.focused', string.format('left:%dpx;top:%dpx;width:%dpx;height:%dpx', x-1, y-1, w, h))
else
mrg:start_with_style('client', string.format('left:%dpx;top:%dpx;width:%dpx;height:%dpx', x-1, y-1, w, h))
end
mrg:start_with_style('title',
string.format('left:%dpx;top:%dpx;width:%dpx;height:%dpx;border-width:1px', x-2, y- 1.5 * em, w-1, em * 1.0))
cr:rectangle(x-2, y- 1.5 * em, w-1, em * 1.5)
-- set up a coordevent blocker behind titlebar
mrg:listen(Mrg.COORD, function(event) event:stop_propagate() end)
local loc_x, loc_y = client:xy()
mrg:listen(Mrg.DRAG_MOTION + Mrg.DRAG_PRESS,
function(event)
if event.type == Mrg.DRAG_PRESS then
host:set_focused(client)
client:raise_top()
elseif event.type == Mrg.DRAG_MOTION then
loc_x, loc_y = loc_x + event.delta_x, loc_y + event.delta_y
if loc_y <= 1.2 * em then loc_y = 1.2 * em end
client:set_xy(loc_x, loc_y)
end
mrg:queue_draw(NULL)
event:stop_propagate()
return 0
end)
mrg:print(client:title())
if not tiled then
mrg:start('close')
mrg:text_listen(Mrg.PRESS, function(event) client:kill() end)
mrg:print('X')
mrg:text_listen_done()
mrg:close()
mrg:start('max')
mrg:text_listen(Mrg.PRESS, function(event) client:maximize() end)
mrg:print(' ')
mrg:text_listen_done()
mrg:close()
end
mrg:close()
cr:rectangle(x + w - 20, y + h - 20, 23, 23)
local loc_w, loc_h = client:size()
mrg:listen(Mrg.DRAG_PRESS + Mrg.DRAG_MOTION, function(event)
if event.type == Mrg.DRAG_MOTION then
loc_w, loc_h = loc_w + event.delta_x, loc_h + event.delta_y;
client:set_size(loc_w, loc_h)
end
mrg:queue_draw(NULL)
event:stop_propagate()
end)
cr:fill()
mrg:close()
end
if client:has_message() ~= 0 then
local message = client:get_message()
-- by having the all target, the compositor acts as a bus
-- and it is up to the clients to know that they are targetd
if message:has_prefix('all ') then
for i2, client2 in ipairs(clients) do
client2:send_message( message:sub(5,-1) )
end
elseif message:has_prefix('notify ') then
table.insert(notifications, {text=message:sub(9.-1)})
mrg:queue_draw(nil)
end
end
end
host:register_events()
-- setting the positions with one frame lag,
-- but permits doing the interaction rects in the same go
--
if tiled then
local rclients = table.copy(clients)
table.reverse(rclients)
for i, client in ipairs(rclients) do
if i == 1 then
w = mrg:width()/2
h = mrg:height() - 120 -y
x = mrg:width()/2
client:set_xy(x, y)
client:set_size(w,h)
elseif i == 2 then
w = mrg:width()/2
h = mrg:height() - 120 -y
x = 0
client:set_xy(x, y)
client:set_size(w,h)
cr:new_path()
cr:rectangle(w - 2 * em,
mrg:height() - 120 + 12 + 12 - 4 * em,
4 * em, 2.3 *em);
cr:set_source_rgba(0,0,1, 0.1)
mrg:listen(Mrg.TAP, function(event)
client:set_stack_order(0)
event:stop_propagate()
mrg:queue_draw(nil)
end)
cr:fill()
w = 160
h = 120 - 12
x = 0
y = mrg:height() - h + 12
else
client:set_xy(x, y)
client:set_size(w,h)
cr:rectangle(x,y - 18,w,h + 18)
cr:set_source_rgba(0,0,1,0.1)
mrg:listen(Mrg.COORD, function(event)
event:stop_propagate()
end)
mrg:listen(Mrg.TAP, function(event)
client:set_stack_order(1)
end)
--cr:fill()
cr:new_path()
x = x + w
if (x + w > mrg:width()) then
x = 0
y = y + h + 18
end
end
end
end
mrg:add_binding("F10", nil, "quit", function() mrg:quit() end)
mrg:add_binding("F11", nil, "fullscreen", toggle_fullscreen)
if #notifications > 0 then
mrg:set_xy(0,0)
mrg:start('notifications')
mrg:text_listen(Mrg.TAP, function(event)
notifications={}
mrg:queue_draw(nil)
end)
for i, notification in ipairs(notifications) do
mrg:start('notification')
mrg:print(notification.text)
mrg:close()
end
mrg:text_listen_done()
mrg:close()
end
if show_apps and #applications > 0 then
mrg:set_xy(0,0)
mrg:start('apps')
for i, application in ipairs(applications) do
mrg:start('application')
mrg:text_listen(Mrg.TAP, function(event)
if application.cb then
application.cb()
elseif application.command then
os.execute (application.command)
show_apps = false
S.nanosleep(0.1) -- XXX: eeek, avoiding construction race... needs fixing in mmm
end
mrg:queue_draw(nil)
end)
mrg:print(application.text)
mrg:text_listen_done()
mrg:close()
end
mrg:close()
end
mrg:close()
Mrg.draw_keyboard(mrg)
end
)
mrg:css_set(css)
mrg:main()