summaryrefslogtreecommitdiffstats
path: root/door.c
diff options
context:
space:
mode:
authorFreeArtMan <dos21h@gmail.com>2015-01-10 15:10:43 +0900
committerFreeArtMan <dos21h@gmail.com>2015-01-10 15:10:43 +0900
commitbd040d066dc5ba642c4fadca26ddd4e1963e5231 (patch)
tree47ee71980b1e8fdef4f3fad7cb869f1b91e97deb /door.c
parent3e61314544a9151cb5a49db12a58c4fb74d6bee3 (diff)
downloadmicrobbs-bd040d066dc5ba642c4fadca26ddd4e1963e5231.tar.gz
microbbs-bd040d066dc5ba642c4fadca26ddd4e1963e5231.zip
Added door game support upto 9 games
Diffstat (limited to 'door.c')
-rw-r--r--door.c205
1 files changed, 205 insertions, 0 deletions
diff --git a/door.c b/door.c
new file mode 100644
index 0000000..6031c9d
--- /dev/null
+++ b/door.c
@@ -0,0 +1,205 @@
+#include "door.h"
+
+#ifdef CONFIG_DOORGAMES
+
+
+
+//just for full fill libc example
+static int dir_door_game_selector (const struct dirent *unused)
+{
+ return 1;
+}
+
+int bbs_door_start( term_screen *ts, const char *cmd )
+{
+ int ret=0;
+
+ int restore_mode=1;
+
+ struct termios orig_i;
+ struct termios orig_o;
+
+ term_clr_scr( ts );
+
+ //if ( tcgetattr( ts->ifd, &orig_i ) == -1 ) restore_mode = 0;
+ //if ( tcgetattr( ts->ofd, &orig_o ) == -1 ) restore_mode = 0;
+
+ tcsetattr( ts->ifd, TCSAFLUSH, &ts->orig_i );
+ //tcsetattr( ts->ofd, TCSAFLUSH, &ts->orig_o );
+
+ //term_clr_scr( ts );
+ if ( system( cmd ) == -1 )
+ {
+ bbs_log( NULL, "couldnt execute %s", cmd );
+ }
+
+ if ( restore_mode )
+ {
+ tcsetattr( ts->ifd, TCSAFLUSH, &ts->raw_i );
+ //tcsetattr( ts->ofd, TCSAFLUSH, &orig_o );
+ }
+
+
+ return ret;
+}
+
+int bbs_door( term_screen *ts, const char *dir_name )
+{
+ int ret=0;
+
+ int quit_loop=0;
+ int menu_input = 0;
+ char menu_cmd = 0;
+ int row = 0;
+ const char *door_game_dir=NULL;
+
+ if ( ts == NULL )
+ return ret;
+
+
+ if ( dir_name == NULL )
+ {
+ door_game_dir = CONFIG_DOOR_DEFAULT_DIR;
+ } else
+ {
+ door_game_dir = dir_name;
+ }
+
+ //list dir
+ struct stat path_node;
+ struct dirent **eps;
+ int n;
+
+ //get all articles names in list and use list every time needed
+ List *dir_list = llist_new();
+
+ bbs_log_article_list( NULL );
+
+ if ( door_game_dir != NULL )
+ {
+ //should have some bugs be carefull
+ n = scandir( door_game_dir, &eps, dir_door_game_selector, alphasort );
+ if ( n >= 0)
+ {
+ int cnt;
+ for ( cnt=0; cnt<n; cnt++)
+ {
+ sds d_name = sds_new( eps[cnt]->d_name );
+ sds pathname = sds_new( door_game_dir );
+ sds_cat( pathname, d_name );
+
+ //check if its file
+ term_cur_set_c( ts, 0 );
+ if ( stat( pathname, &path_node ) == 0 )
+ {
+ if ( (path_node.st_mode & S_IFREG) && (path_node.st_mode & 0111) )
+ {
+ llist_push( dir_list, pathname );
+ } else
+ {
+ sds_free( pathname );
+ sds_free( d_name );
+ }
+ } else
+ {
+ sds_free( pathname );
+ sds_free( d_name );
+ }
+ free( eps[cnt] );
+ }
+ free( eps );
+ } else
+ {
+ printf("Err\n");
+ ERROR("Cannot open article directory\n");
+ }
+ ret = 0;
+ }
+
+ while( (quit_loop == 0) )
+ {
+ term_clr_scr( ts );
+ term_cur_set_c( ts, 0 );
+ printf("(L)ist games, (P)lay game, (Q)uit"); fflush( stdout );
+
+
+ term_cur_set_c( ts, 0 );
+ term_cur_set_r( ts, term_get_maxrow( ts ) );
+ printf(":"); fflush( stdout );
+
+ menu_input = term_getc( ts );
+ //if something whent wrong dont know why, need to get some test case
+ if ( menu_input == -1 )
+ {
+ term_cur_set_c( ts, 0 );
+ ERROR("Cannot read char");
+ sleep(1);
+ continue;
+ }
+ menu_cmd = (char)menu_input;
+
+ switch( menu_cmd )
+ {
+ case 'q':
+ case 'Q':
+ quit_loop = 1;
+ break;
+
+ case 'l':
+ case 'L':
+ {
+ term_cur_set_c( ts, 0 );
+ term_cur_set_r( ts, 2 );
+ //print list of executables
+ struct ListNode *iter=dir_list->first;
+ int cnt = 1;
+ while (iter != NULL)
+ {
+ term_cur_set_c( ts, 0 );
+ printf( "[%02d] %s\n", cnt, (char *)iter->val);
+ cnt += 1;
+ iter = iter->next;
+ }
+ term_cur_set_c( ts, 0 );
+ term_print( ts, "Press ANYKEY\n", 13);
+ term_getc( ts );
+ }
+ break;
+ case 'p':
+ case 'P':
+ {
+ term_cur_set_c( ts, 0 );
+ printf("Input game number 1-9:"); fflush( stdout );
+ int game_input = term_getc( ts );
+ //if something whent wrong dont know why, need to get some test case
+ char game_number = (char)game_input-'0';
+
+ if ( game_number > 0 && game_number < 10)
+ {
+ struct ListNode *iter = dir_list->first;
+ int cnt = 1;
+ while ( iter != NULL )
+ {
+ if ( cnt == game_number )
+ {
+ bbs_door_start( ts, iter->val );
+ break;
+ }
+ cnt += 1;
+ iter = iter->next;
+ }
+ }
+ }
+ break;
+ default:
+ printf("Try more\n");
+ }
+ }
+
+ //needed special care to free sds strings
+ //llist_free( dir_list );
+
+ return ret;
+}
+
+#endif \ No newline at end of file