1
/* $Id: RootViewController.m 3552 2011-05-05 05:50:48Z nanang $ */
3
* Copyright (C) 2010-2011 Teluu Inc. (http://www.teluu.com)
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
#import "RootViewController.h"
23
/* Sleep interval duration, change to shorter duration for better
24
* interaction but make sure there is enough time for the other
25
* thread to do what it's supposed to do
27
#define SLEEP_INTERVAL 0.5
29
@implementation RootViewController
34
RootViewController *view;
36
bool systest_initialized;
45
pj_status_t gui_init(gui_menu *menu)
51
/* Run GUI main loop */
52
pj_status_t gui_start(gui_menu *menu)
54
view.titles = [NSMutableArray arrayWithCapacity:menu->submenu_cnt];
55
view.menus = [NSMutableArray arrayWithCapacity:menu->submenu_cnt];
56
NSMutableArray *smenu;
57
for (int i = 0; i < menu->submenu_cnt; i++) {
58
NSString *str = [NSString stringWithFormat:@"%s" , menu->submenus[i]->title];
59
[view.titles addObject: str];
60
smenu = [NSMutableArray arrayWithCapacity:menu->submenus[i]->submenu_cnt];
61
/* We do not need the last two menus of the "Tests" menu (NULL and "Exit"),
64
for (int j = 0; j < menu->submenus[i]->submenu_cnt - (i==0? 2: 0); j++) {
65
str = [NSString stringWithFormat:@"%s" , menu->submenus[i]->submenus[j]->title];
66
[smenu addObject:str];
68
[view.menus addObject:smenu];
75
/* Signal GUI mainloop to stop */
76
void gui_destroy(void)
80
/* AUX: display messagebox */
81
enum gui_key gui_msgbox(const char *title, const char *message, enum gui_flag flag)
86
[view performSelectorOnMainThread:@selector(showMsg) withObject:nil waitUntilDone:YES];
88
view.testView.key = 0;
89
while(view.testView.key == 0) {
90
/* Let the main thread do its job (refresh the view) while we wait for
91
* user interaction (button click)
93
[NSThread sleepForTimeInterval:SLEEP_INTERVAL];
96
if (view.testView.key == 1)
99
return (flag == WITH_YESNO? KEY_NO: KEY_CANCEL);
103
void gui_sleep(unsigned sec)
105
[NSThread sleepForTimeInterval:sec];
108
- (void)viewDidLoad {
113
/* Get a writable path for output files */
114
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
115
NSString *documentsDirectory = [paths objectAtIndex:0];
116
[documentsDirectory getCString:doc_path maxLength:PATH_LENGTH encoding:NSASCIIStringEncoding];
117
strncat(doc_path, "/", PATH_LENGTH);
119
/* Get path for our test resources (the wav files) */
120
NSString *resPath = [[NSBundle mainBundle] resourcePath];
121
[resPath getCString:res_path maxLength:PATH_LENGTH encoding:NSASCIIStringEncoding];
122
strncat(res_path, "/", PATH_LENGTH);
124
systest_initialized = false;
126
[NSThread detachNewThreadSelector:@selector(startTest) toTarget:self withObject:nil];
127
/* Let our new thread initialize */
128
while (!systest_initialized) {
129
[NSThread sleepForTimeInterval:SLEEP_INTERVAL];
132
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
133
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
137
- (void)viewWillAppear:(BOOL)animated {
138
[super viewWillAppear:animated];
142
- (void)viewDidAppear:(BOOL)animated {
143
[super viewDidAppear:animated];
147
- (void)viewWillDisappear:(BOOL)animated {
148
[super viewWillDisappear:animated];
152
- (void)viewDidDisappear:(BOOL)animated {
153
[super viewDidDisappear:animated];
158
// Override to allow orientations other than the default portrait orientation.
159
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
160
// Return YES for supported orientations.
161
return (interfaceOrientation == UIInterfaceOrientationPortrait);
165
- (void)didReceiveMemoryWarning {
166
// Releases the view if it doesn't have a superview.
167
[super didReceiveMemoryWarning];
169
// Release any cached data, images, etc that aren't in use.
172
- (void)viewDidUnload {
173
// Release anything that can be recreated in viewDidLoad or on demand.
174
// e.g. self.myOutlet = nil;
179
[NSThread sleepForTimeInterval:SLEEP_INTERVAL];
183
#pragma mark Table view methods
186
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
187
return [titles count];
191
// Customize the number of rows in the table view.
192
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
193
return [[menus objectAtIndex:section] count];
197
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
198
// The header for the section is the region name -- get this from the region at the section index.
199
return [titles objectAtIndex:section];
202
// Customize the appearance of table view cells.
203
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
205
static NSString *CellIdentifier = @"Cell";
207
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
209
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
212
// Configure the cell.
213
cell.textLabel.text = [[menus objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
219
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
220
if (systest_init() != PJ_SUCCESS) {
227
systest_initialized = 1;
228
while(!thread_quit) {
230
while (section == -1) {
231
[NSThread sleepForTimeInterval:SLEEP_INTERVAL];
233
(*gmenu->submenus[section]->submenus[row]->handler)();
235
[view performSelectorOnMainThread:@selector(showMsg) withObject:nil waitUntilDone:YES];
244
self.testView.testDesc.text = [self.testView.testDesc.text stringByAppendingString: @"Finished"];
245
[self.testView.testDesc scrollRangeToVisible:NSMakeRange([self.testView.testDesc.text length] - 1, 1)];
246
[self.testView.button1 setHidden:true];
247
[self.testView.button2 setHidden:true];
250
self.testView.title = [NSString stringWithFormat:@"%s", ctitle];
251
self.testView.testDesc.text = [self.testView.testDesc.text stringByAppendingString: [NSString stringWithFormat:@"%s\n\n", cmsg]];
252
[self.testView.testDesc scrollRangeToVisible:NSMakeRange([self.testView.testDesc.text length] - 1, 1)];
254
[self.testView.button1 setHidden:false];
255
[self.testView.button2 setHidden:false];
256
if (cflag == WITH_YESNO) {
257
[self.testView.button1 setTitle:@"Yes" forState:UIControlStateNormal];
258
[self.testView.button2 setTitle:@"No" forState:UIControlStateNormal];
259
} else if (cflag == WITH_OK) {
260
[self.testView.button1 setTitle:@"OK" forState:UIControlStateNormal];
261
[self.testView.button2 setHidden:true];
262
} else if (cflag == WITH_OKCANCEL) {
263
[self.testView.button1 setTitle:@"OK" forState:UIControlStateNormal];
264
[self.testView.button2 setTitle:@"Cancel" forState:UIControlStateNormal];
269
// Override to support row selection in the table view.
270
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
272
// Navigation logic may go here -- for example, create and push another view controller.
273
// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
274
// [self.navigationController pushViewController:anotherViewController animated:YES];
275
// [anotherViewController release];
277
if (self.testView == nil) {
278
self.testView = [[TestViewController alloc] initWithNibName:@"TestViewController" bundle:[NSBundle mainBundle]];
281
[self.navigationController pushViewController:self.testView animated:YES];
282
self.testView.title = [[menus objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
283
self.testView.testDesc.text = @"";
284
section = indexPath.section;
290
// Override to support conditional editing of the table view.
291
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
292
// Return NO if you do not want the specified item to be editable.
299
// Override to support editing the table view.
300
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
302
if (editingStyle == UITableViewCellEditingStyleDelete) {
303
// Delete the row from the data source.
304
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
306
else if (editingStyle == UITableViewCellEditingStyleInsert) {
307
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
314
// Override to support rearranging the table view.
315
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
321
// Override to support conditional rearranging of the table view.
322
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
323
// Return NO if you do not want the item to be re-orderable.