Browse code

Fix background loading for windows target

Dario Rodriguez authored on 20/04/2025 18:08:53
Showing 4 changed files
... ...
@@ -7,7 +7,7 @@ all: imgmover imgmover.exe
7 7
 imgmover: imgmover.c roboto_regular.c
8 8
 	$(CC) $(CFLAGS) -Iexternal/raylib/include/ imgmover.c external/raylib/libraylib.a -lm $(LDFLAGS) -o imgmover
9 9
 
10
-imgmover.exe: imgmover.c roboto_regular.c
10
+imgmover.exe: imgmover.c win32_pipe.c roboto_regular.c
11 11
 	sh -c "if [ ! -e test-icon_256x256.png ] ; then convert -size 256x256 xc:white test-icon_256x256.png ; fi"
12 12
 	toolchain-zig/compile.sh imgmover.c -c -o imgmover-windows.o
13 13
 	toolchain-zig/compile.sh win32_pipe.c -c -o win32_pipe-windows.o
... ...
@@ -33,6 +33,7 @@
33 33
  *      20250329 Background loading of thumbnails.
34 34
  *      20250330 Refine background loading.
35 35
  *      20250413 Background loading for windows target.
36
+ *      20250420 Fix background loading for windows target.
36 37
  *
37 38
  * Author: Dario Rodriguez dario@darionomono.com
38 39
  * (c) Dario Rodriguez 2025
... ...
@@ -48,6 +49,9 @@
48 49
 #include <sys/stat.h>
49 50
 #include <sys/time.h>
50 51
 #include <pthread.h>
52
+#if !defined(__linux__) && !defined(ANDROID)
53
+#include "win32_pipe.h"
54
+#endif
51 55
 
52 56
 #include "raylib.h"
53 57
 #include "roboto_regular.c"
... ...
@@ -328,6 +332,8 @@ int bg_freeunmarked(bg_t *bg);
328 332
 void *bg_thread(void *);
329 333
 
330 334
 static int mypipe(int fds[2]);
335
+static int mypiperead(int fd, char *buf, int count);
336
+static int mypipewrite(int fd, char *buf, int count);
331 337
 
332 338
 int
333 339
 main(int argc, char *argv[])
... ...
@@ -345,6 +351,9 @@ main(int argc, char *argv[])
345 351
         int is_scrolling;
346 352
         int leftscrollposstart;
347 353
         char *sel_menu,*sel_submenu;
354
+#if !defined(__linux__) && !defined(ANDROID)
355
+        win32pipe_init();
356
+#endif
348 357
         if((im=im_init("Fichero\nAjustes\nSalir\n\nEditar\nNuevo directorio\n\nAyuda\nInformación sobre el programa\n\n",ROOTDIR))==NULL) {
349 358
                 return(1);
350 359
         }
... ...
@@ -470,6 +479,9 @@ fprintf(stderr,"SELECTED: \"%s\"->\"%s\"\n",sel_menu,sel_submenu);
470 479
                 EndDrawing();
471 480
         }
472 481
         im_free(im),im=NULL;
482
+#if !defined(__linux__) && !defined(ANDROID)
483
+        win32pipe_fini();
484
+#endif
473 485
         return(0);
474 486
 }
475 487
 
... ...
@@ -656,6 +668,10 @@ im_menubar_mouse(menubar_t *menubar, Vector2 mousepos, int lmbpressed, int lmbre
656 668
         *sel_menu=NULL;
657 669
         *sel_submenu=NULL;
658 670
         flag_outsideall=1;
671
+#if 1
672
+if(lmbpressed || lmbdown)
673
+ fprintf(stderr,"in_menubar_mouse: lmbpressed:%i lmbdown:%i\n",lmbpressed,lmbdown);
674
+#endif
659 675
         for(i=0;i<menubar->sizemenudata;i++) {
660 676
                 int insidetitle,currentoption;
661 677
                 insidetitle=is_imutil_insidexywh(mousepos,&(menubar->menudata[i]->xywh),0);
... ...
@@ -1877,8 +1893,17 @@ bg_free(bg_t *bg)
1877 1893
                 return; /* nothing to do */
1878 1894
         if(bg->flag_threadstarted) {
1879 1895
                 char dummy=1;
1880
-                write(bg->pipe[WR],&dummy,1);
1896
+#if 1
1897
+fprintf(stderr,"bg_free: notifying thread to exit\n");
1898
+#endif
1899
+                mypipewrite(bg->pipe[WR],&dummy,1);
1900
+#if 1
1901
+fprintf(stderr,"bg_free: joining thread\n");
1902
+#endif
1881 1903
                 pthread_join(bg->thread,NULL);
1904
+#if 1
1905
+fprintf(stderr,"bg_free: thread joined OK\n");
1906
+#endif
1882 1907
                 bg->flag_threadstarted=0;
1883 1908
         }
1884 1909
         if(bg->pipe[0]!=-1)
... ...
@@ -1934,7 +1959,7 @@ bg_add(bg_t *bg, char *path)
1934 1959
 {
1935 1960
         int i;
1936 1961
         bgload_t *bgload;
1937
-        int dummy;
1962
+        char dummy;
1938 1963
         if(bg==NULL)
1939 1964
                 return(-1);
1940 1965
         for(i=0,bgload=bg->bgload;i<bg->sizebgload;i++,bgload++) {
... ...
@@ -1942,7 +1967,7 @@ bg_add(bg_t *bg, char *path)
1942 1967
                         bgload->is_todo=1;
1943 1968
                         bgload->has_mark=1;
1944 1969
                         dummy=0;
1945
-                        write(bg->pipe[WR],&dummy,1);
1970
+                        mypipewrite(bg->pipe[WR],&dummy,1);
1946 1971
                         return(0); /* already on list */
1947 1972
                 }
1948 1973
         }
... ...
@@ -1955,7 +1980,7 @@ bg_add(bg_t *bg, char *path)
1955 1980
                         bgload->has_mark=1;
1956 1981
                         dummy=0;
1957 1982
                         bgload->lended_to_thread=1;
1958
-                        write(bg->pipe[WR],&dummy,1);
1983
+                        mypipewrite(bg->pipe[WR],&dummy,1);
1959 1984
                         return(0); /* added to list */
1960 1985
                 }
1961 1986
         }
