import x
, xlib
 
const
  libXrandr* = "libXrandr.so(.2|)"
  RR_HSyncPositive* = 0x00000001
  RR_HSyncNegative* = 0x00000002
  RR_VSyncPositive* = 0x00000004
  RR_VSyncNegative* = 0x00000008
  RR_Interlace* = 0x00000010
  RR_DoubleScan* = 0x00000020
  RR_CSync* = 0x00000040
  RR_CSyncPositive* = 0x00000080
  RR_CSyncNegative* = 0x00000100
  RR_HSkewPresent* = 0x00000200
  RR_BCast* = 0x00000400
  RR_PixelMultiplex* = 0x00000800
  RR_DoubleClock* = 0x00001000
  RR_ClockDivideBy2* = 0x00002000
  RR_Connected* = 0
  RR_Disconnected* = 1
  RR_UnknownConnection* = 2

type
  PRotation* = ptr Rotation
  Rotation* = cushort

  PSizeID* = ptr SizeID
  SizeID* = cushort

  PSubpixelOrder* = ptr SubpixelOrder
  SubpixelOrder* = cushort

  PRRCrtc* = ptr RRCrtc
  RRCrtc* = XID

  PRROutput* = ptr RROutput
  RROutput* = XID

  PRRMode* = ptr RRMode
  RRMode* = XID

  PXRRModeFlags* = ptr XRRModeFlags
  XRRModeFlags* = culong

  PConnection* = ptr Connection
  Connection* = cushort

{.deprecated: [TRotation: Rotation].}
{.deprecated: [TSizeID: SizeID].}
{.deprecated: [TSubpixelOrder: SubpixelOrder].}
{.deprecated: [TRRCrtc: RRCrtc].}
{.deprecated: [TRROutput: RROutput].}
{.deprecated: [TRRMode: RRMode].}
{.deprecated: [TXRRModeFlags: XRRModeFlags].}
{.deprecated: [TConnection: Connection].}

const
  RANDR_NAME* = "RANDR"
  RANDR_MAJOR* = 1
  RANDR_MINOR* = 1
  RRNumberErrors* = 0
  RRNumberEvents* = 1
  constX_RRQueryVersion* = 0 # we skip 1 to make old clients fail pretty immediately
  X_RROldGetScreenInfo* = 1
  X_RR1_0SetScreenConfig* = 2 # V1.0 apps share the same set screen config request id
  constX_RRSetScreenConfig* = 2
  X_RROldScreenChangeSelectInput* = 3 # 3 used to be ScreenChangeSelectInput:  deprecated
  constX_RRSelectInput* = 4
  constX_RRGetScreenInfo* = 5      # used in XRRSelectInput
  RRScreenChangeNotifyMask* = 1 shl 0
  RRScreenChangeNotify* = 0   # used in the rotation field:  rotation and reflection in 0.1 proto.
  RR_Rotate_0* = 1
  RR_Rotate_90* = 2
  RR_Rotate_180* = 4
  RR_Rotate_270* = 8          # new in 1.0 protocol, to allow reflection of screen
  RR_Reflect_X* = 16
  RR_Reflect_Y* = 32
  RRSetConfigSuccess* = 0
  RRSetConfigInvalidConfigTime* = 1
  RRSetConfigInvalidTime* = 2
  RRSetConfigFailed* = 3

