File: webwin.py

package info (click to toggle)
displaycal-py3 3.9.16-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 29,120 kB
  • sloc: python: 115,777; javascript: 11,540; xml: 598; sh: 257; makefile: 173
file content (156 lines) | stat: -rw-r--r-- 4,112 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: utf-8 -*-

"""
Re-implementation of Argyll's webwin in pure python.

"""

import http.server
import time
from urllib.parse import unquote

from DisplayCAL.meta import name as appname, version as appversion


WEBDISP_HTML = (
    r"""<!DOCTYPE html>
<html>
<head>
<title>%s Web Display</title>
<script src="webdisp.js"></script>
<style>
html, body {`
    background: #000;
    margin: 0;
    padding: 0;
    overflow: hidden;
    height: 100%%;
}
#pattern {
    position: absolute;
    left: 45%%;
    top: 45%%;
    width: 10%%;
    height: 10%%;
}
</style>
</head>
<body>
<div id="pattern"></div>
</body>
</html>
"""
    % appname
)

WEBDISP_JS = r"""if (typeof XMLHttpRequest == "undefined") {
    XMLHttpRequest = function () {
        try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
            catch (e) {}
        try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
            catch (e) {}
        try { return new ActiveXObject("Microsoft.XMLHTTP"); }
            catch (e) {}
        throw new Error("This browser does not support XMLHttpRequest.");
    };
}

var cpat = ["#000"];
var oXHR;
var pat;

function XHR_request() {
    oXHR.open("GET", "/ajax/messages?" + encodeURIComponent(cpat.join("|") + "|" + Math.random()), true);
    oXHR.onreadystatechange = XHR_response;
    oXHR.send();
}

function XHR_response() {
    if (oXHR.readyState != 4)
        return;

    if (oXHR.status != 200) {
        return;
    }
    var rt = oXHR.responseText;
    if (rt.charAt(0) == '\r' && rt.charAt(1) == '\n')
        rt = rt.slice(2);
    rt = rt.split("|")
    if (rt[0] && cpat != rt) {
        cpat = rt;
        pat.style.background = rt[1] ? rt[0] : "transparent";  // Older dispwin compat
        document.body.style.background = (rt[1] || rt[0]);  // Older dispwin compat
        if (rt.length == 6) {
            pat.style.left = (rt[2] * 100) + "%";
            pat.style.top = (rt[3] * 100) + "%";
            pat.style.width = (rt[4] * 100) + "%";
            pat.style.height = (rt[5] * 100) + "%";
        }
    }
    setTimeout(XHR_request, 50);
}

window.onload = function() {
    pat = document.getElementById("pattern");

    oXHR = new XMLHttpRequest();
    XHR_request();
};
"""


class WebWinHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
    """Simple HTTP request handler with GET and HEAD commands."""

    server_version = appname + "-WebWinHTTP/" + appversion

    def do_GET(self):
        """Serve a GET request."""
        s = self.send_head()
        if s:
            self.wfile.write(s.encode())

    def do_HEAD(self):
        """Serve a HEAD request."""
        self.send_head()

    def log_message(self, format, *args):
        pass

    def send_head(self):
        """Common code for GET and HEAD commands.

        This sends the response code and MIME headers.

        Return value is either a string (which has to be written
        to the outputfile by the caller unless the command was HEAD), or
        None, in which case the caller has nothing further to do.

        """
        if self.path == "/":
            s = WEBDISP_HTML
            ctype = "text/html; charset=UTF-8"
        elif self.path == "/webdisp.js":
            s = WEBDISP_JS
            ctype = "application/javascript"
        elif self.path.startswith("/ajax/messages?"):
            curpat = "|".join(unquote(self.path.split("?").pop()).split("|")[:6])
            while (
                self.server.patterngenerator.listening
                and self.server.patterngenerator.pattern == curpat
            ):
                time.sleep(0.05)
            s = self.server.patterngenerator.pattern
            ctype = "text/plain; charset=UTF-8"
        else:
            self.send_error(404)
            return
        if self.server.patterngenerator.listening:
            try:
                self.send_response(200)
                self.send_header("Cache-Control", "no-cache")
                self.send_header("Content-Type", ctype)
                self.end_headers()
                return s
            except BaseException:
                pass