add initial cli

This commit is contained in:
Jussi Kuokkanen 2023-12-22 14:24:40 +02:00
parent 38e178bf0f
commit 44fbff02f5
7 changed files with 82 additions and 1 deletions

View File

@ -3,8 +3,9 @@
, boost , boost
, cmake , cmake
, cudatoolkit , cudatoolkit
, git
, fetchFromGitHub , fetchFromGitHub
, git
, haskellPackages
, libdrm , libdrm
, libX11 , libX11
, libXext , libXext
@ -40,6 +41,7 @@ mkDerivation rec {
''; '';
nativeBuildInputs = [ nativeBuildInputs = [
(haskellPackages.ghcWithPackages (p: with p; [ dbus ]))
git git
pkg-config pkg-config
]; ];

3
dev/cli-run.sh Executable file
View File

@ -0,0 +1,3 @@
cd "$(dirname "$0")"
DBUS_SYSTEM_BUS_ADDRESS=unix:path=/tmp/tuxclocker-dbus-socket ../inst/bin/tuxclocker

View File

@ -4,6 +4,8 @@ option('daemon', type: 'boolean', value: 'true', description: 'Build daemon')
option('plugins', type: 'boolean', value: 'true', description: 'Build plugins') option('plugins', type: 'boolean', value: 'true', description: 'Build plugins')
option('library', type: 'boolean', value: 'true', description: 'Build library') option('library', type: 'boolean', value: 'true', description: 'Build library')
option('gui', type: 'boolean', value: 'true', description: 'Build Qt GUI') option('gui', type: 'boolean', value: 'true', description: 'Build Qt GUI')
# Disabled by default to not break builds
option('cli', type: 'boolean', value: 'false', description: 'Build CLI')
option('require-python-hwdata', type: 'boolean', value: 'false', option('require-python-hwdata', type: 'boolean', value: 'false',
description: 'Require python-hwdata for prettier AMD GPU names') description: 'Require python-hwdata for prettier AMD GPU names')
option('require-amd', type: 'boolean', value: 'false', option('require-amd', type: 'boolean', value: 'false',

View File

@ -50,3 +50,7 @@ endif
if get_option('gui') if get_option('gui')
subdir('tuxclocker-qt') subdir('tuxclocker-qt')
endif endif
if get_option('cli')
subdir('tuxclocker-cli')
endif

View File

@ -0,0 +1,57 @@
{-# LANGUAGE OverloadedStrings #-}
import Data.Either
import Data.Functor
import Data.Maybe
import Data.Text (Text)
import Data.Tree
import DBus
import DBus.Client
import qualified DBus.Introspection as I
tuxClockerCall :: MethodCall -> MethodCall
tuxClockerCall call = call { methodCallDestination = Just "org.tuxclocker" }
printTree :: Show a => Tree a -> IO ()
printTree tree = printIndent tree 0 where
printIndent :: Show a => Tree a -> Int -> IO ()
printIndent (Node rootLabel subForest) level = do
print $ (replicate level ' ') <> show rootLabel
mapM_ (\tree -> printIndent tree (level + 1)) subForest
getObject :: Client -> ObjectPath -> IO I.Object
getObject client path = do
reply <- call_ client $
tuxClockerCall $ methodCall path "org.freedesktop.DBus.Introspectable" "Introspect"
let xml = fromVariant (head $ methodReturnBody reply)
pure $ fromMaybe
(error ("Invalid introspection XML: " ++ show xml))
(xml >>= I.parseXML path)
getName :: Client -> ObjectPath -> IO String
getName client path =
let
call = tuxClockerCall $ methodCall path "org.tuxclocker.Node" "name"
in
getProperty client call <&> fromRight (toVariant ("Unnamed" :: String)) <&> variantToString
getDBusTree :: Client -> IO (Tree Variant)
getDBusTree client = unfoldTreeM (buildNode client) "/" where
buildNode :: Client -> ObjectPath -> IO (ObjectPath, [ObjectPath])
buildNode client path = do
object <- getObject client path
let childPaths = I.objectPath <$> I.objectChildren object
pure (I.objectPath object, childPaths)
variantToString :: Variant -> String
variantToString x = fromMaybe "Invalid" $ fromVariant x
getNameTree :: Client -> Tree ObjectPath -> IO (Tree String)
getNameTree client = mapM (getName client)
main = do
client <- connectSystem
tree <- getDBusTree client
nameTree <- getNameTree client tree
putStr $ drawTree nameTree

View File

@ -0,0 +1,3 @@
install_dir = join_paths(get_option('prefix'), get_option('bindir'))
run_command('cabal', 'install', '--installdir=@0@'.format(install_dir))

View File

@ -0,0 +1,10 @@
name: tuxclocker
version: 0.0.1
cabal-version: 2.0
build-type: Simple
executable tuxclocker
build-depends: base >= 4.16,
dbus >= 1.2,
text >= 2.0
main-is: Main.hs