type
  PXRRScreenSize* = ptr XRRScreenSize
  XRRScreenSize*{.bycopy.} = object  #
                                    #   Events.
                                    #
    width*, height*: cint
    mwidth*, mheight*: cint

  XRRScreenChangeNotifyEvent*{.bycopy.} = object  # internal representation is private to the library
    typ*: cint               # event base
    serial*: culong          # # of last request processed by server
    send_event*: XBool        # true if this came from a SendEvent request
    display*: PDisplay       # Display the event was read from
    window*: Window          # window which selected for this event
    root*: Window            # Root window for changed screen
    timestamp*: Time         # when the screen change occurred
    config_timestamp*: Time  # when the last configuration change
    size_index*: SizeID
    subpixel_order*: SubpixelOrder
    rotation*: Rotation
    width*: cint
    height*: cint
    mwidth*: cint
    mheight*: cint

  PXRRScreenConfiguration* = ptr XRRScreenConfiguration
  XRRScreenConfiguration* {.bycopy.} = object

  PXRRModeInfo* = ptr XRRModeInfo
  XRRModeInfo* {.bycopy.} = object
    id*: RRMode
    width*, height*: cuint
    dotClock*: culong
    hSyncStart*: cuint
    hSyncEnd*: cuint
    hTotal*: cuint
    hSkew*: cuint
    vSyncStart*: cuint
    vSyncEnd*: cuint
    vTotal*: cuint
    name*: cstring
    nameLength*: cuint
    modeFlags*: XRRModeFlags

  PXRRScreenResources* = ptr XRRScreenResources
  XRRScreenResources* {.bycopy.} = object
    timestamp*, configTimestamp*: Time
    ncrtc*: cint
    crtcs*: ptr RRCrtc
    noutput*: cint
    outputs*: ptr RROutput
    nmode*: cint
    modes*: ptr XRRModeInfo

  PXRROutputInfo* = ptr XRROutputInfo
  XRROutputInfo* {.bycopy.} = object
    timestamp*: Time
    crtc*: RRCrtc
    name*: cstring
    nameLen*: cint
    mmWidth*, mmHeight*: culong
    connection*: Connection
    subpixelOrder*: SubpixelOrder
    ncrtc*: cint
    crtcs*: ptr UncheckedArray[RRCrtc]
    nclone*: cint
    clones*: ptr UncheckedArray[RROutput]
    nmode*: cint
    npreferred*: cint
    modes*: ptr UncheckedArray[RRMode]

  PXRRPropertyInfo* = ptr XRRPropertyInfo
  XRRPropertyInfo* {.bycopy.} = object
    pending, range, immutable: bool
    numValues: cint
    values: ptr UncheckedArray[clong]

  XRRCrtcInfo*  {.bycopy.} = object
   timestamp*: Time
   x*, y*: int
   width*, height*: cuint
   mode*: RRMode
   rotation*: Rotation
   noutput*: int
   outputs*: ptr UncheckedArray[RROutput]
   rotations*: Rotation
   npossible*: int
   possible*: ptr UncheckedArray[RROutput]


  RandrFormat* = enum
    randrFormat16bit = 16, randrFormat32bit = 32

{.deprecated: [TXRRScreenSize: XRRScreenSize].}
{.deprecated: [TXRRScreenChangeNotifyEvent: XRRScreenChangeNotifyEvent].}
{.deprecated: [TXRRScreenConfiguration: XRRScreenConfiguration].}
{.deprecated: [TXRRModeInfo: XRRModeInfo].}
{.deprecated: [TXRRScreenResources: XRRScreenResources].}
{.deprecated: [TXRROutputInfo: XRROutputInfo].}
{.deprecated: [TXRRPropertyInfo: XRRPropertyInfo].}

proc XRRQueryExtension*(dpy: PDisplay, event_basep, error_basep: Pcint): XBool{.
    cdecl, importc.}
proc XRRQueryVersion*(dpy: PDisplay, major_versionp: Pcint,
                      minor_versionp: Pcint): Status{.cdecl,
    importc.}
proc XRRQueryOutputProperty*(dpy: PDisplay, output: RROutput, property: Atom):
    PXRRPropertyInfo {.cdecl, importc.}
proc XRRChangeOutputProperty*(dpy: PDisplay, output: RROutput,
    property, kind: Atom, format, mode: cint, data: ptr char, nelements: cint) {.
    cdecl, importc.}
proc XRRGetScreenInfo*(dpy: PDisplay, draw: Drawable): PXRRScreenConfiguration{.
    cdecl, importc.}
proc XRRFreeScreenConfigInfo*(config: PXRRScreenConfiguration){.cdecl,
    dynlib: libXrandr, importc.}
  #
  #  Note that screen configuration changes are only permitted if the client can
  #  prove it has up to date configuration information.  We are trying to
  #  insist that it become possible for screens to change dynamically, so
  #  we want to ensure the client knows what it is talking about when requesting
  #  changes.
  #
