exposurses

Check-in [a88f7285c1]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:For ref: Try wrapping menu and window creation in single function Got this so it drew everything ok, but segfaults as soon as a key is pressed. I obviously don't understand pointers, etc properly yet. As although it's drawn the menus it hasn't "attached" them the the objects/pointers I was expecting.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | origin/master | trunk
Files: files | file ages | folders
SHA3-256: a88f7285c1b85e569cca145436d9d3691a3842a3fe625dda41b01fb470d8a541
User & Date: base@atomicules.co.uk 2013-09-18 10:33:49
Context
2013-09-18
10:44
Add debug options to Makefile Not as simple as adding `-g` option as also need to ensure not calling the `-s` option which strips the debug info. Decided to change so have a `make` and a `make debug` check-in: 86fd753f70 user: base@atomicules.co.uk tags: origin/master, trunk
10:33
For ref: Try wrapping menu and window creation in single function Got this so it drew everything ok, but segfaults as soon as a key is pressed. I obviously don't understand pointers, etc properly yet. As although it's drawn the menus it hasn't "attached" them the the objects/pointers I was expecting. check-in: a88f7285c1 user: base@atomicules.co.uk tags: origin/master, trunk
2013-09-10
08:51
Make exposure menu functional - Make it function so that user is constrained to exposure menu initially and must select value there. - Once selected, the ISO menu becomes active and user must select value there - Finally make both shutter and aperture menus actives (switch between them with left/right arrow keys). Whichever is selected, the other will be calculated. This is not very pretty: - I don't like having both the selection_counter and menu_counter. Seems almost redundant. Made sense before I constrained selections and stepped through the menu's etc (and therefore you could re-select), but now seems annoying to have to keep it only for how last two menus work. - I don't like the repetitive menu creation stuff. That should be DRY-ed up. check-in: 580afe6ab4 user: base@atomicules.co.uk tags: origin/master, trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to exposurses.c.

    83     83   MENU *aperture_menu;
    84     84   WINDOW *exposure_win;
    85     85   WINDOW *iso_win;
    86     86   WINDOW *shutter_win;
    87     87   WINDOW *aperture_win;
    88     88   
    89     89   void selection(char *name);
           90  +void add_menu(char **array, ITEM **items, MENU *men, WINDOW *win, int n, int xpos, char *title);
           91  +void remove_menu(ITEM **items, MENU *men, int n);
    90     92   void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color);
    91     93   int exposure(int iso);
    92     94   double shutter(int exposure, double aperture);
    93     95   double aperture(int exposure, double shutter);
    94     96   int nearest_match(double x, int menu);
    95     97   double fraction_to_double(char *fraction);
    96     98   /* No one will ever need more than 9 bytes! */
