- WIP Adding support for 3rd party controllers

This commit is contained in:
David Khachaturov 2020-06-12 16:04:24 +01:00
parent a291f0daf6
commit e1f3e573fd
5 changed files with 753 additions and 669 deletions

View file

@ -1,212 +1,206 @@
namespace BetterJoyForCemu { namespace BetterJoyForCemu {
partial class _3rdPartyControllers { partial class _3rdPartyControllers {
/// <summary> /// <summary>
/// Required designer variable. /// Required designer variable.
/// </summary> /// </summary>
private System.ComponentModel.IContainer components = null; private System.ComponentModel.IContainer components = null;
/// <summary> /// <summary>
/// Clean up any resources being used. /// Clean up any resources being used.
/// </summary> /// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) { protected override void Dispose(bool disposing) {
if (disposing && (components != null)) { if (disposing && (components != null)) {
components.Dispose(); components.Dispose();
} }
base.Dispose(disposing); base.Dispose(disposing);
} }
#region Windows Form Designer generated code #region Windows Form Designer generated code
/// <summary> /// <summary>
/// Required method for Designer support - do not modify /// Required method for Designer support - do not modify
/// the contents of this method with the code editor. /// the contents of this method with the code editor.
/// </summary> /// </summary>
private void InitializeComponent() { private void InitializeComponent() {
this.components = new System.ComponentModel.Container(); this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(_3rdPartyControllers)); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(_3rdPartyControllers));
this.list_allControllers = new System.Windows.Forms.ListBox(); this.list_allControllers = new System.Windows.Forms.ListBox();
this.list_customControllers = new System.Windows.Forms.ListBox(); this.list_customControllers = new System.Windows.Forms.ListBox();
this.btn_add = new System.Windows.Forms.Button(); this.btn_add = new System.Windows.Forms.Button();
this.btn_remove = new System.Windows.Forms.Button(); this.btn_remove = new System.Windows.Forms.Button();
this.group_props = new System.Windows.Forms.GroupBox(); this.group_props = new System.Windows.Forms.GroupBox();
this.chk_isLeft = new System.Windows.Forms.CheckBox(); this.label2 = new System.Windows.Forms.Label();
this.chk_isPro = new System.Windows.Forms.CheckBox(); this.chooseType = new System.Windows.Forms.ComboBox();
this.btn_applyAndClose = new System.Windows.Forms.Button(); this.btn_applyAndClose = new System.Windows.Forms.Button();
this.btn_apply = new System.Windows.Forms.Button(); this.btn_apply = new System.Windows.Forms.Button();
this.lbl_all = new System.Windows.Forms.Label(); this.lbl_all = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label();
this.tip_device = new System.Windows.Forms.ToolTip(this.components); this.tip_device = new System.Windows.Forms.ToolTip(this.components);
this.btn_refresh = new System.Windows.Forms.Button(); this.btn_refresh = new System.Windows.Forms.Button();
this.group_props.SuspendLayout(); this.group_props.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// list_allControllers // list_allControllers
// //
this.list_allControllers.FormattingEnabled = true; this.list_allControllers.FormattingEnabled = true;
this.list_allControllers.Location = new System.Drawing.Point(12, 27); this.list_allControllers.Location = new System.Drawing.Point(12, 27);
this.list_allControllers.Name = "list_allControllers"; this.list_allControllers.Name = "list_allControllers";
this.list_allControllers.Size = new System.Drawing.Size(103, 225); this.list_allControllers.Size = new System.Drawing.Size(103, 225);
this.list_allControllers.TabIndex = 0; this.list_allControllers.TabIndex = 0;
this.list_allControllers.SelectedValueChanged += new System.EventHandler(this.list_allControllers_SelectedValueChanged); this.list_allControllers.SelectedValueChanged += new System.EventHandler(this.list_allControllers_SelectedValueChanged);
this.list_allControllers.MouseDown += new System.Windows.Forms.MouseEventHandler(this.list_allControllers_MouseDown); this.list_allControllers.MouseDown += new System.Windows.Forms.MouseEventHandler(this.list_allControllers_MouseDown);
// //
// list_customControllers // list_customControllers
// //
this.list_customControllers.FormattingEnabled = true; this.list_customControllers.FormattingEnabled = true;
this.list_customControllers.Location = new System.Drawing.Point(169, 27); this.list_customControllers.Location = new System.Drawing.Point(169, 27);
this.list_customControllers.Name = "list_customControllers"; this.list_customControllers.Name = "list_customControllers";
this.list_customControllers.Size = new System.Drawing.Size(103, 108); this.list_customControllers.Size = new System.Drawing.Size(103, 108);
this.list_customControllers.TabIndex = 1; this.list_customControllers.TabIndex = 1;
this.list_customControllers.SelectedValueChanged += new System.EventHandler(this.list_customControllers_SelectedValueChanged); this.list_customControllers.SelectedValueChanged += new System.EventHandler(this.list_customControllers_SelectedValueChanged);
this.list_customControllers.MouseDown += new System.Windows.Forms.MouseEventHandler(this.list_customControllers_MouseDown); this.list_customControllers.MouseDown += new System.Windows.Forms.MouseEventHandler(this.list_customControllers_MouseDown);
// //
// btn_add // btn_add
// //
this.btn_add.Location = new System.Drawing.Point(121, 27); this.btn_add.Location = new System.Drawing.Point(121, 27);
this.btn_add.Name = "btn_add"; this.btn_add.Name = "btn_add";
this.btn_add.Size = new System.Drawing.Size(42, 23); this.btn_add.Size = new System.Drawing.Size(42, 23);
this.btn_add.TabIndex = 2; this.btn_add.TabIndex = 2;
this.btn_add.Text = "->"; this.btn_add.Text = "->";
this.btn_add.UseVisualStyleBackColor = true; this.btn_add.UseVisualStyleBackColor = true;
this.btn_add.Click += new System.EventHandler(this.btn_add_Click); this.btn_add.Click += new System.EventHandler(this.btn_add_Click);
// //
// btn_remove // btn_remove
// //
this.btn_remove.Location = new System.Drawing.Point(121, 112); this.btn_remove.Location = new System.Drawing.Point(121, 112);
this.btn_remove.Name = "btn_remove"; this.btn_remove.Name = "btn_remove";
this.btn_remove.Size = new System.Drawing.Size(42, 23); this.btn_remove.Size = new System.Drawing.Size(42, 23);
this.btn_remove.TabIndex = 3; this.btn_remove.TabIndex = 3;
this.btn_remove.Text = "<-"; this.btn_remove.Text = "<-";
this.btn_remove.UseVisualStyleBackColor = true; this.btn_remove.UseVisualStyleBackColor = true;
this.btn_remove.Click += new System.EventHandler(this.btn_remove_Click); this.btn_remove.Click += new System.EventHandler(this.btn_remove_Click);
// //
// group_props // group_props
// //
this.group_props.Controls.Add(this.chk_isLeft); this.group_props.Controls.Add(this.label2);
this.group_props.Controls.Add(this.chk_isPro); this.group_props.Controls.Add(this.chooseType);
this.group_props.Location = new System.Drawing.Point(122, 142); this.group_props.Location = new System.Drawing.Point(122, 142);
this.group_props.Name = "group_props"; this.group_props.Name = "group_props";
this.group_props.Size = new System.Drawing.Size(150, 81); this.group_props.Size = new System.Drawing.Size(150, 81);
this.group_props.TabIndex = 4; this.group_props.TabIndex = 4;
this.group_props.TabStop = false; this.group_props.TabStop = false;
this.group_props.Text = "Settings"; this.group_props.Text = "Settings";
// //
// chk_isLeft // label2
// //
this.chk_isLeft.AutoSize = true; this.label2.AutoSize = true;
this.chk_isLeft.CheckAlign = System.Drawing.ContentAlignment.MiddleRight; this.label2.Location = new System.Drawing.Point(10, 22);
this.chk_isLeft.Location = new System.Drawing.Point(6, 42); this.label2.Name = "label2";
this.chk_isLeft.Name = "chk_isLeft"; this.label2.Size = new System.Drawing.Size(31, 13);
this.chk_isLeft.Size = new System.Drawing.Size(96, 17); this.label2.TabIndex = 1;
this.chk_isLeft.TabIndex = 1; this.label2.Text = "Type";
this.chk_isLeft.Text = "Left Joycon? "; //
this.chk_isLeft.UseVisualStyleBackColor = true; // chooseType
this.chk_isLeft.CheckedChanged += new System.EventHandler(this.chk_isLeft_CheckedChanged); //
// this.chooseType.FormattingEnabled = true;
// chk_isPro this.chooseType.Location = new System.Drawing.Point(47, 19);
// this.chooseType.Name = "chooseType";
this.chk_isPro.AutoSize = true; this.chooseType.Size = new System.Drawing.Size(97, 21);
this.chk_isPro.CheckAlign = System.Drawing.ContentAlignment.MiddleRight; this.chooseType.TabIndex = 0;
this.chk_isPro.Location = new System.Drawing.Point(6, 19); this.chooseType.SelectedValueChanged += new System.EventHandler(this.chooseType_SelectedValueChanged);
this.chk_isPro.Name = "chk_isPro"; //
this.chk_isPro.Size = new System.Drawing.Size(95, 17); // btn_applyAndClose
this.chk_isPro.TabIndex = 0; //
this.chk_isPro.Text = "Pro Controller?"; this.btn_applyAndClose.Location = new System.Drawing.Point(203, 229);
this.chk_isPro.UseVisualStyleBackColor = true; this.btn_applyAndClose.Name = "btn_applyAndClose";
this.chk_isPro.CheckedChanged += new System.EventHandler(this.chk_isPro_CheckedChanged); this.btn_applyAndClose.Size = new System.Drawing.Size(69, 23);
// this.btn_applyAndClose.TabIndex = 5;
// btn_applyAndClose this.btn_applyAndClose.Text = "Close";
// this.btn_applyAndClose.UseVisualStyleBackColor = true;
this.btn_applyAndClose.Location = new System.Drawing.Point(203, 229); this.btn_applyAndClose.Click += new System.EventHandler(this.btn_applyAndClose_Click);
this.btn_applyAndClose.Name = "btn_applyAndClose"; //
this.btn_applyAndClose.Size = new System.Drawing.Size(69, 23); // btn_apply
this.btn_applyAndClose.TabIndex = 5; //
this.btn_applyAndClose.Text = "Close"; this.btn_apply.Location = new System.Drawing.Point(121, 229);
this.btn_applyAndClose.UseVisualStyleBackColor = true; this.btn_apply.Name = "btn_apply";
this.btn_applyAndClose.Click += new System.EventHandler(this.btn_applyAndClose_Click); this.btn_apply.Size = new System.Drawing.Size(69, 23);
// this.btn_apply.TabIndex = 6;
// btn_apply this.btn_apply.Text = "Apply";
// this.btn_apply.UseVisualStyleBackColor = true;
this.btn_apply.Location = new System.Drawing.Point(121, 229); this.btn_apply.Click += new System.EventHandler(this.btn_apply_Click);
this.btn_apply.Name = "btn_apply"; //
this.btn_apply.Size = new System.Drawing.Size(69, 23); // lbl_all
this.btn_apply.TabIndex = 6; //
this.btn_apply.Text = "Apply"; this.lbl_all.AutoSize = true;
this.btn_apply.UseVisualStyleBackColor = true; this.lbl_all.Location = new System.Drawing.Point(12, 11);
this.btn_apply.Click += new System.EventHandler(this.btn_apply_Click); this.lbl_all.Name = "lbl_all";
// this.lbl_all.Size = new System.Drawing.Size(60, 13);
// lbl_all this.lbl_all.TabIndex = 7;
// this.lbl_all.Text = "All Devices";
this.lbl_all.AutoSize = true; //
this.lbl_all.Location = new System.Drawing.Point(12, 11); // label1
this.lbl_all.Name = "lbl_all"; //
this.lbl_all.Size = new System.Drawing.Size(60, 13); this.label1.AutoSize = true;
this.lbl_all.TabIndex = 7; this.label1.Location = new System.Drawing.Point(166, 11);
this.lbl_all.Text = "All Devices"; this.label1.Name = "label1";
// this.label1.Size = new System.Drawing.Size(91, 13);
// label1 this.label1.TabIndex = 8;
// this.label1.Text = "Switch Controllers";
this.label1.AutoSize = true; //
this.label1.Location = new System.Drawing.Point(166, 11); // btn_refresh
this.label1.Name = "label1"; //
this.label1.Size = new System.Drawing.Size(91, 13); this.btn_refresh.Location = new System.Drawing.Point(121, 56);
this.label1.TabIndex = 8; this.btn_refresh.Name = "btn_refresh";
this.label1.Text = "Switch Controllers"; this.btn_refresh.Size = new System.Drawing.Size(42, 50);
// this.btn_refresh.TabIndex = 9;
// btn_refresh this.btn_refresh.Text = "Re-\r\nfresh";
// this.btn_refresh.UseVisualStyleBackColor = true;
this.btn_refresh.Location = new System.Drawing.Point(121, 56); this.btn_refresh.Click += new System.EventHandler(this.btn_refresh_Click);
this.btn_refresh.Name = "btn_refresh"; //
this.btn_refresh.Size = new System.Drawing.Size(42, 50); // _3rdPartyControllers
this.btn_refresh.TabIndex = 9; //
this.btn_refresh.Text = "Re-\r\nfresh"; this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.btn_refresh.UseVisualStyleBackColor = true; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.btn_refresh.Click += new System.EventHandler(this.btn_refresh_Click); this.ClientSize = new System.Drawing.Size(284, 261);
// this.Controls.Add(this.btn_refresh);
// _3rdPartyControllers this.Controls.Add(this.label1);
// this.Controls.Add(this.lbl_all);
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.Controls.Add(this.btn_apply);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Controls.Add(this.btn_applyAndClose);
this.ClientSize = new System.Drawing.Size(284, 261); this.Controls.Add(this.group_props);
this.Controls.Add(this.btn_refresh); this.Controls.Add(this.btn_remove);
this.Controls.Add(this.label1); this.Controls.Add(this.btn_add);
this.Controls.Add(this.lbl_all); this.Controls.Add(this.list_customControllers);
this.Controls.Add(this.btn_apply); this.Controls.Add(this.list_allControllers);
this.Controls.Add(this.btn_applyAndClose); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Controls.Add(this.group_props); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Controls.Add(this.btn_remove); this.MaximizeBox = false;
this.Controls.Add(this.btn_add); this.MinimizeBox = false;
this.Controls.Add(this.list_customControllers); this.Name = "_3rdPartyControllers";
this.Controls.Add(this.list_allControllers); this.Text = "Add 3rd-Party Controllers";
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this._3rdPartyControllers_FormClosing);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.group_props.ResumeLayout(false);
this.MaximizeBox = false; this.group_props.PerformLayout();
this.MinimizeBox = false; this.ResumeLayout(false);
this.Name = "_3rdPartyControllers"; this.PerformLayout();
this.Text = "Add 3rd-Party Controllers";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this._3rdPartyControllers_FormClosing);
this.group_props.ResumeLayout(false);
this.group_props.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
} }
#endregion #endregion
private System.Windows.Forms.ListBox list_allControllers; private System.Windows.Forms.ListBox list_allControllers;
private System.Windows.Forms.ListBox list_customControllers; private System.Windows.Forms.ListBox list_customControllers;
private System.Windows.Forms.Button btn_add; private System.Windows.Forms.Button btn_add;
private System.Windows.Forms.Button btn_remove; private System.Windows.Forms.Button btn_remove;
private System.Windows.Forms.GroupBox group_props; private System.Windows.Forms.GroupBox group_props;
private System.Windows.Forms.Button btn_applyAndClose; private System.Windows.Forms.Button btn_applyAndClose;
private System.Windows.Forms.Button btn_apply; private System.Windows.Forms.Button btn_apply;
private System.Windows.Forms.Label lbl_all; private System.Windows.Forms.Label lbl_all;
private System.Windows.Forms.CheckBox chk_isPro; private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label1; private System.Windows.Forms.ToolTip tip_device;
private System.Windows.Forms.CheckBox chk_isLeft; private System.Windows.Forms.Button btn_refresh;
private System.Windows.Forms.ToolTip tip_device; private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button btn_refresh; private System.Windows.Forms.ComboBox chooseType;
} }
} }

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
using System.Drawing; using System.Drawing;
using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text; using System.Text;
@ -12,110 +13,179 @@ using System.Windows.Forms;
using static BetterJoyForCemu.HIDapi; using static BetterJoyForCemu.HIDapi;
namespace BetterJoyForCemu { namespace BetterJoyForCemu {
public partial class _3rdPartyControllers : Form { public partial class _3rdPartyControllers : Form {
public _3rdPartyControllers() { public class SController {
InitializeComponent(); public String name;
list_allControllers.DisplayMember = "Text"; public ushort product_id;
list_allControllers.ValueMember = "Value"; public ushort vendor_id;
list_customControllers.DisplayMember = "Text"; public byte type; // 1 is pro, 2 is left joy, 3 is right joy
list_customControllers.ValueMember = "Value";
RefreshControllerList(); public SController(String name, ushort vendor_id, ushort product_id, byte type) {
this.product_id = product_id; this.vendor_id = vendor_id; this.type = type;
this.name = name;
}
group_props.Controls.Add(chk_isLeft); public override bool Equals(object obj) {
group_props.Controls.Add(chk_isPro); //Check for null and compare run-time types.
group_props.Enabled = false; if ((obj == null) || !this.GetType().Equals(obj.GetType())) {
} return false;
} else {
SController s = (SController)obj;
return (s.product_id == product_id) && (s.vendor_id == vendor_id);
}
}
private bool ContainsText(ListBox a, String manu) { public override int GetHashCode() {
foreach (var v in a.Items) { return Tuple.Create(product_id, vendor_id).GetHashCode();
dynamic d = v as dynamic; }
if (d.Text == null)
continue;
if (d.Text.Equals(manu))
return true;
}
return false;
}
private void RefreshControllerList() { public override string ToString() {
list_allControllers.Items.Clear(); return name;
IntPtr ptr = HIDapi.hid_enumerate(0x0, 0x0); }
IntPtr top_ptr = ptr;
hid_device_info enumerate; // Add device to list public string Serialise() {
while (ptr != IntPtr.Zero) { return String.Format("{0}|{1}|{2}|{3}", name, product_id, vendor_id, type);
enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); }
}
if (!ContainsText(list_customControllers, enumerate.product_string) && !ContainsText(list_allControllers, enumerate.product_string)) const string PATH = "3rdPartyControllers";
list_allControllers.Items.Add(new { Text = enumerate.product_string, Value = enumerate });
ptr = enumerate.next; public _3rdPartyControllers() {
} InitializeComponent();
} /*list_allControllers.DisplayMember = "Text";
list_allControllers.ValueMember = "Value";
list_customControllers.DisplayMember = "Text";
list_customControllers.ValueMember = "Value";*/
list_allControllers.HorizontalScrollbar = true; list_customControllers.HorizontalScrollbar = true;
private void btn_add_Click(object sender, EventArgs e) { chooseType.Items.AddRange(new String[] { "Pro Controller", "Left Joycon", "Right Joycon" });
if (list_allControllers.SelectedItem != null) {
list_customControllers.Items.Add(list_allControllers.SelectedItem);
list_allControllers.Items.Remove(list_allControllers.SelectedItem);
list_allControllers.ClearSelected();
}
}
private void btn_remove_Click(object sender, EventArgs e) { chooseType.FormattingEnabled = true;
if (list_customControllers.SelectedItem != null) { group_props.Controls.Add(chooseType);
list_allControllers.Items.Add(list_customControllers.SelectedItem); group_props.Enabled = false;
list_customControllers.Items.Remove(list_customControllers.SelectedItem);
list_customControllers.ClearSelected();
}
}
private void chk_isPro_CheckedChanged(object sender, EventArgs e) { if (File.Exists(PATH)) {
using (StreamReader file = new StreamReader(PATH)) {
string line = String.Empty;
while ((line = file.ReadLine()) != null && (line != String.Empty)) {
String[] split = line.Split('|');
list_customControllers.Items.Add(new SController(split[0], ushort.Parse(split[1]), ushort.Parse(split[2]), byte.Parse(split[3])));
}
}
}
} CopyCustomControllers();
RefreshControllerList();
}
private void chk_isLeft_CheckedChanged(object sender, EventArgs e) { public void CopyCustomControllers() {
Program.thirdPartyCons.Clear();
foreach (SController v in list_customControllers.Items) {
Program.thirdPartyCons.Add(v);
}
}
} private bool ContainsText(ListBox a, String manu) {
foreach (SController v in a.Items) {
if (v == null)
continue;
if (v.name.Equals(manu))
return true;
}
return false;
}
private void btn_apply_Click(object sender, EventArgs e) { private void RefreshControllerList() {
list_allControllers.Items.Clear();
IntPtr ptr = HIDapi.hid_enumerate(0x0, 0x0);
} hid_device_info enumerate; // Add device to list
while (ptr != IntPtr.Zero) {
enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info));
private void btn_applyAndClose_Click(object sender, EventArgs e) { if (!ContainsText(list_customControllers, enumerate.product_string) && !ContainsText(list_allControllers, enumerate.product_string)) {
btn_apply_Click(sender, e); list_allControllers.Items.Add(new SController(enumerate.product_string, enumerate.vendor_id, enumerate.product_id, 0));
Close(); // 0 type is undefined
} }
private void _3rdPartyControllers_FormClosing(object sender, FormClosingEventArgs e) { ptr = enumerate.next;
btn_apply_Click(sender, e); }
} }
private void btn_refresh_Click(object sender, EventArgs e) { private void btn_add_Click(object sender, EventArgs e) {
RefreshControllerList(); if (list_allControllers.SelectedItem != null) {
} list_customControllers.Items.Add(list_allControllers.SelectedItem);
list_allControllers.Items.Remove(list_allControllers.SelectedItem);
private void list_allControllers_SelectedValueChanged(object sender, EventArgs e) { list_allControllers.ClearSelected();
if (list_allControllers.SelectedItem != null) }
tip_device.Show((list_allControllers.SelectedItem as dynamic).Text, list_allControllers); }
}
private void list_customControllers_SelectedValueChanged(object sender, EventArgs e) { private void btn_remove_Click(object sender, EventArgs e) {
if (list_customControllers.SelectedItem != null) { if (list_customControllers.SelectedItem != null) {
tip_device.Show((list_customControllers.SelectedItem as dynamic).Text, list_customControllers); list_allControllers.Items.Add(list_customControllers.SelectedItem);
group_props.Enabled = true; list_customControllers.Items.Remove(list_customControllers.SelectedItem);
} else
group_props.Enabled = false;
}
private void list_customControllers_MouseDown(object sender, MouseEventArgs e) { list_customControllers.ClearSelected();
if (e.Y > list_customControllers.ItemHeight * list_customControllers.Items.Count) }
list_customControllers.SelectedItems.Clear(); }
}
private void list_allControllers_MouseDown(object sender, MouseEventArgs e) { private void btn_apply_Click(object sender, EventArgs e) {
if (e.Y > list_allControllers.ItemHeight * list_allControllers.Items.Count) String sc = "";
list_allControllers.SelectedItems.Clear(); foreach (SController v in list_customControllers.Items) {
} sc += v.Serialise() + "\r\n";
} }
File.WriteAllText(PATH, sc);
CopyCustomControllers();
}
private void btn_applyAndClose_Click(object sender, EventArgs e) {
btn_apply_Click(sender, e);
Close();
}
private void _3rdPartyControllers_FormClosing(object sender, FormClosingEventArgs e) {
btn_apply_Click(sender, e);
}
private void btn_refresh_Click(object sender, EventArgs e) {
RefreshControllerList();
}
private void list_allControllers_SelectedValueChanged(object sender, EventArgs e) {
if (list_allControllers.SelectedItem != null)
tip_device.Show((list_allControllers.SelectedItem as SController).name, list_allControllers);
}
private void list_customControllers_SelectedValueChanged(object sender, EventArgs e) {
if (list_customControllers.SelectedItem != null) {
SController v = (list_customControllers.SelectedItem as SController);
tip_device.Show(v.name, list_customControllers);
chooseType.SelectedIndex = v.type - 1;
group_props.Enabled = true;
} else {
chooseType.SelectedIndex = -1;
group_props.Enabled = false;
}
}
private void list_customControllers_MouseDown(object sender, MouseEventArgs e) {
if (e.Y > list_customControllers.ItemHeight * list_customControllers.Items.Count)
list_customControllers.SelectedItems.Clear();
}
private void list_allControllers_MouseDown(object sender, MouseEventArgs e) {
if (e.Y > list_allControllers.ItemHeight * list_allControllers.Items.Count)
list_allControllers.SelectedItems.Clear();
}
private void chooseType_SelectedValueChanged(object sender, EventArgs e) {
if (list_customControllers.SelectedItem != null) {
SController v = (list_customControllers.SelectedItem as SController);
v.type = (byte)(chooseType.SelectedIndex + 1);
}
}
}
} }

