#include #include #include "lx.h" #include "proto.h" #include "internal.h" typedef struct priv_QueryColors PRIV_QueryColors; struct priv_QueryColors { unsigned char *req; int n; LX_RGB *rgbp; } ; static void done_QueryColors(LX_OP *op, const unsigned char *rep) { PRIV_QueryColors *p; int i; p = op->reqpriv; do <"ret"> { if (r_card32(rep+4) != 2*p->n) { lx__protoerr(op->conn,"QueryColors response length wrong (%u, expecting %d)",r_card32(rep+4),2*p->n); break <"ret">; } if (r_card16(rep+8) != p->n) { lx__protoerr(op->conn,"QueryColors response color count wrong (%u, expecting %d)",r_card16(rep+8),p->n); break <"ret">; } for (i=p->n-1;i>=0;i--) { p->rgbp[i] = (LX_RGB){ .r = r_card16(rep+32+(8*i)), .g = r_card16(rep+32+(8*i)+2), .b = r_card16(rep+32+(8*i)+4) }; } } while (0); op->reqpriv = 0; free(p->req); free(p); } LX_OP *lx_QueryColors(LX_CONN *xc, LX_XID cmap, int npix, const unsigned int *pixv, LX_RGB *rgbp) { unsigned char *req; PRIV_QueryColors *p; int i; if ((xc->flags & XCF_FAIL) || (npix < 0) || (2+npix > 65535)) { lx__bad_call(xc,"lx_QueryColors"); return(0); } req = malloc(8+(4*npix)); if (! req) { lx__nomem_fail(xc); return(0); } p = malloc(sizeof(PRIV_QueryColors)); if (! p) { free(req); lx__nomem_fail(xc); return(0); } p->req = req; p->n = npix; p->rgbp = rgbp; req[0] = XP_REQ_QueryColors; req[1] = 0; w_card16(req+2,2+npix); w_card32(req+4,cmap); for (i=npix-1;i>=0;i--) w_card32(req+8+(4*i),pixv[i]); return(lx__expect_reply(xc,req,2+npix,&done_QueryColors,p,0)); }