Created
November 1, 2010 20:48
-
-
Save lian/658842 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # % ruby scrot.rb /tmp/screenshot.png | |
| # scrot.rb uses xlib + imlib via ffi to capture screenshots. | |
| require 'ffi' | |
| module X11 | |
| extend FFI::Library | |
| ffi_lib 'X11' | |
| attach_function :XOpenDisplay, [:string], :pointer | |
| attach_function :XFlush, [:pointer], :pointer | |
| module_function | |
| def get_display(display = nil); XOpenDisplay(display || ENV['DISPLAY']); end | |
| @ptr_size = FFI.type_size(:pointer) | |
| def ptr_size; @ptr_size; end | |
| def ScreenOfDisplay(d, i); d.get_pointer( @ptr_size == 4 ? 35*4 : 58*4 ); end | |
| def RootWindow(d, i=0); ScreenOfDisplay(d,i).get_int(2*@ptr_size); end | |
| def DefaultVisual(d, i=0); ScreenOfDisplay(d,i).get_pointer( @ptr_size == 4 ? 40 : 48); end | |
| def DefaultColormap(d, i=0); ScreenOfDisplay(d,i).get_int(12*@ptr_size); end | |
| end | |
| module Imlib | |
| extend FFI::Library | |
| ffi_lib 'Imlib2' | |
| attach_function :context_set_display, :imlib_context_set_display, [:pointer], :int | |
| attach_function :context_set_visual, :imlib_context_set_visual, [:pointer], :int | |
| attach_function :context_set_colormap, :imlib_context_set_colormap, [:int], :int | |
| attach_function :context_set_color_modifier, :imlib_context_set_color_modifier, [:pointer], :int | |
| attach_function :context_set_operation, :imlib_context_set_operation, [:int], :int | |
| IMLIB_OP_COPY = 0 | |
| attach_function :context_set_drawable, :imlib_context_set_drawable, [:int], :int | |
| attach_function :create_image_from_drawable, | |
| :imlib_create_image_from_drawable, [:int, :int, :int, :int, :int, :int], :pointer | |
| attach_function :context_set_image, :imlib_context_set_image, [:pointer], :int | |
| attach_function :image_set_format, :imlib_image_set_format, [:string], :int | |
| attach_function :save_image, :imlib_save_image, [:string], :int | |
| module_function | |
| def setup_context(display, visual, colormap, colormap_mod=nil, operation=nil) | |
| context_set_display(display) | |
| context_set_visual(visual) | |
| context_set_colormap(colormap) | |
| context_set_color_modifier(colormap_mod || nil) | |
| context_set_operation(operation || IMLIB_OP_COPY) | |
| end | |
| end | |
| if $0 == __FILE__ | |
| fileout = ARGV.first || File.join(ENV['HOME'], 'screenshot.png') | |
| $d = X11.get_display | |
| screen, root = X11.ScreenOfDisplay($d, 0), X11.RootWindow($d, 0) | |
| visual, cm = X11.DefaultVisual($d, 0), X11.DefaultColormap($d, 0) | |
| Imlib.setup_context($d, visual, cm, nil, Imlib::IMLIB_OP_COPY) | |
| Imlib.context_set_drawable(root) | |
| s_width, s_height = *screen.get_array_of_int(3*X11.ptr_size, 2) | |
| image = Imlib.create_image_from_drawable(0, 0, 0, s_width, s_height, 1) | |
| Imlib.context_set_image(image) | |
| Imlib.image_set_format('png') | |
| Imlib.save_image(fileout) if File.directory?(File.dirname(fileout)) | |
| end | |
| __END__ | |
| Imlib_Color color; int x, y; | |
| for (x = 0; x < screen->width; x++) { | |
| for (y = 0; y < screen->height; y++) { | |
| imlib_image_query_pixel(x, y, &color); | |
| printf("#%02x%02x%02x\n", color.red, color.green, color.blue); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment