| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
#include <stdlib.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <sys/types.h> |
| 30 |
#include <unistd.h> |
| 31 |
#include <string.h> |
| 32 |
#include <signal.h> |
| 33 |
#include <time.h> |
| 34 |
#ifdef HAVE_NCURSES |
| 35 |
#ifdef HAVE_NCURSES_SUBDIR |
| 36 |
#include <ncurses/ncurses.h> |
| 37 |
#else |
| 38 |
#include <ncurses.h> |
| 39 |
#endif |
| 40 |
#else |
| 41 |
#include <curses.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#include "menuselect.h" |
| 45 |
|
| 46 |
#define MENU_HELP "Press 'h' for help." |
| 47 |
|
| 48 |
#define TITLE_HEIGHT 7 |
| 49 |
|
| 50 |
#define MIN_X 80 |
| 51 |
#define MIN_Y 27 |
| 52 |
|
| 53 |
#define PAGE_OFFSET 10 |
| 54 |
|
| 55 |
#define SCROLL_NONE 0 |
| 56 |
#define SCROLL_DOWN 1 |
| 57 |
|
| 58 |
#define SCROLL_DOWN_INDICATOR "... More ..." |
| 59 |
|
| 60 |
#define MIN(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); ((__a > __b) ? __b : __a);}) |
| 61 |
#define MAX(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); ((__a < __b) ? __b : __a);}) |
| 62 |
|
| 63 |
extern int changes_made; |
| 64 |
|
| 65 |
|
| 66 |
static int max_x = 0; |
| 67 |
|
| 68 |
static int max_y = 0; |
| 69 |
|
| 70 |
static const char * const help_info[] = { |
| 71 |
"scroll => up/down arrows", |
| 72 |
"toggle selection => Enter", |
| 73 |
"select => y", |
| 74 |
"deselect => n", |
| 75 |
"select all => F8", |
| 76 |
"deselect all => F7", |
| 77 |
"back => left arrow", |
| 78 |
"quit => q", |
| 79 |
"save and quit => x", |
| 80 |
"", |
| 81 |
"XXX means dependencies have not been met", |
| 82 |
" or a conflict exists", |
| 83 |
"", |
| 84 |
"< > means a dependency has been deselected", |
| 85 |
" and will be automatically re-selected", |
| 86 |
" if this item is selected", |
| 87 |
"", |
| 88 |
"( ) means a conflicting item has been", |
| 89 |
" selected", |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
static void _winch_handler(int sig) |
| 94 |
{ |
| 95 |
getmaxyx(stdscr, max_y, max_x); |
| 96 |
|
| 97 |
if (max_x < MIN_X || max_y < MIN_Y) { |
| 98 |
fprintf(stderr, "Terminal must be at least %d x %d.\n", MIN_X, MIN_Y); |
| 99 |
max_x = MIN_X - 1; |
| 100 |
max_y = MIN_Y - 1; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
static struct sigaction winch_handler = { |
| 105 |
.sa_handler = _winch_handler, |
| 106 |
}; |
| 107 |
|
| 108 |
|
| 109 |
static void _sigint_handler(int sig) |
| 110 |
{ |
| 111 |
|
| 112 |
} |
| 113 |
|
| 114 |
static struct sigaction sigint_handler = { |
| 115 |
.sa_handler = _sigint_handler, |
| 116 |
}; |
| 117 |
|
| 118 |
|
| 119 |
static void show_help(WINDOW *win) |
| 120 |
{ |
| 121 |
int i; |
| 122 |
|
| 123 |
wclear(win); |
| 124 |
for (i = 0; i < (sizeof(help_info) / sizeof(help_info[0])); i++) { |
| 125 |
wmove(win, i, max_x / 2 - 15); |
| 126 |
waddstr(win, (char *) help_info[i]); |
| 127 |
} |
| 128 |
wrefresh(win); |
| 129 |
getch(); |
| 130 |
} |
| 131 |
|
| 132 |
static int really_quit(WINDOW *win) |
| 133 |
{ |
| 134 |
int c; |
| 135 |
wclear(win); |
| 136 |
wmove(win, 2, max_x / 2 - 15); |
| 137 |
waddstr(win, "ARE YOU SURE?"); |
| 138 |
wmove(win, 3, max_x / 2 - 12); |
| 139 |
waddstr(win, "--- It appears you have made some changes, and"); |
| 140 |
wmove(win, 4, max_x / 2 - 12); |
| 141 |
waddstr(win, "you have opted to Quit without saving these changes!"); |
| 142 |
wmove(win, 6, max_x / 2 - 12); |
| 143 |
waddstr(win, " Please Enter Y to exit without saving;"); |
| 144 |
wmove(win, 7, max_x / 2 - 12); |
| 145 |
waddstr(win, " Enter N to cancel your decision to quit,"); |
| 146 |
wmove(win, 8, max_x / 2 - 12); |
| 147 |
waddstr(win, " and keep working in menuselect, or"); |
| 148 |
wmove(win, 9, max_x / 2 - 12); |
| 149 |
waddstr(win, " Enter S to save your changes, and exit"); |
| 150 |
wmove(win, 10, max_x / 2 - 12); |
| 151 |
wrefresh(win); |
| 152 |
while ((c=getch())) { |
| 153 |
if (c == 'Y' || c == 'y') { |
| 154 |
c = 'q'; |
| 155 |
break; |
| 156 |
} |
| 157 |
if (c == 'S' || c == 's') { |
| 158 |
c = 'S'; |
| 159 |
break; |
| 160 |
} |
| 161 |
if (c == 'N' || c == 'n') { |
| 162 |
c = '%'; |
| 163 |
break; |
| 164 |
} |
| 165 |
} |
| 166 |
return c; |
| 167 |
} |
| 168 |
|
| 169 |
#define MENU_HELP_LEFT_ADJ 16 |
| 170 |
#define MAIN_MENU_LEFT_ADJ 20 |
| 171 |
#define CAT_MENU_LEFT_ADJ 20 |
| 172 |
#define SCROLL_DOWN_LEFT_ADJ 15 |
| 173 |
#define MEMBER_INFO_LEFT_ADJ 25 |
| 174 |
|
| 175 |
static void draw_main_menu(WINDOW *menu, int curopt) |
| 176 |
{ |
| 177 |
struct category *cat; |
| 178 |
char buf[64]; |
| 179 |
int i = 0; |
| 180 |
|
| 181 |
wclear(menu); |
| 182 |
|
| 183 |
AST_LIST_TRAVERSE(&categories, cat, list) { |
| 184 |
wmove(menu, i++, max_x / 2 - MAIN_MENU_LEFT_ADJ); |
| 185 |
snprintf(buf, sizeof(buf), "%s", strlen_zero(cat->displayname) ? cat->name : cat->displayname); |
| 186 |
waddstr(menu, buf); |
| 187 |
} |
| 188 |
|
| 189 |
wmove(menu, curopt, (max_x / 2) - MAIN_MENU_LEFT_ADJ - 5); |
| 190 |
waddstr(menu, "--->"); |
| 191 |
wmove(menu, curopt, (max_x / 2) - MAIN_MENU_LEFT_ADJ); |
| 192 |
|
| 193 |
wrefresh(menu); |
| 194 |
} |
| 195 |
|
| 196 |
static void display_mem_info(WINDOW *menu, struct member *mem, int start_y, int end) |
| 197 |
{ |
| 198 |
char buf[64]; |
| 199 |
struct reference *dep; |
| 200 |
struct reference *con; |
| 201 |
struct reference *use; |
| 202 |
int start_x = (max_x / 2 - MEMBER_INFO_LEFT_ADJ); |
| 203 |
int maxlen = (max_x - start_x); |
| 204 |
|
| 205 |
wmove(menu, end - start_y + 1, 0); |
| 206 |
wclrtoeol(menu); |
| 207 |
wmove(menu, end - start_y + 2, 0); |
| 208 |
wclrtoeol(menu); |
| 209 |
wmove(menu, end - start_y + 3, 0); |
| 210 |
wclrtoeol(menu); |
| 211 |
wmove(menu, end - start_y + 4, 0); |
| 212 |
wclrtoeol(menu); |
| 213 |
wmove(menu, end - start_y + 5, 0); |
| 214 |
wclrtoeol(menu); |
| 215 |
wmove(menu, end - start_y + 6, 0); |
| 216 |
wclrtoeol(menu); |
| 217 |
wmove(menu, end - start_y + 7, 0); |
| 218 |
wclrtoeol(menu); |
| 219 |
|
| 220 |
if (mem->displayname) { |
| 221 |
char buf[maxlen + 1]; |
| 222 |
char *displayname = ast_strdupa(mem->displayname); |
| 223 |
char *word; |
| 224 |
int current_line = 1; |
| 225 |
int new_line = 1; |
| 226 |
|
| 227 |
buf[0] = '\0'; |
| 228 |
wmove(menu, end - start_y + 1, start_x); |
| 229 |
|
| 230 |
while ((word = strsep(&displayname, " "))) { |
| 231 |
if ((strlen(buf) + strlen(word) + 1) > maxlen) { |
| 232 |
waddstr(menu, buf); |
| 233 |
current_line++; |
| 234 |
wmove(menu, end - start_y + current_line, start_x); |
| 235 |
buf[0] = '\0'; |
| 236 |
new_line = 1; |
| 237 |
} |
| 238 |
sprintf(buf + strlen(buf), "%*.*s%s", new_line ? 0 : 1, new_line ? 0 : 1, " ", word); |
| 239 |
new_line = 0; |
| 240 |
} |
| 241 |
if (strlen(buf)) { |
| 242 |
waddstr(menu, buf); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
if (!AST_LIST_EMPTY(&mem->deps)) { |
| 247 |
wmove(menu, end - start_y + 4, start_x); |
| 248 |
strcpy(buf, "Depends on: "); |
| 249 |
AST_LIST_TRAVERSE(&mem->deps, dep, list) { |
| 250 |
strncat(buf, dep->displayname, sizeof(buf) - strlen(buf) - 1); |
| 251 |
strncat(buf, dep->member ? "(M)" : "(E)", sizeof(buf) - strlen(buf) - 1); |
| 252 |
if (AST_LIST_NEXT(dep, list)) |
| 253 |
strncat(buf, ", ", sizeof(buf) - strlen(buf) - 1); |
| 254 |
} |
| 255 |
waddstr(menu, buf); |
| 256 |
} |
| 257 |
if (!AST_LIST_EMPTY(&mem->uses)) { |
| 258 |
wmove(menu, end - start_y + 5, start_x); |
| 259 |
strcpy(buf, "Can use: "); |
| 260 |
AST_LIST_TRAVERSE(&mem->uses, use, list) { |
| 261 |
strncat(buf, use->displayname, sizeof(buf) - strlen(buf) - 1); |
| 262 |
strncat(buf, use->member ? "(M)" : "(E)", sizeof(buf) - strlen(buf) - 1); |
| 263 |
if (AST_LIST_NEXT(use, list)) |
| 264 |
strncat(buf, ", ", sizeof(buf) - strlen(buf) - 1); |
| 265 |
} |
| 266 |
waddstr(menu, buf); |
| 267 |
} |
| 268 |
if (!AST_LIST_EMPTY(&mem->conflicts)) { |
| 269 |
wmove(menu, end - start_y + 6, start_x); |
| 270 |
strcpy(buf, "Conflicts with: "); |
| 271 |
AST_LIST_TRAVERSE(&mem->conflicts, con, list) { |
| 272 |
strncat(buf, con->displayname, sizeof(buf) - strlen(buf) - 1); |
| 273 |
strncat(buf, con->member ? "(M)" : "(E)", sizeof(buf) - strlen(buf) - 1); |
| 274 |
if (AST_LIST_NEXT(con, list)) |
| 275 |
strncat(buf, ", ", sizeof(buf) - strlen(buf) - 1); |
| 276 |
} |
| 277 |
waddstr(menu, buf); |
| 278 |
} |
| 279 |
|
| 280 |
if (!mem->is_separator) { |
| 281 |
{ |
| 282 |
char buf2[64]; |
| 283 |
wmove(menu, end - start_y + 7, start_x); |
| 284 |
snprintf(buf, sizeof(buf), "Support Level: %s", mem->support_level); |
| 285 |
if (mem->replacement && *mem->replacement) { |
| 286 |
snprintf(buf2, sizeof(buf2), ", Replaced by: %s", mem->replacement); |
| 287 |
strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1); |
| 288 |
} |
| 289 |
if (mem->deprecated_in && *mem->deprecated_in) { |
| 290 |
snprintf(buf2, sizeof(buf2), ", Deprecated in: %s", mem->deprecated_in); |
| 291 |
strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1); |
| 292 |
} |
| 293 |
if (mem->removed_in && *mem->removed_in) { |
| 294 |
snprintf(buf2, sizeof(buf2), ", Removed in: %s", mem->removed_in); |
| 295 |
strncat(buf, buf2, sizeof(buf) - strlen(buf) - 1); |
| 296 |
} |
| 297 |
waddstr(menu, buf); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
static void draw_category_menu(WINDOW *menu, struct category *cat, int start, int end, int curopt, int changed, int flags) |
| 303 |
{ |
| 304 |
int i = 0; |
| 305 |
int j = 0; |
| 306 |
struct member *mem; |
| 307 |
char buf[64]; |
| 308 |
|
| 309 |
if (!changed) { |
| 310 |
|
| 311 |
|
| 312 |
AST_LIST_TRAVERSE(&cat->members, mem, list) { |
| 313 |
i++; |
| 314 |
if (curopt + 1 == i) { |
| 315 |
display_mem_info(menu, mem, start, end); |
| 316 |
break; |
| 317 |
} |
| 318 |
} |
| 319 |
wmove(menu, curopt - start, (max_x / 2) - (CAT_MENU_LEFT_ADJ - 1)); |
| 320 |
wrefresh(menu); |
| 321 |
return; |
| 322 |
} |
| 323 |
|
| 324 |
wclear(menu); |
| 325 |
|
| 326 |
i = 0; |
| 327 |
AST_LIST_TRAVERSE(&cat->members, mem, list) { |
| 328 |
if (i < start) { |
| 329 |
i++; |
| 330 |
continue; |
| 331 |
} |
| 332 |
wmove(menu, j++, max_x / 2 - CAT_MENU_LEFT_ADJ); |
| 333 |
i++; |
| 334 |
if ((mem->depsfailed == HARD_FAILURE) || (mem->conflictsfailed == HARD_FAILURE)) { |
| 335 |
snprintf(buf, sizeof(buf), "XXX %s", mem->name); |
| 336 |
} else if (mem->is_separator) { |
| 337 |
snprintf(buf, sizeof(buf), " --- %s ---", mem->name); |
| 338 |
} else if (mem->depsfailed == SOFT_FAILURE) { |
| 339 |
snprintf(buf, sizeof(buf), "<%s> %s", mem->enabled ? "*" : " ", mem->name); |
| 340 |
} else if (mem->conflictsfailed == SOFT_FAILURE) { |
| 341 |
snprintf(buf, sizeof(buf), "(%s) %s", mem->enabled ? "*" : " ", mem->name); |
| 342 |
} else { |
| 343 |
snprintf(buf, sizeof(buf), "[%s] %s", mem->enabled ? "*" : " ", mem->name); |
| 344 |
} |
| 345 |
waddstr(menu, buf); |
| 346 |
|
| 347 |
if (curopt + 1 == i) |
| 348 |
display_mem_info(menu, mem, start, end); |
| 349 |
|
| 350 |
if (i == end - (flags & SCROLL_DOWN ? 1 : 0)) |
| 351 |
break; |
| 352 |
} |
| 353 |
|
| 354 |
if (flags & SCROLL_DOWN) { |
| 355 |
wmove(menu, j, max_x / 2 - SCROLL_DOWN_LEFT_ADJ); |
| 356 |
waddstr(menu, SCROLL_DOWN_INDICATOR); |
| 357 |
} |
| 358 |
|
| 359 |
wmove(menu, curopt - start, (max_x / 2) - (CAT_MENU_LEFT_ADJ - 1)); |
| 360 |
wrefresh(menu); |
| 361 |
} |
| 362 |
|
| 363 |
static void play_space(void); |
| 364 |
|
| 365 |
static int move_up(int *current, int itemcount, int delta, int *start, int *end, int scroll) |
| 366 |
{ |
| 367 |
if (*current > 0) { |
| 368 |
*current = MAX(*current - delta, 0); |
| 369 |
if (*current < *start) { |
| 370 |
int diff = *start - MAX(*start - delta, 0); |
| 371 |
*start -= diff; |
| 372 |
*end -= diff; |
| 373 |
return 1; |
| 374 |
} |
| 375 |
} |
| 376 |
return 0; |
| 377 |
} |
| 378 |
|
| 379 |
static int move_down(int *current, int itemcount, int delta, int *start, int *end, int scroll) |
| 380 |
{ |
| 381 |
if (*current < itemcount) { |
| 382 |
*current = MIN(*current + delta, itemcount); |
| 383 |
if (*current > *end - 1 - (scroll & SCROLL_DOWN ? 1 : 0)) { |
| 384 |
int diff = MIN(*end + delta - 1, itemcount) - *end + 1; |
| 385 |
*start += diff; |
| 386 |
*end += diff; |
| 387 |
return 1; |
| 388 |
} |
| 389 |
} |
| 390 |
return 0; |
| 391 |
} |
| 392 |
|
| 393 |
static int run_category_menu(WINDOW *menu, int cat_num) |
| 394 |
{ |
| 395 |
struct category *cat; |
| 396 |
int i = 0; |
| 397 |
int start = 0; |
| 398 |
int end = max_y - TITLE_HEIGHT - 8; |
| 399 |
int c; |
| 400 |
int curopt = 0; |
| 401 |
int maxopt; |
| 402 |
int changed = 1; |
| 403 |
int scroll = SCROLL_NONE; |
| 404 |
|
| 405 |
AST_LIST_TRAVERSE(&categories, cat, list) { |
| 406 |
if (i++ == cat_num) |
| 407 |
break; |
| 408 |
} |
| 409 |
if (!cat) |
| 410 |
return -1; |
| 411 |
|
| 412 |
maxopt = count_members(cat) - 1; |
| 413 |
|
| 414 |
if (maxopt > end) { |
| 415 |
scroll = SCROLL_DOWN; |
| 416 |
} |
| 417 |
|
| 418 |
draw_category_menu(menu, cat, start, end, curopt, changed, scroll); |
| 419 |
|
| 420 |
while ((c = getch())) { |
| 421 |
changed = 0; |
| 422 |
switch (c) { |
| 423 |
case KEY_UP: |
| 424 |
changed = move_up(&curopt, maxopt, 1, &start, &end, scroll); |
| 425 |
break; |
| 426 |
case KEY_DOWN: |
| 427 |
changed = move_down(&curopt, maxopt, 1, &start, &end, scroll); |
| 428 |
break; |
| 429 |
case KEY_PPAGE: |
| 430 |
changed = move_up( |
| 431 |
&curopt, |
| 432 |
maxopt, |
| 433 |
MIN(PAGE_OFFSET, max_y - TITLE_HEIGHT - 6 - (scroll & SCROLL_DOWN ? 1 : 0)), |
| 434 |
&start, |
| 435 |
&end, |
| 436 |
scroll); |
| 437 |
break; |
| 438 |
case KEY_NPAGE: |
| 439 |
changed = move_down( |
| 440 |
&curopt, |
| 441 |
maxopt, |
| 442 |
MIN(PAGE_OFFSET, max_y - TITLE_HEIGHT - 6 - (scroll & SCROLL_DOWN ? 1 : 0)), |
| 443 |
&start, |
| 444 |
&end, |
| 445 |
scroll); |
| 446 |
break; |
| 447 |
case KEY_HOME: |
| 448 |
changed = move_up(&curopt, maxopt, curopt, &start, &end, scroll); |
| 449 |
break; |
| 450 |
case KEY_END: |
| 451 |
changed = move_down(&curopt, maxopt, maxopt - curopt, &start, &end, scroll); |
| 452 |
break; |
| 453 |
case KEY_LEFT: |
| 454 |
case 27: |
| 455 |
return 0; |
| 456 |
case KEY_RIGHT: |
| 457 |
case KEY_ENTER: |
| 458 |
case '\n': |
| 459 |
case ' ': |
| 460 |
toggle_enabled_index(cat, curopt); |
| 461 |
changed = 1; |
| 462 |
break; |
| 463 |
case 'y': |
| 464 |
case 'Y': |
| 465 |
set_enabled(cat, curopt); |
| 466 |
changed = 1; |
| 467 |
break; |
| 468 |
case 'n': |
| 469 |
case 'N': |
| 470 |
clear_enabled(cat, curopt); |
| 471 |
changed = 1; |
| 472 |
break; |
| 473 |
case 'h': |
| 474 |
case 'H': |
| 475 |
show_help(menu); |
| 476 |
changed = 1; |
| 477 |
break; |
| 478 |
case KEY_F(7): |
| 479 |
set_all(cat, 0); |
| 480 |
changed = 1; |
| 481 |
break; |
| 482 |
case KEY_F(8): |
| 483 |
set_all(cat, 1); |
| 484 |
changed = 1; |
| 485 |
default: |
| 486 |
break; |
| 487 |
} |
| 488 |
if (c == 'x' || c == 'X' || c == 'Q' || c == 'q') |
| 489 |
break; |
| 490 |
|
| 491 |
if (end <= maxopt) { |
| 492 |
scroll |= SCROLL_DOWN; |
| 493 |
} else { |
| 494 |
scroll &= ~SCROLL_DOWN; |
| 495 |
} |
| 496 |
|
| 497 |
draw_category_menu(menu, cat, start, end, curopt, changed, scroll); |
| 498 |
} |
| 499 |
|
| 500 |
wrefresh(menu); |
| 501 |
|
| 502 |
return c; |
| 503 |
} |
| 504 |
|
| 505 |
static void draw_title_window(WINDOW *title) |
| 506 |
{ |
| 507 |
char titlebar[strlen(menu_name) + 9]; |
| 508 |
|
| 509 |
memset(titlebar, '*', sizeof(titlebar) - 1); |
| 510 |
titlebar[sizeof(titlebar) - 1] = '\0'; |
| 511 |
wclear(title); |
| 512 |
wmove(title, 1, (max_x / 2) - (strlen(titlebar) / 2)); |
| 513 |
waddstr(title, titlebar); |
| 514 |
wmove(title, 2, (max_x / 2) - (strlen(menu_name) / 2)); |
| 515 |
waddstr(title, (char *) menu_name); |
| 516 |
wmove(title, 3, (max_x / 2) - (strlen(titlebar) / 2)); |
| 517 |
waddstr(title, titlebar); |
| 518 |
wmove(title, 5, (max_x / 2) - MENU_HELP_LEFT_ADJ); |
| 519 |
waddstr(title, MENU_HELP); |
| 520 |
wrefresh(title); |
| 521 |
} |
| 522 |
|
| 523 |
int run_menu(void) |
| 524 |
{ |
| 525 |
WINDOW *title; |
| 526 |
WINDOW *menu; |
| 527 |
int maxopt; |
| 528 |
int curopt = 0; |
| 529 |
int c; |
| 530 |
int res = 0; |
| 531 |
|
| 532 |
setenv("ESCDELAY", "0", 1); |
| 533 |
|
| 534 |
initscr(); |
| 535 |
getmaxyx(stdscr, max_y, max_x); |
| 536 |
sigaction(SIGWINCH, &winch_handler, NULL); |
| 537 |
sigaction(SIGINT, &sigint_handler, NULL); |
| 538 |
|
| 539 |
if (max_x < MIN_X || max_y < MIN_Y) { |
| 540 |
fprintf(stderr, "Terminal must be at least %d x %d.\n", MIN_X, MIN_Y); |
| 541 |
endwin(); |
| 542 |
return -1; |
| 543 |
} |
| 544 |
|
| 545 |
cbreak(); |
| 546 |
noecho(); |
| 547 |
keypad(stdscr, TRUE); |
| 548 |
clear(); |
| 549 |
refresh(); |
| 550 |
|
| 551 |
maxopt = count_categories() - 1; |
| 552 |
|
| 553 |
|
| 554 |
title = newwin(TITLE_HEIGHT, max_x, 0, 0); |
| 555 |
menu = newwin(max_y - TITLE_HEIGHT, max_x, TITLE_HEIGHT, 0); |
| 556 |
draw_title_window(title); |
| 557 |
draw_main_menu(menu, curopt); |
| 558 |
|
| 559 |
while ((c = getch())) { |
| 560 |
switch (c) { |
| 561 |
case KEY_UP: |
| 562 |
if (curopt > 0) |
| 563 |
curopt--; |
| 564 |
break; |
| 565 |
case KEY_DOWN: |
| 566 |
if (curopt < maxopt) |
| 567 |
curopt++; |
| 568 |
break; |
| 569 |
case KEY_HOME: |
| 570 |
curopt = 0; |
| 571 |
break; |
| 572 |
case KEY_END: |
| 573 |
curopt = maxopt; |
| 574 |
break; |
| 575 |
case KEY_RIGHT: |
| 576 |
case KEY_ENTER: |
| 577 |
case '\n': |
| 578 |
case ' ': |
| 579 |
c = run_category_menu(menu, curopt); |
| 580 |
break; |
| 581 |
case 'h': |
| 582 |
case 'H': |
| 583 |
show_help(menu); |
| 584 |
break; |
| 585 |
case 'i': |
| 586 |
case 'I': |
| 587 |
play_space(); |
| 588 |
draw_title_window(title); |
| 589 |
default: |
| 590 |
break; |
| 591 |
} |
| 592 |
if (c == 'q' || c == 'Q' || c == 27 || c == 3) { |
| 593 |
if (changes_made) { |
| 594 |
c = really_quit(menu); |
| 595 |
if (c == 'q') { |
| 596 |
res = -1; |
| 597 |
break; |
| 598 |
} |
| 599 |
} else { |
| 600 |
res = -1; |
| 601 |
break; |
| 602 |
} |
| 603 |
} |
| 604 |
if (c == 'x' || c == 'X' || c == 's' || c == 'S') |
| 605 |
break; |
| 606 |
draw_main_menu(menu, curopt); |
| 607 |
} |
| 608 |
|
| 609 |
endwin(); |
| 610 |
|
| 611 |
return res; |
| 612 |
} |
| 613 |
|
| 614 |
enum blip_type { |
| 615 |
BLIP_TANK = 0, |
| 616 |
BLIP_SHOT, |
| 617 |
BLIP_BOMB, |
| 618 |
BLIP_ALIEN, |
| 619 |
BLIP_BARRIER, |
| 620 |
BLIP_UFO |
| 621 |
}; |
| 622 |
|
| 623 |
struct blip { |
| 624 |
enum blip_type type; |
| 625 |
int x; |
| 626 |
int y; |
| 627 |
int ox; |
| 628 |
int oy; |
| 629 |
int goingleft; |
| 630 |
int health; |
| 631 |
AST_LIST_ENTRY(blip) entry; |
| 632 |
}; |
| 633 |
|
| 634 |
static AST_LIST_HEAD_NOLOCK(, blip) blips; |
| 635 |
|
| 636 |
static int respawn = 0; |
| 637 |
static int score = 0; |
| 638 |
static int num_aliens = 0; |
| 639 |
static int alien_sleeptime = 0; |
| 640 |
struct blip *ufo = NULL; |
| 641 |
struct blip *tank = NULL; |
| 642 |
|
| 643 |
|
| 644 |
#define BOMB_PROB 1 |
| 645 |
|
| 646 |
static int add_barrier(int x, int y) |
| 647 |
{ |
| 648 |
struct blip *cur = NULL; |
| 649 |
|
| 650 |
cur = calloc(1,sizeof(struct blip)); |
| 651 |
if(!cur) { |
| 652 |
return -1; |
| 653 |
} |
| 654 |
cur->type=BLIP_BARRIER; |
| 655 |
cur->x = x; |
| 656 |
cur->y=max_y - y; |
| 657 |
cur->health = 1; |
| 658 |
AST_LIST_INSERT_HEAD(&blips, cur,entry); |
| 659 |
return 0; |
| 660 |
} |
| 661 |
|
| 662 |
static int init_blips(void) |
| 663 |
{ |
| 664 |
int i, j; |
| 665 |
struct blip *cur; |
| 666 |
int offset = 4; |
| 667 |
|
| 668 |
srandom(time(NULL) + getpid()); |
| 669 |
|
| 670 |
|
| 671 |
cur = calloc(1, sizeof(struct blip)); |
| 672 |
if (!cur) |
| 673 |
return -1; |
| 674 |
cur->type = BLIP_TANK; |
| 675 |
cur->x = max_x / 2; |
| 676 |
cur->y = max_y - 1; |
| 677 |
AST_LIST_INSERT_HEAD(&blips, cur, entry); |
| 678 |
tank = cur; |
| 679 |
|
| 680 |
|
| 681 |
num_aliens = 0; |
| 682 |
for (i = 0; i < 3; i++) { |
| 683 |
for (j = 0; j < 10; j++) { |
| 684 |
cur = calloc(1, sizeof(struct blip)); |
| 685 |
if (!cur) |
| 686 |
return -1; |
| 687 |
cur->type = BLIP_ALIEN; |
| 688 |
cur->x = (j * 2) + 1; |
| 689 |
cur->y = (i * 2) + 2; |
| 690 |
AST_LIST_INSERT_HEAD(&blips, cur, entry); |
| 691 |
num_aliens++; |
| 692 |
} |
| 693 |
} |
| 694 |
for(i=0; i < 4; i++) { |
| 695 |
if (i > 0) |
| 696 |
offset += 5 + ((max_x) -28) / 3; |
| 697 |
add_barrier(offset + 1, 6); |
| 698 |
add_barrier(offset + 2, 6); |
| 699 |
add_barrier(offset + 3, 6); |
| 700 |
|
| 701 |
add_barrier(offset, 5); |
| 702 |
add_barrier(offset + 1, 5); |
| 703 |
add_barrier(offset + 2, 5); |
| 704 |
add_barrier(offset + 3, 5); |
| 705 |
add_barrier(offset + 4, 5); |
| 706 |
|
| 707 |
add_barrier(offset, 4); |
| 708 |
add_barrier(offset + 1, 4); |
| 709 |
add_barrier(offset + 3, 4); |
| 710 |
add_barrier(offset + 4, 4); |
| 711 |
} |
| 712 |
return 0; |
| 713 |
} |
| 714 |
|
| 715 |
static inline chtype type2chtype(enum blip_type type) |
| 716 |
{ |
| 717 |
switch (type) { |
| 718 |
case BLIP_TANK: |
| 719 |
return 'A'; |
| 720 |
case BLIP_ALIEN: |
| 721 |
return 'X'; |
| 722 |
case BLIP_SHOT: |
| 723 |
return '|'; |
| 724 |
case BLIP_BOMB: |
| 725 |
return 'o'; |
| 726 |
case BLIP_BARRIER: |
| 727 |
return '*'; |
| 728 |
case BLIP_UFO: |
| 729 |
return '@'; |
| 730 |
default: |
| 731 |
break; |
| 732 |
} |
| 733 |
return '?'; |
| 734 |
} |
| 735 |
|
| 736 |
static int repaint_screen(void) |
| 737 |
{ |
| 738 |
struct blip *cur; |
| 739 |
|
| 740 |
wmove(stdscr, 0, 0); |
| 741 |
wprintw(stdscr, "Score: %d", score); |
| 742 |
|
| 743 |
AST_LIST_TRAVERSE(&blips, cur, entry) { |
| 744 |
if (cur->x != cur->ox || cur->y != cur->oy) { |
| 745 |
wmove(stdscr, cur->oy, cur->ox); |
| 746 |
waddch(stdscr, ' '); |
| 747 |
wmove(stdscr, cur->y, cur->x); |
| 748 |
waddch(stdscr, type2chtype(cur->type)); |
| 749 |
cur->ox = cur->x; |
| 750 |
cur->oy = cur->y; |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
wmove(stdscr, 0, max_x - 1); |
| 755 |
|
| 756 |
wrefresh(stdscr); |
| 757 |
|
| 758 |
return 0; |
| 759 |
} |
| 760 |
|
| 761 |
static int tank_move_left(void) |
| 762 |
{ |
| 763 |
if (tank->x > 0) |
| 764 |
tank->x--; |
| 765 |
|
| 766 |
return 0; |
| 767 |
} |
| 768 |
|
| 769 |
static int tank_move_right(void) |
| 770 |
{ |
| 771 |
if (tank->x < (max_x - 1)) |
| 772 |
tank->x++; |
| 773 |
|
| 774 |
return 0; |
| 775 |
} |
| 776 |
|
| 777 |
static int count_shots(void) |
| 778 |
{ |
| 779 |
struct blip *cur; |
| 780 |
int count = 0; |
| 781 |
|
| 782 |
AST_LIST_TRAVERSE(&blips, cur, entry) { |
| 783 |
if (cur->type == BLIP_SHOT) |
| 784 |
count++; |
| 785 |
} |
| 786 |
|
| 787 |
return count; |
| 788 |
} |
| 789 |
|
| 790 |
static int tank_shoot(void) |
| 791 |
{ |
| 792 |
struct blip *shot; |
| 793 |
|
| 794 |
if (count_shots() == 3) |
| 795 |
return 0; |
| 796 |
|
| 797 |
score--; |
| 798 |
|
| 799 |
shot = calloc(1, sizeof(struct blip)); |
| 800 |
if (!shot) |
| 801 |
return -1; |
| 802 |
shot->type = BLIP_SHOT; |
| 803 |
shot->x = tank->x; |
| 804 |
shot->y = max_y - 2; |
| 805 |
AST_LIST_INSERT_HEAD(&blips, shot, entry); |
| 806 |
|
| 807 |
return 0; |
| 808 |
} |
| 809 |
|
| 810 |
static int remove_blip(struct blip *blip) |
| 811 |
{ |
| 812 |
if (!blip) { |
| 813 |
return -1; |
| 814 |
} |
| 815 |
|
| 816 |
AST_LIST_REMOVE(&blips, blip, entry); |
| 817 |
|
| 818 |
if (blip->type == BLIP_ALIEN) { |
| 819 |
num_aliens--; |
| 820 |
} |
| 821 |
wmove(stdscr, blip->oy, blip->ox); |
| 822 |
waddch(stdscr, ' '); |
| 823 |
free(blip); |
| 824 |
|
| 825 |
return 0; |
| 826 |
} |
| 827 |
|
| 828 |
static int move_aliens(void) |
| 829 |
{ |
| 830 |
struct blip *cur; |
| 831 |
struct blip *current_barrier; |
| 832 |
|
| 833 |
AST_LIST_TRAVERSE(&blips, cur, entry) { |
| 834 |
if (cur->type != BLIP_ALIEN) { |
| 835 |
|
| 836 |
continue; |
| 837 |
} |
| 838 |
if (cur->goingleft && (cur->x == 0)) { |
| 839 |
cur->y++; |
| 840 |
cur->goingleft = 0; |
| 841 |
} else if (!cur->goingleft && cur->x == (max_x - 1)) { |
| 842 |
cur->y++; |
| 843 |
cur->goingleft = 1; |
| 844 |
} else if (cur->goingleft) { |
| 845 |
cur->x--; |
| 846 |
} else { |
| 847 |
cur->x++; |
| 848 |
} |
| 849 |
|
| 850 |
if (cur->x == tank->x && cur->y == tank->y) |
| 851 |
return 1; |
| 852 |
AST_LIST_TRAVERSE(&blips, current_barrier, entry){ |
| 853 |
if(current_barrier->type!=BLIP_BARRIER) |
| 854 |
continue; |
| 855 |
if(cur->y == current_barrier->y && cur->x == current_barrier -> x) |
| 856 |
remove_blip(current_barrier); |
| 857 |
} |
| 858 |
if (random() % 100 < BOMB_PROB && cur->y != max_y) { |
| 859 |
struct blip *bomb = calloc(1, sizeof(struct blip)); |
| 860 |
if (!bomb) |
| 861 |
continue; |
| 862 |
bomb->type = BLIP_BOMB; |
| 863 |
bomb->x = cur->x; |
| 864 |
bomb->y = cur->y + 1; |
| 865 |
AST_LIST_INSERT_HEAD(&blips, bomb, entry); |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
return 0; |
| 870 |
} |
| 871 |
|
| 872 |
static int move_bombs(void) |
| 873 |
{ |
| 874 |
struct blip *cur; |
| 875 |
struct blip *current_barrier; |
| 876 |
|
| 877 |
AST_LIST_TRAVERSE(&blips, cur, entry) { |
| 878 |
int mark = 0; |
| 879 |
if (cur->type != BLIP_BOMB) |
| 880 |
continue; |
| 881 |
cur->y++; |
| 882 |
if (cur->x == tank->x && cur->y == tank->y) { |
| 883 |
return 1; |
| 884 |
} |
| 885 |
|
| 886 |
AST_LIST_TRAVERSE(&blips, current_barrier, entry) { |
| 887 |
if (current_barrier->type != BLIP_BARRIER) |
| 888 |
continue; |
| 889 |
if (cur->x == current_barrier->x && cur->y == current_barrier->y) { |
| 890 |
mark = 1; |
| 891 |
current_barrier->health--; |
| 892 |
if (current_barrier->health == 0) |
| 893 |
remove_blip(current_barrier); |
| 894 |
} |
| 895 |
} |
| 896 |
if (mark){ |
| 897 |
remove_blip(cur);} |
| 898 |
} |
| 899 |
|
| 900 |
return 0; |
| 901 |
} |
| 902 |
|
| 903 |
static void move_shots(void) |
| 904 |
{ |
| 905 |
struct blip *cur; |
| 906 |
|
| 907 |
AST_LIST_TRAVERSE(&blips, cur, entry) { |
| 908 |
if (cur->type != BLIP_SHOT) |
| 909 |
continue; |
| 910 |
cur->y--; |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
static int ufo_action() |
| 916 |
{ |
| 917 |
struct blip *cur; |
| 918 |
|
| 919 |
AST_LIST_TRAVERSE(&blips, cur, entry) { |
| 920 |
if (cur->type != BLIP_UFO) { |
| 921 |
continue; |
| 922 |
} |
| 923 |
|
| 924 |
cur->x--; |
| 925 |
|
| 926 |
if (cur->x < 0) { |
| 927 |
remove_blip(cur); |
| 928 |
respawn += 1; |
| 929 |
} |
| 930 |
|
| 931 |
} |
| 932 |
|
| 933 |
if (respawn == 7) { |
| 934 |
respawn = 0; |
| 935 |
|
| 936 |
cur = calloc(1, sizeof(struct blip)); |
| 937 |
if(!cur) |
| 938 |
return -1; |
| 939 |
cur->type = BLIP_UFO; |
| 940 |
cur->x = max_x - 1; |
| 941 |
cur->y = 1; |
| 942 |
AST_LIST_INSERT_HEAD(&blips, cur, entry); |
| 943 |
} |
| 944 |
|
| 945 |
return 0; |
| 946 |
} |
| 947 |
|
| 948 |
static void game_over(int win) |
| 949 |
{ |
| 950 |
clear(); |
| 951 |
|
| 952 |
wmove(stdscr, max_y / 2, max_x / 2 - 10); |
| 953 |
wprintw(stdscr, "Game over! You %s!", win ? "win" : "lose"); |
| 954 |
|
| 955 |
wmove(stdscr, 0, max_x - 1); |
| 956 |
|
| 957 |
wrefresh(stdscr); |
| 958 |
|
| 959 |
sleep(1); |
| 960 |
|
| 961 |
while (getch() != ' '); |
| 962 |
|
| 963 |
return; |
| 964 |
} |
| 965 |
|
| 966 |
static int check_shot(struct blip *shot) |
| 967 |
{ |
| 968 |
struct blip *cur; |
| 969 |
|
| 970 |
AST_LIST_TRAVERSE(&blips, cur, entry) { |
| 971 |
if ((cur->type == BLIP_ALIEN || cur->type == BLIP_UFO) && cur->x == shot->x && cur->y == shot->y){ |
| 972 |
if (cur->type == BLIP_UFO) { |
| 973 |
score += 80; |
| 974 |
} |
| 975 |
score += 20; |
| 976 |
remove_blip(cur); |
| 977 |
remove_blip(shot); |
| 978 |
respawn += 1; |
| 979 |
if (!num_aliens) { |
| 980 |
if(alien_sleeptime < 101) { |
| 981 |
game_over(1); |
| 982 |
return 1; |
| 983 |
} else { |
| 984 |
alien_sleeptime = alien_sleeptime - 100; |
| 985 |
return 1; |
| 986 |
} |
| 987 |
} |
| 988 |
break; |
| 989 |
} |
| 990 |
if (cur->type == BLIP_BARRIER) { |
| 991 |
if (shot->x == cur->x && shot->y == cur->y) { |
| 992 |
remove_blip(cur); |
| 993 |
remove_blip(shot); |
| 994 |
break; |
| 995 |
} |
| 996 |
} |
| 997 |
} |
| 998 |
|
| 999 |
return 0; |
| 1000 |
} |
| 1001 |
|
| 1002 |
static int check_placement(void) |
| 1003 |
{ |
| 1004 |
struct blip *cur; |
| 1005 |
|
| 1006 |
AST_LIST_TRAVERSE_SAFE_BEGIN(&blips, cur, entry) { |
| 1007 |
if (cur->y <= 0 || cur->y >= max_y) { |
| 1008 |
AST_LIST_REMOVE_CURRENT(&blips, entry); |
| 1009 |
remove_blip(cur); |
| 1010 |
} else if (cur->type == BLIP_SHOT && check_shot(cur)) |
| 1011 |
return 1; |
| 1012 |
} |
| 1013 |
AST_LIST_TRAVERSE_SAFE_END |
| 1014 |
|
| 1015 |
return 0; |
| 1016 |
} |
| 1017 |
|
| 1018 |
static void play_space(void) |
| 1019 |
{ |
| 1020 |
int c; |
| 1021 |
unsigned int jiffies = 1; |
| 1022 |
int quit = 0; |
| 1023 |
struct blip *blip; |
| 1024 |
alien_sleeptime = 1000; |
| 1025 |
score = 0; |
| 1026 |
|
| 1027 |
while(alien_sleeptime > 100) { |
| 1028 |
|
| 1029 |
jiffies = 1; |
| 1030 |
clear(); |
| 1031 |
nodelay(stdscr, TRUE); |
| 1032 |
init_blips(); |
| 1033 |
repaint_screen(); |
| 1034 |
|
| 1035 |
for (;;) { |
| 1036 |
c = getch(); |
| 1037 |
switch (c) { |
| 1038 |
case ' ': |
| 1039 |
tank_shoot(); |
| 1040 |
break; |
| 1041 |
case KEY_LEFT: |
| 1042 |
tank_move_left(); |
| 1043 |
break; |
| 1044 |
case KEY_RIGHT: |
| 1045 |
tank_move_right(); |
| 1046 |
break; |
| 1047 |
case 'x': |
| 1048 |
case 'X': |
| 1049 |
case 'q': |
| 1050 |
case 'Q': |
| 1051 |
quit = 1; |
| 1052 |
default: |
| 1053 |
|
| 1054 |
break; |
| 1055 |
} |
| 1056 |
if (quit) { |
| 1057 |
alien_sleeptime = 1; |
| 1058 |
break; |
| 1059 |
} |
| 1060 |
if (!(jiffies % 25)) { |
| 1061 |
if (move_aliens() || move_bombs() || ufo_action()) { |
| 1062 |
alien_sleeptime = 1; |
| 1063 |
game_over(0); |
| 1064 |
break; |
| 1065 |
} |
| 1066 |
if (check_placement()) |
| 1067 |
break; |
| 1068 |
} |
| 1069 |
if (!(jiffies % 10)) { |
| 1070 |
move_shots(); |
| 1071 |
if (check_placement()) |
| 1072 |
break; |
| 1073 |
} |
| 1074 |
repaint_screen(); |
| 1075 |
jiffies++; |
| 1076 |
usleep(alien_sleeptime); |
| 1077 |
} |
| 1078 |
|
| 1079 |
while ((blip = AST_LIST_REMOVE_HEAD(&blips, entry))) |
| 1080 |
free(blip); |
| 1081 |
} |
| 1082 |
|
| 1083 |
nodelay(stdscr, FALSE); |
| 1084 |
} |