PS2SDK
PS2 Homebrew Libraries
osd_config.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 
18 #include <tamtypes.h>
19 #include <kernel.h>
20 #include <stdio.h>
21 #include <sys/fcntl.h>
22 #include <sys/unistd.h>
23 #include <string.h>
24 #include <osd_config.h>
25 #include <rom0_info.h>
26 #define NEWLIB_PORT_AWARE
27 #include <fileio.h>
28 
30 typedef struct
31 {
32  s16 timezoneOffset;
33  u8 screenType;
34  u8 dateFormat;
35  u8 language;
36  u8 spdifMode;
37  u8 daylightSaving;
38  u8 timeFormat;
40 
41 #define defaultIODriver { (void *)fioOpen, fioClose, fioRead, FIO_O_RDONLY }
42 
43 extern ConfigParamT10K g_t10KConfig;
44 
45 #ifdef F__config_internals
46 ConfigParamT10K g_t10KConfig = {540, TV_SCREEN_43, DATE_YYYYMMDD, LANGUAGE_JAPANESE, 0, 0, 0};
47 #endif
48 
49 #ifdef F_converttobcd
50 static inline unsigned char tobcd(unsigned char dec)
51 {
52  return dec + (dec / 10) * 6;
53 }
54 
55 void converttobcd(sceCdCLOCK *time)
56 {
57  time->second = tobcd(time->second);
58  time->minute = tobcd(time->minute);
59  time->hour = tobcd(time->hour);
60  time->day = tobcd(time->day);
61  time->month = tobcd(time->month);
62  time->year = tobcd(time->year);
63 }
64 #else
65 extern void converttobcd(sceCdCLOCK *time);
66 #endif
67 
68 #ifdef F_convertfrombcd
69 static inline unsigned char frombcd(unsigned char bcd)
70 {
71  return bcd - (bcd >> 4) * 6;
72 }
73 
74 
75 void convertfrombcd(sceCdCLOCK *time)
76 {
77  time->second = frombcd(time->second);
78  time->minute = frombcd(time->minute);
79  time->hour = frombcd(time->hour);
80  time->day = frombcd(time->day);
81  time->month = frombcd(time->month);
82  time->year = frombcd(time->year);
83 }
84 #else
85 extern void convertfrombcd(sceCdCLOCK *time);
86 #endif
87 
88 #ifdef F___adjustTime
89 static const unsigned char gDaysInMonths[12] = {
90  31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
91 
92 static void adddate(sceCdCLOCK *time)
93 {
94  // get the days in each month and fix up feb depending on leap year
95  unsigned char days_in_months[12];
96  memcpy(days_in_months, gDaysInMonths, 12);
97  if ((time->year & 3) == 0)
98  days_in_months[1] = 29;
99 
100  // increment the day and check its within the "day of the month" bounds
101  time->day++;
102  if (time->day > days_in_months[time->month - 1]) {
103  time->day = 1;
104 
105  // increment the month and check its within the "months in a year" bounds
106  time->month++;
107  if (time->month == 13) {
108  time->month = 1;
109 
110  // check the year and increment it
111  time->year++;
112  if (time->year == 100) {
113  time->year = 0;
114  }
115  }
116  }
117 }
118 
119 static void subdate(sceCdCLOCK *time)
120 {
121  // get the days in each month and fix up feb depending on leap year
122  unsigned char days_in_months[12];
123  memcpy(days_in_months, gDaysInMonths, 12);
124  if ((time->year & 3) == 0)
125  days_in_months[1] = 29;
126 
127  // decrement the day and check its within the "day of the month" bounds
128  time->day--;
129  if (time->day == 0) {
130  // decrement the month and check its within the "months in a year" bounds
131  time->month--;
132  if (time->month == 0) {
133  time->month = 12;
134 
135  // check the year and decrement it
136  if (time->year == 0)
137  time->year = 99;
138  else
139  time->year--;
140  }
141 
142  time->day = days_in_months[time->month - 1];
143  }
144 }
145 
146 static void addhour(sceCdCLOCK *time)
147 {
148  time->hour++;
149  if (time->hour == 24) {
150  adddate(time);
151  time->hour = 0;
152  }
153 }
154 
155 static void subhour(sceCdCLOCK *time)
156 {
157  if (time->hour == 0) {
158  subdate(time);
159  time->hour = 23;
160  } else
161  time->hour--;
162 }
163 
164 void __adjustTime(sceCdCLOCK *time, int offset)
165 {
166  convertfrombcd(time);
167  offset += time->minute;
168 
169  if (offset >= 0) {
170  while (offset >= 60) {
171  addhour(time);
172  offset -= 60;
173  }
174  time->minute = offset;
175  } else {
176  while (offset < 0) {
177  subhour(time);
178  offset += 60;
179  }
180  time->minute = offset;
181  }
182 
183  converttobcd(time);
184 }
185 #else
186 extern void __adjustTime(sceCdCLOCK *time, int offset);
187 #endif
188 
189 #ifdef F_IsEarlyJap
190 int IsEarlyJap(ConfigParam config)
191 {
192  return config.version == 0;
193 }
194 #endif
195 
196 #ifdef F_configGetLanguageWithIODriver
197 int configGetLanguageWithIODriver(_io_driver *driver)
198 {
199  ConfigParam config;
200 
201  if (IsT10KWithIODriver(driver))
202  return g_t10KConfig.language;
203 
204  GetOsdConfigParam(&config);
205  if (IsEarlyJap(config))
206  return config.japLanguage;
207  return config.language;
208 }
209 #endif
210 
211 #ifdef F_configGetLanguage
212 int configGetLanguage(void)
213 {
214  _io_driver driver = defaultIODriver;
215  return configGetLanguageWithIODriver(&driver);
216 }
217 #endif
218 
219 #ifdef F_configSetLanguageWithIODriver
220 void configSetLanguageWithIODriver(int language, _io_driver *driver)
221 {
222  ConfigParam config;
223 
224  // make sure language is valid
225  if (language < LANGUAGE_JAPANESE || language > LANGUAGE_PORTUGUESE)
226  return;
227  if (IsT10KWithIODriver(driver))
228  g_t10KConfig.language = language;
229 
230  // set language
231  GetOsdConfigParam(&config);
232  if (IsEarlyJap(config))
233  config.japLanguage = language;
234  else
235  config.language = language;
236  SetOsdConfigParam(&config);
237 }
238 #endif
239 
240 #ifdef F_configSetLanguage
241 void configSetLanguage(int language)
242 {
243  _io_driver driver = defaultIODriver;
244  configSetLanguageWithIODriver(language, &driver);
245 }
246 #endif
247 
248 #ifdef F_configGetTvScreenTypeWithIODriver
249 int configGetTvScreenTypeWithIODriver(_io_driver *driver)
250 {
251  ConfigParam config;
252 
253  if (IsT10KWithIODriver(driver))
254  return g_t10KConfig.screenType;
255 
256  GetOsdConfigParam(&config);
257  return config.screenType;
258 }
259 #endif
260 
261 #ifdef F_configGetTvScreenType
262 int configGetTvScreenType(void)
263 {
264  _io_driver driver = defaultIODriver;
265  return configGetTvScreenTypeWithIODriver(&driver);
266 }
267 #endif
268 
269 #ifdef F_configSetTvScreenTypeWithIODriver
270 void configSetTvScreenTypeWithIODriver(int screenType, _io_driver *driver)
271 {
272  ConfigParam config;
273 
274  // make sure screen type is valid
275  if (screenType < TV_SCREEN_43 || screenType > TV_SCREEN_169)
276  return;
277  if (IsT10KWithIODriver(driver))
278  g_t10KConfig.screenType = screenType;
279 
280  // set screen type
281  GetOsdConfigParam(&config);
282  config.screenType = screenType;
283  SetOsdConfigParam(&config);
284 }
285 #endif
286 
287 #ifdef F_configSetTvScreenType
288 void configSetTvScreenType(int screenType)
289 {
290  _io_driver driver = defaultIODriver;
291  configSetTvScreenTypeWithIODriver(screenType, &driver);
292 }
293 #endif
294 
295 #ifdef F_configGetDateFormatWithIODriver
296 int configGetDateFormatWithIODriver(_io_driver *driver)
297 {
298  ConfigParam config;
299  Config2Param config2;
300 
301  if (IsT10KWithIODriver(driver))
302  return g_t10KConfig.dateFormat;
303 
304  GetOsdConfigParam(&config);
305  if (IsEarlyJap(config))
306  return 0;
307  GetOsdConfigParam2(&config2, sizeof(config2), 0);
308  return config2.dateFormat;
309 }
310 #endif
311 
312 #ifdef F_configGetDateFormat
313 int configGetDateFormat(void)
314 {
315  _io_driver driver = defaultIODriver;
316  return configGetDateFormatWithIODriver(&driver);
317 }
318 #endif
319 
320 #ifdef F_configSetDateFormatWithIODriver
321 void configSetDateFormatWithIODriver(int dateFormat, _io_driver *driver)
322 {
323  ConfigParam config;
324  Config2Param config2;
325 
326  // make sure date format is valid
327  if (dateFormat < DATE_YYYYMMDD || dateFormat > DATE_DDMMYYYY)
328  return;
329  if (IsT10KWithIODriver(driver))
330  g_t10KConfig.dateFormat = dateFormat;
331 
332  // set date format
333  GetOsdConfigParam(&config);
334  if (IsEarlyJap(config))
335  return;
336  GetOsdConfigParam2(&config2, sizeof(config2), 0);
337  config2.dateFormat = dateFormat;
338  SetOsdConfigParam2(&config2, sizeof(config2), 0);
339 }
340 #endif
341 
342 #ifdef F_configSetDateFormat
343 void configSetDateFormat(int dateFormat)
344 {
345  _io_driver driver = defaultIODriver;
346  configSetDateFormatWithIODriver(dateFormat, &driver);
347 }
348 #endif
349 
350 #ifdef F_configGetTimeFormatWithIODriver
351 int configGetTimeFormatWithIODriver(_io_driver *driver)
352 {
353  ConfigParam config;
354  Config2Param config2;
355 
356  if (IsT10KWithIODriver(driver))
357  return g_t10KConfig.timeFormat;
358 
359  GetOsdConfigParam(&config);
360  if (IsEarlyJap(config))
361  return 0;
362  GetOsdConfigParam2(&config2, sizeof(config2), 0);
363  return config2.timeFormat;
364 }
365 #endif
366 
367 #ifdef F_configGetTimeFormat
368 int configGetTimeFormat(void)
369 {
370  _io_driver driver = defaultIODriver;
371  return configGetTimeFormatWithIODriver(&driver);
372 }
373 #endif
374 
375 #ifdef F_configSetTimeFormatWithIODriver
376 void configSetTimeFormatWithIODriver(int timeFormat, _io_driver *driver)
377 {
378  ConfigParam config;
379  Config2Param config2;
380 
381  // make sure time format is valid
382  if (timeFormat < TIME_24H || timeFormat > TIME_12H)
383  return;
384  if (IsT10KWithIODriver(driver))
385  g_t10KConfig.timeFormat = timeFormat;
386 
387  // set time format
388  GetOsdConfigParam(&config);
389  if (IsEarlyJap(config))
390  return;
391  GetOsdConfigParam2(&config2, sizeof(config2), 0);
392  config2.timeFormat = timeFormat;
393  SetOsdConfigParam2(&config2, sizeof(config2), 0);
394 }
395 #endif
396 
397 #ifdef F_configSetTimeFormat
398 void configSetTimeFormat(int timeFormat)
399 {
400  _io_driver driver = defaultIODriver;
401  configSetTimeFormatWithIODriver(timeFormat, &driver);
402 }
403 #endif
404 
405 #ifdef F_configGetTimezoneWithIODriver
406 int configGetTimezoneWithIODriver(_io_driver *driver)
407 {
408  ConfigParam config;
409  int timezoneOffset;
410 
411  if (IsT10KWithIODriver(driver))
412  {
413  timezoneOffset = g_t10KConfig.timezoneOffset;
414  }
415  else
416  {
417  GetOsdConfigParam(&config);
418  if (IsEarlyJap(config))
419  {
420  timezoneOffset = 540;
421  }
422  else
423  {
424  timezoneOffset = config.timezoneOffset;
425  // Check if this is negative, and manually make it positive using bit manipulation
426  if ((timezoneOffset & 0x400) != 0)
427  {
428  // Flip bits
429  timezoneOffset ^= 0x7ff;
430  // Add one
431  timezoneOffset += 1;
432  // Make it negative
433  timezoneOffset *= -1;
434  }
435  }
436  }
437  return timezoneOffset;
438 }
439 #endif
440 
441 #ifdef F_configGetTimezone
442 int configGetTimezone(void)
443 {
444  _io_driver driver = defaultIODriver;
445  return configGetTimezoneWithIODriver(&driver);
446 }
447 #endif
448 
449 #ifdef F_configSetTimezoneWithIODriver
450 void configSetTimezoneWithIODriver(int timezoneOffset, _io_driver *driver, void (*finishedCallback)(void))
451 {
452  ConfigParam config;
453 
454  // set offset from GMT
455  if (IsT10KWithIODriver(driver))
456  g_t10KConfig.timezoneOffset = timezoneOffset;
457 
458  GetOsdConfigParam(&config);
459  if (IsEarlyJap(config))
460  return;
461 
462  {
463  u32 wantedTimezoneOffset;
464 
465  // Reduce it to signed 11 bits if it is negative using bit manipulation
466  if (timezoneOffset < 0)
467  {
468  // Make it positive
469  wantedTimezoneOffset = -timezoneOffset;
470  // Subtract one
471  wantedTimezoneOffset -= 1;
472  // Flip bits
473  wantedTimezoneOffset ^= 0x7ff;
474  }
475  else
476  {
477  wantedTimezoneOffset = timezoneOffset;
478  }
479 
480  config.timezoneOffset = wantedTimezoneOffset;
481  }
482 
483  SetOsdConfigParam(&config);
484  if (finishedCallback)
485  finishedCallback();
486 }
487 #endif
488 
489 #ifdef F_configSetTimezone
490 void configSetTimezone(int timezoneOffset)
491 {
492  _io_driver driver = defaultIODriver;
493  configSetTimezoneWithIODriver(timezoneOffset, &driver, NULL);
494 }
495 #endif
496 
497 #ifdef F_configIsSpdifEnabledWithIODriver
498 int configIsSpdifEnabledWithIODriver(_io_driver *driver)
499 {
500  ConfigParam config;
501 
502  if (IsT10KWithIODriver(driver))
503  return g_t10KConfig.spdifMode ^ 1;
504 
505  GetOsdConfigParam(&config);
506  return config.spdifMode ^ 1;
507 }
508 #endif
509 
510 #ifdef F_configIsSpdifEnabled
511 int configIsSpdifEnabled(void)
512 {
513  _io_driver driver = defaultIODriver;
514  return configIsSpdifEnabledWithIODriver(&driver);
515 }
516 #endif
517 
518 #ifdef F_configSetSpdifEnabledWithIODriver
519 void configSetSpdifEnabledWithIODriver(int enabled, _io_driver *driver)
520 {
521  ConfigParam config;
522 
523  if (IsT10KWithIODriver(driver))
524  g_t10KConfig.spdifMode = enabled ^ 1;
525 
526  GetOsdConfigParam(&config);
527  config.spdifMode = enabled ^ 1;
528  SetOsdConfigParam(&config);
529 }
530 #endif
531 
532 #ifdef F_configSetSpdifEnabled
533 void configSetSpdifEnabled(int enabled)
534 {
535  _io_driver driver = defaultIODriver;
536  configSetSpdifEnabledWithIODriver(enabled, &driver);
537 }
538 #endif
539 
540 #ifdef F_configIsDaylightSavingEnabledWithIODriver
541 int configIsDaylightSavingEnabledWithIODriver(_io_driver *driver)
542 {
543  ConfigParam config;
544  Config2Param config2;
545 
546  if (IsT10KWithIODriver(driver))
547  return g_t10KConfig.daylightSaving;
548 
549  GetOsdConfigParam(&config);
550  if (IsEarlyJap(config))
551  return 0;
552  GetOsdConfigParam2(&config2, sizeof(config2), 0);
553 
554  return config2.daylightSaving;
555 }
556 #endif
557 
558 #ifdef F_configIsDaylightSavingEnabled
560 {
561  _io_driver driver = defaultIODriver;
562  return configIsDaylightSavingEnabledWithIODriver(&driver);
563 }
564 #endif
565 
566 #ifdef F_configSetDaylightSavingEnabledWithIODriver
567 void configSetDaylightSavingEnabledWithIODriver(int daylightSaving, _io_driver *driver, void (*finishedCallback)(void))
568 {
569  ConfigParam config;
570  Config2Param config2;
571 
572  if (IsT10KWithIODriver(driver))
573  g_t10KConfig.daylightSaving = daylightSaving;
574 
575  GetOsdConfigParam(&config);
576  if (IsEarlyJap(config))
577  return;
578  GetOsdConfigParam2(&config2, sizeof(config2), 0);
579  config2.daylightSaving = daylightSaving;
580  SetOsdConfigParam2(&config2, sizeof(config2), 0);
581  if (finishedCallback)
582  finishedCallback();
583 }
584 #endif
585 
586 #ifdef F_configSetDaylightSavingEnabled
587 void configSetDaylightSavingEnabled(int daylightSaving)
588 {
589  _io_driver driver = defaultIODriver;
590  configSetDaylightSavingEnabledWithIODriver(daylightSaving, &driver, NULL);
591 }
592 #endif
593 
594 #ifdef F_configConvertToGmtTime
596 {
597  __adjustTime(time, -540);
598 }
599 #endif
600 
601 #ifdef F_configConvertToLocalTimeWithIODriver
602 void configConvertToLocalTimeWithIODriver(sceCdCLOCK *time, _io_driver *driver)
603 {
604  int timezone_offset = configGetTimezoneWithIODriver(driver);
605  int daylight_saving = configIsDaylightSavingEnabledWithIODriver(driver);
606  __adjustTime(time, timezone_offset - 540 + (daylight_saving * 60));
607 }
608 #endif
609 
610 #ifdef F_configConvertToLocalTime
611 void configConvertToLocalTime(sceCdCLOCK *time)
612 {
613  _io_driver driver = defaultIODriver;
614  configConvertToLocalTimeWithIODriver(time, &driver);
615 }
616 #endif
kernel.h
configSetTvScreenType
void configSetTvScreenType(int screenType)
Config2Param::timeFormat
u8 timeFormat
Definition: osd_config.h:113
IsEarlyJap
int IsEarlyJap(ConfigParam config)
ConfigParam::spdifMode
u32 spdifMode
Definition: osd_config.h:85
sceCdCLOCK
Definition: libcdvd-common.h:188
rom0_info.h
ConfigParam::language
u32 language
Definition: osd_config.h:97
configSetDaylightSavingEnabled
void configSetDaylightSavingEnabled(int enabled)
configGetTvScreenType
int configGetTvScreenType(void)
ConfigParam
Definition: osd_config.h:82
ConfigParam::version
u32 version
Definition: osd_config.h:95
fileio.h
ConfigParam::screenType
u32 screenType
Definition: osd_config.h:87
Config2Param::dateFormat
u8 dateFormat
Definition: osd_config.h:115
ConfigParam::timezoneOffset
u32 timezoneOffset
Definition: osd_config.h:99
configGetTimezone
int configGetTimezone(void)
configGetLanguage
int configGetLanguage(void)
configGetDateFormat
int configGetDateFormat(void)
_io_driver
Definition: rom0_info.h:24
configIsDaylightSavingEnabled
int configIsDaylightSavingEnabled(void)
configGetTimeFormat
int configGetTimeFormat(void)
configSetTimeFormat
void configSetTimeFormat(int timeFormat)
configSetLanguage
void configSetLanguage(int language)
tamtypes.h
stdio.h
configConvertToGmtTime
void configConvertToGmtTime(sceCdCLOCK *time)
ConfigParam::japLanguage
u32 japLanguage
Definition: osd_config.h:91
configSetDateFormat
void configSetDateFormat(int dateFormat)
Config2Param
Definition: osd_config.h:105
ConfigParamT10K
Definition: osd_config.c:30
Config2Param::daylightSaving
u8 daylightSaving
Definition: osd_config.h:111
osd_config.h
configSetSpdifEnabled
void configSetSpdifEnabled(int enabled)
configSetTimezone
void configSetTimezone(int offset)
configIsSpdifEnabled
int configIsSpdifEnabled(void)