ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
mc_example.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7 # Licenced under Academic Free License version 2.0
8 # Review ps2sdk README & LICENSE files for further details.
9 #
10 # libmc API sample.
11 */
12 
13 #include <tamtypes.h>
14 #include <kernel.h>
15 #include <sifrpc.h>
16 #include <loadfile.h>
17 #include <fileio.h>
18 #include <malloc.h>
19 #include <libmc.h>
20 #include <stdio.h>
21 #include <string.h>
22 
23 //
24 // TODO: Update comments to reflect recent modifications to libmc (ie: support for
25 // MCMAN/MCSERV or XMCMAN/XMCSERV, asynchronous support etc.
26 //
27 
28 
29 //#define TYPE_MC
30 #define TYPE_XMC
31 
32 void LoadModules(void);
33 int CreateSave(void);
34 
35 #define ARRAY_ENTRIES 64
36 static mcTable mcDir[ARRAY_ENTRIES] __attribute__((aligned(64)));
37 static int mc_Type, mc_Free, mc_Format;
38 
39 int main() {
40 
41  int fd, ret;
42  int i;
43 
44  // Initialise
45  SifInitRpc(0);
46  LoadModules();
47 #ifdef TYPE_MC
48  if(mcInit(MC_TYPE_MC) < 0) {
49  printf("Failed to initialise memcard server!\n");
50  SleepThread();
51  }
52 #else
53  if(mcInit(MC_TYPE_XMC) < 0) {
54  printf("Failed to initialise memcard server!\n");
55  SleepThread();
56  }
57 #endif
58 
59  printf("\nMemory card library example code - by Sjeep\n\n");
60 
61  // int mcGetInfo(int port, int slot, int* type, int* free, int* format);
62  //
63  // mcGetInfo retrieves memcard state info, such as the memcard type, free blocks and
64  // the format status.
65  //
66  // mcGetInfo is passed pointers to three variables, which are filled upon completion
67  // of the getinfo rpc command. The mcGetInfo return values are as follows:
68  //
69  // 0 : The same memory card has been connected since the last mcGetInfo call.
70  // -1 : Switched to a formatted memory card.
71  // -2 : Switched to an unformatted memory card.
72  // -10 or less : The memory card could not be detected.
73  //
74  // NOTE: With the MCMAN/MCSERV, *format is always returned as 0 regardless of if
75  // if the memcard is formatted or not.
76 
77  // Since this is the first call, -1 should be returned.
78  mcGetInfo(0, 0, &mc_Type, &mc_Free, &mc_Format);
79  mcSync(0, NULL, &ret);
80  printf("mcGetInfo returned %d\n",ret);
81  printf("Type: %d Free: %d Format: %d\n\n", mc_Type, mc_Free, mc_Format);
82 
83  // Assuming that the same memory card is connected, this should return 0
85  mcSync(0, NULL, &ret);
86  printf("mcGetInfo returned %d\n",ret);
87  printf("Type: %d Free: %d Format: %d\n\n", mc_Type, mc_Free, mc_Format);
88 
89  // int mcGetDir(int port, int slot, char *name, unsigned mode, int maxent, mcTable* table);
90  //
91  // mcGetDir retrieves the directory structure of a specific path on the memory card.
92  //
93  // The filename is relative to the root of the memory card. Wildcards such as '*' and '?'
94  // may be used. "maxent" is the maximum number of mcTable elements your array specified
95  // by "table" can hold. The mc_getdir return values are as follows:
96  //
97  // 0 or more : The number of file entries that were obtained.
98  // -2 : The memory card is unformatted
99  // -4 : A non-existant path was specified in the "name" parameter
100  // -10 or less : The memory card could not be detected.
101 
102  mcGetDir(0, 0, "/*", 0, ARRAY_ENTRIES - 10, mcDir);
103  mcSync(0, NULL, &ret);
104  printf("mcGetDir returned %d\n\nListing of root directory on memory card:\n\n", ret);
105 
106  for(i=0; i < ret; i++)
107  {
108  if(mcDir[i].attrFile & MC_ATTR_SUBDIR)
109  printf("[DIR] %s\n", mcDir[i].name);
110  else
111  printf("%s - %d bytes\n", mcDir[i].name, mcDir[i].fileSizeByte);
112  }
113 
114  // Check if existing save is present
115  fd = open("mc0:PS2DEV/icon.sys", O_RDONLY);
116  if(fd <= 0) {
117 
118  printf("\nNo previous save exists, creating...\n");
119 
120  if((ret = CreateSave()) < 0) {
121 
122  printf("Failed to create save! Errorno: %d\n",ret);
123  SleepThread();
124  }
125 
126  } else {
127 
128  printf("\nPrevious save exists, listing directory\n\n");
129 
130  ret = mcGetDir(0, 0, "/PS2DEV/*", 0, ARRAY_ENTRIES, mcDir);
131  printf("mcGetDir returned %d\n\n", ret);
132 
133  for(i=0; i < ret; i++)
134  {
135  if(mcDir[i].attrFile & MC_ATTR_SUBDIR)
136  printf("[DIR] %s\n", mcDir[i].name);
137  else
138  printf("%s - %d bytes\n", mcDir[i].name, mcDir[i].fileSizeByte);
139  }
140  }
141 
142  // Return to the browser, so you can see the PS2Dev icon :)
143  SifExitRpc();
144  return 0;
145 }
146 
147 int CreateSave(void)
148 {
149  int mc_fd;
150  int icon_fd,icon_size;
151  char* icon_buffer;
152  mcIcon icon_sys;
153 
154  static iconIVECTOR bgcolor[4] = {
155  { 68, 23, 116, 0 }, // top left
156  { 255, 255, 255, 0 }, // top right
157  { 255, 255, 255, 0 }, // bottom left
158  { 68, 23, 116, 0 }, // bottom right
159  };
160 
161  static iconFVECTOR lightdir[3] = {
162  { 0.5, 0.5, 0.5, 0.0 },
163  { 0.0,-0.4,-0.1, 0.0 },
164  {-0.5,-0.5, 0.5, 0.0 },
165  };
166 
167  static iconFVECTOR lightcol[3] = {
168  { 0.3, 0.3, 0.3, 0.00 },
169  { 0.4, 0.4, 0.4, 0.00 },
170  { 0.5, 0.5, 0.5, 0.00 },
171  };
172 
173  static iconFVECTOR ambient = { 0.50, 0.50, 0.50, 0.00 };
174 
175 
176  if(mkdir("mc0:PS2DEV", 0777) < 0) return -1;
177 
178  // Set up icon.sys. This is the file which controls how our memory card save looks
179  // in the PS2 browser screen. It contains info on the bg colour, lighting, save name
180  // and icon filenames. Please note that the save name is sjis encoded.
181 
182  memset(&icon_sys, 0, sizeof(mcIcon));
183  strcpy(icon_sys.head, "PS2D");
184  strcpy_sjis((short *)&icon_sys.title, "Memcard Example\nPS2Dev r0x0rs");
185  icon_sys.nlOffset = 16;
186  icon_sys.trans = 0x60;
187  memcpy(icon_sys.bgCol, bgcolor, sizeof(bgcolor));
188  memcpy(icon_sys.lightDir, lightdir, sizeof(lightdir));
189  memcpy(icon_sys.lightCol, lightcol, sizeof(lightcol));
190  memcpy(icon_sys.lightAmbient, ambient, sizeof(ambient));
191  strcpy(icon_sys.view, "ps2dev.icn"); // these filenames are relative to the directory
192  strcpy(icon_sys.copy, "ps2dev.icn"); // in which icon.sys resides.
193  strcpy(icon_sys.del, "ps2dev.icn");
194 
195  // Write icon.sys to the memory card (Note that this filename is fixed)
196  mc_fd = open("mc0:PS2DEV/icon.sys",O_WRONLY | O_CREAT);
197  if(mc_fd < 0) return -2;
198 
199  write(mc_fd, &icon_sys, sizeof(icon_sys));
200  close(mc_fd);
201  printf("icon.sys written sucessfully.\n");
202 
203  // Write icon file to the memory card.
204  // Note: The icon file was created with my bmp2icon tool, available for download at
205  // http://www.ps2dev.org
206  icon_fd = open("host:ps2dev.icn",O_RDONLY);
207  if(icon_fd < 0) return -3;
208 
209  icon_size = lseek(icon_fd,0,SEEK_END);
210  lseek(icon_fd,0,SEEK_SET);
211 
212  icon_buffer = malloc(icon_size);
213  if(icon_buffer == NULL) return -4;
214  if(read(icon_fd, icon_buffer, icon_size) != icon_size) return -5;
215  close(icon_fd);
216 
217  icon_fd = open("mc0:PS2DEV/ps2dev.icn",O_WRONLY | O_CREAT);
218  if(icon_fd < 0) return -6;
219 
220  write(icon_fd,icon_buffer,icon_size);
221  close(icon_fd);
222  printf("ps2dev.icn written sucessfully.\n");
223 
224  return 0;
225 }
226 
227 
228 void LoadModules(void)
229 {
230  int ret;
231 
232 #ifdef TYPE_MC
233  ret = SifLoadModule("rom0:SIO2MAN", 0, NULL);
234  if (ret < 0) {
235  printf("Failed to load module: SIO2MAN");
236  SleepThread();
237  }
238 
239  ret = SifLoadModule("rom0:MCMAN", 0, NULL);
240  if (ret < 0) {
241  printf("Failed to load module: MCMAN");
242  SleepThread();
243  }
244 
245  ret = SifLoadModule("rom0:MCSERV", 0, NULL);
246  if (ret < 0) {
247  printf("Failed to load module: MCSERV");
248  SleepThread();
249  }
250 #else
251  ret = SifLoadModule("rom0:XSIO2MAN", 0, NULL);
252  if (ret < 0) {
253  printf("Failed to load module: SIO2MAN");
254  SleepThread();
255  }
256 
257  ret = SifLoadModule("rom0:XMCMAN", 0, NULL);
258  if (ret < 0) {
259  printf("Failed to load module: MCMAN");
260  SleepThread();
261  }
262 
263  ret = SifLoadModule("rom0:XMCSERV", 0, NULL);
264  if (ret < 0) {
265  printf("Failed to load module: MCSERV");
266  SleepThread();
267  }
268 #endif
269 
270 }
int SifLoadModule(const char *path, int arg_len, const char *args)
s32 SleepThread(void)
#define MC_TYPE_XMC
Definition: libmc.h:196
#define MC_ATTR_SUBDIR
Definition: libmc.h:55
float iconFVECTOR[4]
Definition: libmc.h:103
int mcGetDir(int port, int slot, const char *name, unsigned mode, int maxent, sceMcTblGetDir *table)
Definition: libmc.c:567
int iconIVECTOR[4]
Definition: libmc.h:102
#define MC_TYPE_MC
Definition: libmc.h:195
int mcGetInfo(int port, int slot, int *type, int *free, int *format)
Definition: libmc.c:336
int mcInit(int type)
Definition: libmc.c:230
int mcSync(int mode, int *cmd, int *result)
Definition: libmc.c:864
static int mc_Format
Definition: mc_example.c:37
static int mc_Free
Definition: mc_example.c:37
#define ARRAY_ENTRIES
Definition: mc_example.c:35
static int mc_Type
Definition: mc_example.c:37
void LoadModules(void)
Definition: mc_example.c:228
int CreateSave(void)
Definition: mc_example.c:147
int main()
Definition: mc_example.c:39
static mcTable mcDir[ARRAY_ENTRIES]
Definition: mc_example.c:36
int mkdir(const char *path, mode_t mode)
Definition: ps2sdkapi.c:466
static u32 bgcolor
Definition: scr_printf.c:26
void SifInitRpc(int mode)
void SifExitRpc(void)
int strcpy_sjis(short *sjis_buff, const char *ascii_buff)
Definition: libmc.h:106
unsigned char view[64]
Definition: libmc.h:128
unsigned short title[34]
Definition: libmc.h:126
iconFVECTOR lightCol[3]
Definition: libmc.h:122
iconFVECTOR lightAmbient
Definition: libmc.h:124
unsigned short nlOffset
Definition: libmc.h:112
unsigned trans
Definition: libmc.h:116
unsigned char copy[64]
Definition: libmc.h:130
iconIVECTOR bgCol[4]
Definition: libmc.h:118
iconFVECTOR lightDir[3]
Definition: libmc.h:120
unsigned char head[4]
Definition: libmc.h:108
unsigned char del[64]
Definition: libmc.h:132
Definition: libmc.h:149
#define NULL
Definition: tamtypes.h:91