Statistics
| Branch: | Revision:

chunker-player / chunker_player / player_gui.c @ 6f1fe643

History | View | Annotate | Download (27.4 KB)

1
/*
2
 *  Copyright (c) 2009-2011 Carmelo Daniele, Dario Marchese, Diego Reforgiato, Giuseppe Tropea
3
 *  developed for the Napa-Wine EU project. See www.napa-wine.eu
4
 *
5
 *  This is free software; see lgpl-2.1.txt
6
 */
7

    
8
#include "player_gui.h"
9
// #include "player_commons.h"
10

    
11
#define SCREEN_BOTTOM_PADDING (BUTTONS_LAYER_OFFSET + BUTTONS_CONTAINER_HEIGHT + STATS_BOX_HEIGHT)
12

    
13
SDL_Cursor *InitSystemCursor(const char *image[]);
14
void AspectRatioResize(float aspect_ratio, int width, int height, int* out_width, int* out_height);
15
void UpdateOverlaySize(float aspect_ratio, int width, int height);
16
void RedrawButtons();
17
void RedrawChannelName();
18
void RedrawStats();
19
void SetupGUI();
20
void ToggleAudio();
21
void PSNRLedCallback();
22

    
23
static char AudioStatsText[255];
24
static char VideoStatsText[255];
25

    
26
SDL_Surface *ChannelTitleSurface = NULL;
27
//SDL_Surface *AudioStatisticsSurface = NULL, *VideoStatisticsSurface = NULL;
28
SDL_Rect ChannelTitleRect, AudioStatisticsRect, VideoStatisticsRect, tmpRect;
29
SDL_Color ChannelTitleColor = { 255, 0, 0 }, StatisticsColor = { 255, 255, 255 };
30
SDL_Color ChannelTitleBgColor = { 0, 0, 0 }, StatisticsBgColor = { 0, 0, 0 };
31
TTF_Font *MainFont = NULL;
32
TTF_Font *StatisticsFont = NULL;
33

    
34
/* XPM */
35
static const char *handXPM[] = {
36
/* columns rows colors chars-per-pixel */
37
"32 32 3 1",
38
"  c black",
39
". c gray100",
40
"X c None",
41
/* pixels */
42
"XXXXX  XXXXXXXXXXXXXXXXXXXXXXXXX",
43
"XXXX .. XXXXXXXXXXXXXXXXXXXXXXXX",
44
"XXXX .. XXXXXXXXXXXXXXXXXXXXXXXX",
45
"XXXX .. XXXXXXXXXXXXXXXXXXXXXXXX",
46
"XXXX .. XXXXXXXXXXXXXXXXXXXXXXXX",
47
"XXXX ..   XXXXXXXXXXXXXXXXXXXXXX",
48
"XXXX .. ..   XXXXXXXXXXXXXXXXXXX",
49
"XXXX .. .. ..  XXXXXXXXXXXXXXXXX",
50
"XXXX .. .. .. . XXXXXXXXXXXXXXXX",
51
"   X .. .. .. .. XXXXXXXXXXXXXXX",
52
" ..  ........ .. XXXXXXXXXXXXXXX",
53
" ... ........... XXXXXXXXXXXXXXX",
54
"X .. ........... XXXXXXXXXXXXXXX",
55
"XX . ........... XXXXXXXXXXXXXXX",
56
"XX ............. XXXXXXXXXXXXXXX",
57
"XXX ............ XXXXXXXXXXXXXXX",
58
"XXX ........... XXXXXXXXXXXXXXXX",
59
"XXXX .......... XXXXXXXXXXXXXXXX",
60
"XXXX .......... XXXXXXXXXXXXXXXX",
61
"XXXXX ........ XXXXXXXXXXXXXXXXX",
62
"XXXXX ........ XXXXXXXXXXXXXXXXX",
63
"XXXXX          XXXXXXXXXXXXXXXXX",
64
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
65
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
66
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
67
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
68
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
69
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
70
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
71
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
72
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
73
"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
74
"0,0"
75
};
76

    
77
int ChunkerPlayerGUI_Init()
78
{
79
        // some initializations
80
        ratio = DEFAULT_RATIO;
81
        FullscreenWidth = 0;
82
        FullscreenHeight = 0;
83
        Audio_ON = 1;
84
        
85
        UpdateOverlaySize(ratio, DEFAULT_WIDTH, DEFAULT_HEIGHT);
86
        
87
        if(!SilentMode)
88
                SetupGUI();
89
        
90
        return 0;
91
}
92

    
93
int SetVideoMode(int width, int height, int fullscreen)
94
{
95
        if(SilentMode)
96
                return 1;
97
                
98
        // printf("SetVideoMode(%d, %d, %d)\n", width, height, fullscreen);
99
        SDL_LockMutex(OverlayMutex);
100

    
101
        if(fullscreen)
102
        {
103
#ifndef __DARWIN__
104
                MainScreen = SDL_SetVideoMode(width, height, 0, SDL_SWSURFACE | SDL_NOFRAME | SDL_FULLSCREEN);
105
#else
106
                MainScreen = SDL_SetVideoMode(width, height, 24, SDL_SWSURFACE | SDL_NOFRAME | SDL_FULLSCREEN);
107
#endif
108
        }
109
        else
110
        {
111
#ifndef __DARWIN__
112
                MainScreen = SDL_SetVideoMode(width, height, 0, SDL_SWSURFACE | SDL_RESIZABLE);
113
#else
114
                MainScreen = SDL_SetVideoMode(width, height, 24, SDL_SWSURFACE | SDL_RESIZABLE);
115
#endif
116
        }
117

    
118
        if(!MainScreen) {
119
                fprintf(stderr, "ERROR: could not change video mode\n");
120
                return 1;
121
        }
122
        SDL_UnlockMutex(OverlayMutex);
123
        
124
        return 0;
125
}
126

    
127
void ChunkerPlayerGUI_HandleResize(int resize_w, int resize_h)
128
{
129
        if(SilentMode)
130
                return;
131
                
132
        SDL_LockMutex(OverlayMutex);
133
        
134
        int res = SetVideoMode(resize_w, resize_h, FullscreenMode?1:0);
135

    
136
        if(res && FullscreenMode)
137
        {
138
                // an error has occurred while switching to fullscreen mode
139
                
140
                // trying resize without fullscreen mode
141
                FullscreenMode = 0;
142
                res = SetVideoMode(resize_w, resize_h, 0);
143
        }
144
        if(res)
145
        {
146
                // nothing to do
147
                fprintf(stderr, "CRITICAL ERROR: could not change video mode\n");
148
                exit(1);
149
        }
150
        
151
        window_width = resize_w;
152
        window_height = resize_h;
153
        
154
        // update the overlay surface size, mantaining the aspect ratio
155
        UpdateOverlaySize(ratio, resize_w, resize_h);
156
        
157
        // update each button coordinates
158
        int i;
159
        for(i=0; i<NBUTTONS; i++)
160
        {
161
                if(Buttons[i].XOffset > 0)
162
                        Buttons[i].ButtonIconBox.x = Buttons[i].XOffset;
163
                else
164
                        Buttons[i].ButtonIconBox.x = (resize_w + Buttons[i].XOffset);
165
                        
166
                Buttons[i].ButtonIconBox.y = resize_h - Buttons[i].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
167
        }
168
        
169
        RedrawButtons();
170
        RedrawChannelName();
171
        RedrawStats();
172
        
173
        SDL_UnlockMutex(OverlayMutex);
174
}
175

    
176
void ChunkerPlayerGUI_HandleGetFocus()
177
{
178
        if(SilentMode)
179
                return;
180

    
181
        RedrawButtons();
182
        RedrawChannelName();
183
        RedrawStats();
184
}
185

    
186
void ChunkerPlayerGUI_HandleMouseMotion(int x, int y)
187
{
188
        if(SilentMode)
189
                return;
190
                
191
        int i;
192
        for(i=0; i<NBUTTONS; i++)
193
        {
194
                //If the mouse is over the button
195
                if(
196
                        ( x > Buttons[i].ButtonIconBox.x ) && ( x < Buttons[i].ButtonIconBox.x + Buttons[i].ButtonIcon->w )
197
                        && ( y > Buttons[i].ButtonIconBox.y ) && ( y < Buttons[i].ButtonIconBox.y + Buttons[i].ButtonIcon->h )
198
                )
199
                {
200
                    // LED Button 
201
                    if(i>=PSNR_LED_RED_BUTTON_INDEX && i<=PSNR_LED_GREEN_BUTTON_INDEX)
202
                    {
203
                        SDL_SetCursor(defaultCursor);
204
                        break;
205
                    }
206
                        
207
                        Buttons[i].Hover = 1;
208
                        SDL_SetCursor(handCursor);
209
                        break;
210
                }
211
                
212
                else
213
                {
214
                        Buttons[i].Hover = 0;
215
                        SDL_SetCursor(defaultCursor);
216
                }
217
        }
218
}
219

    
220
void ChunkerPlayerGUI_HandleLButton(int x, int y)
221
{
222
        if(SilentMode)
223
                return;
224
                
225
        int i;
226
        for(i=0; i<NBUTTONS; i++)
227
        {
228
                //If the mouse is over the button
229
                if(
230
                        ( x > Buttons[i].ButtonIconBox.x ) && ( x < Buttons[i].ButtonIconBox.x + Buttons[i].ButtonIcon->w )
231
                        && ( y > Buttons[i].ButtonIconBox.y ) && ( y < Buttons[i].ButtonIconBox.y + Buttons[i].ButtonIcon->h )
232
                )
233
                {
234
                        Buttons[i].LButtonUpCallback();
235
                        break;
236
                }
237
        }
238
}
239

    
240
void ChunkerPlayerGUI_HandleKey()
241
{
242
        /*static Uint32 LastTime=0;
243
        static int LastKey=-1;
244

245
        Uint32 Now=SDL_GetTicks();
246
        Uint8* keystate=SDL_GetKeyState(NULL);
247
        if(keystate[SDLK_ESCAPE] &&
248
          (LastKey!=SDLK_ESCAPE || (LastKey==SDLK_ESCAPE && (Now-LastTime>1000))))
249
        {
250
                LastKey=SDLK_ESCAPE;
251
                LastTime=Now;
252
                quit=1;
253
        }*/
254
}
255

    
256
void ChunkerPlayerGUI_Close()
257
{
258
        if(ChannelTitleSurface != NULL)
259
                SDL_FreeSurface( ChannelTitleSurface );
260
        
261
        TTF_CloseFont( MainFont );
262
        TTF_CloseFont( StatisticsFont );
263
        TTF_Quit();
264
        IMG_Quit();
265
}
266

    
267
void RedrawButtons()
268
{
269
        if(SilentMode)
270
                return;
271
                
272
        int i;
273
        for(i=0; i<NBUTTONS; i++)
274
        {
275
                if(!Buttons[i].Visible)
276
                {
277
                        SDL_LockMutex(OverlayMutex);
278
                        SDL_FillRect( MainScreen, &Buttons[i].ButtonIconBox, SDL_MapRGB(MainScreen->format, 0, 0, 0) );
279
                        SDL_UpdateRects(MainScreen, 1, &Buttons[i].ButtonIconBox);
280
                        SDL_UnlockMutex(OverlayMutex);
281
                }
282
        }
283
        for(i=0; i<NBUTTONS; i++)
284
        {
285
                if(Buttons[i].Visible)
286
                {
287
                        if(!Buttons[i].Hover)
288
                        {
289
                                SDL_LockMutex(OverlayMutex);
290
                                SDL_BlitSurface(Buttons[i].ButtonIcon, NULL, MainScreen, &Buttons[i].ButtonIconBox);
291
                                SDL_UpdateRects(MainScreen, 1, &Buttons[i].ButtonIconBox);
292
                                SDL_UnlockMutex(OverlayMutex);
293
                        }
294
                        else
295
                        {
296
                                SDL_LockMutex(OverlayMutex);
297
                                SDL_BlitSurface(Buttons[i].ButtonHoverIcon, NULL, MainScreen, &(Buttons[i].ButtonIconBox));
298
                                SDL_UpdateRects(MainScreen, 1, &(Buttons[i].ButtonIconBox));
299
                                SDL_UnlockMutex(OverlayMutex);
300
                        }
301
                }
302
        }
303
}
304

    
305
void RedrawChannelName()
306
{
307
        if(SilentMode)
308
                return;
309
                
310
        if(ChannelTitleSurface != NULL)
311
        {
312
                SDL_LockMutex(OverlayMutex);
313
                SDL_FillRect( MainScreen, &ChannelTitleRect, SDL_MapRGB(MainScreen->format, 0, 0, 0) );
314
                SDL_UpdateRect(MainScreen, ChannelTitleRect.x, ChannelTitleRect.y, ChannelTitleRect.w, ChannelTitleRect.h);
315
                
316
                ChannelTitleRect.w = ChannelTitleSurface->w;
317
                ChannelTitleRect.h = ChannelTitleSurface->h;
318
                ChannelTitleRect.x = ((FullscreenMode?FullscreenWidth:window_width) - ChannelTitleRect.w)/2;
319
                ChannelTitleRect.y = Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.y+5;
320
                SDL_BlitSurface(ChannelTitleSurface, NULL, MainScreen, &ChannelTitleRect);
321
                SDL_UpdateRects(MainScreen, 1, &ChannelTitleRect);
322
                SDL_UnlockMutex(OverlayMutex);
323
        }
324
}
325

    
326
void ChunkerPlayerGUI_ToggleFullscreen()
327
{
328
        if(SilentMode)
329
                return;
330

    
331
        SDL_LockMutex(OverlayMutex);
332
                
333
        int i, done = 0;
334
        //If the screen is windowed
335
        if( !FullscreenMode )
336
        {
337
                int res = SetVideoMode(FullscreenWidth, FullscreenHeight, 1);
338
                //Set the window state flag
339
                FullscreenMode = 1;
340

    
341
                if(res)
342
                {
343
                        fprintf(stderr, "ERROR: an error has occurred while switching to fullscreen mode\n");
344
                }
345
                else
346
                {
347
                        // update the overlay surface size, mantaining the aspect ratio
348
                        UpdateOverlaySize(ratio, FullscreenWidth, FullscreenHeight);
349
                        
350
                        // update each button coordinates
351
                        for(i=0; i<NBUTTONS; i++)
352
                        {
353
                                if(Buttons[i].XOffset > 0)
354
                                        Buttons[i].ButtonIconBox.x = Buttons[i].XOffset;
355
                                else
356
                                        Buttons[i].ButtonIconBox.x = (FullscreenWidth + Buttons[i].XOffset);
357
                                        
358
                                Buttons[i].ButtonIconBox.y = FullscreenHeight - Buttons[i].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
359
                        }
360
                        
361
                        Buttons[FULLSCREEN_BUTTON_INDEX].Visible = 0;
362
                        Buttons[NO_FULLSCREEN_BUTTON_INDEX].Visible = 1;
363
                        
364
                        done = 1;
365
                }
366
        }
367
        
368
        //If the screen is fullscreen
369
        if(FullscreenMode && !done)
370
        {
371
                int res = SetVideoMode(window_width, window_height, 0);
372
                if(res)
373
                {
374
                        // nothing to do
375
                        fprintf(stderr, "CRITICAL ERROR: could not change video mode\n");
376
                        exit(1);
377
                }
378
                
379
                // update the overlay surface size, mantaining the aspect ratio
380
                UpdateOverlaySize(ratio, window_width, window_height);
381
                
382
                // update each button coordinates
383
                for(i=0; i<NBUTTONS; i++)
384
                {
385
                        if(Buttons[i].XOffset > 0)
386
                                Buttons[i].ButtonIconBox.x = Buttons[i].XOffset;
387
                        else
388
                                Buttons[i].ButtonIconBox.x = (window_width + Buttons[i].XOffset);
389
                                
390
                        Buttons[i].ButtonIconBox.y = window_height - Buttons[i].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
391
                }
392
                
393
                //Set the window state flag
394
                FullscreenMode = 0;
395
                
396
                Buttons[FULLSCREEN_BUTTON_INDEX].Visible = 1;
397
                Buttons[NO_FULLSCREEN_BUTTON_INDEX].Visible = 0;
398
        }
399

    
400
        RedrawButtons();
401
        RedrawChannelName();
402
        RedrawStats();
403
        
404
        SDL_UnlockMutex(OverlayMutex);
405
}
406

    
407
void ChunkerPlayerGUI_AspectRatioResize(float aspect_ratio, int width, int height, int* out_width, int* out_height)
408
{
409
        AspectRatioResize(aspect_ratio, width, height, out_width, out_height);
410
}
411

    
412
void AspectRatioResize(float aspect_ratio, int width, int height, int* out_width, int* out_height)
413
{
414
        int h,w;
415
        h = (int)((float)width/aspect_ratio);
416
        if(h<=height)
417
        {
418
                w = width;
419
        }
420
        else
421
        {
422
                w = (int)((float)height*aspect_ratio);
423
                h = height;
424
        }
425
        *out_width = w;
426
        *out_height = h;
427
}
428

    
429
/**
430
 * Updates the overlay surface size, mantaining the aspect ratio
431
 */
