WebInterface: small fixes for Screenshot API
[enigma2-plugins.git] / webinterface / src / WebChilds / Screenshot.py
1 from enigma import ePixmap, getDesktop
2 from twisted.web import resource, http, http_headers, server
3
4 from Tools.Log import Log
5
6 class ScreenshotResource(resource.Resource):
7         SPECIAL_ARGS = ('format', 'filename', 'save')
8
9         def render(self, request):
10                 args = []
11                 append = args.append
12
13                 # some presets
14                 filename = 'screenshot'
15                 imageformat = ePixmap.FMT_JPEG
16                 extension = 'jpg'
17                 jpgquali = '80'
18                 osd = False
19                 video = False
20                 save = False
21
22                 for key, value in request.args.items():
23                         if key in ScreenshotResource.SPECIAL_ARGS:
24                                 if key == 'extension':
25                                         extension = value[0]
26
27                                         if extension == 'jpg':
28                                                 #-j (quality) produce jpg files instead of bmp
29                                                 #Quality Setting
30                                                 if 'jpgquali' in request.args:
31                                                         jpgquali = request.args["jpgquali"][0]
32                                                         del request.args['jpgquali']
33                                         if extension == 'png':
34                                                 imageformat = ePixmap.FMT_PNG
35                                         if extension == 'gif':
36                                                 imageformat = ePixmap.FMT_GIF
37                                 elif key == 'filename':
38                                         filename = value[0]
39
40                                 elif key == 'save':
41                                         save = True
42                         else:
43                                 if key == "o":
44                                         osd = True
45                                 if key == "v":
46                                         video = True
47                 if not osd and not video:
48                         osd = video = True
49                 
50                 filename = "%s.%s" %(filename, extension)
51                 
52                 request.setHeader('Content-Disposition', 'inline; filename=%s;' %filename)
53                 mimetype = {'jpg' : 'jpeg'}.get(extension, extension)
54                 request.setHeader('Content-Type','image/%s' %mimetype)
55                 pixmap = ePixmap(None)
56                 size = getDesktop(0).size()
57                 if osd:
58                         if video:
59                                 pixmap.setPixmapFromScreen(size)
60                         else:
61                                 pixmap.setPixmapFromUI(size)
62                 elif video:
63                         pixmap.setPixmapFromVideo(0, size)
64
65 #               if save:
66 #                       pixmap.save(imageformat, "/tmp/%s" %filename)
67                 
68                 buffer = pixmap.save(imageformat)
69                 buffer.data()
70                 return bytes