................................................................................
   117    119   	cbreak();
   118    120   	noecho();
   119    121   	keypad(stdscr, TRUE);
   120    122   	init_pair(1, COLOR_RED, COLOR_BLACK);
   121    123   	init_pair(2, COLOR_CYAN, COLOR_BLACK);
   122    124   
   123    125   	/* Create items */
   124         -	/* Lot's of repitition here. Surely can be wrapped in a function */
          126  +	/* Can't really avoid finding array sizes here */
   125    127   	n_exposure = ARRAY_SIZE(exposure_array);
   126    128   	n_iso = ARRAY_SIZE(iso_array);
   127    129   	n_shutter = ARRAY_SIZE(shutter_array);
   128    130   	n_aperture = ARRAY_SIZE(aperture_array);
   129         -	exposure_items = (ITEM **)calloc(n_exposure, sizeof(ITEM *));
   130         -	for(i = 0; i < n_exposure; ++i) {
   131         -		exposure_items[i] = new_item(exposure_array[i], exposure_array[i]);
   132         -		set_item_userptr(exposure_items[i], selection);
   133         -	}
   134         -	iso_items = (ITEM **)calloc(n_iso, sizeof(ITEM *));
   135         -	for(i = 0; i < n_iso; ++i) {
   136         -		iso_items[i] = new_item(iso_array[i], iso_array[i]);
   137         -		set_item_userptr(iso_items[i], selection);
   138         -	}
   139         -	shutter_items = (ITEM **)calloc(n_shutter, sizeof(ITEM *));
   140         -	for(i = 0; i < n_shutter; ++i) {
   141         -		shutter_items[i] = new_item(shutter_array[i], shutter_array[i]);
   142         -		set_item_userptr(shutter_items[i], selection);
   143         -	}
   144         -	aperture_items = (ITEM **)calloc(n_aperture, sizeof(ITEM *));
   145         -	for(i = 0; i < n_aperture; ++i) {
   146         -		aperture_items[i] = new_item(aperture_array[i], aperture_array[i]);
   147         -		set_item_userptr(aperture_items[i], selection);
   148         -	}
   149         -
   150         -	/* Create menu */
   151         -	exposure_menu = new_menu((ITEM **)exposure_items);
   152         -	iso_menu = new_menu((ITEM **)iso_items);
   153         -	shutter_menu = new_menu((ITEM **)shutter_items);
   154         -	aperture_menu = new_menu((ITEM **)aperture_items);
   155         -
   156         -	/* Create the window to be associated with the menu */
   157         -	exposure_win = newwin(10, 40, 4, 4);
   158         -	keypad(exposure_win, TRUE);
   159         -	iso_win = newwin(10, 40, 4, 45);
   160         -	keypad(iso_win, TRUE);
   161         -	shutter_win = newwin(10, 40, 4, 86);
   162         -	keypad(shutter_win, TRUE);
   163         -	aperture_win = newwin(10, 40, 4, 127);
   164         -	keypad(aperture_win, TRUE);
   165         -
   166         -	/* Set main window and sub window */
   167         -	set_menu_win(exposure_menu, exposure_win);
   168         -	set_menu_sub(exposure_menu, derwin(exposure_win, 6, 38, 3, 1));
   169         -	set_menu_win(iso_menu, iso_win);
   170         -	set_menu_sub(iso_menu, derwin(iso_win, 6, 38, 3, 1));
   171         -	set_menu_win(shutter_menu, shutter_win);
   172         -	set_menu_sub(shutter_menu, derwin(shutter_win, 6, 38, 3, 1));
   173         -	set_menu_win(aperture_menu, aperture_win);
   174         -	set_menu_sub(aperture_menu, derwin(aperture_win, 6, 38, 3, 1));
   175         -	set_menu_format(exposure_menu, 5, 1);
   176         -	set_menu_format(iso_menu, 5, 1);
   177         -	set_menu_format(shutter_menu, 5, 1);
   178         -	set_menu_format(aperture_menu, 5, 1);
   179         -
   180         -	/* Set menu mark to the string " * " */
   181         -	set_menu_mark(exposure_menu, " * ");
   182         -	set_menu_mark(iso_menu, " * ");
   183         -	set_menu_mark(shutter_menu, " * ");
   184         -	set_menu_mark(aperture_menu, " * ");
   185         -
   186         -	/* Print a border around the main window and print a title */
   187         -	box(exposure_win, 0, 0);
   188         -	box(iso_win, 0, 0);
   189         -	box(shutter_win, 0, 0);
   190         -	box(aperture_win, 0, 0);
   191         -	print_in_middle(exposure_win, 1, 0, 40, "EV", COLOR_PAIR(1));
   192         -	print_in_middle(iso_win, 1, 0, 40, "ISO", COLOR_PAIR(1));
   193         -	print_in_middle(shutter_win, 1, 0, 40, "Shutter", COLOR_PAIR(1));
   194         -	print_in_middle(aperture_win, 1, 0, 40, "Aperture", COLOR_PAIR(1));
   195         -	mvwaddch(exposure_win, 2, 0, ACS_LTEE);
   196         -	mvwaddch(iso_win, 2, 0, ACS_LTEE);
   197         -	mvwaddch(shutter_win, 2, 0, ACS_LTEE);
   198         -	mvwaddch(aperture_win, 2, 0, ACS_LTEE);
   199         -	mvwhline(exposure_win, 2, 1, ACS_HLINE, 38);
   200         -	mvwhline(iso_win, 2, 1, ACS_HLINE, 38);
   201         -	mvwhline(shutter_win, 2, 1, ACS_HLINE, 38);
   202         -	mvwhline(aperture_win, 2, 1, ACS_HLINE, 38);
   203         -	mvwaddch(exposure_win, 2, 39, ACS_RTEE);
   204         -	mvwaddch(iso_win, 2, 39, ACS_RTEE);
   205         -	mvwaddch(shutter_win, 2, 39, ACS_RTEE);
   206         -	mvwaddch(aperture_win, 2, 39, ACS_RTEE);
   207         -
   208         -	/* Post the menu */
   209         -	post_menu(exposure_menu);
   210         -	post_menu(iso_menu);
   211         -	post_menu(shutter_menu);
   212         -	post_menu(aperture_menu);
   213         -	wrefresh(exposure_win);
   214         -	wrefresh(iso_win);
   215         -	wrefresh(shutter_win);
   216         -	wrefresh(aperture_win);
          131  +	add_menu(exposure_array, exposure_items, exposure_menu, exposure_win, n_exposure, 4, "EV");
          132  +	add_menu(iso_array, iso_items, iso_menu, iso_win, n_iso, 45, "ISO");
          133  +	add_menu(shutter_array, shutter_items, shutter_menu, shutter_win, n_shutter, 86, "Shutter");
          134  +	add_menu(aperture_array, aperture_items, aperture_menu, aperture_win, n_aperture, 127, "Aperture");
   217    135   
   218    136   	attron(COLOR_PAIR(2));
   219    137   	mvprintw(LINES - 2, 0, "Select EV");
   220    138   	/*mvprintw(LINES - 2, 0, "Select ISO and then one of Shutter/Aperture to calculate other of Shutter/Aperture");*/
   221    139   	mvprintw(LINES - 1, 0, "Arrow keys to navigate, Enter to select, Q to exit");
   222    140   	attroff(COLOR_PAIR(2));
   223    141   	refresh();
