Exemplo n.º 1
0
ETERM *body_set_collision_circle(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);
    ETERM *radiusp = erl_element(3, argp);
    ETERM *collision_typep = erl_element(4, argp);

    erlmunk_space *s;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    cpShape *shape = cpSpaceAddShape(s->space,
                                     cpCircleShapeNew(b->body, ERL_FLOAT_VALUE(radiusp),
                                                      cpvzero));
    cpShapeSetCollisionType(shape, ERL_INT_VALUE(collision_typep));

    // DEBUGF(("body_set_collision_circle has succeeded"));
    return NULL;
}
Exemplo n.º 2
0
Arquivo: hello.c Projeto: DBarney/efs
static int hello_getattr(const char *path, struct stat *stbuf)
{
	ETERM * response = erl_rpc(FERL_DATA->erlang_fd,"gen_server","call",erl_format("[nefs,{get_attr,~s}]",path));
	ETERM * pattern = erl_format("{directory,Mode,Nlink}");
	ETERM * pattern2 = erl_format("{file,Mode,Nlink,Size}");
        int res = 0;
	memset(stbuf, 0, sizeof(struct stat));

	if(erl_match(pattern, response)) { //directory
		ETERM * Mode = erl_var_content(pattern, "Mode");
		ETERM * Nlink = erl_var_content(pattern, "Nlink");
		if(ERL_IS_INTEGER(Nlink) && ERL_IS_INTEGER(Mode)){
			stbuf->st_mode = S_IFDIR | ERL_INT_VALUE(Mode); //permissions
        	        stbuf->st_nlink = ERL_INT_VALUE(Nlink); // directories have the number of files in them
		}else{
			res = -ENOENT;
		}
	}else if(erl_match(pattern2, response)){ //file
		ETERM * Mode = erl_var_content(pattern2, "Mode");
                ETERM * Nlink = erl_var_content(pattern2, "Nlink");
		ETERM * Size = erl_var_content(pattern2, "Size");
                if(ERL_IS_INTEGER(Nlink) && ERL_IS_INTEGER(Mode) && ERL_IS_INTEGER(Size)){

			stbuf->st_mode = S_IFREG | ERL_INT_VALUE(Mode); //permissions
			stbuf->st_nlink = ERL_INT_VALUE(Nlink); //files only have 1
			stbuf->st_size = ERL_INT_VALUE(Size); // length of the file
                }else{
                        res = -ENOENT;
                }
	}else{
		res = -ENOENT;
	}	

	return res;
}
Exemplo n.º 3
0
int main() {
  ETERM *tuplep, *intp;
  ETERM *fnp, *argp;
  int res;
  byte buf[100];
  long allocated, freed;

  erl_init(NULL, 0);

  while (read_cmd(buf) > 0) {
    tuplep = erl_decode(buf);
    fnp    = erl_element(1, tuplep);
    argp   = erl_element(2, tuplep);
    
    if (strncmp(ERL_ATOM_PTR(fnp), "foo", 3) == 0) {
      res = foo(ERL_INT_VALUE(argp));
    } else if (strncmp(ERL_ATOM_PTR(fnp), "bar", 17) == 0) {
      res = bar(ERL_INT_VALUE(argp));
    }

    intp = erl_mk_int(res);
    erl_encode(intp, buf);
    write_cmd(buf, erl_term_len(intp));

    erl_free_compound(tuplep);
    erl_free_term(fnp);
    erl_free_term(argp);
    erl_free_term(intp);
  }
}
Exemplo n.º 4
0
ETERM *body_copy(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);
    ETERM *from_idp = erl_element(3, argp);

    erlmunk_space *s;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    int from_body_id = ERL_INT_VALUE(from_idp);
    erlmunk_body *from_b;
    HASH_FIND_INT(s->bodies, &from_body_id, from_b);

    // DEBUGF(("copying location from body #%d(%p) to #%d(%p)",
    //     from_body_id, from_b, body_id, b));

    // copy position and angle from the from body
    cpBodySetPosition(b->body, cpBodyGetPosition(from_b->body));
    cpBodySetAngle(b->body, cpBodyGetAngle(from_b->body));
    cpBodySetVelocity(b->body, cpBodyGetVelocity(from_b->body));

    return NULL;
}
Exemplo n.º 5
0
static int is_printable_list(const ETERM* term)
{
    while (ERL_TYPE(term) == ERL_LIST) {
	ETERM* head = HEAD(term);

	if (!ERL_IS_BYTE(head)) {
	    return 0;
	}
	if (ERL_INT_VALUE(head) < ' ') {
	    switch (ERL_INT_VALUE(head)) {
	    case '\n':
	    case '\r':
	    case '\t':
	    case '\v':
	    case '\b':
	    case '\f':
		break;
	    default:
		return 0;
	    }
	}
	term = TAIL(term);
    }

    return ERL_IS_EMPTY_LIST(term);
}
Exemplo n.º 6
0
ETERM * new_image_blank(ETERM *arg, int c_node) { 
    int stride, cbufsize, status, key_length;
    ETERM *width, *height;
    cairo_context *ctx = NULL;             
    width = erl_element(1, arg);
    height = erl_element(2, arg);
    stride = ERL_INT_VALUE(width) * 4;
    cbufsize = ERL_INT_VALUE(height) * stride;
    ctx = malloc(sizeof(cairo_context));
    if (ctx) {
        ctx->cbuf = (byte *)malloc(cbufsize);
        if (ctx->cbuf) {
            memset(ctx->cbuf, 0, cbufsize);
            ctx->sf = cairo_image_surface_create_for_data(ctx->cbuf, 
                    CAIRO_FORMAT_ARGB32, ERL_INT_VALUE(width), 
                    ERL_INT_VALUE(height), stride);
            ctx->cr = cairo_create(ctx->sf);
            return erl_format("{c_node, ~i, {ok, ~i}}", c_node, ctx);
        } else {
            free(ctx);
            return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT);
        }
    } else {
        return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT);
    }
    erl_free_term(width);
    erl_free_term(height);
}
Exemplo n.º 7
0
int main(int argc, char **argv) {
  int fd;                                  /* fd to Erlang node */
  
  int loop = 1;                            /* Loop flag */
  int got;                                 /* Result of receive */
  unsigned char buf[BUFSIZE];              /* Buffer for incoming message */
  ErlMessage emsg;                         /* Incoming message */
  
  ETERM *fromp, *tuplep, *fnp, *argp, *resp;
  int res;
  
  erl_init(NULL, 0);

  if (erl_connect_init(1, "secretcookie", 0) == -1)
    erl_err_quit("erl_connect_init");
  
  if ((fd = erl_connect("e1@hostname")) < 0)
    erl_err_quit("erl_connect");
  fprintf(stderr, "Connected to e1@hostname\n\r");
  
  while (loop) {

    got = erl_receive_msg(fd, buf, BUFSIZE, &emsg);
    if (got == ERL_TICK) {
      /* ignore */
    } else if (got == ERL_ERROR) {
      loop = 0;
    } else {
      
      if (emsg.type == ERL_REG_SEND) {
	fromp = erl_element(1, emsg.msg);
	tuplep = erl_element(2, emsg.msg);
	fnp = erl_element(1, tuplep);
	argp = erl_element(2, tuplep);
        
	if (strncmp(ERL_ATOM_PTR(fnp), "foo", 3) == 0) {
	  res = foo(ERL_INT_VALUE(argp));
	} else if (strncmp(ERL_ATOM_PTR(fnp), "bar", 3) == 0) {
	  res = bar(ERL_INT_VALUE(argp));
	}
        
	resp = erl_format("{cnode, ~i}", res);
	erl_send(fd, fromp, resp);
        
	erl_free_term(emsg.from); erl_free_term(emsg.msg);
	erl_free_term(fromp); erl_free_term(tuplep);
	erl_free_term(fnp); erl_free_term(argp);
	erl_free_term(resp);
      }
    }
  }
}
Exemplo n.º 8
0
ETERM * line_to(ETERM* arg, int c_node) {
    ETERM *x, *y;
    cairo_context *ctx = get_cairo_context(arg);
    if (ctx) {
        x = erl_element(2, arg);  
        y = erl_element(3, arg); 
        cairo_line_to(ctx->cr, ERL_INT_VALUE(x), ERL_INT_VALUE(y));
        erl_free_term(x);
        erl_free_term(y);
        return erl_format("{c_node, ~i, ok}", c_node);
    } else { 
        return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT);
    }
}
Exemplo n.º 9
0
ETERM *space_add_boundaries(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *lower_leftp = erl_element(2, argp);
    ETERM *lower_rightp = erl_element(3, argp);
    ETERM *upper_leftp = erl_element(4, argp);
    ETERM *upper_rightp = erl_element(5, argp);
    ETERM *collision_categoryp = erl_element(6, argp);
    ETERM *datap = erl_element(7, argp);

    erlmunk_space *s;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    cpVect lowerLeft = cpv(ERL_FLOAT_VALUE(erl_element(1, lower_leftp)),
                           ERL_FLOAT_VALUE(erl_element(2, lower_leftp)));
    cpVect lowerRight = cpv(ERL_FLOAT_VALUE(erl_element(1, lower_rightp)),
                            ERL_FLOAT_VALUE(erl_element(2, lower_rightp)));
    cpVect upperLeft = cpv(ERL_FLOAT_VALUE(erl_element(1, upper_leftp)),
                           ERL_FLOAT_VALUE(erl_element(2, upper_leftp)));
    cpVect upperRight = cpv(ERL_FLOAT_VALUE(erl_element(1, upper_rightp)),
                            ERL_FLOAT_VALUE(erl_element(2, upper_rightp)));

    // get the static body that comes with the space
    cpBody *static_body = cpSpaceGetStaticBody(s->space);
    erlmunk_body_data *data = malloc(sizeof(erlmunk_body_data));
    data->id = BOUNDARY_BODY_ID;
    data->term = erl_copy_term(datap);
    cpBodySetUserData(static_body, (cpDataPointer) data);

    // bottom
    cpShape *bottomBoundaryShape = cpSegmentShapeNew(static_body, lowerLeft, lowerRight, 0.0f);
    cpShapeSetCollisionType(bottomBoundaryShape, ERL_INT_VALUE(collision_categoryp));
    cpSpaceAddShape(s->space, bottomBoundaryShape);
    // top
    cpShape *topBoundaryShape = cpSegmentShapeNew(static_body, upperLeft, upperRight, 0.0f);
    cpShapeSetCollisionType(topBoundaryShape, ERL_INT_VALUE(collision_categoryp));
    cpSpaceAddShape(s->space, topBoundaryShape);
    // left
    cpShape *leftBoundaryShape = cpSegmentShapeNew(static_body, lowerLeft, upperLeft, 0.0f);
    cpShapeSetCollisionType(leftBoundaryShape, ERL_INT_VALUE(collision_categoryp));
    cpSpaceAddShape(s->space, leftBoundaryShape);
    // right
    cpShape *rightBoundaryShape = cpSegmentShapeNew(static_body, lowerRight, upperRight, 0.0f);
    cpShapeSetCollisionType(rightBoundaryShape, ERL_INT_VALUE(collision_categoryp));
    cpSpaceAddShape(s->space, rightBoundaryShape);

    return NULL;
}
Exemplo n.º 10
0
ETERM *space_add_body(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);
    ETERM *massp = erl_element(3, argp);
    // ETERM *inertiap = erl_element(4, argp);

    erlmunk_space *s;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int object_id = ERL_INT_VALUE(idp);

    cpBody *body = cpSpaceAddBody(s->space,
                                  cpBodyNew(ERL_FLOAT_VALUE(massp),
                                            INFINITY));
    // the body is created inactive, it is explicitly activated
    // when all it's values have been set.
    cpBodySleep(body);
    erlmunk_body_data *data = malloc(sizeof(erlmunk_body_data));
    data->id = object_id;
    data->term = NULL;
    cpBodySetUserData(body, (cpDataPointer) data);
    space_add_body_hash(s, object_id, body);

    return NULL;
}
Exemplo n.º 11
0
response_t set_queue_len(int len, ETERM *tuplep, q_data_t **q_data, thread_data_t *data) {

    int q_num, cq_idx;
    ETERM *arg;
    response_t r;

    if(erl_size(tuplep) > 2) {

        arg = erl_element(3, tuplep);
        q_num = ERL_INT_VALUE(arg);
        erl_free_term(arg);

        syslog(LOG_NOTICE,"[%d] q_num: %d, len: %d\n\r", data->idx, q_num, len);

        if((cq_idx = get_queue_idx(q_num, q_data)) >= 0) {

            if(nfq_set_queue_maxlen(q_data[cq_idx]->qh, len) >= 0) {
                r.cs = erl_mk_atom("ok");
                r.rsp = erl_mk_atom("ok");
            } else {
                r.cs = erl_mk_atom("error");
                r.rsp = erl_mk_estring("failed to set queue max length", strlen("failed to set queue max length"));
            }

        } else {
            r.cs = erl_mk_atom("error");
            r.rsp = erl_mk_estring("no such queue", strlen("no such queue"));
        }

    } else {
        r.cs = erl_mk_atom("error");
        r.rsp = erl_mk_estring("argument missing", strlen("argument missing"));
    }
    return r;
}
Exemplo n.º 12
0
Arquivo: mllib.cpp Projeto: tjacek/ML2
att_type* get_atts_types(ETERM* term)
{
	lst point;
	int len = erl_length(term), i;
	att_type* res = (att_type*) malloc(len * sizeof(att_type));
	att_type tmp;
	next(term, &point);
	
	for (i=0; i<len; i++) 
	{
		switch (ERL_INT_VALUE(point.head)) 
		{
			case 0:
				tmp = nominal;
				break;

			case 1:
				tmp = continuous;
				break;
		}
		res[i] = tmp;
		next(term, &point);
	}

	return res;
}
Exemplo n.º 13
0
/**
* @brief Port interface for gpio_write
*/
int
port_gpio_write (int pin, ETERM *valuet)
{
   int value;
   value = ERL_INT_VALUE(valuet);
   return gpio_write(pin, value);
}
Exemplo n.º 14
0
ETERM * curve_to(ETERM* arg, int c_node) {
    ETERM *c1x, *c1y, *c2x, *c2y, *x, *y;
    cairo_context *ctx = get_cairo_context(arg);
    if (ctx) {
        c1x = erl_element(2, arg);  
        c1y = erl_element(3, arg); 
        c2x = erl_element(4, arg);  
        c2y = erl_element(5, arg);
        x = erl_element(6, arg);  
        y = erl_element(7, arg);
        cairo_curve_to(ctx->cr, ERL_INT_VALUE(c1x), 
            val(c1y),
            val(c2x),
            val(c2y),
            val(x),
            val(y));
        erl_free_term(c1x);
        erl_free_term(c1y);
        erl_free_term(c2x);
        erl_free_term(c2y);
        erl_free_term(x);
        erl_free_term(y);
        return erl_format("{c_node, ~i, ok}", c_node);
    } else { 
        return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT);
    }
}
Exemplo n.º 15
0
ETERM *space_remove_body(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);

    erlmunk_space *s;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b = NULL;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    // DEBUGF(("removing body #%d\n", body_id));

    // remove the user data associated with the body
    erlmunk_body_data *data = cpBodyGetUserData(b->body);
    if (data->term != NULL)
        erl_free_compound(data->term);
    free(data);

    cpBodyEachShape(b->body, shapeRemove, NULL);
    cpSpaceRemoveBody(s->space, b->body);

    space_remove_body_hash(s, b);

    cpBodyDestroy(b->body);
    cpBodyFree(b->body);

    return NULL;
}
Exemplo n.º 16
0
static int process_command(byte *buf)
{
  int retval = 0;
  ETERM *pattern, *tuple, *cmd, *port, *data;
  pattern = erl_format("{Cmd, Port, Data}");
  tuple = erl_decode(buf);
  if (erl_match(pattern, tuple))
    {
      cmd = erl_var_content(pattern, "Cmd");
      port = erl_var_content(pattern, "Port");
      data = erl_var_content(pattern, "Data");
      switch (ERL_INT_VALUE(cmd))
	{
	case CMD_AUTH:
	  retval = process_auth(port, data);
	  break;
	case CMD_ACCT:
	  retval = process_acct(port, data);
	  break;
	};
      erl_free_term(cmd);
      erl_free_term(port);
      erl_free_term(data);
    }
  erl_free_term(pattern);
  erl_free_term(tuple);
  return retval;
}
Exemplo n.º 17
0
static void iolist_to_buf(const ETERM* term, char** bufp)
{
    char* dest = *bufp;

    while (ERL_IS_CONS(term)) {
	ETERM* obj = HEAD(term);

	if (ERL_IS_BYTE(obj)) {
	    *dest++ = ERL_INT_VALUE(obj);
	} else if (ERL_IS_CONS(obj)) {
	    iolist_to_buf(obj, &dest);
	} else if (ERL_IS_BINARY(obj)) {
	    memcpy(dest, ERL_BIN_PTR(obj), ERL_BIN_SIZE(obj));
	    dest += ERL_BIN_SIZE(obj);
	} else {
	    /*
	     * Types have been checked by caller.
	     */
	  if (!ERL_IS_EMPTY_LIST(obj)) return;
	  /* ASSERT(ERL_IS_EMPTY_LIST(obj)); */
	}
	term = TAIL(term);
    }
    if (ERL_IS_BINARY(term)) {
	memcpy(dest, ERL_BIN_PTR(term), ERL_BIN_SIZE(term));
	dest += ERL_BIN_SIZE(term);
    } else {
	/*
	 * Types have been checked by caller.
	 */
      if (!ERL_IS_EMPTY_LIST(term)) return;
      /* ASSERT(ERL_IS_EMPTY_LIST(term));*/
    }
    *bufp = dest;
}
Exemplo n.º 18
0
ETERM *erl_mk_estring(const char *s, int len)
{
    ETERM *ep;
    int i;

    if ((!s) || (len < 0)) return NULL;

    /*
     * ASSERT(s != NULL);
     * ASSERT(len >= 0);
     */

    ep = erl_mk_empty_list();
    for (i = len-1; i >= 0; i--) {
	ETERM* integer;
	ETERM* cons;

	integer = erl_alloc_eterm(ERL_INTEGER);
	ERL_COUNT(integer) = 1;
	ERL_INT_VALUE(integer) = (unsigned char)s[i];

	cons = erl_alloc_eterm(ERL_LIST);
	ERL_COUNT(cons) = 1;
	HEAD(cons) = integer;
	TAIL(cons) = ep;
	ep = cons;
    }
    return ep;
}
Exemplo n.º 19
0
static int print_string(FILE* fp, const ETERM* ep)
{
    int ch_written = 0; /* counter of written chars */
  
    putc('"', fp);
    ch_written++;
    while (ERL_IS_CONS(ep)) {
	int c = ERL_INT_VALUE(HEAD(ep));

	if (c >= ' ') {
	    putc(c, fp);
	    ch_written++;
	}
	else {
	    switch (c) {
	    case '\n': fputs("\\n", fp); ch_written += 2; break;
	    case '\r': fputs("\\r", fp); ch_written += 2; break;
	    case '\t': fputs("\\t", fp); ch_written += 2; break;
	    case '\v': fputs("\\v", fp); ch_written += 2; break;
	    case '\b': fputs("\\b", fp); ch_written += 2; break;
	    case '\f': fputs("\\f", fp); ch_written += 2; break;
		break;
	    default:
		ch_written += fprintf(fp, "\\%o", c);
		break;
	    }
	}
	ep = TAIL(ep);
    }
    putc('"', fp);
    ch_written++;
    return ch_written;
}
static int
process_command(unsigned char *buf)
{
  int retval = 0;
  ETERM *pattern, *tuple, *cmd, *port, *data;
  pattern = erl_format("{Cmd, Port, Data}");
  tuple = erl_decode(buf);
  if (erl_match(pattern, tuple)) {
    cmd = erl_var_content(pattern, "Cmd");
    port = erl_var_content(pattern, "Port");
    data = erl_var_content(pattern, "Data");
    switch (ERL_INT_VALUE(cmd)) {
    case CMD_SALT:
      retval = process_encode_salt(port, data);
      break;
    case CMD_HASHPW:
      retval = process_hashpw(port, data);
      break;
    };
    erl_free_term(cmd);
    erl_free_term(port);
    erl_free_term(data);
  }
  erl_free_term(pattern);
  erl_free_term(tuple);
  return retval;
}
Exemplo n.º 21
0
static VALUE erlix_uint_to_fix(VALUE self){
    ErlixTerm *euint;
    unsigned int l;
    Data_Get_Struct(self,ErlixTerm,euint);
    l=ERL_INT_VALUE(euint->term);
    return INT2FIX(l);
}
Exemplo n.º 22
0
ETERM *body_apply_impulse(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);
    ETERM *impulsep = erl_element(3, argp);

    erlmunk_space *s;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    // apply the impulse at the center of the body and along it's current angle
    float angle = deg_to_rad(cpBodyGetAngle(b->body));
    cpVect angleV = cpvforangle(angle);
    cpVect impulse = cpvmult(angleV, ERL_FLOAT_VALUE(impulsep));
    cpBodyApplyImpulseAtWorldPoint(b->body, impulse, angleV);

    return NULL;
}
Exemplo n.º 23
0
ETERM *body_get_user_data(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);

    erlmunk_space *s;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    erlmunk_body_data *data = cpBodyGetUserData(b->body);
    ETERM *datap = erl_copy_term(data->term);

    ETERM *atom_ok = erl_mk_atom("ok");
    ETERM **body_get_data_array = (ETERM **) malloc(sizeof(ETERM*) * 2);
    body_get_data_array[0] = atom_ok;
    body_get_data_array[1] = datap;
    ETERM *body_get_data_tuple = erl_mk_tuple(body_get_data_array, 2);
    free(body_get_data_array);

    ETERM *reply_tuple = erl_mk_reply(fromp, body_get_data_tuple);
    ETERM *gen_cast_tuple = erl_mk_gen_cast(reply_tuple);

    return gen_cast_tuple;
}
Exemplo n.º 24
0
ETERM *body_update_position(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);
    ETERM *deltap = erl_element(3, argp);

    erlmunk_space *s = NULL;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b = NULL;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    cpVect position = cpBodyGetPosition(b->body);
    float angle = deg_to_rad(cpBodyGetAngle(b->body));
    cpVect angleV = cpvforangle(angle);
    cpVect projection = cpvmult(angleV, ERL_FLOAT_VALUE(deltap));
    cpVect new_position = cpvadd(projection, position);
    cpBodySetPosition(b->body, new_position);

    // DEBUGF(("body_update_position(x: %f, y: %f, delta: %f) has succeeded (x: %f, y: %f)",
    //     position.x, position.y, ERL_FLOAT_VALUE(deltap),
    //     new_position.x, new_position.y));
    return NULL;
}
Exemplo n.º 25
0
ETERM *body_set_position(ETERM *fromp, ETERM *argp) {

    // get the args
    ETERM *space_refp = erl_element(1, argp);
    ETERM *idp = erl_element(2, argp);
    ETERM *vectorp = erl_element(3, argp);
    ETERM *xp = erl_element(1, vectorp);
    ETERM *yp = erl_element(2, vectorp);

    erlmunk_space *s = NULL;
    int space_id = ERL_REF_NUMBER(space_refp);
    HASH_FIND_INT(erlmunk_spaces, &space_id, s);

    int body_id = ERL_INT_VALUE(idp);
    erlmunk_body *b = NULL;
    HASH_FIND_INT(s->bodies, &body_id, b);
    if (b == NULL)
        return NULL;

    cpBodySetPosition(b->body, cpv(ERL_FLOAT_VALUE(xp),
                                   ERL_FLOAT_VALUE(yp)));

    // DEBUGF(("body_set_position(x: %f, y: %f) has succeeded",
    //     ERL_FLOAT_VALUE(xp), ERL_FLOAT_VALUE(yp)));
    return NULL;
}
Exemplo n.º 26
0
/*
 * Create an INTEGER. Depending on its value it 
 * may end up as a BigNum.
 */