View file

@ -26,7 +26,6 @@
this.components = new System.ComponentModel.Container(); this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.console = new System.Windows.Forms.TextBox(); this.console = new System.Windows.Forms.TextBox();
this.console_lbl = new System.Windows.Forms.Label();
this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components); this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenu = new System.Windows.Forms.ContextMenuStrip(this.components); this.contextMenu = new System.Windows.Forms.ContextMenuStrip(this.components);
this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
@ -68,16 +67,6 @@
this.console.Size = new System.Drawing.Size(262, 100); this.console.Size = new System.Drawing.Size(262, 100);
this.console.TabIndex = 2; this.console.TabIndex = 2;
// //
// console_lbl
//
this.console_lbl.AutoSize = true;
this.console_lbl.Location = new System.Drawing.Point(9, 116);
this.console_lbl.Name = "console_lbl";
this.console_lbl.Size = new System.Drawing.Size(80, 13);
this.console_lbl.TabIndex = 1;
this.console_lbl.Text = "Console Output";
this.console_lbl.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// notifyIcon // notifyIcon
// //
this.notifyIcon.BalloonTipText = "Double click the tray icon to maximise"; this.notifyIcon.BalloonTipText = "Double click the tray icon to maximise";
@ -265,7 +254,7 @@
// //
// btn_open3rdP // btn_open3rdP
// //
this.btn_open3rdP.Location = new System.Drawing.Point(188, 112); this.btn_open3rdP.Location = new System.Drawing.Point(93, 112);
this.btn_open3rdP.Name = "btn_open3rdP"; this.btn_open3rdP.Name = "btn_open3rdP";
this.btn_open3rdP.Size = new System.Drawing.Size(86, 20); this.btn_open3rdP.Size = new System.Drawing.Size(86, 20);
this.btn_open3rdP.TabIndex = 7; this.btn_open3rdP.TabIndex = 7;
@ -337,7 +326,7 @@
// //
// btn_reassign_open // btn_reassign_open
// //
this.btn_reassign_open.Location = new System.Drawing.Point(107, 112); this.btn_reassign_open.Location = new System.Drawing.Point(12, 112);
this.btn_reassign_open.Name = "btn_reassign_open"; this.btn_reassign_open.Name = "btn_reassign_open";
this.btn_reassign_open.Size = new System.Drawing.Size(75, 20); this.btn_reassign_open.Size = new System.Drawing.Size(75, 20);
this.btn_reassign_open.TabIndex = 13; this.btn_reassign_open.TabIndex = 13;
@ -362,7 +351,6 @@
this.Controls.Add(this.donationLink); this.Controls.Add(this.donationLink);
this.Controls.Add(this.passiveScanBox); this.Controls.Add(this.passiveScanBox);
this.Controls.Add(this.version_lbl); this.Controls.Add(this.version_lbl);
this.Controls.Add(this.console_lbl);
this.Controls.Add(this.console); this.Controls.Add(this.console);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
@ -384,7 +372,6 @@
#endregion #endregion
public System.Windows.Forms.TextBox console; public System.Windows.Forms.TextBox console;
private System.Windows.Forms.Label console_lbl;
private System.Windows.Forms.NotifyIcon notifyIcon; private System.Windows.Forms.NotifyIcon notifyIcon;
private System.Windows.Forms.Label version_lbl; private System.Windows.Forms.Label version_lbl;
private System.Windows.Forms.ContextMenuStrip contextMenu; private System.Windows.Forms.ContextMenuStrip contextMenu;