... ...
@@ -1998,7 +2023,10 @@ bg_thread(void *parambg)
1998 2023
         int i;
1999 2024
         bgload_t *bgload;
2000 2025
         while(1) {
2001
-                read(bg->pipe[RD],&dummy,1);
2026
+                mypiperead(bg->pipe[RD],&dummy,1);
2027
+#if 1
2028
+fprintf(stderr,"Thread received byte: %i\n",(int)((unsigned char)dummy));
2029
+#endif
2002 2030
                 if(dummy!=0)
2003 2031
                         break; /* was told to exit */
2004 2032
                 for(i=0,bgload=bg->bgload;i<bg->sizebgload;i++,bgload++) {
... ...
@@ -2019,20 +2047,45 @@ bg_thread(void *parambg)
2019 2047
                 }
2020 2048
         }
2021 2049
         pthread_exit(NULL);
2050
+        return(NULL);
2022 2051
 }
2023 2052
 
2024 2053
 #if !defined(__linux__) && !defined(ANDROID)
2025
-#include "win32_pipe.h"
2026 2054
 static int
2027 2055
 mypipe(int fds[2])
2028 2056
 {
2029
-        return(win32_pipe(fds));
2057
+        return(win32pipe_pipe(fds));
2058
+}
2059
+
2060
+static int
2061
+mypiperead(int fd, char *buf, int count)
2062
+{
2063
+        return(win32pipe_read(fd,buf,count));
2064
+}
2065
+
2066
+static int
2067
+mypipewrite(int fd, char *buf, int count)
2068
+{
2069
+        return(win32pipe_write(fd,buf,count));
2030 2070
 }
2031 2071
 #else
2032 2072
 static int
2033
-mypipe(char fds[2])
2073
+mypipe(int fds[2])
2034 2074
 {
2035 2075
         return(pipe(fds));
2036 2076
 }
2077
+
2078
+static int
2079
+mypiperead(int fd, char *buf, int count)
2080
+{
2081
+        return(read(fd,buf,count));
2082
+}
2083
+
2084
+static int
2085
+mypipewrite(int fd, char *buf, int count)
2086
+{
2087
+        return(write(fd,buf,count));
2088
+}
2037 2089
 #endif
2038 2090
 
2091
+
... ...
@@ -1,4 +1,20 @@
1
+#if !defined(__linux__) && !defined(ANDROID)
1 2
 #include <winsock2.h>
3
+typedef SOCKET socket_t; /* https://stackoverflow.com/questions/10817252/why-is-invalid-socket-defined-as-0-in-winsock2-h-c */
4
+#ifndef SHUT_WR
5
+#define SHUT_WR SD_SEND
6
+#endif
7
+#ifndef SHUT_RD
8
+#define SHUT_RD SD_RECEIVE
9
+#endif
10
+#define close(s) closesocket(s)
11
+#else
12
+#include <sys/socket.h>
13
+#include <netinet/in.h>
14
+#include <fcntl.h>
15
+typedef int socket_t;
16
+#define INVALID_SOCKET -1
17
+#endif
2 18
 
3 19
 #ifndef FILLIPV4ADDR
4 20
 #define FILLIPV4ADDR(a,family,addr,port) \
... ...
@@ -8,37 +24,66 @@
8 24
                 s.sin_port=htons(port)
9 25
 #endif
10 26
 
