annotate menu.c @ 868:1bab7e01ae98

Allow directory navigation in menu. Sort directory entries
author Michael Pavone <pavone@retrodev.com>
date Fri, 06 Nov 2015 14:17:41 -0800
parents 69a6ec208111
children f173317ecdb4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
866
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
1 #include <stdint.h>
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
2 #include <stdlib.h>
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
3 #include <string.h>
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
4 #include <stdio.h>
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
5 #include "blastem.h"
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
6 #include "menu.h"
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
7 #include "backend.h"
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
8 #include "util.h"
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
9
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
10
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
11 uint16_t menu_read_w(uint32_t address, void * context)
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
12 {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
13 //This should return the status of the last request with 0
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
14 //meaning either the request is complete or no request is pending
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
15 //in the current implementation, the operations happen instantly
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
16 //in emulated time so we can always return 0
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
17 return 0;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
18 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
19
868
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
20 int menu_dir_sort(const void *a, const void *b)
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
21 {
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
22 const dir_entry *da, *db;
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
23 da = a;
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
24 db = b;
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
25 if (da->is_dir != db->is_dir) {
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
26 return db->is_dir - da->is_dir;
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
27 }
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
28 return strcasecmp(((dir_entry *)a)->name, ((dir_entry *)b)->name);
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
29 }
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
30
866
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
31 void * menu_write_w(uint32_t address, void * context, uint16_t value)
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
32 {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
33 m68k_context *m68k = context;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
34 genesis_context *gen = m68k->system;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
35 menu_context *menu = gen->extra;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
36 if (!menu) {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
37 gen->extra = menu = calloc(1, sizeof(menu_context));
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
38 menu->curpath = strdup(get_home_dir());
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
39 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
40 if (menu->state) {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
41 uint32_t dst = menu->latch << 16 | value;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
42 switch (address >> 2)
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
43 {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
44 case 0: {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
45 size_t num_entries;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
46 dir_entry *entries = get_dir_list(menu->curpath, &num_entries);
868
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
47 if (entries) {
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
48 qsort(entries, num_entries, sizeof(dir_entry), menu_dir_sort);
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
49 }
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
50 uint8_t *dest;
866
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
51 for (size_t i = 0; i < num_entries; i++)
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
52 {
868
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
53 dest = get_native_pointer(dst, (void **)m68k->mem_pointers, &m68k->options->gen);
866
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
54 if (!dest) {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
55 break;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
56 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
57 *(dest++) = entries[i].is_dir;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
58 *(dest++) = 1;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
59 dst += 2;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
60 uint8_t term = 0;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
61 for (char *cpos = entries[i].name; *cpos; cpos++)
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
62 {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
63 dest[1] = *cpos;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
64 dest[0] = cpos[1];
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
65 if (cpos[1]) {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
66 cpos++;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
67 } else {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
68 term = 1;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
69 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
70 dst += 2;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
71 if (!(dst & 0xFFFF)) {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
72 //we may have walked off the end of a memory block, get a fresh native pointer
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
73 dest = get_native_pointer(dst, (void **)m68k->mem_pointers, &m68k->options->gen);
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
74 if (!dest) {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
75 break;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
76 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
77 } else {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
78 dest += 2;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
79 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
80 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
81 if (!term) {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
82 *(dest++) = 0;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
83 *dest = 0;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
84 dst += 2;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
85 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
86 }
868
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
87 //terminate list
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
88 dest = get_native_pointer(dst, (void **)m68k->mem_pointers, &m68k->options->gen);
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
89 if (dest) {
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
90 *dest = dest[1] = 0;
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
91 free_dir_list(entries, num_entries);
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
92 }
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
93 break;
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
94 }
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
95 case 1: {
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
96 char buf[4096];
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
97 char *cur;
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
98 char * dest = NULL;
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
99 for (cur = buf; cur < buf+sizeof(buf); cur+=2, dst+=2, dest+=2)
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
100 {
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
101 if (!dest || !(dst & 0xFFFF)) {
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
102 //we may have walked off the end of a memory block, get a fresh native pointer
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
103 dest = get_native_pointer(dst, (void **)m68k->mem_pointers, &m68k->options->gen);
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
104 if (!dest) {
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
105 break;
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
106 }
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
107 }
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
108 *cur = dest[1];
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
109 cur[1] = *dest;
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
110 if (!*dest || !dest[1]) {
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
111 break;
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
112 }
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
113 }
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
114 char *pieces[] = {menu->curpath, "/", buf};
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
115 menu->curpath = alloc_concat_m(3, pieces);
1bab7e01ae98 Allow directory navigation in menu. Sort directory entries
Michael Pavone <pavone@retrodev.com>
parents: 866
diff changeset
116 free(pieces[0]);
866
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
117 break;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
118 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
119 default:
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
120 fprintf(stderr, "WARNING: write to undefined menu port %X\n", address);
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
121 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
122 menu->state = 0;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
123 } else {
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
124 menu->latch = value;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
125 menu->state = 1;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
126 }
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
127
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
128 return context;
69a6ec208111 Menu ROM now pulls real file names from the OS rather than using a fake list
Michael Pavone <pavone@retrodev.com>
parents:
diff changeset
129 }