View file

@ -14,423 +14,420 @@ using System.Windows.Forms;
using System.Xml.Linq; using System.Xml.Linq;
namespace BetterJoyForCemu { namespace BetterJoyForCemu {
public partial class MainForm : Form { public partial class MainForm : Form {
public bool nonOriginal = Boolean.Parse(ConfigurationManager.AppSettings["NonOriginalController"]); public bool nonOriginal = Boolean.Parse(ConfigurationManager.AppSettings["NonOriginalController"]);
public List<Button> con, loc; public List<Button> con, loc;
public bool calibrate; public bool calibrate;
public List<KeyValuePair<string, float[]>> caliData; public List<KeyValuePair<string, float[]>> caliData;
private Timer countDown; private Timer countDown;
private int count; private int count;
public List<int> xG, yG, zG, xA, yA, zA; public List<int> xG, yG, zG, xA, yA, zA;
public MainForm() { public MainForm() {
xG = new List<int>(); yG = new List<int>(); zG = new List<int>(); xG = new List<int>(); yG = new List<int>(); zG = new List<int>();
xA = new List<int>(); yA = new List<int>(); zA = new List<int>(); xA = new List<int>(); yA = new List<int>(); zA = new List<int>();
caliData = new List<KeyValuePair<string, float[]>> { caliData = new List<KeyValuePair<string, float[]>> {
new KeyValuePair<string, float[]>("0", new float[6] {0,0,0,-710,0,0}) new KeyValuePair<string, float[]>("0", new float[6] {0,0,0,-710,0,0})
}; };
InitializeComponent(); InitializeComponent();
if (!nonOriginal) if (!nonOriginal)
AutoCalibrate.Hide(); AutoCalibrate.Hide();
// Feature not yet implemented - hide con = new List<Button> { con1, con2, con3, con4 };
btn_open3rdP.Hide(); loc = new List<Button> { loc1, loc2, loc3, loc4 };
con = new List<Button> { con1, con2, con3, con4 }; //list all options
loc = new List<Button> { loc1, loc2, loc3, loc4 }; string[] myConfigs = ConfigurationManager.AppSettings.AllKeys;
Size childSize = new Size(87, 20);
for (int i = 0; i != myConfigs.Length; i++) {
settingsTable.RowCount++;
settingsTable.Controls.Add(new Label() { Text = myConfigs[i], TextAlign = ContentAlignment.BottomLeft, AutoEllipsis = true, Size = childSize }, 0, i);
//list all options var value = ConfigurationManager.AppSettings[myConfigs[i]];
string[] myConfigs = ConfigurationManager.AppSettings.AllKeys; Control childControl;
Size childSize = new Size(87, 20); if (value == "true" || value == "false") {
for (int i = 0; i != myConfigs.Length; i++) { childControl = new CheckBox() { Checked = Boolean.Parse(value), Size = childSize };
settingsTable.RowCount++; } else {
settingsTable.Controls.Add(new Label() { Text = myConfigs[i], TextAlign = ContentAlignment.BottomLeft, AutoEllipsis = true, Size = childSize }, 0, i); childControl = new TextBox() { Text = value, Size = childSize };
}
var value = ConfigurationManager.AppSettings[myConfigs[i]]; childControl.MouseClick += cbBox_Changed;
Control childControl; settingsTable.Controls.Add(childControl, 1, i);
if (value == "true" || value == "false") { }
childControl = new CheckBox() { Checked = Boolean.Parse(value), Size = childSize }; }
} else {
childControl = new TextBox() { Text = value, Size = childSize };
}
childControl.MouseClick += cbBox_Changed; private void HideToTray() {
settingsTable.Controls.Add(childControl, 1, i); this.WindowState = FormWindowState.Minimized;
} notifyIcon.Visible = true;
} notifyIcon.ShowBalloonTip(1);
this.ShowInTaskbar = false;
this.Hide();
}
private void HideToTray() { private void ShowFromTray() {
this.WindowState = FormWindowState.Minimized; this.Show();
notifyIcon.Visible = true; this.WindowState = FormWindowState.Normal;
notifyIcon.ShowBalloonTip(1); this.ShowInTaskbar = true;
this.ShowInTaskbar = false; this.FormBorderStyle = FormBorderStyle.FixedSingle;
this.Hide(); this.Icon = Properties.Resources.betterjoyforcemu_icon;
} notifyIcon.Visible = false;
}
private void ShowFromTray() { private void MainForm_Resize(object sender, EventArgs e) {
this.Show(); if (this.WindowState == FormWindowState.Minimized) {
this.WindowState = FormWindowState.Normal; HideToTray();
this.ShowInTaskbar = true; }
this.FormBorderStyle = FormBorderStyle.FixedSingle; }
this.Icon = Properties.Resources.betterjoyforcemu_icon;
notifyIcon.Visible = false;
}
private void MainForm_Resize(object sender, EventArgs e) { private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) {
if (this.WindowState == FormWindowState.Minimized) { ShowFromTray();
HideToTray(); }
}
}
private void notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) { private void MainForm_Load(object sender, EventArgs e) {
ShowFromTray(); Config.Init(caliData);
}
private void MainForm_Load(object sender, EventArgs e) { Program.Start();
Config.Init(caliData);
Program.Start(); passiveScanBox.Checked = Config.IntValue("ProgressiveScan") == 1;
startInTrayBox.Checked = Config.IntValue("StartInTray") == 1;
passiveScanBox.Checked = Config.IntValue("ProgressiveScan") == 1; if (Config.IntValue("StartInTray") == 1) {
startInTrayBox.Checked = Config.IntValue("StartInTray") == 1; HideToTray();
} else {
ShowFromTray();
}
}
if (Config.IntValue("StartInTray") == 1) { private void MainForm_FormClosing(object sender, FormClosingEventArgs e) {
HideToTray(); try {
} else { Program.Stop();
ShowFromTray(); Environment.Exit(0);
} } catch { }
} }
private void MainForm_FormClosing(object sender, FormClosingEventArgs e) { private void exitToolStripMenuItem_Click(object sender, EventArgs e) { // this does not work, for some reason. Fix before release
try { try {
Program.Stop(); Program.Stop();
Environment.Exit(0); Close();
} catch { } Environment.Exit(0);
} } catch { }
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e) { // this does not work, for some reason. Fix before release private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
try { donationLink.LinkVisited = true;
Program.Stop(); System.Diagnostics.Process.Start("http://paypal.me/DavidKhachaturov/5");
Close(); }
Environment.Exit(0);
} catch { }
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { private void passiveScanBox_CheckedChanged(object sender, EventArgs e) {
donationLink.LinkVisited = true; Config.SetValue("ProgressiveScan", passiveScanBox.Checked ? "1" : "0");
System.Diagnostics.Process.Start("http://paypal.me/DavidKhachaturov/5"); Config.Save();
} }
private void passiveScanBox_CheckedChanged(object sender, EventArgs e) { public void AppendTextBox(string value) { // https://stackoverflow.com/questions/519233/writing-to-a-textbox-from-another-thread
Config.SetValue("ProgressiveScan", passiveScanBox.Checked ? "1" : "0"); if (InvokeRequired) {
Config.Save(); this.Invoke(new Action<string>(AppendTextBox), new object[] { value });
} return;
}
console.AppendText(value);
}
public void AppendTextBox(string value) { // https://stackoverflow.com/questions/519233/writing-to-a-textbox-from-another-thread bool toRumble = Boolean.Parse(ConfigurationManager.AppSettings["EnableRumble"]);
if (InvokeRequired) { bool showAsXInput = Boolean.Parse(ConfigurationManager.AppSettings["ShowAsXInput"]);
this.Invoke(new Action<string>(AppendTextBox), new object[] { value }); bool showAsDS4 = Boolean.Parse(ConfigurationManager.AppSettings["ShowAsDS4"]);
return;
}
console.AppendText(value);
}
bool toRumble = Boolean.Parse(ConfigurationManager.AppSettings["EnableRumble"]); public void locBtnClick(object sender, EventArgs e) {
bool showAsXInput = Boolean.Parse(ConfigurationManager.AppSettings["ShowAsXInput"]); Button bb = sender as Button;
bool showAsDS4 = Boolean.Parse(ConfigurationManager.AppSettings["ShowAsDS4"]);
public void locBtnClick(object sender, EventArgs e) { if (bb.Tag.GetType() == typeof(Button)) {
Button bb = sender as Button; Button button = bb.Tag as Button;
if (bb.Tag.GetType() == typeof(Button)) { if (button.Tag.GetType() == typeof(Joycon)) {
Button button = bb.Tag as Button; Joycon v = (Joycon)button.Tag;
v.SetRumble(20.0f, 400.0f, 1.0f, 300);
}
}
}
if (button.Tag.GetType() == typeof(Joycon)) { public void conBtnClick(object sender, EventArgs e) {
Joycon v = (Joycon)button.Tag; Button button = sender as Button;
v.SetRumble(20.0f, 400.0f, 1.0f, 300);
}
}
}
public void conBtnClick(object sender, EventArgs e) { if (button.Tag.GetType() == typeof(Joycon)) {
Button button = sender as Button; Joycon v = (Joycon)button.Tag;
if (button.Tag.GetType() == typeof(Joycon)) { if (v.other == null && !v.isPro) { // needs connecting to other joycon (so messy omg)
Joycon v = (Joycon)button.Tag; bool succ = false;
if (v.other == null && !v.isPro) { // needs connecting to other joycon (so messy omg) if (Program.mgr.j.Count == 1) { // when want to have a single joycon in vertical mode
bool succ = false; v.other = v; // hacky; implement check in Joycon.cs to account for this
succ = true;
} else {
foreach (Joycon jc in Program.mgr.j) {
if (!jc.isPro && jc.isLeft != v.isLeft && jc != v && jc.other == null) {
v.other = jc;
jc.other = v;
if (Program.mgr.j.Count == 1) { // when want to have a single joycon in vertical mode //Set both Joycon LEDs to the one with the lowest ID
v.other = v; // hacky; implement check in Joycon.cs to account for this byte led = jc.LED <= v.LED ? jc.LED : v.LED;
succ = true; jc.LED = led;
} else { v.LED = led;
foreach (Joycon jc in Program.mgr.j) { jc.SetPlayerLED(led);
if (!jc.isPro && jc.isLeft != v.isLeft && jc != v && jc.other == null) { v.SetPlayerLED(led);
v.other = jc;
jc.other = v;
//Set both Joycon LEDs to the one with the lowest ID if (v.out_xbox != null) {
byte led = jc.LED <= v.LED ? jc.LED : v.LED; v.out_xbox.Disconnect();
jc.LED = led; v.out_xbox = null;
v.LED = led; }
jc.SetPlayerLED(led);
v.SetPlayerLED(led);
if (v.out_xbox != null) { if (v.out_ds4 != null) {
v.out_xbox.Disconnect(); v.out_ds4.Disconnect();
v.out_xbox = null; v.out_ds4 = null;
} }
if (v.out_ds4 != null) { // setting the other joycon's button image
v.out_ds4.Disconnect(); foreach (Button b in con)
v.out_ds4 = null; if (b.Tag == jc)
} b.BackgroundImage = jc.isLeft ? Properties.Resources.jc_left : Properties.Resources.jc_right;
// setting the other joycon's button image succ = true;
foreach (Button b in con) break;
if (b.Tag == jc) }
b.BackgroundImage = jc.isLeft ? Properties.Resources.jc_left : Properties.Resources.jc_right; }
}
succ = true; if (succ)
break; foreach (Button b in con)
} if (b.Tag == v)
} b.BackgroundImage = v.isLeft ? Properties.Resources.jc_left : Properties.Resources.jc_right;
} } else if (v.other != null && !v.isPro) { // needs disconnecting from other joycon
ReenableViGEm(v);
ReenableViGEm(v.other);
if (succ) button.BackgroundImage = v.isLeft ? Properties.Resources.jc_left_s : Properties.Resources.jc_right_s;
foreach (Button b in con)
if (b.Tag == v)
b.BackgroundImage = v.isLeft ? Properties.Resources.jc_left : Properties.Resources.jc_right;
} else if (v.other != null && !v.isPro) { // needs disconnecting from other joycon
ReenableViGEm(v);
ReenableViGEm(v.other);
button.BackgroundImage = v.isLeft ? Properties.Resources.jc_left_s : Properties.Resources.jc_right_s; foreach (Button b in con)
if (b.Tag == v.other)
b.BackgroundImage = v.other.isLeft ? Properties.Resources.jc_left_s : Properties.Resources.jc_right_s;
foreach (Button b in con) //Set original Joycon LEDs
if (b.Tag == v.other) v.other.LED = (byte)(0x1 << v.other.PadId);
b.BackgroundImage = v.other.isLeft ? Properties.Resources.jc_left_s : Properties.Resources.jc_right_s; v.LED = (byte)(0x1 << v.PadId);
v.other.SetPlayerLED(v.other.LED);
v.SetPlayerLED(v.LED);
//Set original Joycon LEDs v.other.other = null;
v.other.LED = (byte)(0x1 << v.other.PadId); v.other = null;
v.LED = (byte)(0x1 << v.PadId); }
v.other.SetPlayerLED(v.other.LED); }
v.SetPlayerLED(v.LED); }
v.other.other = null; private void startInTrayBox_CheckedChanged(object sender, EventArgs e) {
v.other = null; Config.SetValue("StartInTray", startInTrayBox.Checked ? "1" : "0");
} Config.Save();
} }
}
private void startInTrayBox_CheckedChanged(object sender, EventArgs e) { private void btn_open3rdP_Click(object sender, EventArgs e) {
Config.SetValue("StartInTray", startInTrayBox.Checked ? "1" : "0"); _3rdPartyControllers partyForm = new _3rdPartyControllers();
Config.Save(); partyForm.ShowDialog();
} }
private void btn_open3rdP_Click(object sender, EventArgs e) { private void settingsApply_Click(object sender, EventArgs e) {
_3rdPartyControllers partyForm = new _3rdPartyControllers(); var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
partyForm.ShowDialog(); var settings = configFile.AppSettings.Settings;
}
private void settingsApply_Click(object sender, EventArgs e) { for (int row = 0; row < ConfigurationManager.AppSettings.AllKeys.Length; row++) {
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); var valCtl = settingsTable.GetControlFromPosition(1, row);
var settings = configFile.AppSettings.Settings; var KeyCtl = settingsTable.GetControlFromPosition(0, row).Text;
for (int row = 0; row < ConfigurationManager.AppSettings.AllKeys.Length; row++) { if (valCtl.GetType() == typeof(CheckBox) && settings[KeyCtl] != null) {
var valCtl = settingsTable.GetControlFromPosition(1, row); settings[KeyCtl].Value = ((CheckBox)valCtl).Checked.ToString().ToLower();
var KeyCtl = settingsTable.GetControlFromPosition(0, row).Text; } else if (valCtl.GetType() == typeof(TextBox) && settings[KeyCtl] != null) {
settings[KeyCtl].Value = ((TextBox)valCtl).Text.ToLower();
}
}
if (valCtl.GetType() == typeof(CheckBox) && settings[KeyCtl] != null) { try {
settings[KeyCtl].Value = ((CheckBox)valCtl).Checked.ToString().ToLower(); configFile.Save(ConfigurationSaveMode.Modified);
} else if (valCtl.GetType() == typeof(TextBox) && settings[KeyCtl] != null) { } catch (ConfigurationErrorsException) {
settings[KeyCtl].Value = ((TextBox)valCtl).Text.ToLower(); AppendTextBox("Error writing app settings.\r\n");
} }
}
try { ConfigurationManager.AppSettings["AutoPowerOff"] = "false"; // Prevent joycons poweroff when applying settings
configFile.Save(ConfigurationSaveMode.Modified); Application.Restart();
} catch (ConfigurationErrorsException) { Environment.Exit(0);
AppendTextBox("Error writing app settings.\r\n"); }
}
ConfigurationManager.AppSettings["AutoPowerOff"] = "false"; // Prevent joycons poweroff when applying settings void ReenableViGEm(Joycon v) {
Application.Restart(); if (showAsXInput && v.out_xbox == null) {
Environment.Exit(0); v.out_xbox = new Controller.OutputControllerXbox360();
}
void ReenableViGEm(Joycon v) { if (toRumble)
if (showAsXInput && v.out_xbox == null) { v.out_xbox.FeedbackReceived += v.ReceiveRumble;
v.out_xbox = new Controller.OutputControllerXbox360(); v.out_xbox.Connect();
}
if (toRumble) if (showAsDS4 && v.out_ds4 == null) {
v.out_xbox.FeedbackReceived += v.ReceiveRumble; v.out_ds4 = new Controller.OutputControllerDualShock4();
v.out_xbox.Connect();
}
if (showAsDS4 && v.out_ds4 == null) { if (toRumble)
v.out_ds4 = new Controller.OutputControllerDualShock4(); v.out_ds4.FeedbackReceived += v.Ds4_FeedbackReceived;
v.out_ds4.Connect();
}
}
if (toRumble) private void foldLbl_Click(object sender, EventArgs e) {
v.out_ds4.FeedbackReceived += v.Ds4_FeedbackReceived; rightPanel.Visible = !rightPanel.Visible;
v.out_ds4.Connect(); foldLbl.Text = rightPanel.Visible ? "<" : ">";
} }
}
private void foldLbl_Click(object sender, EventArgs e) { private void cbBox_Changed(object sender, EventArgs e) {
rightPanel.Visible = !rightPanel.Visible; var coord = settingsTable.GetPositionFromControl(sender as Control);
foldLbl.Text = rightPanel.Visible ? "<" : ">";
}
private void cbBox_Changed(object sender, EventArgs e) { var valCtl = settingsTable.GetControlFromPosition(coord.Column, coord.Row);
var coord = settingsTable.GetPositionFromControl(sender as Control); var KeyCtl = settingsTable.GetControlFromPosition(coord.Column - 1, coord.Row).Text;
var valCtl = settingsTable.GetControlFromPosition(coord.Column, coord.Row); try {
var KeyCtl = settingsTable.GetControlFromPosition(coord.Column - 1, coord.Row).Text; var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var settings = configFile.AppSettings.Settings;
if (valCtl.GetType() == typeof(CheckBox) && settings[KeyCtl] != null) {
settings[KeyCtl].Value = ((CheckBox)valCtl).Checked.ToString().ToLower();
} else if (valCtl.GetType() == typeof(TextBox) && settings[KeyCtl] != null) {
settings[KeyCtl].Value = ((TextBox)valCtl).Text.ToLower();
}
try { if (KeyCtl == "HomeLEDOn") {
var configFile = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); bool on = settings[KeyCtl].Value.ToLower() == "true";
var settings = configFile.AppSettings.Settings; foreach (Joycon j in Program.mgr.j) {
if (valCtl.GetType() == typeof(CheckBox) && settings[KeyCtl] != null) { j.SetHomeLight(on);
settings[KeyCtl].Value = ((CheckBox)valCtl).Checked.ToString().ToLower(); }
} else if (valCtl.GetType() == typeof(TextBox) && settings[KeyCtl] != null) { }
settings[KeyCtl].Value = ((TextBox)valCtl).Text.ToLower();
}
if (KeyCtl == "HomeLEDOn") { configFile.Save(ConfigurationSaveMode.Modified);
bool on = settings[KeyCtl].Value.ToLower() == "true"; ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name);
foreach (Joycon j in Program.mgr.j) { } catch (ConfigurationErrorsException) {
j.SetHomeLight(on); AppendTextBox("Error writing app settings\r\n");
} Trace.WriteLine(String.Format("rw {0}, column {1}, {2}, {3}", coord.Row, coord.Column, sender.GetType(), KeyCtl));
} }
}
private void StartCalibrate(object sender, EventArgs e) {
if (Program.mgr.j.Count == 0) {
this.console.Text = "Please connect a single pro controller.";
return;
}
if (Program.mgr.j.Count > 1) {
this.console.Text = "Please calibrate one controller at a time (disconnect others).";
return;
}
this.AutoCalibrate.Enabled = false;
countDown = new Timer();
this.count = 4;
this.CountDown(null, null);
countDown.Tick += new EventHandler(CountDown);
countDown.Interval = 1000;
countDown.Enabled = true;
}
configFile.Save(ConfigurationSaveMode.Modified); private void StartGetData() {
ConfigurationManager.RefreshSection(configFile.AppSettings.SectionInformation.Name); this.xG.Clear(); this.yG.Clear(); this.zG.Clear();
} catch (ConfigurationErrorsException) { this.xA.Clear(); this.yA.Clear(); this.zA.Clear();
AppendTextBox("Error writing app settings\r\n"); countDown = new Timer();
Trace.WriteLine(String.Format("rw {0}, column {1}, {2}, {3}", coord.Row, coord.Column, sender.GetType(), KeyCtl)); this.count = 3;
} this.calibrate = true;
} countDown.Tick += new EventHandler(CalcData);
private void StartCalibrate(object sender, EventArgs e) { countDown.Interval = 1000;
if (Program.mgr.j.Count == 0) { countDown.Enabled = true;
this.console.Text = "Please connect a single pro controller."; }
return;
}
if (Program.mgr.j.Count > 1) {
this.console.Text = "Please calibrate one controller at a time (disconnect others).";
return;
}
this.AutoCalibrate.Enabled = false;
countDown = new Timer();
this.count = 4;
this.CountDown(null, null);
countDown.Tick += new EventHandler(CountDown);
countDown.Interval = 1000;
countDown.Enabled = true;
}
private void StartGetData() { private void btn_reassign_open_Click(object sender, EventArgs e) {
this.xG.Clear(); this.yG.Clear(); this.zG.Clear(); Reassign mapForm = new Reassign();
this.xA.Clear(); this.yA.Clear(); this.zA.Clear(); mapForm.ShowDialog();
countDown = new Timer(); }
this.count = 3;
this.calibrate = true;
countDown.Tick += new EventHandler(CalcData);
countDown.Interval = 1000;
countDown.Enabled = true;
}
private void btn_reassign_open_Click(object sender, EventArgs e) { private void CountDown(object sender, EventArgs e) {
Reassign mapForm = new Reassign(); if (this.count == 0) {
mapForm.ShowDialog(); this.console.Text = "Calibrating...";
} countDown.Stop();
this.StartGetData();
} else {
this.console.Text = "Plese keep the controller flat." + "\r\n";
this.console.Text += "Calibration will start in " + this.count + " seconds.";
this.count--;
}
}
private void CalcData(object sender, EventArgs e) {
if (this.count == 0) {
countDown.Stop();
this.calibrate = false;
string serNum = Program.mgr.j.First().serial_number;
int serIndex = this.findSer(serNum);
float[] Arr = new float[6] { 0, 0, 0, 0, 0, 0 };
if (serIndex == -1) {
this.caliData.Add(new KeyValuePair<string, float[]>(
serNum,
Arr
));
} else {
Arr = this.caliData[serIndex].Value;
}
Random rnd = new Random();
Arr[0] = (float)quickselect_median(this.xG, rnd.Next);
Arr[1] = (float)quickselect_median(this.yG, rnd.Next);
Arr[2] = (float)quickselect_median(this.zG, rnd.Next);
Arr[3] = (float)quickselect_median(this.xA, rnd.Next);
Arr[4] = (float)quickselect_median(this.yA, rnd.Next);
Arr[5] = (float)quickselect_median(this.zA, rnd.Next) - 4010; //Joycon.cs acc_sen 16384
this.console.Text += "Calibration completed!!!" + "\r\n";
Config.SaveCaliData(this.caliData);
Program.mgr.j.First().getActiveData();
this.AutoCalibrate.Enabled = true;
} else {
this.count--;
}
private void CountDown(object sender, EventArgs e) { }
if (this.count == 0) { private double quickselect_median(List<int> l, Func<int, int> pivot_fn) {
this.console.Text = "Calibrating..."; int ll = l.Count;
countDown.Stop(); if (ll % 2 == 1) {
this.StartGetData(); return this.quickselect(l, ll / 2, pivot_fn);
} else { } else {
this.console.Text = "Plese keep the controller flat." + "\r\n"; return 0.5 * (quickselect(l, ll / 2 - 1, pivot_fn) + quickselect(l, ll / 2, pivot_fn));
this.console.Text += "Calibration will start in " + this.count + " seconds."; }
this.count--; }
}
}
private void CalcData(object sender, EventArgs e) {
if (this.count == 0) {
countDown.Stop();
this.calibrate = false;
string serNum = Program.mgr.j.First().serial_number;
int serIndex = this.findSer(serNum);
float[] Arr = new float[6] { 0, 0, 0, 0, 0, 0 };
if (serIndex == -1) {
this.caliData.Add(new KeyValuePair<string, float[]>(
serNum,
Arr
));
} else {
Arr = this.caliData[serIndex].Value;
}
Random rnd = new Random();
Arr[0] = (float)quickselect_median(this.xG, rnd.Next);
Arr[1] = (float)quickselect_median(this.yG, rnd.Next);
Arr[2] = (float)quickselect_median(this.zG, rnd.Next);
Arr[3] = (float)quickselect_median(this.xA, rnd.Next);
Arr[4] = (float)quickselect_median(this.yA, rnd.Next);
Arr[5] = (float)quickselect_median(this.zA, rnd.Next) - 4010; //Joycon.cs acc_sen 16384
this.console.Text += "Calibration completed!!!" + "\r\n";
Config.SaveCaliData(this.caliData);
Program.mgr.j.First().getActiveData();
this.AutoCalibrate.Enabled = true;
} else {
this.count--;
}
} private int quickselect(List<int> l, int k, Func<int, int> pivot_fn) {
private double quickselect_median(List<int> l, Func<int, int> pivot_fn) { if (l.Count == 1 && k == 0) {
int ll = l.Count; return l[0];
if (ll % 2 == 1) { }
return this.quickselect(l, ll / 2, pivot_fn); int pivot = l[pivot_fn(l.Count)];
} else { List<int> lows = l.Where(x => x < pivot).ToList();
return 0.5 * (quickselect(l, ll / 2 - 1, pivot_fn) + quickselect(l, ll / 2, pivot_fn)); List<int> highs = l.Where(x => x > pivot).ToList();
} List<int> pivots = l.Where(x => x == pivot).ToList();
} if (k < lows.Count) {
return quickselect(lows, k, pivot_fn);
} else if (k < (lows.Count + pivots.Count)) {
return pivots[0];
} else {
return quickselect(highs, k - lows.Count - pivots.Count, pivot_fn);
}
}
private int quickselect(List<int> l, int k, Func<int, int> pivot_fn) { public float[] activeCaliData(string serNum) {
if (l.Count == 1 && k == 0) { for (int i = 0; i < this.caliData.Count; i++) {
return l[0]; if (this.caliData[i].Key == serNum) {
} return this.caliData[i].Value;
int pivot = l[pivot_fn(l.Count)]; }
List<int> lows = l.Where(x => x < pivot).ToList(); }
List<int> highs = l.Where(x => x > pivot).ToList(); return this.caliData[0].Value;
List<int> pivots = l.Where(x => x == pivot).ToList(); }
if (k < lows.Count) {
return quickselect(lows, k, pivot_fn);
} else if (k < (lows.Count + pivots.Count)) {
return pivots[0];
} else {
return quickselect(highs, k - lows.Count - pivots.Count, pivot_fn);
}
}
public float[] activeCaliData(string serNum) { private int findSer(string serNum) {
for (int i = 0; i < this.caliData.Count; i++) { for (int i = 0; i < this.caliData.Count; i++) {
if (this.caliData[i].Key == serNum) { if (this.caliData[i].Key == serNum) {
return this.caliData[i].Value; return i;
} }
} }
return this.caliData[0].Value; return -1;
} }
}
private int findSer(string serNum) {
for (int i = 0; i < this.caliData.Count; i++) {
if (this.caliData[i].Key == serNum) {
return i;
}
}
return -1;
}
}
} }