ETERM *erl_mk_int (int i)
{
    ETERM *ep;

    ep = erl_alloc_eterm(ERL_INTEGER);
    ERL_COUNT(ep) = 1;
    ERL_INT_VALUE(ep) = i;
    return ep;
}
Exemplo n.º 27
0
double val(ETERM *arg) {
    if (ERL_IS_INTEGER(arg))
        return ERL_INT_VALUE(arg);
    else if (ERL_IS_FLOAT(arg))
        return ERL_FLOAT_VALUE(arg);
    else {
        exit(EXIT_FAILURE);
        return 0;
    }
}
Exemplo n.º 28
0
ETERM * set_line_width(ETERM* arg, int c_node) {
    ETERM *width;
    cairo_context *ctx = get_cairo_context(arg);
    if (ctx) {
        width = erl_element(2, arg);   
        cairo_set_line_width(ctx->cr, ERL_INT_VALUE(width));
        erl_free_term(width);
        return erl_format("{c_node, ~i, ok}", c_node);
    } else { 
        return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT);
    }
}
Exemplo n.º 29
0
ETERM * set_operator(ETERM* arg, int c_node) {
    ETERM *operator;
    cairo_context *ctx = get_cairo_context(arg);
    if (ctx) {
        operator = erl_element(2, arg);    
        cairo_set_operator(ctx->cr, ERL_INT_VALUE(operator));
        erl_free_term(operator);
        return erl_format("{c_node, ~i, ok}", c_node);
    } else { 
        return erl_format("{c_node, ~i, {error, '~s'}}", c_node, ERR_CONTEXT);
    }
}
Exemplo n.º 30
0
bool command::process(term & t)
{
	bool ret = false;
	term resp;
#ifdef PRINTCMD
	char * tmpbuf = print_term(command);
	REMOTE_LOG(DBG, "========================================\nCOMMAND : %s %s\n========================================\n",
		CMD_NAME_STR(ERL_INT_VALUE(cmd)),
		tmpbuf);
	delete tmpbuf;
#endif

	if(t.is_tuple() && t[1].is_integer()) {
        int cmd = t[1].v.i;
		resp.tuple();
		resp.add(t[0]);
		resp.insert().integer(cmd);
        if((t.length() - 1) != (size_t)CMD_ARGS_COUNT(cmd)) {
			term & _t = resp.insert().tuple();
	    	_t.insert().atom("error");
	    	_t.insert().atom("badarg");
	    	if(resp.is_undef())
                REMOTE_LOG(ERR, "ERROR badarg %s expected %d, got %d\n", CMD_NAME_STR(cmd)
                    , CMD_ARGS_COUNT(cmd), (t.length() - 1));
	        if(resp.is_undef()) REMOTE_LOG(CRT, "driver error: no resp generated, shutting down port\n");
            vector<unsigned char> respv = tc.encode(resp);
            p.write_cmd(respv);
	    } else {
		    switch(cmd) {
            case RMOTE_MSG:	ret = change_log_flag(t, resp);	break;
            case GET_SESSN:	ret = get_session(t, resp);		break;
            case PUT_SESSN:	ret = release_conn(t, resp);	break;
		    case CMT_SESSN:	ret = commit(t, resp);			break;
            case RBK_SESSN:	ret = rollback(t, resp);		break;
            case CMD_DSCRB:	ret = describe(t, resp);		break;
            case PREP_STMT:	ret = prep_sql(t, resp);		break;
            case BIND_ARGS:	ret = bind_args(t, resp);		break;
            case EXEC_STMT:	ret = exec_stmt(t, resp);		break;
            case FTCH_ROWS:	ret = fetch_rows(t, resp);		break;
            case CLSE_STMT:	ret = close_stmt(t, resp);		break;
            case GET_LOBDA:	ret = get_lob_data(t, resp);	break;
            case CMD_ECHOT:	ret = echo(t, resp);			break;
		    case SESN_PING:	ret = ping(t, resp);			break;
            default:
		    	ret = true;
                break;
            }
        }
    }

	return ret;
}