Browse code

Fix windows pipe init, add debug messages for windows using messagebox

Dario Rodriguez authored on 25/07/2025 08:00:50
Showing 3 changed files
... ...
@@ -10,7 +10,7 @@ imgmover: imgmover.c roboto_regular.c
10 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
-	toolchain-zig/compile.sh win32_pipe.c -c -o win32_pipe-windows.o
13
+	toolchain-zig/compile.sh win32_pipe.c -c -o win32_pipe-windows.o -Wno-missing-declarations
14 14
 	toolchain-zig/link.sh -icon test-icon_256x256.png imgmover.exe imgmover-windows.o win32_pipe-windows.o toolchain-zig/windows-msys2-mingw/lib/libraylib.a toolchain-zig/windows-msys2-mingw/lib/libpthread.a toolchain-zig/windows-msys2-mingw/lib/w32api/libuser32.a
15 15
 
16 16
 roboto_regular.c: ttf2h.sh fonts/Roboto-Regular.ttf
... ...
@@ -141,6 +141,14 @@
141 141
 #define WR 1
142 142
 #endif
143 143
 
144
+#if !defined(__linux__) && !defined(ANDROID)
145
+int MessageBoxA(void *hWnd,void *lpText,void *lpCaption,unsigned int uType);
146
+#define messagebox(str) MessageBoxA(NULL,str,"imgmover",0);
147
+#else
148
+#define messagebox(str) fprintf(stderr,"%s\n",str);
149
+#endif
150
+
151
+
144 152
 
145 153
 #if !defined(__linux__) && !defined(ANDROID)
146 154
 /* the old raylib used in the windows build lacks this function */
... ...
@@ -365,9 +373,7 @@ main(int argc, char *argv[])
365 373
         win32pipe_init();
366 374
 #endif
367 375
         if((im=im_init("Fichero\nAjustes\nSalir\n\nEditar\nNuevo directorio\n\nAyuda\nInformación sobre el programa\n\n",ROOTDIR))==NULL) {
368
-#if !defined(__linux__) && !defined(ANDROID)
369
-                MessageBoxA(NULL,"Couldn't init interface","imgmover",0);
370
-#endif
376
+                messagebox("Couldn't init interface");
371 377
                 return(1);
372 378
         }
373 379
         flag_ignorelmb=0;
... ...
@@ -507,6 +513,7 @@ im_t *
507 513
 im_init(char *menus, char *rootdir)
