Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#26) Add experimental Live Update feature #72

Merged
merged 2 commits into from
Dec 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ $ ln -s /path/to/boomer/overlay/ boomer
$ nix-env -iA nixos.boomer
```

## Experimental Live Update

See issue [#26]. For an experimental Live Update feature compile the application with the following flags:

```console
$ nimble build -d:live
```

The feature is really unstable and experimental, so use it at your own risk.

## References

- https://github.com/nim-lang/x11/blob/bf9dc74dd196a98b7c2a2beea4d92640734f7c60/examples/x11ex.nim
Expand All @@ -46,3 +56,5 @@ You can support my work via

- Twitch channel: https://www.twitch.tv/subs/tsoding
- Patreon: https://www.patreon.com/tsoding

[#26]: https://github.com/tsoding/boomer/issues/26
24 changes: 22 additions & 2 deletions src/boomer.nim
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ proc main() =
echo "Screen rate: ", rate

var screenshot = takeScreenshot(display, root)
assert screenshot.bpp == 32
assert screenshot.bits_per_pixel == 32

let screen = XDefaultScreen(display)
var glxMajor, glxMinor: int
Expand Down Expand Up @@ -264,7 +264,7 @@ proc main() =
# TODO(#13): the texture format is hardcoded
GL_BGRA,
GL_UNSIGNED_BYTE,
screenshot.pixels)
screenshot.data)
glGenerateMipmap(GL_TEXTURE_2D)

glUniform1i(glGetUniformLocation(shaderProgram, "tex".cstring), 0)
Expand Down Expand Up @@ -393,4 +393,24 @@ proc main() =
glXSwapBuffers(display, win)
glFinish()

when defined(live):
screenshot = XGetSubImage(display, root,
0, 0,
screenshot.width.cuint,
screenshot.height.cuint,
AllPlanes,
ZPixmap,
screenshot,
0, 0)
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB.GLint,
screenshot.width,
screenshot.height,
0,
# TODO(#13): the texture format is hardcoded
GL_BGRA,
GL_UNSIGNED_BYTE,
screenshot.data)

main()
19 changes: 6 additions & 13 deletions src/image.nim
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import x11/xlib, x11/x

type Image* = object
width*, height*, bpp*: cint
pixels*: cstring
type Image* = PXImage

proc saveToPPM*(image: Image, filePath: string) =
var f = open(filePath, fmWrite)
Expand All @@ -11,26 +9,21 @@ proc saveToPPM*(image: Image, filePath: string) =
writeLine(f, image.width, " ", image.height)
writeLine(f, 255)
for i in 0..<(image.width * image.height):
f.write(image.pixels[i * 4 + 2])
f.write(image.pixels[i * 4 + 1])
f.write(image.pixels[i * 4 + 0])
f.write(image.data[i * 4 + 2])
f.write(image.data[i * 4 + 1])
f.write(image.data[i * 4 + 0])

# NOTE: it's not possible to deallocate the returned Image because the
# reference to XImage is lost.
proc takeScreenshot*(display: PDisplay, root: TWindow): Image =
var attributes: TXWindowAttributes
discard XGetWindowAttributes(display, root, addr attributes)

var screenshot = XGetImage(display, root,
result = XGetImage(display, root,
0, 0,
attributes.width.cuint,
attributes.height.cuint,
AllPlanes,
ZPixmap)
if screenshot == nil:
if result == nil:
quit "Could not get a screenshot"

result.width = attributes.width
result.height = attributes.height
result.bpp = screenshot.bits_per_pixel
result.pixels = screenshot.data