View file

@ -15,6 +15,7 @@ using System.Timers;
using System.Web.Configuration; using System.Web.Configuration;
using System.Windows.Forms; using System.Windows.Forms;
using Nefarius.ViGEm.Client; using Nefarius.ViGEm.Client;
using static BetterJoyForCemu._3rdPartyControllers;
using static BetterJoyForCemu.HIDapi; using static BetterJoyForCemu.HIDapi;
namespace BetterJoyForCemu { namespace BetterJoyForCemu {
@ -95,15 +96,28 @@ namespace BetterJoyForCemu {
} }
} }
private ushort TypeToProdId(byte type) {
switch (type) {
case 1:
return product_pro;
case 2:
return product_l;
case 3:
return product_r;
}
return 0;
}
public void CheckForNewControllers() { public void CheckForNewControllers() {
// move all code for initializing devices here and well as the initial code from Start() // move all code for initializing devices here and well as the initial code from Start()
bool isLeft = false; bool isLeft = false;
IntPtr ptr = HIDapi.hid_enumerate(vendor_id, 0x0); IntPtr ptr = HIDapi.hid_enumerate(0x0, 0x0);
IntPtr top_ptr = ptr; IntPtr top_ptr = ptr;
hid_device_info enumerate; // Add device to list hid_device_info enumerate; // Add device to list
bool foundNew = false; bool foundNew = false;
while (ptr != IntPtr.Zero) { while (ptr != IntPtr.Zero) {
SController thirdParty = null;
enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info)); enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info));
if (enumerate.serial_number == null) { if (enumerate.serial_number == null) {
@ -117,9 +131,21 @@ namespace BetterJoyForCemu {
} }
bool validController = (enumerate.product_id == product_l || enumerate.product_id == product_r || bool validController = (enumerate.product_id == product_l || enumerate.product_id == product_r ||
enumerate.product_id == product_pro || enumerate.product_id == product_snes); enumerate.product_id == product_pro || enumerate.product_id == product_snes) && enumerate.vendor_id == vendor_id;
// check list of custom controllers specified
foreach (SController v in Program.thirdPartyCons) {
if (enumerate.vendor_id == v.vendor_id && enumerate.product_id == v.product_id) {
validController = true;
thirdParty = v;
break;
}
}
ushort prod_id = thirdParty == null ? enumerate.product_id : TypeToProdId(thirdParty.type);
if (prod_id == 0)
continue; // controller was not assigned a type
if (validController && !ControllerAlreadyAdded(enumerate.path)) { if (validController && !ControllerAlreadyAdded(enumerate.path)) {
switch (enumerate.product_id) { switch (prod_id) {
case product_l: case product_l:
isLeft = true; isLeft = true;
form.AppendTextBox("Left Joy-Con connected.\r\n"); break; form.AppendTextBox("Left Joy-Con connected.\r\n"); break;
@ -166,8 +192,8 @@ namespace BetterJoyForCemu {
break; break;
} }
bool isPro = enumerate.product_id == product_pro; bool isPro = prod_id == product_pro;
bool isSnes = enumerate.product_id == product_snes; bool isSnes = prod_id == product_snes;
j.Add(new Joycon(handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft, enumerate.path, enumerate.serial_number, j.Count, isPro, isSnes)); j.Add(new Joycon(handle, EnableIMU, EnableLocalize & EnableIMU, 0.05f, isLeft, enumerate.path, enumerate.serial_number, j.Count, isPro, isSnes));
foundNew = true; foundNew = true;
@ -179,7 +205,7 @@ namespace BetterJoyForCemu {
ii++; ii++;
if (!v.Enabled) { if (!v.Enabled) {
System.Drawing.Bitmap temp; System.Drawing.Bitmap temp;
switch (enumerate.product_id) { switch (prod_id) {
case (product_l): case (product_l):
temp = Properties.Resources.jc_left_s; break; temp = Properties.Resources.jc_left_s; break;
case (product_r): case (product_r):
@ -210,8 +236,12 @@ namespace BetterJoyForCemu {
} }
byte[] mac = new byte[6]; byte[] mac = new byte[6];
for (int n = 0; n < 6; n++) try {
mac[n] = byte.Parse(enumerate.serial_number.Substring(n * 2, 2), System.Globalization.NumberStyles.HexNumber); for (int n = 0; n < 6; n++)
mac[n] = byte.Parse(enumerate.serial_number.Substring(n * 2, 2), System.Globalization.NumberStyles.HexNumber);
} catch (Exception e) {
// could not parse mac address
}
j[j.Count - 1].PadMacAddress = new PhysicalAddress(mac); j[j.Count - 1].PadMacAddress = new PhysicalAddress(mac);
} }
@ -330,6 +360,8 @@ namespace BetterJoyForCemu {
static public bool useHIDG = Boolean.Parse(ConfigurationManager.AppSettings["UseHIDG"]); static public bool useHIDG = Boolean.Parse(ConfigurationManager.AppSettings["UseHIDG"]);
public static List<SController> thirdPartyCons = new List<SController>();
private static WindowsInput.Events.Sources.IKeyboardEventSource keyboard; private static WindowsInput.Events.Sources.IKeyboardEventSource keyboard;
private static WindowsInput.Events.Sources.IMouseEventSource mouse; private static WindowsInput.Events.Sources.IMouseEventSource mouse;
@ -390,6 +422,10 @@ namespace BetterJoyForCemu {
} }
} }
// a bit hacky
_3rdPartyControllers partyForm = new _3rdPartyControllers();
partyForm.CopyCustomControllers();
mgr = new JoyconManager(); mgr = new JoyconManager();
mgr.form = form; mgr.form = form;
mgr.Awake(); mgr.Awake();