-
Notifications
You must be signed in to change notification settings - Fork 11
/
egi_page.c
1647 lines (1396 loc) · 52.3 KB
/
egi_page.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-------------------------------------------------------------------
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2 as
published by the Free Software Foundation.
Midas Zhou
--------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "sys_list.h"
#include "xpt2046.h"
#include "egi.h"
#include "egi_timer.h"
#include "egi_page.h"
#include "egi_debug.h"
#include "egi_color.h"
#include "egi_symbol.h"
#include "egi_bjp.h"
#include "egi_touch.h"
#include "egi_log.h"
/*---------------------------------------------
init a egi page
tag: a tag for the page
Return
page pointer ok
NULL fail
--------------------------------------------*/
EGI_PAGE * egi_page_new(char *tag)
{
int i;
EGI_PAGE *page;
/* 2. malloc page */
page=calloc(1, sizeof(struct egi_page));
if(page == NULL)
{
printf("%s: Fail to calloc page.\n", __func__);
return NULL;
}
/* 3. malloc page->ebox */
page->ebox=egi_ebox_new(type_page);
if( page == NULL)
{
printf("%s: egi_ebox_new() fails.\n", __func__);
free(page);
return NULL;
}
/* set page as its container */
page->ebox->container=page;
/* set type */
page->ebox->type=type_page;
/* 4. put tag here */
egi_ebox_settag(page->ebox,tag);
//strncpy(page->ebox->tag,tag,EGI_TAG_LENGTH); /* EGI_TAG_LENGTH+1 for a EGI_EBOX */
/* 5. set prmcolor<0, so it will NOT draw prmcolor in page refresh()
!!!! 0 will be deemed as pure black in refresh() */
page->ebox->prmcolor=-1;
/* 6. put default routine method here */
page->routine=egi_page_routine;
/* 7. init pthreads. ??? Not necessary. since alread memset() in above ??? */
for(i=0;i<EGI_PAGE_MAXTHREADS;i++)
{
page->thread_running[i]=false;
page->runner[i]=NULL; /* thread functions */
}
/* 8. init list */
INIT_LIST_HEAD(&page->list_head);
/* 9. init pgmutex */
if(pthread_mutex_init(&page->pgmutex,NULL) !=0 ) {
EGI_PLOG(LOGLV_ERROR, "%s: Fail to pathread_mutex_init page.pgmutex!", __func__ );
egi_ebox_free(page->ebox);
free(page);
return NULL;
}
return page;
}
/*--------------------------------------------
Free a EGI_PAGE
Return:
0 OK
<0 fails
--------------------------------------------*/
int egi_page_free(EGI_PAGE *page)
{
int ret=0;
int i;
struct list_head *tnode, *tmpnode;
EGI_EBOX *ebox;
/* check data */
if(page == NULL) {
printf("egi_page_free(): page is NULL! fail to free.\n");
return -1;
}
if( page->ebox==NULL ) {
printf("%s: WARN: input page->ebox is NULL!\n",__func__);
}
/* change status, wait to join runners ....*/
page->ebox->status=status_page_exiting;
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
/* cancel and join Runners */
printf(" %s: Cancel and join page [%s] runners...\n", __func__, page->ebox->tag);
for(i=0;i<EGI_PAGE_MAXTHREADS;i++) {
if(page->thread_running[i]) {
EGI_PDEBUG(DBG_PAGE,"page ['%s'] wait to join runner thread [%d].\n",
page->ebox->tag,i);
if( pthread_cancel(page->threadID[i]) !=0 )
EGI_PLOG(LOGLV_ERROR,"%s:Fail to call pthread_cancel() for page ['%s'] threadID[%d]",
__func__, page->ebox->tag, i);
if( pthread_join(page->threadID[i],NULL) !=0 )
EGI_PLOG(LOGLV_ERROR,"%s:Fail to call pthread_join() for page ['%s'] threadID[%d]",
__func__, page->ebox->tag, i);
else
EGI_PDEBUG(DBG_PAGE,"page ['%s'] runner thread [%d] joined!.\n",
page->ebox->tag,i);
}
}
/* Destroy mutex locks and conds for runner signals */
if(pthread_mutex_destroy(&page->runner_mutex) !=0 ) {
EGI_PLOG(LOGLV_ERROR, "%s: Fail to destroy PAGE.runner_mutex!", __func__ );
}
if(pthread_cond_destroy(&page->runner_cond) !=0 ) {
EGI_PLOG(LOGLV_ERROR, "%s: Fail to destroy PAGE.runner_cond!", __func__ );
}
/* Destroy thread mutex lock for page resource access */
if(pthread_mutex_destroy(&page->pgmutex) !=0 ) {
EGI_PLOG(LOGLV_ERROR, "%s: Fail to destroy PAGE.pgmutex!", __func__ );
}
/* free every child in list */
if(!list_empty(&page->list_head))
{
/* traverse the list, not safe */
list_for_each_safe(tnode, tmpnode, &page->list_head)
{
ebox=list_entry(tnode,EGI_EBOX,node);
EGI_PDEBUG(DBG_PAGE,"ebox '%s' is unlisted from page '%s' and freed.\n"
,ebox->tag,page->ebox->tag);
list_del(tnode);
ebox->free(ebox);
}
}
/* free self ebox */
if(page->ebox != NULL) {
//free(page->ebox);
egi_ebox_free(page->ebox);
}
/* free page */
free(page);
return ret;
}
/*----------------------------------------------------------
Suspend a runner of a PAGE, set signal variable sigSUSPEND[]
of the thread and wait until it confirms.
NOTE:
The response time depends on the runner's loop period.
return:
0 OK, thread suspended.
<0 fails
----------------------------------------------------------*/
int egi_suspend_runner(EGI_PAGE *page, int runnerID)
{
if(page==NULL)
return -1;
/* runner_ID is invalid or the thread is NOT running */
if( runnerID < 0 || runnerID > EGI_PAGE_MAXTHREADS
|| page->thread_running[runnerID]==false ) {
printf("%s: Runner ID is invalid or it's not running!\n",__func__);
return -2;
}
/* 1. ------- set signal SigSUSPEND ------- */
if( pthread_mutex_lock(&page->runner_mutex) !=0 ) {
printf("%s: Fail to lock pthread mutex!\n",__func__);
return -3;
}
/* 1.1 Start of Critical Zone >>>>>>>>> */
/* 1.2 check status */
if( page->thread_running[runnerID]==false ) {
printf("%s: The page runner thread with runnerID=%d is NOT running!\n",__func__, runnerID);
pthread_mutex_unlock(&page->runner_mutex);
return -4;
}
if( page->thread_suspending[runnerID]==true ) {
printf("%s: The page runner thread with runnerID=%d is already suspended!\n",__func__, runnerID);
pthread_mutex_unlock(&page->runner_mutex);
return -5;
}
/* 1.3 set signal sigSUSPEND */
page->thread_SigSUSPEND[runnerID]=true;
/* 1.4<<<<<<<<<< End of Critical Zone */
pthread_mutex_unlock(&page->runner_mutex);
#if 0 //// If it gets mutex_lock before the runner_sigSuspend_handler, then it ends in deadlock! ////
/* 2. ------- Wait to confirm suspending status ------- */
if( pthread_mutex_lock(&page->runner_mutex) !=0 ) {
printf("%s: Fail to lock pthread mutex!\n",__func__);
return -3;
}
#endif
/* 2.1 Start of Critical Zone >>>>>>>>> */
/* 2.2 wait to confirm suspending status */
printf("%s: waiting runner ID=%d to suspend...\n",__func__, runnerID);
while(page->thread_suspending[runnerID] != true) {
usleep(5000);
}
#if 0
/* 2.3 <<<<<<<<<< End of Critical Zone */
pthread_mutex_unlock(&page->runner_mutex);
#endif
return 0;
}
/*------------------------------------------------------
Resume a runner from suspending.
return:
0 OK.
<0 fails
-------------------------------------------------------*/
int egi_resume_runner(EGI_PAGE *page, int runnerID)
{
if(page==NULL)
return -1;
/* runner_ID is invalid or is NOT running */
if( runnerID < 0 || runnerID > EGI_PAGE_MAXTHREADS
|| page->thread_running[runnerID]==false ) {
printf("%s: Runner ID is invalid or it's not running!\n",__func__);
return -2;
}
if( pthread_mutex_lock(&page->runner_mutex) !=0 ) {
printf("%s: Fail to lock pthread mutex!\n",__func__);
return -3;
}
/* Start of Critical Zone >>>>>>>>> */
/* Reset SigSUSPEND first!!! then send pthread_cond_signal */
EGI_PDEBUG(DBG_PAGE,"Call pthread_cond_signal to resume runner ID=%d.\n",runnerID);
/* NOTE: A pthread_cond_signal() can only invoke one thread, When several runners are waiting
for the same condition variable and use differenct predicates to evaluate, the
signal may be received by a runner expecting other predicate, thus this cond_signal
will be wasted! So pthread_cond_signal() is not safe in asynchronous situation, so
we call pthread_cond_broadcast() instead.
*/
page->thread_SigSUSPEND[runnerID]=false; /* as predicate */
//pthread_cond_signal(&page->runner_cond);
pthread_cond_broadcast(&page->runner_cond);
/* <<<<<<<<<< End of Critical Zone */
pthread_mutex_unlock(&page->runner_mutex);
return 0;
}
/*------------------------------------------------------
Signal handler for page pthread runners.
If SigSUSPEND received, then this function will wait until
get mutex cond signal runner_cond.
current only for thread_SigSUSPEND[]
Add more if enum runner_signals needed.
return:
0 OK
<0 fails
------------------------------------------------------*/
int egi_runner_sigSuspend_handler(EGI_PAGE *page)
{
int i;
int runnerID=-1;
pthread_t threadID;
if(page==NULL)
return -1;
/* get thread ID */
threadID=pthread_self();
/* get runner ID */
for(i=0; i<EGI_PAGE_MAXTHREADS; i++) {
if(threadID==page->threadID[i]) {
runnerID=i;
break;
}
}
if(runnerID<0) {
printf("%s: Fail to get runner ID! Pls ensure the caller is a page runner.\n",__func__);
return -2;
}
if( pthread_mutex_lock(&page->runner_mutex) !=0 ) {
printf("%s: Fail to lock pthread mutex!\n",__func__);
return -3;
}
/* Start of Critical Zone >>>>>>>>> */
/* NOTE: "Some implementations, particularly on a multiprocessor, may sometimes cause multiple
* threads to wake up when the condition variable is signaled simultaneously on different
* processors. so whenever a condition wait returns, the thread has to re-evaluate the
* predicate associated with the condition wait to determine whether is can safely proceed,
* should wait again, or should declare a timeout. It is thus recommend that a condition
* wait be enclosed in the equivalent of a "while loop" that checks the predicate "
*/
/* wait thread_SigSUSPEND[] to be reset to FALSE by other thread, who will call
* pthread_cond_signal() after that.
*/
while ( page->thread_SigSUSPEND[runnerID] ) {
page->thread_suspending[runnerID]=true; /* reset status */
EGI_PDEBUG(DBG_PAGE,"start pthread_cond_wait() for '%s' runner_cond... \n",page->ebox->tag);
pthread_cond_wait(&page->runner_cond, &page->runner_mutex); /* put mutex first, and get mutex again when cond reaches */
}
/* reset status suspending */
page->thread_suspending[runnerID]=false;
/* <<<<<<<<<< End of Critical Zone */
pthread_mutex_unlock(&page->runner_mutex);
return 0;
}
/*--------------------------------------------------
add a ebox into a page's head list.
return:
0 OK
>0 ingored
<0 fails
---------------------------------------------------*/
int egi_page_addlist(EGI_PAGE *page, EGI_EBOX *ebox)
{
/* check data */
if( page==NULL || page->ebox==NULL )
{
printf("%s: input page or page->ebox is NULL!\n",__func__);
return -1;
}
if(ebox==NULL)
{
printf("egi_page_addlist(): input ebox is NULL!\n");
return 1;
}
/* set ebox->container */
ebox->container=page;
/* add to list tail */
list_add_tail(&ebox->node, &page->list_head);
EGI_PDEBUG(DBG_PAGE,"ebox '%s' is added to page '%s' \n",
ebox->tag, page->ebox->tag);
return 0;
}
/*--------------------------------------------------
traverse the page list and print it.
return:
0 OK
<0 fails
---------------------------------------------------*/
int egi_page_travlist(EGI_PAGE *page)
{
struct list_head *tnode;
EGI_EBOX *ebox=NULL;
/* check data */
if(page==NULL || page->ebox==NULL )
{
printf("%s: input page or page->ebox is NULL!\n",__func__);
return -1;
}
/* check list */
if(list_empty(&page->list_head))
{
printf("%s: page '%s' has an empty list_head.\n",__func__,page->ebox->tag);
return -2;
}
/* traverse the list, not safe */
list_for_each(tnode, &page->list_head)
{
ebox=list_entry(tnode, EGI_EBOX, node);
EGI_PDEBUG(DBG_PAGE,"Find child --- ebox: '%s' --- \n",ebox->tag);
}
return 0;
}
/*--------------------------------------------------
activate a page and its eboxes in its list.
return:
0 OK
<0 fails
---------------------------------------------------*/
int egi_page_activate(EGI_PAGE *page)
{
struct list_head *tnode;
EGI_EBOX *ebox;
int ret=0;
int xres __attribute__((__unused__))=gv_fb_dev.vinfo.xres;
int yres __attribute__((__unused__))=gv_fb_dev.vinfo.yres;
EGI_IMGBUF *imgbuf;
/* check data */
if(page==NULL || page->ebox==NULL)
{
printf("egi_page_activate(): input page or page->ebox is NULL!\n");
return -1;
}
/* check list */
if(list_empty(&page->list_head))
{
printf("egi_page_activate(): page '%s' has an empty list_head .\n",page->ebox->tag);
return -2;
}
/* set page status */
page->ebox->status=status_active;
/*** Display page->ebox->frame_img, or page->fpath, or use prime color as wallpaper.
* !!!NOTE: In page->ebox.refresh(), the page->fpath will NOT be seen and handled, It's a page method!
* We have to refresh page element(wallpaper) here, NOT in egi_page_refresh().
*/
if( page->ebox->frame_img !=NULL ) {
EGI_PDEBUG(DBG_PAGE,"Apply page->ebox->frame_img for '%s' wallpaper.\n", page->ebox->tag);
imgbuf=page->ebox->frame_img;
/* egi_image_setFrame() if necessary */
/* no subcolor, no FB filo */
egi_imgbuf_windisplay2(imgbuf, &gv_fb_dev, 0, 0, 0, 0, imgbuf->width, imgbuf->height);
}
else if( page->fpath != NULL) {
EGI_PDEBUG(DBG_PAGE,"Load '%s' for '%s' wallpaper.\n", page->fpath, page->ebox->tag);
/* Also see egi_page_refresh(EGI_PAGE *page) */
/* load a picture or use prime color as wallpaper */
if(page->fpath != NULL) {
//show_jpg(page->fpath, &gv_fb_dev, SHOW_BLACK_NOTRANSP, 0, 0);
imgbuf=egi_imgbuf_alloc(); //new();
/* First try to load as PNG file */
printf("%s: Try to load as PNG file ...\n",__func__);
if(egi_imgbuf_loadpng(page->fpath, imgbuf) !=0) {
// printf("%s: Load PNG imgbuf fail, try egi_imgbuf_loadjpg()...\n",__func__);
/* Then try to load as JPG file */
if(egi_imgbuf_loadjpg(page->fpath, imgbuf) !=0) {
//printf("%s: Load JPG imgbuf fail, try egi_imgbuf_free()...\n",__func__);
}
else {
printf("%s: Finish loading JPG file '%s' as page wallpaper.\n",__func__,
page->fpath);
}
}
else {
printf("%s: Finish loading PNG file '%s' as page wallpaper.\n",__func__,
page->fpath);
}
/* Display it */
if(imgbuf->imgbuf != NULL) {
/* no subcolor, no FB filo */
egi_imgbuf_windisplay2(imgbuf, &gv_fb_dev, 0, 0, 0, 0,
imgbuf->width, imgbuf->height);
}
/* free imgbuf */
EGI_PDEBUG(DBG_PAGE,"egi_imgbuf_free(imgbuf)...\n");
egi_imgbuf_free(imgbuf);
}
}
else /* use ebox prime color to clear(fill) screen */
{
if( page->ebox->prmcolor >= 0)
{
#if 0
fbset_color( page->ebox->prmcolor );
draw_filled_rect(&gv_fb_dev,0,0,xres-1,yres-1); /* full screen */
#endif
clear_screen(&gv_fb_dev, page->ebox->prmcolor);
}
}
/* decroating job if any */
egi_ebox_decorate(page->ebox);
/* traverse the list and activate list eboxes, not safe */
list_for_each(tnode, &page->list_head)
{
ebox=list_entry(tnode, EGI_EBOX, node);
ret +=ebox->activate(ebox);
EGI_PDEBUG(DBG_PAGE,"activate page list item ebox: '%s' with ret=%d \n",ebox->tag,ret);
}
/* other misc jobs after listed ebox refreshed */
if( page->page_refresh_misc != NULL )
page->page_refresh_misc(page);
return ret;
}
/*---------------------------------------------------------------------
1. Check need_refresh flag for page and refresh it if true.
2. Refresh each of page's child eboxes only if its needrefresh
flag is true.
3. Refresh sequence:
3.1 First refresh PAGE items, such as wallpaper, deco, misc..
3.2 then refresh its child eboxes.
3.3 <--- WARNINGS! --->
3.3.1 Because PAGE ebox and each of its child eboxes has its
own 'need_refresh' indicators, they will be checked
and refreshed separately/independently.
So if any child ebox has its image relied on PAGE image,
their 'need_refresh' indicators MUST be set together.
3.3.2 To call egi_page_needrefresh() will activate 'need_refresh'
indicators for PAGE and its child eboxes.
3.3.3 To call egi_ebox_forcerefresh() is seemed as dangerous,
for it refreshs itself and ingnores its PAGE image coordination.
Return:
1 need_refresh=false
0 If any ebox has been refreshed.
<0 fails
---------------------------------------------------------------------*/
int egi_page_refresh(EGI_PAGE *page)
{
struct list_head *tnode;
EGI_EBOX *ebox;
int ret=1; /* set 1 first! */
int xres __attribute__((__unused__))=gv_fb_dev.vinfo.xres;
int yres __attribute__((__unused__))=gv_fb_dev.vinfo.yres;
EGI_IMGBUF *imgbuf;
/* check data */
if( page==NULL || page->ebox==NULL )
{
printf("egi_page_refresh(): input page or page->ebox is NULL!\n");
return -1;
}
/* Get pgmutex for PAGE resource access */
if(pthread_mutex_lock(&page->pgmutex) !=0) {
printf("%s: Fail to get PAGE.pgmutex!\n",__func__);
return -1;
}
/* --------------- ***** 1. FOR 'PAGE' REFRESH, wallpaper etc. ***** ------------ */
/* only if need_refresh */
if(page->ebox->need_refresh)
{
printf("egi_page_refresh(): refresh page '%s'.\n",page->ebox->tag);
EGI_PDEBUG(DBG_PAGE,"refresh page '%s' wallpaper.\n",page->ebox->tag);
/* Also see egi_page_activate(EGI_PAGE *page) */
/* load a picture or use prime color as wallpaper */
if( page->ebox->frame_img !=NULL ) {
EGI_PDEBUG(DBG_PAGE,"Apply page->ebox->frame_img for '%s' wallpaper.\n",
page->ebox->tag);
imgbuf=page->ebox->frame_img;
/* egi_image_setFrame() if necessary */
/* no subcolor, no FB filo */
egi_imgbuf_windisplay2(imgbuf, &gv_fb_dev, 0, 0, 0, 0, imgbuf->width, imgbuf->height);
}
else if(page->fpath != NULL) {
EGI_PDEBUG(DBG_PAGE,"Load '%s' for '%s' wallpaper.\n", page->fpath, page->ebox->tag);
//show_jpg(page->fpath, &gv_fb_dev, SHOW_BLACK_NOTRANSP, 0, 0);
imgbuf=egi_imgbuf_alloc();
/* First try to load as PNG file */
printf("%s: Try to load as PNG file ...\n",__func__);
if(egi_imgbuf_loadpng(page->fpath, imgbuf) !=0) {
// printf("%s: Load PNG imgbuf fail, try egi_imgbuf_loadjpg()...\n",__func__);
/* Then try to load as JPG file */
if(egi_imgbuf_loadjpg(page->fpath, imgbuf) !=0) {
//printf("%s: Load JPG imgbuf fail, try egi_imgbuf_free()...\n",__func__);
}
else {
printf("%s: Finish loading JPG file '%s' as page wallpaper.\n",__func__,
page->fpath);
}
}
else {
printf("%s: Finish loading PNG file '%s' as page wallpaper.\n",__func__,
page->fpath);
}
/* Display it */
if(imgbuf->imgbuf != NULL) {
/* no subcolor, no FB filo */
egi_imgbuf_windisplay2(imgbuf, &gv_fb_dev, 0, 0, 0, 0,
imgbuf->width, imgbuf->height);
}
/* free imgbuf */
EGI_PDEBUG(DBG_PAGE,"egi_imgbuf_free(imgbuf)...\n");
egi_imgbuf_free(imgbuf);
}
else /* use ebox prime color to clear(fill) screen */
{
if( page->ebox->prmcolor >= 0)
{
#if 0
fbset_color( page->ebox->prmcolor );
draw_filled_rect(&gv_fb_dev,0,0,xres-1,yres-1); /* full screen */
#endif
clear_screen(&gv_fb_dev, page->ebox->prmcolor);
}
}
/* decroating job if any */
egi_ebox_decorate(page->ebox);
/* other misc. jobs after listed ebox refreshed */
if( page->page_refresh_misc != NULL )
page->page_refresh_misc(page);
/* reset need_refresh */
page->ebox->need_refresh=false;
page->page_update=true; /* Synchronized with page->ebox->need_refresh currently
* It' an indicator just to inform its child eboxes (which
* are about to be refreshed in following codes.) that
* the PAGE back image (and other items) just refreshed,
* and their bkimgs may need to refresh too.
*/
/* set ret */
ret=0;
}
/* --------------- ***** 2. FOR PAGE CHILD REFRESH ***** ------------*/
/* check list */
if(list_empty(&page->list_head))
{
printf("egi_page_refresh(): page '%s' has an empty list_head .\n",page->ebox->tag);
return -1*ret;
}
/* traverse the list and activate list eboxes, not safe */
/* !!!! WRONG!!!!, if page->ebox->need_refresh is false, this token will mislead ebox
* to ignore fb_cpyfrom_buf()
* It MUST synchronize with page->ebox->need_refresh!!!
* see in above if(page->ebox->need_refresh){ ... }
*/
//page->page_update=true;
list_for_each(tnode, &page->list_head)
{
ebox=list_entry(tnode, EGI_EBOX, node);
ret *= ebox->refresh(ebox);
#if 0 /* Debug only, it will prints out whether need_refresh flag is on or off! */
if(ret==0) {
/* ret==1, means need_refresh=false */
EGI_PDEBUG(DBG_PAGE,"Succeed to refresh page '%s' item ebox: '%s'.\n",
page->ebox->tag,ebox->tag,ret);
}
// else if(ret==1) {
// EGI_PDEBUG(DBG_PAGE,"Fail to refresh page '%s' item ebox: '%s'. need_refresh is false.\n",
// page->ebox->tag,ebox->tag,ret);
// }
#endif
}
/*** COMMENTS:
* It's too later to reset 'need_refresh' here, if any thread try to set 'need_refresh'
* during PAGE child refreshing, it will be reset by following re_assignment!!!
* Move it to end part of PAGE REFRESH codes, though it still poses race condition with other
* threads trying to set 'need_refresh' at any time....
*/
/* reset need_refresh at last */
// page->ebox->need_refresh=false;
/* reset page_update, synchronized with page->ebox->need_refresh currently. */
page->page_update=false;
/* put PAGE.pgmutex */
pthread_mutex_unlock(&page->pgmutex);
return ret; /* if any ebox refreshed, return 0 */
}
/*--------------------------------------------------------------
Just set need_refresh flag for the page, but do not set flag for
its children.
return:
0 OK
<0 fails
----------------------------------------------------------------*/
int egi_page_flag_needrefresh(EGI_PAGE *page)
{
/* 1. check data */
if(page==NULL || page->ebox==NULL)
{
printf("%s: input page or page->ebox is NULL!\n",__func__);
return -1;
}
/* Get pgmutex for PAGE resource access */
if(pthread_mutex_lock(&page->pgmutex) !=0) {
printf("%s: Fail to get PAGE.pgmutex!\n",__func__);
return -1;
}
/*
* Race condition may still exist?
*/
page->ebox->need_refresh=true;
/* put PAGE.pgmutex */
pthread_mutex_unlock(&page->pgmutex);
return 0;
}
/*-----------------------------------------------
Set all eboxes in a page to be need_refresh=true
return:
0 OK
<0 fails
-----------------------------------------------*/
int egi_page_needrefresh(EGI_PAGE *page)
{
struct list_head *tnode;
EGI_EBOX *ebox;
/* 1. check data */
if(page==NULL || page->ebox==NULL)
{
printf("egi_page_needrefresh(): input page or page->ebox is NULL!\n");
return -1;
}
/* 2. Get pgmutex for PAGE resource access */
if(pthread_mutex_lock(&page->pgmutex) !=0) {
printf("%s: Fail to get PAGE.pgmutex!\n",__func__);
return -1;
}
/* 3. check list */
if(list_empty(&page->list_head))
{
printf("egi_page_needrefresh(): page '%s' has an empty list_head.\n",page->ebox->tag);
return -2;
}
/* 4. set page->ebox */
page->ebox->need_refresh=true;
/* 5. traverse the list and set page need_refresh, not safe */
/* WARNING: to avoid mutex deadlock, DO NOT call egi_ebox_needrefresh() */
list_for_each(tnode, &page->list_head)
{
ebox=list_entry(tnode, EGI_EBOX, node);
ebox->need_refresh=true;
EGI_PDEBUG(DBG_PAGE,"find child ebox: '%s' \n",ebox->tag);
}
/* 6.put PAGE.pgmutex */
pthread_mutex_unlock(&page->pgmutex);
return 0;
}
/*--------------------------------------------------------
pick a btn or slider pointer by its type and id number
@page a page struct holding eboxes.
@type type_btn OR type_slider
@id NOTE: id of a EGI_DATA_BTN
return:
pointer OK
NULL fails,or no match
--------------------------------------------------------*/
EGI_EBOX *egi_page_pickbtn(EGI_PAGE *page,enum egi_ebox_type type, unsigned int id)
{
struct list_head *tnode;
EGI_EBOX *ebox;
/* 1. check data */
if(page==NULL || page->ebox==NULL )
{
printf("%s: input page or page->ebox is NULL!\n",__func__);
return NULL;
}
/* 2. check list */
if(list_empty(&page->list_head))
{
printf("%s: page '%s' has an empty list_head.\n",__func__,page->ebox->tag);
return NULL;
}
/* 3. traverse the list to find ebox that matches the given id */
list_for_each(tnode, &page->list_head)
{
ebox=list_entry(tnode, EGI_EBOX, node);
/* Note: EBOX.id for all EBOX!! EBOX.egi_data.id for some EBOXs */
if( ebox->type==type && ((EGI_DATA_BTN *)(ebox->egi_data))->id == id )
{
EGI_PDEBUG(DBG_PAGE,"%s: find an ebox '%s' with data_btn->id=%d in page '%s'. \n",
__func__, ebox->tag,id,page->ebox->tag);
return ebox;
}
}
EGI_PLOG(LOGLV_WARN,"%s: ebox '%s' with id=%d can NOT be found in page '%s'.",
__func__, ebox->tag,id,page->ebox->tag);
return NULL;
}
/*--------------------------------------------------------------
pick a ebox pointer by its type and id number
@page a page struct holding eboxes.
@type type of ebox
@id ID of the ebox. ebox->id, MUST >0.
return:
pointer OK
NULL fails, or no match
-----------------------------------------------------------------*/
EGI_EBOX *egi_page_pickebox(EGI_PAGE *page,enum egi_ebox_type type, unsigned int id)
{
struct list_head *tnode;
EGI_EBOX *ebox;
/* 1. check data */
if(id==0)
{
printf("%s: invalid for id=0!\n",__func__);
return NULL;
}
if(page==NULL || page->ebox==NULL )
{
printf("%s: input page or page->ebox is NULL!\n",__func__);
return NULL;
}
/* 2. check list */
if(list_empty(&page->list_head))
{
printf("%s: page '%s' has an empty list_head.\n",__func__,page->ebox->tag);
return NULL;
}
/* 3. traverse the list to find ebox that matches the given id */
list_for_each(tnode, &page->list_head)
{
ebox=list_entry(tnode, EGI_EBOX, node);
if( ebox->type==type && ebox->id == id )
{
// EGI_PDEBUG(DBG_PAGE,"Find an ebox '%s' with ebox->id=%d in page '%s'. \n",
// ebox->tag,id,page->ebox->tag);
return ebox;
}
}
EGI_PLOG(LOGLV_WARN,"ebox '%s' with id=%d can NOT be found in page '%s'.",
ebox->tag,id,page->ebox->tag);
return NULL;
}
/*----------------------------------------
Start EGI page.runners.
return:
0 OK
<0 fails
----------------------------------------*/
int egi_page_start_runners(EGI_PAGE *page)
{
int i;
if(page==NULL)
return -1;
/* 1. load page runner threads */
EGI_PDEBUG(DBG_PAGE,"start to load [PAGE %s]'s runner...\n",page->ebox->tag);
for(i=0;i<EGI_PAGE_MAXTHREADS;i++)
{
if( page->runner[i] !=0 )
{
/* launch Runners in order */
EGI_PDEBUG(DBG_PAGE,"Start creating runner: pthreadID[%d]=%u ...\n",
i ,(unsigned int)page->threadID[i] );
if( pthread_create( &page->threadID[i],NULL,(void *)page->runner[i],(void *)page)==0)
{
page->thread_running[i]=true;
printf("%s: Create runner pthreadID[%d]=%u successfully. \n", __func__,
i, (unsigned int)page->threadID[i] );
}
else {
EGI_PLOG(LOGLV_ERROR,"%s: Fail to create pthread for runner[%d] of page[%s].", __func__,
i, page->ebox->tag );
/* carry on anyway..... */
}
}
}
/* 2. Initiate thread mutex locks, NOTE: also for egi_page_routine() */
EGI_PDEBUG(DBG_PAGE,"Start to initiate thread mutex lock for page runners.\n");
if(pthread_mutex_init(&page->runner_mutex,NULL) !=0 ) {
EGI_PLOG(LOGLV_ERROR, "%s: Fail to call pthread_mutex_init()!", __func__ );
return -2;
}
if(pthread_cond_init(&page->runner_cond,NULL) !=0 ) {
EGI_PLOG(LOGLV_ERROR, "%s: Fail to call pthread_cond_init()!", __func__ );
return -3;
}
return 0;
}
/*-------------------------------------------
Default page routine job ,No sliding handling
return:
loop or >=0 OK
<0 fails
-------------------------------------------*/
int egi_page_routine(EGI_PAGE *page)
{
// int i;
int ret;
uint16_t sx,sy;
enum egi_touch_status last_status=released_hold;
EGI_TOUCH_DATA touch_data;
/* delay a while, to avoid touch-jittering ???? necessary ??????? */
tm_delayms(200);
/* for time struct */
// struct timeval t_start,t_end; /* record two pressing_down time */
// long tus;
EGI_EBOX *hitbtn; /* hit button_ebox */
EGI_EBOX *last_holdbtn=NULL; /* remember last hold btn */
/* 1. check data */