diff --git a/app/WM/Core.hs b/app/WM/Core.hs index be3e72b..dce4b5b 100644 --- a/app/WM/Core.hs +++ b/app/WM/Core.hs @@ -1,17 +1,25 @@ module WM.Core where import Control.Concurrent (threadDelay) +import Control.Exception (bracket) import Foreign (Ptr) import System.Environment (getArgs) import WM.Mruby import WM.XKutu data State = State - { mruby_pointer :: Ptr MrbState, - running :: Bool + { mruby_pointer :: !(Ptr MrbState), + running :: !Bool } deriving (Show) +newState :: Ptr MrbState -> State +newState ptr = + State + { mruby_pointer = ptr, + running = True + } + run :: IO () run = do is_started <- deploy @@ -24,11 +32,10 @@ run = do print screen_info pointer_info <- get_pointer print pointer_info - mrb <- mrb_open - runRuby mrb runtimeRb - handle_args mrb - loop $ State mrb True - mrb_close mrb + bracket mrb_open mrb_close $ \mrb -> do + loadRubyLibs mrb + handle_args mrb + loop (newState mrb) handle_args :: Ptr MrbState -> IO () handle_args mrb = do @@ -41,12 +48,10 @@ handle_args mrb = do _ -> putStrLn "unknown arguments" handle_event :: Maybe Event -> State -> IO State -handle_event Nothing state = pure state -handle_event (Just ev) state - | event_btn ev == 24 = pure state {running = False} - | otherwise = do - print ev - pure state +handle_event Nothing s = pure s +handle_event (Just ev) s + | event_btn ev == 24 = pure s {running = False} + | otherwise = print ev >> pure s loop :: State -> IO () loop state diff --git a/app/WM/Mruby.hs b/app/WM/Mruby.hs index 4a5f7e0..47cee43 100644 --- a/app/WM/Mruby.hs +++ b/app/WM/Mruby.hs @@ -1,20 +1,11 @@ {-# LANGUAGE CApiFFI #-} {-# LANGUAGE ForeignFunctionInterface #-} -{-# LANGUAGE TemplateHaskell #-} module WM.Mruby where -import Data.FileEmbed (embedFile) -import Data.Text qualified as T -import Data.Text.Encoding qualified as TE -import Data.Text.Encoding.Error qualified as TEE -import Foreign.C (CString, withCString) +import Foreign.C (CChar, CInt (CInt), CString, withCString) import Foreign.Ptr -runtimeRb :: String -runtimeRb = - T.unpack (TE.decodeUtf8With TEE.lenientDecode $(embedFile "./app/ruby/lib.rb")) - data MrbState foreign import capi "mruby.h mrb_open" @@ -30,3 +21,15 @@ runRuby :: Ptr MrbState -> String -> IO () runRuby mrb code = withCString code $ \cstr -> c_mrb_load_string mrb cstr + +foreign import capi "ruby_compiled.h value mgems" + c_mgems :: Ptr CChar + +foreign import capi "ruby_compiled.h value mgems_len" + c_mgems_len :: CInt + +foreign import capi "mruby/irep.h mrb_load_irep_buf" + c_mrb_load_irep_buf :: Ptr MrbState -> Ptr CChar -> CInt -> IO () + +loadRubyLibs :: Ptr MrbState -> IO () +loadRubyLibs mrb = c_mrb_load_irep_buf mrb c_mgems c_mgems_len diff --git a/app/ruby/lib.rb b/app/ruby/lib.rb deleted file mode 100644 index 1753755..0000000 --- a/app/ruby/lib.rb +++ /dev/null @@ -1 +0,0 @@ -puts "hello from library" diff --git a/app/ruby/main.rb b/app/ruby/main.rb deleted file mode 100644 index 30a0d6e..0000000 --- a/app/ruby/main.rb +++ /dev/null @@ -1 +0,0 @@ -puts "hello from ruby" diff --git a/config/config.rb b/config/config.rb new file mode 100644 index 0000000..b209fbd --- /dev/null +++ b/config/config.rb @@ -0,0 +1 @@ +puts "hello from config" diff --git a/flake.nix b/flake.nix index 1b824c1..2076392 100644 --- a/flake.nix +++ b/flake.nix @@ -35,6 +35,7 @@ gnumake ruby + xxd haskellPackages.ghc haskellPackages.cabal-install diff --git a/kutu.cabal b/kutu.cabal index a4c1492..33e791f 100644 --- a/kutu.cabal +++ b/kutu.cabal @@ -24,7 +24,7 @@ executable kutu WM.XKutu WM.Mruby - build-depends: base, extra, bytestring, file-embed, text + build-depends: base, bytestring hs-source-dirs: app diff --git a/libs/l_mgems/compile.sh b/libs/l_mgems/compile.sh index 0d62ef4..78e1062 100644 --- a/libs/l_mgems/compile.sh +++ b/libs/l_mgems/compile.sh @@ -4,7 +4,7 @@ set -e SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" OUTPUT_TMP=$(mktemp) -OUTPUT="$SCRIPT_DIR/../../include/binding/ruby_compiled.h" +OUTPUT="$SCRIPT_DIR/../../app/c/ruby_compiled.h" if test "$1" = "clean"; then rm -f "$OUTPUT" @@ -32,9 +32,9 @@ fi echo "#ifndef MGEMS_H" echo "#define MGEMS_H" xxd -i -n mgems $OUTPUT_TMP \ - | sed -e 's/^unsigned char /constexpr unsigned char /' \ - -e 's/^unsigned int /constexpr unsigned int /' + | sed -e 's/^unsigned char /unsigned char /' \ + -e 's/^unsigned int /unsigned int /' echo "#endif" } > "$OUTPUT" -echo "Compiled mgems!" \ No newline at end of file +echo "Compiled mgems!" diff --git a/libs/l_mgems/lib.rb b/libs/l_mgems/lib.rb new file mode 100644 index 0000000..2af6890 --- /dev/null +++ b/libs/l_mgems/lib.rb @@ -0,0 +1 @@ +puts "hello from lib" diff --git a/libs/l_mgems/pp.rb b/libs/l_mgems/pp.rb index a931f61..3cdc021 100644 --- a/libs/l_mgems/pp.rb +++ b/libs/l_mgems/pp.rb @@ -1007,4 +1007,4 @@ class Object < BasicObject # :nodoc: end } } -end \ No newline at end of file +end