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