console.c
changeset 4077 d3022f976946
parent 3559 1d4b8eb9b493
child 4171 3fadda3afe70
equal deleted inserted replaced
4076:e3ef1c1f149c 4077:d3022f976946
   107 				case WKC_DOWN:
   107 				case WKC_DOWN:
   108 					IConsoleHistoryNavigate(-1);
   108 					IConsoleHistoryNavigate(-1);
   109 					SetWindowDirty(w);
   109 					SetWindowDirty(w);
   110 					break;
   110 					break;
   111 				case WKC_SHIFT | WKC_PAGEUP:
   111 				case WKC_SHIFT | WKC_PAGEUP:
   112 					if (_iconsole_scroll - (w->height / ICON_LINE_HEIGHT) - 1 < 0)
   112 					if (_iconsole_scroll - (w->height / ICON_LINE_HEIGHT) - 1 < 0) {
   113 						_iconsole_scroll = 0;
   113 						_iconsole_scroll = 0;
   114 					else
   114 					} else {
   115 						_iconsole_scroll -= (w->height / ICON_LINE_HEIGHT) - 1;
   115 						_iconsole_scroll -= (w->height / ICON_LINE_HEIGHT) - 1;
       
   116 					}
   116 					SetWindowDirty(w);
   117 					SetWindowDirty(w);
   117 					break;
   118 					break;
   118 				case WKC_SHIFT | WKC_PAGEDOWN:
   119 				case WKC_SHIFT | WKC_PAGEDOWN:
   119 					if (_iconsole_scroll + (w->height / ICON_LINE_HEIGHT) - 1 > ICON_BUFFER)
   120 					if (_iconsole_scroll + (w->height / ICON_LINE_HEIGHT) - 1 > ICON_BUFFER) {
   120 						_iconsole_scroll = ICON_BUFFER;
   121 						_iconsole_scroll = ICON_BUFFER;
   121 					else
   122 					} else {
   122 						_iconsole_scroll += (w->height / ICON_LINE_HEIGHT) - 1;
   123 						_iconsole_scroll += (w->height / ICON_LINE_HEIGHT) - 1;
       
   124 					}
   123 					SetWindowDirty(w);
   125 					SetWindowDirty(w);
   124 					break;
   126 					break;
   125 				case WKC_SHIFT | WKC_UP:
   127 				case WKC_SHIFT | WKC_UP:
   126 					if (_iconsole_scroll <= 0)
   128 					if (_iconsole_scroll <= 0) {
   127 						_iconsole_scroll = 0;
   129 						_iconsole_scroll = 0;
   128 					else
   130 					} else {
   129 						--_iconsole_scroll;
   131 						--_iconsole_scroll;
       
   132 					}
   130 					SetWindowDirty(w);
   133 					SetWindowDirty(w);
   131 					break;
   134 					break;
   132 				case WKC_SHIFT | WKC_DOWN:
   135 				case WKC_SHIFT | WKC_DOWN:
   133 					if (_iconsole_scroll >= ICON_BUFFER)
   136 					if (_iconsole_scroll >= ICON_BUFFER) {
   134 						_iconsole_scroll = ICON_BUFFER;
   137 						_iconsole_scroll = ICON_BUFFER;
   135 					else
   138 					} else {
   136 						++_iconsole_scroll;
   139 						++_iconsole_scroll;
       
   140 					}
   137 					SetWindowDirty(w);
   141 					SetWindowDirty(w);
   138 					break;
   142 					break;
   139 				case WKC_BACKQUOTE:
   143 				case WKC_BACKQUOTE:
   140 					IConsoleSwitch();
   144 					IConsoleSwitch();
   141 					break;
   145 					break;
   180 					if (IsValidAsciiChar(e->keypress.ascii)) {
   184 					if (IsValidAsciiChar(e->keypress.ascii)) {
   181 						_iconsole_scroll = ICON_BUFFER;
   185 						_iconsole_scroll = ICON_BUFFER;
   182 						InsertTextBufferChar(&_iconsole_cmdline, e->keypress.ascii);
   186 						InsertTextBufferChar(&_iconsole_cmdline, e->keypress.ascii);
   183 						IConsoleResetHistoryPos();
   187 						IConsoleResetHistoryPos();
   184 						SetWindowDirty(w);
   188 						SetWindowDirty(w);
   185 					} else
   189 					} else {
   186 						e->keypress.cont = true;
   190 						e->keypress.cont = true;
       
   191 					}
   187 			break;
   192 			break;
   188 		}
   193 		}
   189 	}
   194 	}
   190 }
   195 }
   191 
   196 
   563 /**
   568 /**
   564  * Perhaps ugly macro, but this saves us the trouble of writing the same function
   569  * Perhaps ugly macro, but this saves us the trouble of writing the same function
   565  * three types, just with different variables. Yes, templates would be handy. It was
   570  * three types, just with different variables. Yes, templates would be handy. It was
   566  * either this define or an even more ugly void* magic function
   571  * either this define or an even more ugly void* magic function
   567  */
   572  */
   568 #define IConsoleAddSorted(_base, item_new, IConsoleType, type)                      \
   573 #define IConsoleAddSorted(_base, item_new, IConsoleType, type)                 \
   569 {                                                                                   \
   574 {                                                                              \
   570 	IConsoleType *item, *item_before;                                                 \
   575 	IConsoleType *item, *item_before;                                            \
   571 	/* first command */                                                               \
   576 	/* first command */                                                          \
   572 	if (_base == NULL) {                                                              \
   577 	if (_base == NULL) {                                                         \
   573 		_base = item_new;                                                               \
   578 		_base = item_new;                                                          \
   574 		return;                                                                         \
   579 		return;                                                                    \
   575 	}                                                                                 \
   580 	}                                                                            \
   576                                                                                     \
   581                                                                                \
   577 	item_before = NULL;                                                               \
   582 	item_before = NULL;                                                          \
   578 	item = _base;                                                                     \
   583 	item = _base;                                                                \
   579                                                                                     \
   584                                                                                \
   580 	/* BEGIN - Alphabetically insert the commands into the linked list */             \
   585 	/* BEGIN - Alphabetically insert the commands into the linked list */        \
   581 	while (item != NULL) {                                                            \
   586 	while (item != NULL) {                                                       \
   582 		int i = strcmp(item->name, item_new->name);                                     \
   587 		int i = strcmp(item->name, item_new->name);                                \
   583 		if (i == 0) {                                                                   \
   588 		if (i == 0) {                                                              \
   584 			IConsoleError(type " with this name already exists; insertion aborted");      \
   589 			IConsoleError(type " with this name already exists; insertion aborted"); \
   585 			free(item_new);                                                               \
   590 			free(item_new);                                                          \
   586 			return;                                                                       \
   591 			return;                                                                  \
   587 		}                                                                               \
   592 		}                                                                          \
   588                                                                                     \
   593                                                                                \
   589 		if (i > 0) break; /* insert at this position */                                 \
   594 		if (i > 0) break; /* insert at this position */                            \
   590                                                                                     \
   595                                                                                \
   591 		item_before = item;                                                             \
   596 		item_before = item;                                                        \
   592 		item = item->next;                                                              \
   597 		item = item->next;                                                         \
   593 	}                                                                                 \
   598 	}                                                                            \
   594                                                                                     \
   599                                                                                \
   595 	if (item_before == NULL) {                                                        \
   600 	if (item_before == NULL) {                                                   \
   596 		_base = item_new;                                                               \
   601 		_base = item_new;                                                          \
   597 	} else                                                                            \
   602 	} else {                                                                     \
   598 		item_before->next = item_new;                                                   \
   603 		item_before->next = item_new;                                              \
   599                                                                                     \
   604   }                                                                            \
   600 	item_new->next = item;                                                            \
   605                                                                                \
   601 	/* END - Alphabetical insert */                                                   \
   606 	item_new->next = item;                                                       \
       
   607 	/* END - Alphabetical insert */                                              \
   602 }
   608 }
   603 
   609 
   604 /**
   610 /**
   605  * Register a new command to be used in the console
   611  * Register a new command to be used in the console
   606  * @param name name of the command that will be used
   612  * @param name name of the command that will be used
   851  * Set a new value to a string-type variable. Basically this
   857  * Set a new value to a string-type variable. Basically this
   852  * means to copy the new value over to the container.
   858  * means to copy the new value over to the container.
   853  * @param *var the variable in question
   859  * @param *var the variable in question
   854  * @param *value the new value
   860  * @param *value the new value
   855  */
   861  */
   856 static void IConsoleVarSetStringvalue(const IConsoleVar *var, char *value)
   862 static void IConsoleVarSetStringvalue(const IConsoleVar* var, const char* value)
   857 {
   863 {
   858 	if (var->type != ICONSOLE_VAR_STRING || var->addr == NULL) return;
   864 	if (var->type != ICONSOLE_VAR_STRING || var->addr == NULL) return;
   859 
   865 
   860 	IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_PRE_ACTION);
   866 	IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_PRE_ACTION);
   861 	ttd_strlcpy((char*)var->addr, (char*)value, var->size);
   867 	ttd_strlcpy(var->addr, value, var->size);
   862 	IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_POST_ACTION);
   868 	IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_POST_ACTION);
   863 	IConsoleVarPrintSetValue(var); // print out the new value, giving feedback
   869 	IConsoleVarPrintSetValue(var); // print out the new value, giving feedback
   864 	return;
   870 	return;
   865 }
   871 }
   866 
   872 
  1096 		}
  1102 		}
  1097 	}
  1103 	}
  1098 
  1104 
  1099 	if (_stdlib_con_developer) {
  1105 	if (_stdlib_con_developer) {
  1100 		uint i;
  1106 		uint i;
  1101 		for (i = 0; tokens[i] != NULL; i++)
  1107 
       
  1108 		for (i = 0; tokens[i] != NULL; i++) {
  1102 			IConsolePrintF(_icolour_dbg, "condbg: token %d is: '%s'", i, tokens[i]);
  1109 			IConsolePrintF(_icolour_dbg, "condbg: token %d is: '%s'", i, tokens[i]);
       
  1110 		}
  1103 	}
  1111 	}
  1104 
  1112 
  1105 	if (tokens[0] == '\0') return; // don't execute empty commands
  1113 	if (tokens[0] == '\0') return; // don't execute empty commands
  1106 	/* 2. Determine type of command (cmd, alias or variable) and execute
  1114 	/* 2. Determine type of command (cmd, alias or variable) and execute
  1107 	 * First try commands, then aliases, and finally variables. Execute
  1115 	 * First try commands, then aliases, and finally variables. Execute
  1108 	 * the found action taking into account its hooking code
  1116 	 * the found action taking into account its hooking code
  1109 	 */
  1117 	 */
  1110 	 cmd = IConsoleCmdGet(tokens[0]);
  1118 	cmd = IConsoleCmdGet(tokens[0]);
  1111 	 if (cmd != NULL) {
  1119 	if (cmd != NULL) {
  1112 		if (IConsoleHookHandle(&cmd->hook, ICONSOLE_HOOK_ACCESS)) {
  1120 		if (IConsoleHookHandle(&cmd->hook, ICONSOLE_HOOK_ACCESS)) {
  1113 			IConsoleHookHandle(&cmd->hook, ICONSOLE_HOOK_PRE_ACTION);
  1121 			IConsoleHookHandle(&cmd->hook, ICONSOLE_HOOK_PRE_ACTION);
  1114 			if (cmd->proc(t_index, tokens)) { // index started with 0
  1122 			if (cmd->proc(t_index, tokens)) { // index started with 0
  1115 				IConsoleHookHandle(&cmd->hook, ICONSOLE_HOOK_POST_ACTION);
  1123 				IConsoleHookHandle(&cmd->hook, ICONSOLE_HOOK_POST_ACTION);
  1116   		} else cmd->proc(0, NULL); // if command failed, give help
  1124 			} else {
       
  1125 				cmd->proc(0, NULL); // if command failed, give help
       
  1126 			}
  1117 		}
  1127 		}
  1118 	 	return;
  1128 		return;
  1119 	 }
  1129 	}
  1120 
  1130 
  1121 	 t_index--; // ignore the variable-name for comfort for both aliases and variaables
  1131 	t_index--; // ignore the variable-name for comfort for both aliases and variaables
  1122 	 alias = IConsoleAliasGet(tokens[0]);
  1132 	alias = IConsoleAliasGet(tokens[0]);
  1123 	 if (alias != NULL) {
  1133 	if (alias != NULL) {
  1124 	 	IConsoleAliasExec(alias, t_index, &tokens[1]);
  1134 		IConsoleAliasExec(alias, t_index, &tokens[1]);
  1125 	 	return;
  1135 		return;
  1126 	 }
  1136 	}
  1127 
  1137 
  1128 	 var = IConsoleVarGet(tokens[0]);
  1138 	var = IConsoleVarGet(tokens[0]);
  1129 	 if (var != NULL) {
  1139 	if (var != NULL) {
  1130 	 	if (IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_ACCESS))
  1140 		if (IConsoleHookHandle(&var->hook, ICONSOLE_HOOK_ACCESS)) {
  1131 	 		IConsoleVarExec(var, t_index, &tokens[1]);
  1141 			IConsoleVarExec(var, t_index, &tokens[1]);
  1132 
  1142 		}
  1133 	 	return;
  1143 		return;
  1134 	 }
  1144 	}
  1135 
  1145 
  1136 	 IConsoleError("command or variable not found");
  1146 	IConsoleError("command or variable not found");
  1137 }
  1147 }