| 1 | // =========================================================== // |
|---|
| 2 | // // |
|---|
| 3 | // File : AW_debug.cxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Coded by Ralf Westram (coder@reallysoft.de) in May 2009 // |
|---|
| 7 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 8 | // http://www.arb-home.de/ // |
|---|
| 9 | // // |
|---|
| 10 | // =========================================================== // |
|---|
| 11 | |
|---|
| 12 | #include "aw_window.hxx" |
|---|
| 13 | #include "aw_Xm.hxx" |
|---|
| 14 | #include "aw_window_Xm.hxx" |
|---|
| 15 | #include "aw_root.hxx" |
|---|
| 16 | #include "aw_msg.hxx" |
|---|
| 17 | |
|---|
| 18 | #include <arbdbt.h> |
|---|
| 19 | |
|---|
| 20 | #include <vector> |
|---|
| 21 | #include <iterator> |
|---|
| 22 | #include <string> |
|---|
| 23 | #include <algorithm> |
|---|
| 24 | |
|---|
| 25 | // do includes above (otherwise depends depend on DEBUG) |
|---|
| 26 | #if defined(DEBUG) |
|---|
| 27 | // -------------------------------------------------------------------------------- |
|---|
| 28 | |
|---|
| 29 | using namespace std; |
|---|
| 30 | |
|---|
| 31 | typedef vector<string> CallbackArray; |
|---|
| 32 | typedef CallbackArray::const_iterator CallbackIter; |
|---|
| 33 | |
|---|
| 34 | static GB_HASH *dontCallHash = 0; |
|---|
| 35 | static GB_HASH *alreadyCalledHash = 0; |
|---|
| 36 | |
|---|
| 37 | static void forgetCalledCallbacks() { |
|---|
| 38 | if (alreadyCalledHash) GBS_free_hash(alreadyCalledHash); |
|---|
| 39 | alreadyCalledHash = GBS_create_hash(2500, GB_MIND_CASE); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | static long auto_dontcall1(const char *key, long value, void *cl_hash) { |
|---|
| 43 | if (strncmp(key, "ARB_NT/", 7) == 0) { |
|---|
| 44 | GB_HASH *autodontCallHash = (GB_HASH*)cl_hash; |
|---|
| 45 | GBS_write_hash(autodontCallHash, GBS_global_string("ARB_NT_1/%s", key+7), value); |
|---|
| 46 | } |
|---|
| 47 | return value; |
|---|
| 48 | } |
|---|
| 49 | static long auto_dontcall2(const char *key, long value, void *) { |
|---|
| 50 | GBS_write_hash(dontCallHash, key, value); |
|---|
| 51 | return value; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | static void build_dontCallHash() { |
|---|
| 55 | aw_assert(!dontCallHash); |
|---|
| 56 | dontCallHash = GBS_create_hash(100, GB_MIND_CASE); |
|---|
| 57 | forgetCalledCallbacks(); |
|---|
| 58 | |
|---|
| 59 | GBS_write_hash(dontCallHash, "ARB_NT/QUIT", 1); |
|---|
| 60 | GBS_write_hash(dontCallHash, "quit", 1); |
|---|
| 61 | GBS_write_hash(dontCallHash, "ARB_EDIT4/QUIT", 1); |
|---|
| 62 | GBS_write_hash(dontCallHash, "ARB_INTRO/CANCEL", 1); |
|---|
| 63 | |
|---|
| 64 | // avoid start of some external programs: |
|---|
| 65 | #if 1 |
|---|
| 66 | GBS_write_hash(dontCallHash, "ARB_NT/EDIT_SEQUENCES", 2); |
|---|
| 67 | GBS_write_hash(dontCallHash, "CPR_MAIN/HELP", 2); |
|---|
| 68 | GBS_write_hash(dontCallHash, "GDE__user__Start_a_slave_ARB_on_a_foreign_host_/GO", 2); |
|---|
| 69 | GBS_write_hash(dontCallHash, "HELP/BROWSE", 2); |
|---|
| 70 | GBS_write_hash(dontCallHash, "HELP/EDIT", 2); |
|---|
| 71 | GBS_write_hash(dontCallHash, "MACROS/EDIT", 2); |
|---|
| 72 | GBS_write_hash(dontCallHash, "MACROS/EXECUTE", 2); |
|---|
| 73 | GBS_write_hash(dontCallHash, "NAME_SERVER_ADMIN/EDIT_NAMES_FILE", 2); |
|---|
| 74 | GBS_write_hash(dontCallHash, "arb_dist", 2); |
|---|
| 75 | GBS_write_hash(dontCallHash, "arb_pars", 2); |
|---|
| 76 | GBS_write_hash(dontCallHash, "arb_pars_quick", 2); |
|---|
| 77 | GBS_write_hash(dontCallHash, "arb_phyl", 2); |
|---|
| 78 | GBS_write_hash(dontCallHash, "count_different_chars", 2); |
|---|
| 79 | GBS_write_hash(dontCallHash, "export_to_ARB", 2); |
|---|
| 80 | GBS_write_hash(dontCallHash, "new2_arb_edit4", 2); |
|---|
| 81 | GBS_write_hash(dontCallHash, "new_arb_edit4", 2); |
|---|
| 82 | GBS_write_hash(dontCallHash, "primer_design", 2); |
|---|
| 83 | GBS_write_hash(dontCallHash, "xterm", 2); |
|---|
| 84 | GBS_write_hash(dontCallHash, "SUBMIT_REG/SEND", 2); |
|---|
| 85 | GBS_write_hash(dontCallHash, "SUBMIT_BUG/SEND", 2); |
|---|
| 86 | GBS_write_hash(dontCallHash, "NAME_SERVER_ADMIN/REMOVE_SUPERFLUOUS_ENTRIES_IN_NAMES_FILE", 2); |
|---|
| 87 | GBS_write_hash(dontCallHash, "PRINT_CANVAS/PRINT", 2); |
|---|
| 88 | GBS_write_hash(dontCallHash, "PT_SERVER_ADMIN/CREATE_TEMPLATE", 2); |
|---|
| 89 | GBS_write_hash(dontCallHash, "SELECT_CONFIGURATION/START", 2); |
|---|
| 90 | #endif |
|---|
| 91 | |
|---|
| 92 | // avoid saving |
|---|
| 93 | GBS_write_hash(dontCallHash, "save_changes", 3); |
|---|
| 94 | GBS_write_hash(dontCallHash, "save_props", 3); |
|---|
| 95 | |
|---|
| 96 | #if 1 |
|---|
| 97 | #if defined(WARN_TODO) |
|---|
| 98 | #warning crashing - fix later |
|---|
| 99 | #endif |
|---|
| 100 | GBS_write_hash(dontCallHash, "ARB_NT/mark_duplicates", 4); |
|---|
| 101 | GBS_write_hash(dontCallHash, "ARB_NT/view_probe_group_result", 4); |
|---|
| 102 | GBS_write_hash(dontCallHash, "PT_SERVER_ADMIN/CHECK_SERVER", 4); |
|---|
| 103 | #endif |
|---|
| 104 | |
|---|
| 105 | #if 1 |
|---|
| 106 | #if defined(WARN_TODO) |
|---|
| 107 | #warning test callbacks asking questions again later |
|---|
| 108 | #endif |
|---|
| 109 | GBS_write_hash(dontCallHash, "ARB_NT/mark_deep_branches", 5); |
|---|
| 110 | GBS_write_hash(dontCallHash, "ARB_NT/mark_degen_branches", 5); |
|---|
| 111 | GBS_write_hash(dontCallHash, "ARB_NT/mark_long_branches", 5); |
|---|
| 112 | GBS_write_hash(dontCallHash, "ARB_NT/tree_scale_lengths", 5); |
|---|
| 113 | GBS_write_hash(dontCallHash, "CREATE_USER_MASK/CREATE", 5); |
|---|
| 114 | GBS_write_hash(dontCallHash, "GDE__Import__Import_sequences_using_Readseq_slow_/GO", 5); |
|---|
| 115 | GBS_write_hash(dontCallHash, "INFO_OF_ALIGNMENT/DELETE", 5); |
|---|
| 116 | GBS_write_hash(dontCallHash, "LOAD_SELECTION_BOX/LOAD", 5); |
|---|
| 117 | GBS_write_hash(dontCallHash, "MULTI_PROBE/CREATE_NEW_SEQUENCE", 5); |
|---|
| 118 | GBS_write_hash(dontCallHash, "NDS_PROPS/SAVELOAD_CONFIG", 5); |
|---|
| 119 | GBS_write_hash(dontCallHash, "PRIMER_DESIGN/SAVELOAD_CONFIG", 5); |
|---|
| 120 | GBS_write_hash(dontCallHash, "PROBE_DESIGN/SAVELOAD_CONFIG", 5); |
|---|
| 121 | GBS_write_hash(dontCallHash, "PT_SERVER_ADMIN/KILL_ALL_SERVERS", 5); |
|---|
| 122 | GBS_write_hash(dontCallHash, "PT_SERVER_ADMIN/KILL_SERVER", 5); |
|---|
| 123 | GBS_write_hash(dontCallHash, "PT_SERVER_ADMIN/UPDATE_SERVER", 5); |
|---|
| 124 | GBS_write_hash(dontCallHash, "REALIGN_DNA/REALIGN", 5); |
|---|
| 125 | GBS_write_hash(dontCallHash, "SPECIES_QUERY/DELETE_LISTED", 5); |
|---|
| 126 | GBS_write_hash(dontCallHash, "SPECIES_QUERY/SAVELOAD_CONFIG_spec", 5); |
|---|
| 127 | GBS_write_hash(dontCallHash, "SPECIES_SELECTIONS/RENAME", 5); |
|---|
| 128 | GBS_write_hash(dontCallHash, "SPECIES_SELECTIONS/STORE", 5); |
|---|
| 129 | GBS_write_hash(dontCallHash, "TREE_PROPS/SAVELOAD_CONFIG", 5); |
|---|
| 130 | GBS_write_hash(dontCallHash, "WWW_PROPS/SAVELOAD_CONFIG", 5); |
|---|
| 131 | GBS_write_hash(dontCallHash, "del_marked", 5); |
|---|
| 132 | #endif |
|---|
| 133 | |
|---|
| 134 | GB_HASH *autodontCallHash = GBS_create_hash(100, GB_MIND_CASE); |
|---|
| 135 | GBS_hash_do_loop(dontCallHash, auto_dontcall1, autodontCallHash); |
|---|
| 136 | GBS_hash_do_loop(autodontCallHash, auto_dontcall2, dontCallHash); |
|---|
| 137 | GBS_free_hash(autodontCallHash); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | static long collect_callbacks(const char *key, long value, void *cl_callbacks) { |
|---|
| 141 | if (GBS_read_hash(alreadyCalledHash, key) == 0) { // don't call twice |
|---|
| 142 | CallbackArray *callbacks = reinterpret_cast<CallbackArray*>(cl_callbacks); |
|---|
| 143 | callbacks->push_back(string(key)); |
|---|
| 144 | } |
|---|
| 145 | return value; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | static int sortedByCallbackLocation(const char *k0, long v0, const char *k1, long v1) { |
|---|
| 149 | AW_cb_struct *cbs0 = reinterpret_cast<AW_cb_struct*>(v0); |
|---|
| 150 | AW_cb_struct *cbs1 = reinterpret_cast<AW_cb_struct*>(v1); |
|---|
| 151 | |
|---|
| 152 | int cmp = (AW_CL)(cbs1->f) - (AW_CL)cbs0->f; // compare address of function |
|---|
| 153 | if (!cmp) { |
|---|
| 154 | cmp = cbs1->get_cd1() - cbs0->get_cd1(); |
|---|
| 155 | if (!cmp) { |
|---|
| 156 | cmp = cbs1->get_cd2() - cbs0->get_cd2(); |
|---|
| 157 | if (!cmp) cmp = strcmp(k0, k1); |
|---|
| 158 | } |
|---|
| 159 | } |
|---|
| 160 | return cmp; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | size_t AW_root::callallcallbacks(int mode) { |
|---|
| 164 | // mode == -2 -> mark all as called |
|---|
| 165 | // mode == -1 -> forget called |
|---|
| 166 | // mode == 0 -> call all in alpha-order |
|---|
| 167 | // mode == 1 -> call all in reverse alpha-order |
|---|
| 168 | // mode == 2 -> call all in code-order |
|---|
| 169 | // mode == 3 -> call all in reverse code-order |
|---|
| 170 | // mode == 10 -> call all in random order |
|---|
| 171 | // mode == 11-> call all in random order (repeated until no uncalled callbacks left) |
|---|
| 172 | |
|---|
| 173 | size_t count = GBS_hash_count_elems(prvt->action_hash); |
|---|
| 174 | size_t callCount = 0; |
|---|
| 175 | |
|---|
| 176 | aw_message(GBS_global_string("Found %zi callbacks", count)); |
|---|
| 177 | |
|---|
| 178 | if (!dontCallHash) build_dontCallHash(); |
|---|
| 179 | |
|---|
| 180 | if (mode == 11) { |
|---|
| 181 | aw_message("Calling callbacks iterated"); |
|---|
| 182 | for (int iter = 1; ; ++iter) { // forever |
|---|
| 183 | size_t thisCount = callallcallbacks(10); // call all in random order |
|---|
| 184 | aw_message(GBS_global_string("%zu callbacks were called (iteration %i)", thisCount, iter)); |
|---|
| 185 | if (!thisCount) { |
|---|
| 186 | aw_message("No uncalled callbacks left"); |
|---|
| 187 | break; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | callCount += thisCount; |
|---|
| 191 | } |
|---|
| 192 | } |
|---|
| 193 | else if (mode == -1) { |
|---|
| 194 | forgetCalledCallbacks(); |
|---|
| 195 | } |
|---|
| 196 | else { |
|---|
| 197 | CallbackArray callbacks; |
|---|
| 198 | switch (mode) { |
|---|
| 199 | case 0: |
|---|
| 200 | case 1: |
|---|
| 201 | GBS_hash_do_sorted_loop(prvt->action_hash, collect_callbacks, GBS_HCF_sortedByKey, &callbacks); |
|---|
| 202 | break; |
|---|
| 203 | case 2: |
|---|
| 204 | case 3: |
|---|
| 205 | GBS_hash_do_sorted_loop(prvt->action_hash, collect_callbacks, sortedByCallbackLocation, &callbacks); |
|---|
| 206 | break; |
|---|
| 207 | default: |
|---|
| 208 | GBS_hash_do_loop(prvt->action_hash, collect_callbacks, &callbacks); |
|---|
| 209 | break; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | switch (mode) { |
|---|
| 213 | case -2: |
|---|
| 214 | case 0: |
|---|
| 215 | case 2: break; // use this order |
|---|
| 216 | case 1: |
|---|
| 217 | case 3: reverse(callbacks.begin(), callbacks.end()); break; // use reverse order |
|---|
| 218 | case 10: random_shuffle(callbacks.begin(), callbacks.end()); break; // use random order |
|---|
| 219 | default: aw_assert(0); break; // unknown mode |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | count = callbacks.size(); |
|---|
| 223 | aw_message(GBS_global_string("%zu callbacks were not called yet", count)); |
|---|
| 224 | |
|---|
| 225 | CallbackIter end = callbacks.end(); |
|---|
| 226 | |
|---|
| 227 | for (int pass = 1; pass <= 2; ++pass) { |
|---|
| 228 | size_t curr = 1; |
|---|
| 229 | CallbackIter cb = callbacks.begin(); |
|---|
| 230 | |
|---|
| 231 | for (; cb != end; ++cb) { |
|---|
| 232 | const char *remote_command = cb->c_str(); |
|---|
| 233 | bool this_pass = remote_command[0] == '-' ? (pass == 2) : (pass == 1); |
|---|
| 234 | |
|---|
| 235 | if (this_pass) { |
|---|
| 236 | GBS_write_hash(alreadyCalledHash, remote_command, 1); // don't call twice |
|---|
| 237 | |
|---|
| 238 | if (mode != -2) { // -2 means "only mark as called" |
|---|
| 239 | AW_cb_struct *cbs = (AW_cb_struct *)GBS_read_hash(prvt->action_hash, remote_command); |
|---|
| 240 | bool skipcb = remote_command[0] == '!' || GBS_read_hash(dontCallHash, remote_command); |
|---|
| 241 | if (!skipcb) { |
|---|
| 242 | if (cbs->f == (AW_CB)AW_help_entry_pressed) skipcb = true; |
|---|
| 243 | } |
|---|
| 244 | if (skipcb) { |
|---|
| 245 | fprintf(stderr, "Skipped callback %zu/%zu (%s)\n", curr, count, remote_command); |
|---|
| 246 | } |
|---|
| 247 | else { |
|---|
| 248 | fprintf(stderr, "Calling back %zu/%zu (%s)\n", curr, count, remote_command); |
|---|
| 249 | |
|---|
| 250 | GB_clear_error(); |
|---|
| 251 | |
|---|
| 252 | cbs->run_callback(); |
|---|
| 253 | callCount++; |
|---|
| 254 | process_pending_events(); |
|---|
| 255 | |
|---|
| 256 | if (GB_have_error()) { |
|---|
| 257 | fprintf(stderr, "Unhandled error in '%s': %s\n", remote_command, GB_await_error()); |
|---|
| 258 | } |
|---|
| 259 | |
|---|
| 260 | if (cbs->f == AW_POPUP) { |
|---|
| 261 | AW_window *awp = cbs->pop_up_window; |
|---|
| 262 | if (awp) { |
|---|
| 263 | awp->force_expose(); |
|---|
| 264 | process_pending_events(); |
|---|
| 265 | |
|---|
| 266 | fprintf(stderr, "Popping down window '%s'\n", awp->get_window_id()); |
|---|
| 267 | awp->hide(); |
|---|
| 268 | process_pending_events(); |
|---|
| 269 | } |
|---|
| 270 | } |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | } |
|---|
| 274 | else { |
|---|
| 275 | if (pass == 1) { |
|---|
| 276 | fprintf(stderr, "Delayed callback %zu/%zu (%s)", curr, count, remote_command); |
|---|
| 277 | } |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | curr++; |
|---|
| 281 | } |
|---|
| 282 | |
|---|
| 283 | if (pass == 1) fprintf(stderr, "Executing delayed callbacks:\n"); |
|---|
| 284 | } |
|---|
| 285 | } |
|---|
| 286 | |
|---|
| 287 | return callCount; |
|---|
| 288 | } |
|---|
| 289 | |
|---|
| 290 | // -------------------------------------------------------------------------------- |
|---|
| 291 | #endif // DEBUG |
|---|
| 292 | |
|---|