source: svn/newcon3bcm2_21bu/nexus/app/nanotv/bapp_util.c @ 22

Last change on this file since 22 was 22, checked in by phkim, 11 years ago
  1. phkim
  2. newcon3sk 를 kctv 로 브랜치 함
  • Property svn:executable set to *
File size: 14.0 KB
Line 
1/***************************************************************************
2 *     Copyright (c) 2003-2006, Broadcom Corporation
3 *     All Rights Reserved
4 *     Confidential Property of Broadcom Corporation
5 *
6 *  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
7 *  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
8 *  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
9 *
10 * $brcm_Workfile:  $
11 * $brcm_Revision:  $
12 * $brcm_Date: $
13 *
14 * Module Description:
15 *
16 * Revision History:
17 *
18 * $brcm_Log:  $
19 *
20 ***************************************************************************/
21#include "bapp_util.h"
22#include "bgfx.h"
23#include "bstd.h"
24#define BAPP_UTIL_DBGMSG(x) printf x
25#ifdef OLD_TEXTBOX
26static unsigned long s_test_char = 0x00000057UL;
27#endif
28
29
30static unsigned int rand_table_idx = 0;
31static const unsigned short rand_table[] = 
32{
33    840,394,783,798,911,197,335,768,277,553,
34    477,628,364,513,952,916,635,717,141,606,
35    16,242,137,804,156,400,129,108,998,218,
36    512,839,612,296,637,524,493,972,292,771,
37    526,769,400,891,283,352,807,919,69,949,
38    525,86,192,663,890,348,64,20,457,63,
39    238,970,902,850,266,539,375,760,512,667,
40    531,39,437,931,930,720,284,738,639,354,
41    687,165,440,880,829,330,228,893,350,686,
42    956,588,657,858,439,923,398,814,684,910,
43    482,215,950,920,147,881,641,431,619,281,
44    786,307,447,226,187,276,556,416,169,906,
45    103,126,495,760,984,935,684,383,749,368,
46    294,232,584,244,152,732,125,793,164,745,
47    74,950,52,521,176,240,797,732,656,967,
48    639,759,93,134,520,78,69,204,461,819,
49    573,755,51,157,999,204,889,125,997,54,
50    870,72,4,923,593,180,163,391,913,819,
51    359,552,579,452,687,99,530,757,304,992,
52    576,877,747,628,35,747,833,925,873,831,
53    979,743,903,983,666,497,163,830,888,76,
54    649,248,629,229,700,316,328,231,74,633,
55    223,651,510,971,280,546,719,113,471,592,
56    944,450,336,847,434,3,344,598,833,233,
57    675,482,481,304,712,182,621,40,413,695,
58    673,637,347,184,609,627,730,328,740,202,
59    920,684,653,257,532,87,260,877,686,93,
60    111,361,576,593,666,288,775,288,329,189,
61    984,3,827,331,188,436,958,918,764,699,
62    121,685,383,774,943,916,861,203,793,548,
63    297,904,909,873,498,576,162,273,864,492,
64    463,848,495,291,180,684,727,139,603,492,
65    838,724,178,221,498,121,138,360,324,931,
66    908,622,836,818,496,334,394,658,608,258,
67    151,72,107,647,363,288,331,91,427,934,
68    583,265,658,761,487,157,883,625,517,207,
69    557,426,829,394,244,326,729,638,984,338,
70    897,136,410,5,783,774,293,114,865,721,
71    49,449,986,707,210,473,865,93,99,382,
72    301,657,809,131,51,53,457,780,692,442,
73    119,589,578,529,595,361,304,888,476,169,
74    609,525,618,596,233,829,70,98,923,169,
75    481,225,826,290,357,878,344,814,659,36,
76    257,778,625,836,308,221,198,612,109,674,
77    782,719,200,401,315,434,230,385,532,154,
78    555,14,380,382,305,737,260,649,552,919,
79    685,809,697,311,645,6,532,843,618,642,
80    518,400,362,718,801,677,152,32,63,685,
81    187,618,700,567,1,5,305,261,655,857,
82    181,341,667,879,653,313,885,186,157,503,
83    828,675,904,191,394,706,868,547,738,932,
84    233,926,551,933,494,552,939,799,814,594,
85    657,995,935,324,874,589,637,759,775,794,
86    262,604,470,166,795,865,873,664,412,611,
87    596,645,538,148,579,32,700,518,832,515,
88    112,489,510,48,814,384,637,452,143,413,
89    247,406,17,717,573,812,582,446,477,995,
90    58,74,640,597,222,219,630,923,737,462,
91    438,850,952,948,899,767,333,536,219,477,
92    949,466,884,967,183,458,780,766,904,257,
93    761,963,331,402,560,554,622,191,477,360,
94    653,916,210,606,865,109,373,199,646,592,
95    676,596,58,560,563,242,18,343,9,923,
96    601,770,887,933,173,447,487,795,639,965,
97    155,292,882,366,899,747,475,272,946,122,
98    865,623,718,924,184,282,167,202,626,176,
99    126,227,946,13,160,119,461,648,915,100,
100    614,70,393,496,436,293,244,912,566,190,
101    34,431,813,753,356,997,35,523,200,661,
102    699,327,889,646,341,50,766,803,698,681,
103    904,312,752,297,809,189,591,53,101,157,
104    244,136,589,58,889,945,56,925,469,256,
105    587,168,584,476,815,926,526,582,729,225,
106    264,633,538,16,931,347,205,522,400,307,
107    679,645,443,269,703,332,214,759,258,683,
108    16,845,852,600,321,667,526,848,250,256,
109    73,514,889,611,531,821,958,736,343,359,
110    43,23,5,487,292,708,820,507,467,78,
111    190,483,923,43,84,244,711,611,92,961,
112    867,166,475,757,777,6,578,736,743,922,
113    96,787,946,101,274,239,809,95,746,277,
114    173,937,760,96,981,845,341,692,456,434,
115    654,323,600,129,81,377,136,659,114,880,
116    582,210,668,528,312,943,768,122,38,514,
117    399,211,452,160,308,433,5,649,126,461,
118    84,780,785,684,910,867,62,47,527,177,
119    927,109,387,596,638,700,539,406,822,577,
120    921,221,789,374,381,97,807,387,747,934,
121    849,831,714,635,516,624,502,578,671,29,
122    755,599,139,143,195,777,844,735,184,666,
123    312,105,888,102,479,270,199,287,657,947,
124    221,506,778,936,142,294,561,644,873,232,
125    673,629,832,812,773,28,590,617,763,774,
126    284,76,880,172,178,359,443,378,647,100,
127    325,869,607,104,805,749,398,366,394,272,
128    599,68,901,432,881,674,460,471,292,224,
129    246,576,301,126,749,480,485,192,858,133,
130    293,184,2,900,288,808,650,687,175,44,
131    959,775,112,861,207,994,536,667,465,828,
132    892,711,405,193,837,154,673,323,347,532,
133};
134
135unsigned int rand(void)
136{
137    unsigned int reg ;
138    reg = rand_table[rand_table_idx++];
139    if (rand_table_idx == RAND_MAX)
140    {
141        rand_table_idx = 0;
142    }
143    return reg;
144}
145
146#ifdef CONFIG_CHINESE_FONTS_RAM
147
148#define isaf(c)         ((c) >= 'a' && (c) <= 'f')
149#define isAF(c)         ((c) >= 'A' && (c) <= 'Z')
150#define isdigit(c)      ((c) >= '0' && (c) <= '9')
151
152/*
153Summary:
154    Convert string to int value
155*/
156unsigned int str_to_int(unsigned char *str_p, int bytes)
157{
158        int i, value = 0;
159        unsigned char c, *p;
160
161        p = str_p;
162
163        for (i = 0; i < bytes; i++) {
164                value <<= 4;
165                c = *p++;
166                if (isdigit(c))
167                        value |= (c - '0');
168                else if (isaf(c))
169                        value |= (c - 'a') + 10;
170                else if (isAF(c))
171                        value |= (c - 'A') + 10;
172                else
173                        return 0;
174        }
175        return value;
176}
177#endif
178
179/*
180Summary:
181    Construct a UNI string in the buffer provided from the cstring.
182*/
183unsigned int c_to_uni_str(
184                        unsigned char *str_p,                /* Null terminated c-string */
185                        unsigned int *p_uni_str,    /* buffer to use for contructing UNI string */
186                        unsigned int max_size)      /* maximum buffer size in words */
187{
188    unsigned int i,num_chars;
189#ifdef CONFIG_CHINESE_FONTS_RAM
190    unsigned char *p;
191#endif
192
193#ifdef BCM_DEBUG
194        if (!p_uni_str || !str_p)
195        {
196                BAPP_UTIL_DBGMSG(("%d bad param (0x%08x, 0x%08x, %d)\n", str_p,p_uni_str,max_size));
197                return 0;
198        }
199#endif
200    num_chars = strlen(str_p);
201    if (num_chars > max_size)
202        num_chars = max_size;
203
204#ifdef CONFIG_CHINESE_FONTS_RAM
205    /* test last 4 bytes to see if it is unicode? */
206    if ((num_chars > 3 && 0 == num_chars % 4) && (0 == (str_to_int(str_p + num_chars - 4, 4)))) {
207        num_chars = strlen(str_p) >> 2;         /* four bytes each */
208        p = str_p;
209        for (i = 0; i < num_chars; ++i, p += 4) {
210                p_uni_str[i] = str_to_int(p, 4);
211        }
212    }
213    else
214#endif
215    for (i = 0; i < strlen(str_p); ++i)
216    {
217        p_uni_str[i] = (unsigned int)(str_p[i]);
218    }
219
220    return num_chars;
221}
222
223/*
224Summary:
225    Construct a UNI string in the buffer provided from the null terminated UTF-16 string.
226*/
227unsigned int utf16_to_uni_str(
228                        unsigned short *str_p,       /* NULL terminated (0x0000) UTF - 16 string */
229                        unsigned int *p_uni_str,    /* buffer to use for contructing UNI string */
230                        unsigned int max_size)      /* maximum buffer size in words */
231{
232    unsigned int i = 0;
233
234    while ((str_p[i] != 0x0000) && (i < max_size))
235    {
236        p_uni_str[i] = (unsigned int)(str_p[i]);
237        i++;
238    }
239    return i;
240}
241
242/*
243Summary:
244    Utility IO function.
245Description:
246    Utility IO function to be used for pseudo IO.
247*/
248
249int bin_read( void *buffer, int size, int count, void *fp )
250{
251    bin_read_t *p_br = (bin_read_t*)fp;
252    size = size * count;
253    if (p_br->cnt + size > p_br->size)
254    {
255        BAPP_UTIL_DBGMSG(("Requesting more bytes than available (cnt = %d, size = %d,total = %d)\n",
256                          p_br->cnt,size,p_br->size));
257        size = p_br->size - p_br->cnt;
258    }
259
260    memcpy(buffer,&p_br->data[p_br->cnt],size);
261    p_br->cnt += size;
262    return size;
263}
264/*
265Summary:
266    Utility IO function.
267Description:
268    Utility IO function to be used for pseudo IO.
269*/
270
271unsigned int bin_tell(void *fp )
272{
273    bin_read_t *p_br = (bin_read_t*)fp;
274
275    return p_br->cnt;
276}
277/*
278Summary:
279    Utility IO function.
280Description:
281    Utility IO function to be used for pseudo IO.
282*/
283
284int bin_set(void *fp, int offset, int whence )
285{
286    bin_read_t *p_br = (bin_read_t*)fp;
287
288        if ((whence != 0) || (offset > p_br->size))
289                return -1;
290       
291        p_br->cnt = offset;
292        return 0;
293}
294
295/*
296Summary:
297    finds a break in the string (space).
298Description:
299    finds a break in the string (space).
300*/
301unsigned int find_break_uni(
302                            unsigned int* str,  /* UNI string */
303                            unsigned int max            /* Number of characters in UNI string */
304                            )
305{
306    unsigned int off = max - 1;
307    while ((off > 0) && (str[off--] != 0x00000020))
308    {
309    }
310    if (off == 0)
311        off = max;
312    else
313        off++;
314    return off;
315}
316
317#ifdef OLD_TEXTBOX
318/*
319Summary:
320    Draw text at x,y and within the specified w,h.
321Description:
322    Draw text at x,y and within the specified w,h.
323*/
324int text_box(bgfx_surf_t *p_surf,               /* bgfx surface */
325             bgfx_font_t *font,                 /* bgfx font */
326            uint16_t x,                         /* x location in pixels */
327            uint16_t y,                         /* y location in pixels */
328            uint16_t width,                     /* width in pixels */
329            uint16_t height,                    /* height in pixels */ 
330            unsigned int *msg,                  /* UNI string */
331            unsigned int msg_len,               /* Number of characters in the msg */
332            bgfx_pixel c,                       /* palette index */
333            int line_spacing                    /* number of vertical pixels between lines */
334            )
335{
336
337    int cmax,xloc,yloc,fheight,chrcnt,maxchrs,boff,w,h;
338    unsigned int *tstr = msg;
339    /* setup the default character configurations */
340    chrcnt = msg_len;
341    /* Get maximum character params */
342    bgfx_string_info(font,&s_test_char,1,&cmax,&fheight);
343
344    maxchrs = width/cmax;
345    xloc = x; yloc = y + fheight;
346
347    if ((fheight > height) || (cmax > width))
348        goto done;
349
350    do
351    {
352        bgfx_string_info(font,(const unsigned long *)tstr,chrcnt,&w,&h);
353
354        if (w <= width)
355        {
356            bgfx_draw_text(p_surf,xloc,yloc,(const unsigned long *)tstr,chrcnt,font,c);         
357            goto done;
358        }else
359        {
360            /* Stop if there is no more vertical space */
361            if (h > (height - (yloc - y)))
362                goto done;
363
364            boff = find_break_uni(tstr,maxchrs); /* if no break is found returns maxchars */
365            bgfx_draw_text(p_surf,xloc,yloc,(const unsigned long *)tstr,boff,font,c);           
366            tstr += boff + 1; /* +1 to skip space */
367           
368            yloc += (fheight + line_spacing);
369        }
370    }while(1);
371
372done:
373    return 0;
374}
375#endif
376/*
377Summary:
378    Same as text box but with shadow.
379Description:
380    Same as text box but with shadow.
381*/
382int text_box_shadow(bgfx_surf_t *p_surf,       /* bgfx surface */
383             bgfx_font_t *font,                 /* bgfx font */
384            uint16_t x,                         /* x location in pixels */
385            uint16_t y,                         /* y location in pixels */
386            uint16_t width,                     /* width in pixels */
387            uint16_t height,                    /* height in pixels */ 
388            unsigned int *msg,                  /* UNI string */
389            unsigned int msg_len,               /* Number of characters in the msg */
390            bgfx_pixel c_f,                     /* palette index for forground text*/
391            bgfx_pixel c_b,                     /* palette index for background text */
392            int shadow_offset,                 /* offset from xy to place shadow (can be negative) */
393            int line_spacing                    /* number of vertical pixels between lines */
394            )
395{
396#ifdef TEXT_SHADDOW
397    text_box(p_surf,font,x + shadow_offset,y + shadow_offset,width,height,msg,msg_len,c_b,line_spacing);
398#endif
399    text_box(p_surf,font,x,y,width,height,msg,msg_len,c_f,line_spacing);
400    return 0;
401}
402
403/*
404 * Delay for the number of milliseconds
405 */
406
407void bapp_sleep(unsigned int ms)
408{
409#ifndef LINUX
410        bos_sleep(ms);
411#else
412        usleep(ms * 1000);
413#endif
414}
415
416
417/****************************************************************
418 * timeval_subtract
419 *
420 * INPUTS:      x,y - subtract the timevals
421 * OUTPUTS:     result - result of x,y timeval subtract
422 * RETURNS:     none
423 * DESCRITPION: Subtract the `struct timeval' values X and Y,
424 *              storing the result in RESULT.
425 *              Return 1 if the difference is negative, otherwise 0
426 ****************************************************************/
427
428int timeval_subtract (struct timeval *result, struct timeval *x, struct timeval *y)
429{
430        /* Perform the carry for the later subtraction by updating y. */
431        if (x->tv_usec < y->tv_usec)
432        {
433                int nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
434                y->tv_usec -= 1000000 * nsec;
435                y->tv_sec += nsec;
436        }
437        if (x->tv_usec - y->tv_usec > 1000000)
438        {
439                int nsec = (x->tv_usec - y->tv_usec) / 1000000;
440                y->tv_usec += 1000000 * nsec;
441                y->tv_sec -= nsec;
442        }
443
444        /* Compute the time remaining to wait.
445           tv_usec is certainly positive. */
446        result->tv_sec = x->tv_sec - y->tv_sec;
447        result->tv_usec = x->tv_usec - y->tv_usec;
448
449        /* Return 1 if result is negative. */
450        return x->tv_sec < y->tv_sec;
451}
452
453
Note: See TracBrowser for help on using the repository browser.