432
void UpdateOverlaySize(float aspect_ratio, int width, int height)
433
{
434
        // height -= (BUTTONS_LAYER_OFFSET + BUTTONS_CONTAINER_HEIGHT);
435
        height -= SCREEN_BOTTOM_PADDING;
436
        int h = 0, w = 0, x, y;
437
        AspectRatioResize(aspect_ratio, width, height, &w, &h);
438
        x = (width - w) / 2;
439
        y = (height - h) / 2;
440
        SDL_LockMutex(OverlayMutex);
441
        OverlayRect.x = x;
442
        OverlayRect.y = y;
443
        OverlayRect.w = w;
444
        OverlayRect.h = h;
445
        // SDL_FillRect( SDL_GetVideoSurface(), NULL, SDL_MapRGB(SDL_GetVideoSurface()->format, 0,0,0) );
446
        SDL_UpdateRect(MainScreen, 0, 0, 0, 0);
447
        SDL_UnlockMutex(OverlayMutex);
448
}
449

    
450
void GetScreenSizeFromOverlay(int overlayWidth, int overlayHeight, int* screenWidth, int* screenHeight)
451
{
452
        *screenHeight = overlayHeight + SCREEN_BOTTOM_PADDING;
453
        *screenWidth = overlayWidth;
454
}
455

    
456
/* From the SDL documentation. */
457
SDL_Cursor *InitSystemCursor(const char *image[])
458
{
459
        int i, row, col;
460
        Uint8 data[4*32];
461
        Uint8 mask[4*32];
462
        int hot_x, hot_y;
463

    
464
        i = -1;
465
        for ( row=0; row<32; ++row ) {
466
                for ( col=0; col<32; ++col ) {
467
                        if ( col % 8 ) {
468
                                data[i] <<= 1;
469
                                mask[i] <<= 1;
470
                        } else {
471
                                ++i;
472
                                data[i] = mask[i] = 0;
473
                        }
474
                        
475
                        switch (image[4+row][col]) {
476
                                case ' ':
477
                                        data[i] |= 0x01;
478
                                        mask[i] |= 0x01;
479
                                        break;
480
                                case '.':
481
                                        mask[i] |= 0x01;
482
                                        break;
483
                                case 'X':
484
                                        break;
485
                        }
486
                }
487
        }
488
        
489
        sscanf(image[4+row], "%d,%d", &hot_x, &hot_y);
490
        return SDL_CreateCursor(data, mask, 32, 32, hot_x, hot_y);
491
}
492

    
493
void SetupGUI()
494
{
495
        //Initialize SDL_ttf 
496
        if( TTF_Init() == -1 )
497
        {
498
                printf("TTF_Init: Failed to init SDL_ttf library!\n");
499
                printf("TTF_Init: %s\n", TTF_GetError());
500
                exit(1);
501
        }
502
        
503
        //Open the font
504
        MainFont = TTF_OpenFont(MAIN_FONT_FILE , MAIN_FONT_SIZE );
505
        StatisticsFont = TTF_OpenFont(STATS_FONT_FILE, STATS_FONT_SIZE );
506
        
507
        //If there was an error in loading the font
508
        if( MainFont == NULL)
509
        {
510
                printf("Cannot initialize GUI, %s file not found\n", MAIN_FONT_FILE);
511
                exit(1);
512
        }
513
        if( StatisticsFont == NULL )
514
        {
515
                printf("Cannot initialize GUI, %s file not found\n", STATS_FONT_FILE);
516
                exit(1);
517
        }
518
        
519
        // init SDL_image
520
        int flags=IMG_INIT_JPG|IMG_INIT_PNG;
521
        int initted=IMG_Init(flags);
522
        if(initted&flags != flags)
523
        {
524
                printf("IMG_Init: Failed to init required jpg and png support!\n");
525
                printf("IMG_Init: %s\n", IMG_GetError());
526
                exit(1);
527
        }
528
        
529
        SDL_VideoInfo* InitialVideoInfo = SDL_GetVideoInfo();
530
        FullscreenWidth = InitialVideoInfo->current_w;
531
        FullscreenHeight = InitialVideoInfo->current_h;
532

    
533
        SDL_Surface *temp;
534
        int screen_w = 0, screen_h = 0;
535

    
536
        if(OverlayRect.w > BUTTONS_CONTAINER_WIDTH)
537
                screen_w = OverlayRect.w;
538
        else
539
                screen_w = BUTTONS_CONTAINER_WIDTH;
540
        screen_h = OverlayRect.h + SCREEN_BOTTOM_PADDING;
541

    
542
        SDL_WM_SetCaption("Filling buffer...", NULL);
543
        
544
        // Make a screen to put our video
545
        SetVideoMode(screen_w, screen_h, 0);
546
        
547
        window_width = screen_w;
548
        window_height = screen_h;
549
        
550
        /** Setting up cursors */
551
        defaultCursor = SDL_GetCursor();
552
        handCursor = InitSystemCursor(handXPM);
553
        
554
        /** Init Buttons */
555
        int i;
556
        for(i=0; i<NBUTTONS; i++)
557
        {
558
                SButton* tmp = &(Buttons[i]);
559
                tmp->Hover = 0;
560
                tmp->ToggledButton = NULL;
561
                tmp->Visible = 1;
562
                tmp->HoverCallback = NULL;
563
                tmp->LButtonUpCallback = NULL;
564
        }
565
        
566
        /** Loading icons */
567
        
568
        // fullscreen
569
        temp = IMG_Load(FULLSCREEN_ICON_FILE);
570
        if (temp == NULL) {
571
                fprintf(stderr, "Error loading %s: %s\n", FULLSCREEN_ICON_FILE, SDL_GetError());
572
                exit(1);
573
        }
574
        Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
575
        if(Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIcon == NULL)
576
        {
577
                printf("ERROR in SDL_DisplayFormatAlpha, cannot load fullscreen button, error message: '%s'\n", SDL_GetError());
578
                exit(1);
579
        }
580
        SDL_FreeSurface(temp);
581
        
582
        // fullscreen hover
583
        temp = IMG_Load(FULLSCREEN_HOVER_ICON_FILE);
584
        if (temp == NULL) {
585
                fprintf(stderr, "Error loading %s: %s\n", FULLSCREEN_HOVER_ICON_FILE, SDL_GetError());
586
                exit(1);
587
        }
588
        Buttons[FULLSCREEN_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
589
        SDL_FreeSurface(temp);
590

    
591
        // no fullscreen
592
        temp = IMG_Load(NOFULLSCREEN_ICON_FILE);
593
        if (temp == NULL) {
594
                fprintf(stderr, "Error loading %s: %s\n", NOFULLSCREEN_ICON_FILE, SDL_GetError());
595
                exit(1);
596
        }
597
        Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
598
        SDL_FreeSurface(temp);
599

    
600
        // no fullscreen hover
601
        temp = IMG_Load(NOFULLSCREEN_HOVER_ICON_FILE);
602
        if (temp == NULL) {
603
                fprintf(stderr, "Error loading %s: %s\n", NOFULLSCREEN_HOVER_ICON_FILE, SDL_GetError());
604
                exit(1);
605
        }
606
        Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
607
        SDL_FreeSurface(temp);
608
        
609
        // channel up
610
        temp = IMG_Load(CHANNEL_UP_ICON_FILE);
611
        if (temp == NULL) {
612
                fprintf(stderr, "Error loading %s: %s\n", CHANNEL_UP_ICON_FILE, SDL_GetError());
613
                exit(1);
614
        }
615
        Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
616
        Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
617
        SDL_FreeSurface(temp);
618
        
619
        // channel down
620
        temp = IMG_Load(CHANNEL_DOWN_ICON_FILE);
621
        if (temp == NULL) {
622
                fprintf(stderr, "Error loading %s: %s\n", CHANNEL_DOWN_ICON_FILE, SDL_GetError());
623
                exit(1);
624
        }
625
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
626
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
627
        SDL_FreeSurface(temp);
628
        
629
        // audio OFF
630
        temp = IMG_Load(AUDIO_OFF_ICON_FILE);
631
        if (temp == NULL) {
632
                fprintf(stderr, "Error loading %s: %s\n", AUDIO_OFF_ICON_FILE, SDL_GetError());
633
                exit(1);
634
        }
635
        Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
636
        Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
637
        SDL_FreeSurface(temp);
638
        
639
        // audio ON
640
        temp = IMG_Load(AUDIO_ON_ICON_FILE);
641
        if (temp == NULL) {
642
                fprintf(stderr, "Error loading %s: %s\n", AUDIO_ON_ICON_FILE, SDL_GetError());
643
                exit(1);
644
        }
645
        Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
646
        Buttons[AUDIO_ON_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
647
        SDL_FreeSurface(temp);
648
        
649
        // PSNR RED LED
650
        temp = IMG_Load(PSNR_LED_RED_ICON_FILE);
651
        if (temp == NULL) {
652
                fprintf(stderr, "Error loading %s: %s\n", PSNR_LED_RED_ICON_FILE, SDL_GetError());
653
                exit(1);
654
        }
655
        Buttons[PSNR_LED_RED_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
656
        Buttons[PSNR_LED_RED_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
657
        SDL_FreeSurface(temp);
658
        
659
        // PSNR YELLOW LED
660
        temp = IMG_Load(PSNR_LED_YELLOW_ICON_FILE);
661
        if (temp == NULL) {
662
                fprintf(stderr, "Error loading %s: %s\n", PSNR_LED_YELLOW_ICON_FILE, SDL_GetError());
663
                exit(1);
664
        }
665
        Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
666
        Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
667
        SDL_FreeSurface(temp);
668
        
669
        // PSNR GREEN LED
670
        temp = IMG_Load(PSNR_LED_GREEN_ICON_FILE);
671
        if (temp == NULL) {
672
                fprintf(stderr, "Error loading %s: %s\n", PSNR_LED_GREEN_ICON_FILE, SDL_GetError());
673
                exit(1);
674
        }
675
        Buttons[PSNR_LED_GREEN_BUTTON_INDEX].ButtonIcon = SDL_DisplayFormatAlpha(temp);
676
        Buttons[PSNR_LED_GREEN_BUTTON_INDEX].ButtonHoverIcon = SDL_DisplayFormatAlpha(temp);
677
        SDL_FreeSurface(temp);
678

    
679
        /** Setting up icon boxes */
680
        Buttons[FULLSCREEN_BUTTON_INDEX].XOffset = Buttons[NO_FULLSCREEN_BUTTON_INDEX].XOffset = 20;
681
        Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.x = 20;
682
        Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.w = Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIcon->w;
683
        Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.h = Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIcon->h;
684
        Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
685
        
686
        Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIconBox.x = 20;
687
        Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIconBox.w = Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIcon->w;
688
        Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIconBox.h = Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIcon->h;
689
        Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[NO_FULLSCREEN_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
690
        
691
        Buttons[CHANNEL_UP_BUTTON_INDEX].XOffset = -61;
692
        Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIconBox.w = Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIcon->w;
693
        Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIconBox.h = Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIcon->h;
694
        Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIconBox.x = (screen_w + Buttons[CHANNEL_UP_BUTTON_INDEX].XOffset);
695
        Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[CHANNEL_UP_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
696
        
697
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].XOffset = -36;
698
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIconBox.w = Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIcon->w;
699
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIconBox.h = Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIcon->h;
700
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIconBox.x = (screen_w + Buttons[CHANNEL_DOWN_BUTTON_INDEX].XOffset);
701
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[CHANNEL_DOWN_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
702
        
703
        Buttons[AUDIO_OFF_BUTTON_INDEX].XOffset = Buttons[AUDIO_ON_BUTTON_INDEX].XOffset = 70;
704
        Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIconBox.x = 20;
705
        Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIconBox.w = Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIcon->w;
706
        Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIconBox.h = Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIcon->h;
707
        Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[AUDIO_OFF_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
708
        Buttons[AUDIO_OFF_BUTTON_INDEX].Visible = 1;
709

    
710
        Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIconBox.x = 20;
711
        Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIconBox.w = Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIcon->w;
712
        Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIconBox.h = Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIcon->h;
713
        Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[AUDIO_ON_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
714
        Buttons[AUDIO_ON_BUTTON_INDEX].Visible = 0;
715
        
716
        Buttons[PSNR_LED_RED_BUTTON_INDEX].XOffset = -106;
717
        Buttons[PSNR_LED_RED_BUTTON_INDEX].ButtonIconBox.w = Buttons[PSNR_LED_RED_BUTTON_INDEX].ButtonIcon->w;
718
        Buttons[PSNR_LED_RED_BUTTON_INDEX].ButtonIconBox.h = Buttons[PSNR_LED_RED_BUTTON_INDEX].ButtonIcon->h;
719
        Buttons[PSNR_LED_RED_BUTTON_INDEX].ButtonIconBox.x = (screen_w + Buttons[PSNR_LED_RED_BUTTON_INDEX].XOffset);
720
        Buttons[PSNR_LED_RED_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[PSNR_LED_RED_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
721
        Buttons[PSNR_LED_RED_BUTTON_INDEX].Visible = 0;
722
        
723
        Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].XOffset = -106;
724
        Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].ButtonIconBox.w = Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].ButtonIcon->w;
725
        Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].ButtonIconBox.h = Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].ButtonIcon->h;
726
        Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].ButtonIconBox.x = (screen_w + Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].XOffset);
727
        Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
728
        Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].Visible = 0;
729
        
730
        Buttons[PSNR_LED_GREEN_BUTTON_INDEX].XOffset = -106;
731
        Buttons[PSNR_LED_GREEN_BUTTON_INDEX].ButtonIconBox.w = Buttons[PSNR_LED_GREEN_BUTTON_INDEX].ButtonIcon->w;
732
        Buttons[PSNR_LED_GREEN_BUTTON_INDEX].ButtonIconBox.h = Buttons[PSNR_LED_GREEN_BUTTON_INDEX].ButtonIcon->h;
733
        Buttons[PSNR_LED_GREEN_BUTTON_INDEX].ButtonIconBox.x = (screen_w + Buttons[PSNR_LED_GREEN_BUTTON_INDEX].XOffset);
734
        Buttons[PSNR_LED_GREEN_BUTTON_INDEX].ButtonIconBox.y = screen_h - Buttons[PSNR_LED_GREEN_BUTTON_INDEX].ButtonIconBox.h - (SCREEN_BOTTOM_PADDING/2);
735
        Buttons[PSNR_LED_GREEN_BUTTON_INDEX].Visible = 1;
736
        
737
        // Setting up buttons events
738
        Buttons[FULLSCREEN_BUTTON_INDEX].ToggledButton = &(Buttons[NO_FULLSCREEN_BUTTON_INDEX]);
739
        Buttons[FULLSCREEN_BUTTON_INDEX].LButtonUpCallback = &ChunkerPlayerGUI_ToggleFullscreen;
740
        Buttons[NO_FULLSCREEN_BUTTON_INDEX].LButtonUpCallback = &ChunkerPlayerGUI_ToggleFullscreen;
741
        Buttons[CHANNEL_UP_BUTTON_INDEX].LButtonUpCallback = &ZapUp;
742
        Buttons[CHANNEL_DOWN_BUTTON_INDEX].LButtonUpCallback = &ZapDown;
743
        Buttons[AUDIO_OFF_BUTTON_INDEX].LButtonUpCallback = &ToggleAudio;
744
        Buttons[AUDIO_ON_BUTTON_INDEX].LButtonUpCallback = &ToggleAudio;
745
        Buttons[PSNR_LED_RED_BUTTON_INDEX].LButtonUpCallback = &PSNRLedCallback;
746
        Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].LButtonUpCallback = &PSNRLedCallback;
747
        Buttons[PSNR_LED_GREEN_BUTTON_INDEX].LButtonUpCallback = &PSNRLedCallback;
748
}
749

    
750
void ChunkerPlayerGUI_SetChannelTitle(char* title)
751
{
752
        if(SilentMode)
753
                return;
754
                
755
        SDL_LockMutex(OverlayMutex);
756
        
757
        SDL_FreeSurface( ChannelTitleSurface );
758
        // ChannelTitleSurface = TTF_RenderText_Solid( MainFont, channel->Title, ChannelTitleColor );
759
        ChannelTitleSurface = TTF_RenderText_Shaded( MainFont, title, ChannelTitleColor, ChannelTitleBgColor );
760
        if(ChannelTitleSurface == NULL)
761
        {
762
                printf("WARNING: CANNOT RENDER CHANNEL TITLE\n");
763
        }
764
        
765
        SDL_UnlockMutex(OverlayMutex);
766
        
767
        RedrawChannelName();
768
}
769

    
770
void ChunkerPlayerGUI_SetStatsText(char* audio_text, char* video_text, int ledstatus)
771
{
772
        if(SilentMode)
773
                return;
774
                
775
        if(audio_text == NULL)
776
                audio_text = AudioStatsText;
777
        
778
        if(video_text == NULL)
779
                video_text = VideoStatsText;
780

    
781
        if((strlen(audio_text) > 255) || (strlen(video_text) > 255))
782
        {
783
                printf("WARNING IN player_gui.c: stats text too long, could not refresh text\n");
784
                return;
785
        }
786
        
787
        strcpy(AudioStatsText, audio_text);
788
        strcpy(VideoStatsText, video_text);
789
        
790
        switch(ledstatus)
791
    {
792
    case LED_RED:
793
        // PSNR LED RED
794
        Buttons[PSNR_LED_RED_BUTTON_INDEX].Visible = 1;
795
                Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].Visible = 0;
796
                Buttons[PSNR_LED_GREEN_BUTTON_INDEX].Visible = 0;
797
            break;
798
        case LED_YELLOW:
799
            // PSNR LED YELLOW
800
            Buttons[PSNR_LED_RED_BUTTON_INDEX].Visible = 0;
801
                Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].Visible = 1;
802
                Buttons[PSNR_LED_GREEN_BUTTON_INDEX].Visible = 0;
803
            break;
804
        case LED_GREEN:
805
            // PSNR LED GREEN
806
            Buttons[PSNR_LED_RED_BUTTON_INDEX].Visible = 0;
807
                Buttons[PSNR_LED_YELLOW_BUTTON_INDEX].Visible = 0;
808
                Buttons[PSNR_LED_GREEN_BUTTON_INDEX].Visible = 1;
809
            break;
810
        }
