Coverage for /home/antoine/projects/xpra-git/dist/python3/lib64/python/xpra/server/shadow/gtk_root_window_model.py : 43%
Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2# This file is part of Xpra.
3# Copyright (C) 2012-2019 Antoine Martin <antoine@xpra.org>
4# Xpra is released under the terms of the GNU GPL v2, or, at your option, any
5# later version. See the file COPYING for details.
7from gi.repository import Gdk
9from xpra.os_util import monotonic_time
10from xpra.codecs.image_wrapper import ImageWrapper
11from xpra.gtk_common.gtk_util import pixbuf_save_to_memory
12from xpra.log import Logger
14log = Logger("shadow")
17def get_rgb_rawdata(window, x, y, width, height):
18 """
19 Extracts pixels from the given pixmap
20 """
21 start = monotonic_time()
22 pixmap_w, pixmap_h = window.get_geometry()[2:4]
23 # Just in case we somehow end up with damage larger than the pixmap,
24 # we don't want to start requesting random chunks of memory (this
25 # could happen if a window is resized but we don't throw away our
26 # existing damage map):
27 assert x >= 0
28 assert y >= 0
29 if x + width > pixmap_w:
30 width = pixmap_w - x
31 if y + height > pixmap_h:
32 height = pixmap_h - y
33 if width <= 0 or height <= 0:
34 return None
35 pixbuf = Gdk.pixbuf_get_from_window(window, x, y, width, height)
36 log("get_rgb_rawdata(..) pixbuf.get_from_drawable took %s ms", int(1000*(monotonic_time()-start)))
37 raw_data = pixbuf.get_pixels()
38 rowstride = pixbuf.get_rowstride()
39 return (x, y, width, height, raw_data, "RGB", 24, rowstride, 3)
41def take_png_screenshot(window):
42 log("grabbing screenshot")
43 w,h = window.get_geometry()[2:4]
44 pixbuf = Gdk.pixbuf_get_from_window(window, 0, 0, w, h)
45 if not pixbuf:
46 return None
47 data = pixbuf_save_to_memory(pixbuf, "png")
48 rowstride = w*3
49 return w, h, "png", rowstride, data
52class GTKImageCapture:
53 def __init__(self, window):
54 self.window = window
56 def __repr__(self):
57 return "GTKImageCapture(%s)" % self.window
59 def clean(self):
60 pass
62 def refresh(self):
63 return True
65 def get_image(self, x, y, width, height):
66 attrs = get_rgb_rawdata(self.window, x, y, width, height)
67 if not attrs:
68 return None
69 return ImageWrapper(*attrs)
71 def take_screenshot(self):
72 return take_png_screenshot(self.window)
74def main(filename):
75 from xpra.gtk_common.gtk_util import get_default_root_window
76 root = get_default_root_window()
77 data = take_png_screenshot(root)[-1]
78 with open(filename, "wb") as f:
79 f.write(data)
80 return 0
82if __name__ == "__main__":
83 import sys
84 if len(sys.argv)!=2:
85 print("usage: %s filename.png" % sys.argv[0])
86 v = 1
87 else:
88 v = main(sys.argv[1])
89 sys.exit(v)