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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
|
/*
* Argyll Color Correction System
* Web Display target patch window
*
* Author: Graeme W. Gill
* Date: 3/4/12
*
* Copyright 2013 Graeme W. Gill
* All rights reserved.
*
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
* see the License.txt file for licencing details.
*/
#include <stdio.h>
#include <string.h>
#include "copyright.h"
#include "aconfig.h"
#include "icc.h"
#include "numsup.h"
#include "cgats.h"
#include "conv.h"
#include "dispwin.h"
#include "webwin.h"
#include "conv.h"
#include "mongoose.h"
#undef DEBUG
//#define STANDALONE_TEST
#ifdef DEBUG
#define errout stderr
# define debug(xx) fprintf(errout, xx )
# define debug2(xx) fprintf xx
# define debugr(xx) fprintf(errout, xx )
# define debugr2(xx) fprintf xx
# define debugrr(xx) fprintf(errout, xx )
# define debugrr2(xx) fprintf xx
# define debugrr2l(lev, xx) fprintf xx
#else
#define errout stderr
# define debug(xx)
# define debug2(xx)
# define debugr(xx) if (p->ddebug) fprintf(errout, xx )
# define debugr2(xx) if (p->ddebug) fprintf xx
# define debugrr(xx) if (callback_ddebug) fprintf(errout, xx )
# define debugrr2(xx) if (callback_ddebug) fprintf xx
# define debugrr2l(lev, xx) if (callback_ddebug >= lev) fprintf xx
#endif
// A handler for the /ajax/get_messages endpoint.
// Return a list of messages with ID greater than requested.
static void ajax_get_messages(struct mg_connection *conn,
const struct mg_request_info *request_info) {
char src_addr[20];
// dispwin *p = (dispwin *)(request_info->user_data);
dispwin *p = (dispwin *)mg_get_user_data(conn);
//sockaddr_to_string(src_addr, sizeof(src_addr), &conn->client.rsa);
// printf("ajax_messages query_string '%s'\n",request_info->query_string);
p->ccix++;
while(p->ncix == p->ccix && p->mg_stop == 0) {
msec_sleep(50);
}
mg_printf(conn,
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n\r\n"
"#%02X%02X%02X",
(int)(p->r_rgb[0] * 255.0 + 0.5),
(int)(p->r_rgb[1] * 255.0 + 0.5),
(int)(p->r_rgb[2] * 255.0 + 0.5));
}
/* Event handler */
static void *webwin_ehandler(enum mg_event event,
struct mg_connection *conn) {
// const struct mg_request_info *request_info) {
const struct mg_request_info *request_info = mg_get_request_info(conn);
if (event != MG_NEW_REQUEST) {
return NULL;
}
#ifdef DEBUG
printf("Got event with uri = '%s'\n",request_info->uri);
#endif
if (strcmp(request_info->uri, "/ajax/messages") == 0) {
ajax_get_messages(conn, request_info);
} else if (strcmp(request_info->uri, "/webdisp.js") == 0) {
#ifndef NEVER
char *webdisp_js =
"HTTP/1.1 200 OK\r\n"
"Content-Type: application/javascript\r\n\r\n"
"if (typeof XMLHttpRequest == \"undefined\") {\r\n"
" XMLHttpRequest = function () {\r\n"
" try { return new ActiveXObject(\"Msxml2.XMLHTTP.6.0\"); }\r\n"
" catch (e) {}\r\n"
" try { return new ActiveXObject(\"Msxml2.XMLHTTP.3.0\"); }\r\n"
" catch (e) {}\r\n"
" try { return new ActiveXObject(\"Microsoft.XMLHTTP\"); }\r\n"
" catch (e) {}\r\n"
" throw new Error(\"This browser does not support XMLHttpRequest.\");\r\n"
" };\r\n"
"}\r\n"
"\r\n"
"var ccolor = \"\";\r\n"
"var oXHR;\r\n"
"\r\n"
"function XHR_response() {\r\n"
" if (oXHR.readyState != 4)\r\n"
" return;\r\n"
"\r\n"
" if (oXHR.status != 200) {\r\n"
" return;\r\n"
" }\r\n"
" var rt = oXHR.responseText;\r\n"
" if (rt.charAt(0) == '\\r' && rt.charAt(1) == '\\n')\r\n"
" rt = rt.slice(2);\r\n"
" if (ccolor != rt) {\r\n"
" ccolor = rt;\r\n"
" document.body.style.background = ccolor;\r\n"
" }\r\n"
" oXHR.open(\"GET\", \"/ajax/messages?\" + document.body.style.background + \" \" + Math.random(), true);\r\n"
" oXHR.onreadystatechange = XHR_response;\r\n"
" oXHR.send();\r\n"
"}\r\n"
"\r\n"
"window.onload = function() {\r\n"
" ccolor = \"#808080\";\r\n"
" document.body.style.background = ccolor;\r\n"
"\r\n"
" oXHR = new XMLHttpRequest();\r\n"
" oXHR.open(\"GET\", \"/ajax/messages?\" + document.body.style.background, true);\r\n"
" oXHR.onreadystatechange = XHR_response;\r\n"
" oXHR.send();\r\n"
"};\r\n";
mg_write(conn, webdisp_js, strlen(webdisp_js));
#else
return NULL; /* Read webdisp.js */
#endif
} else if (strcmp(request_info->uri, "/") == 0) {
mg_printf(conn, "HTTP/1.1 200 OK\r\n"
"Cache-Control: no-cache\r\n"
"Content-Type: text/html; charset=UTF-8\r\n\r\n"
"<!DOCTYPE html>\r\n"
"<html>\r\n"
"<head>\r\n"
"<title>ArgyllCMS Web Display</title>\r\n"
"<script src=\"webdisp.js\"></script>\r\n"
"</head>\r\n"
"<body>\r\n"
"</body>\r\n"
"</html>\r\n"
);
} else {
mg_printf(conn, "HTTP/1.1 404 Not Found\r\n"
"Content-Type: text/plain\r\n\r\n"
"404 Not Found: %s",
request_info->uri
);
}
// "<script type=\"text/javascript\"src=\"webdisp.js\"></script>"
return "yes";
}
/* ----------------------------------------------- */
/* Get RAMDAC values. ->del() when finished. */
/* Return NULL if not possible */
static ramdac *webwin_get_ramdac(dispwin *p) {
debugr("webdisp doesn't have a RAMDAC\n");
return NULL;
}
/* Set the RAMDAC values. */
/* Return nz if not possible */
static int webwin_set_ramdac(dispwin *p, ramdac *r, int persist) {
debugr("webdisp doesn't have a RAMDAC\n");
return 1;
}
/* ----------------------------------------------- */
/* Install a display profile and make */
/* it the default for this display. */
/* Return nz if failed */
int webwin_install_profile(dispwin *p, char *fname, ramdac *r, p_scope scope) {
debugr("webdisp doesn't support installing profiles\n");
return 1;
}
/* Un-Install a display profile */
/* Return nz if failed, */
int webwin_uninstall_profile(dispwin *p, char *fname, p_scope scope) {
debugr("webdisp doesn't support uninstalling profiles\n");
return 1;
}
/* Get the currently installed display profile. */
/* Return NULL if failed. */
icmFile *webwin_get_profile(dispwin *p, char *name, int mxlen) {
debugr("webdisp doesn't support getting the current profile\n");
return NULL;
}
/* ----------------------------------------------- */
/* Change the window color. */
/* Return 1 on error, 2 on window being closed */
static int webwin_set_color(
dispwin *p,
double r, double g, double b /* Color values 0.0 - 1.0 */
) {
int j;
double orgb[3]; /* Previous RGB value */
double kr, kf;
int update_delay = 0;
debugr("webwin_set_color called\n");
if (p->nowin)
return 1;
orgb[0] = p->rgb[0]; p->rgb[0] = r;
orgb[1] = p->rgb[1]; p->rgb[1] = g;
orgb[2] = p->rgb[2]; p->rgb[2] = b;
for (j = 0; j < 3; j++) {
if (p->rgb[j] < 0.0)
p->rgb[j] = 0.0;
else if (p->rgb[j] > 1.0)
p->rgb[j] = 1.0;
p->r_rgb[j] = p->s_rgb[j] = p->rgb[j];
if (p->out_tvenc) {
p->r_rgb[j] = p->s_rgb[j] = ((235.0 - 16.0) * p->s_rgb[j] + 16.0)/255.0;
/* For video encoding the extra bits of precision are created by bit shifting */
/* rather than scaling, so we need to scale the fp value to account for this. */
if (p->edepth > 8)
p->r_rgb[j] = (p->s_rgb[j] * 255 * (1 << (p->edepth - 8)))
/((1 << p->edepth) - 1.0);
}
}
/* This is probably not actually thread safe... */
p->ncix++;
while(p->ncix != p->ccix) {
msec_sleep(50);
}
/* Allow for display update & instrument delays */
update_delay = dispwin_compute_delay(p, orgb);
debugr2((errout, "webwin_set_color delaying %d msec\n",update_delay));
msec_sleep(update_delay);
return 0;
}
/* Set/unset the full screen black flag */
/* Return nz on error */
static int webwin_set_fc(dispwin *p, int fullscreen) {
return 1; /* Setting black BG not supported */
}
/* ----------------------------------------------- */
/* Set the shell set color callout */
void webwin_set_callout(
dispwin *p,
char *callout
) {
debugr2((errout,"webwin_set_callout called with '%s'\n",callout));
p->callout = strdup(callout);
}
/* ----------------------------------------------- */
/* Destroy ourselves */
static void webwin_del(
dispwin *p
) {
debugr("webwin_del called\n");
if (p == NULL)
return;
p->mg_stop = 1;
mg_stop((struct mg_context *)p->pcntx);
if (p->name != NULL)
free(p->name);
if (p->description != NULL)
free(p->description);
if (p->callout != NULL)
free(p->callout);
free(p);
}
/* ----------------------------------------------- */
/* Create a web display test window, default grey */
dispwin *new_webwin(
int webdisp, /* Port number */
double width, double height, /* Width and height in mm */
double hoff, double voff, /* Offset from center in fraction of screen, range -1.0 .. 1.0 */
int nowin, /* NZ if no window should be created - RAMDAC access only */
int native, /* X0 = use current per channel calibration curve */
/* X1 = set native linear output and use ramdac high precn. */
/* 0X = use current color management cLut (MadVR) */
/* 1X = disable color management cLUT (MadVR) */
int *noramdac, /* Return nz if no ramdac access. native is set to X0 */
int *nocm, /* Return nz if no CM cLUT access. native is set to 0X */
int out_tvenc, /* 1 = use RGB Video Level encoding */
int fullscreen, /* NZ if whole screen should be filled with black */
int verb, /* NZ for verbose prompts */
int ddebug /* >0 to print debug statements to stderr */
) {
dispwin *p = NULL;
char *cp;
struct mg_context *mg;
const char *options[3];
char port[50];
char *url;
debug("new_webwin called\n");
if ((p = (dispwin *)calloc(sizeof(dispwin), 1)) == NULL) {
if (ddebug) fprintf(stderr,"new_webwin failed because malloc failed\n");
return NULL;
}
/* !!!! Make changes in dispwin.c & madvrwin.c as well !!!! */
p->name = strdup("Web Window");
p->width = width;
p->height = height;
p->nowin = nowin;
p->native = native;
p->out_tvenc = out_tvenc;
p->fullscreen = fullscreen;
p->ddebug = ddebug;
p->get_ramdac = webwin_get_ramdac;
p->set_ramdac = webwin_set_ramdac;
p->install_profile = webwin_install_profile;
p->uninstall_profile = webwin_uninstall_profile;
p->get_profile = webwin_get_profile;
p->set_color = webwin_set_color;
p->set_fc = webwin_set_fc;
p->set_update_delay = dispwin_set_update_delay;
p->set_settling_delay = dispwin_set_settling_delay;
p->enable_update_delay = dispwin_enable_update_delay;
p->set_callout = webwin_set_callout;
p->del = webwin_del;
if (noramdac != NULL)
*noramdac = 1;
p->native &= ~1;
if (nocm != NULL)
*nocm = 1;
p->native &= ~2;
p->rgb[0] = p->rgb[1] = p->rgb[2] = 0.5; /* Set Grey as the initial test color */
dispwin_set_default_delays(p);
p->ncix = 1;
p->fdepth = 8; /* Assume this by API */
p->rdepth = p->fdepth; /* Assumed */
p->ndepth = p->rdepth; /* Assumed */
p->nent = 0; /* No ramdac */
p->edepth = 8; /* Assumed */
/* Basic object is initialised, so create a web server */
options[0] = "listening_ports";
sprintf(port,"%d", webdisp);
options[1] = port;
options[2] = NULL;
mg = mg_start(&webwin_ehandler, (void *)p, options);
p->pcntx = (void *)mg;
//printf("Domain = %s'\n",mg_get_option(mg, "authentication_domain"));
/* Create a suitable description/url */
{
char buf[100], *url;
if ((url = mg_get_url(mg)) == NULL)
error("Failed to get Web server URL");
sprintf(buf,"Web Window at '%s'",url);
p->description = strdup(buf);
printf("Created web server at '%s', now waiting for browser to connect\n",url);
free(url);
}
/* Wait for the web server to connect */
debugr("new_webwin: waiting for web browser to connect\n");
while(p->ccix == 0) {
msec_sleep(50);
}
debugr("new_webwin: return successfully\n");
return p;
}
|