c64-ready 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +70 -62
  2. package/bin/headless.mjs +1 -1
  3. package/dist/assets/{CHANGELOG-CPlXKLCV.js → CHANGELOG-0a7A8Ekq.js} +113 -1
  4. package/dist/assets/index-BESofQoh.js +627 -0
  5. package/dist/games/cartridges/goat-beat.crt +0 -0
  6. package/dist/index.html +1 -1
  7. package/dist-ts/emulator/c64-emulator.d.ts +3 -0
  8. package/dist-ts/emulator/c64-emulator.d.ts.map +1 -1
  9. package/dist-ts/emulator/c64-emulator.js +36 -4
  10. package/dist-ts/emulator/c64-emulator.js.map +1 -1
  11. package/dist-ts/emulator/crt-info.d.ts +61 -0
  12. package/dist-ts/emulator/crt-info.d.ts.map +1 -0
  13. package/dist-ts/emulator/crt-info.js +190 -0
  14. package/dist-ts/emulator/crt-info.js.map +1 -0
  15. package/dist-ts/emulator/input.d.ts +96 -0
  16. package/dist-ts/emulator/input.d.ts.map +1 -1
  17. package/dist-ts/emulator/input.js +549 -4
  18. package/dist-ts/emulator/input.js.map +1 -1
  19. package/dist-ts/headless/c64-headless.d.ts +1 -0
  20. package/dist-ts/headless/c64-headless.d.ts.map +1 -1
  21. package/dist-ts/headless/c64-headless.js +5 -0
  22. package/dist-ts/headless/c64-headless.js.map +1 -1
  23. package/dist-ts/types/emulator.d.ts +2 -1
  24. package/dist-ts/types/emulator.d.ts.map +1 -1
  25. package/package.json +1 -1
  26. package/public/games/cartridges/goat-beat.crt +0 -0
  27. package/src/emulator/c64-emulator.ts +37 -7
  28. package/src/emulator/crt-info.ts +35 -23
  29. package/src/emulator/input.ts +239 -19
  30. package/src/headless/c64-headless.ts +5 -0
  31. package/src/headless/headless-cli.mjs +462 -249
  32. package/src/headless/input-server.mjs +649 -278
  33. package/src/headless/webrtc-server.mjs +218 -71
  34. package/src/main.ts +30 -4
  35. package/src/player/c64-player.ts +121 -40
  36. package/src/player/input-handler.ts +8 -0
  37. package/src/player/styles/ui-controller.css +103 -21
  38. package/src/player/ui-controller.ts +186 -23
  39. package/src/types/emulator.ts +1 -1
  40. package/dist/assets/index-Ckbi7gQo.js +0 -535
@@ -37,6 +37,18 @@ import {
37
37
  type LoadTypeSelection,
38
38
  } from './load-formats';
39
39
 
