-
Notifications
You must be signed in to change notification settings - Fork 12
/
egi_surface.c
4238 lines (3482 loc) · 143 KB
/
egi_surface.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.
A module for SURFACE, SURFUSER, SURFMAN.
----- Concept and Definition -----
SURFACE(SURFSHMEM): An imgbuf and relevant image data allocated for an application.
It defines an area for interface of (image) displaying and (mouse) interacting.
At SURFUSER side, it can only access SURFSHMEM part of the SURFACE.
The TOP Surface is the current focused surface on the desktop, and it's
the sole surface that gets mouse/keyboard input from SURFMAN via ERING. --- TODO&TBD,
TASKSURFACE:
A surface associated with a certain task, graphics of the surface are updated
to SURFSHMEM by the task, while rendered by the SURFMAN.
SURFUSER: The owner of a SURFACE, an uclient to SURFMAN. (TODO: It MAY hold
several SURFACEs? XXX One surfuser one surface. ).
However it can only access SURFSHMEM part of the SURFACE, as
a way to protect private data for the SURFMAN.
The SURUSER constrols to move/adjust its position/size by surfuser_parse_mouse_event().
>>> The SURFUSER (or a thread of it) is always blocked at ering_msg_recv() when its
surface is NOT on the TOP layer, and starts to parse input data from SURFMAN once
it's activated.
SURFMAN: It's the manager of all SURFACEs and SURFUSERs, rendering surface
imgbufs to FB, and communicating with surfusers.
It totally contorls data of all SURFACEs.
<<< The SURFMAN keeps eringing input data to the TOP surface.
ERING: MSG ring through local UNIX socket, between SURFACEs and SURFMAN.
!!! --- WARING --- !!!
You CAN NOT use ERING(emsg) to synchronize two threads, use memeber of surfshmem instead.
FirstDraw: To draw an object (surface etc) first time, and MAY also initialize/create images for
control elements (Icon,Button, etc.) on it.
Minibar: A group/list of icons/tabs linked to minimized surfaces, now arranged at left side of screen.
Top Layer Operation:
1. A Top Layer Operation obtain current events(mevent, kevent) exclusively, while other
operations MAY all blocked waiting for event.
Example: 1. Menulist selection operation. 2. Top surface routine.
2. A Top Layer Operation MAY be interrupted/forced to quit by user interventions.
Example: Click on outside area of a MenuList to quit the menu_selection operation.
Click on other Surface to take the place of Top Surface.
----- Routine Process ----
SURFUSER:
1. Register a SURFUSER (with a SURFACE) to SURFMAN by calling egi_register_surfuser(),
via ERING_PATH_SURFMAN.
2. Add (more) SURFACEs to a registered SURFUSER by calling ering_request_surface(),
via connected UNIX type sockfd.
3. Receive ERING message from the SURFMAN by calling ering_msg_recv(BLOCKED),
via connected UNIX type sockfd. < Mouse/Keyboard data >
4. Do routine jobs, update image data to SURFSHMEM and synchronize with SURFMAN.
SURFMAN:
1. Create a SURFMAN by calling egi_create_surfman(), which initializes envs with:
1.1 Establish ERING_PATH_SURFMAN for ERING.
1.2 Initilize/mmap FB device.
2. When an EGI_SURMAN is created, there are 3 threads running for routine jobs.
2.1 userv_listen_thread(): To accept connection from SURFUER uclint, and save to
a session list.
2.2 surfman_request_process_thread(void *surfman): Listen to SURFUSERs by select().
2.2.1 Upon SURFUSER's request, to create and register SURFACEs.
2.2.2 Retire/unregister SURFUSER if it disconnets from ERING.
2.3 surfman_render_thread():
2.3.1 To render all surfaces/mouse/menus to FB.
2.3.2 To render mouse cursor.
2.3.3 Synchronize with all SURFUSER(SURFSHMEM).
3. Main thread of the SURFMAN.
3.1 Scan mouse and keyboard input, and parse it.
3.2 Update mouse position, as for surfman ->mx,my.
3.3 Adjust SURFACE rendering sequence, bring a SURFACE to top by mouse clicking.
3.4 Minimize/Maximize/Close SURFACEs. (by mouse clicking)
3.5 Send mouse status to SURFUSER of the current top(focused) surface.
3.6 Send keyboard input to SURFUSER of the current top(focused) surface.
3.7 XXX Move SURFACEs by mouse dragging. (OR by SURFUSER? --OK )
Note:
1. Anonymous shared memory leakage/not_freed?? Busybox: ipcs ipcrm
2. Surface lib functions call fbset_color() to apply fb_color, while APP set pixcolor_on and
call fbset_color2() to use FBDEV.pixcolor, thus to avoid muti_thread interference.
OR: both use its own FBDEV.pixcolor.
OK -----> Set VFBDEV.pixcolor_on at egi_register_surfuser()
!!! --- CAUTION --- !!!
Set VFBDEV.pixcolor_on after reinit_virt_fbdev().
TODO:
1. XXX Abrupt break of a surfuser will force surfman_request_process_thread()
and surfman_render_thread() to quit!!!
--- OK, Apply PTHREAD_MUTEX_ROBUST.
2. Check sync/need_refresh token of each surface before rendering, skip
it if the surface image is not updated, thus may help surfman cutdown rendering time.
However, mouse cursor has to be refreshed directly on the working buffer.
since hardware does NOT support 2_layer frame buffer, working buffer
need redraw the mouse cursor....
3. Syn eringing mostat:
NOW: surfman awaits until surfuer clears flag SURFACE_FLAG_MEVENT(finishing parsing last mevent), and all mostats
are ignored during that period, which reuslts in SUM(mouseDY) != mousteY
4. Close surface brutly by calling shutdown( (*surfuser)->uclit->sockfd, SHUT_RDWR), which will force process to exit immediately!???
surfuser_close_surface(): To avoid thread_EringRoutine be blocked at ering_msg_recv(), JUST brutly close sockfd
and force the thread to quit. ....
....??? Callback user defined SURFSHMEM.close_surface().
5. If a SURFUSER do NOT release its shmem_mutex, then SURFMAN's surfman_render_thread will be BLOCKED!
Each SURFSUER MAY need a workFB? as a VFrameImg in a virtual FBDEV.
SAVE THAT AS DESSERT ;)
6. If a frame of a SURFUSER vfbdev lives with very short time, it may be skipped by surfman_render_thread().
(sync) <----------
7. Add a list for SURFACE to hook up all control elements.
8. It's NOT reliable to deem ERING_SURFACE_BRINGTOP emsg as start of a new mevent round!
See NOTE at surfuser_parse_mouse_event().
9. Sync. mechanism for SURFACE mouse_event_suspend and
NOW: LeftKeyUp as mevent_suspend signal token.
10. FT_library for mutli_thread
NOW: Apply lib_mutex for EGI_SYSFONTS.
11. XXX A menulist tree can be created/displayed within a surface.
But how to create menulist SURFACE. --- Ok
12. If a SURFACE launches/creates a child SURFACE by calling egi_register_surfuser() (NOT in an new thread or process)
and then waits for it to end, then the last mouse event to close the child SURFACE may ALSO trigger the calling SURFACE.
13. If a SURFMAN creates a surfuser in the same process, then virtual addresses of surfman.surfaces[]->surfshmem and surfuser->surfshmem
is NOT the SAME!! Why?
Journal
2021-02-22:
1. Add surfman_render_thread().
2. Apply surfman_mutex.
2021-02-23:
1. Add surface_compare_zseq() and surface_insertSort_zseq().
2021-02-24:
1. Add EGI_SURFUSER, egi_register_surfuser()/egi_unregister_surfuser()
2021-02-25:
1. Add mutex_lock and mutexattr for surface
2021-02-26:
1. Separate EGI_SURFSHMEM from EGI_SURFACE.
2021-02-27:
1. Apply PTHREAD_MUTEX_ROBUST for surfshmem->shmem_mutex.
2021-03-2:
1. Apply surfman->mcursor.
2021-03-3:
1. Add surfman_xyget_Zseq().
2. Add surfman_xyget_surfaceID().
3. surfman_bringtop_surface_nolock().
2021-03-10:
1. Test Minimize/Maximize surfaces.
2. surfman_bringtop_surface_nolock(): Update surfaces[]->id!
2021-03-11:
1. Definition and description of SURFACE, SURFUSER, SURFMAN.
2. Put SURFACE name to leftside minibar menu. (struct memeber surfnam[] for SURFSHMEM)
3. Mouse click on leftside minibar to bring the SURFACE to TOP layer.
(Add struct memeber IndexMpMinSurf for SURFMAN)
2021-03-12:
1. Add ESURF_BTN: egi_surfBtn_create(), egi_surfBtn_free().
2. Add egi_point_on_surfBtn()
2021-03-13:
1. Move surface->status to surface->surfshmem->status, so SURFUSER can access.
2021-03-15:
1. surfman_render_thread(): If surface status is downhold, then draw a rim to highlight it.
2021-03-16:
1. Add maxW,maxH for EGI_SURFSHMEM, and add them to following functions
as input paramters:
egi_register_surfuser(), surfman_register_surface(), ering_request_surface()
2021-03-21:
1. surfman_render_thread(): Send ERING_SURFACE_BRINGTOP to the new selected TOP surface.
A surface is brought to TOP by means of:
A. Picked by surfman. see mouse click event in test_surfman.c.
B. The prior TOP surface is minimized by surfuser. see mouse event in test_surfuser.c
c. The prior TOP surface is closed by surfuser.
2. Add EGI_SURFSHMEM memebers: .sbtns[3], .mptbn
2021-03-22:
1. Add: surfuser_resize_surface(), surfuser_parse_mouse_event().
2. Add EGI_SURFSHMEM memebers: .resize_surface()
2021-03-24:
1. Add default surface operations:move/resize/minimize/maximize/normalize/close
2021-03-25:
1. Rename surfuser_resize_surface() to surfuser_redraw_surface()
2021-03-27:
1. Add default surface operations: surfuser_firstdraw_surface()
2021-03-28:
1. Make surface top_bar icons (CLOSE/MIN/MAX) optional.
surfuser_firstdraw_surface(): Add param 'topbtns', make MAX/MIN/CLOSE buttons optional.
surfuser_redraw_surface(): Redraw topbar SURFBTNs only for selected tbns.
2021-03-29:
1. surfuser_parse_mouse_event():
Use (mouseX/Y-lastX/Y) instead of mouseDX/DY for adjust_size to avoid slip away.
2. SURFACE_FLAG_MEVENT: SRUFMAN set, SURFUSER reset.
Try to suspend ering mostat while SURFUSER is busy parsing last mostat.
2021-04-02:
1. Rename EGI_SURFBTN to ESURF_BTN.
2. Add: ESURF_BOX, ESURF_TICKBOX and their funcs.
2021-04-03:
1. surfuser_parse_mouse_event(): Add callback surfshmem->user_mouse_event().
2021-04-06:
1. Add: egi_point_on_surface().
2021-04-07:
1. Spin off surface controls to egi_surfcontrols.c
2021-04-16:
1. Add member 'pid' and 'level' for EGI_SURFACE, and modify folloing functions:
1.1 ering_request_surface(): Add pid for the calling process.
1.2 surfman_register_surface(): Assign eface.pid and eface.level
1.3 surface_compare_zseq() : Also compare with surface->level
1.4 surfman_bringtop_surface_nolock(): If surfaces[surfID] has child,
then bringtop its child with the biggest level value.
2021-04-17:
1. Add member 'bool ering_bringTop' for EGI_SURFUSER.
2. surfuser_parse_mouse_event(): use 'ering_bringTop==true' as start of a new
round of mevent parsing session.
2021-04-20:
1. Add member 'ESURF_BOX sboxes[]' for EGI_SURFSHMEM. For menu test.
2021-04-21:
1. Add surfuser_ering_routine().
2021-04-26:
1. surfuser_firstdraw_surface(): Param. 'topbtns' modified to be 'options'.
2021-04-29:
1. EGI_SURFSHMEM: Add memeber user_close_surface() for EGI_SURFSHMEM.
2. EGI_SURFUSER: Add memeber 'mevent_suspend'. as start token of new round of mevent session.
2021-05-06:
1. EGI_SURFMAN: Add memeber 'msgid' for message queue.
Modify: egi_create_surfman(), egi_destroy_surfman() --- create/remove surfman->msgid.
2. Add: SURFMSG_DATA
2021-05-07:
1. EGI_SURFUSER: Add memeber 'msgid' for message queue.
Modify: egi_register_surfuser() --- msgget msgid.
2021-05-11:
1. surfman_bringtop_surface_nolock(): BringUp all group surfaces with same PID value.
2. Add: surfmsg_send(), surfmsg_recv()
3. surfman_render_thread() TEST: surfmsg_recv()SURFMSG_REQUEST_REFRESH
Rendering all surfaces ONLY when SURFMSG_REQUEST_REFRESH is received.
2021-05-12:
1. surfman_render_thread(): Display Minibar at SURFMAN_MINIBAR_PIXZ layer.
2021-05-14:
1. EGI_SURFUSER: Add memeber 'menulist'
2. surfman_render_thread(): Draw surfman->menulist.
2021-05-18:
1. egi_unregister_surfuser(): Free topbar CLOSE/MIN/MAX btns in surfshmem->sbtns[].
2021-05-19:
1. surfman_render_thread(): Add triangular ushering marks at left_top and left_bottom.
2021-06-07:
1. Add member 'fd_flock' for EGI_SURFUSER.
2. Modify egi_register_surfuser(): call egi_lock_pidfile() to get fd_flock.
2. Modify egi_unregister_surfuser(): to close(fd_flock).
2021-06-13:
1. surfuser_ering_routine():
1.1 If sockfd broken when calling ering_msg_recv(surfuser->uclit->sockfd, emsg)
DO NOT exit there, let main thread do it.
1.2 Add memeber 'eringRoutine_running' for EGI_SURFSHMEM.
1.3 Set/reset surfuser->surfshmem->eringRoutine_running.
2021-06-24:
1. surfuser_ering_routine(): case ERING_SURFACE_CLOSE, the surfman request to close the surface.
2. Add: surfman_minimize_allSurfaces(), surfman_normalize_allSurfaces().
2021-06-25:
1. surfman_bringtop_surface_nolock(): If it has any brother/child surfaces, bringtop together from minibar.
bringtop surfaces with same pid.
2021-06-28:
1. surfuser_ering_routine(): If SURFMAN quits, set signal usersig=1.
2021-06-29:
1. Add memeber 'ESURF_LABEL *menus[TOPMENU_MAX]' for EGI_SURFSHMEM
2. surfuser_firstdraw_surface(): Firstdraw top menus as per input parameters: int menuc, const char **menuv
3. surfuser_redraw_surface(): Redraw top muens (label text).
2021-07-01:
1. surfuser_parse_mouse_event():
Case clicking on TOPBTN_MIN_INDEX: set surfuser->mevent_suspend=true to suspend surface.
2021-07-02:
1. Add member 'topmenu_bkgcolor', 'topmenu_hltbkgcolor' for EGI_SURFSHMEM.
1.1 Modify surfuser_firstdraw_surface() accordingly.
1.2 Modifu surfuser_redraw_surface() accordingly.
2. surfuser_parse_mouse_event(): 2A. If mouse touches topmenus[], refresh its image.
2021-07-14:
1. Add member 'menu_react[]' for EGI_SURFSHMEM
2. surfuser_parse_mouse_event(): Call menu_react[surfshmem->mpmenu] if a TOPMENU is clicked.
2021-07-19:
1. surfman_register_surface(): Init. surfshmem->bkgcolor = SURF_MAIN_BKGCOLOR
2. EGI_SURFSHMEM: Add member 'prvbtnsMAX', 'prvbtns', 'mpprvbtn'. Init mpprvbtn=-1 at surfman_register_surface().
2021-07-20:
1. surfuser_close_surface(): check confirmation.
2022-04-15:
1. surfuser_firstdraw_surface(): parse option SURFFRAME_NONE and SURFFRAME_THICK.
2022-04-16:
1. surfman_unregister_surface(): If ERR goto FREE_AND_SORT.
2. surfman_render_thread(): Check and bringTop in case the previou TopSurface is minimized/retired.
2022-04-18:
1. egi_destroy_surfman(): To end renderThread at last when shut down, so it can show surfaces destoryed sequencely.
2022-04-24:
1. surfuser_firstdraw_surface(): firstdraw all elements with vh=maxH/vw=maxW, restore vh/vw at the end.
2. struct SURFSHMEM: Add member 'apoptions' for apperance options.
2022-04-026:
1. egi_register_surfuser(): Set VFBDEV.pixcolor_on at egi_register_surfuser()
2. Set VFBDEV.pixcolor_on after reinit_virt_fbdev(): surfuser_firstdraw_surface(), surfuser_redraw_surface()
2022-05-08:
1. Add surfuser_set_name().
2022-05-11:
1. Add ering_msg_type ERING_SURFACE_REFRESH.
2. Add SURFSHMEM.refresh_surface.
3. Add surfuser_refresh_surface().
4. surfuser_ering_routine(): case ERING_SURFACE_REFRESH, call surfuser_refresh_surface(surfuser).
5. egi_unregister_surfuser(): unlock fd_flock before close(fd_flock).
2022-05-13:
1. Add surfman_get_SurfuserCSID()
2. Add surfman_surfuser_surface()
2022-05-15:
1. EGI_SURFMAN: Add IME members: imeThread, imeThread_on, surfuserIME, surfaceIME.
2022-05-16:
1. surfman_render_thread(): Make surfaceIME always float at top layer.
Midas Zhou
midaszhou@yahoo.com(Not in use since 2022_03_01)
知之者不如好之者好之者不如乐之者
---------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/select.h>
#include <sys/msg.h>
#include <sys/file.h>
#include "egi_log.h"
#include "egi_unet.h"
#include "egi_timer.h"
#include "egi_debug.h"
#include "egi_surface.h"
#include "egi_fbgeom.h"
#include "egi_image.h"
#include "egi_math.h"
#include "egi_FTsymbol.h"
#include "egi_input.h"
#include "egi_utils.h"
static void* surfman_request_process_thread(void *surfman);
static void * surfman_render_thread(void *surfman);
/*** NOTE: To keep consistent with definition of SURF_COLOR_TYPE! */
static int surf_colortype_pixsize[]=
{
[SURF_RGB565] =2,
[SURF_RGB565_A8] =3,
[SURF_RGB888] =3,
[SURF_RGB888_A8] =4,
[SURF_RGBA8888] =4,
[SURF_COLOR_MAX] =4,
};
/*-------------------------------------------------
Get pixel size as per the color type. Pixel size is
the sum of RBG_size and ALPHA_size.
In case colorType outof range, then return 4 as MAX!
--------------------------------------------------*/
int surf_get_pixsize(SURF_COLOR_TYPE colorType)
{
if( colorType >= SURF_COLOR_MAX ) /* In case... */
return surf_colortype_pixsize[SURF_COLOR_MAX];
else
return surf_colortype_pixsize[colorType];
}
/*--------------------------------------------------------
Create an anonymous file(as anonymous memory) and ftruncate
its size.
@name: Name of the anonymous file.
@memsize: Inital size of the file
Reutrn:
>0 OK, the file descriptor that refers to the
anonymous memory.
<0 Fails
--------------------------------------------------------*/
int egi_create_memfd(const char *name, size_t memsize)
{
int memfd;
/* Create memfd, the file descriptor is open with (O_RDWR) and O_LARGEFILE. */
#ifdef SYSCALL_MEMFD_CREATE /* see egi_unet.h */
memfd=syscall(SYS_memfd_create, name, 0); /* MFD_ALLOW_SEALING , MFD_CLOEXEC */
#else
memfd=memfd_create(name,0); /* MFD_ALLOW_SEALING , MFD_CLOEXEC */
#endif
if(memfd<0) {
egi_dperr("Fail to create memfd!");
return -1;
}
if( ftruncate(memfd, memsize) <0 ) {
egi_dperr("ftruncate");
close(memfd);
return -2;
}
// fsync(memfd);
return memfd;
}
/*-------------------------------------------------
Mmap fd to get pointer to an EGI_SURFSHMEM
Note: memfd NOT close here!
@memfd: A file descriptor created by memfd_create.
Reutrn:
A pointer to EGI_SURFSHMEM OK
NULL Fails
--------------------------------------------------*/
EGI_SURFSHMEM *egi_mmap_surfshmem(int memfd)
{
EGI_SURFSHMEM *surfshmem=NULL;
struct stat sb;
if (fstat(memfd, &sb)<0) {
egi_dperr("fstat");
return NULL;
}
surfshmem=mmap(NULL, sb.st_size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_SHARED, memfd, 0);
if(surfshmem==MAP_FAILED) {
egi_dperr("Fail to mmap memfd!");
return NULL;
}
return surfshmem;
}
/*------------------------------------------
Unmap an EGI_SURFSHMEM.
@eface: Pointer to EGI_SURFSHMEM.
Return:
0 OK
<0 Fails
------------------------------------------*/
int egi_munmap_surfshmem(EGI_SURFSHMEM **shmem)
{
if(shmem==NULL|| (*shmem)==NULL)
return 0;
if( *shmem==MAP_FAILED )
return 0;
/* Unmap surface */
if( munmap(*shmem, (*shmem)->shmsize)!=0 ) {
//printf("%s: Fail to unmap eface!\n",__func__);
egi_dperr("Fail to unmap eface!");
return -1;
}
/* Reset *shmem */
*shmem = NULL;
return 0;
}
/*---------------------------------------------------
Compare surfaces with surface->zseq and surface->level.
A NULL surface always has Min. zseq value!
Return:
Relative Priority Sequence position:
-1 (<0) eface1->zseq < eface2->zseq
0 (=0) eface1->zseq = eface2->zseq and same level
-1 OR 1 NOT same level.
1 (>0) eface1->zset > eface2->zseq
---------------------------------------------------*/
int surface_compare_zseq(const EGI_SURFACE *eface1, const EGI_SURFACE *eface2)
{
/* Check NULL surfaces */
if( eface1 == NULL && eface2 == NULL )
return 0;
else if( eface1 == NULL )
return -1;
else if( eface2 == NULL )
return 1;
/* NOW: eface1 != NULL && eface2 != NULL */
if( eface1->zseq < eface2->zseq )
return -1;
else if( eface1->zseq == eface2->zseq ) {
/* NOTE: In surfman_register_surface(), surfaces with same pid will all be assigned with MAX zseq. */
if ( eface1->level > eface2->level )
return 1;
else if( eface1->level < eface2->level )
return -1;
else /* Same level value, MUST all level==0! means no parent. */
return 0;
}
else /* eface1->zseq > eface2->zseq */
return 1;
}
/*----------------------------------------------------------------
Sort an EGI_SURFACE array by Insertion Sort algorithm, to rearrange
surfaces in ascending order of surface->zseq!
Then re_assign id AND serial zseq for all surfaces!
Note:
1. The caller MUST enusre surfaces array has at least n memmbers!
2. NULL surface to be rearranged at start of the array.
@surfaces: An array of surface pointers.
@n: Size of the array. n>1!
------------------------------------------------------------------*/
void surface_insertSort_zseq(EGI_SURFACE **surfaces, int n)
{
int i,k;
int maxzseq;
EGI_SURFACE *tmp;
if( surfaces==NULL || n==1)
return;
for(i=1; i<n; i++) {
tmp=surfaces[i];
for(k=i; k>0 && surface_compare_zseq(surfaces[k-1], tmp)>0 ; k--) {
surfaces[k]=surfaces[k-1]; /* swap nearby */
}
/* Settle the inserting surface point at last swapped place */
surfaces[k]=tmp;
}
///// NOTE: if the function is applied for quickSort, then following MUST NOT be included /////////
/* Get maxzseq */
maxzseq=0;
for(i=0; i< SURFMAN_MAX_SURFACES; i++)
if(surfaces[i]) maxzseq++;
/* Reset all surface->id as index of surfman->surfaces[], Reset all zseq */
for(i=SURFMAN_MAX_SURFACES-1; i>=0; i--) {
if(surfaces[i]==NULL) { /* NULL followed by NULL */
break;
}
else {
surfaces[i]->id=i; /* Reset index of surfman->surfaces[] as ascending order */
surfaces[i]->zseq = maxzseq--;
}
}
}
/*-------------------------------------------------------------
Register/create an EGI_SURFUSER from an EGI_SURFMAN via svrpath.
TODO: NOW, Support ColorType SURF_RGB565 and SURF_RGB565_A8.
@svrpath: Path to an EGI_SURFMAN.
@x0,y0: Surface position relative to FB
@maxW,maxH: Max. Surface width and height
@w,h: Default surface width and height.
@colorType: Surface color data type
Return:
A pointer to EGI_SURFUSER OK
NULL Fails
-------------------------------------------------------------*/
EGI_SURFUSER *egi_register_surfuser( const char *svrpath, const char *pid_lock_file,
int x0, int y0, int maxW, int maxH, int w, int h, SURF_COLOR_TYPE colorType )
{
int fd_flock;
EGI_SURFUSER *surfuser;
/* Check if instance already running */
fd_flock=0;
if(pid_lock_file) {
if( (fd_flock=egi_lock_pidfile(pid_lock_file)) <=0 ) {
egi_dperr("Fail to lock pidfile, an instance is already running?");
return NULL;
}
}
/* Check input */
if( colorType != SURF_RGB565 && colorType != SURF_RGB565_A8 ) {
egi_dpstd("NOW Only support colorType for SURF_RGB565 and SURF_RGB565_A8!\n");
return NULL;
}
if(w<1 || h<1)
return NULL;
if(maxW<w) maxW=w;
if(maxH<h) maxH=h;
/* 1. Calloc surfuser */
surfuser=calloc(1, sizeof(EGI_SURFUSER));
if(surfuser==NULL) {
return NULL;
}
/* 2. Link to EGI_SURFMAN */
surfuser->uclit=unet_create_Uclient(svrpath);
if(surfuser->uclit==NULL) {
egi_dpstd("Fail to create Uclient!\n");
free(surfuser);
return NULL;
}
egi_dpstd("Uclient succeed to connect to '%s'.\n", svrpath);
/* 3. Get message queue, SURFMAN is the Owner. */
key_t mqkey=ftok(ERING_PATH_SURFMAN, SURFMAN_MSGKEY_PROJID);
surfuser->msgid = msgget(mqkey, 0); /* SURFMAN created it */
if(surfuser->msgid<0) {
egi_dperr("Fail msgget()!");
unet_destroy_Uclient(&surfuser->uclit);
free(surfuser);
return NULL;
}
/* 4. Request for a surface and get shared memory data */
surfuser->surfshmem=ering_request_surface( surfuser->uclit->sockfd, x0, y0, maxW, maxH, w, h, colorType);
if(surfuser->surfshmem==NULL) {
egi_dpstd("Fail to request surface!\n");
unet_destroy_Uclient(&surfuser->uclit);
free(surfuser);
return NULL;
}
/* 5. Allocate an EGI_IMGBUF, for its vfbdev. */
surfuser->imgbuf=egi_imgbuf_alloc();
if(surfuser->imgbuf==NULL) {
egi_dpstd("Fail to allocate imgbuf!\n");
egi_munmap_surfshmem(&surfuser->surfshmem);
unet_destroy_Uclient(&surfuser->uclit);
free(surfuser);
return NULL;
}
/* 6. Map surface data to imgbuf */
surfuser->imgbuf->width = w; /* Default surface size */
surfuser->imgbuf->height = h;
surfuser->imgbuf->imgbuf = (EGI_16BIT_COLOR *)surfuser->surfshmem->color; /* SURF_RGB565 ! */
if( surfuser->surfshmem->off_alpha >0 )
surfuser->imgbuf->alpha = (EGI_8BIT_ALPHA *)(surfuser->surfshmem->color+surfuser->surfshmem->off_alpha);
else
surfuser->imgbuf->alpha = NULL;
/* 7. Init. virtual FBDEV */
if( init_virt_fbdev(&surfuser->vfbdev, surfuser->imgbuf, NULL) != 0 ) {
egi_dpstd("Fail to init vfbdev!\n");
surfuser->imgbuf->imgbuf=NULL; /* Unlink to surface data before free imgbuf */
surfuser->imgbuf->alpha=NULL;
egi_imgbuf_free2(&surfuser->imgbuf);
egi_munmap_surfshmem(&surfuser->surfshmem);
unet_destroy_Uclient(&surfuser->uclit);
free(surfuser);
return NULL;
}
/* 7A. Set VFBDEV.pixcolor_on to avoid interference! MidasHK_2022-04-26 */
surfuser->vfbdev.pixcolor_on=true;
/* xxxx. Init first 3 surfuser->surfbtns[] as for CLOSE/MIN/MAX --- */
/* NOW in surfuser_firstdraw_surface() */
/* Assign other memebers */
surfuser->fd_flock = fd_flock; /* If pid_lock_file==NULL, then fd_lock=0 */
surfuser->mevent_suspend = true;
/* OK */
return surfuser;
}
/*---------------------------------------------------------
Unregister/destory an EGI_SURFUSER.
!!! WARNING !!! Imgbuf data is unlinked before destory the surfuser.
@surfuser: Pointer to pointer to an EGI_SURFUSER
Return:
0 OK
<0 Fails
----------------------------------------------------------*/
int egi_unregister_surfuser(EGI_SURFUSER **surfuser)
{
int i;
int ret=0;
if(surfuser==NULL || (*surfuser)==NULL)
return -1;
/* 0. Make sure surfshmem->shmem_mutex unlocked! */
//pthread_mutex_unlock(&surfshmem->shmem_mutex);
#if 0 /* XXX NOPE!! Should NOT join eringRoutine in egi_unregister_surfuser()! Put it at end of main()! */
/* 1. Join ering_routine */
(*surfuser)->surfshmem->usersig =1; // Useless if thread is busy calling a BLOCKING function.
//pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
//pthread_cancel( (*surfuser)->surfshmem->thread_eringRoutine );
tm_delayms(100);
/* Make sure mutex unlocked in pthread if any! */
if( pthread_join((*surfuser)->surfshmem->thread_eringRoutine, NULL)!=0 ) {
egi_dperr("Fail to join eringRoutine");
ret = -1;
}
#endif
/* 1. Free topbar CLOSE/MIN/MAX btns: surfshmem->sbtns[] */
//egi_dpstd("free topbar sbtns...\n");
for(i=0; i<TOPBTN_MAXNUM; i++)
egi_surfBtn_free( &((*surfuser)->surfshmem->sbtns[i]) );
//free((*surfuser)->surfshmem->sbtns[i]);
/* 1A. Free top menus[] */
for(i=0; i<TOPMENU_MAX; i++)
egi_surfLab_free( &((*surfuser)->surfshmem->menus[i]) );
/* 2. Release virtual FBDEV */
//egi_dpstd("release_virt_fbdev...\n");
release_virt_fbdev(&(*surfuser)->vfbdev);
/* 3. Unlink imgbuf data and free the holding imgbuf */
//egi_dpstd("Free surfuser->imgbuf...\n");
(*surfuser)->imgbuf->imgbuf=NULL;
(*surfuser)->imgbuf->alpha=NULL;
egi_imgbuf_free2(&(*surfuser)->imgbuf);
/* 4. Unmap surfshmem */
//egi_dpstd("munmap_surfshmem...\n");
if( egi_munmap_surfshmem(&(*surfuser)->surfshmem) !=0 )
ret += -(1<<1);
/* 5. Free uclit, and close its socket. SURFMAN should receive signal to retire the surface. */
//egi_dpstd("unet_destroy_Uclient...\n");
if( unet_destroy_Uclient(&(*surfuser)->uclit) !=0 )
ret += -(1<<2);
/* 6. close(fd_flock) to unlock file. */
//egi_dpstd("Close fd_flock ...\n");
if( (*surfuser)->fd_flock >0 ) {
flock((*surfuser)->fd_flock,LOCK_UN); /* ;) Hi! HK2022-05-11 */
close((*surfuser)->fd_flock);
}
/* 7. Free surfuser struct */
free(*surfuser);
*surfuser=NULL;
return ret;
}
/*------------------------------------------
Set a name for SURFUSER.surfshmem->surfname.
Return:
0 OK
<0 Fails
-------------------------------------------*/
int surfuser_set_name(EGI_SURFUSER *surfuser, const char *name)
{
if(surfuser==NULL || surfuser->surfshmem==NULL || name==NULL)
return -1;
strncpy(surfuser->surfshmem->surfname, name, SURFNAME_MAX-1);
return 0;
}
/*---------------------------------------------------------
Request a surface from the Surfman via local socket.
Return a surfshmem pointer to shared memory space.
Note:
1. It will be BLOCKED while waiting for reply.
2. DO NOT forget to munmap it after use!
@sockfd A valid UNIX SOCK_STREAM type socket, connected
to an EGI_SURFACE server.
Assume to be BLOCKING type.
@x0,y0 Initial origin of the surface.
@maxW,maxH Max. surface size.
@w,h Default width and height of a surface.
@colorType Surface color type
Also including alpha size!
Return:
A pointer to EGI_SURFSHEM OK
NULL Fails
----------------------------------------------------------*/
EGI_SURFSHMEM *ering_request_surface(int sockfd, int x0, int y0, int maxW, int maxH, int w, int h, SURF_COLOR_TYPE colorType)
{
EGI_SURFSHMEM *surfshm=NULL;
struct msghdr msg={0};
struct cmsghdr *cmsg;
int data[16]={0};
struct iovec iov={.iov_base=data, .iov_len=sizeof(data) };
int memfd;
/* Check input */
if( w<1 || h<1 )
return NULL;
if(maxW<w) maxW=w;
if(maxH<h) maxH=h;
/* 1. Prepare request params, put in data[]
* Request data[] content:
* data[0]:ering_request_type,
* data[1]:x0, data[2]:y0,
* data[3]:maxW, data[4]:maxH, data[5]:width, data[6]:height, data[7]:colorTYpe
* data[8]: pid of the calling process
*/
memset(data,0,sizeof(data));
data[0]=ERING_REQUEST_ESURFACE;
data[1]=x0; data[2]=y0;
data[3]=maxW; data[4]=maxH;
data[5]=w; data[6]=h;
data[7]=colorType;
data[8]=getpid();
/* 2. MSG iov data */
msg.msg_iov=&iov; /* iov holds data */
msg.msg_iovlen=1;
/* 3. Send msg to server */
if( unet_sendmsg(sockfd, &msg) <=0 ) {
//printf("%s: unet_sendmsg() fails!\n", __func__);
egi_dpstd("unet_sendmsg() fails!\n");
return NULL;
}
/* 4. Prepare MSG buffer, to receive memfd */
union {
/* Wrapped in a union in ordert o ensure it's suitably aligned */
char buf[CMSG_SPACE(sizeof(memfd))]; /* CMSG_SPACE(): space with header and required alignment. */
struct cmsghdr align;
} u;
msg.msg_control = u.buf;
msg.msg_controllen = sizeof(u.buf);
/* 5. Wait to receive msg */
egi_dpstd("Request msg sent out, wait for reply...\n");
if( unet_recvmsg(sockfd, &msg) <=0 ) {
egi_dpstd("unet_recvmsg() fails!\n");
return NULL;
}
/* 6. Check request result */
if( data[0]!=ERING_RESULT_OK ) {
switch(data[0]) {
case ERING_RESULT_ERR:
egi_dpstd("Request fails for 'ERING_RESULT_ERR'!\n");
break;
case ERING_MAX_LIMIT:
egi_dpstd("Request fails for 'ERING_MAX_LIMIT'!\n");
break;
default:
egi_dpstd("Request fails for undefined error! data[0]=%d\n", data[0]);
}
return NULL;
}
/* 7. Get returned memfd */
cmsg = CMSG_FIRSTHDR(&msg);
/* TODO: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] */
memfd = *(int *)CMSG_DATA(cmsg);
//memcpy(&memfd, (int *)CMSG_DATA(cmsg), n*sizeof(int));
egi_dpstd("get memfd=%d\n", memfd);
/* 8. Mmap memfd to get pointer to EGI_SURFACE */
surfshm=egi_mmap_surfshmem(memfd);
if(surfshm==NULL) {
close(memfd);
return NULL;
}
/* 9. Close memfd */
if(close(memfd)<0) {
//printf("Fail to close memfd, Err'%s'.!\n", strerror(errno));
egi_dperr("Fail to close memfd.");
}
/* OK */
return surfshm;
}
/*---------------- A thread function -------------
Start EGI_SURFMAN request_process thread.
Disconnected clients will NOT be detected here!
@arg Pointer to an EGI_SURFMAN.
Return:
0 OK
<0 Fails
------------------------------------------------*/
static void* surfman_request_process_thread(void *arg)
{
int i; /* USERV_MAX_CLIENTS */
int rfds;
int csFD;
int maxfd;
fd_set set_uclits;
struct timeval tm;
int nrcv;
//int nsnd;
int memfd;
int sfID; /* surface id, as index of surfman->surface[] */
/* Surface request MSG buffer */
struct msghdr msg={0};
struct cmsghdr *cmsg;
/*** Request data[] content:
* data[0]:ering_request_type,
* data[1]:x0, data[2]:y0,
* data[3]:maxW, data[4]:maxH, data[5]:width, data[6]:height, data[7]:colorTYpe
*/
int data[16]={0};
struct iovec iov={.iov_base=&data, .iov_len=sizeof(data) };
/* Prepare MSG_control buffer: msg.msg_control = u.buf */
union {
/* Wrapped in a union in ordert o ensure it's suitably aligned */
char buf[CMSG_SPACE(sizeof(memfd))]; /* CMSG_SPACE(): space with header and required alignment. */
struct cmsghdr align;
} u;
EGI_SURFMAN *surfman=(EGI_SURFMAN *)arg;
if(surfman==NULL || surfman->userv==NULL)
return (void *)-1;
EGI_USERV_SESSION *sessions=surfman->userv->sessions;
if(sessions==NULL)
return (void *)-2;
/* Set select wait timeout */
tm.tv_sec=0;
tm.tv_usec=500000;
/* Enter routine loop */
egi_dpstd("Start routine job...\n");
surfman->repThread_on=true;
while( surfman->cmd != SURFMAN_CMD_END_REPTHREAD ) {
/* If there is no surfuser connected */
if( surfman->userv->ccnt==0) {
usleep(50000);
continue;
}
/* Add all clients to select set */
maxfd=0;
FD_ZERO(&set_uclits);
for(i=0; i<USERV_MAX_CLIENTS; i++) {
//if( surfman->userv->sessions[i].alive ) { //csFD>0 )
if( sessions[i].alive ) {
csFD=sessions[i].csFD;
//csFD=surfman->userv->sessions[i].csFD;
FD_SET(csFD, &set_uclits);
if( csFD > maxfd )
maxfd=csFD;
}
}
/* select readable fds, all fds to be NONBLOCKING. */
// egi_dpstd("Select maxfd=%d\n", maxfd);
tm.tv_sec=0;
tm.tv_usec=500000;
rfds=select(maxfd+1, &set_uclits, NULL, NULL, &tm);
/* Case 1: No readbale fds, No request */
if(rfds==0) {
//egi_dpstd("select() rfd=0!\n");
//usleep(50000);
continue;
}
/* Case 2: Error occurs */