| 1 | /* Copyright (C) 2008 Ricardo Catalinas Jiménez <jimenezrick@gmail.com> |
| 2 | * |
| 3 | * This program is free software: you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation, either version 3 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | |
| 17 | #include "fm.h" |
| 18 | |
| 19 | void get_key(void) |
| 20 | { |
| 21 | static int command_repetitions = -1; |
| 22 | static chtype previous_key = 0; |
| 23 | char *info; |
| 24 | chtype key; |
| 25 | |
| 26 | key = getch(); |
| 27 | if (key != KEY_RESIZE) |
| 28 | clear_info(); |
| 29 | |
| 30 | if (isdigit(key)) { |
| 31 | if (command_repetitions == -1) |
| 32 | command_repetitions = key - '0'; |
| 33 | else { |
| 34 | command_repetitions *= 10; |
| 35 | command_repetitions += key - '0'; |
| 36 | } |
| 37 | previous_key = key; |
| 38 | |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | if (previous_key == 'f') { |
| 43 | select_next_line_by_first_letter((char) key); |
| 44 | command_repetitions = -1; |
| 45 | previous_key = 0; |
| 46 | |
| 47 | return; |
| 48 | } else if (previous_key == 'F') { |
| 49 | select_previous_line_by_first_letter((char) key); |
| 50 | command_repetitions = -1; |
| 51 | previous_key = 0; |
| 52 | |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | if (command_repetitions == -1) |
| 57 | command_repetitions = 1; |
| 58 | |
| 59 | switch (key) { |
| 60 | case KEY_RESIZE: |
| 61 | endwin(); |
| 62 | init_curses(); |
| 63 | update_last_line(); |
| 64 | print_lines(first_line, last_line, TRUE); |
| 65 | select_line_inside_window(); |
| 66 | PRINT_LAST_INFO(); |
| 67 | break; |
| 68 | case 0x0c: /* Ctrl + l */ |
| 69 | clearok(curscr, TRUE); |
| 70 | if (!G_NODE_IS_ROOT(NODE(selected_line))) |
| 71 | select_file(NODE(selected_line)->parent); |
| 72 | update_directory(NODE(selected_line)); |
| 73 | break; |
| 74 | case 0x1b: /* Escape */ |
| 75 | command_repetitions = -1; |
| 76 | previous_key = 0; |
| 77 | |
| 78 | return; |
| 79 | case KEY_NPAGE: |
| 80 | case 0x06: /* Ctrl + f */ |
| 81 | select_line(last_line); |
| 82 | scroll_tree(command_repetitions * (getmaxy(tree_window) - 1)); |
| 83 | break; |
| 84 | case KEY_PPAGE: |
| 85 | case 0x02: /* Ctrl + b */ |
| 86 | select_line(first_line); |
| 87 | scroll_tree(command_repetitions * (-(getmaxy(tree_window) - 1))); |
| 88 | break; |
| 89 | case 0x04: /* Ctrl + d */ |
| 90 | scroll_tree(command_repetitions * getmaxy(tree_window) / 2); |
| 91 | select_nth_line(command_repetitions * getmaxy(tree_window) / 2); |
| 92 | break; |
| 93 | case 0x15: /* Ctrl + u */ |
| 94 | scroll_tree(command_repetitions * -getmaxy(tree_window) / 2); |
| 95 | select_nth_line(command_repetitions * -getmaxy(tree_window) / 2); |
| 96 | break; |
| 97 | case 'g': |
| 98 | if (previous_key == 'g') |
| 99 | select_line(lines); |
| 100 | break; |
| 101 | case 'G': |
| 102 | select_line(g_list_last(lines)); |
| 103 | break; |
| 104 | case 0x07: /* Ctrl + g */ |
| 105 | if ((info = get_file_info(NODE(selected_line))) != NULL) { |
| 106 | print_info(info, FALSE); |
| 107 | free(info); |
| 108 | } |
| 109 | break; |
| 110 | case 'z': |
| 111 | if (previous_key == 'z') |
| 112 | scroll_tree(g_list_position(first_line, selected_line) - |
| 113 | getmaxy(tree_window) / 2); |
| 114 | break; |
| 115 | case 'H': |
| 116 | select_line(first_line); |
| 117 | break; |
| 118 | case 'L': |
| 119 | select_line(last_line); |
| 120 | break; |
| 121 | case KEY_DOWN: |
| 122 | case 'j': |
| 123 | select_nth_line(command_repetitions); |
| 124 | break; |
| 125 | case KEY_UP: |
| 126 | case 'k': |
| 127 | select_nth_line(-command_repetitions); |
| 128 | break; |
| 129 | case KEY_RIGHT: |
| 130 | case 'l': |
| 131 | if (FILE(NODE(selected_line))->type == directory_type && |
| 132 | FILE(NODE(selected_line))->open == FALSE) |
| 133 | open_directory(NODE(selected_line)); |
| 134 | else if (FILE(NODE(selected_line))->type == directory_type && |
| 135 | FILE(NODE(selected_line))->open == TRUE && |
| 136 | FILE(NODE(selected_line))->show_dotfiles == FALSE) |
| 137 | show_dotfiles(NODE(selected_line)); |
| 138 | else if (FILE(NODE(selected_line))->type == file_type) |
| 139 | read_file(NODE(selected_line)); |
| 140 | break; |
| 141 | case KEY_LEFT: |
| 142 | case 'h': |
| 143 | close_parent: if (FILE(NODE(selected_line))->type == directory_type && |
| 144 | FILE(NODE(selected_line))->open == TRUE && |
| 145 | FILE(NODE(selected_line))->show_dotfiles == TRUE && |
| 146 | !G_NODE_IS_LEAF(NODE(selected_line)) && |
| 147 | FILE(g_node_first_child(NODE(selected_line)))->name[0] == '.') |
| 148 | hide_dotfiles(NODE(selected_line)); |
| 149 | else if (FILE(NODE(selected_line))->type == directory_type && |
| 150 | FILE(NODE(selected_line))->open == TRUE && |
| 151 | !G_NODE_IS_LEAF(NODE(selected_line))) |
| 152 | close_directory(NODE(selected_line)); |
| 153 | else if (!G_NODE_IS_ROOT(NODE(selected_line))) { |
| 154 | select_file(NODE(selected_line)->parent); |
| 155 | goto close_parent; |
| 156 | } |
| 157 | break; |
| 158 | case 'q': |
| 159 | endwin(); |
| 160 | exit(EXIT_SUCCESS); |
| 161 | break; |
| 162 | case '{': |
| 163 | if (g_node_prev_sibling(NODE(selected_line)) != NULL) |
| 164 | select_file(g_node_prev_sibling(NODE(selected_line))); |
| 165 | else |
| 166 | select_nth_line(-1); |
| 167 | break; |
| 168 | case '}': |
| 169 | if (g_node_next_sibling(NODE(selected_line)) != NULL) |
| 170 | select_file(g_node_next_sibling(NODE(selected_line))); |
| 171 | else |
| 172 | select_nth_line(1); |
| 173 | break; |
| 174 | case '[': |
| 175 | if (previous_key == '[' && !G_NODE_IS_ROOT(NODE(selected_line))) |
| 176 | select_file(NODE(selected_line)->parent); |
| 177 | break; |
| 178 | case 's': |
| 179 | if (!G_NODE_IS_ROOT(NODE(selected_line))) |
| 180 | run_shell(NODE(selected_line)->parent); |
| 181 | else |
| 182 | run_shell(NODE(selected_line)); |
| 183 | break; |
| 184 | case KEY_ENTER: |
| 185 | case 0x0d: /* Enter */ |
| 186 | if (FILE(NODE(selected_line))->type == file_type) |
| 187 | edit_file(NODE(selected_line)); |
| 188 | break; |
| 189 | } |
| 190 | if (key != KEY_RESIZE) { |
| 191 | command_repetitions = -1; |
| 192 | previous_key = key; |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | void run_program(GNode *current_directory, char *program_name_variable, |
| 197 | char *default_program_name, char *arg, gboolean wait_proccess) |
| 198 | { |
| 199 | char *path, *pwd, *program_name; |
| 200 | pid_t child; |
| 201 | int status; |
| 202 | |
| 203 | endwin(); |
| 204 | if ((child = fork()) != 0 && wait_proccess) { |
| 205 | unset_sighandlers(); |
| 206 | waitpid(child, &status, 0); |
| 207 | set_sighandlers(); |
| 208 | } else if (child == 0) { |
| 209 | path = get_path(current_directory); |
| 210 | if (chdir(path) == -1) |
| 211 | exit(EXIT_FAILURE); |
| 212 | pwd = g_strconcat("PWD=", path, NULL); |
| 213 | putenv(pwd); |
| 214 | free(path); |
| 215 | |
| 216 | if (!wait_proccess) { |
| 217 | close(0); |
| 218 | close(1); |
| 219 | close(2); |
| 220 | } |
| 221 | |
| 222 | if (program_name_variable != NULL && |
| 223 | (program_name = getenv(program_name_variable)) != NULL) { |
| 224 | if (execlp(program_name, program_name, arg, NULL) == -1) |
| 225 | exit(EXIT_FAILURE); |
| 226 | } else { |
| 227 | if (execlp(default_program_name, default_program_name, arg, NULL) == -1) |
| 228 | exit(EXIT_FAILURE); |
| 229 | } |
| 230 | } |
| 231 | init_curses(); |
| 232 | update_last_line(); |
| 233 | print_lines(first_line, last_line, TRUE); |
| 234 | select_line_inside_window(); |
| 235 | |
| 236 | if (wait_proccess && WIFEXITED(status) && WEXITSTATUS(status) != EXIT_SUCCESS) |
| 237 | PRINT_ERROR_INFO("Something failed when running the external program"); |
| 238 | } |
| 239 | |
| 240 | void run_shell(GNode *directory) |
| 241 | { |
| 242 | run_program(directory, "SHELL", DEFAULT_SHELL, NULL, TRUE); |
| 243 | } |
| 244 | |
| 245 | void read_file(GNode *file) |
| 246 | { |
| 247 | if (g_str_has_suffix(FILE(file)->name, ".pdf") || |
| 248 | g_str_has_suffix(FILE(file)->name, ".pdf.gz") || |
| 249 | g_str_has_suffix(FILE(file)->name, ".pdf.Z")) |
| 250 | run_program(file->parent, NULL, DEFAULT_PDF_VIEWER, FILE(file)->name, FALSE); |
| 251 | else if (g_str_has_suffix(FILE(file)->name, ".ps") || |
| 252 | g_str_has_suffix(FILE(file)->name, ".ps.gz") || |
| 253 | g_str_has_suffix(FILE(file)->name, ".ps.Z")) |
| 254 | run_program(file->parent, NULL, DEFAULT_PS_VIEWER, FILE(file)->name, FALSE); |
| 255 | else if (g_str_has_suffix(FILE(file)->name, ".jpg") || |
| 256 | g_str_has_suffix(FILE(file)->name, ".jpeg") || |
| 257 | g_str_has_suffix(FILE(file)->name, ".png") || |
| 258 | g_str_has_suffix(FILE(file)->name, ".gif")) |
| 259 | run_program(file->parent, NULL, DEFAULT_IMAGE_VIEWER, FILE(file)->name, FALSE); |
| 260 | else |
| 261 | run_program(file->parent, "PAGER", DEFAULT_PAGER, FILE(file)->name, TRUE); |
| 262 | } |
| 263 | |
| 264 | void edit_file(GNode *file) |
| 265 | { |
| 266 | run_program(file->parent, "EDITOR", DEFAULT_EDITOR, FILE(file)->name, TRUE); |
| 267 | } |