Advertisement
Guest User

X11 window bug example

a guest
Jun 8th, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.94 KB | None | 0 0
  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.             respond(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.             respond(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.                     respond "error in Gtk.queue: #{$!}"
  41.                     Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
  42.                 rescue SyntaxError
  43.                     respond "error in Gtk.queue: #{$!}"
  44.                     Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
  45.                 rescue SystemExit
  46.                     nil
  47.                 rescue SecurityError
  48.                     respond "error in Gtk.queue: #{$!}"
  49.                     Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
  50.                 rescue ThreadError
  51.                     respond "error in Gtk.queue: #{$!}"
  52.                     Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
  53.                 rescue SystemStackError
  54.                     respond "error in Gtk.queue: #{$!}"
  55.                     Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
  56.                 rescue Exception
  57.                     respond "error in Gtk.queue: #{$!}"
  58.                     Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
  59.                 rescue ScriptError
  60.                     respond "error in Gtk.queue: #{$!}"
  61.                     Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
  62.                 rescue LoadError
  63.                     respond "error in Gtk.queue: #{$!}"
  64.                     Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
  65.                 rescue NoMemoryError
  66.                     respond "error in Gtk.queue: #{$!}"
  67.                     Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
  68.                 rescue
  69.                     respond "error in Gtk.queue: #{$!}"
  70.                     Lich.log "error in Gtk.queue: #{$!}\n\t#{$!.backtrace.join("\n\t")}"
  71.                 end
  72.                 false # don't repeat timeout
  73.             }
  74.         end
  75.     end
  76. end
  77.  
  78.  
  79. window_width       = 400
  80. window_height      = 800
  81. window_position    = [ 0, 0 ]
  82.  
  83. scale           = nil
  84. map_offset_x    = nil
  85. map_offset_y    = nil
  86. narost_exit     = false
  87. window_resized  = true
  88. start           = nil
  89.  
  90. window          = nil
  91.  
  92. Gtk.queue {
  93.     window = Gtk::Window.new
  94.     window.title = "Test window"
  95.     window.signal_connect('delete_event') { narost_exit = true}
  96.  
  97.     window.show_all
  98.  
  99.     window_width = [window_width,100].max
  100.     window_height = [window_height,100].max
  101.     window.resize(window_width, window_height)
  102.  
  103.     window_position[0] = [[0, window_position[0].to_i].max, (Gdk.screen_width-window_width)].min
  104.     window_position[1] = [[0, window_position[1].to_i].max, (Gdk.screen_height-window_height)].min
  105.     window.move(window_position[0], window_position[1])
  106.  
  107.     start = true
  108. }
  109.  
  110. thread = Thread.new {
  111.     Gtk.main
  112. }
  113.  
  114. wait_until { start }
  115.  
  116. require 'pp'
  117. pp Gdk.screen_width
  118. pp Gdk.screen_height
  119.  
  120. wait_while { !narost_exit }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement