Example #1
0
static std::wstring improve_name_from_base_path(const std::wstring &regpath, bool *hid)
{
	// now try to open the registry key
	auto device_key = reg_open_key(HKEY_LOCAL_MACHINE, regpath);
	if (!device_key.valid())
		return std::wstring();

	// fetch the device description; if it exists, we are finished
	auto regstring = reg_query_string(device_key, L"DeviceDesc");
	if (!regstring.empty())
		return trim_prefix(regstring);

	// if the key name does not contain "HID", it's not going to be in the USB tree; give up
	*hid = regpath.find(L"HID") != std::string::npos;
	return std::wstring();
}
Example #2
0
static std::wstring improve_name_from_usb_path(const std::wstring &regpath)
{
	static const std::wstring usbbasepath(L"SYSTEM\\CurrentControlSet\\Enum\\USB");

	// extract the expected parent ID from the regpath
	size_t last_slash_index = regpath.find_last_of('\\');
	if (last_slash_index == std::wstring::npos)
		return std::wstring();

	std::wstring parentid = regpath.substr(last_slash_index + 1);

	// open the USB key
	auto usb_key = reg_open_key(HKEY_LOCAL_MACHINE, usbbasepath);
	if (!usb_key.valid())
		return std::wstring();

	std::wstring regstring;

	foreach_subkey(usb_key, [&regstring, &parentid](HKEY subkey)
	{
		foreach_subkey(subkey, [&regstring, &parentid](HKEY endkey)
		{
			std::wstring endparentid = reg_query_string(endkey, L"ParentIdPrefix");

			// This key doesn't have a ParentIdPrefix
			if (endparentid.empty())
				return true;

			// do we have a match?
			if (parentid.find(endparentid) == 0)
				regstring = reg_query_string(endkey, L"DeviceDesc");

			return regstring.empty();
		});

		return regstring.empty();
	});

	return trim_prefix(regstring);
}
Example #3
0
static int
read_test_from_file(char *filename)
{
	char buf[1024], *cur_buf, sub_buf[1024], func_buf[32], *line;
	FILE *file;
	RLayout *layout = NULL;
	RArea win = { 0 };
	int linenum, buf_size, expected1, expected2, errors, comment = 0;
	unsigned int width, height, x, y;

	file = fopen(filename, "r");
	if(file == NULL) {
		fprintf(stderr, "Cannot open %s: %s\n", filename, strerror(errno));
		return 1;
	}

	errors = 0;

	cur_buf = buf;
	buf_size = sizeof(buf);

	for(linenum = 1; fgets(cur_buf, buf_size, file) != NULL; linenum++) {
		int last_chr_idx = strlen(cur_buf) - 1;
		if(last_chr_idx >= 0) {
			if(cur_buf[last_chr_idx] == '\n') {
				last_chr_idx--;
			}

			if(last_chr_idx >= 0 && cur_buf[last_chr_idx] == '\\') {
				cur_buf += last_chr_idx;
				buf_size -= last_chr_idx;

				if(buf_size > 0) {
					continue;
				}

				fprintf(stderr, "%s:%d: line too long\n", filename, linenum);
				errors++;
				break;
			}
		}

		cur_buf = buf;
		buf_size = sizeof(buf);

		line = trim_spaces(buf);

		// Multiline comments: =comment -> =end
		if(comment) {
			if(strcmp(line, "=end") == 0) {
				comment = 0;
			}
			continue;
		}

		if(strcmp(line, "=comment") == 0) {
			comment = 1;
			continue;
		}

		if(line[0] == '#' || line[0] == '\0') {
			continue;
		}

		// layout FILENAME|area ...
		if(sscanf(line, "layout %1023s", sub_buf) == 1) {
			FILE *file_layout;

			if((file_layout = fopen(sub_buf, "r")) != NULL) {
				layout = read_layout_file(file_layout, sub_buf);
				fclose(file_layout);
			}
			else if(errno == ENOENT) {
				line = trim_spaces(&line[7]);

				layout = extract_layout(filename, linenum, line);
			}

			if(layout != NULL) {
				continue;
			}

			fprintf(stderr, "%s:%d: layout error\n", filename, linenum);
			errors++;
			break;
		}

		// check_horizontal_layout area ...
		if(strncmp(line, "check_horizontal_layout ", 24) == 0) {
			RLayout *check_layout;

			line += 24;
			line = trim_spaces(line);

			check_layout = extract_layout(filename, linenum, line);
			if(check_layout == NULL) {
				break;
			}

			if(cmp_rarea_list(filename, linenum,
			                  "check_horizontal_layout",
			                  layout->horiz, check_layout->monitors)) {
				continue;
			}
			break;
		}

		// check_vertical_layout area ...
		if(strncmp(line, "check_vertical_layout ", 22) == 0) {
			RLayout *check_layout;

			line += 22;
			line = trim_spaces(line);

			check_layout = extract_layout(filename, linenum, line);
			if(check_layout == NULL) {
				break;
			}

			if(cmp_rarea_list(filename, linenum,
			                  "check_vertical_layout",
			                  layout->vert, check_layout->monitors)) {
				continue;
			}
			break;
		}

		// window area
		if(sscanf(line, "window %dx%d+%d+%d", &width, &height, &x, &y) == 4) {
			win = RAreaNew((int)x, (int)y, (int)width, (int)height);
			if(RAreaIsValid(&win)) {
				continue;
			}

			fprintf(stderr, "%s:%d: bad window geometry\n", filename, linenum);
			errors++;
			break;
		}

		if(layout == NULL) {
			fprintf(stderr,
			        "%s:%d: cannot continue as `layout ...' line not found\n",
			        filename, linenum);
			errors++;
			break;
		}

		if(!RAreaIsValid(&win)) {
			fprintf(stderr,
			        "%s:%d: cannot continue as `window ...' line not found\n",
			        filename, linenum);
			errors++;
			break;
		}

		// Function area
		//   RLayoutFull       area
		//   RLayoutFullHoriz  area
		//   RLayoutFullVert   area
		//   RLayoutFull1      area
		//   RLayoutFullHoriz1 area
		//   RLayoutFullVert1  area
		if(sscanf(line, "%31s %dx%d+%d+%d",
		                func_buf, &width, &height, &x, &y) == 5) {
			RArea got_area, expected_area;
			char *function = trim_prefix(func_buf, "RLayoutFull");

			expected_area = RAreaNew((int)x, (int)y, (int)width, (int)height);

			if(function[0] == '\0') {
				got_area = RLayoutFull(layout, &win);
			}
			else if(strcmp(function, "Horiz") == 0) {
				got_area = RLayoutFullHoriz(layout, &win);
			}
			else if(strcmp(function, "Vert") == 0) {
				got_area = RLayoutFullVert(layout, &win);
			}
			else if(strcmp(function, "1") == 0) {
				got_area = RLayoutFull1(layout, &win);
			}
			else if(strcmp(function, "Horiz1") == 0) {
				got_area = RLayoutFullHoriz1(layout, &win);
			}
			else if(strcmp(function, "Vert1") == 0) {
				got_area = RLayoutFullVert1(layout, &win);
			}
			else {
				fprintf(stderr, "%s:%d: bad function name bound to area\n",
				        filename, linenum);
				errors++;
				break;
			}

			if(memcmp(&got_area, &expected_area, sizeof(got_area)) != 0) {
				printf("%s:%d: %s failed\n"
				       "\t     got: %dx%d+%d+%d\n"
				       "\texpected: %dx%d+%d+%d\n",
				       filename, linenum, func_buf,
				       got_area.width, got_area.height, got_area.x, got_area.y,
				       expected_area.width, expected_area.height,
				       expected_area.x, expected_area.y);
				errors++;
			}
		}
		// Function num1 num2
		//   RLayoutFindTopBottomEdges top  bottom
		//   RLayoutFindLeftRightEdges left right
		else if(sscanf(line, "%31s %d %d",
		                func_buf, &expected1, &expected2) == 3) {
			int got1, got2;
			char *function = trim_prefix(func_buf, "RLayoutFind");

			if(strcmp(function, "TopBottomEdges") == 0) {
				RLayoutFindTopBottomEdges(layout, &win, &got1, &got2);
			}
			else if(strcmp(function, "LeftRightEdges") == 0) {
				RLayoutFindLeftRightEdges(layout, &win, &got1, &got2);
			}
			else {
				fprintf(stderr,
				        "%s:%d: bad function name bound to 2 expected results\n",
				        filename, linenum);
				errors++;
				break;
			}

			if(got1 != expected1 || got2 != expected2) {
				printf("%s:%d: %s failed\n"
				       "\t     got: (%d, %d)\n"
				       "\texpected: (%d, %d)\n",
				       filename, linenum, func_buf,
				       got1, got2,
				       expected1, expected2);
				errors++;
			}
		}
		// Function num
		//   RLayoutFindMonitorBottomEdge bottom
		//   RLayoutFindMonitorTopEdge    top
		//   RLayoutFindMonitorLeftEdge   left
		//   RLayoutFindMonitorRightEdge  right
		else if(sscanf(line, "%31s %d", func_buf, &expected1) == 2) {
			int got;
			char *function = trim_prefix(func_buf, "RLayoutFindMonitor");

			if(strcmp(function, "BottomEdge") == 0) {
				got = RLayoutFindMonitorBottomEdge(layout, &win);
			}
			else if(strcmp(function, "TopEdge") == 0) {
				got = RLayoutFindMonitorTopEdge(layout, &win);
			}
			else if(strcmp(function, "LeftEdge") == 0) {
				got = RLayoutFindMonitorLeftEdge(layout, &win);
			}
			else if(strcmp(function, "RightEdge") == 0) {
				got = RLayoutFindMonitorRightEdge(layout, &win);
			}
			else {
				fprintf(stderr,
				        "%s:%d: bad function name bound to one expected result\n",
				        filename, linenum);
				errors++;
				break;
			}

			if(got != expected1) {
				printf("%s:%d: %s failed\n"
				       "\t     got: %d\n"
				       "\texpected: %d\n",
				       filename, linenum, func_buf,
				       got,
				       expected1);
				errors++;
			}
		}
		else {
			fprintf(stderr, "%s:%d: test unrecognized line\n", filename, linenum);
			errors++;
			break;
		}
	}

	fclose(file);

	return errors;
}