1 /* 2 * ECOS - Embedded Conic Solver. 3 * Copyright (C) 2012-2015 A. Domahidi [domahidi@embotech.com], 4 * Automatic Control Lab, ETH Zurich & embotech GmbH, Zurich, Switzerland. 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 21 22 /* 23 * Implements signal handling (ctrl-c) for ECOS. 24 * 25 * Under Windows, we use SetConsoleCtrlHandler. 26 * Under Unix systems, we use sigaction. 27 * For Mex files, we use utSetInterruptEnabled/utIsInterruptPending. 28 * 29 * This module is (c) Michael Grant, [mcg@cvxr.com] contributed by Github PR #82 30 */ 31 32 #include "ctrlc.h" 33 34 #if CTRLC > 0 35 36 #if defined MATLAB_MEX_FILE 37 38 /* No header file available here; define the prototypes ourselves */ 39 extern bool utIsInterruptPending(void); 40 extern bool utSetInterruptEnabled(bool); 41 42 static int istate; 43 void init_ctrlc(void) 44 { 45 istate = (int)utSetInterruptEnabled(true); 46 } 47 void remove_ctrlc(void) 48 { 49 utSetInterruptEnabled((bool)istate); 50 } 51 int check_ctrlc(void) 52 { 53 return (int)utIsInterruptPending(); 54 } 55 56 #elif defined _WIN32 || defined _WIN64 57 58 /* Use Windows SetConsoleCtrlHandler for signal handling */ 59 #include <windows.h> 60 61 static int int_detected; 62 BOOL WINAPI handle_ctrlc(DWORD dwCtrlType) 63 { 64 if (dwCtrlType != CTRL_C_EVENT) return FALSE; 65 int_detected = 1; 66 return TRUE; 67 } 68 void init_ctrlc(void) 69 { 70 int_detected = 0; 71 SetConsoleCtrlHandler( handle_ctrlc, TRUE ); 72 } 73 void remove_ctrlc(void) 74 { 75 SetConsoleCtrlHandler( handle_ctrlc, FALSE ); 76 } 77 int check_ctrlc(void) 78 { 79 return int_detected; 80 } 81 82 #else /* Unix */ 83 84 /* Use POSIX clocl_gettime() for timing on non-Windows machines */ 85 86 #include <signal.h> 87 static int int_detected; 88 struct sigaction oact; 89 void handle_ctrlc(int dummy) 90 { 91 int_detected = dummy?dummy:-1; 92 } 93 void init_ctrlc(void) 94 { 95 int_detected = 0; 96 struct sigaction act; 97 act.sa_flags = 0; 98 sigemptyset(&act.sa_mask); 99 act.sa_handler = handle_ctrlc; 100 sigaction(SIGINT,&act,&oact); 101 } 102 void remove_ctrlc(void) 103 { 104 struct sigaction act; 105 sigaction(SIGINT,&oact,&act); 106 } 107 int check_ctrlc(void) 108 { 109 return int_detected; 110 } 111 112 #endif /* END IF MATLAB_MEX_FILE / WIN32 */ 113 114 #endif /* END IF CTRLC > 0 */ 115 116