1 // Written by William Schelter.
2 // Additional work from Davind Billinghurst and Andrej Vopodivec.
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 // SPDX-License-Identifier: GPL-2.0+
22 // winkill_lib is a library that offers a shared memory location a GUI can send
23 // an interrupt signal to on MS Windows.
32 #define signal_mask(n) (1 << (n))
39 } sharedMemory
= {0,0,0x10000} ;
43 typedef struct {int signumber
; char *name
;} sigNameStruct
;
44 sigNameStruct sigNames
[]=
47 { SIGHUP
, "HUP" }, /* Hangup (POSIX). */
50 { SIGINT
, "INT" }, /* Interrupt (ANSI). */
53 { SIGQUIT
, "QUIT" }, /* Quit (POSIX). */
56 { SIGILL
, "ILL" }, /* Illegal instruction (ANSI). */
59 { SIGTRAP
, "TRAP" }, /* Trace trap (POSIX). */
62 { SIGABRT
, "ABRT" }, /* Abort (ANSI). */
65 { SIGIOT
, "IOT" }, /* IOT trap (4.2 BSD). */
68 { SIGBUS
, "BUS" }, /* BUS error (4.2 BSD). */
71 { SIGFPE
, "FPE" }, /* Floating-point exception (ANSI). */
74 { SIGKILL
, "KILL" }, /* Kill, unblockable (POSIX). */
77 { SIGUSR1
, "USR1" }, /* User-defined signal 1 (POSIX). */
80 { SIGSEGV
, "SEGV" }, /* Segmentation violation (ANSI). */
83 { SIGUSR2
, "USR2" }, /* User-defined signal 2 (POSIX). */
86 { SIGPIPE
, "PIPE" }, /* Broken pipe (POSIX). */
89 { SIGALRM
, "ALRM" }, /* Alarm clock (POSIX). */
92 { SIGTERM
, "TERM" }, /* Termination (ANSI). */
95 { SIGSTKFLT
, "STKFLT" }, /* Stack fault. */
98 { SIGCLD
, "CLD" }, /* Same as SIGCHLD (System V). */
101 { SIGCHLD
, "CHLD" }, /* Child status has changed (POSIX). */
104 { SIGCONT
, "CONT" }, /* Continue (POSIX). */
107 { SIGSTOP
, "STOP" }, /* Stop, unblockable (POSIX). */
110 { SIGTSTP
, "TSTP" }, /* Keyboard stop (POSIX). */
113 { SIGTTIN
, "TTIN" }, /* Background read from tty (POSIX). */
116 { SIGTTOU
, "TTOU" }, /* Background write to tty (POSIX). */
119 { SIGURG
, "URG" }, /* Urgent condition on socket (4.2 BSD). */
122 { SIGXCPU
, "XCPU" }, /* CPU limit exceeded (4.2 BSD). */
125 { SIGXFSZ
, "XFSZ" }, /* File size limit exceeded (4.2 BSD). */
128 { SIGVTALRM
, "VTALRM" }, /* Virtual alarm clock (4.2 BSD). */
131 { SIGPROF
, "PROF" }, /* Profiling alarm clock (4.2 BSD). */
134 { SIGWINCH
, "WINCH" }, /* Window size change (4.3 BSD, Sun). */
137 { SIGPOLL
, "POLL" }, /* Pollable event occurred (System V). */
140 { SIGIO
, "IO" }, /* I/O now possible (4.2 BSD). */
143 { SIGPWR
, "PWR" }, /* Power failure restart (System V). */
151 void close_shared_memory()
153 if (sharedMemory
.handle
)
154 CloseHandle(sharedMemory
.handle
);
155 sharedMemory
.handle
= NULL
;
156 if (sharedMemory
.address
)
157 UnmapViewOfFile(sharedMemory
.address
);
158 sharedMemory
.address
= NULL
;
161 int is_shared_memory_initialised
= FALSE
;
163 int check_shared_memory()
165 return is_shared_memory_initialised
;
168 void print_shared_memory_name()
170 if (is_shared_memory_initialised
)
171 puts(sharedMemory
.name
);
174 void init_shared_memory (void)
176 if ( ! is_shared_memory_initialised
) {
177 sprintf ( sharedMemory
.name
, "maxima-%d", getpid());
179 is_shared_memory_initialised
= TRUE
;
181 sharedMemory
.handle
=
182 CreateFileMapping ( (HANDLE
)-1,
187 TEXT (sharedMemory
.name
) );
189 if ( sharedMemory
.handle
== NULL
) {
190 is_shared_memory_initialised
= FALSE
;
193 sharedMemory
.address
=
194 MapViewOfFile(sharedMemory
.handle
, /* Handle to mapping object. */
195 FILE_MAP_WRITE
, /* Read/write permission */
196 0, /* Max. object size. */
197 0, /* Size of hFile. */
198 0); /* Map entire file. */
200 if ( sharedMemory
.address
== NULL
) {
201 is_shared_memory_initialised
= FALSE
;
204 atexit ( close_shared_memory
);
208 int read_shared_memory(void)
212 if (! is_shared_memory_initialised
)
215 at
= (int *)(sharedMemory
.address
);
217 if (*at
& signal_mask(SIGINT
))
219 if (*at
& signal_mask(SIGTERM
))
224 int read_sm_sigterm(void)
228 if (! is_shared_memory_initialised
)
231 at
= (int *)(sharedMemory
.address
);
233 if (*at
& signal_mask(SIGTERM
))
239 int read_sm_sigint(void)
243 if (! is_shared_memory_initialised
)
246 at
= (int *)(sharedMemory
.address
);
248 if (*at
& signal_mask(SIGINT
))
254 void reset_shared_memory(void)
258 if (! is_shared_memory_initialised
)
261 at
= (int *)(sharedMemory
.address
);
266 void reset_sm_sigint(void)
270 if (! is_shared_memory_initialised
)
273 at
= (int *)(sharedMemory
.address
);
275 *at
&= ~signal_mask(SIGINT
);
278 void reset_sm_sigterm(void)
282 if (! is_shared_memory_initialised
)
285 at
= (int *)(sharedMemory
.address
);
287 *at
&= ~signal_mask(SIGTERM
);