Skip to content

Commit

Permalink
chore: initial commit for visibility listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
Tormak9970 committed Jan 7, 2024
1 parent be905c8 commit 7426244
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/lib/controllers/PluginController.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LogController } from "./LogController";
import { TabMasterManager } from "../../state/TabMasterManager";
import { getCurrentUserId } from "../Utils";
import { DestructiveModal } from '../../components/generic/DestructiveModal';
import { VisibilityListenersController } from "./VisibilityListenersController";

function showMigrationModal(okCallback: () => Promise<void>, cancelCallback: () => Promise<void>) {
showModal(
Expand Down Expand Up @@ -43,6 +44,7 @@ export class PluginController {
// @ts-ignore
private static server: ServerAPI;
private static tabMasterManager: TabMasterManager;
private static listenersController: VisibilityListenersController;

private static steamController: SteamController;

Expand All @@ -54,6 +56,7 @@ export class PluginController {
this.server = server;
this.tabMasterManager = tabMasterManager;
this.steamController = new SteamController();
this.listenersController = new VisibilityListenersController(this.tabMasterManager, this.steamController);
}

/**
Expand Down Expand Up @@ -122,6 +125,7 @@ export class PluginController {
*/
static dismount(): void {
this.tabMasterManager.disposeReactions();
this.listenersController.destroy();
LogController.log("PluginController dismounted.");
}
}
9 changes: 9 additions & 0 deletions src/lib/controllers/SteamController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,4 +77,13 @@ export class SteamController {
async getLocalizedTags(tags: number[]): Promise<TagResponse[]> {
return await SteamClient.Apps.GetStoreTagLocalization(tags);
}

/**
* Registers a callback for controller info changes.
* @param callback The callback to run when controllers change.
* @returns A function to call to unregister the callback.
*/
registerForControllerListChanges(callback: (controllers: ControllerInfo[]) => void): Unregisterer {
return SteamClient.Input.RegisterForControllerListChanges(callback);
}
}
74 changes: 74 additions & 0 deletions src/lib/controllers/VisibilityListenersController.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { TabMasterManager } from "../../state/TabMasterManager";
import { SteamController } from "./SteamController";


export class VisibilityListenersController {
private tabMasterManager: TabMasterManager;
private steamController: SteamController;

// The number of connected controllers, including the SteamDeck itself.
private numberOfControllers = 1;
private controllerListSub!: Unregisterer;

/**
* Creates a new VisibilityListenersController.
* @param tabMasterManager The plugin's state manager.
* @param steamController The plugin's SteamController.
*/
constructor(tabMasterManager: TabMasterManager, steamController: SteamController) {
this.tabMasterManager = tabMasterManager;
this.steamController = steamController;

this.initializeListeners();
}

private initializeListeners() {
// * Initialize player count listener.
this.controllerListSub = this.registerForLocalPlayerCountChanges(this.controllerListCallback);


// TODO: other listeners here.
}

private controllerListCallback(previousNumPlayers: number, currentNumPlayers: number): void {
// * check if deck should be included, if so, decrease both by one
// ! TabGroups should take priority. If the group is set to override visibility changes, only update tabs in that group, otherwise, check all.

// * check visible tab group
const visibleTabGroup = null; // hard coded value for default, will change once there is a way to get this info
const tabs = this.tabMasterManager.getTabs();
let tabsToCheck = [...tabs.visibleTabsList, ...tabs.hiddenTabsList];

if (visibleTabGroup.overrideVisibilityChanges) {
// TODO: set tabs to check to be the tabs in the tabGroup.
}

// * loop over tabs.
for (const tab of tabsToCheck) {
// TODO: tabs now need a property for visibility listeners
}
}

/**
* Registers a callback for local player count changes.
* @param callback The callback to run when controllers change.
* @returns A function to call to unregister the callback.
*/
private registerForLocalPlayerCountChanges(callback: (previousNumPlayers: number, currentNumPlayers: number) => void): Unregisterer {
return this.steamController.registerForControllerListChanges((controllers: ControllerInfo[]) => {
if (controllers.length !== this.numberOfControllers) {
const previousNumPlayers = this.numberOfControllers;
this.numberOfControllers = controllers.length;

callback(previousNumPlayers, this.numberOfControllers);
}
});
}

/**
* Function to run when cleaning up the plugin.
*/
destroy(): void {
if (this.controllerListSub) this.controllerListSub.unregister();
}
}
31 changes: 31 additions & 0 deletions src/lib/models/VisibilityListeners.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { IconType } from "react-icons/lib";
import { FaUserFriends } from "react-icons/fa";

export type VisibilityListenerType = 'local player count';

type LocalPlayerCountParams = { playerCount: number, includeSteamdeck: boolean }

export type VisibilityListenerParams<T extends VisibilityListenerType> =
T extends 'local player count' ? LocalPlayerCountParams :
never;

/**
* Dictionary of default VisibilityListener params.
*/
export const VisibilityListenerParamsDefaults: { [key in VisibilityListenerType]: VisibilityListenerParams<key> } = {
"local player count": { playerCount: 2, includeSteamdeck: false }
}

/**
* Dictionary of descriptions for each VisibilityListener.
*/
export const VisibilityListenerDescriptions: { [visbilityListenerType in VisibilityListenerType]: string } = {
"local player count": "Change tab visibility based on the number of local players."
}

/**
* Dictionary of icons for each VisibilityListener.
*/
export const VisibilityListenerIcons: { [visbilityListenerType in VisibilityListenerType]: IconType } = {
"local player count": FaUserFriends,
}
2 changes: 1 addition & 1 deletion src/types/SteamTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ interface SteamClient {
FriendSettings: any,
Friends: any,
GameSessions: GameSession,
Input: any,
Input: Input,
InstallFolder: any,
Installs: Installs,
MachineStorage: any,
Expand Down
79 changes: 79 additions & 0 deletions src/types/steam-client/input.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Types for SteamClient.Input

type Input = {
/**
* Registers a callback for changes in the list of active controllers.
* @param callback The callback to run when controller list changes.
* @returns A function to call to unregister the callback.
*/
RegisterForControllerListChanges(callback: (controllerListChanges: ControllerInfo[]) => void): Unregisterer;
}

enum ControllerType {
None = -1,
Unknown = 0,
UnknownSteamController = 1,
SteamController = 2, // Codename Gordon
SteamControllerV2 = 3, // Codename Headcrab
SteamControllerNeptune = 4, // Steam Deck
FrontPanelBoard = 20,
Generic = 30,
XBox360Controller = 31,
XBoxOneController = 32,
PS3Controller = 33,
PS4Controller = 34,
WiiController = 35,
AppleController = 36,
AndroidController = 37,
SwitchProController = 38,
SwitchJoyConLeft = 39,
SwitchJoyConRight = 40,
SwitchJoyConPair = 41,
SwitchProGenericInputOnlyController = 42,
MobileTouch = 43,
SwitchProXInputSwitchController = 44,
PS5Controller = 45,
XboxEliteController = 46,
LastController = 47, // Unverified
PS5EdgeController = 48,
GenericKeyboard = 400,
GenericMouse = 800,
}

interface ControllerInfo {
strName: string;
eControllerType: ControllerType;
nXInputIndex: number;
nControllerIndex: number;
eRumblePreference: number; // ControllerRumbleSetting
bWireless: boolean;
unUniqueID: number;
unVendorID: number;
unProductID: number;
unCapabilities: number;
strFirmwareBuildTime: string;
strSerialNumber: string;
strChipID: string;
nLEDColorR: number;
nLEDColorG: number;
nLEDColorB: number;
flLEDBrightness: number;
flLEDSaturation: number;
nTurnOnSound: number;
nTurnOffSound: number;
nLStickDeadzone: number;
nRStickDeadzone: number;
nLHapticStrength: number;
nRHapticStrength: number;
flLPadPressureCurve: number;
flRPadPressureCurve: number;
bHaptics: boolean;
bSWAntiDrift: boolean;
flGyroStationaryTolerance: number;
flAccelerometerStationaryTolerance: number;
bRemoteDevice: boolean;
bNintendoLayout: boolean;
bUseReversedLayout: boolean;
ActiveAccount: ActiveAccount | undefined;
vecAltAccounts: any[]; // The type for this property might need to be more specific based on the actual data structure
}

0 comments on commit 7426244

Please sign in to comment.