508 514
 {
509 515
         im_t *im;
516
+        char *errstr;
510 517
         if(menus==NULL)
511 518
                 return(NULL); /* sanity check failed */
512 519
         if((im=calloc(1,sizeof(im_t)))==NULL) {
... ...
@@ -519,12 +526,16 @@ im_init(char *menus, char *rootdir)
519 526
         im->windowinit=1;
520 527
         SetTargetFPS(30);
521 528
         /* init fonts and contents */
522
-        if((im->font=im_font_init(FONTSIZE))==NULL
529
+        if((errstr="Couldn't init font")==NULL
530
+          || (im->font=im_font_init(FONTSIZE))==NULL
523 531
           || (im->fontbig=im_font_init(FONTBIGSIZE))==NULL
524 532
           || (im->fonthuge=im_font_init(FONTHUGESIZE))==NULL
533
+          || (errstr="Couldn't init menus")==NULL
525 534
           || (im->menubar=im_menubar_init(menus,im->font))==NULL
535
+          || (errstr="Couldn't init interface data")==NULL
526 536
           || (im->body=im_body_init(0,im->menubar->height, im->font, im->fontbig, im->fonthuge, LEFTSIZE, rootdir,im->w,im->h))==NULL
527 537
         ) {
538
+                messagebox(errstr);
528 539
                 im_free(im),im=NULL;
529 540
                 return(NULL); /* insuf. mem. */
530 541
         }
... ...
@@ -827,13 +838,20 @@ body_t *
827 838
 im_body_init(int x, int y, font_t *font, font_t *fontbig, font_t *fonthuge, int leftsize, char *rootdir, int windowwidth, int windowheight)
828 839
 {
829 840
         body_t *body;
841
+        char *errstr;
830 842
         static char sep[]={SEP};
831
-        if(font==NULL || fontbig==NULL || fonthuge==NULL || rootdir==NULL)
843
+        if(font==NULL || fontbig==NULL || fonthuge==NULL || rootdir==NULL) {
844
+                messagebox("im_body_init sanity check failed");
832 845
                 return(NULL); /* sanity check failed */
833
-        if((body=calloc(1,sizeof(body_t)))==NULL
846
+        }
847
+        if((errstr="Insuf. mem. for body")==NULL
848
+          || (body=calloc(1,sizeof(body_t)))==NULL
849
+          || (errstr="Insuf. mem. for rootdir str")==NULL
834 850
           || (body->rootdir=strdup(rootdir))==NULL
851
+          || (errstr="Error init bg")==NULL
835 852
           || (body->bg=bg_init(SIZEBGLOAD))==NULL
836 853
         ) {
854
+                messagebox(errstr);
837 855
                 im_body_free(body),body=NULL;
838 856
                 return(NULL);
839 857
         }
... ...
@@ -1962,16 +1980,23 @@ bg_t *
1962 1980
 bg_init(int sizebgload)
1963 1981
 {
1964 1982
         bg_t *bg;
1983
+        char *errstr;
1965 1984
         bg=NULL;
1966
-        if((bg=calloc(1,sizeof(bg_t)))==NULL
1985
+        if((errstr="Insuf. mem. for bg")==NULL
1986
+          || (bg=calloc(1,sizeof(bg_t)))==NULL
1987
+          || (errstr="Error init pipes (please check program is not blocked in firewall)")==NULL
1967 1988
           || (bg->pipe[0]=bg->pipe[1]=-1)!=-1
1968 1989
           || mypipe(bg->pipe)!=0
1990
+          || (errstr="Insuf mem bgload")==NULL
1969 1991
           || (bg->bgload=calloc(sizebgload,sizeof(bgload_t)))==NULL
1970 1992
           || (bg->sizebgload=sizebgload)!=sizebgload
1993
+          || (errstr="pthread attr init error")==NULL
1971 1994
           || pthread_attr_init(&(bg->tattr))!=0
1995
+          || (errstr="pthread create error")==NULL
1972 1996
           || pthread_create(&(bg->thread),&(bg->tattr),bg_thread,(void *)bg)!=0
1973 1997
           || (bg->flag_threadstarted=1)!=1
1974 1998
         ) {
1999
+                messagebox(errstr);
1975 2000
                 bg_free(bg);
1976 2001
                 return(NULL);
1977 2002
         }
... ...
@@ -8,6 +8,9 @@ typedef SOCKET socket_t; /* https://stackoverflow.com/questions/10817252/why-is-
8 8
 #define SHUT_RD SD_RECEIVE
9 9
 #endif
10 10
 #define close(s) closesocket(s)
11
+#ifndef messagebox
12
+#define messagebox(str) MessageBoxA(NULL,str,"win32_pipe",0);
13
+#endif
11 14
 #else
12 15
 #include <sys/socket.h>
13 16
 #include <netinet/in.h>
... ...
@@ -73,32 +76,57 @@ win32pipe_pipe(int fds[2])
73 76
         socket_t serverfd,workfd,clientfd;
74 77
         struct sockaddr_in s;
75 78
         int sizes;
79
+        unsigned char *ip;
80
+        char *errstr;
76 81
         win32pipe_init();
77 82
         FILLIPV4ADDR(s,AF_INET,htonl(INADDR_ANY),0);
83
+        ip=(unsigned char *) &(s.sin_addr.s_addr);
84
+        ip[0]=127,ip[1]=0,ip[2]=0,ip[3]=1;
78 85
         serverfd=workfd=clientfd=INVALID_SOCKET;
79
-        if((serverfd=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET
86
+        if((errstr="Couldn't create server socket")==NULL
87
+          || (serverfd=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET
88
+          || (errstr="Couldn't bind server socket")==NULL
80 89
           || bind(serverfd,(struct sockaddr *)&s,sizeof(s))!=0
90
+          || (errstr="Couldn't listen on server socket")==NULL
81 91
           || listen(serverfd,4)==-1
82 92
           || (sizes=sizeof(s))!=sizeof(s)
93
+          || (errstr="Couldn't get server socket name")==NULL
83 94
           || getsockname(serverfd,(struct sockaddr *)&s,&sizes)==-1
95
+          || (errstr="Couldn't create client socket")==NULL
84 96
           || (clientfd=socket(AF_INET,SOCK_STREAM,0))==INVALID_SOCKET
97
+          || (errstr="Couldn't connect client socket to server socket")==NULL
85 98
           || connect(clientfd,(struct sockaddr *)&s,sizeof(s))==-1
99
+          || (errstr="Couldn't accept client socket connection to server socket")==NULL
86 100
           || (workfd=accept(serverfd,(struct sockaddr *)&s,&sizes))==-1
87 101
         ) {
102
+                {
103
+                        char errbuf[4096];
104
+                        unsigned char *ip;
105
+                        int port;
106
+                        if(strcmp(errstr,"Couldn't connect client socket to server socket")==0) {
107
+                                ip=(unsigned char *) &(s.sin_addr.s_addr);
108
+                                port=ntohs(s.sin_port);
109
+                                snprintf(errbuf,sizeof(errbuf),"%s\nWinsock error: %ld\nRemote address:%i.%i.%i.%i\nRemote port:%i"
110
+                                  ,errstr
111
+                                  ,WSAGetLastError()
112
+                                  ,(int)(ip[0]),(int)(ip[1]),(int)(ip[2]),(int)(ip[3])
113
+                                  ,port
114
+                                );
115
+                        } else {
116
+                                snprintf(errbuf,sizeof(errbuf),"%s\nWinsock error: %ld",errstr,WSAGetLastError());
117
+                        }
118
+                        errbuf[sizeof(errbuf)-1]='\0';
119
+                        messagebox(errbuf);
120
+                }
121
+
88 122
                 if(serverfd!=-1)
89 123
                         close(serverfd),serverfd=-1;
90 124
                 if(clientfd!=-1)
91 125
                         close(clientfd),clientfd=-1;
92 126
                 if(workfd!=-1)
93 127
                         close(workfd),workfd=-1;
94
-#if 1
95
-fprintf(stderr,"win32_pipe error creating socket\n");
96
-#endif
97 128
                 return(-1);
98 129
         }
99
-#if 1
100
-fprintf(stderr,"win32_pipe socket generated\n");
101
-#endif
102 130
         close(serverfd),serverfd=-1;
103 131
         shutdown(workfd,SHUT_WR);
104 132
         shutdown(clientfd,SHUT_RD);