Skip to content

Commit 656104c

Browse files
author
Dan Rosén
committedNov 4, 2014
Add screen capture example
1 parent f9054bd commit 656104c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
 

‎examples/ScreenCapture.hs

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
-- Screen capture (screenshot)
2+
-- Captures 1600x900 and writes a 24-bit ascii PPM image on stdout.
3+
module Main where
4+
5+
import qualified Graphics.X11.Types as X
6+
import qualified Graphics.X11.Xlib.Image as X
7+
import qualified Graphics.X11.Xlib.Display as X
8+
import qualified Graphics.X11.Xlib.Screen as X
9+
10+
import Foreign.C.Types
11+
import Data.Bits
12+
13+
main :: IO ()
14+
main = do
15+
let (w,h) = (1600,900)
16+
disp <- X.openDisplay ":0"
17+
let scr = X.defaultScreenOfDisplay disp
18+
root <- X.rootWindow disp (X.screenNumberOfScreen scr)
19+
img <- X.getImage disp root 0 0 w h (-1) X.xyPixmap
20+
21+
let int :: CUInt -> CInt; int = fromIntegral . toInteger
22+
23+
let rgb :: CULong -> String
24+
rgb v = unwords [ show (0xff .&. (v `shiftR` s)) | s <- [16,8,0] ]
25+
26+
let ppm = unlines [ unwords [ rgb (X.getPixel img x y)
27+
| x <- [0..int (w-1)] ]
28+
| y <- [0..int (h-1)] ]
29+
30+
putStrLn (unlines ["P3",show w,show h,"255",ppm])
31+
X.destroyImage img
32+

0 commit comments

Comments
 (0)
Please sign in to comment.