rework keyframe hide popup, keyframe auto render, textbox set_selection wide text
[goodguy/history.git] / cinelerra-5.1 / guicast / bcsignals.h
1 /*
2  * CINELERRA
3  * Copyright (C) 1997-2014 Adam Williams <broadcast at earthling dot net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  * 
19  */
20
21
22 // debugging functions go here
23
24 #ifndef BCSIGNALS_H
25 #define BCSIGNALS_H
26
27 #include "arraylist.h"
28 #include "bcsignals.inc"
29 #include <stdio.h>
30 #include <pthread.h>
31 #include <signal.h>
32 #include <X11/Xlib.h>
33
34 #define TRON(x) BC_Signals::new_function(x);
35 #define TROFF(x) BC_Signals::delete_function(x);
36
37 // BC_Signals must be initialized at the start of every program using
38 // debugging.
39 //#define ENABLE_TRACE
40 #define TRACE_LOCKS
41 //#ifdef TRACE_LOCKS
42 //#undef TRACE_LOCKS
43 //#endif
44 //#define TRACE_MEMORY
45
46
47 // Need to use structs to avoid the memory manager.
48 // One of these tables is created every time someone locks a lock.
49 // After successfully locking, the table is flagged as being the owner of the lock.
50 // In the unlock function, the table flagged as the owner of the lock is deleted.
51 typedef struct 
52 {
53         void *ptr;
54         const char *title;
55         const char *location;
56         int is_owner;
57         int id;
58         pthread_t tid;
59 } bc_locktrace_t;
60
61 class BC_Signals
62 {
63         int (*old_err_handler)(Display *, XErrorEvent *);
64         static int x_error_handler(Display *display, XErrorEvent *event);
65 public:
66         BC_Signals();
67         void initialize();
68         void initialize2();
69         void terminate();
70
71
72         virtual void signal_handler(int signum);
73
74         static void dump_stack(FILE *fp=stdout);
75
76 #ifdef ENABLE_TRACE
77 // Add a trace
78 #define TRACE(text) BC_Signals::new_trace(text);
79 #define SET_TRACE BC_Signals::new_trace(__FILE__, __FUNCTION__, __LINE__);
80 #define PRINT_TRACE { printf("%s: %d\n", __FILE__, __LINE__); fflush(stdout); }
81 // Delete all traces
82 #define UNTRACE BC_Signals::delete_traces();
83
84 #else
85
86 #define TRACE(text) ;
87 #define UNTRACE ;
88 #define PRINT_TRACE { printf("%s: %d\n", __FILE__, __LINE__); fflush(stdout); }
89 //#define PRINT_TRACE ;
90 #define SET_TRACE ;
91
92 #endif
93
94
95 #ifdef TRACE_LOCKS
96
97 // Before user acquires
98 #define SET_LOCK(ptr, title, location) int table_id = BC_Signals::set_lock(ptr, title, location);
99 // After successful acquisition of a mutex, the table is flagged
100 #define SET_LOCK2 BC_Signals::set_lock2(table_id);
101 // After successful acquisition of a condition, the table is removed because
102 // the user never unlocks a condition after locking it.
103 // Release current lock table after failing to acquire
104 #define UNSET_LOCK2 BC_Signals::unset_lock2(table_id);
105
106 // Release current owner of lock
107 #define UNSET_LOCK(ptr) BC_Signals::unset_lock(ptr);
108
109 // Delete a lock
110 #define UNSET_ALL_LOCKS(ptr) BC_Signals::unset_all_locks(ptr);
111
112 #define LOCK_LOCKS(s) BC_Signals::lock_locks(s);
113 #define UNLOCK_LOCKS BC_Signals::unlock_locks();
114 #define CLEAR_LOCKS_TID(tid) BC_Signals::clear_locks_tid(tid);
115
116 #else
117
118 #define SET_LOCK(ptr, title, location) ;
119 #define SET_LOCK2 ;
120 #define SET_LOCK2_CONDITION ;
121 #define UNSET_LOCK(ptr) ;
122 #define UNSET_LOCK2 ;
123 #define UNSET_ALL_LOCKS(ptr) ;
124
125 #define LOCK_LOCKS(s) ;
126 #define UNLOCK_LOCKS ;
127 #define CLEAR_LOCKS_TID(tid) ;
128 #endif
129
130
131 #ifdef TRACE_MEMORY
132
133 #define ENABLE_BUFFER BC_Signals::enable_memory();
134 #define DISABLE_BUFFER BC_Signals::disable_memory();
135 // Note the size, pointer, and location of an allocation
136 #define BUFFER(size, ptr, location) BC_Signals::set_buffer(size, ptr, location);
137 // Note the pointer and location of an allocation
138 #define BUFFER2(ptr, location) BC_Signals::set_buffer(0, ptr, location);
139 // Remove a pointer from the allocation table
140 #define UNBUFFER(ptr) BC_Signals::unset_buffer(ptr);
141
142 #else
143
144 #define ENABLE_BUFFER ;
145 #define DISABLE_BUFFER ;
146 #define BUFFER(size, ptr, location);
147 #define UNBUFFER(ptr);
148
149 #endif
150
151 // Handling of temporary files in crash
152 #define SET_TEMP BC_Signals::set_temp
153 #define UNSET_TEMP BC_Signals::unset_temp
154
155 // Forks need to reset the lock status in case they forked when a lock was held.
156         static void reset_locks();
157
158 // Temporary files
159         static void delete_temps();
160         static void set_temp(char *string);
161         static void unset_temp(char *string);
162         static void signal_dump(int signum);
163
164
165         static void kill_subs();
166
167         static int set_lock(void *ptr, const char *title, const char *location);
168         static void set_lock2(int table_id);
169         static void set_lock2_condition(int table_id);
170         static void unset_lock2(int table_id);
171         static void unset_lock(void *ptr);
172 // Used in lock destructors so takes away all references
173         static void unset_all_locks(void *ptr);
174         static void clear_locks_tid(pthread_t tid);
175
176         static void new_trace(const char *text);
177         static void new_trace(const char *file, const char *function, int line);
178         static void delete_traces();
179
180         static void enable_memory();
181         static void disable_memory();
182         static void set_buffer(int size, void *ptr, const char* location);
183 // This one returns 1 if the buffer wasn't found.
184         static int unset_buffer(void *ptr);
185         static void lock_locks(const char *s);
186         static void unlock_locks();
187
188         static void dump_traces(FILE *fp=stdout);
189         static void dump_locks(FILE *fp=stdout);
190         static void dump_buffers(FILE *fp=stdout);
191         static void set_sighup_exit(int enable);
192
193         static void set_trap_path(const char *path);
194         static void set_trap_hook(void (*hook)(FILE *fp, void *data), void *data);
195         static void set_catch_segv(bool v);
196         static void set_catch_intr(bool v);
197
198 // Convert signum to text
199         static const char* sig_to_str(int number);
200
201         static BC_Signals *global_signals;
202         static const char *trap_path;
203         static void *trap_data;
204         static void (*trap_hook)(FILE *fp, void *vp);
205         static bool trap_sigsegv, trap_sigintr;
206 };
207
208
209 #endif