40
+ const CRT_PRELOAD_CHECKS_STORAGE_KEY = 'c64-disable-crt-preload-checks';
41
+
42
+ type ConnectedGamepad = {
43
+ index: number;
44
+ name: string;
45
+ };
46
+
47
+ function formatSnapshotFilename(now: Date = new Date()): string {
48
+ const pad = (value: number): string => String(value).padStart(2, '0');
49
+ return `snapshot-${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}-${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}.c64`;
50
+ }
51
+
40
52
  export default class UIController {
41
53
  private helpOverlay: HTMLElement | null = null;
42
54
  private settingsOverlay: HTMLElement | null = null;
@@ -45,15 +57,98 @@ export default class UIController {
45
57
  // Save previous overflow styles so we can restore them when exiting full/stretch
46
58
  private savedHtmlOverflow: string | null = null;
47
59
  private savedBodyOverflow: string | null = null;
60
+ private connectedGamepads = new Map<number, ConnectedGamepad>();
61
+ private readonly handleControllerConnected = (event: Event): void => {
62
+ const detail = (event as CustomEvent<{ name?: string; index?: number }>).detail;
63
+ if (typeof detail?.index !== 'number') return;
64
+ this.connectedGamepads.set(detail.index, {
65
+ index: detail.index,
66
+ name: detail.name ?? `Gamepad ${detail.index}`,
67
+ });
68
+ this.renderGamepadButtons();
69
+ };
70
+ private readonly handleControllerDisconnected = (event: Event): void => {
71
+ const detail = (event as CustomEvent<{ index?: number }>).detail;
72
+ if (typeof detail?.index !== 'number') return;
73
+ this.connectedGamepads.delete(detail.index);
74
+ this.renderGamepadButtons();
75
+ };
48
76
 
49
77
  init(player?: C64Player): void {
50
78
  this.player = player ?? null;
79
+ this.syncConnectedGamepads();
51
80
  this.injectCSS();
52
81
  this.createButton();
53
82
  this.createDialog();
54
83
  this.createHamburger();
55
84
  this.createMenu();
56
85
  this.createUnmuteButton();
86
+ window.addEventListener('c64-controller-connected', this.handleControllerConnected);
87
+ window.addEventListener('c64-controller-disconnected', this.handleControllerDisconnected);
88
+ }
89
+
90
+ private syncConnectedGamepads(): void {
91
+ this.connectedGamepads.clear();
92
+ const gamepads = navigator.getGamepads?.() ?? [];
93
+ for (const gamepad of gamepads) {
94
+ if (!gamepad) continue;
95
+ this.connectedGamepads.set(gamepad.index, {
96
+ index: gamepad.index,
97
+ name: gamepad.id,
98
+ });
99
+ }
100
+ }
101
+
102
+ private getConnectedGamepads(): ConnectedGamepad[] {
103
+ return Array.from(this.connectedGamepads.values()).sort((a, b) => a.index - b.index);
104
+ }
105
+
106
+ private getGamepadLabel(gamepad: ConnectedGamepad): string {
107
+ const shortenedName = gamepad.name.replace(/\s*\([^)]*\)\s*$/u, '').trim();
108
+ const name = shortenedName || gamepad.name.trim() || `Gamepad ${gamepad.index}`;
109
+ return `${gamepad.index}: ${name}`;
110
+ }
111
+
112
+ private renderGamepadButtons(): void {
113
+ const container = document.getElementById('c64-gamepad-list');
114
+ const emptyState = document.getElementById('c64-gamepad-empty');
115
+ if (!container || !emptyState) return;
116
+
117
+ const gamepads = this.getConnectedGamepads();
118
+ const activeIndex = this.player?.getActiveGamepadIndex() ?? -1;
119
+
120
+ container.replaceChildren(
121
+ ...gamepads.map((gamepad) => {
122
+ const button = document.createElement('button');
123
+ const active = gamepad.index === activeIndex;
124
+ button.type = 'button';
125
+ button.className = `c64-btn c64-gamepad-btn${active ? ' active' : ''}`;
126
+ button.textContent = this.getGamepadLabel(gamepad);
127
+ button.setAttribute('aria-pressed', active ? 'true' : 'false');
128
+ button.addEventListener('click', () => {
129
+ if (!this.player) return;
130
+ this.player.setActiveGamepadIndex(gamepad.index);
131
+ this.renderGamepadButtons();
132
+ });
133
+ return button;
134
+ }),
135
+ );
136
+
137
+ emptyState.hidden = gamepads.length > 0;
138
+ }
139
+
140
+ private downloadSnapshot(snapshot: Uint8Array): void {
141
+ const snapshotCopy = new Uint8Array(snapshot.byteLength);
142
+ snapshotCopy.set(snapshot);
143
+ const blob = new Blob([snapshotCopy], { type: 'application/bin' });
144
+ const url = URL.createObjectURL(blob);
145
+ const link = document.createElement('a');
146
+ link.href = url;
147
+ link.download = formatSnapshotFilename();
148
+ document.body.appendChild(link);
149
+ link.click();
150
+ link.remove();
151
+ URL.revokeObjectURL(url);
57
152
  }
58
153
 
59
154
  private injectCSS(): void {
@@ -202,13 +297,13 @@ export default class UIController {
202
297
  <div class="c64-menu-body">
203
298
  <div class="c64-settings-tabs" role="tablist" aria-label="Settings sections">
204
299
  ${sections
205
- .map(
206
- (section, i) =>
207
- `<button class="c64-settings-tab${i === 0 ? ' active' : ''}" data-settings-tab="${section.id}" role="tab" aria-selected="${
208
- i === 0 ? 'true' : 'false'
209
- }">${section.label}</button>`,
210
- )
211
- .join('')}
300
+ .map(
301
+ (section, i) =>
302
+ `<button class="c64-settings-tab${i === 0 ? ' active' : ''}" data-settings-tab="${section.id}" role="tab" aria-selected="${
303
+ i === 0 ? 'true' : 'false'
304
+ }">${section.label}</button>`,
305
+ )
306
+ .join('')}
212
307
  </div>
213
308
 
214
309
  <div class="c64-settings-sections">
@@ -264,6 +359,9 @@ export default class UIController {
264
359
  <label><input type="radio" name="c64-input-mode" value="mixed" checked /> Mixed</label>
265
360
  </div>
266
361
  <div id="c64-input-mode-hint" class="c64-section-hint">Arrows + Z + Ctrl = joystick &amp; all other keys &rarr; C64 keyboard</div>
362
+ <label class="c64-section-label">Gamepads</label>
363
+ <div id="c64-gamepad-list" class="c64-gamepad-list"></div>
364
+ <div id="c64-gamepad-empty" class="c64-section-hint">To use a gamepad, connect it to your computer and press any button.</div>
267
365
  </section>
268
366
 
269
367
  <section class="c64-settings-section" data-settings-section="display" hidden>
@@ -282,9 +380,16 @@ export default class UIController {
282
380
  <section class="c64-settings-section" data-settings-section="system" hidden>
283
381
  <label class="c64-section-label">System Actions</label>
284
382
  <div class="c64-system-actions">
383
+ <button class="c64-btn" id="c64-save-snapshot-btn">Save Snapshot</button>
285
384
  <button class="c64-btn" id="c64-detach-btn">Detach Cartridge</button>
286
385
  <button class="c64-btn" id="c64-reset-btn">Hard Reset</button>
386
+ <button class="c64-btn" id="c64-reboot-btn">Reboot Emulator</button>
287
387
  </div>
388
+ <label class="c64-checkbox-row" for="c64-disable-crt-preload-checks">
389
+ <input type="checkbox" id="c64-disable-crt-preload-checks" />
390
+ Disable crt preload checks
391
+ </label>
392
+ <div class="c64-section-hint">Allows any .crt file and skips compatibility prechecks.</div>
288
393
  </section>
289
394
  </div>
290
395
  </div>
@@ -298,25 +403,25 @@ export default class UIController {
298
403
  // The settings overlay is the overlay we just created
299
404
  this.settingsOverlay = overlay;
300
405
 
301
- const tabButtons = panel.querySelectorAll('[data-settings-tab]') as NodeListOf<HTMLButtonElement>;
302
- const sectionEls = panel.querySelectorAll(
303
- '[data-settings-section]'
304
- ) as NodeListOf<HTMLElement>;
406
+ const tabButtons = panel.querySelectorAll(
407
+ '[data-settings-tab]',
408
+ ) as NodeListOf<HTMLButtonElement>;
409
+ const sectionEls = panel.querySelectorAll('[data-settings-section]') as NodeListOf<HTMLElement>;
305
410
 
306
411
  const activateSection = (sectionId: string) => {
307
412
  tabButtons.forEach((tab) => {
308
- const active = tab.dataset.settingsTab === sectionId;
413
+ const active = tab.getAttribute('data-settings-tab') === sectionId;
309
414
  tab.classList.toggle('active', active);
310
415
  tab.setAttribute('aria-selected', active ? 'true' : 'false');
311
416
  });
312
417
  sectionEls.forEach((section) => {
313
- section.hidden = section.dataset.settingsSection !== sectionId;
418
+ section.hidden = section.getAttribute('data-settings-section') !== sectionId;
314
419
  });
315
420
  };
316
421
 
317
422
  tabButtons.forEach((tab) => {
318
423
  tab.addEventListener('click', () => {
319
- const sectionId = tab.dataset.settingsTab;
424
+ const sectionId = tab.getAttribute('data-settings-tab');
320
425
  if (sectionId) activateSection(sectionId);
321
426
  });
322
427
  });
@@ -409,15 +514,14 @@ export default class UIController {
409
514
  };
410
515
 
411
516
  const parseManifest = (raw: unknown): ToolItem[] => {
412
- const entries: unknown[] =
413
- Array.isArray(raw)
414
- ? raw
415
- : raw && typeof raw === 'object' && Array.isArray((raw as { tools?: unknown[] }).tools)
416
- ? (raw as { tools: unknown[] }).tools
417
- : [];
517
+ const entries: unknown[] = Array.isArray(raw)
518
+ ? raw
519
+ : raw && typeof raw === 'object' && Array.isArray((raw as { tools?: unknown[] }).tools)
520
+ ? (raw as { tools: unknown[] }).tools
521
+ : [];
418
522
 
419
523
  return entries
420
- .map((entry) => {
524
+ .map((entry: unknown) => {
421
525
  const path =
422
526
  typeof entry === 'string'
423
527
  ? entry
@@ -433,10 +537,13 @@ export default class UIController {
433
537
  .map((part) => encodeURIComponent(part))
434
538
  .join('/');
435
539
  const normalizedPath = encodedPath.replace(/^\/+/, '');
436
- const url = `${import.meta.env.BASE_URL}tools/${normalizedPath}`.replace(/([^:]\/)\/+/, '$1');
540
+ const url = `${import.meta.env.BASE_URL}tools/${normalizedPath}`.replace(
541
+ /([^:]\/)\/+/,
542
+ '$1',
543
+ );
437
544
  return { name: filename, url, type } satisfies ToolItem;
438
545
  })
439
- .filter((v): v is ToolItem => !!v);
546
+ .filter((v: ToolItem | null): v is ToolItem => !!v);
440
547
  };
441
548
 
442
549
  const loadToolsManifest = async () => {
@@ -470,8 +577,41 @@ export default class UIController {
470
577
 
471
578
  const section = panel.querySelector('[data-settings-section="system"]');
472
579
  // Detach cartridge / hard reset controls
580
+ const saveSnapshotBtn = section?.querySelector('#c64-save-snapshot-btn') as HTMLButtonElement | null;
473
581
  const detachBtn = section?.querySelector('#c64-detach-btn') as HTMLButtonElement | null;
474
582
  const resetBtn = section?.querySelector('#c64-reset-btn') as HTMLButtonElement | null;
583
+ const rebootBtn = section?.querySelector('#c64-reboot-btn') as HTMLButtonElement | null;
584
+ const disableChecksToggle = section?.querySelector(
585
+ '#c64-disable-crt-preload-checks',
586
+ ) as HTMLInputElement | null;
587
+
588
+ const disableCrtPreloadChecks =
589
+ window.localStorage.getItem(CRT_PRELOAD_CHECKS_STORAGE_KEY) === '1';
590
+ this.player?.setCrtPreloadChecksDisabled(disableCrtPreloadChecks);
591
+ if (disableChecksToggle) {
592
+ disableChecksToggle.checked = disableCrtPreloadChecks;
593
+ disableChecksToggle.addEventListener('change', () => {
594
+ const disabled = disableChecksToggle.checked;
595
+ window.localStorage.setItem(CRT_PRELOAD_CHECKS_STORAGE_KEY, disabled ? '1' : '0');
596
+ this.player?.setCrtPreloadChecksDisabled(disabled);
597
+ });
598
+ }
599
+ if (saveSnapshotBtn) {
600
+ saveSnapshotBtn.addEventListener('click', () => {
601
+ if (!this.player || typeof this.player.getSnapshot !== 'function') return;
602
+ const statusEl = document.getElementById('status');
603
+ try {
604
+ this.downloadSnapshot(this.player.getSnapshot());
605
+ if (statusEl) {
606
+ statusEl.textContent = 'Snapshot saved';
607
+ }
608
+ } catch (err) {
609
+ if (statusEl) {
610
+ statusEl.textContent = `Snapshot save failed: ${String((err as Error)?.message ?? err)}`;
611
+ }
612
+ }
613
+ });
614
+ }
475
615
  if (detachBtn) {
476
616
  detachBtn.addEventListener('click', () => {
477
617
  if (!this.player) return;
@@ -495,6 +635,27 @@ export default class UIController {
495
635
  }
496
636
  });
497
637
  }
638
+ if (rebootBtn) {
639
+ rebootBtn.addEventListener('click', async () => {
640
+ if (!this.player) return;
641
+ if (!confirm('Perform full reboot? This re-instantiates the emulator with no game loaded.'))
642
+ return;
643
+ const statusEl = document.getElementById('status');
644
+ if (statusEl) {
645
+ statusEl.textContent = 'Rebooting emulator...';
646
+ }
647
+ try {
648
+ await this.player.reboot();
649
+ if (statusEl) {
650
+ statusEl.textContent = 'Emulator rebooted (no game loaded)';
651
+ }
652
+ } catch (err) {
653
+ if (statusEl) {
654
+ statusEl.textContent = `Reboot failed: ${String((err as Error)?.message ?? err)}`;
655
+ }
656
+ }
657
+ });
658
+ }
498
659
 
499
660
  // ── Audio section ─────────────────────────────────────────────────────
500
661
  const audioContainer = panel.querySelector('#c64-audio-section-anchor') as HTMLElement;
@@ -548,6 +709,7 @@ export default class UIController {
548
709
  if (r.checked) applyInputMode(r.value);
549
710
  });
550
711
  });
712
+ this.renderGamepadButtons();
551
713
 
552
714
  // ── Display section wiring ─────────────────────────────────────────────
553
715
  const displayRadios = panel.querySelectorAll(
@@ -621,6 +783,7 @@ export default class UIController {
621
783
  window.addEventListener('c64-close-dialog', () => {
622
784
  this.closeMenu();
623
785
  });
786
+
624
787
  // Close settings when a load completes or errors so the user sees the result
625
788
  // window.addEventListener('c64-load-success', () => {
626
789
  // this.settingsOverlay?.classList.remove('visible');
@@ -57,7 +57,7 @@ export interface WASMExports {
57
57
 
58
58
  // Snapshot
59
59
  c64_getSnapshotSize(): number;
60
- c64_getSnapshot(ptr: number): void;
60
+ c64_getSnapshot(): number;
61
61
  c64_loadSnapshot(ptr: number, len: number): void;
62
62
 
63
63
  // Data