-
Notifications
You must be signed in to change notification settings - Fork 6
/
lws
765 lines (729 loc) · 21.8 KB
/
lws
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
local unpack = unpack or table.unpack
local function saveDimensions(proc)
--saves window dimensions when closing windows
if proc.path and proc.windows and #proc.windows > 0 then
local wInfo = {}
if proc.windows[#proc.windows].maximized then
wInfo.x = proc.windows[#proc.windows].normalSize.x
wInfo.y = proc.windows[#proc.windows].normalSize.y
wInfo.w = proc.windows[#proc.windows].normalSize.w
wInfo.h = proc.windows[#proc.windows].normalSize.h
else
wInfo.x = proc.windows[#proc.windows].x
wInfo.y = proc.windows[#proc.windows].y
wInfo.w = proc.windows[#proc.windows].w
wInfo.h = proc.windows[#proc.windows].h
end
wInfo.max = tostring(proc.windows[#proc.windows].maximized)
local lines = {}
if fs.exists(fs.combine(LyqydOS.folderPath, ".lyqwin")) then
local dimsFile = io.open(fs.combine(LyqydOS.folderPath, ".lyqwin"), "r")
if dimsFile then
for line in dimsFile:lines() do
if string.match(line, "%d+,%d+,%d+,%d+,%a+,(.*)") ~= proc.path then
table.insert(lines, line)
end
end
dimsFile:close()
end
end
table.insert(lines, wInfo.x..","..wInfo.y..","..wInfo.w..","..wInfo.h..","..wInfo.max..","..proc.path)
winDims = io.open(fs.combine(LyqydOS.folderPath, ".lyqwin"), "w")
if winDims then
for _, line in ipairs(lines) do
winDims:write(line.."\n")
end
winDims:close()
end
end
end
local function windowAction(pID, win)
--index 2 should be the pID and 3 should be the window, forward compat for child windows.
if process.list[process.focus] then
local parent = process.focus
local proc = process.list[parent]
local lwsProc = process.getFromName("lws")
local thisProc = process.this()
local x, y
if win.minimized then
x, y = 1, 2
else
x, y = win.x, win.y + 1
end
local lwsWin = window.new(15, 9, x, y, "Action Menu", process.compositor:newBuffer(process.id()))
thisProc:addWindow(lwsWin)
lwsWin:setMode("ephemeral")
lwsWin:setType("menu")
lwsWin:setTheme(LyqydOS.window.theme)
process.focus = process.id()
local _old = term.redirect(lwsWin.redirect)
local success, selection = pcall(menu.select, {"Restore", "Move", "Resize", "Minimize", "Maximize", "Close", "Cancel"})
if _old then
term.redirect(_old)
else
term.restore()
end
thisProc:removeWindow(lwsWin)
if success then
if selection == 1 then
--restore
if win.maximized then
win:restore()
proc:queue("window_resize")
proc:queue("term_resize")
elseif win.minimized then
win:unMinimize()
proc:toFront(win)
win:draw()
end
elseif selection == 2 then
--move
while true do
e, p1, p2, p3 = os.pullEvent()
if e == "key" then
if p1 == 200 then
--up
win:move(win.x, win.y - 1)
win:draw()
elseif p1 == 203 then
--left
win:move(win.x - 1, win.y)
win:draw()
elseif p1 == 205 then
--right
win:move(win.x + 1, win.y)
win:draw()
elseif p1 == 208 then
--down
win:move(win.x, win.y + 1)
win:draw()
elseif p1 == 28 then
--enter
break
end
elseif e == "mouse_click" and p1 == 1 then
--mouse click
win:move(p2, p3)
win:draw()
break
end
end
elseif selection == 3 then
--resize
win.maximized = false
while true do
e, p1 = os.pullEvent()
if e == "key" then
if p1 == 200 then
--up
win:resize(win.w, win.h - 1)
proc:queue("window_resize")
proc:queue("term_resize")
elseif p1 == 203 then
--left
win:resize(win.w - 1, win.h)
proc:queue("window_resize")
proc:queue("term_resize")
elseif p1 == 205 then
--right
win:resize(win.w + 1, win.h)
proc:queue("window_resize")
proc:queue("term_resize")
elseif p1 == 208 then
--down
win:resize(win.w, win.h + 1)
proc:queue("window_resize")
proc:queue("term_resize")
elseif p1 == 28 then
--enter
break
end
end
end
elseif selection == 4 then
--minimize
if not win.minimized then
win:minimize()
--also move the window to the bottom of the buffer stack.
proc:toBack()
win:draw()
end
elseif selection == 5 then
--maximize
if not win.maximized then
win:maximize()
if win.minimized then win:unMinimize() end
proc:queue("window_resize")
proc:queue("term_resize")
end
elseif selection == 6 then
--close
proc:queue("window_close", win)
if win.closable then
lwsProc:queue("close_window", pID, win)
end
end
process.focus = parent
end
end
end
local function windowSelect()
local oldFocus = process.focus
local lws = process.id()
local lwsProc = process.list[lws]
local lwsWin = window.new(19, 11, 1, 1, "Select Window", process.compositor:newBuffer(lws))
lwsProc:addWindow(lwsWin)
lwsWin:setMode("ephemeral")
lwsWin:setTheme(LyqydOS.window.theme)
lwsWin:setType("menu")
process.focus = lws
local _old = term.redirect(lwsWin.redirect)
local pList, menuList = {}, {}
for pID, proc in ipairs(process.list) do
if proc and proc.windows and proc.windows.selectable and #proc.windows > 0 then
table.insert(pList, pID)
table.insert(menuList, proc.windows[#proc.windows].caption)
end
end
--removes self from list of running programs.
table.remove(menuList)
table.remove(pList)
table.insert(menuList, "Cancel")
local success, selection = pcall(menu.select, menuList)
if _old then
term.redirect(_old)
else
term.restore()
end
lwsProc:removeWindow(lwsWin)
if success then
if pList[selection] then
process.focus = pList[selection]
local proc = process.list[process.focus]
local win = proc.windows[#proc.windows]
if win.minimized then win:unMinimize() end
proc:toFront()
else
process.focus = oldFocus
end
end
end
local function findWindow(x, y)
for i = #process.compositor.bufferStack, 1, -1 do
for pID, proc in ipairs(process.list) do
if proc and proc.windows then
for j = #proc.windows, 1, -1 do
local win = proc.windows[j]
if win.target.buffer == process.compositor.bufferStack[i] and not win.minimized then
if x >= win.x and x <= win.x + win.w - 1 and y >= win.y and y <= win.y + win.h - 1 then
return pID, proc, j, win
end
end
end
end
end
end
end
local lws = process.id()
local lwsProc = process.list[lws]
local watchMouseDragging = false
local mouseStartX = nil
local mouseStartY = nil
local mouseDragAction = nil
--injection magic table, localized here for use later.
local procTable = getmetatable(process.this()).__index
local oldPcheck, oldPkill, oldPremove, oldPsetup, oldPpost
process.addHook({"char"})
process.addHook({"key"})
--process.addHook({"key", 61})
--process.addHook({"key", 62})
process.addHook({"key_up"})
process.addHook({"mouse_click"})
process.addHook({"mouse_drag"})
process.addHook({"mouse_scroll"})
process.addHook({"mouse_up"})
do
if LyqydOS.shell and LyqydOS.shell.multishell then
LyqydOS.shell.multishell.getTitle = function(n)
if process.list[n] and process.list[n].windows and #process.list[n].windows >= 1 then
return process.list[n].windows[#process.list[n].windows].caption
end
end
LyqydOS.shell.multishell.setTitle = function(n, title)
if process.list[n] and process.list[n].windows and #process.list[n].windows >= 1 then
process.list[n].windows[#process.list[n].windows].caption = title
end
end
LyqydOS.shell.multishell.getCount = function()
return #process.list
end
LyqydOS.shell.multishell.launch = function(env, path, ...)
local focus = process.focus
local args = {...}
local startProcess = process.id()
if path then
local w, h, x, y, max = 19, 9, 1, 1, "false"
windowDimensions = {}
winDims = io.open(fs.combine(LyqydOS.folderPath, ".lyqwin"), "r")
if winDims then
for line in winDims:lines() do
if string.match(line, "%d+,%d+,%d+,%d+,%a+,(.*)") == path then
x, y, w, h, max = string.match(line, "(%d+),(%d+),(%d+),(%d+),(%a+),")
break
end
end
winDims:close()
end
local win = window.new(w, h, x, y, fs.getName(path), process.compositor:newBuffer(#process.list + 1))
if process.compositor.target.isColor() then
win:setType("window")
end
win:setTheme(LyqydOS.window.theme)
if max == "true" then win:maximize() end
process.new(function() os.run(env, shell.resolveProgram("lsh"), path, unpack(args)) end, shell.resolveProgram("lsh"), nil, win)
if coroutine.status(process.list[process.focus].thread) == "dead" then
process.getFromName("lws"):queue("close_window", process.focus)
process.focus = startProcess
end
if focus then
process.focus = focus
end
end
end
LyqydOS.shell.multishell.getCurrent = function()
return process.id()
end
LyqydOS.shell.multishell.getFocus = function()
return process.focus
end
LyqydOS.shell.multishell.setFocus = function(n)
process.focus = n
end
LyqydOS.shell.shell.openTab = function(...)
local env = {shell = LyqydOS.shell.shell, multishell = LyqydOS.shell.multishell}
local command = table.concat({...}, " ")
local args = {}
for match in string.gmatch(command, "[^ \t]+") do
table.insert(args, match)
end
local path = table.remove(args, 1)
LyqydOS.shell.multishell.launch(env, path, unpack(args))
end
LyqydOS.shell.shell.switchTab = function(n)
LyqydOS.shell.multishell.setFocus(n)
end
end
do
function process.getWindow()
if process.active and process.list[process.active] and process.list[process.active].windows and #process.list[process.active].windows >= 1 then
return process.list[process.active].windows[#process.list[process.active].windows]
else
return nil, "No window!"
end
end
function process.nextWindow(default)
local nextBuf = process.compositor.bufferStack[#process.compositor.bufferStack]
for pID, proc in ipairs(process.list) do
if proc then
for i = 1, #proc.windows do
if proc.windows[i].target.buffer == nextBuf then
return pID
end
end
end
end
return default
end
--injection magic.
function procTable.addWindow(self, win)
local newWindow = true
for i = 1, #self.windows do
if self.windows[i] == win then
newWindow = false
break
end
end
if newWindow then
table.insert(self.windows, win)
end
end
function procTable.removeWindow(self, win)
local winFound = false
for i = 1, #self.windows do
if self.windows[i] == win then
table.remove(self.windows, i)
winFound = true
break
end
end
if winFound then
win:destroy()
end
end
function procTable.toFront(self, win)
--win is optional, if not specified will maintain order of windows.
if win then
for i = 1, #self.windows do
if self.windows[i] == win then
table.insert(self.windows, table.remove(self.windows, i))
break
end
end
end
for i = 1, #self.windows do
for j = 1, #process.compositor.bufferStack do
if process.compositor.bufferStack[j] == self.windows[i].target.buffer then
process.compositor:toFront(j)
break
end
end
end
end
function procTable.toBack(self, win)
--win is optional, will move that window to behind the rest of them, as well as moving the whole set to the bottom of the stack.
if #self.windows >= 1 then
if win then
for i = 1, #self.windows do
if self.windows[i] == win then
table.insert(self.windows, 1, table.remove(self.windows, i))
break
end
end
end
for i = 1, #self.windows do
for j = 1, #process.compositor.bufferStack do
if self.windows[i].target.buffer == process.compositor.bufferStack[j] then
process.compositor:toBack(j)
break
end
end
end
end
process.focus = process.next()
if process.list[process.focus] then
process.list[process.focus]:toFront()
end
end
--localized above to allow resetting later.
oldPcheck = procTable.check
function procTable.check(self)
if not oldPcheck(self) then lwsProc:queue("close_window", self.id); return false end
if self.windows then
for i = 1, #self.windows do
if self.windows[i].mode == "ephemeral" and (i < #self.windows or self.id ~= process.focus) then
--kill the individual window
lwsProc:resume({"close_window", self.id, self.windows[i]})
end
end
end
return true
end
--localized above to allow resetting later,
oldPkill = procTable.kill
function procTable.kill(self)
if self.windows then
for _, win in ipairs(self.windows) do
if win.destroy then
win:destroy()
end
end
end
return oldPkill(self)
end
oldPremove = procTable.remove
function procTable.remove(self)
oldPremove(self)
if process.focus then
process.list[process.focus]:toFront()
end
end
oldPsetup = procTable.setup
function procTable.setup(self, path, redirect, ...)
local args = {...}
self.windows = {
selectable = true,
}
if args[1] then
table.insert(self.windows, args[1])
self.redirect = args[1].redirect
process.focus = #process.list
elseif redirect then
self.redirect = redirect
else
self.redirect = process.nullRedirect
end
return oldPsetup(self, unpack(args))
end
oldPpost = procTable.post
function procTable.post(self, ...)
if self.windows then
for _, win in ipairs(self.windows) do
win:draw()
end
end
return oldPpost(self, ...)
end
end
end
while true do
local event = {os.pullEventRaw()}
if event[1] == "key" then
if event[2] == 61 then
if process.focus and process.list[process.focus] then
lwsProc:queue("select_window")
end
elseif event[2] == 62 then
if process.focus and process.list[process.focus] then
lwsProc:queue("window_action", process.focus, process.list[process.focus].windows[#process.list[process.focus].windows])
end
elseif not process.eventHooks.key[event[2]] then
if process.focus and process.list[process.focus] then
process.list[process.focus]:resume(event)
end
end
elseif event[1] == "key_up" or event[1] == "char" and not process.eventHooks[event[1]][event[2]] then
if process.focus and process.list[process.focus] then
process.list[process.focus]:resume(event)
end
elseif event[1] == "mouse_click" then
watchMouseDragging = false
local windowFound = false
local pID, proc, winNum, win = findWindow(event[3], event[4])
if pID and proc and win then
if process.focus ~= pID then
process.focus = pID
proc:toFront(win)
elseif winNum < #proc.windows then
local modalFound = false
for k = #proc.windows, 1, -1 do
if proc.windows[k].mode == "modal" then
modalFound = true
break
elseif proc.windows[k].mode == "normal" then
break
end
end
if not modalFound then
--no modal window active in this application, so move window to top.
proc:toFront(win)
end
end
local x, y = event[3] - win.x, event[4] - win.y
if win.borderless then
--mouse click, resume.
x = x + 1
y = y + 1
if x > 0 and x < win.w + 1 and y > 0 and y < win.h + 1 then
event[3], event[4] = x, y
proc:resume(event)
end
else
if x > 0 and x < win.w - 1 and y > 0 and y < win.h - 1 then
--mouse click resume
event[3], event[4] = x, y
proc:resume(event)
elseif y == 0 and event[2] == 2 then
--right click on window title bar, open window menu.
if win.mode ~= "ephemeral" then
lwsProc:queue("window_action", pID, win)
end
elseif (win.windowType == "modal" or win.windowType == "window") and y == 0 and event[2] == 1 then
local function findTransformedX(position)
if position < 0 then
return win.w + position
elseif position > 0 then
return position - 1
end
end
if x == findTransformedX(win.decorations.closePos) then
--close window
proc:queue("window_close", win)
if win.closable then
lwsProc:queue("close_window", pID, win)
end
elseif win.windowType == "window" then
if x == findTransformedX(win.decorations.minPos) then
--minimize
win:minimize()
local allMin = true
for k = 1, #proc.windows do
if proc.windows[k].minimized == false then
allMin = false
end
end
if allMin then
proc:toBack()
end
win:draw()
elseif x == findTransformedX(win.decorations.maxPos) then
if win.resizable then
if win.maximized then
win:restore()
proc:queue("window_resize")
proc:queue("term_resize")
win:draw()
else
win:maximize()
proc:queue("window_resize")
proc:queue("term_resize")
win:draw()
end
end
else
watchMouseDragging = true
mouseDragAction = "move"
mouseStartX = event[3]
mouseStartY = event[4]
end
else
watchMouseDragging = true
mouseDragAction = "move"
mouseStartX = event[3]
mouseStartY = event[4]
end
elseif y == 0 and event[2] == 1 and win.windowType ~= "menu" and not proc.special then
--clicked on titlebar, set variables to see if we drag.
watchMouseDragging = true
mouseDragAction = "move"
mouseStartX = event[3]
mouseStartY = event[4]
elseif x == win.w - 1 and y == win.h - 1 and event[2] == 1 and not proc.special then
--clicked on lower right corner, resize.
watchMouseDragging = true
mouseDragAction = "resize"
mouseStartX = event[3]
mouseStartY = event[4]
end
end
end
elseif event[1] == "mouse_drag" then
--mouse is dragging, see if we need to do something with windows.
if watchMouseDragging then
local proc = process.list[process.focus]
if mouseDragAction == "move" then
proc.windows[#proc.windows]:move(proc.windows[#proc.windows].x + (event[3] - mouseStartX), proc.windows[#proc.windows].y + (event[4] - mouseStartY))
mouseStartX = event[3]
mouseStartY = event[4]
proc.windows[#proc.windows]:draw()
elseif mouseDragAction == "resize" then
proc.windows[#proc.windows]:resize(proc.windows[#proc.windows].w + (event[3] - mouseStartX), proc.windows[#proc.windows].h + (event[4] - mouseStartY))
mouseStartX = event[3]
mouseStartY = event[4]
proc.windows[#proc.windows].maximized = false
proc:queue("window_resize")
proc:queue("term_resize")
end
else
local pID, proc, winNum, win = findWindow(event[3], event[4])
if pID and proc and winNum and win then
event[3] = event[3] - win.x
event[4] = event[4] - win.y
if event[3] > 0 and event[3] < win.w - 1 and event[4] > 0 and event[4] < win.h - 1 then
proc:resume(event)
end
end
end
elseif event[1] == "mouse_scroll" then
local pID, proc, winNum, win = findWindow(event[3], event[4])
if pID and proc and winNum and win then
event[3] = event[3] - win.x
event[4] = event[4] - win.y
if event[3] > 0 and event[3] < win.w - 1 and event[4] > 0 and event[4] < win.h - 1 then
proc:resume(event)
end
end
elseif event[1] == "mouse_up" then
local pID, proc, winNum, win = findWindow(event[3], event[4])
if pID and proc and winNum and win then
event[3] = event[3] - win.x
event[4] = event[4] - win.y
if event[3] > 0 and event[3] < win.w - 1 and event[4] > 0 and event[4] < win.h - 1 then
proc:resume(event)
end
end
elseif event[1] == "window_action" then
process.new(function() windowAction(event[2], event[3]) end, "lwsa")
elseif event[1] == "select_window" then
process.new(windowSelect, "lwss")
elseif event[1] == "close_window" then
--index 2 is pID, index 3 is window index.
if process.list[event[2]] and process.list[event[2]].windows then
local proc = process.list[event[2]]
if event[3] then
if #proc.windows == 1 then
saveDimensions(proc)
proc:kill()
else
proc:removeWindow(event[3])
event[3]:destroy()
end
else
saveDimensions(proc)
proc:kill()
end
end
elseif event[1] == "service_stop" then
process.removeHook({"char"})
process.removeHook({"key"})
--process.removeHook({"key", 61})
--process.removeHook({"key", 62})
process.removeHook({"key_up"})
process.removeHook({"mouse_click"})
process.removeHook({"mouse_drag"})
process.removeHook({"mouse_scroll"})
process.removeHook({"mouse_up"})
--clean up injected methods:
procTable.addWindow = nil
procTable.removeWindow = nil
procTable.toFront = nil
procTable.toBack = nil
procTable.check = oldPcheck
procTable.kill = oldPkill
procTable.setup = oldPsetup
procTable.post = oldPpost
return
elseif event[1] == "start_program" then
--2, program path
--3, start in background
local focus
if event[3] then
focus = process.focus
end
local args = {}
for match in string.gmatch( event[2], "[^ \t]+" ) do
table.insert( args, match )
end
local startProcess = process.id()
shell.setDir("")
path = shell.resolveProgram(table.remove(args, 1))
if path then
local w, h, x, y, max = 19, 9, 1, 1, "false"
windowDimensions = {}
winDims = io.open(fs.combine(LyqydOS.folderPath, ".lyqwin"), "r")
if winDims then
for line in winDims:lines() do
if string.match(line, "%d+,%d+,%d+,%d+,%a+,(.*)") == path then
x, y, w, h, max = string.match(line, "(%d+),(%d+),(%d+),(%d+),(%a+),")
break
end
end
winDims:close()
end
local win = window.new(w, h, x, y, fs.getName(path), process.compositor:newBuffer(#process.list + 1))
if process.compositor.target.isColor() then
win:setType("window")
end
win:setTheme(LyqydOS.window.theme)
if max == "true" then win:maximize() end
process.new(function() shell.run("lsh", path, unpack(args)) end, shell.resolveProgram("lsh"), nil, win)
if coroutine.status(process.list[process.focus].thread) == "dead" then
lwsProc:queue("close_window", process.focus)
process.focus = startProcess
end
win.deferDraw = true
if focus then
process.focus = focus
end
end
end
end