console.cpp
Go to the documentation of this file.
00001 /*****************************************************************************
00002  * PROJECT: iwaki
00003  *
00004  * FILE: console.cpp
00005  *
00006  * ABSTRACT: This is a simple app that reads user's terminal input and
00007  * sends it to Iwakishi ROS wrapper around Iwaki interaction manager.
00008  *
00009  * Iwakishi: a ROS wrapper around Iwaki interaction manager library.
00010  * Copyright (C) 2012-2013 Maxim Makatchev.
00011  * 
00012  * This program is free software: you can redistribute it and/or modify
00013  * it under the terms of the GNU General Public License as published by
00014  * the Free Software Foundation, either version 3 of the License, or
00015  * (at your option) any later version.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00024  * 
00025  * 
00026  ****************************************************************/
00027 
00028 #include <iostream>
00029 #include <sstream>
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <string>
00033 #include <getopt.h>
00034 #include <ncursesw/ncurses.h> /* for getch() */
00035 
00036 #include "ros/ros.h"
00037 #include "iwaki/AtomMsg.h"
00038 
00039 using std::string;
00040 using std::iostream;
00041 
00042 
00046 #define KB_ENTER int('\n')
00047 #define KEY_CONTROL_SMALL_C 3
00048 
00049 void publishBufferAtom(string &kb_buffer,  ros::Publisher &atom_pub) {
00050         /* first, create atom_msg */
00051     iwaki::AtomMsg newAtomMsg;
00052 
00053     iwaki::VarSlot v1, v2, v3, v4, v5;
00054 
00055     v1.name = "type";
00056     v1.val = "im";
00057     v1.relation = "equal";
00058     v1.type = "string";
00059     v1.unique_mask = true;
00060 
00061     v2.name = "subtype";
00062     v2.val = "user";
00063     v2.relation = "equal";
00064     v2.type = "string";
00065     v2.unique_mask = true;
00066         
00067     v3.name = "uu_unhandled";
00068     v3.val = "true";
00069     v3.relation = "equal";
00070     v3.type = "string";
00071     v3.unique_mask = false;
00072     
00073     v4.name = "uu_string";
00074     v4.val = kb_buffer;
00075     v4.relation = "equal";
00076     v4.type = "string";
00077     v4.unique_mask = false;
00078 
00079     v5.name = "id";
00080     v5.val = "1";
00081     v5.relation = "equal";
00082     v5.type = "string";
00083     v5.unique_mask = false;
00084 
00085     
00086     newAtomMsg.varslots.push_back(v1);
00087     newAtomMsg.varslots.push_back(v2);
00088     newAtomMsg.varslots.push_back(v3);
00089     newAtomMsg.varslots.push_back(v4);
00090     newAtomMsg.varslots.push_back(v5);
00091 
00092     atom_pub.publish(newAtomMsg);
00093 }
00094 
00095 
00096 void renderKbLines(std::list<string> &kb_lines) {
00097     int nrow, ncol, dy;
00098     clear();
00099     getmaxyx(stdscr, nrow, ncol);
00100     dy = nrow - 1;
00101 
00102     move(0,0);
00103     
00104     std::list<string>::iterator msg_it = kb_lines.begin();
00105     while  (( msg_it != kb_lines.end() ) && (dy >= 0)) {  /* print from the bottom
00106                                                                  * of the pane */
00107         move(0 + dy, 0);
00108         printw( msg_it->c_str() );            
00109         ++msg_it;
00110         --dy;
00111     }
00112     refresh();
00113 }
00114 
00115 void updateKbBuffer(string &kb_buffer, int ch, ros::Publisher &atom_pub) {
00116     char buffer[8];
00117     sprintf (buffer, "%c", ch);
00118     if ((ch >= 32) && (ch <= 126)) {
00119             /* legal visible characters */
00120         kb_buffer += string(buffer);
00121     } else if (ch == KEY_BACKSPACE) {
00122             /* apparently this symbol is already defined */
00123         if (!kb_buffer.empty()) {
00124             kb_buffer = kb_buffer.substr(0, kb_buffer.size() - 1);
00125         }
00126     } else if (ch == KEY_CONTROL_SMALL_C) {
00127         endwin(); /* end curses mode */
00128         ros::shutdown();
00129     } else if (ch == KB_ENTER) {
00130         publishBufferAtom(kb_buffer, atom_pub);
00131         kb_buffer = "";
00132     }
00133 }
00134 
00135 void updateKbLines(std::list<string> &kb_lines, int &ch, ros::Publisher &atom_pub) {
00136     char buffer[8];
00137     string kb_buffer = *kb_lines.begin();
00138     
00139     sprintf (buffer, "%c", ch);
00140     if ((ch >= 32) && (ch <= 126)) {
00141             /* legal visible characters */
00142         kb_buffer += string(buffer);
00143             /* update buffer */
00144         kb_lines.pop_front();
00145         kb_lines.push_front(kb_buffer);
00146     } else if (ch == KEY_BACKSPACE) {
00147             /* apparently this symbol is already defined */
00148         if (!kb_buffer.empty()) {
00149             kb_buffer = kb_buffer.substr(0, kb_buffer.size() - 1);
00150                 /* update buffer */
00151             kb_lines.pop_front();
00152             kb_lines.push_front(kb_buffer);
00153         }
00154     } else if (ch == KEY_CONTROL_SMALL_C) {
00155         endwin(); /* end curses mode */
00156         ros::shutdown();
00157     } else if (ch == KB_ENTER) {
00158         publishBufferAtom(kb_buffer, atom_pub);
00159         kb_buffer = "";
00160         kb_lines.push_front(kb_buffer);
00161     }
00162 
00163 }
00164 
00168 int main(int argc, char **argv) {
00169     int ch;
00170     std::list<string> kb_lines;
00171 
00172         /* initalize kb_lines */
00173     kb_lines.push_back("");
00174     
00175         /* before reading the app's arglist */
00176      ros::init(argc, argv, "console");
00177 
00178         /* init text UI*/
00179     setlocale(LC_CTYPE,"");       /* for Unicode support */
00180     initscr();                  /* Start curses mode */
00181     raw();                      /* Line buffering disabled      */
00182     keypad(stdscr, TRUE);       /* We get F1, F2 etc..          */
00183     noecho();                   /* Don't echo() while we do getch */
00184     timeout(0);
00185     
00186     
00187 
00188       /* ROS stuff */
00189     ros::NodeHandle n;
00190     ros::Publisher atom_pub = n.advertise<iwaki::AtomMsg>("IwakiInputAtom", 1000);
00191         /******************************
00192          *
00193          * Main loop
00194          * 
00195          ******************************/
00196     ros::Rate loop_rate(10);
00197     while (ros::ok()) {
00198         ch = getch();
00199         updateKbLines(kb_lines, ch, atom_pub);
00200         renderKbLines(kb_lines);
00201         loop_rate.sleep();
00202     }
00203 }


iwaki
Author(s): Maxim Makatchev, Reid Simmons
autogenerated on Thu Aug 27 2015 13:34:44