[MerlinEPGCenter] - implement workaround to fix zapping to channel when alternatives...
[enigma2-plugins.git] / webinterface / src / WebChilds / Screenshot.py
1 from enigma import ePixmap, getDesktop, eSize
2 from twisted.web import resource, http, http_headers, server
3
4 from Tools.Log import Log
5
6 class ScreenshotResource(resource.Resource):
7         def __init__(self):
8                 resource.Resource.__init__(self)
9                 self._buffer = None
10
11         def render(self, request):
12                 args = []
13                 append = args.append
14
15                 # some presets
16                 filename = 'screenshot'
17                 imageformat = ePixmap.FMT_PNG
18                 format = 'png'
19                 jpgquali = '80'
20                 osd = False
21                 video = False
22                 x = y = 0
23                 display = False
24
25                 for key, value in request.args.items():
26                         if key == 'format':
27                                 format = value[0]
28                                 if format == 'jpg':
29                                         #-j (quality) produce jpg files instead of bmp
30                                         #Quality Setting
31                                         if 'jpgquali' in request.args:
32                                                 jpgquali = request.args["jpgquali"][0]
33                                                 del request.args['jpgquali']
34                                 if format == 'jpg':
35                                         imageformat = ePixmap.FMT_JPEG
36                                 if format == 'gif':
37                                         imageformat = ePixmap.FMT_GIF
38                         elif key == 'filename':
39                                 filename = value[0]
40                         elif key == "osd":
41                                 osd = True
42                         elif key == "video":
43                                 video = True
44                         elif key == "res":
45                                 try:
46                                         x, y  = map(lambda val: int(val), value[0].split("x"))
47                                 except Exception as e:
48                                         print e
49                                         Log.w("%s is not a valid value for video size. Please use something in the style of '1280x720'" %value)
50                         elif key == 'display':
51                                 display = True
52                 if not osd and not video:
53                         osd = True
54
55                 filename = "%s.%s" %(filename, format)
56                 Log.i("display=%s, osd=%s, video=%s, filename=%s" %(display, osd, video, filename))
57                 request.setHeader('Content-Disposition', 'inline; filename=%s;' %filename)
58                 #no caching!
59                 request.setHeader('Cache-Control', 'no-cache, no-store, must-revalidate');
60                 request.setHeader('Cache-Directive', 'no-cache');
61                 request.setHeader('Pragma-Directive', 'no-cache');
62                 request.setHeader('Pragma', 'no-cache');
63                 request.setHeader('Expires', '0');
64
65                 screenid = 1 if display else 0
66                 desktop = getDesktop(screenid)
67                 if not desktop:
68                         request.setResponseCode(http.NOT_FOUND)
69                         request.finish()
70                         return server.NOT_DONE_YET
71                 mimetype = {'jpg' : 'jpeg'}.get(format, format)
72                 request.setHeader('Content-Type','image/%s' %mimetype)
73                 pixmap = ePixmap(None)
74                 size = desktop.size()
75                 if x > 0 and y > 0:
76                         size = eSize(x,y)
77
78                 if display:
79                         try:
80                                 pixmap.setPixmapFromUI(size, screenid)
81                         except:
82                                 request.setResponseCode(http.INTERNAL_SERVER_ERROR)
83                                 request.finish()
84                                 return server.NOT_DONE_YET
85                 elif osd:
86                         if video:
87                                 if not pixmap.setPixmapFromScreen(size):
88                                         Log.w("Failed setting pixmap from Screen!")
89                         else:
90                                 if not pixmap.setPixmapFromUI(size):
91                                         Log.w("Failed setting pixmap from UI!")
92                 elif video:
93                         if not pixmap.setPixmapFromVideo(0, size):
94                                 Log.w("Failed setting pixmap from Video!")
95
96                 buffer = pixmap.save(imageformat)
97                 if buffer:
98                         bytes = buffer.data()
99                         request.write(str(bytes))
100                         request.finish()
101                 return server.NOT_DONE_YET