................................................................................
   251    169   			case 10: { /* ENTER */
   252    170   				ITEM *cur;
   253    171   				void (*p)(char *);
   254    172   
   255    173   				cur = current_item(*menu);
   256    174   				p = item_userptr(cur);
   257    175   				/* Learning notes - Don't understand this bit */
          176  +				/* Is this a function pointer? */
   258    177   				p((char *)item_name(cur));
   259    178   				
   260    179   				switch (selection_counter) {
   261    180   					case 1: { /* Exposure selected */
   262    181   						selection_counter += 1;
   263    182   						menu_counter += 1;
   264    183   						move(LINES - 2, 0);
................................................................................
   331    250   				}
   332    251   			}
   333    252   			break;
   334    253   		}
   335    254   		wrefresh(*win);
   336    255   	}	
   337    256   	/* Unpost and free all the memory taken up */
   338         -	unpost_menu(exposure_menu);
   339         -	unpost_menu(iso_menu);
   340         -	unpost_menu(shutter_menu);
   341         -	unpost_menu(aperture_menu);
   342         -	free_menu(exposure_menu);
   343         -	free_menu(iso_menu);
   344         -	free_menu(shutter_menu);
   345         -	free_menu(aperture_menu);
   346         -	for(i = 0; i < n_exposure; ++i)
   347         -		free_item(iso_items[i]);
   348         -	for(i = 0; i < n_iso; ++i)
   349         -		free_item(iso_items[i]);
   350         -	for(i = 0; i < n_shutter; ++i)
   351         -		free_item(shutter_items[i]);
   352         -	for(i = 0; i < n_aperture; ++i)
   353         -		free_item(aperture_items[i]);
          257  +	remove_menu(exposure_items, exposure_menu, n_exposure);
          258  +	remove_menu(iso_items, iso_menu, n_iso);
          259  +	remove_menu(shutter_items, shutter_menu, n_shutter);
          260  +	remove_menu(aperture_items, aperture_menu, n_aperture);
   354    261   	endwin();
   355    262   }
          263  +
          264  +void add_menu(char **array, ITEM **items, MENU *men, WINDOW *win, int n, int xpos, char *title) {
          265  +	int i;
          266  +
          267  +	items = (ITEM **)calloc(n, sizeof(ITEM *));
          268  +	for(i = 0; i<n; ++i) {
          269  +		items[i] = new_item(array[i], array[i]);
          270  +		set_item_userptr(items[i], selection);
          271  +	}
          272  +	men = new_menu((ITEM **)items);
          273  +	win = newwin(10, 40, 4, xpos);
          274  +	keypad(win, TRUE);
          275  +	set_menu_win(men, win);
          276  +	set_menu_sub(men, derwin(win, 6, 38, 3, 1));
          277  +	set_menu_format(men, 5, 1);
          278  +	set_menu_mark(men, " * ");
          279  +	box(win, 0, 0);
          280  +	print_in_middle(win, 1, 0, 40, title, COLOR_PAIR(1));
          281  +	mvwaddch(win, 2, 0, ACS_LTEE);
          282  +	mvwhline(win, 2, 1, ACS_HLINE, 38);
          283  +	mvwaddch(win, 2, 39, ACS_RTEE);
          284  +	post_menu(men);
          285  +	wrefresh(win);
          286  +}
          287  +
          288  +void remove_menu(ITEM **items, MENU *men, int n) {
          289  +	int i;
          290  +
          291  +	unpost_menu(men);
          292  +	free_menu(men);
          293  +	for(i = 0; i < n; ++i)
          294  +		free_item(items[i]);
          295  +}
   356    296   
   357    297   void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color) {
   358    298   	int length, x, y;
   359    299   	float temp;
   360    300   
   361    301   	if(win == NULL)
   362    302   		win = stdscr;