struct result selection_add_item(struct selection *selection, char const *description, selection_action_fn *action) { if (!selection) return result_set_system_error(EINVAL); if (!description) return result_set_system_error(EINVAL); if (!description[0]) return result_set_system_error(EINVAL); int index = selection->items_count; int null_index = index + 1; ++selection->items_count; selection->items = reallocarray_or_die(selection->items, selection->items_count + 1, sizeof(ITEM *)); char name[] = "1"; name[0] += index; char *name_dup = strdup_or_die(name); char *description_dup = strdup_or_die(description); selection->items[index] = new_item(name_dup, description_dup); if (!selection->items[index]) { free_or_die(description_dup); free_or_die(name_dup); return result_system_error(); } struct selection_item *selection_item = calloc_or_die(1, sizeof(struct selection_item)); selection_item->action = action; set_item_userptr(selection->items[index], selection_item); selection->items[null_index] = NULL; return result_success(); }
int mainmenu(int height, int width) { int ret; ITEM **menu_items; MENU *main_menu; int n_choices, i; clean_main_menu(); build_main_menu(instance_path); n_choices = menu_size; menu_items = (ITEM **) calloc(n_choices + 1, sizeof (ITEM *)); for (i = 0; i < n_choices; ++i) { menu_items[i] = new_item(menu_values[i], menu_values[i]); set_item_userptr(menu_items[i], (void *) menu_selected); } menu_items[n_choices] = (ITEM *) NULL; main_menu = new_menu((ITEM **) menu_items); menu_opts_off(main_menu, O_SHOWDESC); set_menu_sub(main_menu, derwin(stdscr, 10, 50, 6, 10)); post_menu(main_menu); attron(A_BOLD); mvprintw(0, 0, name); mvprintw(0, width - strlen(vers), vers); attroff(A_BOLD); refresh(); pos_menu_cursor(main_menu); while ((i = getch()) != KEY_F(4)) { switch (i) { case KEY_DOWN: menu_driver(main_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(main_menu, REQ_UP_ITEM); break; case 10: { ITEM *cur; int (*p)(char *); cur = current_item(main_menu); p = (int (*)(char *))item_userptr(cur); ret = p((char *) item_name(cur)); pos_menu_cursor(main_menu); goto menu_sel; } } } if (i == KEY_F(4)) { ret = menu_size - 1; } menu_sel: unpost_menu(main_menu); free_menu(main_menu); for (i = 0; i < n_choices; ++i) free_item(menu_items[i]); return ret; }
void displayMenu(int y, int x, char title[],int numOfOpt, char *options[], void (*p[])(void)){ int i,c; WINDOW *menuWindow; MENU *myMenu; ITEM **myOptions; ITEM *currentOption; initscr(); noecho(); cbreak(); keypad(stdscr,TRUE); init_pair(1,COLOR_CYAN,COLOR_BLACK); myOptions=(ITEM **)calloc(numOfOpt+1,sizeof(ITEM *)); for(i=0;i<numOfOpt;i++){ myOptions[i]=new_item(options[i]," "); set_item_userptr(myOptions[i],p[i]); } myOptions[numOfOpt]=(ITEM *)NULL; myMenu=new_menu(myOptions); menuWindow = newwin(8,20,(LINES-y)/2,(COLS-x)/2); keypad(menuWindow,TRUE); set_menu_win(myMenu,menuWindow); set_menu_sub(myMenu, derwin(menuWindow,y-4,x-2,3,1)); set_menu_format(myMenu,numOfOpt,1); menu_opts_off(myMenu, O_SHOWDESC); set_menu_mark(myMenu," * "); post_menu(myMenu); wrefresh(menuWindow); while((c=wgetch(menuWindow))!=KEY_F(2)){ switch(c){ case KEY_UP: menu_driver(myMenu,REQ_UP_ITEM); break; case KEY_DOWN: menu_driver(myMenu,REQ_DOWN_ITEM); break; case 10:{ ITEM *temp; temp=current_item(myMenu); void(*pointer)(void); pointer=item_userptr(temp); pointer(); pos_menu_cursor(myMenu); menu_driver(myMenu,REQ_DOWN_ITEM); break; } wrefresh(menuWindow); } } /*unpost_menu(myMenu);*/ /*for(i=0;i<numOfOpt;++i){*/ /*free_item(myOptions[i]);*/ /*}*/ /*free_menu(myMenu);*/ /*free(optionsNumbers);*/ }
/* * Create a menu of technologies: a selectable list of technologies. * @param jobj format of the json object: [ [ "\/net\/connman\/technology\/wifi", { "Name":"WiFi", "Type":"wifi", "Powered":true, "Connected":false, "Tethering":false } ], [ "\/net\/connman\/technology\/ethernet", { "Name":"Wired", "Type":"ethernet", "Powered":true, "Connected":true, "Tethering":false } ] ] */ static void renderers_technologies(struct json_object *jobj) { int i; char *desc_base = "%-20s Powered %-5s Connected %-5s"; char desc_base_sub[30]; const char *k_name, *k_type, *k_powered, *k_connected; char *desc, *tech_short_name; struct json_object *sub_array, *dbus_tech_name, *tech_dict; struct userptr_data *data; nb_items = json_object_array_length(jobj); main_items = malloc(sizeof(ITEM*) * (nb_items+1)); assert(main_items != NULL && nb_items > 0); for (i = 0; i < nb_items; i++) { sub_array = json_object_array_get_idx(jobj, i); if (!sub_array) continue; dbus_tech_name = json_object_array_get_idx(sub_array, 0); tech_dict = json_object_array_get_idx(sub_array, 1); json_object_object_foreach(tech_dict, key, val) { if (strcmp(key_serv_name, key) == 0) k_name = json_object_get_string(val); else if (strcmp("Type", key) == 0) k_type = json_object_get_string(val); else if (strcmp("Powered", key) == 0) k_powered = json_object_to_json_string(val); else if (strcmp("Connected", key) == 0) k_connected = json_object_to_json_string(val); } snprintf(desc_base_sub, 30, "%s (%s)", k_name, k_type); desc_base_sub[29] = '\0'; desc = malloc(RENDERERS_STRING_MAX_LEN); assert(desc != NULL); snprintf(desc, RENDERERS_STRING_MAX_LEN-1, desc_base, desc_base_sub, k_powered, k_connected); desc[RENDERERS_STRING_MAX_LEN-1] = '\0'; tech_short_name = extract_dbus_short_name(json_object_get_string(dbus_tech_name)); main_items[i] = new_item(tech_short_name, desc); data = malloc(sizeof(struct userptr_data)); assert(data != NULL); data->dbus_name = strdup(json_object_get_string(dbus_tech_name)); data->pretty_name = strdup(k_name); set_item_userptr(main_items[i], data); } main_items[nb_items] = NULL; main_menu = new_menu(main_items); set_menu_win(main_menu, win_body); set_menu_sub(main_menu, derwin(win_body, win_body_lines-2, COLS-4, 3, 2)); set_menu_mark(main_menu, ""); set_menu_format(main_menu, win_body_lines-3, 1); assert(post_menu(main_menu) == E_OK); refresh_home_msg(); wrefresh(win_header); wrefresh(win_body); repos_cursor(); }
void viewListPlayers(FootBallGame::TeamNumber teamNumber) { String teamName; int nChoices; ITEM **items; const char *itemName; const ITEM *currentItem; LinkedList<String*> names = LinkedList<String*>(deleteString); LinkedList<int*> pids = LinkedList<int*>(deleteInt); const int window_nlines = 10, window_ncols = 90; for(;;) { // Create new window. WINDOW *window = newwin(window_nlines, window_ncols, headerWindow_begin_y + headerWindow_nlines + 1, 0); // Set new window options. keypad(window, TRUE); // Set up the items of menu. FootBallTeam *team = game.getFootBallTeam(teamNumber); if (team != NULL) { nChoices = team->getPlayerCount() + 2; items = new ITEM* [nChoices]; TeamIterator iterator, end; team->begin(iterator); team->end(end); for (int i = 0; iterator != end; i++, iterator++) { Player *player = *iterator; String *playerName = new String; int *pid = new int; player->getName(*playerName); *pid = player->getPid(); names.append(playerName); pids.append(pid); items[i] = new_item(playerName->c_str(), NULL); set_item_userptr(items[i], pid); } } else { nChoices = 2; items = new ITEM* [nChoices]; } items[nChoices-2] = new_item("Exit", NULL); items[nChoices-1] = NULL; // Create the menu MENU *menu = new_menu((ITEM **)items); set_menu_win(menu, window); set_menu_sub(menu, derwin(window, 10, 80, 0, 10)); set_menu_mark(menu, NULL); // Do not Show the item descritpions menu_opts_off(menu, O_SHOWDESC); post_menu(menu); wrefresh(window); refresh(); int c; bool restart = false; while (!restart && (c = wgetch(window))) { switch (c) { case KEY_DOWN: menu_driver(menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(menu, REQ_UP_ITEM); break; case 10: // Enter currentItem = current_item(menu); itemName = item_name(currentItem); if (itemName != NULL) { if (strcmp(itemName, "Exit") == 0) { // Delete allocated data unpost_menu(menu); free_menu(menu); for (int i = 0; i < nChoices-1; i++) free_item(items[i]); delete[] items; delwin(window); return; } else { int *pid = (int*)item_userptr(currentItem); // Delete allocated data unpost_menu(menu); free_menu(menu); for (int i = 0; i < nChoices-1; i++) free_item(items[i]); delete[] items; delwin(window); viewPlayer(team, itemName, *pid); restart = true; } } } } } }
void column_select_create(ui_t *ui) { int attr_id, column; MENU *menu; column_select_info_t *info; // Cerate a new indow for the panel and form ui_panel_create(ui, 20, 60); // Initialize Filter panel specific data info = sng_malloc(sizeof(column_select_info_t)); // Store it into panel userptr set_panel_userptr(ui->panel, (void*) info); // Initialize the fields info->fields[FLD_COLUMNS_ACCEPT] = new_field(1, 10, ui->height - 2, 13, 0, 0); info->fields[FLD_COLUMNS_SAVE] = new_field(1, 10, ui->height - 2, 25, 0, 0); info->fields[FLD_COLUMNS_CANCEL] = new_field(1, 10, ui->height - 2, 37, 0, 0); info->fields[FLD_COLUMNS_COUNT] = NULL; // Field Labels set_field_buffer(info->fields[FLD_COLUMNS_ACCEPT], 0, "[ Accept ]"); set_field_buffer(info->fields[FLD_COLUMNS_SAVE], 0, "[ Save ]"); set_field_buffer(info->fields[FLD_COLUMNS_CANCEL], 0, "[ Cancel ]"); // Create the form and post it info->form = new_form(info->fields); set_form_sub(info->form, ui->win); post_form(info->form); // Create a subwin for the menu area info->menu_win = derwin(ui->win, 10, ui->width - 2, 7, 0); // Initialize one field for each attribute for (attr_id = 0; attr_id < SIP_ATTR_COUNT; attr_id++) { // Create a new field for this column info->items[attr_id] = new_item("[ ]", sip_attr_get_description(attr_id)); set_item_userptr(info->items[attr_id], (void*) sip_attr_get_name(attr_id)); } info->items[SIP_ATTR_COUNT] = NULL; // Create the columns menu and post it info->menu = menu = new_menu(info->items); // Set current enabled fields // FIXME Stealing Call list columns :/ call_list_info_t *list_info = call_list_info(ui_find_by_type(PANEL_CALL_LIST)); // Enable current enabled fields and move them to the top for (column = 0; column < list_info->columncnt; column++) { const char *attr = list_info->columns[column].attr; for (attr_id = 0; attr_id < item_count(menu); attr_id++) { if (!strcmp(item_userptr(info->items[attr_id]), attr)) { column_select_toggle_item(ui, info->items[attr_id]); column_select_move_item(ui, info->items[attr_id], column); break; } } } // Set main window and sub window set_menu_win(menu, ui->win); set_menu_sub(menu, derwin(ui->win, 10, ui->width - 5, 7, 2)); set_menu_format(menu, 10, 1); set_menu_mark(menu, ""); set_menu_fore(menu, COLOR_PAIR(CP_DEF_ON_BLUE)); menu_opts_off(menu, O_ONEVALUE); post_menu(menu); // Draw a scrollbar to the right info->scroll = ui_set_scrollbar(info->menu_win, SB_VERTICAL, SB_RIGHT); info->scroll.max = item_count(menu) - 1; ui_scrollbar_draw(info->scroll); // Set the window title and boxes mvwprintw(ui->win, 1, ui->width / 2 - 14, "Call List columns selection"); wattron(ui->win, COLOR_PAIR(CP_BLUE_ON_DEF)); title_foot_box(ui->panel); mvwhline(ui->win, 6, 1, ACS_HLINE, ui->width - 1); mvwaddch(ui->win, 6, 0, ACS_LTEE); mvwaddch(ui->win, 6, ui->width - 1, ACS_RTEE); wattroff(ui->win, COLOR_PAIR(CP_BLUE_ON_DEF)); // Some brief explanation abotu what window shows wattron(ui->win, COLOR_PAIR(CP_CYAN_ON_DEF)); mvwprintw(ui->win, 3, 2, "This windows show the list of columns displayed on Call"); mvwprintw(ui->win, 4, 2, "List. You can enable/disable using Space Bar and reorder"); mvwprintw(ui->win, 5, 2, "them using + and - keys."); wattroff(ui->win, COLOR_PAIR(CP_CYAN_ON_DEF)); info->form_active = 0; }
/* Load items from sp struct to menu */ static int msgloader_loaditems(smsgloader *_this,shitemsgparser *sp) { int i; VERBOSE_DEBUGPR("scom menu loading items\n"); /* Release and unpost menu before trying to load new items to sp */ if(_this->loaditems) _this->release_menu(_this); if(sp->load_msgs(sp)) { char *nomsgs; DEBUGPR("Failed to load messages (no message file in ./nibbles ?)!\n"); nomsgs=malloc(20); strcpy(nomsgs,"no messages"); if(!nomsgs) { DEBUGPR("ALLOC FAILED! %s:%d\n",__FILE__,__LINE__); return -1; } _this->loaditems=calloc(2,sizeof(ITEM *)); if(!_this->loaditems) { free(nomsgs); DEBUGPR("ALLOC FAILED! %s:%d\n",__FILE__,__LINE__); return -1; } _this->loaditems[0]=new_item(nomsgs,nomsgs); if(!_this->loaditems[0]) { free(_this->loaditems); _this->loaditems=NULL; DEBUGPR("ALLOC FAILED! %s:%d\n",__FILE__,__LINE__); return -1; } _this->loaditems[1]=NULL; _this->itemamnt=1; } else { /* This forms a tight binding between data hold by sp, and data in menu... It is nasty I know, but on the other hand, I do not want to allocate space for dublicate information.. So lets just try to invent a way to sync menu showing&access and message data updates... */ void *msglistitemhandle; int itemamnt=sp->loaded_msg_amnt(sp); char *msgname; VERBOSE_DEBUGPR("scommenu - messages loaded to sp\n"); _this->loaditems=calloc(itemamnt+1,sizeof(ITEM *)); if(!_this->loaditems) { DEBUGPR("Calloc FAILED! %s:%d\n",__FILE__,__LINE__); return -1; } _this->itemhiddens=calloc(itemamnt,sizeof(void *)); if(!_this->itemhiddens) { free(_this->loaditems); DEBUGPR("Calloc FAILED! %s:%d\n",__FILE__,__LINE__); return -1; } msglistitemhandle=sp->get_first_msgitem(sp); msgname=sp->get_matching_name(sp,msglistitemhandle); for(i=0;i<itemamnt && (msgname=sp->get_matching_name(sp,msglistitemhandle));i++,msglistitemhandle=sp->get_next_msgitem(sp,msglistitemhandle)) { VERBOSE_DEBUGPR("scommenu - creating msg item %d for msg '%s'\n",i,msgname); _this->loaditems[i]=new_item(msgname,sp->get_matching_desc(sp,msglistitemhandle)); VERBOSE_DEBUGPR("scommenu - storing msglistitemhandle %p for msg item\n",msglistitemhandle); set_item_userptr(_this->loaditems[i],msglistitemhandle); } _this->loaditems[i]=NULL; if(i<itemamnt) { DEBUGPR("Odd,Item amount (%d) and amount of found message names (%d) not same!\n",itemamnt,i); } _this->itemamnt=i; VERBOSE_DEBUGPR("scommenu - FOUND %d messages!\n",_this->itemamnt); } return 0; }
void createMenus() { int i; /* create menu items */ mainMenuItems = (ITEM**)calloc(5, sizeof(ITEM*)); mainMenuItems[0] = new_item("Operating Mode", ""); set_item_userptr(mainMenuItems[0], (void*)MENU_ITEM_OPMODE); mainMenuItems[1] = new_item("Spectra", ""); set_item_userptr(mainMenuItems[1], (void*)MENU_ITEM_SPECTRA); mainMenuItems[2] = new_item("Invariants", ""); set_item_userptr(mainMenuItems[2], (void*)MENU_ITEM_INVARIANTS); mainMenuItems[3] = new_item("Exit", ""); set_item_userptr(mainMenuItems[3], (void*)MENU_ITEM_EXIT); mainMenuItems[4] = new_item(NULL, NULL); opmodeMenuItems = (ITEM**)calloc(3, sizeof(ITEM*)); opmodeMenuItems[0] = new_item("Training", ""); set_item_userptr(opmodeMenuItems[0], (void*)MENU_ITEM_TRAINING); opmodeMenuItems[1] = new_item("Testing", ""); set_item_userptr(opmodeMenuItems[1], (void*)MENU_ITEM_TESTING); opmodeMenuItems[2] = new_item(NULL, NULL); spectraMenuItems = (ITEM**)calloc(guiProgramData->nSpectra+1, sizeof(ITEM*)); for(i=0; i<guiProgramData->nSpectra; i++) { char *spectrumInfo = (char*)malloc(64); sprintf(spectrumInfo, "%-20s | %5d", getSpectrum(guiProgramData, i)->name, getSpectrum(guiProgramData, i)->nComponents); spectraMenuItems[i] = new_item(spectrumInfo, ""); } spectraOpMenuItems = (ITEM**)calloc(3, sizeof(ITEM*)); spectraOpMenuItems[0] = new_item("Inspect data", ""); set_item_userptr(spectraOpMenuItems[0], (void*)MENU_ITEM_DATA); spectraOpMenuItems[1] = new_item("Perform SFL analysis", ""); set_item_userptr(spectraOpMenuItems[1], (void*)MENU_ITEM_SFL); spectraOpMenuItems[2] = new_item(NULL, NULL); sflCoeffMenuItems = (ITEM**)calloc(3, sizeof(ITEM*)); sflCoeffMenuItems[0] = new_item("Ochiai", ""); set_item_userptr(sflCoeffMenuItems[0], (void*)S_OCHIAI); sflCoeffMenuItems[1] = new_item("Jaccard", ""); set_item_userptr(sflCoeffMenuItems[1], (void*)S_JACCARD); sflCoeffMenuItems[2] = new_item("Tarantula", ""); set_item_userptr(sflCoeffMenuItems[1], (void*)S_TARANTULA); sflCoeffMenuItems[3] = new_item(NULL, NULL); invariantTypesMenuItems = (ITEM**)calloc(guiProgramData->nInvariantTypes+1, sizeof(ITEM*)); for(i=0; i<guiProgramData->nInvariantTypes; i++) { char *invariantTypeInfo = (char*)malloc(64); sprintf(invariantTypeInfo, "%-20s | %5d", getInvariantType(guiProgramData, i)->name, getInvariantType(guiProgramData, i)->nInvariants); invariantTypesMenuItems[i] = new_item(invariantTypeInfo, ""); } /*create actual menus*/ mainMenu = new_menu((ITEM**)mainMenuItems); set_menu_win(mainMenu, mainMenuWin); set_menu_sub(mainMenu, derwin(mainMenuWin, 10,16, 1,1)); set_menu_format(mainMenu, 10,1); box(mainMenuWin, 0, 0); post_menu(mainMenu); opmodeMenu = new_menu((ITEM**)opmodeMenuItems); set_menu_win(opmodeMenu, opmodeMenuWin); set_menu_format(opmodeMenu, 10,1); post_menu(opmodeMenu); spectraMenu = new_menu((ITEM**)spectraMenuItems); set_menu_win(spectraMenu, spectraMenuWin); set_menu_format(spectraMenu, 10,1); post_menu(spectraMenu); spectraOpMenu = new_menu((ITEM**)spectraOpMenuItems); set_menu_win(spectraOpMenu, spectraOpMenuWin); set_menu_format(spectraOpMenu, 10,1); post_menu(spectraOpMenu); sflCoeffMenu = new_menu((ITEM**)sflCoeffMenuItems); set_menu_win(sflCoeffMenu, sflCoeffMenuWin); set_menu_format(sflCoeffMenu, 10,1); post_menu(sflCoeffMenu); invariantTypesMenu = new_menu((ITEM**)invariantTypesMenuItems); set_menu_win(invariantTypesMenu, invariantTypesMenuWin); set_menu_format(invariantTypesMenu, 10,1); post_menu(invariantTypesMenu); }
int main() { ITEM **my_items; int c; MENU *my_menu; WINDOW *my_menu_win; int n_choices, i; /* Initialize curses */ initscr(); start_color(); cbreak(); noecho(); keypad(stdscr, TRUE); clear(); refresh(); init_pair(1, COLOR_RED, COLOR_BLACK); getmaxyx(stdscr,y,x); /* Create items */ n_choices = ARRAY_SIZE(choices); my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); for(i=0;i<n_choices;++i) { my_items[i] = new_item(choices[i],""); set_item_userptr(my_items[i], func); } my_items[n_choices] = (ITEM *)NULL; /* Crate menu */ my_menu = new_menu((ITEM **)my_items); /* Create the window to be associated with the menu */ my_menu_win = newwin(10, 45, (y/2)-6,(x/2)-22 ); keypad(my_menu_win, TRUE); /* Set main window and sub window */ set_menu_win(my_menu, my_menu_win); set_menu_sub(my_menu, derwin(my_menu_win, 6, 40, 3, 1)); /* Set menu mark to the string " * " */ set_menu_mark(my_menu, " * "); /* Print a border around the main window and print a title */ box(my_menu_win, 0, 0); print_in_middle(my_menu_win, 1, 0, 45, "Welcome to Ncurses Phone Book (NPB)", COLOR_PAIR(1)); mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 43); mvwaddch(my_menu_win, 2, 44, ACS_RTEE); mvprintw(LINES - 2, 0, "Press ESC to exit"); refresh(); /* Post the menu */ post_menu(my_menu); wrefresh(my_menu_win); //upon(); while((c = wgetch(my_menu_win)) != 27) { switch(c) { case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); break; case 10: /* Enter */ { ITEM *cur; void (*p)(char *); cur = current_item(my_menu); p = item_userptr(cur); p((char *)item_name(cur)); refresh(); //printw("%s",item_name(cur)); if(!strcmp("[6] Exit",item_name(cur))) { unpost_menu(my_menu); free_menu(my_menu); endwin(); return 0; } else if(!strcmp("[3] Searching for a contact by name",item_name(cur))) { upon("Search by name"); //if(strlen(str)>1) clear(); set_menu_sub(my_menu, derwin(my_menu_win, 6, 40, 3, 1)); /* Set menu mark to the string " * " */ set_menu_mark(my_menu, " * "); /* Print a border around the main window and print a title */ box(my_menu_win, 0, 0); print_in_middle(my_menu_win, 1, 0, 45, "Welcome to Simple Phone Book Application", COLOR_PAIR(1)); mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 43); mvwaddch(my_menu_win, 2, 44, ACS_RTEE); mvprintw(LINES - 2, 0, "Press ESC to exit"); refresh(); /* Post the menu */ post_menu(my_menu); wrefresh(my_menu_win); } else if(!strcmp("[4] Searching for a contact by number",item_name(cur))) { upon("Search by number"); clear(); set_menu_sub(my_menu, derwin(my_menu_win, 6, 40, 3, 1)); /* Set menu mark to the string " * " */ set_menu_mark(my_menu, " * "); /* Print a border around the main window and print a title */ box(my_menu_win, 0, 0); print_in_middle(my_menu_win, 1, 0, 45, "Welcome to Simple Phone Book Application", COLOR_PAIR(1)); mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 43); mvwaddch(my_menu_win, 2, 44, ACS_RTEE); mvprintw(LINES - 2, 0, "Press ESC to exit"); refresh(); /* Post the menu */ post_menu(my_menu); wrefresh(my_menu_win); } else if(!strcmp("[2] Removing a contact",item_name(cur))) { upon("Enter contact name to remove"); clear(); set_menu_sub(my_menu, derwin(my_menu_win, 6, 40, 3, 1)); /* Set menu mark to the string " * " */ set_menu_mark(my_menu, " * "); /* Print a border around the main window and print a title */ box(my_menu_win, 0, 0); print_in_middle(my_menu_win, 1, 0, 45, "Welcome to Simple Phone Book Application", COLOR_PAIR(1)); mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 43); mvwaddch(my_menu_win, 2, 44, ACS_RTEE); mvprintw(LINES - 2, 0, "Press ESC to exit"); refresh(); /* Post the menu */ post_menu(my_menu); wrefresh(my_menu_win); } else if(!strcmp("[5] Displaying contact list",item_name(cur))) { show(); clear(); set_menu_sub(my_menu, derwin(my_menu_win, 6, 40, 3, 1)); /* Set menu mark to the string " * " */ set_menu_mark(my_menu, " * "); /* Print a border around the main window and print a title */ box(my_menu_win, 0, 0); print_in_middle(my_menu_win, 1, 0, 45, "Welcome to Simple Phone Book Application", COLOR_PAIR(1)); mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 43); mvwaddch(my_menu_win, 2, 44, ACS_RTEE); mvprintw(LINES - 2, 0, "Press ESC to exit"); refresh(); /* Post the menu */ post_menu(my_menu); wrefresh(my_menu_win); } else if(!strcmp("[1] Adding a contact",item_name(cur))) { int ret=uponadd(); clear(); set_menu_sub(my_menu, derwin(my_menu_win, 6, 40, 3, 1)); /* Set menu mark to the string " * " */ set_menu_mark(my_menu, " * "); /* Print a border around the main window and print a title */ box(my_menu_win, 0, 0); print_in_middle(my_menu_win, 1, 0, 45, "Welcome to Simple Phone Book Application", COLOR_PAIR(1)); mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 43); mvwaddch(my_menu_win, 2, 44, ACS_RTEE); mvprintw(LINES - 2, 0, "Press ESC to exit"); refresh(); /* Post the menu */ post_menu(my_menu); wrefresh(my_menu_win); if(ret==1) { int trash=outprint(); clear(); set_menu_sub(my_menu, derwin(my_menu_win, 6, 40, 3, 1)); /* Set menu mark to the string " * " */ set_menu_mark(my_menu, " * "); /* Print a border around the main window and print a title */ box(my_menu_win, 0, 0); print_in_middle(my_menu_win, 1, 0, 45, "Welcome to Simple Phone Book Application", COLOR_PAIR(1)); mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 43); mvwaddch(my_menu_win, 2, 44, ACS_RTEE); mvprintw(LINES - 2, 0, "Press ESC to exit"); refresh(); /* Post the menu */ post_menu(my_menu); wrefresh(my_menu_win); } } pos_menu_cursor(my_menu); break; } } wrefresh(my_menu_win); } /* Unpost and free all the memory taken up */ unpost_menu(my_menu); free_menu(my_menu); // for(i = 0; i < n_choices; ++i) // free_item(my_items[i]); endwin(); }
/* * Return codes: * 0 - action performed * -1 - quit was pressed (only for main menu) * +1 - backspace or escape was pressed, back to previous menu */ static int do_menu(struct menuoption *runmenu, int num, int upper, const char *title, ...) { int ch, i, y, x; // char buf[BUFSIZ]; char *buf; struct menuoption *curmenu; ITEM **mitems, *mitem; MENU *mmenu; WINDOW *msubwin; va_list ap; /* Clear screen and print title */ if (upper) show_mainwin(0); else show_mainwin(1); va_start(ap, title); head_mainwin(title, ap); va_end(ap); x = 0; y = 0; if (upper) msubwin = create_uppersubwin(); else msubwin = create_lowersubwin(&y, &x); mitems = (ITEM **)calloc(num + 1, sizeof(ITEM*)); for (curmenu = runmenu, i = 0; i < num; i++) { ch = sizeof buf > x ? x - 1 : sizeof buf - 1; // if (snprintf(buf, ch, "%s", curmenu->m_name) <= 0) { if (asprintf(&buf, "%s", curmenu->m_name) <= 0) { ask_ok("Error creating the menu."); for (i--; i >= 0; i--) free(mitems[i]); return 0; } mitems[i] = new_item(buf, NULL); set_item_userptr(mitems[i], (void *)curmenu); // warnx("%ld %s", (long)curmenu, curmenu->m_name); curmenu++; } mitems[num] = NULL; mmenu = new_menu(mitems); set_menu_win(mmenu, mainwin); set_menu_sub(mmenu, msubwin); set_menu_format(mmenu, upper ? 1 : y, 1); if (colouring) { set_menu_fore(mmenu, COLOR_PAIR(1)); set_menu_back(mmenu, COLOR_PAIR(1)); set_menu_grey(mmenu, COLOR_PAIR(1)); } set_menu_mark(mmenu, ">"); if (post_menu(mmenu)) { ask_ok("Error posting the menu."); return 1; } show_mainwin(0); show_lowersubwin(0, msubwin); while ((ch = getch()) != '\n' && ch != '\b') { switch (ch) { case KEY_DOWN: menu_driver(mmenu, REQ_NEXT_ITEM); break; case KEY_UP: menu_driver(mmenu, REQ_PREV_ITEM); break; case KEY_NPAGE: menu_driver(mmenu, REQ_SCR_DPAGE); break; case KEY_PPAGE: menu_driver(mmenu, REQ_SCR_UPAGE); break; case KEY_END: menu_driver(mmenu, REQ_LAST_ITEM); break; case KEY_HOME: menu_driver(mmenu, REQ_FIRST_ITEM); break; } show_mainwin(0); show_lowersubwin(0, msubwin); } mitem = current_item(mmenu); curmenu = (struct menuoption *)item_userptr(mitem); unpost_menu(mmenu); delwin(msubwin); free_menu(mmenu); for (i = 0; i < num; i++) free_item(mitems[i]); free(mitems); if (ch != '\b' && curmenu->m_action != NULL) { curmenu->m_action(curmenu->m_argv); return 0; } else { return 1; } }
int main() { ITEM **my_items; int c; MENU *my_menu; int n_choices, i; ITEM *cur_item; /* Initialize curses */ initscr(); start_color(); cbreak(); noecho(); keypad(stdscr, TRUE); init_pair(1, COLOR_RED, COLOR_BLACK); init_pair(2, COLOR_GREEN, COLOR_BLACK); init_pair(3, COLOR_MAGENTA, COLOR_BLACK); /* Initialize items */ n_choices = ARRAY_SIZE(choices); my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); for(i = 0; i < n_choices; ++i) { my_items[i] = new_item(choices[i], choices[i]); /* Set the user pointer */ set_item_userptr(my_items[i], func); } my_items[n_choices] = (ITEM *)NULL; /* Create menu */ my_menu = new_menu((ITEM **)my_items); /* Post the menu */ mvprintw(LINES - 3, 0, "Press <ENTER> to see the option selected"); mvprintw(LINES - 2, 0, "Up and Down arrow keys to naviage (F1 to Exit)"); post_menu(my_menu); refresh(); while((c = getch()) != KEY_F(1)) { switch(c) { case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); break; case 10: /* Enter */ { ITEM *cur; void (*p)(char *); cur = current_item(my_menu); p = item_userptr(cur); p((char *)item_name(cur)); pos_menu_cursor(my_menu); break; } break; } } unpost_menu(my_menu); for(i = 0; i < n_choices; ++i) free_item(my_items[i]); free_menu(my_menu); endwin(); }
// Scan MAME ROMs folder, cross-reference against XML (if present) for // verbose descriptions, generate ncurses menu. static void find_roms(void) { FILE *fp; struct dirent **dirList; int i, nFiles; Game *g; // For traversing Game linked list WINDOW *scanWin; // Modal 'Scanning...' window if(noRomWin) { // Delete 'No ROMS' window if present delwin(noRomWin); noRomWin = NULL; werase(mainWin); box(mainWin, 0, 0); } if(items) { // Delete old ROM menu and contents, if any if(menu) { unpost_menu(menu); free_menu(menu); menu = NULL; } for(i=0; items[i]; i++) free_item(items[i]); free(items); items = NULL; } const char scanMsg[] = "Scanning ROM folder..."; scanWin = newwin(3, strlen(scanMsg) + 4, (LINES - 4) / 2 - 1, (COLS - strlen(scanMsg)) / 2 - 2); box(scanWin, 0, 0); mvwprintw(scanWin, 1, 2, scanMsg); wnoutrefresh(mainWin); wnoutrefresh(scanWin); doupdate(); delwin(scanWin); werase(mainWin); box(mainWin, 0, 0); while(gameList) { // Delete existing gameList, if any g = gameList->next; if(gameList->name) free(gameList->name); if(gameList->desc) free(gameList->desc); free(gameList); gameList = g; } i = 0; // Count number of games found & successfully alloc'd if((nFiles = scandir(rom, &dirList, sel, alphasort)) > 0) { // Copy dirent array to a Game linked list while(nFiles--) { // List is assembled in reverse if((g = (Game *)malloc(sizeof(Game)))) { g->name = strdup(dirList[nFiles]->d_name); g->desc = NULL; g->next = gameList; gameList = g; i++; // A winner is you } // dirList contents are freed as we go free(dirList[nFiles]); } free(dirList); } // Alloc, load, cross-reference XML file against ROM filenames if((fp = fopen(xml, "r"))) { fseek(fp, 0, SEEK_END); char *buf; int len = ftell(fp); if((buf = (char *)malloc(len))) { int depth = 0; fseek(fp, 0, SEEK_SET); fread(buf, 1, len, fp); XML_Parser parser = XML_ParserCreate(NULL); XML_SetUserData(parser, &depth); XML_SetElementHandler(parser, startElement, endElement); XML_SetCharacterDataHandler(parser, elementData); XML_Parse(parser, buf, len, 1); XML_ParserFree(parser); free(buf); } fclose(fp); } if((items = (ITEM**)malloc((i + 1) * sizeof(ITEM *)))) { for(i=0, g=gameList; g; g=g->next, i++) { items[i] = new_item(g->name, g->desc); set_item_userptr(items[i], g); } items[i] = NULL; menu = new_menu(items); set_menu_win(menu, mainWin); set_menu_sub(menu, derwin(mainWin, LINES-6, COLS-2, 1, 1)); set_menu_format(menu, LINES-6, 1); set_menu_mark(menu, " "); post_menu(menu); } wrefresh(mainWin); if(!menu) { // If no ROMs, throw up a message to that effect const char noRomMsg[] = "No ROMs found"; noRomWin = newwin(3, strlen(noRomMsg) + 4, (LINES - 4) / 2 - 1, (COLS - strlen(noRomMsg)) / 2 - 2); box(noRomWin, 0, 0); mvwprintw(noRomWin, 1, 2, noRomMsg); wrefresh(noRomWin); } }
int hgd_update_files_win(struct ui *u) { ITEM **items = NULL; char *slash_append, *prep_item_str; struct dirent **dirents_dirs = 0, **dirents_files = 0, *d, *d_copy; int n_dirs = 0, n_files = 0; int i, cur_item = 0, ret = HGD_FAIL; DPRINTF(HGD_D_INFO, "Update files window"); wclear(u->content_wins[HGD_WIN_FILES]); hgd_unpost_and_free_content_menu(u, HGD_WIN_FILES); if ((n_dirs = scandir( u->cwd, &dirents_dirs, hgd_filter_dirs, alphasort)) < 0) { DPRINTF(HGD_D_WARN, "Failed to scan directory: '%s'", u->cwd); goto clean; } if ((n_files = scandir( u->cwd, &dirents_files, hgd_filter_files, alphasort)) < 0) { DPRINTF(HGD_D_WARN, "Failed to scan directory: '%s'", u->cwd); goto clean; } /* make our menu items */ DPRINTF(HGD_D_INFO, "allocating %d menu items", n_files + n_dirs); items = xcalloc(n_files + n_dirs + 1, sizeof(ITEM *)); /* add dirs */ for (i = 0; i < n_dirs; i++) { d = dirents_dirs[i]; xasprintf(&slash_append, "%s/", d->d_name); hgd_prepare_item_string(&prep_item_str, slash_append); free(slash_append); items[cur_item] = new_item(prep_item_str, NULL); if (items[cur_item] == NULL) { DPRINTF(HGD_D_WARN, "Could not make new menu item: %s", SERROR); free(prep_item_str); continue; } /* * jam away the dirent for later use * Note! scandir does notallocate a full struct dirent */ #if !defined(__linux__) d_copy = xcalloc(1, sizeof(struct dirent)); d_copy->d_fileno = d->d_fileno;2 d_copy->d_reclen = d->d_reclen; d_copy->d_type = d->d_type; d_copy->d_namlen = d->d_namlen; strlcpy(d_copy->d_name, d->d_name, d->d_namlen + 1); #else d_copy = xcalloc(1, d->d_reclen); memcpy(d_copy, d, d->d_reclen); #endif set_item_userptr(items[cur_item], d_copy); cur_item++; } /* add files */ for (i = 0; i < n_files; i++) { d = dirents_files[i]; hgd_prepare_item_string(&prep_item_str, d->d_name); items[cur_item] = new_item(prep_item_str, NULL); if (items[cur_item] == NULL) { DPRINTF(HGD_D_WARN, "Could not make new menu item: %s", SERROR); free(prep_item_str); continue; } /* * copy manually, do not use memcpy, as scandir does not * allocate a full struct dirent */ #if !defined(__linux__) d_copy = xcalloc(1, sizeof(struct dirent)); d_copy->d_fileno = d->d_fileno;2 d_copy->d_reclen = d->d_reclen; d_copy->d_type = d->d_type; d_copy->d_namlen = d->d_namlen; strlcpy(d_copy->d_name, d->d_name, d->d_namlen + 1); #else d_copy = xcalloc(1, d->d_reclen); memcpy(d_copy, d, d->d_reclen); #endif set_item_userptr(items[cur_item], d_copy); cur_item++; } DPRINTF(HGD_D_INFO, "Actually allocated %d menu items", cur_item); items[cur_item] = NULL; u->content_menus[HGD_WIN_FILES] = new_menu(items); keypad(u->content_wins[HGD_WIN_FILES], TRUE); set_menu_win(u->content_menus[HGD_WIN_FILES], u->content_wins[HGD_WIN_FILES]); set_menu_mark(u->content_menus[HGD_WIN_FILES], ""); set_menu_format(u->content_menus[HGD_WIN_FILES], LINES - 2, 1); set_menu_fore(u->content_menus[HGD_WIN_FILES], COLOR_PAIR(HGD_CPAIR_SELECTED)); if ((post_menu(u->content_menus[HGD_WIN_FILES])) != E_OK) DPRINTF(HGD_D_WARN, "Could not post menu"); ret = HGD_OK; clean: if (dirents_files) { for (i = 0; i < n_files; i ++) free(dirents_files[i]); free(dirents_files); } if (dirents_dirs) { for (i = 0; i < n_dirs; i ++) free(dirents_dirs[i]); free(dirents_dirs); } #if 0 if (items) free(items); #endif return (ret); }
int main(){ main_pid = getpid(); char line[BUF][BUF]; FILE *rlist = NULL; int igt = 0; int total = 0; rlist = fopen("STATIONS.txt", "r"); while( fgets ( line[igt], BUF, rlist )) { /* get rid of ending \n from fgets */ line[igt][strlen(line[igt]) - 1] = '\0'; char *line_f = line[igt]; uris[igt] = strtok(line_f, "@"); choices[igt] = strtok(NULL, "@"); descriptions[igt] = strtok(NULL, "@"); igt++; } /* Initialize curses */ ITEM **my_items; int c; MENU *my_menu; int n_choices, i; ITEM *cur_item; initscr(); /*We dont really need colors for now...*/ /*start_color();*/ cbreak(); noecho(); keypad(stdscr, TRUE); /*wborder(0 , 0,0,0,0,0,0,0,0,);*/ /*init_pair(1, COLOR_RED,COLOR_WHITE);*/ /*init_pair(2, COLOR_GREEN,COLOR_WHITE);*/ /*init_pair(3, COLOR_MAGENTA,COLOR_WHITE);*/ /* Initialize items */ n_choices = ARRAY_SIZE(choices); my_items = (ITEM **)calloc(n_choices + 1, sizeof(ITEM *)); for(i = 0; i < n_choices; ++i) { my_items[i] = new_item(choices[i],descriptions[i]); /* The F*cking user pointer */ set_item_userptr(my_items[i], func); } my_items[n_choices] = (ITEM *)NULL; /* Create menu */ my_menu = new_menu((ITEM **)my_items); set_menu_opts(my_menu,O_SHOWDESC); int y_lines_menu = LINES - y_lines_reserved; set_menu_format(my_menu,y_lines_menu,1); /* Post the menu */ /*Help thingy*/ hacete_una_linea_putin(5); mvprintw(LINES - 4, 0, " Press <ENTER> to play the station,"); mvprintw(LINES - 3, 0, " any <arrow> to move the menu buffer, <Q> to Quit or"); mvprintw(LINES - 2, 0, " <K> to Kill child mplayer process."); hacete_una_linea_putin(1); post_menu(my_menu); refresh(); /*big loop thingy*/ while((c = getch()) != 113){ switch(c){ case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); break; case KEY_LEFT: menu_driver(my_menu, REQ_SCR_UPAGE); break; case KEY_RIGHT: menu_driver(my_menu, REQ_SCR_DPAGE); break; case 107: // k kill_child(); break; case 10: { /* Enter == Play some radio! */ ITEM *cur; void (*p)(char *); if (*contador >= 1){ kill_child(); } cur = current_item(my_menu); description_fn = (char *)item_description(cur); p = item_userptr(cur); p((char *)item_name(cur)); pos_menu_cursor(my_menu); play_radio(); cuenta = cuenta + 1; break; } break; } } /*That's all, free memory, kill any pid and exit.*/ unpost_menu(my_menu); for(i = 0; i < n_choices; ++i){ free_item(my_items[i]); } free_menu(my_menu); endwin(); kill_child(); exit(0); }
/* Starts the program and prints the main menu */ int main(int qw) { static int mm = -1; mm++; initscr(); curs_set(0); noecho(); if(qw == 2) banner(4); else if(qw == 1) banner(3); ITEM **my_items; int c; MENU *my_menu; WINDOW *my_menu_win; int n_choices, i; ITEM *cur; /* Initialize curses */ initscr(); start_color(); init_pair(5, COLOR_RED, COLOR_BLACK); init_pair(6, COLOR_BLACK, COLOR_RED); init_pair(7, COLOR_CYAN, COLOR_BLACK); cbreak(); noecho(); keypad(stdscr, TRUE); init_pair(1, COLOR_RED, COLOR_BLACK); START: attrset(COLOR_PAIR(7)); n_choices = ARRAY_SIZE(choices_index); my_items = (ITEM **)calloc(n_choices, sizeof(ITEM *)); for(i = 0; i < n_choices; ++i) { my_items[i] = new_item(choices_index[i], NULL); if(i == 0) /* Set the user pointer */ set_item_userptr(my_items[i], mains); else if(i == 1) set_item_userptr(my_items[i], race_menu); else if(i == 2) set_item_userptr(my_items[i], exitit); } my_items[n_choices] = (ITEM *)NULL; /* Crate menu */ my_menu = new_menu((ITEM **)my_items); /* Create the window to be associated with the menu */ my_menu_win = newwin(8, 25, 15, 70); keypad(my_menu_win, TRUE); /* Set main window and sub window */ set_menu_win(my_menu, my_menu_win); set_menu_sub(my_menu, derwin(my_menu_win, 5, 19, 3, 1)); /* Set menu mark to the string " * " */ set_menu_mark(my_menu, "--> "); /* Print a border around the main window and print a title */ box(my_menu_win, 0, 0); print_in_middle1(my_menu_win, 1, 0, 25, "CHOOSE A GAME", COLOR_PAIR(7)); attrset(COLOR_PAIR(7)); mvwaddch(my_menu_win, 2, 0, ACS_LTEE); mvwhline(my_menu_win, 2, 1, ACS_HLINE, 24); mvwaddch(my_menu_win, 2, 24, ACS_RTEE); mvprintw(LINES - 2, 1, " Press F1 to exit"); /* Post the menu */ mvprintw(LINES - 3, 1, " Press <ENTER> to see the option selected"); mvprintw(LINES - 2, 1, " Up and Down arrow keys to navigate (F1 to Exit)"); post_menu(my_menu); if(!mm) moto(0); else moto(1); refresh(); /* Post the menu */ post_menu(my_menu); wrefresh(my_menu_win); while((c = wgetch(my_menu_win)) != KEY_F(1)) { switch(c) { case KEY_DOWN: menu_driver(my_menu, REQ_DOWN_ITEM); break; case KEY_UP: menu_driver(my_menu, REQ_UP_ITEM); break; case 10: cur = current_item(my_menu); endwin(); unpost_menu(my_menu); clear(); refresh(); void (*p)(char *); cur = current_item(my_menu); p = item_userptr(cur); p((char *)item_name(cur)); pos_menu_cursor(my_menu); initscr(); /* Post the menu */ attrset(COLOR_PAIR(7)); mvprintw(LINES - 3, 1, " Press <ENTER> to see the option selected"); mvprintw(LINES - 2, 1, " Up and Down arrow keys to naviage (F1 to Exit)"); goto START; refresh(); break; } wrefresh(my_menu_win); } /* Unpost and free all the memory taken up */ for(i = 0; i < n_choices; ++i) free_item(my_items[i]); free_menu(my_menu); endwin(); exit(1); }