proc XRRSetScreenConfig*(dpy: PDisplay, config: PXRRScreenConfiguration,
                         draw: Drawable, size_index: cint, rotation: Rotation,
                         timestamp: Time): Status{.cdecl,
    importc.}
  # added in v1.1, sorry for the lame name
proc XRRSetScreenConfigAndRate*(dpy: PDisplay, config: PXRRScreenConfiguration,
                                draw: Drawable, size_index: cint,
                                rotation: Rotation, rate: cshort,
                                timestamp: Time): Status{.cdecl,
    dynlib: libXrandr, importc.}
proc XRRConfigRotations*(config: PXRRScreenConfiguration,
                         current_rotation: PRotation): Rotation{.cdecl,
    dynlib: libXrandr, importc.}
proc XRRConfigTimes*(config: PXRRScreenConfiguration, config_timestamp: PTime): Time{.
    cdecl, importc.}
proc XRRConfigSizes*(config: PXRRScreenConfiguration, nsizes: Pcint): PXRRScreenSize{.
    cdecl, importc.}
proc XRRConfigRates*(config: PXRRScreenConfiguration, sizeID: cint,
                     nrates: Pcint): ptr int16{.cdecl, importc.}
proc XRRConfigCurrentConfiguration*(config: PXRRScreenConfiguration,
                                    rotation: PRotation): SizeID{.cdecl,
    dynlib: libXrandr, importc.}
proc XRRConfigCurrentRate*(config: PXRRScreenConfiguration): cshort{.cdecl,
    dynlib: libXrandr, importc.}
proc XRRRootToScreen*(dpy: PDisplay, root: Window): cint{.cdecl,
    dynlib: libXrandr, importc.}
  #
  #  returns the screen configuration for the specified screen:  does a lazy
  #  evalution to delay getting the information, and caches the result.
  #  These routines should be used in preference to XRRGetScreenInfo
  #  to avoid unneeded round trips to the X server.  These are new
  #  in protocol version 0.1.
  #
proc XRRScreenConfig*(dpy: PDisplay, screen: cint): PXRRScreenConfiguration{.
    cdecl, importc.}
proc XRRConfig*(screen: PScreen): PXRRScreenConfiguration{.cdecl,
    dynlib: libXrandr, importc.}
proc XRRSelectInput*(dpy: PDisplay, window: Window, mask: cint){.cdecl,
    dynlib: libXrandr, importc.}
  #
  #  the following are always safe to call, even if RandR is not implemented
  #  on a screen
  #
proc XRRRotations*(dpy: PDisplay, screen: cint, current_rotation: PRotation): Rotation{.cdecl, importc.}
proc XRRSizes*(dpy: PDisplay, screen: cint, nsizes: Pcint): PXRRScreenSize{.cdecl, importc.}
proc XRRRates*(dpy: PDisplay, screen: cint, sizeID: cint, nrates: Pcint): ptr int16{.cdecl, importc.}
proc XRRTimes*(dpy: PDisplay, screen: cint, config_timestamp: PTime): Time{.cdecl, importc.}

proc XRRUpdateConfiguration*(event: PXEvent): cint{.cdecl, importc.}
proc XRRGetScreenResourcesCurrent*(dpy: PDisplay, win: Window): PXRRScreenResources {.cdecl, importc.}
proc XRRFreeScreenResources*(res: PXRRScreenResources) {.cdecl, importc.}
proc XRRGetOutputInfo*(dpy: PDisplay, res: PXRRScreenResources, ret: RROutput): PXRROutputInfo {.cdecl, importc.}
proc XRRFreeOutputInfo*(info: PXRROutputInfo) {.cdecl, importc.}

# added
proc XRRGetCrtcInfo*(dpy: ptr Display
                    , resources: ptr XRRScreenResources
                    , crtc: RRCrtc
                    ): ptr XRRCrtcInfo {.importc, cdecl.}