/* Thread to monitor and kill the named processes */ static int th_kkill_nprocess(void *pp) { kkill_nproc_ctx *ctx = (kkill_nproc_ctx *)pp; /* set result to 0 if it ever suceeds or there was no such process */ ctx->th->result = -1; while(ctx->stop == 0) { if (kill_nprocess(ctx->pname, ctx->log) >= 0) ctx->th->result = 0; msec_sleep(20); /* Don't hog the CPU */ } ctx->done = 1; return 0; }
/* Delayed beep handler */ static int delayed_beep(void *pp) { msec_sleep(beep_delay); a1logd(g_log,8, "msec_beep activate\n"); #ifdef __APPLE__ # if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 AudioServicesPlayAlertSound(kUserPreferredAlert); # else SysBeep((beep_msec * 60)/1000); # endif #else /* UNIX */ /* Linux is pretty lame in this regard... */ fprintf(stdout, "\a"); fflush(stdout); #endif return 0; }
static void kkill_nprocess_del(kkill_nproc_ctx *p) { int i; p->stop = 1; DBG("kkill_nprocess del called\n"); for (i = 0; p->done == 0 && i < 100; i++) { msec_sleep(50); } if (p->done == 0) { /* Hmm */ a1logw(p->log,"kkill_nprocess del failed to stop - killing thread\n"); p->th->del(p->th); } del_a1log(p->log); free(p); DBG("kkill_nprocess del done\n"); }
/* Delayed beep handler */ static int delayed_beep(void *pp) { msec_sleep(beep_delay); a1logd(g_log,8, "msec_beep activate\n"); Beep(beep_freq, beep_msec); return 0; }
/* Return the instrument error code */ static inst_code dtp41_read_sample( inst *pp, char *name, /* Strip name (7 chars) */ ipatch *val, /* Pointer to instrument patch value */ instClamping clamp) { /* NZ if clamp XYZ/Lab to be +ve */ dtp41 *p = (dtp41 *)pp; char *tp; static char buf[MAX_RD_SIZE]; int i, se; inst_code ev = inst_ok; int switch_trig = 0; int user_trig = 0; if (!p->gotcoms) return inst_no_coms; if (!p->inited) return inst_no_init; /* Configure for static mode */ p->lastmode = (p->lastmode & ~inst_mode_sub_mask) | inst_mode_spot; activate_mode(p); /* Set static measurement mode */ if ((ev = dtp41_command(p, "0013CF\r", buf, MAX_MES_SIZE, 1.5)) != inst_ok) return ev; if (p->trig == inst_opt_trig_user_switch) { /* Wait for the Read status, or a user trigger/abort */ for (;;) { if ((ev = dtp41_command(p, "", buf, MAX_MES_SIZE, 0.5)) != inst_ok) { if ((ev & inst_mask) == inst_needs_cal) p->need_cal = 1; if ((ev & inst_imask) != DTP41_TIMEOUT) return ev; /* Instrument or comms error */ /* Timed out */ if (p->uicallback != NULL) { /* Check for user trigger */ if ((ev = p->uicallback(p->uic_cntx, inst_armed)) != inst_ok) { if (ev == inst_user_abort) return ev; /* User abort */ if (ev == inst_user_trig) { user_trig = 1; break; /* User trigger */ } } } } else { /* Assume read status and trigger */ switch_trig = 1; break; /* Switch activated */ } } /* Notify of trigger */ if (p->uicallback) p->uicallback(p->uic_cntx, inst_triggered); } else if (p->trig == inst_opt_trig_user) { if (p->uicallback == NULL) { a1logd(p->log, 1, "dtp41: inst_opt_trig_user but no uicallback function set!\n"); return inst_unsupported; } for (;;) { if ((ev = p->uicallback(p->uic_cntx, inst_armed)) != inst_ok) { if (ev == inst_user_abort) return ev; /* Abort */ if (ev == inst_user_trig) { user_trig = 1; break; /* Trigger */ } } msec_sleep(200); } /* Notify of trigger */ if (p->uicallback) p->uicallback(p->uic_cntx, inst_triggered); /* Progromatic Trigger */ } else { /* Check for abort */ if (p->uicallback != NULL && (ev = p->uicallback(p->uic_cntx, inst_armed)) == inst_user_abort) return ev; /* Abort */ } /* Trigger a read if the switch has not been used */ if (switch_trig == 0) { /* Do a read */ if ((ev = dtp41_command(p, "RM\r", buf, MAX_MES_SIZE, 20.0)) != inst_ok) { if ((ev & inst_mask) == inst_needs_cal) p->need_cal = 1; return ev; } } /* Gather the results in D50_2 XYZ */ if ((ev = dtp41_command(p, "0405TS\r", buf, MAX_RD_SIZE, 0.5)) != inst_ok) return ev; /* Strip misread */ /* Parse the buffer */ /* Replace '\r' with '\000' */ for (tp = buf; *tp != '\000'; tp++) { if (*tp == '\r') *tp = '\000'; } val->XYZ[0] = val->XYZ[1] = val->XYZ[2] = 0.0; /* for all the readings taken */ for (tp = buf, i = 0; i < p->nstaticr; i++) { double XYZ[3]; if (*tp == '\000') return inst_protocol_error; if (sscanf(tp, " %lf %lf %lf ", &XYZ[0], &XYZ[1], &XYZ[2]) != 3) { return inst_protocol_error; } val->XYZ[0] += XYZ[0]; val->XYZ[1] += XYZ[1]; val->XYZ[2] += XYZ[2]; tp += strlen(tp) + 1; } /* Average */ val->XYZ[0] /= (double)p->nstaticr; val->XYZ[1] /= (double)p->nstaticr; val->XYZ[2] /= (double)p->nstaticr; /* This may not change anything since instrument may clamp */ if (clamp) icmClamp3(val->XYZ, val->XYZ); val->loc[0] = '\000'; if ((p->mode & inst_mode_illum_mask) == inst_mode_transmission) val->mtype = inst_mrt_transmissive; else val->mtype = inst_mrt_reflective; val->XYZ_v = 1; val->sp.spec_n = 0; val->duration = 0.0; if (p->mode & inst_mode_spectral) { int j; /* Gather the results in Spectral reflectance */ if ((ev = dtp41_command(p, "0403TS\r", buf, MAX_RD_SIZE, 0.5)) != DTP41_OK) return ev; /* Strip misread */ /* Parse the buffer */ /* Replace '\r' with '\000' */ for (tp = buf; *tp != '\000'; tp++) { if (*tp == '\r') *tp = '\000'; } for (j = 0; j < 31; j++) val->sp.spec[j] = 0.0; /* Get each readings spetra */ for (tp = buf, i = 0; i < p->nstaticr; i++) { char *tpp; if (strlen(tp) < (31 * 8 - 1)) { return inst_protocol_error; } /* Read the spectral value */ for (tpp = tp, j = 0; j < 31; j++, tpp += 8) { char c; c = tpp[7]; tpp[7] = '\000'; val->sp.spec[j] += atof(tpp); tpp[7] = c; } tp += strlen(tp) + 1; } /* Average the result */ for (j = 0; j < 31; j++) val->sp.spec[j] /= (double)p->nstaticr; val->sp.spec_n = 31; val->sp.spec_wl_short = 400.0; val->sp.spec_wl_long = 700.0; val->sp.norm = 100.0; } /* Set back to dynamic measurement mode */ if ((ev = dtp41_command(p, "0113CF\r", buf, MAX_MES_SIZE, 1.5)) != inst_ok) return ev; if (user_trig) return inst_user_trig; return inst_ok; }
/* Return the instrument error code */ static inst_code dtp41_read_strip( inst *pp, char *name, /* Strip name (7 chars) */ int npatch, /* Number of patches in the pass */ char *pname, /* Pass name (3 chars) */ int sguide, /* Guide number */ double pwid, /* Patch length in mm (For DTP41) */ double gwid, /* Gap length in mm (For DTP41) */ double twid, /* Trailer length in mm (For DTP41T) */ ipatch *vals) { /* Pointer to array of instrument patch values */ dtp41 *p = (dtp41 *)pp; char tbuf[200], *tp; static char buf[MAX_RD_SIZE]; int i, se; inst_code ev = inst_ok; int switch_trig = 0; int user_trig = 0; if (!p->gotcoms) return inst_no_coms; if (!p->inited) return inst_no_init; /* Configure for dynamic mode */ p->lastmode = (p->lastmode & ~inst_mode_sub_mask) | inst_mode_strip; activate_mode(p); build_strip(p, tbuf, name, npatch, pname, sguide, pwid, gwid, twid); /* Send strip definition */ if ((ev = dtp41_command(p, tbuf, buf, MAX_MES_SIZE, 1.5)) != inst_ok) return ev; if (p->trig == inst_opt_trig_user_switch) { /* Wait for the Read status, or a user trigger/abort */ for (;;) { if ((ev = dtp41_command(p, "", buf, MAX_MES_SIZE, 0.5)) != inst_ok) { if ((ev & inst_mask) == inst_needs_cal) p->need_cal = 1; if ((ev & inst_imask) != DTP41_TIMEOUT) return ev; /* Instrument or comms error */ /* Timed out */ if (p->uicallback != NULL) { /* Check for user trigger */ if ((ev = p->uicallback(p->uic_cntx, inst_armed)) != inst_ok) { if (ev == inst_user_abort) return ev; /* User abort */ if (ev == inst_user_trig) { user_trig = 1; break; } } } } else { /* Got read status - assume triggered */ switch_trig = 1; break; /* Switch activated */ } } /* Notify of trigger */ if (p->uicallback) p->uicallback(p->uic_cntx, inst_triggered); } else if (p->trig == inst_opt_trig_user) { if (p->uicallback == NULL) { a1logd(p->log, 1, "dtp41: inst_opt_trig_user but no uicallback function set!\n"); return inst_unsupported; } for (;;) { if ((ev = p->uicallback(p->uic_cntx, inst_armed)) != inst_ok) { if (ev == inst_user_abort) return ev; /* Abort */ if (ev == inst_user_trig) { user_trig = 1; break; /* Trigger */ } } msec_sleep(200); } /* Notify of trigger */ if (p->uicallback) p->uicallback(p->uic_cntx, inst_triggered); /* Progromatic Trigger */ } else { /* Check for abort */ if (p->uicallback != NULL && (ev = p->uicallback(p->uic_cntx, inst_armed)) == inst_user_abort) return ev; /* Abort */ } /* Trigger a read if the switch has not been used */ if (switch_trig == 0) { /* Do a strip read */ if ((ev = dtp41_command(p, "RM\r", buf, MAX_MES_SIZE, 30.0)) != inst_ok) { if ((ev & inst_mask) == inst_needs_cal) p->need_cal = 1; return ev; } } /* Gather the results in D50_2 XYZ */ if ((ev = dtp41_command(p, "0405TS\r", buf, MAX_RD_SIZE, 0.5 + npatch * 0.1)) != inst_ok) return ev; /* Strip misread */ /* Parse the buffer */ /* Replace '\r' with '\000' */ for (tp = buf; *tp != '\000'; tp++) { if (*tp == '\r') *tp = '\000'; } for (tp = buf, i = 0; i < npatch; i++) { if (*tp == '\000') return inst_protocol_error; if (sscanf(tp, " %lf %lf %lf ", &vals[i].XYZ[0], &vals[i].XYZ[1], &vals[i].XYZ[2]) != 3) { if (sscanf(tp, " %lf %lf %lf ", &vals[i].XYZ[0], &vals[i].XYZ[1], &vals[i].XYZ[2]) != 3) { return inst_protocol_error; } } vals[i].loc[0] = '\000'; if ((p->mode & inst_mode_illum_mask) == inst_mode_transmission) vals[i].mtype = inst_mrt_transmissive; else vals[i].mtype = inst_mrt_reflective; vals[i].XYZ_v = 1; vals[i].sp.spec_n = 0; vals[i].duration = 0.0; tp += strlen(tp) + 1; } if (p->mode & inst_mode_spectral) { /* Gather the results in Spectral reflectance */ if ((ev = dtp41_command(p, "0403TS\r", buf, MAX_RD_SIZE, 0.5 + npatch * 0.1)) != inst_ok) return ev; /* Strip misread */ /* Parse the buffer */ /* Replace '\r' with '\000' */ for (tp = buf; *tp != '\000'; tp++) { if (*tp == '\r') *tp = '\000'; } /* Get each patches spetra */ for (tp = buf, i = 0; i < npatch; i++) { int j; char *tpp; if (strlen(tp) < (31 * 8 - 1)) { return inst_protocol_error; } /* Read the spectral value */ for (tpp = tp, j = 0; j < 31; j++, tpp += 8) { char c; c = tpp[7]; tpp[7] = '\000'; vals[i].sp.spec[j] = atof(tpp); tpp[7] = c; } vals[i].sp.spec_n = 31; vals[i].sp.spec_wl_short = 400.0; vals[i].sp.spec_wl_long = 700.0; vals[i].sp.norm = 100.0; tp += strlen(tp) + 1; } } if (user_trig) return inst_user_trig; return inst_ok; }
void msec_sleep_tv(struct timeval *tv) { msec_sleep(tv->tv_sec * 1000.0 + tv->tv_usec / 1000.0); }
/* inst_license, inst_licensenc or inst_tamper on licening problem */ static int ccwin_set_color( dispwin *p, double r, double g, double b /* Color values 0.0 - 1.0 */ ) { chws *ws = (chws *)p->pcntx; int j; double orgb[3]; /* Previous RGB value */ double kr, kf; int update_delay = 0; debugr2((errout, "ccwin_set_color called with %f %f %f\n",r,g,b)); if (p->nowin) { debugr2((errout,"ccwin_set_color: nowin - give up\n")); 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->pdepth > 8) p->r_rgb[j] = (p->s_rgb[j] * 255 * (1 << (p->pdepth - 8))) /((1 << p->pdepth) - 1.0); } } /* This is probably not actually thread safe... */ p->ncix++; #if DDITHER != 1 # pragma message("############################# ccwin.c DDITHER != 1 ##") #endif /* Turn the color into a png file */ { /* We want a raster of IWIDTH x IHEIGHT pixels for web server, */ /* or p->w x p->h for PNG direct. */ render2d *rr; prim2d *rct; depth2d depth = bpc8_2d; #if DDITHER == 1 int dither = 0x8002; /* 0x8002 = error diffuse FG only */ #elif DDITHER == 2 int dither = 0x4000; /* 0x4000 = no dither but don't average pixels */ /* so as to allow pattern to come through. */ #else int dither = 0; /* Don't dither in renderer */ #endif double hres = 1.0; /* Resoltion in pix/mm */ double vres = 1.0; /* Resoltion in pix/mm */ double iw, ih; /* Size of page in mm (pixels) */ color2d c; unsigned char *ibuf; /* Memory image of .png file */ size_t ilen; int rv; #ifdef DO_TIMING int stime; #endif if (ws->direct) { iw = ws->w; /* Requested size */ ih = ws->h; } else { iw = IWIDTH; ih = IHEIGHT; /* Size of page in mm */ } /* Full screen background: */ if (p->fullscreen) { if (p->bge == dw_bg_grey) { ws->bg[0] = 0.2; ws->bg[1] = 0.2; ws->bg[2] = 0.2; } else if (p->bge == dw_bg_cvideo) { ws->bg[0] = p->area * (1.0 - r)/(1.0 - p->area); ws->bg[1] = p->area * (1.0 - g)/(1.0 - p->area); ws->bg[2] = p->area * (1.0 - b)/(1.0 - p->area); } else if (p->bge == dw_bg_clight) { double gamma = 2.3; ws->bg[0] = pow(p->area * (1.0 - pow(r, gamma))/(1.0 - p->area), 1.0/gamma); ws->bg[1] = pow(p->area * (1.0 - pow(g, gamma))/(1.0 - p->area), 1.0/gamma); ws->bg[2] = pow(p->area * (1.0 - pow(b, gamma))/(1.0 - p->area), 1.0/gamma); } else { /* Assume dw_bg_black */ ws->bg[0] = 0.0; ws->bg[1] = 0.0; ws->bg[2] = 0.0; } /* Use default dark gray background */ } else { ws->bg[0] = 0.2; ws->bg[1] = 0.2; ws->bg[2] = 0.2; } debugr2((errout, "ccwin_set_color iw %f ih %f\n",iw,ih)); if ((rr = new_render2d(iw, ih, NULL, hres, vres, rgb_2d, 0, depth, dither, #if DDITHER == 1 ccastQuant, NULL, 3.0/255.0 #else NULL, NULL, 0.0 #endif )) == NULL) { a1loge(g_log, 1,"ccwin: new_render2d() failed\n"); return 1; } /* Set the background color */ c[0] = ws->bg[0]; c[1] = ws->bg[1]; c[2] = ws->bg[2]; rr->set_defc(rr, c); c[0] = p->r_rgb[0]; c[1] = p->r_rgb[1]; c[2] = p->r_rgb[2]; if (ws->direct) rr->add(rr, rct = new_rect2d(rr, 0.0, 0.0, ws->w, ws->h, c)); else rr->add(rr, rct = new_rect2d(rr, ws->x, ws->y, ws->w, ws->h, c)); #if DDITHER == 2 /* Use dither pattern */ { double rgb[3]; double dpat[CCDITHSIZE][CCDITHSIZE][3]; double (*cpat)[MXPATSIZE][MXPATSIZE][TOTC2D]; int i, j; /* Get a chrome cast dither pattern to match target color */ for (i = 0; i < 3; i++) rgb[i] = p->r_rgb[i] * 255.0; get_ccast_dith(dpat, rgb); if ((cpat = malloc(sizeof(double) * MXPATSIZE * MXPATSIZE * TOTC2D)) == NULL) { a1loge(g_log, 1, "ccwin: malloc of dither pattern failed\n"); return 1; } for (i = 0; i < CCDITHSIZE; i++) { for (j = 0; j < CCDITHSIZE; j++) { int k = (((int)IHEIGHT-2) - j) % CCDITHSIZE; /* Flip to origin bot left */ (*cpat)[i][k][0] = dpat[i][j][0]/255.0; /* (HEIGHT-2 is correct!) */ (*cpat)[i][k][1] = dpat[i][j][1]/255.0; (*cpat)[i][k][2] = dpat[i][j][2]/255.0; } } set_rect2d_dpat((rect2d *)rct, cpat, CCDITHSIZE, CCDITHSIZE); } #endif /* DDITHER == 2 */ #ifdef CCTEST_PATTERN #pragma message("############################# ccwin.c TEST_PATTERN defined ! ##") if (getenv("ARGYLL_CCAST_TEST_PATTERN") != NULL) { verbose(0, "Writing test pattern to '%s'\n","testpattern.png"); if (r->write(r, "testpattern.png", 1, NULL, NULL, png_file)) { a1loge(g_log, 1, "ccwin: render->write failed\n"); return 1; } } #else /* !CCTEST_PATTERN */ # ifdef WRITE_PNG /* Write it to a file so that we can look at it */ # pragma message("############################# spectro/ccwin.c WRITE_PNG is enabled ######") if (r->write(rr, "ccwin.png", 1, NULL, NULL, png_file)) { a1loge(g_log, 1, "ccwin: render->write failed\n"); return 1; } # endif /* WRITE_PNG */ #endif /* !CCTEST_PATTERN */ #ifdef DO_TIMING stime = msec_time(); #endif rv = rr->write(rr, "MemoryBuf", 1, &ibuf, &ilen, png_mem); if (rv) { a1loge(g_log, 1, "ccwin: render->write failed\n"); return 1; } rr->del(rr); #ifdef DO_TIMING stime = msec_time() - stime; printf("render->write took %d msec\n",stime); #endif if (ws->update(ws, ibuf, ilen, ws->bg)) { a1loge(g_log, 1, "ccwin: color update failed\n"); return 1; } p->ccix = p->ncix; } /* If update is notified asyncronously ... */ while(p->ncix != p->ccix) { msec_sleep(50); } //printf("#################################################################\n"); //printf("################# RGB update notified ################\n"); //printf("#################################################################\n"); /* Allow for display update & instrument delays */ update_delay = dispwin_compute_delay(p, orgb); debugr2((errout, "ccwin_set_color delaying %d msec\n",update_delay)); msec_sleep(update_delay); return 0; }
static inline int msec_sleep_tv(struct timeval *tv) { msec_sleep(tv->tv_sec * 1000.0 + tv->tv_usec / 1000.0); return 0; }