Debian Codegraph @

src/win32_support.c

raw
1 #include "config.h"
2 #include "mpg123app.h"
3 #include "common/debug.h"
4
5 #ifdef WANT_WIN32_UNICODE
6
7 /* Obscure and undocumented call from MS C Runtime "MSVCRT.DLL" */
8 typedef struct
9 {
10 int newmode;
11 } _startupinfo;
12
13 /* XP and later has an int return though */
14 void __cdecl __declspec(dllimport) __wgetmainargs (
15 int *_Argc,
16 wchar_t ***_Argv,
17 wchar_t ***_Env,
18 int _DoWildCard,
19 _startupinfo * _StartInfo
20 );
21
22 int win32_cmdline_utf8(int * argc, char *** argv)
23 {
24 int argcounter;
25 int nargc;
26 wchar_t **argv_wide;
27 wchar_t **env;
28 char *argvptr;
29 _startupinfo startup;
30
31 /* That's too lame. */
32 if(argv == NULL || argc == NULL) return -1;
33
34 startup.newmode = 0;
35 nargc = -1;
36 argv_wide = NULL;
37 env = NULL;
38 __wgetmainargs(&nargc, &argv_wide,&env,1, &startup);
39 if (nargc == -1 || argv_wide == NULL || env == NULL) {
40 error("Cannot allocate memory for wide command line.");
41 return -1;
42 }
43 *argc = nargc;
44 *argv = (char **)calloc(sizeof (char *), *argc);
45 if(*argv == NULL)
46 {
47 error("Cannot allocate memory for UTF-8 command line.");
48 return -1;
49 }
50
51 for(argcounter = 0; argcounter < *argc; argcounter++)
52 {
53 INT123_win32_wide_utf8(argv_wide[argcounter], &argvptr, NULL);
54 (*argv)[argcounter] = argvptr;
55 }
56 return 0;
57 }
58
59 void win32_cmdline_free(int argc, char **argv)
60 {
61 int i;
62 if(argv == NULL) return;
63 for(i=0; i<argc; ++i) free(argv[i]);
64 }
65 #endif /* WANT_WIN32_UNICODE */
66
67 #ifdef _WIN32
68 void win32_set_priority (const int arg)
69 {
70 DWORD proc_result = 0;
71 HANDLE current_proc = NULL;
72 if(arg)
73 {
74 if((current_proc = GetCurrentProcess()))
75 {
76 switch(arg)
77 {
78 case -2: proc_result = SetPriorityClass(current_proc, IDLE_PRIORITY_CLASS); break;
79 case -1: proc_result = SetPriorityClass(current_proc, BELOW_NORMAL_PRIORITY_CLASS); break;
80 case 0: proc_result = SetPriorityClass(current_proc, NORMAL_PRIORITY_CLASS); break; /*default priority*/
81 case 1: proc_result = SetPriorityClass(current_proc, ABOVE_NORMAL_PRIORITY_CLASS); break;
82 case 2: proc_result = SetPriorityClass(current_proc, HIGH_PRIORITY_CLASS); break;
83 case 3: proc_result = SetPriorityClass(current_proc, REALTIME_PRIORITY_CLASS); break;
84 default: fprintf(stderr,"Unknown priority class specified\n");
85 }
86 if(!proc_result) fprintf(stderr,"SetPriorityClass failed\n");
87 }
88 else fprintf(stderr,"GetCurrentProcess failed\n");
89 }
90 }
91 #endif
92
93 #ifdef WANT_WIN32_FIFO
94 static HANDLE fifohandle;
95 static OVERLAPPED ov1;
96
97 VOID CALLBACK ReadComplete(
98 DWORD dwErrorCode,
99 DWORD dwNumberOfBytesTransfered,
100 LPOVERLAPPED lpOverlapped
101 )
102 {
103 /* Reset OVERLAPPED structure */
104 debug("ReadFileEx completed");
105 memset(lpOverlapped,0,sizeof(OVERLAPPED));
106 return;
107 }
108
109 mpg123_ssize_t win32_fifo_read(void *buf, size_t nbyte)
110 {
111 int check;
112 DWORD readbuff;
113 DWORD available;
114 debug1("Reading pipe handle %p", fifohandle);
115 if (!fifohandle) return 0;
116
117 available = win32_fifo_read_peek(NULL);
118 if (!available) return 0;
119
120 /* This looks more like a hack than a proper check */
121 readbuff = (nbyte > available) ? available : nbyte;
122 debug("Starting ReadFileEx");
123 check = ReadFileEx(fifohandle,buf,readbuff,&ov1,&ReadComplete);
124 WaitForSingleObjectEx(fifohandle,INFINITE,TRUE);
125 debug1("Read %ld bytes from pipe", readbuff);
126 return (!check) ? 0 : readbuff;
127 }
128
129 /* function should be able to tell if bytes are
130 available and return immediately on overlapped
131 asynchrounous pipes, like unix select() */
132 DWORD win32_fifo_read_peek(struct timeval *tv)
133 {
134 DWORD ret = 0;
135 DWORD err, timer;
136
137 timer = (tv) ? tv -> tv_sec * 1000 : INFINITE;
138
139 debug1("Peeking on pipe handle %p", fifohandle);
140
141 SetLastError(0);
142 if(!fifohandle) return 0;
143 PeekNamedPipe(fifohandle, NULL, 0, NULL, &ret, NULL);
144 err = GetLastError();
145 debug1("Waiting %ld msec for pipe to be ready", timer);
146 debug1("GetLastError was %ld", err);
147 if(err == ERROR_BROKEN_PIPE)
148 {
149 debug("Broken pipe, disconnecting");
150 DisconnectNamedPipe(fifohandle);
151 ConnectNamedPipe(fifohandle,&ov1);
152 WaitForSingleObjectEx(fifohandle,timer,TRUE);
153 }
154 else if(err == ERROR_BAD_PIPE)
155 {
156 debug("Bad pipe, Waiting for connect");
157 DisconnectNamedPipe(fifohandle);
158 ConnectNamedPipe(fifohandle,&ov1);
159 WaitForSingleObjectEx(fifohandle,timer,TRUE);
160 }
161 debug2("peek %ld bytes, error %ld",ret, err);
162 return ret;
163 }
164
165 void win32_fifo_close(void)
166 {
167 debug1("Attempting to close handle %p", fifohandle);
168 if(fifohandle)
169 {
170 DisconnectNamedPipe(fifohandle);
171 CloseHandle(fifohandle);
172 }
173 fifohandle = NULL;
174 }
175
176 int win32_fifo_mkfifo(const char *path)
177 {
178 HANDLE ret;
179 win32_fifo_close();
180 #ifdef WANT_WIN32_UNICODE
181 wchar_t *str;
182 if(INT123_win32_utf8_wide(path,&str,NULL) == 0)
183 {
184 fprintf(stderr,"Cannot get FIFO name, likely out of memory\n");
185 return -1;
186 }
187 #if (DEBUG == 1)
188 fwprintf(stderr,L"CreateNamedPipeW %ws\n", str);
189 #endif
190 ret = CreateNamedPipeW(str,PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,PIPE_TYPE_BYTE,1,255,255,0,NULL);
191 free(str);
192 #else
193 #if (DEBUG == 1)
194 fprintf(stderr,"CreateNamedPipeA %s\n", path);
195 #endif
196 ret = CreateNamedPipeA(path,PIPE_ACCESS_DUPLEX|FILE_FLAG_OVERLAPPED,PIPE_TYPE_BYTE,1,255,255,0,NULL);
197 #endif /* WANT_WIN32_UNICODE */
198 if(ret == INVALID_HANDLE_VALUE) return -1;
199
200 fifohandle = ret;
201 /* Wait for client */
202 ConnectNamedPipe(fifohandle,&ov1);
203 WaitForSingleObjectEx(fifohandle,INFINITE,TRUE);
204 return 0;
205 }
206 #endif /* WANT_WIN32_FIFO */