void SelChanged()

in Lilypad/Config.cpp [295:513]


void SelChanged(int port, int slot) {
	HWND hWnd = hWnds[port][slot];
	if (!hWnd) return;
	HWND hWndTemp, hWndList = GetDlgItem(hWnd, IDC_LIST);
	int j, i = ListView_GetSelectedCount(hWndList);
	wchar_t *devName = L"N/A";
	wchar_t *key = L"N/A";
	wchar_t *command = L"N/A";
	// Second value is now turbo.
	int turbo = -1;
	int sensitivity = 0;
	int deadZone = 0;
	int nonButtons = 0;
	// Set if sensitivity != 0, but need to disable flip anyways.
	// Only used to relative axes.
	int disableFlip = 0;
	wchar_t temp[4][1000];
	Device *dev;
	int bFound = 0;
	int ffbFound = 0;
	ForceFeedbackBinding *ffb = 0;
	Binding *b = 0;
	if (i >= 1) {
		int index = -1;
		int flipped = 0;
		while (1) {
			index = ListView_GetNextItem(hWndList, index, LVNI_SELECTED);
			if (index < 0) break;
			LVITEMW item;
			item.iItem = index;
			item.mask = LVIF_TEXT;
			item.pszText = temp[3];
			for (j=0; j<3; j++) {
				item.iSubItem = j;
				item.cchTextMax = sizeof(temp[0])/sizeof(temp[3][0]);
				if (!ListView_GetItem(hWndList, &item)) break;
				if (!bFound && !ffbFound)
					wcscpy(temp[j], temp[3]);
				else if (wcsicmp(temp[j], temp[3])) {
					int q = 0;
					while (temp[j][q] == temp[3][q]) q++;
					if (q && temp[j][q-1] == ' ' && temp[j][q] && temp[j][q+1] == 0) q--;
					if (j == 1) {
						// Really ugly, but merges labels for multiple directions for same axis.
						if ((temp[j][q] == 0 || (temp[j][q] == ' ' && temp[j][q+2] == 0)) &&
							(temp[3][q] == 0 || (temp[3][q] == ' ' && temp[3][q+2] == 0))) {
								temp[j][q] = 0;
								continue;
						}
					}
					// Merge different directions for same stick.
					else if (j == 2 && q > 4) {
						temp[j][q] = 0;
						continue;
					}
					wcscpy(temp[j], L"*");
				}
			}
			if (j == 3) {
				devName = temp[0];
				key = temp[1];
				command = temp[2];
				if (GetBinding(port, slot, index, dev, b, ffb)) {
					if (b) {
						bFound ++;
						VirtualControl *control = &dev->virtualControls[b->controlIndex];
						// Ignore
						if (b->command != 0x7F) {
							// Only relative axes can't have negative sensitivity.
							if (((control->uid >> 16) & 0xFF) == RELAXIS) {
								disableFlip = 1;
							}
							turbo += b->turbo;
							if (b->sensitivity < 0) {
								flipped ++;
								sensitivity -= b->sensitivity;
							}
							else {
								sensitivity += b->sensitivity;
							}
							if (((control->uid >> 16)&0xFF) != PSHBTN && ((control->uid >> 16)&0xFF) != TGLBTN) {
								deadZone += b->deadZone;
								nonButtons++;
							}
						}
						else disableFlip = 1;
					}
					else ffbFound++;
				}
			}
		}
		if ((bFound && ffbFound) || ffbFound > 1) {
			ffb = 0;
			turbo = -1;
			deadZone = 0;
			sensitivity = 0;
			disableFlip = 1;
			bFound = ffbFound = 0;
		}
		else if (bFound) {
			turbo++;
			sensitivity /= bFound;
			if (nonButtons) {
				deadZone /= nonButtons;
			}
			if (bFound > 1) disableFlip = 1;
			else if (flipped) {
				sensitivity = -sensitivity;
			}
		}
	}

	for (i=IDC_SLIDER1; i<ID_DELETE; i++) {
		hWndTemp = GetDlgItem(hWnd, i);
		if (hWndTemp)
			ShowWindow(hWndTemp, ffb == 0);
	}
	for (i=0x1300; i<0x1400; i++) {
		hWndTemp = GetDlgItem(hWnd, i);
		if (hWndTemp) {
			int enable = ffb != 0;
			if (ffb && i >= IDC_FF_AXIS1_ENABLED) {
				int index = (i - IDC_FF_AXIS1_ENABLED) / 16;
				if (index >= dev->numFFAxes) {
					enable = 0;
				}
				else {
					int type = i - index*16;
					AxisEffectInfo *info = 0;
					if (dev->numFFAxes > index) {
						info = ffb->axes+index;
					}
					if (type == IDC_FF_AXIS1) {
						// takes care of IDC_FF_AXIS1_FLIP as well.
						SetupLogSlider(hWndTemp);
						int force = 0;
						if (info) {
							force = info->force;
						}
						SetLogSliderVal(hWnd, i, GetDlgItem(hWnd, i-IDC_FF_AXIS1+IDC_FF_AXIS1_SCALE), force);
					}
					else if (type == IDC_FF_AXIS1_ENABLED) {
						CheckDlgButton(hWnd, i, BST_CHECKED * (info->force!=0));
						wsprintfW(temp[0], L"Axis %i", index+1);
						if (dev->ffAxes[index].displayName && wcslen(dev->ffAxes[index].displayName) < 50) {
							wchar_t *end = wcschr(temp[0], 0);
							wsprintfW(end, L": %s", dev->ffAxes[index].displayName);
						}
						SetWindowText(hWndTemp, temp[0]);
					}
				}
			}
			ShowWindow(hWndTemp, enable);
		}
	}
	if (!ffb) {
		SetLogSliderVal(hWnd, IDC_SLIDER1, GetDlgItem(hWnd, IDC_AXIS_SENSITIVITY1), sensitivity);
		SetLogSliderVal(hWnd, IDC_SLIDER_DEADZONE, GetDlgItem(hWnd, IDC_AXIS_DEADZONE), deadZone);

		if (disableFlip) EnableWindow(GetDlgItem(hWnd, IDC_FLIP1), 0);

		EnableWindow(GetDlgItem(hWnd, IDC_TURBO), turbo >= 0);
		if (turbo > 0 && turbo < bFound) {
			SendMessage(GetDlgItem(hWnd, IDC_TURBO), BM_SETSTYLE, BS_AUTO3STATE, 0);
			CheckDlgButton(hWnd, IDC_TURBO, BST_INDETERMINATE);
		}
		else {
			SendMessage(GetDlgItem(hWnd, IDC_TURBO), BM_SETSTYLE, BS_AUTOCHECKBOX, 0);
			CheckDlgButton(hWnd, IDC_TURBO, BST_CHECKED * (bFound && turbo == bFound));
		}
		HWND hWndCombo = GetDlgItem(hWnd, IDC_AXIS_DIRECTION);
		int enableCombo = 0;
		SendMessage(hWndCombo, CB_RESETCONTENT, 0, 0);
		if (b && bFound == 1) {
			VirtualControl *control = &dev->virtualControls[b->controlIndex];
			unsigned int uid = control->uid;
			if (((uid>>16) & 0xFF) == ABSAXIS) {
				enableCombo = 1;
				wchar_t *endings[3] = {L" -", L" +", L""};
				wchar_t *string = temp[3];
				wcscpy(string, key);
				wchar_t *end = wcschr(string, 0);
				int sel = 2;
				if (!(uid & UID_AXIS)) {
					end[-2] = 0;
					sel = (end[-1] == '+');
					end -= 2;
				}
				for (int i=0; i<3; i++) {
					wcscpy(end, endings[i]);
					SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM) string);
					if (i == sel)
						SendMessage(hWndCombo, CB_SETCURSEL, i, 0);
				}
			}
		}
		EnableWindow(hWndCombo, enableCombo);
		if (!enableCombo) {
			SendMessage(hWndCombo, CB_ADDSTRING, 0, (LPARAM) key);
			SendMessage(hWndCombo, CB_SELECTSTRING, i, (LPARAM) key);
		}

		SetDlgItemText(hWnd, IDC_AXIS_DEVICE1, devName);
		SetDlgItemText(hWnd, IDC_AXIS_CONTROL1, command);
	}
	else {
		wchar_t temp2[2000];
		wsprintfW(temp2, L"%s / %s", devName, command);
		SetDlgItemText(hWnd, ID_FF, temp2);

		hWndTemp = GetDlgItem(hWnd, IDC_FF_EFFECT);
		SendMessage(hWndTemp, CB_RESETCONTENT, 0, 0);
		for (i=0; i<dev->numFFEffectTypes; i++) {
			SendMessage(hWndTemp, CB_INSERTSTRING, i, (LPARAM)dev->ffEffectTypes[i].displayName);
		}
		SendMessage(hWndTemp, CB_SETCURSEL, ffb->effectIndex, 0);
		EnableWindow(hWndTemp, dev->numFFEffectTypes>1);
	}
}