// ********************************************************* // -(poll libmapper)---------------------------------------- void impmap_poll(impmap *x) { mapper_device_poll(x->device, 0); if (!x->ready) { if (mapper_device_ready(x->device)) { x->ready = 1; // create a new generic output signal x->dummy_output = mapper_device_add_output_signal(x->device, "CONNECT_TO_DESTINATION", 1, 'f', 0, 0, 0); // create a new generic input signal x->dummy_input = mapper_device_add_input_signal(x->device, "CONNECT_TO_SOURCE", 1, 'f', 0, 0, 0, 0, x); impmap_print_properties(x); } } if (x->new_in) { outlet_anything(x->outlet1, gensym("list"), x->size_in, x->buffer_in); x->new_in = 0; } clock_delay(x->clock, INTERVAL); // Set clock to go off after delay }
/*! Creation of a local destination. */ int setup_destination() { char sig_name[10]; destination = mapper_device_new("testquery-recv", 0, 0); if (!destination) goto error; eprintf("destination created.\n"); float mn[]={0,0,0,0}, mx[]={1,1,1,1}; for (int i = 0; i < 4; i++) { snprintf(sig_name, 10, "%s%i", "insig_", i); recvsig[i] = mapper_device_add_input_signal(destination, sig_name, i+1, 'f', 0, mn, mx, insig_handler, 0); } eprintf("Input signal 'insig' registered.\n"); eprintf("Number of inputs: %d\n", mapper_device_num_signals(destination, MAPPER_DIR_INCOMING)); return 0; error: return 1; }
int setup_destination() { destination = mapper_device_new("testqueue-recv", port, 0); if (!destination) goto error; eprintf("destination created.\n"); float mn=0, mx=1; recvsig = mapper_device_add_input_signal(destination, "insig", 1, 'f', 0, &mn, &mx, insig_handler, 0); recvsig1= mapper_device_add_input_signal(destination, "insig1", 1, 'f', 0, &mn, &mx, insig_handler, 0); eprintf("Input signal 'insig' registered.\n"); eprintf("Number of inputs: %d\n", mapper_device_num_signals(destination, MAPPER_DIR_INCOMING)); return 0; error: return 1; }
int main(int argc, char **argv) { int i, j, seen, result = 0; // process flags for -v verbose, -h help for (i = 1; i < argc; i++) { if (argv[i] && argv[i][0] == '-') { int len = strlen(argv[i]); for (j = 1; j < len; j++) { switch (argv[i][j]) { case 'h': eprintf("testprops.c: possible arguments " "-q quiet (suppress output), " "-h help\n"); return 1; break; case 'q': verbose = 0; break; default: break; } } } } mapper_device dev = mapper_device_new("testprops", 0, 0); mapper_signal sig = mapper_device_add_input_signal(dev, "test", 1, 'f', "Hz", 0, 0, 0, 0); while (!mapper_device_ready(dev)) { mapper_device_poll(dev, 100); } /* Test that default parameters are all listed. */ eprintf("Test 1: checking default parameters... "); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test that adding maximum causes it to be listed. */ float mx = 35.0; mapper_signal_set_maximum(sig, &mx); eprintf("Test 2: adding static property 'maximum'... "); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT | SEEN_MAX)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test that adding an extra parameter causes the extra parameter * to be listed. */ char *str = "test_value"; mapper_signal_set_property(sig, "test", 1, 's', str, 1); eprintf("Test 3: adding extra string property 'test'... "); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT | SEEN_MAX | SEEN_TEST)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("Test 4: retrieving property 'test'... "); char type; const void *val; int length; if (mapper_signal_property(sig, "test", &length, &type, &val)) { eprintf("ERROR\n"); result = 1; goto cleanup; } eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 's') { eprintf("ERROR (expected %c)\n", 's'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %d ... ", length); if (length != 1) { eprintf("ERROR (expected %d)\n", 1); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking value: '%s' ... ", (char*)val); if (strcmp((char*)val, str)) { eprintf("ERROR (expected '%s')\n", str); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test that removing an extra parameter causes the extra * parameter to _not_ be listed. */ mapper_signal_remove_property(sig, "test"); eprintf("Test 5: removing extra property 'test'... "); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT | SEEN_MAX)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test that adding two more properties works as expected. */ int x = 123; mapper_signal_set_property(sig, "x", 1, 'i', &x, 1); int y = 234; mapper_signal_set_property(sig, "y", 1, 'i', &y, 1); eprintf("Test 6: adding extra integer properties 'x' and 'y'... "); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT | SEEN_MAX | SEEN_X | SEEN_Y)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test the type and value associated with "x". */ eprintf("Test 7: retrieving property 'x'..."); if (mapper_signal_property(sig, "x", &length, &type, &val)) { eprintf("ERROR\n"); result = 1; goto cleanup; } eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 'i') { eprintf("ERROR (expected %c)\n", 'i'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %d ... ", length); if (length != 1) { eprintf("ERROR (expected %d)\n", 1); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking value: %i ... ", *(int*)val); if (*(int*)val != 123) { eprintf("ERROR (expected %d)\n", 123); result = 1; goto cleanup; } else eprintf("OK\n"); /* Check that there is no value associated with previously-removed * "test". */ eprintf("Test 8: retrieving removed property 'test': "); if (!mapper_signal_property(sig, "test", &length, &type, &val)) { eprintf("found... ERROR\n"); result = 1; goto cleanup; } else eprintf("not found... OK\n"); /* Check that there is an integer value associated with static, * required property "length". */ eprintf("Test 9: retrieving static, required property 'length'... "); if (mapper_signal_property(sig, "length", &length, &type, &val)) { eprintf("not found... ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 'i') { eprintf("ERROR (expected %c)\n", 'i'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %d ... ", length); if (length != 1) { eprintf("ERROR (expected %d)\n", 1); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking value: '%d' ... ", *(int*)val); if (*(int*)val != 1) { eprintf("ERROR (expected %d)\n", 1); result = 1; goto cleanup; } else eprintf("OK\n"); /* Check that there is a string value associated with static, * required property "name". */ eprintf("Test 10: retrieving static, required property 'name'... "); if (mapper_signal_property(sig, "name", &length, &type, &val)) { eprintf("not found... ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 's') { eprintf("ERROR (expected %c)\n", 's'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %d ... ", length); if (length != 1) { eprintf("ERROR (expected %d)\n", 1); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking value: '%s' ... ", (char*)val); if (strcmp((char*)val, "test")) { eprintf("ERROR (expected '%s')\n", str); result = 1; goto cleanup; } else eprintf("OK\n"); /* Check that there is a float value associated with static, * optional property "max". */ eprintf("Test 11: retrieving static, optional property 'max'... "); if (mapper_signal_property(sig, "max", &length, &type, &val)) { eprintf("not found... ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 'f') { eprintf("ERROR (expected %c)\n", 'f'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %d ... ", length); if (length != 1) { eprintf("ERROR (expected %d)\n", 1); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking value: '%f' ... ", *(float*)val); if (*(float*)val != 35.0f) { eprintf("ERROR (expected %f)\n", *(float*)val); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test that removing maximum causes it to _not_ be listed. */ mapper_signal_set_maximum(sig, 0); eprintf("Test 12: removing optional property 'max'... "); seen = check_keys(sig); if (seen & SEEN_MAX) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("Test 13: retrieving optional property 'max': "); if (!mapper_signal_property(sig, "max", &length, &type, &val)) { eprintf("found... ERROR\n"); result = 1; goto cleanup; } else eprintf("not found... OK\n"); /* Test adding and retrieving an integer vector property. */ eprintf("Test 14: adding an extra integer vector property 'test'... "); int set_int[] = {1, 2, 3, 4, 5}; mapper_signal_set_property(sig, "test", 5, 'i', &set_int, 1); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT | SEEN_X | SEEN_Y | SEEN_TEST)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("Test 15: retrieving vector property 'test': "); if (mapper_signal_property(sig, "test", &length, &type, &val)) { eprintf("not found... ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 'i') { eprintf("ERROR (expected %c)\n", 'i'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %d ... ", length); if (length != 5) { eprintf("ERROR (expected %d)\n", 1); result = 1; goto cleanup; } else eprintf("OK\n"); int *read_int = (int*)val; int matched = 0; eprintf("\t checking value: [%i,%i,%i,%i,%i] ... ", read_int[0], read_int[1], read_int[2], read_int[3], read_int[4]); for (i = 0; i < 5; i++) { if (read_int[i] == set_int[i]) matched++; } if (matched != 5) { eprintf("ERROR (expected [%i,%i,%i,%i,%i])\n", set_int[0], set_int[1], set_int[2], set_int[3], set_int[4]); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test rewriting 'test' as float vector property. */ eprintf("Test 16: rewriting 'test' as vector float property... "); float set_float[] = {10., 20., 30., 40., 50.}; mapper_signal_set_property(sig, "test", 5, 'f', &set_float, 1); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT | SEEN_X | SEEN_Y | SEEN_TEST)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("Test 17: retrieving property 'test'... "); if (mapper_signal_property(sig, "test", &length, &type, &val)) { eprintf("not found... ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 'f') { eprintf("ERROR (expected '%c')\n", 'f'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %i ... ", length); if (length != 5) { eprintf("ERROR (expected %d)\n", length); result = 1; goto cleanup; } else eprintf("OK\n"); float *read_float = (float*)val; eprintf("\t checking value: [%f,%f,%f,%f,%f] ... ", read_float[0], read_float[1], read_float[2], read_float[3], read_float[4]); matched = 0; for (i = 0; i < 5; i++) { if (read_float[i] == set_float[i]) matched++; } if (matched != 5) { eprintf("ERROR (expected [%f,%f,%f,%f,%f]\n", set_float[0], set_float[1], set_float[2], set_float[3], set_float[4]); result = 1; goto cleanup; } else eprintf("OK\n"); /* Test rewriting property 'test' as string vector property. */ eprintf("Test 18: rewriting 'test' as vector string property... "); char *set_string[] = {"foo", "bar"}; mapper_signal_set_property(sig, "test", 2, 's', &set_string, 1); seen = check_keys(sig); if (seen != (SEEN_DIR | SEEN_LENGTH | SEEN_NAME | SEEN_TYPE | SEEN_UNIT | SEEN_X | SEEN_Y | SEEN_TEST)) { eprintf("ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("Test 19: retrieving property 'test'... "); if (mapper_signal_property(sig, "test", &length, &type, &val)) { eprintf("not found... ERROR\n"); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking type: %c ... ", type); if (type != 's') { eprintf("ERROR (expected '%c')\n", 's'); result = 1; goto cleanup; } else eprintf("OK\n"); eprintf("\t checking length: %d ...", length); if (length != 2) { eprintf("ERROR (expected %d)\n", 2); result = 1; goto cleanup; } else eprintf("OK\n"); char **read_string = (char**)val; eprintf("\t checking value: ['%s','%s'] ... ", read_string[0], read_string[1]); matched = 0; for (i = 0; i < 2; i++) { if (read_string[i] && strcmp(read_string[i], set_string[i]) == 0) matched++; } if (matched != 2) { eprintf("ERROR (expected ['%s','%s'])\n", set_string[0], set_string[1]); result = 1; goto cleanup; } else eprintf("OK\n"); cleanup: if (dev) mapper_device_free(dev); if (!verbose) printf(".................................................."); printf("Test %s.\n", result ? "FAILED" : "PASSED"); return result; }
// ********************************************************* // -(connection handler)------------------------------------ void impmap_on_map(mapper_device dev, mapper_map map, mapper_record_event e) { // if connected involves current generic signal, create a new generic signal impmap *x = (void*)mapper_device_user_data(dev); if (!x) { post("error in connect handler: user_data is NULL"); return; } if (!x->ready) { post("error in connect handler: device not ready"); return; } // retrieve devices and signals mapper_slot slot = mapper_map_slot(map, MAPPER_LOC_SOURCE, 0); mapper_signal src_sig = mapper_slot_signal(slot); mapper_device src_dev = mapper_signal_device(src_sig); slot = mapper_map_slot(map, MAPPER_LOC_DESTINATION, 0); mapper_signal dst_sig = mapper_slot_signal(slot); mapper_device dst_dev = mapper_signal_device(dst_sig); // sanity check: don't allow self-connections if (src_dev == dst_dev) { mapper_map_release(map); return; } char full_name[256]; if (e == MAPPER_ADDED) { if (src_dev == x->device) { snprintf(full_name, 256, "%s/%s", mapper_device_name(dst_dev), mapper_signal_name(dst_sig)); if (strcmp(mapper_signal_name(src_sig), full_name) == 0) { // <thisDev>:<dstDevName>/<dstSigName> -> <dstDev>:<dstSigName> return; } if (mapper_device_num_signals(x->device, MAPPER_DIR_OUTGOING) >= MAX_LIST) { post("Max outputs reached!"); return; } // unmap the generic signal mapper_map_release(map); // add a matching output signal int i, length = mapper_signal_length(dst_sig); char type = mapper_signal_type(dst_sig); void *min = mapper_signal_minimum(dst_sig); void *max = mapper_signal_maximum(dst_sig); float *minf = 0, *maxf = 0; if (type == 'f') { minf = (float*)min; maxf = (float*)max; } else { if (min) { minf = alloca(length * sizeof(float)); if (type == 'i') { int *mini = (int*)min; for (i = 0; i < length; i++) minf[i] = (float)mini[i]; } else if (type == 'd') { double *mind = (double*)min; for (i = 0; i < length; i++) minf[i] = (float)mind[i]; } else minf = 0; } if (max) { maxf = alloca(length * sizeof(float)); if (type == 'i') { int *maxi = (int*)max; for (i = 0; i < length; i++) maxf[i] = (float)maxi[i]; } else if (type == 'd') { double *maxd = (double*)max; for (i = 0; i < length; i++) maxf[i] = (float)maxd[i]; } else maxf = 0; } } src_sig = mapper_device_add_output_signal(x->device, full_name, length, 'f', 0, minf, maxf); if (!src_sig) { post("error creating new source signal!"); return; } mapper_signal_set_callback(src_sig, impmap_on_query); // map the new signal map = mapper_map_new(1, &src_sig, 1, &dst_sig); mapper_map_set_mode(map, MAPPER_MODE_EXPRESSION); mapper_map_set_expression(map, "y=x"); mapper_map_push(map); impmap_update_output_vector_positions(x); //output numOutputs maxpd_atom_set_int(&x->msg_buffer, mapper_device_num_signals(x->device, MAPPER_DIR_OUTGOING) - 1); outlet_anything(x->outlet3, gensym("numOutputs"), 1, &x->msg_buffer); } else if (dst_dev == x->device) { snprintf(full_name, 256, "%s/%s", mapper_device_name(src_dev), mapper_signal_name(src_sig)); if (strcmp(mapper_signal_name(dst_sig), full_name) == 0) { // <srcDevName>:<srcSigName> -> <thisDev>:<srcDevName>/<srcSigName> return; } if (mapper_device_num_signals(x->device, MAPPER_DIR_INCOMING) >= MAX_LIST) { post("Max inputs reached!"); return; } // unmap the generic signal mapper_map_release(map); // add a matching input signal int i, length = mapper_signal_length(src_sig); char type = mapper_signal_type(src_sig); void *min = mapper_signal_minimum(src_sig); void *max = mapper_signal_maximum(src_sig); float *minf = 0, *maxf = 0; if (type == 'f') { minf = (float*)min; maxf = (float*)max; } else { if (min) { minf = alloca(length * sizeof(float)); if (type == 'i') { int *mini = (int*)min; for (i = 0; i < length; i++) minf[i] = (float)mini[i]; } else if (type == 'd') { double *mind = (double*)min; for (i = 0; i < length; i++) minf[i] = (float)mind[i]; } else minf = 0; } if (max) { maxf = alloca(length * sizeof(float)); if (type == 'i') { int *maxi = (int*)max; for (i = 0; i < length; i++) maxf[i] = (float)maxi[i]; } else if (type == 'd') { double *maxd = (double*)max; for (i = 0; i < length; i++) maxf[i] = (float)maxd[i]; } else maxf = 0; } } dst_sig = mapper_device_add_input_signal(x->device, full_name, length, 'f', 0, minf, maxf, impmap_on_input, 0); if (!dst_sig) { post("error creating new destination signal!"); return; } // map the new signal map = mapper_map_new(1, &src_sig, 1, &dst_sig); mapper_map_set_mode(map, MAPPER_MODE_EXPRESSION); mapper_map_set_expression(map, "y=x"); mapper_map_push(map); impmap_update_input_vector_positions(x); //output numInputs maxpd_atom_set_int(&x->msg_buffer, mapper_device_num_signals(x->device, MAPPER_DIR_INCOMING) - 1); outlet_anything(x->outlet3, gensym("numInputs"), 1, &x->msg_buffer); } } else if (e == MAPPER_REMOVED) { if (src_sig == x->dummy_input || src_sig == x->dummy_output || dst_sig == x->dummy_input || dst_sig == x->dummy_output) return; if (src_dev == x->device) { snprintf(full_name, 256, "%s/%s", mapper_device_name(dst_dev), mapper_signal_name(dst_sig)); if (strcmp(mapper_signal_name(src_sig), full_name) != 0) return; // remove signal mapper_device_remove_signal(x->device, src_sig); impmap_update_input_vector_positions(x); //output numOutputs maxpd_atom_set_int(&x->msg_buffer, mapper_device_num_signals(x->device, MAPPER_DIR_OUTGOING) - 1); outlet_anything(x->outlet3, gensym("numOutputs"), 1, &x->msg_buffer); } else if (dst_dev == x->device) { snprintf(full_name, 256, "%s/%s", mapper_device_name(src_dev), mapper_signal_name(src_sig)); if (strcmp(mapper_signal_name(dst_sig), full_name) != 0) return; // remove signal mapper_device_remove_signal(x->device, dst_sig); impmap_update_input_vector_positions(x); //output numInputs maxpd_atom_set_int(&x->msg_buffer, mapper_device_num_signals(x->device, MAPPER_DIR_INCOMING) - 1); outlet_anything(x->outlet3, gensym("numInputs"), 1, &x->msg_buffer); } } }