From 9d688b6ffad14912bd1afe42555747cb3d213d95 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 9 Feb 2019 02:13:06 +0100 Subject: Main.app: copy capture to clipboard with xclip --- src/Flameshot/Internal.hs | 18 ++++-------------- src/Flameshot/Internal/Process.hs | 26 ++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 16 deletions(-) (limited to 'src/Flameshot') diff --git a/src/Flameshot/Internal.hs b/src/Flameshot/Internal.hs index 56a35b4..b01b145 100644 --- a/src/Flameshot/Internal.hs +++ b/src/Flameshot/Internal.hs @@ -10,19 +10,17 @@ import Control.Concurrent.Async (race) import Control.Concurrent.Extended import Control.Exception import Data.ByteString (ByteString) -import qualified Data.ByteString as BS import qualified Data.Char as C import Data.Function (on) import Data.Maybe (fromMaybe) -import Data.Time.Clock import Data.Time.Clock.System -import Data.Time.Format import Data.Time.ISO8601 import DBus import DBus.Internal.Message import DBus.Socket import Data.Text (Text) import qualified Data.Text.Extended as T +import qualified Flameshot.Internal.Process as P blessBusName :: BusName -> Blessings Text @@ -110,14 +108,6 @@ showUnprintable = . T.groupBy ((==) `on` p) -saveImage :: FilePath -> ByteString -> IO () -saveImage cGuiPath rawImage = do - t <- formatISO8601Seconds . systemToUTCTime <$> getSystemTime - let path = cGuiPath <> "/" <> baseName - baseName = t <> "_flameshot.png" - BS.writeFile path rawImage - - -formatISO8601Seconds :: UTCTime -> String -formatISO8601Seconds = - formatTime defaultTimeLocale (iso8601DateFormat $ Just "%H:%M:%SZ") +copyToClipboard :: String -> ByteString -> IO () +copyToClipboard mimetype = + P.writeDaemon "xclip" ["-selection", "clipboard", "-t", mimetype, "-i"] diff --git a/src/Flameshot/Internal/Process.hs b/src/Flameshot/Internal/Process.hs index c435d48..47220a3 100644 --- a/src/Flameshot/Internal/Process.hs +++ b/src/Flameshot/Internal/Process.hs @@ -3,12 +3,14 @@ {-# LANGUAGE MultiWayIf #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RecordWildCards #-} -module Flameshot.Internal.Process (run,Callbacks(..)) where +module Flameshot.Internal.Process (run,Callbacks(..),writeDaemon) where import Control.Concurrent (forkIO,threadDelay) import Control.Concurrent.Async (race) import Control.Exception -import Control.Monad.Extended (untilM_,unless) +import Control.Monad.Extended (untilM_,unless,void) +import Data.ByteString (ByteString) +import qualified Data.ByteString as BS import Data.Text (Text) import qualified Data.Text.IO as T import System.Exit @@ -16,10 +18,16 @@ import System.IO (BufferMode(LineBuffering),hSetBuffering) import System.IO (Handle,hClose,hPutStr,hIsEOF) import System.IO.Error (catchIOError,isDoesNotExistError) import System.Process +import System.Posix.Directory (changeWorkingDirectory) +import System.Posix.Files (setFileCreationMask) +import System.Posix.IO (closeFd,dupTo,openFd,stdError,stdInput,stdOutput) +import System.Posix.IO (defaultFileFlags,OpenMode(WriteOnly)) +import System.Posix.Process (createSession,forkProcess,getProcessStatus) import System.Posix.Process (getProcessGroupIDOf) import System.Posix.Signals (Signal,signalProcessGroup,killProcess) import System.Posix.Types (ProcessGroupID) + data Callbacks = Callbacks { onOutLine :: Pid -> Text -> IO () , onErrLine :: Pid -> Text -> IO () @@ -73,6 +81,20 @@ run path args cwd env input hTimeout Callbacks{..} = } +writeDaemon :: FilePath -> [String] -> ByteString -> IO () +writeDaemon path args input = do + pid <- forkProcess $ do + _ <- setFileCreationMask 0o0027 + _ <- createSession + changeWorkingDirectory "/" + closeFd stdInput + bracket (openFd "/dev/null" WriteOnly Nothing defaultFileFlags) closeFd + $ \hnull -> mapM_ (dupTo hnull) [stdOutput, stdError] + (Just hin, _, _, _) <- + createProcess (proc path args) { std_in = CreatePipe } + BS.hPut hin input + hClose hin + void (getProcessStatus True False pid) killProcessGroup :: ProcessGroupID -> IO () killProcessGroup = signalProcessGroup' killProcess -- cgit v1.2.3