Skip to content

Commit

Permalink
(#26) Don't link with xshm if mitshm parameter is not provided
Browse files Browse the repository at this point in the history
  • Loading branch information
rexim committed Dec 12, 2019
1 parent 735c6d2 commit 095e285
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 72 deletions.
6 changes: 1 addition & 5 deletions src/boomer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,7 @@ proc main() =

var shaderProgram = newShaderProgram(vertexShader, fragmentShader)

var screenshot =
when defined(mitshm):
newMitshmScreenshot(display)
else:
newDefaultScreenshot(display)
var screenshot = newScreenshot(display)
defer: screenshot.destroy(display)

let w = screenshot.image.width.float32
Expand Down
127 changes: 60 additions & 67 deletions src/screenshot.nim
Original file line number Diff line number Diff line change
@@ -1,100 +1,93 @@
import x11/xlib, x11/x, x11/xutil, x11/xshm
import syscall
import x11/xlib, x11/x, x11/xutil

const
IPC_PRIVATE = 0
IPC_CREAT = 512
IPC_RMID = 0
when defined(mitshm):
import x11/xshm
import syscall

type ScreenshotBackend* = enum
DEFAULT, # XGetImage
MITSHM # XShmGetImage
const
IPC_PRIVATE = 0
IPC_CREAT = 512
IPC_RMID = 0

type Screenshot* = object
backend*: ScreenshotBackend
image*: PXImage
shminfo*: PXShmSegmentInfo

proc newDefaultScreenshot*(display: PDisplay): Screenshot =
result.backend = DEFAULT

var root = DefaultRootWindow(display)
var attributes: TXWindowAttributes
discard XGetWindowAttributes(display, root, addr attributes)
result.image = XGetImage(display, root,
0, 0,
attributes.width.cuint,
attributes.height.cuint,
AllPlanes,
ZPixmap)

proc newMitshmScreenshot*(display: PDisplay): Screenshot =
result.backend = MITSHM
result.shminfo = cast[PXShmSegmentInfo](allocShared(sizeof(TXShmSegmentInfo)))
when defined(mitshm):
shminfo*: PXShmSegmentInfo

proc newScreenshot*(display: PDisplay): Screenshot =
var root = DefaultRootWindow(display)
var attributes: TXWindowAttributes
discard XGetWindowAttributes(display, root, addr attributes)

let screen = DefaultScreen(display)
result.image = XShmCreateImage(
display,
DefaultVisual(display, screen),
DefaultDepthOfScreen(ScreenOfDisplay(display, screen)).cuint,
ZPixmap,
nil,
result.shminfo,
attributes.width.cuint,
attributes.height.cuint)
when defined(mitshm):
result.shminfo = cast[PXShmSegmentInfo](
allocShared(sizeof(TXShmSegmentInfo)))
let screen = DefaultScreen(display)
result.image = XShmCreateImage(
display,
DefaultVisual(display, screen),
DefaultDepthOfScreen(ScreenOfDisplay(display, screen)).cuint,
ZPixmap,
nil,
result.shminfo,
attributes.width.cuint,
attributes.height.cuint)

result.shminfo.shmid = syscall(
SHMGET,
IPC_PRIVATE,
result.image.bytes_per_line * result.image.height,
IPC_CREAT or 0o777).cint
result.shminfo.shmid = syscall(
SHMGET,
IPC_PRIVATE,
result.image.bytes_per_line * result.image.height,
IPC_CREAT or 0o777).cint

result.shminfo.shmaddr = cast[cstring](syscall(
SHMAT,
result.shminfo.shmid,
0, 0))
result.image.data = result.shminfo.shmaddr
result.shminfo.readOnly = 0
result.shminfo.shmaddr = cast[cstring](syscall(
SHMAT,
result.shminfo.shmid,
0, 0))
result.image.data = result.shminfo.shmaddr
result.shminfo.readOnly = 0

discard XShmAttach(display, result.shminfo)
discard XShmGetImage(display, root, result.image, 0.cint, 0.cint, AllPlanes)
discard XShmAttach(display, result.shminfo)
discard XShmGetImage(
display, root, result.image, 0.cint, 0.cint, AllPlanes)
else:
result.image = XGetImage(
display, root,
0, 0,
attributes.width.cuint,
attributes.height.cuint,
AllPlanes,
ZPixmap)

proc refresh*(screenshot: var Screenshot, display: PDisplay) =
var root = DefaultRootWindow(display)

case screenshot.backend
of DEFAULT:
screenshot.image =
XGetSubImage(display, root,
0, 0,
screenshot.image.width.cuint,
screenshot.image.height.cuint,
AllPlanes,
ZPixmap,
screenshot.image,
0, 0)
of MITSHM:
when defined(mitshm):
discard XShmGetImage(
display,
root, screenshot.image,
0.cint, 0.cint,
AllPlanes)
else:
screenshot.image = XGetSubImage(
display, root,
0, 0,
screenshot.image.width.cuint,
screenshot.image.height.cuint,
AllPlanes,
ZPixmap,
screenshot.image,
0, 0)

proc destroy*(screenshot: var Screenshot, display: PDisplay) =
case screenshot.backend
of DEFAULT:
discard XDestroyImage(screenshot.image)
of MITSHM:
when defined(mitshm):
discard XSync(display, 0)
discard XShmDetach(display, screenshot.shminfo)
discard XDestroyImage(screenshot.image)
discard syscall(SHMDT, screenshot.shminfo.shmaddr)
discard syscall(SHMCTL, screenshot.shminfo.shmid, IPC_RMID, 0)
deallocShared(screenshot.shminfo)
else:
discard XDestroyImage(screenshot.image)

proc saveToPPM*(image: PXImage, filePath: string) =
var f = open(filePath, fmWrite)
Expand Down

0 comments on commit 095e285

Please sign in to comment.