xmonad / X11

A Haskell binding to the X11 graphics library.

Home Page:http://hackage.haskell.org/package/X11

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use properly the XRandR library functions?

jokogr opened this issue · comments

Hello,

Could anyone make an example of using the XRandR bindings?

I have started making a small clone of the "xrandr -q" command:

import Graphics.X11.Xlib
import Graphics.X11.Xrandr

main = do
  dpy <- openDisplay ""
  let dflt = defaultScreen dpy
  root <- rootWindow dpy dflt
  sc <- xrrGetScreenInfo dpy root
  sizes <- xrrConfigSizes sc

Is the code moving to the right direction?

Thanks in advance

I think the code should be similar to this one, the original in C: http://cgit.freedesktop.org/xorg/app/xrandr/tree/xrandr.c

ok, I have written the following:

import Graphics.X11.Xlib
import Graphics.X11.Xrandr

maybeXrrConfigSizes :: Maybe XRRScreenConfiguration -> IO (Maybe [XRRScreenSize])
maybeXrrConfigSizes mc = case mc of
  Nothing -> do
    return Nothing
  Just c -> xrrConfigSizes c

maybeXrrScreenSize :: Maybe [XRRScreenSize] -> [XRRScreenSize]
maybeXrrScreenSize ms = case ms of
  Nothing -> []
  Just s -> s

main = do
  dpy <- openDisplay ""
  let dflt = defaultScreen dpy
  root <- rootWindow dpy dflt
  sc <- xrrGetScreenInfo dpy root
  sizes <- maybeXrrConfigSizes sc
  putStrLn $ show $ maybeXrrScreenSize sizes

Although the code compiles, I am getting a segmentation fault.

Sounds like xrrGetScreenInfo is not checking for NULL on return (there is a lot of this in the X11 bindings, sadly) so it cores when you try to access the information returned from C.

It appears I was not using properly the functions. After reading more carefully xrandr.c I have managed to write proper code which does not segfault.

@jokogr would you be willing you share your working code? I'm currently struggling with the same thing.

@thedward sure, have a look here.

Bear in mind that I have replaced this code with the XRRGetMonitors bindings that I have proposed with a pull request in this repository.