Skip to content

Instantly share code, notes, and snippets.

@flo-l
Created March 22, 2016 09:55
Show Gist options
  • Save flo-l/f383a541d5d93fc7e9b0 to your computer and use it in GitHub Desktop.
Save flo-l/f383a541d5d93fc7e9b0 to your computer and use it in GitHub Desktop.
This is a super dumb port of [this gist](https://gist.github.com/vkravets/5370589). It should be functionally equivalent. I ported the code because I had trouble getting it to work with jruby. This is confirmed to work on Fedora 23 with jruby 9.0.4.0. All credits to the original author Vladimir Kravets.
# This is a port of [this gist](https://gist.github.com/vkravets/5370589)
# I literally copied the code and ported it line by line, so all credits belong to the author, Vladimir Kravets.
require 'java'
# I guess this is necessary..
# I'm on Fedora 23, I had to install the packages jna and jna-contrib
# on other platforms these two jar files might live in other locations
require '/usr/share/java/jna.jar'
require '/usr/share/java/jna/jna-platform.jar'
java_import 'com.sun.jna.Native'
java_import 'com.sun.jna.NativeLong'
java_import 'com.sun.jna.platform.unix.X11'
class X11FullscreenHelper
NET_WM_STATE_REMOVE = 0
NET_WM_STATE_ADD = 1
attr_reader :isFullScreenMode
def initialize
@isFullScreenMode = false
end
def setFullScreenWindow(w, fullScreen)
x = X11.INSTANCE
display = nil
begin
display = x.XOpenDisplay(nil)
result = sendClientMessage(
display,
Native.getWindowID(w),
"_NET_WM_STATE",
[
NativeLong.new(fullScreen ? NET_WM_STATE_ADD : NET_WM_STATE_REMOVE),
x.XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", false),
x.XInternAtom(display, "_NET_WM_STATE_ABOVE", false),
NativeLong.new(0),
NativeLong.new(0)
].to_java(NativeLong)
)
@isFullScreenMode = (result != 0) && fullScreen;
result != 0
ensure
x.XCloseDisplay(display) if display
end
end
def sendClientMessage(display, wid, msg, data)
raise ArgumentException unless data.length == 5
x = X11.INSTANCE
event = X11::XEvent.new
event.type = X11::ClientMessage
event.setType(X11::XClientMessageEvent.java_class)
event.xclient.type = X11::ClientMessage
event.xclient.serial = NativeLong.new(0)
event.xclient.send_event = 1
event.xclient.message_type = x.XInternAtom(display, msg, false)
event.xclient.window = X11::Window.new(wid)
event.xclient.format = 32
event.xclient.data.setType([].to_java(NativeLong).java_class)
event.xclient.data.l = data
mask = NativeLong.new(X11.SubstructureRedirectMask | X11.SubstructureNotifyMask)
result = x.XSendEvent(display, x.XDefaultRootWindow(display), 0, mask, event)
x.XFlush(display)
result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment