Skip to content

Instantly share code, notes, and snippets.

@Largo
Created June 12, 2023 03:08
Show Gist options
  • Save Largo/bf95d8d0089f7c411fb7bdc48c8e6a30 to your computer and use it in GitHub Desktop.
Save Largo/bf95d8d0089f7c411fb7bdc48c8e6a30 to your computer and use it in GitHub Desktop.
Ruby and VLC gtk. Probably doesn't work
# released under the public domain
# I have sort of gotten this to work last year, but I'm in a hurry now. I think it doesn't completely work with GTK...
require 'gtk3'
#require 'vlc'
require_relative 'vlc'
require 'ffi'
module User32
extend FFI::Library
ffi_lib 'user32'
end
module User32
attach_function :GetForegroundWindow, [], :pointer
end
class MyWindow < Gtk::Window
def initialize
super
end
def addVlc(win)
win.signal_connect("realize") { |win|
hwnd = User32.GetForegroundWindow
xid = hwnd.address
@vlc_player = VLC::VLCPlayer.new
@vlc_player.play(File.join(__dir__, 'a.mp4'))
@vlc_player.set_hwnd(xid)
}
end
end
win = MyWindow.new
win.set_size_request(300, 300)
#win.show_all
#xid_value = win.window.
win.signal_connect("delete-event") { |_widget| Gtk.main_quit }
win.addVlc(win)
win.realize
Gtk.main
# released under the public domain
require 'ffi'
module VLC
extend FFI::Library
#ffi_lib File.join(__dir__, 'libvlc.dll')
#ffi_lib 'libvlc'
#ffi_lib File.dirname(__FILE__) + '\libvlc.dll'
ffi_lib 'C:\Program Files\VideoLAN\VLC\libvlc.dll'
VLC_LIBVLC_MEDIA_H = 1
libvlc_media_option_unique = 0x100
libvlc_media_option_trusted = 0x2
attach_function :libvlc_new, [:int, :pointer], :pointer
attach_function :libvlc_media_new_path, [:pointer, :string], :pointer
attach_function :libvlc_media_new_location, [:pointer, :string], :pointer
attach_function :libvlc_media_player_new_from_media, [:pointer], :pointer
attach_function :libvlc_media_player_play, [:pointer], :void
attach_function :libvlc_media_player_stop, [:pointer], :void
attach_function :libvlc_media_player_set_hwnd, [:pointer, :pointer], :void
attach_function :libvlc_media_player_release, [:pointer], :void
attach_function :libvlc_release, [:pointer], :void
class VLCPlayer
def initialize
@vlc = VLC.libvlc_new(0, nil)
@media_player = nil
end
def play(path)
media = VLC.libvlc_media_new_path(@vlc, path)
@media_player = VLC.libvlc_media_player_new_from_media(media)
VLC.libvlc_media_player_play(@media_player)
end
def stop
VLC.libvlc_media_player_stop(@media_player)
end
def set_hwnd(hwnd)
VLC.libvlc_media_player_set_hwnd(@media_player, hwnd)
end
def release
VLC.libvlc_media_player_release(@media_player)
VLC.libvlc_release(@vlc)
end
end
end
module User32
extend FFI::Library
ffi_lib :user32
attach_function :FindWindowA, [:string, :string], :pointer
attach_function :GetForegroundWindow, [], :pointer
attach_function :find_window, :FindWindowA, [:string, :string], :pointer
attach_function :get_window_text, :GetWindowTextA, [:pointer, :pointer, :int], :int
ffi_convention :stdcall
end
def w(title:)
hwnd = User32.find_window(nil, title)
return hwnd
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment