Skip to content

Commit 827e23e

Browse files
committed
restructure core for probably the last time before release
1 parent 6c85404 commit 827e23e

13 files changed

Lines changed: 149 additions & 83 deletions

File tree

quark.nimble

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,20 @@ author = "cobaltgit"
55
description = "Quark stock mod for TrimUI Smart"
66
license = "GPL-3.0"
77
srcDir = "src"
8-
bin = @["fbscreenshot/fbscreenshot", "quark_hotkeyd/quark_hotkeyd", "sysjson_monitor/sysjson_monitor", "mainui_game_picker/mainui_game_picker", "bootlogo/bootlogo", "display/display"]
98

109
requires "nim >= 2.0.0"
1110
requires "nimPNG >= 0.3.1"
1211

1312
const Root = getCurrentDir()
1413
const BinDir = Root / "dist" / "System" / "bin"
14+
const Bins = {
15+
"fbscreenshot.nim": "fbscreenshot",
16+
"quark_hotkeyd/main.nim": "quark_hotkeyd",
17+
"sysjson_monitor/main.nim": "sysjson_monitor",
18+
"mainui_game_picker.nim": "mainui_game_picker",
19+
"bootlogo.nim": "bootlogo",
20+
"display.nim": "display",
21+
}.toTable()
1522
const Threads = gorge("nproc")
1623

1724
# Import task files
@@ -20,9 +27,8 @@ include "tasks/dist.nims"
2027
include "tasks/locale.nims"
2128

2229
task buildBins, "Build binaries":
23-
for exe in bin:
24-
let binName = exe.split("/")[^1]
25-
selfExec &"c -o:{BinDir}/{binName} {srcDir}/{exe}.nim"
30+
for src, output in Bins:
31+
selfExec &"c -o:{BinDir}/{output} {srcDir}/{src}"
2632

2733
task cleanup, "Cleanup all":
2834
exec "nimble clean"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import std/[cmdline, os, posix]
2-
import ../common/[bootlogo, reboot]
3-
import ../display/display
2+
import common/[bootlogo, reboot]
3+
import display
44

55
when isMainModule:
66
var shouldReboot = false

src/common/ffi/amixer.nim

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const libasound = "libasound.so.2"
2+
3+
type
4+
SndMixerT* = pointer
5+
SndMixerElemT* = pointer
6+
SndMixerSelemIdT* = pointer
7+
8+
proc snd_mixer_open*(mixer: ptr SndMixerT, mode: cint): cint
9+
{.importc, dynlib: libasound.}
10+
11+
proc snd_mixer_close*(mixer: SndMixerT): cint
12+
{.importc, dynlib: libasound.}
13+
14+
proc snd_mixer_attach*(mixer: SndMixerT, name: cstring): cint
15+
{.importc, dynlib: libasound.}
16+
17+
proc snd_mixer_load*(mixer: SndMixerT): cint
18+
{.importc, dynlib: libasound.}
19+
20+
proc snd_mixer_selem_register*(mixer: SndMixerT,
21+
options: pointer,
22+
classp: pointer): cint
23+
{.importc, dynlib: libasound.}
24+
25+
proc snd_mixer_selem_id_sizeof*(): csize_t
26+
{.importc, dynlib: libasound.}
27+
28+
proc snd_mixer_selem_id_set_name*(obj: SndMixerSelemIdT, val: cstring)
29+
{.importc, dynlib: libasound.}
30+
31+
proc snd_mixer_selem_id_set_index*(obj: SndMixerSelemIdT, val: cuint)
32+
{.importc, dynlib: libasound.}
33+
34+
proc snd_mixer_find_selem*(mixer: SndMixerT,
35+
id: SndMixerSelemIdT): SndMixerElemT
36+
{.importc, dynlib: libasound.}
37+
38+
proc snd_mixer_selem_get_playback_volume_range*(elem: SndMixerElemT,
39+
minVal: ptr clong,
40+
maxVal: ptr clong): cint
41+
{.importc, dynlib: libasound.}
42+
43+
proc snd_mixer_selem_set_playback_volume_all*(elem: SndMixerElemT,
44+
value: clong): cint
45+
{.importc, dynlib: libasound.}

src/common/process.nim

Lines changed: 53 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,58 @@
1-
import std/[os, posix, strformat, strutils, sets]
1+
import std/[os, posix, strformat, strutils, sets, tables]
22

