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 
28 typedef struct
29 {
30  s16 timezoneOffset;
31  u8 screenType;
32  u8 dateFormat;
33  u8 language;
34  u8 spdifMode;
35  u8 daylightSaving;
36  u8 timeFormat;
38 
39 extern ConfigParamT10K g_t10KConfig;
40 
41 #ifdef F__config_internals
42 ConfigParamT10K g_t10KConfig;
43 
44 __attribute__((constructor))
45 static void __t10KConfigInitialize(void)
46 {
47  g_t10KConfig.timezoneOffset = 540;
48  g_t10KConfig.screenType = TV_SCREEN_43;
49  g_t10KConfig.dateFormat = DATE_YYYYMMDD;
50  g_t10KConfig.language = LANGUAGE_JAPANESE;
51 }
52 #endif
53 
54 #ifdef F_converttobcd
55 static inline unsigned char tobcd(unsigned char dec)
56 {
57  return dec + (dec / 10) * 6;
58 }
59 
60 void converttobcd(sceCdCLOCK *time)
61 {
62  time->second = tobcd(time->second);
63  time->minute = tobcd(time->minute);
64  time->hour = tobcd(time->hour);
65  time->day = tobcd(time->day);
66  time->month = tobcd(time->month);
67  time->year = tobcd(time->year);
68 }
69 #else
70 extern void converttobcd(sceCdCLOCK *time);
71 #endif
72 
73 #ifdef F_convertfrombcd
74 static inline unsigned char frombcd(unsigned char bcd)
75 {
76  return bcd - (bcd >> 4) * 6;
77 }
78 
79 
80 void convertfrombcd(sceCdCLOCK *time)
81 {
82  time->second = frombcd(time->second);
83  time->minute = frombcd(time->minute);
84  time->hour = frombcd(time->hour);
85  time->day = frombcd(time->day);
86  time->month = frombcd(time->month);
87  time->year = frombcd(time->year);
88 }
89 #else
90 extern void convertfrombcd(sceCdCLOCK *time);
91 #endif
92 
93 #ifdef F___adjustTime
94 static const unsigned char gDaysInMonths[12] = {
95  31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
96 
97 static void adddate(sceCdCLOCK *time)
98 {
99  // get the days in each month and fix up feb depending on leap year
100  unsigned char days_in_months[12];
101  memcpy(days_in_months, gDaysInMonths, 12);
102  if ((time->year & 3) == 0)
103  days_in_months[1] = 29;
104 
105  // increment the day and check its within the "day of the month" bounds
106  time->day++;
107  if (time->day > days_in_months[time->month - 1]) {
108  time->day = 1;
109 
110  // increment the month and check its within the "months in a year" bounds
111  time->month++;
112  if (time->month == 13) {
113  time->month = 1;
114 
115  // check the year and increment it
116  time->year++;
117  if (time->year == 100) {
118  time->year = 0;
119  }
120  }
121  }
122 }
123 
124 static void subdate(sceCdCLOCK *time)
125 {
126  // get the days in each month and fix up feb depending on leap year
127  unsigned char days_in_months[12];
128  memcpy(days_in_months, gDaysInMonths, 12);
129  if ((time->year & 3) == 0)
130  days_in_months[1] = 29;
131 
132  // decrement the day and check its within the "day of the month" bounds
133  time->day--;
134  if (time->day == 0) {
135  // decrement the month and check its within the "months in a year" bounds
136  time->month--;
137  if (time->month == 0) {
138  time->month = 12;
139 
140  // check the year and decrement it
141  if (time->year == 0)
142  time->year = 99;
143  else
144  time->year--;
145  }
146 
147  time->day = days_in_months[time->month - 1];
148  }
149 }
150 
151 static void addhour(sceCdCLOCK *time)
152 {
153  time->hour++;
154  if (time->hour == 24) {
155  adddate(time);
156  time->hour = 0;
157  }
158 }
159 
160 static void subhour(sceCdCLOCK *time)
161 {
162  if (time->hour == 0) {
163  subdate(time);
164  time->hour = 23;
165  } else
166  time->hour--;
167 }
168 
169 void __adjustTime(sceCdCLOCK *time, int offset)
170 {
171  convertfrombcd(time);
172  offset += time->minute;
173 
174  if (offset >= 0) {
175  while (offset >= 60) {
176  addhour(time);
177  offset -= 60;
178  }
179  time->minute = offset;
180  } else {
181  while (offset < 0) {
182  subhour(time);
183  offset += 60;
184  }
185  time->minute = offset;
186  }
187 
188  converttobcd(time);
189 }
190 #else
191 extern void __adjustTime(sceCdCLOCK *time, int offset);
192 #endif
193 
194 #ifdef F_IsEarlyJap
195 int IsEarlyJap(ConfigParam config)
196 {
197  return config.version == 0;
198 }
199 #endif
200 
201 #ifdef F_configGetLanguage
202 int configGetLanguage(void)
203 {
204  ConfigParam config;
205 
206  if (IsT10K())
207  return g_t10KConfig.language;
208 
209  GetOsdConfigParam(&config);
210  if (IsEarlyJap(config))
211  return config.japLanguage;
212  return config.language;
213 }
214 #endif
215 
216 #ifdef F_configSetLanguage
217 void configSetLanguage(int language)
218 {
219  ConfigParam config;
220 
221  // make sure language is valid
222  if (language < LANGUAGE_JAPANESE || language > LANGUAGE_PORTUGUESE)
223  return;
224  if (IsT10K())
225  g_t10KConfig.language = language;
226 
227  // set language
228  GetOsdConfigParam(&config);
229  if (IsEarlyJap(config))
230  config.japLanguage = language;
231  else
232  config.language = language;
233  SetOsdConfigParam(&config);
234 }
235 #endif
236 
237 #ifdef F_configGetTvScreenType
238 int configGetTvScreenType(void)
239 {
240  ConfigParam config;
241 
242  if (IsT10K())
243  return g_t10KConfig.screenType;
244 
245  GetOsdConfigParam(&config);
246  return config.screenType;
247 }
248 #endif
249 
250 #ifdef F_configSetTvScreenType
251 void configSetTvScreenType(int screenType)
252 {
253  ConfigParam config;
254 
255  // make sure screen type is valid
256  if (screenType < TV_SCREEN_43 || screenType > TV_SCREEN_169)
257  return;
258  if (IsT10K())
259  g_t10KConfig.screenType = screenType;
260 
261  // set screen type
262  GetOsdConfigParam(&config);
263  config.screenType = screenType;
264  SetOsdConfigParam(&config);
265 }
266 #endif
267 
268 #ifdef F_configGetDateFormat
269 int configGetDateFormat(void)
270 {
271  ConfigParam config;
272  Config2Param config2;
273 
274  if (IsT10K())
275  return g_t10KConfig.dateFormat;
276 
277  GetOsdConfigParam(&config);
278  if (IsEarlyJap(config))
279  return 0;
280  GetOsdConfigParam2(&config2, sizeof(config2), 0);
281  return config2.dateFormat;
282 }
283 #endif
284 
285 #ifdef F_configSetDateFormat
286 void configSetDateFormat(int dateFormat)
287 {
288  ConfigParam config;
289  Config2Param config2;
290 
291  // make sure date format is valid
292  if (dateFormat < DATE_YYYYMMDD || dateFormat > DATE_DDMMYYYY)
293  return;
294  if (IsT10K())
295  g_t10KConfig.dateFormat = dateFormat;
296 
297  // set date format
298  GetOsdConfigParam(&config);
299  if (IsEarlyJap(config))
300  return;
301  GetOsdConfigParam2(&config2, sizeof(config2), 0);
302  config2.dateFormat = dateFormat;
303  SetOsdConfigParam2(&config2, sizeof(config2), 0);
304 }
305 #endif
306 
307 #ifdef F_configGetTimeFormat
308 int configGetTimeFormat(void)
309 {
310  ConfigParam config;
311  Config2Param config2;
312 
313  if (IsT10K())
314  return g_t10KConfig.timeFormat;
315 
316  GetOsdConfigParam(&config);
317  if (IsEarlyJap(config))
318  return 0;
319  GetOsdConfigParam2(&config2, sizeof(config2), 0);
320  return config2.timeFormat;
321 }
322 #endif
323 
324 #ifdef F_configSetTimeFormat
325 void configSetTimeFormat(int timeFormat)
326 {
327  ConfigParam config;
328  Config2Param config2;
329 
330  // make sure time format is valid
331  if (timeFormat < TIME_24H || timeFormat > TIME_12H)
332  return;
333  if (IsT10K())
334  g_t10KConfig.timeFormat = timeFormat;
335 
336  // set time format
337  GetOsdConfigParam(&config);
338  if (IsEarlyJap(config))
339  return;
340  GetOsdConfigParam2(&config2, sizeof(config2), 0);
341  config2.timeFormat = timeFormat;
342  SetOsdConfigParam2(&config2, sizeof(config2), 0);
343 }
344 #endif
345 
346 #ifdef F_configGetTimezone
347 int configGetTimezone(void)
348 {
349  ConfigParam config;
350  int timezoneOffset;
351 
352  if (IsT10K())
353  {
354  timezoneOffset = g_t10KConfig.timezoneOffset;
355  }
356  else
357  {
358  GetOsdConfigParam(&config);
359  if (IsEarlyJap(config))
360  {
361  timezoneOffset = 540;
362  }
363  else
364  {
365  timezoneOffset = config.timezoneOffset;
366  // Check if this is negative, and manually make it positive using bit manipulation
367  if ((timezoneOffset & 0x400) != 0)
368  {
369  // Flip bits
370  timezoneOffset ^= 0x7ff;
371  // Add one
372  timezoneOffset += 1;
373  // Make it negative
374  timezoneOffset *= -1;
375  }
376  }
377  }
378  return timezoneOffset;
379 }
380 #endif
381 
382 #ifdef F_configSetTimezone
383 void configSetTimezone(int timezoneOffset)
384 {
385  ConfigParam config;
386 
387  // set offset from GMT
388  if (IsT10K())
389  g_t10KConfig.timezoneOffset = timezoneOffset;
390 
391  GetOsdConfigParam(&config);
392  if (IsEarlyJap(config))
393  return;
394 
395  {
396  u32 wantedTimezoneOffset;
397 
398  // Reduce it to signed 11 bits if it is negative using bit manipulation
399  if (timezoneOffset < 0)
400  {
401  // Make it positive
402  wantedTimezoneOffset = -timezoneOffset;
403  // Subtract one
404  wantedTimezoneOffset -= 1;
405  // Flip bits
406  wantedTimezoneOffset ^= 0x7ff;
407  }
408  else
409  {
410  wantedTimezoneOffset = timezoneOffset;
411  }
412 
413  config.timezoneOffset = wantedTimezoneOffset;
414  }
415 
416  SetOsdConfigParam(&config);
417 }
418 #endif
419 
420 #ifdef F_configIsSpdifEnabled
421 int configIsSpdifEnabled(void)
422 {
423  ConfigParam config;
424 
425  if (IsT10K())
426  return g_t10KConfig.spdifMode ^ 1;
427 
428  GetOsdConfigParam(&config);
429  return config.spdifMode ^ 1;
430 }
431 #endif
432 
433 #ifdef F_configSetSpdifEnabled
434 void configSetSpdifEnabled(int enabled)
435 {
436  ConfigParam config;
437 
438  if (IsT10K())
439  g_t10KConfig.spdifMode = enabled ^ 1;
440 
441  GetOsdConfigParam(&config);
442  config.spdifMode = enabled ^ 1;
443  SetOsdConfigParam(&config);
444 }
445 #endif
446 
447 #ifdef F_configIsDaylightSavingEnabled
449 {
450  ConfigParam config;
451  Config2Param config2;
452 
453  if (IsT10K())
454  return g_t10KConfig.daylightSaving;
455 
456  GetOsdConfigParam(&config);
457  if (IsEarlyJap(config))
458  return 0;
459  GetOsdConfigParam2(&config2, sizeof(config2), 0);
460 
461  return config2.daylightSaving;
462 }
463 #endif
464 
465 #ifdef F_configSetDaylightSavingEnabled
466 void configSetDaylightSavingEnabled(int daylightSaving)
467 {
468  ConfigParam config;
469  Config2Param config2;
470 
471  if (IsT10K())
472  g_t10KConfig.daylightSaving = daylightSaving;
473 
474  GetOsdConfigParam(&config);
475  if (IsEarlyJap(config))
476  return;
477  GetOsdConfigParam2(&config2, sizeof(config2), 0);
478  config2.daylightSaving = daylightSaving;
479  SetOsdConfigParam2(&config2, sizeof(config2), 0);
480 }
481 #endif
482 
483 #ifdef F_configConvertToGmtTime
485 {
486  __adjustTime(time, -540);
487 }
488 #endif
489 
490 #ifdef F_configConvertToLocalTime
492 {
493  int timezone_offset = configGetTimezone();
494  int daylight_saving = configIsDaylightSavingEnabled();
495  __adjustTime(time, timezone_offset - 540 + (daylight_saving * 60));
496 }
497 #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
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)
IsT10K
int IsT10K(void)
configGetLanguage
int configGetLanguage(void)
configGetDateFormat
int configGetDateFormat(void)
__attribute__
typedef __attribute__
Definition: tlbfunc.c:60
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:28
Config2Param::daylightSaving
u8 daylightSaving
Definition: osd_config.h:111
osd_config.h
configSetSpdifEnabled
void configSetSpdifEnabled(int enabled)
configConvertToLocalTime
void configConvertToLocalTime(sceCdCLOCK *time)
configSetTimezone
void configSetTimezone(int offset)
configIsSpdifEnabled
int configIsSpdifEnabled(void)