11
-#ifndef SHUT_WR
12
-#define SHUT_WR SD_SEND
27
+
28
+#if 1
29
+#include <stdio.h>
13 30
 #endif
14 31
 
15
-#ifndef SHUT_RD
16
-#define SHUT_RD SD_RECEIVE
32
+static int *
33
+win32pipe_initvalue(void)
34
+{
35
+        static int init=0;
36
+        return(&init);
37
+}
38
+
39
+int
40
+win32pipe_init(void)
41
+{
42
+        int *init;
43
+        init=win32pipe_initvalue();
44
+        if(*init!=0)
45
+                return(0);
46
+        *init=1;
47
+#if !defined(__linux__) && !defined(ANDROID)
48
+        {
49
+                WSADATA wsaData;
50
+                return(WSAStartup(0x202,&wsaData));
51
+        }
52
+#else
53
+        return(0);
17 54
 #endif
55
+}
18 56
 
19
-#define close(s) closesocket(s)
57
+void
58
+win32pipe_fini(void)
59
+{
60
+        int *init;
61
+        init=win32pipe_initvalue();
62
+        if(*init==0)
63
+                return;
64
+#if !defined(__linux__) && !defined(ANDROID)
65
+        WSACleanup();
66
+#endif
67
+        *init=0;
68
+}
20 69
 
21 70
 int
22
-win32_pipe(int fds[2])
71
+win32pipe_pipe(int fds[2])
23 72
 {
24
-        int serverfd,workfd,clientfd;
25
-        struct sockaddr_in c,s;
26
-        int sizes,sizec;
73
+        socket_t serverfd,workfd,clientfd;
74
+        struct sockaddr_in s;
75
+        int sizes;
76
+        win32pipe_init();
27 77
         FILLIPV4ADDR(s,AF_INET,htonl(INADDR_ANY),0);
28
-        serverfd=workfd=clientfd=-1;
29
-        if((serverfd=socket(AF_INET,SOCK_STREAM,0))==-1
78
+        serverfd=workfd=clientfd=INVALID_SOCKET;
79
+        if((serverfd=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET
30 80
           || bind(serverfd,(struct sockaddr *)&s,sizeof(s))!=0
31
-          || listen(serverfd,1)==-1
81
+          || listen(serverfd,4)==-1
32 82
           || (sizes=sizeof(s))!=sizeof(s)
33 83
           || getsockname(serverfd,(struct sockaddr *)&s,&sizes)==-1
34
-          || memcpy(&c,&s,sizeof(c))==NULL
35
-          || (clientfd=socket(AF_INET,SOCK_STREAM,0))==-1
36
-          || connect(clientfd,(struct sockaddr *)&c,sizeof(c))==-1
37
-          || (sizes=sizeof(s))!=sizeof(s)
84
+          || (clientfd=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET
85
+          || connect(clientfd,(struct sockaddr *)&s,sizeof(s))==-1
38 86
           || (workfd=accept(serverfd,(struct sockaddr *)&s,&sizes))==-1
39
-          || (sizec=sizeof(c))!=sizeof(c)
40
-          || getsockname(workfd,(struct sockaddr *)&c,&sizec)==-1
41
-          || memcmp(&c,&s,sizeof(c))!=0
42 87
         ) {
43 88
                 if(serverfd!=-1)
44 89
                         close(serverfd),serverfd=-1;
... ...
@@ -46,7 +91,14 @@ win32_pipe(int fds[2])
46 91
                         close(clientfd),clientfd=-1;
47 92
                 if(workfd!=-1)
48 93
                         close(workfd),workfd=-1;
94
+#if 1
95
+fprintf(stderr,"win32_pipe error creating socket\n");
96
+#endif
97
+                return(-1);
49 98
         }
99
+#if 1
100
+fprintf(stderr,"win32_pipe socket generated\n");
101
+#endif
50 102
         close(serverfd),serverfd=-1;
51 103
         shutdown(workfd,SHUT_WR);
52 104
         shutdown(clientfd,SHUT_RD);
... ...
@@ -55,3 +107,16 @@ win32_pipe(int fds[2])
55 107
         return(0);
56 108
 }
57 109
 
110
+int
111
+win32pipe_read(int fd, char *buf, int count)
112
+{
113
+        return(recv(fd,buf,count,0));
114
+}
115
+
116
+int
117
+win32pipe_write(int fd, char *buf, int count)
118
+{
119
+        return(send(fd,buf,count,0));
120
+}
121
+
122
+
... ...
@@ -1,2 +1,7 @@
1
+int win32pipe_init(void);
2
+void win32pipe_fini(void);
3
+int win32pipe_pipe(int fds[2]);
4
+int win32pipe_read(int fd, char *buf, int count);
5
+int win32pipe_write(int fd, char *buf, int count);
6
+
1 7
 
2
-int win32_pipe(int fds[2]);