Example #1
0
// Scan input for newly-pressed keys.
// Return value:
// 0  = no key was pressed
// >0 = key code for pressed key
// <0 = error
int control_scankey(){
	static unsigned ready = 0;
	unsigned k=0;
	unsigned port0=xbox_get_playerinput(0);
	unsigned port1=xbox_get_playerinput(1);
	unsigned port2=xbox_get_playerinput(2);
	unsigned port3=xbox_get_playerinput(3);

		 if(port0) k = 1 + 0*16 + flag_to_index(port0);
	else if(port1) k = 1 + 1*16 + flag_to_index(port1);
	else if(port2) k = 1 + 2*16 + flag_to_index(port2);
	else if(port3) k = 1 + 3*16 + flag_to_index(port3);

	if(ready && k) {
		ready = 0;
		return k;
	}
	ready = (!k);
	return 0;
}
// Scan input for newly-pressed keys.
// Return value:
// 0  = no key was pressed
// >0 = key code for pressed key
// <0 = error
int control_scankey()
{
	static unsigned ready = 0;
	unsigned k=0;
	unsigned port0=lastkey[0];
	unsigned port1=lastkey[1];
	unsigned port2=lastkey[2];
	unsigned port3=lastkey[3];

	     if(port0) k = 1 + 0*18 + flag_to_index(port0);
	else if(port1) k = 1 + 1*18 + flag_to_index(port1);
	else if(port2) k = 1 + 2*18 + flag_to_index(port2);
	else if(port3) k = 1 + 3*18 + flag_to_index(port3);

	if(ready && k)
	{
		ready = 0;
		return k;
	}
	ready = (!k);
	return 0;
}
void control_setkey(s_playercontrols * pcontrols, unsigned int flag, int key)
{
	if(!pcontrols) return;
	pcontrols->settings[flag_to_index(flag)] = key;
	pcontrols->keyflags = pcontrols->newkeyflags = 0;
}
Example #4
0
/** 
 * \fn int radix_trie_unit_test()
 * \params none
 * \return 0 on success
 * \return 1 on failure
 */
