tarina

git clone https://git.tarina.org/tarina
Log | Files | Refs | README | LICENSE

main.c (12927B)


      1 /*
      2 Copyright (c) 2012, Broadcom Europe Ltd
      3 All rights reserved.
      4 
      5 Redistribution and use in source and binary forms, with or without
      6 modification, are permitted provided that the following conditions are met:
      7     * Redistributions of source code must retain the above copyright
      8       notice, this list of conditions and the following disclaimer.
      9     * Redistributions in binary form must reproduce the above copyright
     10       notice, this list of conditions and the following disclaimer in the
     11       documentation and/or other materials provided with the distribution.
     12     * Neither the name of the copyright holder nor the
     13       names of its contributors may be used to endorse or promote products
     14       derived from this software without specific prior written permission.
     15 
     16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
     17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
     20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26 */
     27 
     28 // Test app for VG font library.
     29 
     30 #include <stdio.h>
     31 #include <stdlib.h>
     32 #include <string.h>
     33 #include <assert.h>
     34 #include <unistd.h>
     35 
     36 #include "bcm_host.h"
     37 #include "vgfont.h"
     38 
     39 int32_t render_subtitle(GRAPHICS_RESOURCE_HANDLE img, const char *text, const uint32_t text_size, const uint32_t text_size_selected, const uint32_t x_offset, const uint32_t y_offset, uint32_t fontcolor)
     40 {
     41     uint32_t height=0;
     42     uint32_t img_w, img_h;
     43 
     44     graphics_get_resource_size(img, &img_w, &img_h);
     45 
     46     // split now points to last line of text. split-text = length of initial text. text_length-(split-text) is length of last line
     47     if (fontcolor == 7) {
     48     graphics_resource_render_text_ext(img, x_offset, y_offset-height,
     49                                      GRAPHICS_RESOURCE_WIDTH,
     50                                      GRAPHICS_RESOURCE_HEIGHT,
     51                                      GRAPHICS_RGBA32(255,255,255,100), /* fg */
     52                                      GRAPHICS_RGBA32(20,20,20,200), /* bg */
     53                                      text, 90, text_size);
     54         }
     55     if (fontcolor == 6) {
     56     graphics_resource_render_text_ext(img, x_offset, y_offset-height,
     57                                      GRAPHICS_RESOURCE_WIDTH,
     58                                      GRAPHICS_RESOURCE_HEIGHT,
     59                                      GRAPHICS_RGBA32(225,255,255,0), /* fg */
     60                                      GRAPHICS_RGBA32(0,0,0,0), /* bg */
     61                                      text, 90, text_size);
     62         }
     63     if (fontcolor == 5) {
     64     graphics_resource_render_text_ext(img, x_offset, y_offset-height,
     65                                      GRAPHICS_RESOURCE_WIDTH,
     66                                      GRAPHICS_RESOURCE_HEIGHT,
     67                                      GRAPHICS_RGBA32(255,255,255,0xff), /* fg */
     68                                      GRAPHICS_RGBA32(0,0,0,150), /* bg */
     69                                      text, 90, text_size);
     70         }
     71     if (fontcolor == 4) {
     72     graphics_resource_render_text_ext(img, x_offset, y_offset-height,
     73                                      GRAPHICS_RESOURCE_WIDTH,
     74                                      GRAPHICS_RESOURCE_HEIGHT,
     75                                      GRAPHICS_RGBA32(30,255,255,0xff), /* fg */
     76                                      GRAPHICS_RGBA32(0,0,0,0), /* bg */
     77                                      text, 90, text_size);
     78         }
     79     if (fontcolor == 3) {
     80     graphics_resource_render_text_ext(img, x_offset, y_offset-height,
     81                                      GRAPHICS_RESOURCE_WIDTH,
     82                                      GRAPHICS_RESOURCE_HEIGHT,
     83                                      GRAPHICS_RGBA32(30,30,255,0xff), /* fg */
     84                                      GRAPHICS_RGBA32(0,0,0,0), /* bg */
     85                                      text, 90, text_size);
     86         }
     87     if (fontcolor == 2) {
     88     graphics_resource_render_text_ext(img, x_offset, y_offset-height,
     89                                      GRAPHICS_RESOURCE_WIDTH,
     90                                      GRAPHICS_RESOURCE_HEIGHT,
     91                                      GRAPHICS_RGBA32(30,255,30,0xff), /* fg */
     92                                      GRAPHICS_RGBA32(0,0,0,0), /* bg */
     93                                      text, 90, text_size);
     94         }
     95     if (fontcolor == 1) {
     96     graphics_resource_render_text_ext(img, x_offset, y_offset-height,
     97                                      GRAPHICS_RESOURCE_WIDTH,
     98                                      GRAPHICS_RESOURCE_HEIGHT,
     99                                      GRAPHICS_RGBA32(0,0,0,0xff), /* fg */
    100                                      GRAPHICS_RGBA32(30,255,255,0xff), /* bg */
    101                                      text, 90, text_size_selected);
    102         }
    103     return 0;
    104     }
    105 
    106 int main(void)
    107 {
    108     GRAPHICS_RESOURCE_HANDLE img;
    109     uint32_t width, height;
    110     int LAYER=10;
    111     bcm_host_init();
    112     int s;
    113 
    114     s = gx_graphics_init(".");
    115     assert(s == 0);
    116 
    117     s = graphics_get_display_size(0, &width, &height);
    118     assert(s == 0);
    119 
    120     s = gx_create_window(0, width, height, GRAPHICS_RESOURCE_RGBA32, &img);
    121     assert(s == 0);
    122 
    123     // transparent before display to avoid screen flash
    124     graphics_resource_fill(img, 0, 0, width, height, GRAPHICS_RGBA32(0,0,0,0x00));
    125 
    126     graphics_display_resource(img, 0, LAYER, 0, 0, GRAPHICS_RESOURCE_WIDTH, GRAPHICS_RESOURCE_HEIGHT, VC_DISPMAN_ROT0, 1);
    127 
    128     uint32_t text_size = 15;
    129     uint32_t text_size_selected = 16;
    130     FILE * fp;
    131     FILE * fp2;
    132     FILE * fp3;
    133     char * line = 0;
    134     //char * b = NULL;
    135     size_t len = 0;
    136     ssize_t read = 0;
    137     int linenr = 0;
    138     int selected;
    139     int showmenu;
    140     int header;
    141     int menuadd = 1;
    142     char newread[500];
    143     char oldread[500];
    144     char vumeter[130];
    145     uint32_t y_offset2 = 0;
    146     uint32_t y_offset3 = 21;
    147     uint32_t y_offset4 = 42;
    148     uint32_t y_offset5 = 421;
    149     uint32_t y_offset6 = 442; 
    150     uint32_t y_offset = 466;
    151     int space = 10;
    152     int morespace = 12;
    153     int rectime = 700;
    154     int color = 3;
    155     int row1 = 0;
    156     int row2 = 0;
    157     int row3 = 0;
    158     int row4 = 0;
    159     int row5 = 0;
    160     if (width == 1920){
    161         y_offset2 = 5;
    162         y_offset3 = 45;
    163         y_offset4 = 85;
    164         y_offset5 = 960;
    165         y_offset6 = 1000;
    166         y_offset = 1040;
    167         rectime = 1600;
    168         text_size = 30;
    169         text_size_selected = 32;
    170         space = 23;
    171         morespace = 27;
    172     }
    173     else {
    174         space = 10;
    175         morespace = 12;
    176     }
    177     while (1) {
    178         // char ch;
    179         linenr = 0;
    180         fp = fopen("/dev/shm/interface","r");
    181         if (fp != NULL){
    182             fread(newread, sizeof(char), 500, fp);
    183             fclose(fp);
    184         }
    185         fp3 = fopen("/dev/shm/vumeter","r");
    186         while(fgets(vumeter, 130, fp3) != NULL);
    187         fclose(fp3);
    188         // check if something has changed
    189         if (strcmp(newread, oldread) != 0) {
    190             strcpy(oldread, newread);
    191             //const char *text = "Never give up on your dreams";
    192             color = 3;
    193             row1 = 0;
    194             row2 = 0;
    195             row3 = 0;
    196             row4 = 0;
    197             row5 = 0;
    198             graphics_resource_fill(img, 0, 0, width, height, GRAPHICS_RGBA32(0,0,0,0x00));
    199             // blue, at the top (y=40)
    200             // selected 0 1 2 3 4 5 6 7 8
    201 
    202             // draw the text if updated
    203             fp2 = fopen("/dev/shm/interface", "r");
    204             if (fp2 != NULL){
    205                 while ((read = getline(&line, &len, fp2)) != -1) {
    206                     //line = b;
    207                     read = read - 1; //don't count the selected line
    208                     line[read] = '\0'; //remove return char
    209                     //strcat(line, " ");
    210                     //printf("%s",line);
    211                     if (linenr == 0)
    212                         selected = atoi(line);
    213                     if (linenr == 1)
    214                         showmenu = atoi(line);
    215                     if (linenr == selected + 2 + menuadd)
    216                         color = 1; //selected color
    217                     else {
    218                         if (showmenu == 1)
    219                             color = 5; //unselected;
    220                         if (showmenu == 2)
    221                             color = 7;
    222                         if (showmenu == 0)
    223                             color = 6;
    224                         };
    225                     if ((linenr == 2) && (read == 0))
    226                         header = 0;
    227                     if ((linenr == 2) && (read > 0))
    228                         header = 1;
    229                     if (selected == 420){
    230                         if (linenr == 1)
    231                             render_subtitle(img, line, text_size, text_size_selected, 0, y_offset2, 5);
    232                         if (linenr > 1) {
    233                             render_subtitle(img, line, text_size, text_size_selected, row1, y_offset3, 5);
    234                             row1 += read * space + morespace;
    235                         }
    236                     }
    237                     if (header == 0){ //check if normal menu or header menu
    238                         if (selected < 420){
    239                             if ((linenr == 6+menuadd) && (read > 0)){ //show recording time if there is any
    240                                 render_subtitle(img, line, text_size, text_size_selected, rectime, y_offset2, 3);
    241                             }
    242                             if (linenr >= 2+menuadd && linenr <= 5+menuadd){
    243                                 if (color == 6)
    244                                     color = 5;
    245                                 render_subtitle(img, line, text_size, text_size_selected, row1, y_offset2, color);
    246                                 row1 += read * space + morespace;
    247                             }
    248                             if (linenr >= 7+menuadd && linenr <= 12+menuadd){
    249                                 render_subtitle(img, line, text_size, text_size_selected, row2, y_offset3, color);
    250                                 row2 += read * space + morespace;
    251                             }
    252                             if (linenr >= 13+menuadd && linenr <= 20+menuadd){
    253                                 render_subtitle(img, line, text_size, text_size_selected, row3, y_offset4, color);
    254                                 row3 += read * space + morespace;
    255                             }
    256                             if (linenr >= 21+menuadd && linenr <= 27+menuadd){
    257                                 render_subtitle(img, line, text_size, text_size_selected, row4, y_offset5, color);
    258                                 row4 += read * space + morespace;
    259                             }
    260                             if (linenr >= 28+menuadd && linenr <= 40+menuadd){
    261                                 render_subtitle(img, line, text_size, text_size_selected, row5, y_offset6, color);
    262                                 row5 += read * space + morespace;
    263                             }
    264                         }
    265                     }
    266                     else { // header menu
    267                         if (linenr == 1+menuadd)
    268                             render_subtitle(img, line, text_size, text_size_selected, 0, y_offset2, 5);
    269                         if (linenr > 1+menuadd){
    270                             render_subtitle(img, line, text_size, text_size_selected, row1, y_offset3, color);
    271                             row1 += read * space + morespace;
    272                         }
    273                     }
    274                     linenr += 1;
    275                     free(line);
    276                     line = NULL;
    277                 }
    278                 free(line);
    279                 line = NULL;
    280                 fclose(fp2);
    281             }
    282         //graphics_update_displayed_resource(img, 0, 0, 0, 0);
    283         }
    284         char s_vol1 = vumeter[85];
    285         char s_vol2 = vumeter[86];
    286         char s_vol[1];
    287         s_vol[0] = s_vol1;
    288         s_vol[1] = s_vol2;
    289         int vol = atoi(s_vol);
    290         int vucolor = 6;
    291         //printf("%s", s_vol);
    292         if (vol >= 0 && vol < 35)
    293             vucolor = 4;
    294         if (vol >= 35 && vol < 99)
    295             vucolor = 2;
    296         if (vol >= 99)
    297             vucolor = 3;
    298         render_subtitle(img, vumeter, text_size, text_size_selected , 0, y_offset, vucolor);
    299         graphics_update_displayed_resource(img, 0, 0, 0, 0);
    300         usleep(20000);
    301     }
    302     graphics_display_resource(img, 0, LAYER, 0, 0, GRAPHICS_RESOURCE_WIDTH, GRAPHICS_RESOURCE_HEIGHT,  VC_DISPMAN_ROT0, 0);
    303     graphics_delete_resource(img);
    304     return 0;
    305 }
    306