Advertisement
Guest User

X11 window bug example

a guest
Jun 8th, 2015
420
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. require 'gtk2'
  3.  
  4. #setup some helper functions to block so the script doesn't exit before we've had a chance to see the behavior
  5. if !defined?(wait_until)
  6.     def wait_until(announce=nil)
  7.         priosave = Thread.current.priority
  8.         Thread.current.priority = 0
  9.         unless announce.nil? or yield
  10.             puts(announce)
  11.         end
  12.         until yield
  13.             sleep 0.25
  14.         end
  15.         Thread.current.priority = priosave
  16.     end
  17.  
  18.     def wait_while(announce=nil)
  19.         priosave = Thread.current.priority
  20.         Thread.current.priority = 0
  21.         unless announce.nil? or !yield
  22.             puts(announce)
  23.         end
  24.         while yield
  25.             sleep 0.25
  26.         end
  27.         Thread.current.priority = priosave
  28.     end
  29.  
  30. end
  31.  
  32. if defined?(Gtk) && !defined?(Gtk.queue)
  33.     module Gtk
  34.         # Calling Gtk API in a thread other than the main thread may cause random segfaults
  35.         def Gtk.queue &block
  36.             GLib::Timeout.add(1) {
  37.                 begin
  38.                     block.call
  39.                 rescue
  40.                     puts "error in Gtk.queue: #{$!}"
  41.                 rescue SyntaxError
  42.                     puts "error in Gtk.queue: #{$!}"
  43.                 rescue SystemExit
  44.                     nil
  45.                 rescue SecurityError
  46.                     puts "error in Gtk.queue: #{$!}"
  47.                 rescue ThreadError
  48.                     puts "error in Gtk.queue: #{$!}"
  49.                 rescue SystemStackError
  50.                     puts "error in Gtk.queue: #{$!}"
  51.                 rescue Exception
  52.                     puts "error in Gtk.queue: #{$!}"
  53.                 rescue ScriptError
  54.                     puts "error in Gtk.queue: #{$!}"
  55.                 rescue LoadError
  56.                     puts "error in Gtk.queue: #{$!}"
  57.                 rescue NoMemoryError
  58.                     puts "error in Gtk.queue: #{$!}"
  59.                 rescue
  60.                     puts "error in Gtk.queue: #{$!}"
  61.                 end
  62.                 false # don't repeat timeout
  63.             }
  64.         end
  65.     end
  66. end
  67.  
  68.  
  69. window_width       = 400
  70. window_height      = 800
  71. window_position    = [ 0, 0 ]
  72.  
  73. narost_exit     = false
  74. window_resized  = true
  75. start           = nil
  76.  
  77. window          = nil
  78.  
  79. require 'pp'
  80.  
  81. Gtk.queue {
  82.     window = Gtk::Window.new
  83.     window.title = "Test window"
  84.     window.signal_connect('delete_event') { narost_exit = true}
  85.  
  86.     window.show_all
  87.  
  88.     window_width = [window_width,100].max
  89.     window_height = [window_height,100].max
  90.     window.resize(window_width, window_height)
  91.  
  92.     window_position[0] = [0, (Gdk.screen_width - window_width)].min
  93.     window_position[1] = [0, (Gdk.screen_height - window_height)].min
  94.  
  95.     window.move(window_position[0], window_position[1])
  96.  
  97.     start = true
  98. }
  99.  
  100. thread = Thread.new {
  101.     # Create a second thread as our window event processor
  102.     Gtk.main
  103. }
  104.  
  105. # Wait for the window to open
  106. wait_until { start }
  107.  
  108. # Wait until the window is closed
  109. wait_while { !narost_exit }
Advertisement
RAW Paste Data Copied
Advertisement