antos-frontend/src/core/core.coffee

151 lines
5.2 KiB
CoffeeScript
Raw Normal View History

2018-03-15 11:00:24 +01:00
# Copyright 2017-2018 Xuan Sang LE <xsang.le AT gmail DOT com>
# AnTOS Web desktop is is licensed under the GNU General Public
# License v3.0, see the LICENCE file for more information
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
# You should have received a copy of the GNU General Public License
#along with this program. If not, see https://www.gnu.org/licenses/.
2018-03-09 10:11:15 +01:00
'use strict'
#define the OS object
self = this
self.OS or=
2017-08-27 23:40:02 +02:00
API: {}
GUI: {}
APP: {}
setting:
user: {}
applications: {}
2018-01-26 18:57:28 +01:00
desktop: {}
2017-08-27 23:40:02 +02:00
appearance: {}
2018-01-28 02:01:21 +01:00
VFS: {}
2018-01-29 19:16:29 +01:00
system: {}
courrier:
observable: riot.observable()
2017-08-26 16:50:13 +02:00
quota: 0
2018-03-24 00:21:53 +01:00
listeners: {}
on: (e, f, a) ->
_courrier.listeners[a.pid] = [] unless _courrier.listeners[a.pid]
_courrier.listeners[a.pid].push { e: e, f: f }
_courrier.observable.on e, f
trigger: (e, d) -> _courrier.observable.trigger e, d
2017-08-27 23:40:02 +02:00
osfail: (m, e, s) ->
_courrier.ostrigger "fail", { m: m, e: e, s: s }
oserror: (m, e, s) ->
_courrier.ostrigger "error", { m: m, e: e, s: s }
2018-01-26 18:57:28 +01:00
osinfo: (m) ->
_courrier.ostrigger "info", { m: m, e: null, s: null }
2017-08-27 23:40:02 +02:00
ostrigger: (e, d) ->
_courrier.trigger e, { id: 0, data: d, name: "OS" }
unregister: (app) ->
return unless _courrier.listeners[app.pid] and _courrier.listeners[app.pid].length > 0
_courrier.observable.off i.e, i.f for i in _courrier.listeners[app.pid]
delete _courrier.listeners[app.pid]
_courrier.listeners[app.pid] = []
2018-01-29 19:16:29 +01:00
getMID: () ->
2017-08-26 16:50:13 +02:00
_courrier.quota += 1
_courrier.quota
2017-08-24 01:53:13 +02:00
register: (name, x) ->
2018-02-01 19:36:09 +01:00
if x.type is 3 then self.OS.GUI.subwindows[name] = x else _OS.APP[name] = x
PM:
pidalloc: 0
2017-08-27 23:40:02 +02:00
processes: {}
createProcess: (app, cls, args) ->
2018-02-16 18:38:14 +01:00
f = () ->
#if it is single ton
# and a process is existing
# just return it
if cls.singleton and _PM.processes[app] and _PM.processes[app].length == 1
_PM.processes[app][0].show()
else
_PM.processes[app] = [] if not _PM.processes[app]
obj = new cls(args)
obj.birth = (new Date).getTime()
_PM.pidalloc++
obj.pid = _PM.pidalloc
_PM.processes[app].push obj
if cls.type is 1 then _GUI.dock obj, cls.meta else _GUI.attachservice obj
if cls.type is 2
_courrier.trigger "srvroutineready", app
if cls.dependencies
libs = (v for v in cls.dependencies)
_API.requires libs, f
else
2018-02-16 18:38:14 +01:00
f()
appByPid: (pid) ->
app = undefined
find = (l) ->
return a for a in l when a.pid is pid
for k, v of _PM.processes
app = find v
break if app
app
kill: (app) ->
2017-08-27 23:40:02 +02:00
return if not app.name or not _PM.processes[app.name]
i = _PM.processes[app.name].indexOf app
if i >= 0
2017-08-27 23:40:02 +02:00
if _OS.APP[app.name].type == 1 then _GUI.undock app else _GUI.detachservice app
_courrier.unregister app
delete _PM.processes[app.name][i]
_PM.processes[app.name].splice i, 1
2017-08-27 23:40:02 +02:00
killAll: (app, force) ->
2017-08-27 23:40:02 +02:00
return unless _PM.processes[app]
2018-03-23 23:18:32 +01:00
a.quit(force) for a in _PM.processes[app]
2018-01-04 11:51:12 +01:00
cleanup: ->
2018-02-28 17:18:00 +01:00
console.log "Clean up system"
2018-03-23 23:18:32 +01:00
_PM.killAll a, true for a, v of _PM.processes
_courrier.observable.off("*") if _courrier.observable
2018-03-24 00:21:53 +01:00
$(window).off('keydown')
delete _courrier.observable
2018-01-04 11:51:12 +01:00
($ "#wrapper").empty()
_GUI.clearTheme()
_courrier.observable = riot.observable()
2018-01-30 14:47:27 +01:00
_courrier.quota = 0
2018-01-04 11:51:12 +01:00
_OS.APP = {}
2018-01-30 14:47:27 +01:00
_OS.setting =
user: {}
applications: {}
desktop: {}
appearance: {}
VFS: {}
system: {}
2018-01-04 11:51:12 +01:00
_PM.processes = {}
_PM.pidalloc = 0
boot: ->
2017-08-27 23:40:02 +02:00
#first login
2018-02-28 17:18:00 +01:00
console.log "Booting sytem"
2017-08-27 23:40:02 +02:00
_API.handler.auth (d) ->
# in case someone call it more than once :)
if d.error
# show login screen
_GUI.login()
else
# startX :)
_GUI.startAntOS d.result
cleanupHandlers: {}
exit: ->
#do clean up first
f() for n, f of _OS.cleanupHandlers
2018-03-24 00:21:53 +01:00
_OS.cleanup()
_API.handler.setting (r) ->
_API.handler.logout()
onexit: (n, f) ->
self.OS.cleanupHandlers[n] = f unless self.OS.cleanupHandlers[n]