Skip to content

Commit 47cbfc8

Browse files
author
BoboTiG
committed
MSS library review
1 parent 2ba67d3 commit 47cbfc8

File tree

7 files changed

+57
-56
lines changed

7 files changed

+57
-56
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
build/
2-
dep/linux/mss.so
32
dist/
3+
libmss*
44
MANIFEST*
55
*.png
66
*.png.old

README.rst

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ You can install it with pip::
1717

1818
pip install --upgrade mss
1919

20-
Or you may just drop it in your project and forget about it.
21-
22-
**For GNU/Linux users concerned by speed**, you will certainly need the MSS library. It is built when using `pip` but if you want to do it manually::
23-
24-
gcc -shared -rdynamic -fPIC -s -O3 -lX11 -o libmss.so dep/linux/mss.c
25-
26-
That's it! Ship the libmms.so file along with mss.py and you are ready to take screenshots at the light speed!
27-
2820
Support
2921
-------
3022

dep/linux/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CC = gcc
2+
CFLAGS = -O3 -s -shared -rdynamic -fPIC -Wall -pedantic -lX11
3+
4+
all:
5+
${CC} ${CFLAGS} -m64 mss.c -o libmss-64.so
6+
${CC} ${CFLAGS} -m32 mss.c -o libmss.so
7+
8+
clean:
9+
rm -f libmss*

dep/linux/libmss-64.so

4.51 KB
Binary file not shown.

dep/linux/mss.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,14 @@
77
* See MSSLinux:get_pixels() for a real world example.
88
*
99
* Source: https://github.com/BoboTiG/python-mss
10-
*
11-
* Build: python setyp.py build_ext
12-
* or: gcc -shared -rdynamic -fPIC -s -O3 -lX11 -o libmss.so mss.c
13-
*
1410
*/
1511

1612
#include <X11/Xlib.h>
1713

1814
/* Prototype from Xutil.h */
1915
extern unsigned long XGetPixel(XImage *ximage, int x, int y);
2016

21-
void GetXImagePixels(
17+
int GetXImagePixels(
2218
XImage *ximage,
2319
const unsigned int width,
2420
const unsigned int height,
@@ -30,6 +26,16 @@ void GetXImagePixels(
3026
unsigned int x, y, offset;
3127
unsigned long pixel;
3228

29+
if ( !ximage ) {
30+
return -1;
31+
}
32+
if ( !width || !height || !red_mask || !blue_mask || !green_mask ) {
33+
return -1;
34+
}
35+
if ( !pixels ) {
36+
return 0;
37+
}
38+
3339
for ( x = 0; x < width; ++x ) {
3440
for ( y = 0; y < height; ++y ) {
3541
offset = x * 3 + width * y * 3;
@@ -39,5 +45,5 @@ void GetXImagePixels(
3945
pixels[offset + 2] = pixel & blue_mask;
4046
}
4147
}
42-
return;
48+
return 1;
4349
}

mss.py

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -301,13 +301,7 @@ def __del__(self):
301301
pass
302302

303303
def __init__(self):
304-
''' GNU/Linux initialisations.
305-
306-
Paths where the MSS library is loaded from:
307-
- /usr/local/lib/pythonx.y/dist-packages/
308-
- current working directory
309-
if no one found, use of the _very_ slow method get_pixels_slow().
310-
'''
304+
''' GNU/Linux initialisations. '''
311305

312306
disp = None
313307
self.display = None
@@ -330,26 +324,12 @@ def __init__(self):
330324
raise ScreenshotError('MSS: no Xrandr library found.')
331325
self.xrandr = cdll.LoadLibrary(xrandr)
332326

333-
v_maj, v_min, _, _, _ = sys.version_info
334-
lib_dir = '/usr/local/lib/python{0}.{1}/dist-packages'.format(v_maj,
335-
v_min)
336-
libmss = '{0}/libmss.so'.format(lib_dir)
337-
try:
327+
libmss = find_library('mss')
328+
if libmss:
338329
self.mss = cdll.LoadLibrary(libmss)
339-
except OSError:
340-
try:
341-
libmss = '{0}/libmss.cpython-{1}{2}m.so'.format(lib_dir, v_maj,
342-
v_min)
343-
self.mss = cdll.LoadLibrary(libmss)
344-
except OSError:
345-
try:
346-
libmss = find_library('mss')
347-
self.mss = cdll.LoadLibrary(libmss)
348-
except OSError:
349-
msg = 'MSS: no MSS library found. ' + \
350-
'Using slow native function.'
351-
print(msg)
352-
self.mss = False
330+
else:
331+
print('MSS: no MSS library found. Using slow native function.')
332+
self.mss = False
353333

354334
self._set_argtypes()
355335
self._set_restypes()
@@ -416,7 +396,7 @@ def _set_restypes(self):
416396
self.xrandr.XRRFreeScreenResources.restype = c_void_p
417397
self.xrandr.XRRFreeCrtcInfo.restype = c_void_p
418398
if self.mss:
419-
self.mss.GetXImagePixels.restype = c_void_p
399+
self.mss.GetXImagePixels.restype = c_int
420400

421401
def enum_display_monitors(self, screen=0):
422402
''' Get positions of one or more monitors.
@@ -474,10 +454,15 @@ def get_pixels(self, monitor):
474454
else:
475455
buffer_len = height * width * 3
476456
self.image = create_string_buffer(buffer_len)
477-
self.mss.GetXImagePixels(ximage, width, height,
478-
ximage.contents.red_mask,
479-
ximage.contents.green_mask,
480-
ximage.contents.blue_mask, self.image)
457+
ret = self.mss.GetXImagePixels(ximage, width, height,
458+
ximage.contents.red_mask,
459+
ximage.contents.green_mask,
460+
ximage.contents.blue_mask,
461+
self.image)
462+
if not ret:
463+
self.xlib.XDestroyImage(ximage)
464+
err = 'MSS: libmss.GetXImagePixels() failed ({0}).'.format(ret)
465+
raise ScreenshotError(err)
481466
self.xlib.XDestroyImage(ximage)
482467
return self.image
483468

setup.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11

2-
from distutils.core import setup, Extension
3-
from platform import system
2+
from distutils.core import setup
3+
from os import unlink
4+
from platform import architecture, system
5+
from shutil import copyfile as copy
46
from mss import __version__
57

68

7-
open('MANIFEST.in', 'w').write("\n".join((
8-
'include *.rst',
9-
'include doc/*'
10-
)))
9+
with open('MANIFEST.in', 'w') as fileh:
10+
files = ['include *.rst', 'include doc/*', 'prune test*']
11+
fileh.write("\n".join(files))
1112

12-
libmss = False
13+
data_files = []
1314
if system() == 'Linux':
14-
libmss = [Extension('libmss', sources = ['dep/linux/mss.c'],
15-
libraries = ['X11'])]
15+
file_ok = 'libmss.so'
16+
file_ = 'dep/linux/libmss.so'
17+
if architecture()[0].startswith('64'):
18+
file_ = 'dep/linux/libmss-64.so'
19+
copy(file_, file_ok)
20+
data_files.append(('/usr/local/lib/', [file_ok]))
1621

1722
setup(
1823
name='mss',
@@ -61,6 +66,10 @@
6166
'Topic :: Utilities'
6267
],
6368
url='https://github.com/BoboTiG/python-mss',
64-
ext_modules = libmss
69+
data_files=data_files
6570
)
6671

72+
try:
73+
unlink(file_ok)
74+
except NameError:
75+
pass

0 commit comments

Comments
 (0)