811
        
812
        RedrawStats();
813
        RedrawButtons();
814
}
815

    
816
void RedrawStats()
817
{
818
        if(SilentMode)
819
                return;
820
                
821
        SDL_Surface *text_surface;
822

    
823
        SDL_LockMutex(OverlayMutex);
824
        
825
        // clear stats text
826
        SDL_FillRect( MainScreen, &VideoStatisticsRect, SDL_MapRGB(MainScreen->format, 0, 0, 0) );
827
        SDL_FillRect( MainScreen, &AudioStatisticsRect, SDL_MapRGB(MainScreen->format, 0, 0, 0) );
828
        SDL_UpdateRect(MainScreen, VideoStatisticsRect.x, VideoStatisticsRect.y, VideoStatisticsRect.w, VideoStatisticsRect.h);
829
        SDL_UpdateRect(MainScreen, AudioStatisticsRect.x, AudioStatisticsRect.y, AudioStatisticsRect.w, AudioStatisticsRect.h);
830
        
831
        
832
        text_surface = TTF_RenderText_Shaded( StatisticsFont, AudioStatsText, StatisticsColor, StatisticsBgColor );
833
    if (text_surface != NULL)
834
    {
835
                AudioStatisticsRect.w = text_surface->w;
836
                AudioStatisticsRect.h = text_surface->h;
837
                AudioStatisticsRect.x = ((FullscreenMode?FullscreenWidth:window_width) - AudioStatisticsRect.w)>>1;
838
                AudioStatisticsRect.y = Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.y+(STATS_FONT_SIZE<<1)+STATS_BOX_HEIGHT;
839

    
840
        SDL_BlitSurface(text_surface, NULL, MainScreen, &AudioStatisticsRect);
841
        SDL_FreeSurface(text_surface);
842
    }
843
    else
844
    {
845
        // report error
846
    }
847
    
848
        text_surface = TTF_RenderText_Shaded( StatisticsFont, VideoStatsText, StatisticsColor, StatisticsBgColor );
849
    if (text_surface != NULL)
850
    {
851
                VideoStatisticsRect.w = text_surface->w;
852
                VideoStatisticsRect.h = text_surface->h;
853
                VideoStatisticsRect.x = ((FullscreenMode?FullscreenWidth:window_width) - VideoStatisticsRect.w)>>1;
854
                VideoStatisticsRect.y = Buttons[FULLSCREEN_BUTTON_INDEX].ButtonIconBox.y+(STATS_FONT_SIZE)+STATS_BOX_HEIGHT;
855

    
856
        SDL_BlitSurface(text_surface, NULL, MainScreen, &VideoStatisticsRect);
857
        SDL_FreeSurface(text_surface);
858
    }
859
    else
860
    {
861
        // report error
862
    }
863
    
864
    SDL_UpdateRect(MainScreen, VideoStatisticsRect.x, VideoStatisticsRect.y, VideoStatisticsRect.w, VideoStatisticsRect.h);
865
        SDL_UpdateRect(MainScreen, AudioStatisticsRect.x, AudioStatisticsRect.y, AudioStatisticsRect.w, AudioStatisticsRect.h);
866
        
867
        SDL_UnlockMutex(OverlayMutex);
868
}
869

    
870
void ChunkerPlayerGUI_SetupOverlayRect(SChannel* channel)
871
{
872
        ratio = channel->Ratio;
873
        int w, h;
874
        GetScreenSizeFromOverlay(channel->Width, channel->Height, &w, &h);
875
        UpdateOverlaySize(ratio, w, h);
876
        // UpdateOverlaySize(ratio, channel->Width, channel->Height);
877
}
878

    
879
void ChunkerPlayerGUI_ForceResize(int width, int height)
880
{
881
        FullscreenMode = 0;
882
        int w, h;
883
        GetScreenSizeFromOverlay(width, height, &w, &h);
884
        ChunkerPlayerGUI_HandleResize(w, h);
885
}
886

    
887
void ToggleAudio()
888
{
889
        if(Audio_ON)
890
        {
891
                SDL_PauseAudio(1);
892
                Buttons[AUDIO_OFF_BUTTON_INDEX].Visible = 0;
893
                Buttons[AUDIO_ON_BUTTON_INDEX].Visible = 1;
894
                Audio_ON = 0;
895
        }
896
        else
897
        {
898
                SDL_PauseAudio(0);
899
                Buttons[AUDIO_OFF_BUTTON_INDEX].Visible = 1;
900
                Buttons[AUDIO_ON_BUTTON_INDEX].Visible = 0;
901
                Audio_ON = 1;
902
        }
903
        
904
        RedrawButtons();
905
}
906

    
907
void PSNRLedCallback()
908
{
909
}
910

    
911
void ChunkerPlayerGUI_ChannelSwitched()
912
{
913
        if(!Audio_ON)
914
        {
915
                SDL_PauseAudio(1);
916
                Buttons[AUDIO_OFF_BUTTON_INDEX].Visible = 0;
917
                Buttons[AUDIO_ON_BUTTON_INDEX].Visible = 1;
918
        }
919
        
920
        RedrawButtons();
921
}