Repository navigation
Expand file tree
/
Copy pathCore.js
More file actions
169 lines (148 loc) · 6.65 KB
/
Copy pathCore.js
File metadata and controls
169 lines (148 loc) · 6.65 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
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
.pragma library
// Máquina de estado del bloqueo: qué workspaces son privados y cuáles están
// bloqueados. Pura (ES5, sin QML): corre en QML y en node (tests/test-core.mjs).
// Cada transición devuelve un estado NUEVO; nunca muta el que recibe.
function uniq(ids) {
var out = []
for (var i = 0; i < ids.length; i++) {
var id = Number(ids[i])
if (isFinite(id) && id > 0 && out.indexOf(id) === -1) out.push(id)
}
return out.sort(function(a, b) { return a - b })
}
function without(ids, id) {
return ids.filter(function(x) { return x !== id })
}
// Copia del estado con algunos campos cambiados. `pending` = { id: msLímite } de
// los privados en modo retardo que se dejaron y aún no se bloquean.
function make(state, changes) {
var out = { privateIds: state.privateIds, lockedIds: state.lockedIds,
previousWorkspaceId: state.previousWorkspaceId, pending: state.pending || {} }
for (var k in changes) out[k] = changes[k]
return out
}
function pendingWithout(pending, ids) {
var out = {}
for (var k in (pending || {})) if (ids.indexOf(Number(k)) === -1) out[k] = pending[k]
return out
}
// Al arrancar, todo privado empieza bloqueado: un reinicio no deja nada abierto.
function start(privateIds) {
var p = uniq(privateIds || [])
return { privateIds: p, lockedIds: p.slice(), previousWorkspaceId: -1, pending: {} }
}
function isPrivate(state, id) { return state.privateIds.indexOf(Number(id)) !== -1 }
function isLocked(state, id) { return state.lockedIds.indexOf(Number(id)) !== -1 }
// El workspace activo pasó de `fromId` a `toId`: el que se deja, si es privado, se
// bloquea; si tiene retardo (`delays[id]` en segundos), queda pendiente hasta
// nowMs + retardo. Volver a uno pendiente lo deja abierto.
function focusChanged(state, fromId, toId, nowMs, delays) {
var from = Number(fromId), to = Number(toId)
var locked = state.lockedIds
var pending = pendingWithout(state.pending, [to])
if (from > 0 && from !== to && isPrivate(state, from) && !isLocked(state, from)) {
var d = delays ? Number(delays[from]) || 0 : 0
if (d > 0) pending[String(from)] = (Number(nowMs) || 0) + d * 1000
else locked = uniq(locked.concat([from]))
}
return make(state, { lockedIds: locked, previousWorkspaceId: to, pending: pending })
}
// Pasa el tiempo: los pendientes vencidos se bloquean.
function tick(state, nowMs) {
var due = [], pending = {}
for (var k in (state.pending || {})) {
if (state.pending[k] <= nowMs) due.push(Number(k))
else pending[k] = state.pending[k]
}
if (due.length === 0) return state
return make(state, { lockedIds: uniq(state.lockedIds.concat(due)), pending: pending })
}
function hasPending(state) {
for (var k in (state.pending || {})) return true
return false
}
// Marcar o desmarcar privado. No bloquea al marcar: el bloqueo llega al salir.
function setPrivate(state, id, value) {
var n = Number(id)
var p = value ? uniq(state.privateIds.concat([n])) : without(state.privateIds, n)
return make(state, { privateIds: p, pending: value ? state.pending : pendingWithout(state.pending, [n]) })
}
// La clave correcta quita el bloqueo de ese workspace (sigue siendo privado).
function unlockOk(state, id) {
return make(state, { lockedIds: without(state.lockedIds, Number(id)), pending: pendingWithout(state.pending, [Number(id)]) })
}
// Los privados cambiaron fuera (settings.json recargado). Lo ya bloqueado sigue
// bloqueado aunque deje de ser privado: solo la clave lo abre.
function syncPrivate(state, ids) {
var p = uniq(ids || [])
var gone = []
for (var k in (state.pending || {})) if (p.indexOf(Number(k)) === -1) gone.push(Number(k))
return make(state, { privateIds: p, pending: pendingWithout(state.pending, gone) })
}
// Quitar `private` a un workspace bloqueado se rechaza: sería abrirlo sin clave.
function canSetPrivate(state, id, value) {
return !!value || !isLocked(state, id)
}
// Workspaces que lista el panel: 1..base siempre, más los que existen por encima
// y los privados (aunque hoy no existan). Ordenados, sin repetir.
function panelIds(existingIds, privateIds, base) {
var n = base > 0 ? base : 5
var ids = []
for (var i = 1; i <= n; i++) ids.push(i)
return uniq(ids.concat(existingIds || [], privateIds || []))
}
// Segundos que faltan para que faillock deje volver a intentar, a partir de la
// salida de `faillock --user <u>` (líneas "AAAA-MM-DD HH:MM:SS TIPO ORIGEN V|I").
// 0 si no hay bloqueo. deny/unlockSec son los de /etc/pam.d/omarchy-lock-password.
function faillockWait(text, nowMs, deny, unlockSec) {
var times = []
var lines = String(text || "").split("\n")
for (var i = 0; i < lines.length; i++) {
var m = lines[i].match(/^(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})\s+\S+\s+.*\s(V)\s*$/)
if (!m) continue
times.push(new Date(+m[1], +m[2] - 1, +m[3], +m[4], +m[5], +m[6]).getTime())
}
if (times.length < deny) return 0
times.sort(function(a, b) { return a - b })
var last = times[times.length - 1]
var left = Math.ceil((last + unlockSec * 1000 - nowMs) / 1000)
return left > 0 ? left : 0
}
// Bloquear a mano el workspace actual (atajo o IPC). Si es público es un bloqueo
// puntual: no se toca privateIds, así que al desbloquearlo sigue público.
function lockNow(state, id) {
var n = Number(id)
if (!(n > 0)) return state
return make(state, { lockedIds: uniq(state.lockedIds.concat([n])), pending: pendingWithout(state.pending, [n]) })
}
// Se bloqueó la sesión entera: todo privado vuelve a quedar bloqueado.
function sessionLocked(state) {
return make(state, { lockedIds: uniq(state.lockedIds.concat(state.privateIds)), pending: {} })
}
// ¿Hay algún privado abierto? Solo entonces vale la pena vigilar el lock de sesión.
function hasUnlockedPrivate(state) {
for (var i = 0; i < state.privateIds.length; i++)
if (state.lockedIds.indexOf(state.privateIds[i]) === -1) return true
return false
}
// Inactividad: qué privados ABIERTOS ya cumplieron su plazo. `targets` = Settings.idleTargets,
// `idleMs` = cuánto lleva el equipo sin teclado ni ratón. Devuelve { lock, wait }: los de
// `wait` solo se bloquean si no suena audio (lo comprueba el servicio).
function idleDue(state, targets, idleMs) {
var out = { lock: [], wait: [] }
if (!(idleMs > 0)) return out
for (var i = 0; i < state.privateIds.length; i++) {
var id = state.privateIds[i]
var t = targets[String(id)]
if (!t || state.lockedIds.indexOf(id) !== -1 || idleMs < t.seconds * 1000) continue
if (t.audio === "wait") out.wait.push(id)
else out.lock.push(id)
}
return out
}
// Varios bloqueos a la vez (lockNow de cada uno).
function lockMany(state, ids) {
var next = state
for (var i = 0; i < ids.length; i++) next = lockNow(next, ids[i])
return next
}