3-
proc killall*(processName: string, signal: cint, excludePid: int = -1): bool =
4-
var killed = false
5-
3+
proc getParentPid(pid: int): int =
4+
try:
5+
let stat = readFile(&"/proc/{pid}/stat")
6+
let afterComm = stat.find(')')
7+
if afterComm < 0:
8+
return -1
9+
let fields = stat[afterComm + 2 .. ^1].splitWhitespace()
10+
if fields.len >= 2:
11+
result = parseInt(fields[1])
12+
else:
13+
result = -1
14+
except CatchableError:
15+
result = -1
16+
17+
proc getProcessMap(): Table[string, seq[int]] =
18+
result = initTable[string, seq[int]]()
619
for kind, path in walkDir("/proc"):
7-
if kind == pcDir:
8-
let name = path.extractFilename()
9-
try:
10-
let pid = parseInt(name)
11-
if pid == excludePid:
12-
continue
13-
14-
let commPath = path / "comm"
15-
if fileExists(commPath):
16-
let comm = readFile(commPath).strip()
17-
18-
if comm == processName:
19-
if kill(cint(pid), signal) == 0:
20-
killed = true
21-
except ValueError:
22-
continue
23-
24-
result = killed
20+
if kind != pcDir:
21+
continue
22+
let name = path.extractFilename()
23+
try:
24+
let pid = parseInt(name)
25+
let commPath = path / "comm"
26+
if fileExists(commPath):
27+
let comm = readFile(commPath).strip()
28+
if comm notin result:
29+
result[comm] = @[]
30+
result[comm].add(pid)
31+
except CatchableError:
32+
continue
33+
34+
proc killall*(processName: string, signal: cint, excludePid: int = -1): bool =
35+
let processMap = getProcessMap()
36+
if processName notin processMap:
37+
return false
38+
39+
for pid in processMap[processName]:
40+
if pid == excludePid:
41+
continue
42+
if kill(cint(pid), signal) == 0:
43+
result = true
2544

2645
proc getProcessChildren*(ppid: int, pids: var HashSet[int]) =
2746
for kind, path in walkDir("/proc"):
28-
if kind == pcDir:
29-
let name = path.extractFilename()
30-
if name.len > 0 and name[0].isDigit:
31-
try:
32-
let pid = parseInt(name)
33-
let statPath = &"/proc/{pid}/stat"
34-
if fileExists(statPath):
35-
let stat = readFile(statPath)
36-
var fields = 0
37-
var parentPid = 0
38-
var inParen = false
39-
var field = ""
40-
41-
for c in stat:
42-
if c == '(':
43-
inParen = true
44-
elif c == ')':
45-
inParen = false
46-
elif c == ' ' and not inParen:
47-
inc fields
48-
if fields == 4:
49-
try:
50-
parentPid = parseInt(field)
51-
except ValueError:
52-
discard
53-
break
54-
field = ""
55-
else:
56-
if not inParen or fields > 0:
57-
field.add(c)
58-
59-
if parentPid == ppid:
60-
pids.incl(pid)
61-
getProcessChildren(pid, pids)
62-
except:
63-
discard
47+
if kind != pcDir:
48+
continue
49+
let name = path.extractFilename()
50+
if name.len == 0 or not name[0].isDigit:
51+
continue
52+
try:
53+
let pid = parseInt(name)
54+
if getParentPid(pid) == ppid:
55+
pids.incl(pid)
56+
getProcessChildren(pid, pids)
57+
except CatchableError:
58+
continue

src/common/reboot.nim

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import std/posix
22

33
const
44
SYS_reboot = cast[clong](88)
5-
5+
66
LINUX_REBOOT_MAGIC1 = cast[clong](0xfee1dead'u32)
77
LINUX_REBOOT_MAGIC2 = cast[clong](0x28121969'u32)
88

@@ -12,7 +12,7 @@ const
1212
RB_ENABLE_CAD* = cast[clong](0x89ABCDEF'u32)
1313
RB_DISABLE_CAD* = cast[clong](0x00000000'u32)
1414

15-
proc syscall(number: clong, arg1: clong, arg2: clong, arg3: clong, arg4: clong): clong
15+
proc syscall(number: clong, arg1: clong, arg2: clong, arg3: clong, arg4: clong): clong
1616
{.importc, header: "<unistd.h>", varargs.}
1717

1818
proc reboot*(cmd: clong): clong =
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import std/[os, posix, strutils]
22
import nimPNG
33

4-
import ../common/[fb, process]
5-
import stb_truetype
4+
import common/[fb, process]
5+
import common/ffi/stb_truetype
66

77
const
88
ScreenWidth = 320
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import std/[cmdline, strformat]
2-
from ../common/fb import fbscreenshot
2+
from common/fb import fbscreenshot
33

44
when isMainModule:
55
if paramCount() < 1:
66
stderr.writeLine "usage: fbscreenshot <output>"
77
quit(1)
8-
8+
99
let output = paramStr(1)
10-
10+
1111
try:
1212
fbscreenshot(output)
1313
echo &"fbscreenshot: Saved screenshot to '{output}'"

0 commit comments

Comments
 (0)