int radix_trie_unit_test () {
    struct radix_trie rt, rt2;
    enum status err;
    struct in_addr a[10];
    unsigned int af[10];
    unsigned int i;
    unsigned int test_failed = 0;
    unsigned int flag;
    zfile output;

    output = zattach(stdout, "w");
    if (output == NULL) {
        fprintf(stderr, "%s: error: could not initialize (possibly compressed) stdout for writing\n",
                __FUNCTION__);
    }
  
    for (i=0; i<32; i++) {
        flag = index_to_flag(i);
        printf("index: %u\tflag: %x\tindex: %u\n", i, flag, flag_to_index(flag));
    }

    err = radix_trie_init(&rt);
    if (err != ok) {
       fprintf(stderr, "%s: error: could not initialize radix_trie\n", __FUNCTION__);
    }
  
    a[0].s_addr = htonl(0xcafebabe); af[0] = 1;
    a[1].s_addr = htonl(0xcafedada); af[1] = 2;
    a[2].s_addr = htonl(0xbaddecaf); af[2] = 4;
    a[3].s_addr = htonl(0x01234567); af[3] = 8;
    a[4].s_addr = htonl(0xffeeddcc); af[4] = 16;
    a[5].s_addr = htonl(0x0a9b8c7d); af[5] = 32;
    a[6].s_addr = htonl(0xfedcba98); af[6] = 64;
    a[7].s_addr = htonl(0x76543210); af[7] = 128;
    a[8].s_addr = htonl(0xa1b2c3d4); af[8] = 256;

    printf("testing add\n");
    flag = 1;
    for (i=0; i<3; i++) {
        if (radix_trie_add_subnet(&rt, a[i], 32, af[i]) != ok) {
            zprintf(output, "error: could not add subnet %s\n", inet_ntoa(a[i]));
            test_failed = 1;
        }
    }

    for (i=6; i<9; i++) {
        if (radix_trie_add_subnet(&rt, a[i], 16, af[i]) != ok) {
            zprintf(output, "error: could not add subnet %s\n", inet_ntoa(a[i]));
            test_failed = 1;
        }
    }

    printf("testing lookup (expecting success)\n");
    for (i=0; i<3; i++) {
        if (radix_trie_lookup_addr(&rt, a[i]) != af[i]) {
            zprintf(output, "error: could not lookup subnet %s\n", inet_ntoa(a[i]));
            test_failed = 1;
        }
    }

    for (i=6; i<9; i++) {
        if (radix_trie_lookup_addr(&rt, a[i]) != af[i]) {
            zprintf(output, "error: could not lookup subnet %s\n", inet_ntoa(a[i]));
            test_failed = 1;
        }
    }

    printf("testing lookup (expecting failure)\n");
    for (i=3; i<6; i++) {
        if (radix_trie_lookup_addr(&rt, a[i]) != 0) {
            zprintf(output, "error: false positive lookup subnet %s\n", inet_ntoa(a[i]));
            test_failed = 1;
        }
    }

    printf("testing 14-bit add\n");
    for (i=0; i<3; i++) {
        if (radix_trie_add_subnet(&rt, a[i], 14, 0x100) != ok) {
            zprintf(output, "error: could not add subnet %s\n", inet_ntoa(a[i]));
            test_failed = 1;
        }
    }

    printf("testing 14-bit lookup (expecting success)\n");
    for (i=0; i<3; i++) {
        unsigned int f = radix_trie_lookup_addr(&rt, a[i]);
        if (f != (af[i] | 0x100)) {
            zprintf(output, "error: could not lookup address %s (%x), got %x instead\n", 
	           inet_ntoa(a[i]), htonl(a[i].s_addr), f);
            test_failed = 1;
        }
    }

    printf("testing 15-bit add\n");
    for (i=0; i<3; i++) {
        if (radix_trie_add_subnet(&rt, a[i], 15, 0x1000) != ok) {
            zprintf(output, "error: could not add subnet %s\n", inet_ntoa(a[i]));
            test_failed = 1;
        }
    }

    printf("testing 15-bit lookup (expecting success)\n");
    for (i=0; i<3; i++) {
        unsigned int f = radix_trie_lookup_addr(&rt, a[i]);
        if (f != (af[i] | 0x1000 | 0x100)) {
            zprintf(output, "error: could not lookup address %s (%x), got %x but expected %x\n", 
	           inet_ntoa(a[i]), htonl(a[i].s_addr), f, (af[i] | 0x1000 | 0x100));
            test_failed = 1;
        }
    }

    printf("testing lookup (expecting failure)\n");
    for (i=3; i<6; i++) {
        if (radix_trie_lookup_addr(&rt, a[i]) != 0) {
            zprintf(output, "error: false positive lookup address %s\n", inet_ntoa(a[i]));
            test_failed = 1;
        }
    }

    if (test_failed) {
        printf("FAILURE; at least one test failed\n");
    } else {
        printf("all tests passed\n");
    }
  
    printf("-----------------------------------\n");
    radix_trie_print(&rt);

    printf("testing high level interface\n");
    err = radix_trie_init(&rt2);
    if (err != ok) {
        fprintf(stderr, "error: could not initialize radix_trie\n");
    }

    attr_flags internal_attr, c2_attr, watchlist_attr, attr;
    struct in_addr addr;

    internal_attr = radix_trie_add_attr_label(&rt2, "internal");
    printf("attr: %x\n", internal_attr);
    c2_attr = radix_trie_add_attr_label(&rt2, "c2");
    watchlist_attr = radix_trie_add_attr_label(&rt2, "watchlist");

    addr = hex2addr(0xcafe0000);
    if (radix_trie_add_subnet(&rt2, addr, 16, internal_attr) != ok) { 
        zprintf(output, "error: could not add subnet %s\n", inet_ntoa(addr));
        test_failed = 1;
    }
    attr = radix_trie_lookup_addr(&rt2, addr); 
    if ((attr & internal_attr) == 0) {
        zprintf(output, "error: attribute lookup failed (expected %x, got %x)\n",
	       internal_attr, attr);
        test_failed = 1;
    }

    addr = hex2addr(0xdecaf000);
    if (radix_trie_add_subnet(&rt2, addr, 20, internal_attr) != ok) {
        zprintf(output, "error: could not add subnet %s\n", inet_ntoa(addr));
        test_failed = 1;
    }
    attr = radix_trie_lookup_addr(&rt2, addr); 
    if ((attr & internal_attr) == 0) {
        zprintf(output, "error: attribute lookup failed (expected %x, got %x)\n",
	       internal_attr, attr);
        test_failed = 1;
    }

    addr = hex2addr(0xdadacafe);
    if (radix_trie_add_subnet(&rt2, addr, 32, c2_attr) != ok) {
        zprintf(output, "error: could not add subnet %s\n", inet_ntoa(addr));
        test_failed = 1;
    }
    attr = radix_trie_lookup_addr(&rt2, addr); 
    if ((attr & c2_attr) == 0) {
        zprintf(output, "error: attribute lookup failed (expected %x, got %x)\n",
	       c2_attr, attr);
        test_failed = 1;
    }

    addr = hex2addr(0xdadacafe);
    if (radix_trie_add_subnet(&rt2, addr, 8, watchlist_attr) != ok) {
        zprintf(output, "error: could not add subnet %s\n", inet_ntoa(addr));
        test_failed = 1;
    }
    attr = radix_trie_lookup_addr(&rt2, addr); 
    if ((attr & watchlist_attr) == 0) {
        zprintf(output, "error: attribute lookup failed (expected %x, got %x)\n",
	       watchlist_attr, attr);
        test_failed = 1;
    }
 
    addr = hex2addr(0xffffffff);
    if (radix_trie_add_subnet(&rt2, addr, 1, watchlist_attr) != ok) {
        zprintf(output, "error: could not add subnet %s\n", inet_ntoa(addr));
        test_failed = 1;
    }
    attr = radix_trie_lookup_addr(&rt2, addr); 
    if ((attr & watchlist_attr) == 0) {
        zprintf(output, "error: attribute lookup failed (expected %x, got %x)\n",
	       c2_attr, attr);
        test_failed = 1;
    }
   
    if (test_failed) {
        printf("FAILURE; at least one test failed\n");
    } else {
        printf("all high level interface tests passed\n");
    }

    printf("-----------------------------------\n");
    radix_trie_print(&rt2);

    printf("-----------------------------------\n");

    if (radix_trie_high_level_unit_test() != ok) {
        test_failed = 1;
    }

    return test_failed; /* 0 on success, 1 otherwise */
}