vmonitor.cpp

00001 /*
00002   VXV モニタ
00003   Satofumi KAMIMURA
00004   $Id$
00005 */
00006 
00007 #include "vmonitor.h"
00008 #include "vexception.h"
00009 #include "parseArgs.h"
00010 #include "timeUtils.h"
00011 #include "screenTask.h"
00012 #include "fileUtils.h"
00013 #include <stdio.h>
00014 #include <stdlib.h>
00015 #include <stdarg.h>
00016 
00017 
00018 vmonitor* vmonitor::obj = NULL;
00019 std::string vmonitor::ttf_path;
00020 
00021 
00022 void vmonitor::VMonitorProperty::evaluate(void) {
00023   const char* home_str = getenv("HOME");
00024   std::string home_path = std::string((home_str ? home_str : ".")) + "/.vxv";
00025   value(ttf_file, "ttf_path", (home_path + "/font.ttf").c_str());
00026   value(urg_type, "urg_type", "URG-04LX");
00027   value(&crd_index, "crd_index", 0);
00028 }
00029 
00030 
00031 vmonitor::vmonitor(void)
00032   : win(NULL), ttf(NULL), task(NULL), scr(NULL),
00033     task_count(0), mode(Monitor::Unknown), is_pause(false),
00034     fname(NULL), log(NULL), env(NULL), pause_sem(NULL),
00035     property(new VMonitorProperty()) {
00036   atexit(terminate);
00037 
00038   const char* home_str = getenv("HOME");
00039   std::string home_path = std::string((home_str ? home_str : ".")) + "/.vxv";
00040   const char* searchPath[] = { ".", home_path.c_str(), NULL };
00041   property->load("monitorconf", searchPath);
00042 
00043   // monitorconf からフォント位置の読み出し
00044   const char* dummyPath[] = { "", NULL };
00045   ttf_path = VXV::searchFile(property->ttf_file.c_str(), dummyPath);
00046   if (ttf_path.empty() && (ttf_path = searchTTF()).empty()) {
00047     // !!! monitorconf を見つけたパスに置き換える
00048     fprintf(stderr, "Please edit ttf_path in ~/.vxv/monitorconf\n");
00049   }
00050   if (!ttf_path.empty()) {
00051     property->ttf_file = ttf_path;
00052   }
00053 }
00054 
00055 
00056 vmonitor::~vmonitor(void) {
00057   pause();
00058   hide();
00059   task->del(scr);
00060 
00061   delete env;
00062   env = NULL;
00063 
00064   delete log;
00065   log = NULL;
00066 
00067   delete scr;
00068   scr = NULL;
00069 
00070   delete task;
00071   task = NULL;
00072 
00073   delete ttf;
00074   ttf = NULL;
00075 
00076   delete win;
00077   win = NULL;
00078 
00079   SDL_DestroySemaphore(obj->pause_sem);
00080 
00081   // !!! 上書きする理由がないため、削除
00082   // !!! あったっけ?
00083   // property->save();
00084 
00085   delete property;
00086 }
00087 
00088 
00089 void vmonitor::terminate(void) {
00090   delete obj;
00091 }
00092 
00093 
00094 vmonitor* vmonitor::getObject(void) {
00095   if (!obj) {
00096     obj = new vmonitor();
00097     obj->win = new SDL_Window();
00098     obj->win->setTitle("VXV Monitor");
00099     obj->win->autoHideCursor(3000);
00100     obj->ttf = new TTF_Draw();
00101     obj->ttf->load(ttf_path.c_str());
00102     obj->task = new MonitorTask();
00103     obj->scr = new ScreenTask(obj->win, obj->ttf);
00104     obj->task->setScreenObject(obj->scr);
00105     obj->env = new EnvironmentManage();
00106 
00107     obj->pause_sem = SDL_CreateSemaphore(1);
00108   }
00109   return obj;
00110 }
00111 
00112 
00113 int vmonitor::connect(int argc, char *argv[]) {
00114   vmonitor* mon = vmonitor::getObject();
00115 
00116   char *logfile = NULL;
00117   char *envfile = NULL;
00118   int monitorMode = Monitor::parseArgs(logfile, envfile, argc-1, &argv[1]);
00119   bool fullscreen = false;
00120   for (int i = 1; i < argc; ++i) {
00121     if (!strcmp("-fs", argv[i])) {
00122       fullscreen = true;
00123     }
00124   }
00125   mon->win->setFullscreen(fullscreen);
00126 
00127   mon->setMonitorMode(monitorMode, logfile);
00128   mon->env->load(envfile);
00129   mon->scr->setEnvironment(mon->env->getPolygonsReference());
00130 
00131   return monitorMode;
00132 }
00133 
00134 
00135 void vmonitor::setMonitorMode(int monitorMode, const char* logfile) {
00136 
00137   if ((monitorMode != Monitor::Unknown) && !log) {
00138     if (monitorMode != Monitor::RealDevice) {
00139       VXV::set_MonitorMode(false);
00140     }
00141     mode = monitorMode;
00142     fname = logfile;
00143     bool write = (mode == Monitor::RealDevice) | (mode == Monitor::Simulator);
00144     log = new LogCtrl(logfile, write);
00145   }
00146 }
00147 
00148 
00149 int vmonitor::getMonitorMode(void) {
00150   if (obj) {
00151     return obj->mode;
00152   } else {
00153     return Monitor::Unknown;
00154   }
00155 }
00156 
00157 
00158 unsigned long vmonitor::getTicks(void) {
00159   if (obj->mode == Monitor::RealDevice) {
00160     return VXV::GetTicks();
00161   } else {
00162     return obj->task->getTicks();
00163   }
00164 }
00165 
00166 
00167 void vmonitor::add(TaskInterface* monitor_task) {
00168   if (task) {
00169     task->add(monitor_task);
00170     ++task_count;
00171   }
00172 }
00173 
00174 
00175 void vmonitor::del(TaskInterface* monitor_task) {
00176   if (task) {
00177     task->del(monitor_task);
00178     --task_count;
00179   }
00180 
00181   // RealDevice では task を登録しないため、終了判定は task_count で行う
00182   if (task_count <= 0) {
00183     log->flush();
00184     hide();
00185   }
00186 }
00187 
00188 
00189 void vmonitor::show(void) {
00190   if (!obj) {
00191     getObject();
00192   }
00193   bool nowPause = obj->is_pause;
00194   obj->pause();
00195   obj->win->activate(true);
00196 
00197   obj->scr->show();
00198   if (!nowPause) {
00199     obj->start();
00200   }
00201 }
00202 
00203 
00204 void vmonitor::hide(void) {
00205   bool nowPause = obj->is_pause;
00206   obj->pause();
00207   if (obj->scr) {
00208     obj->scr->hide();
00209   }
00210   if (obj->win) {
00211     obj->win->activate(false);
00212   }
00213   if (!nowPause) {
00214     obj->start();
00215   }
00216 }
00217 
00218 
00219 void vmonitor::pause(void) {
00220   if (obj->mode != Monitor::Playback) {
00221     return;
00222   }
00223 
00224   if (!obj->is_pause) {
00225     if (obj->task) {
00226       obj->task->pause();
00227     }
00228     obj->is_pause = true;
00229   }
00230 }
00231 
00232 
00233 void vmonitor::start(void) {
00234   if (obj->is_pause) {
00235     obj->task->start();
00236     obj->is_pause = false;
00237   }
00238 }
00239 
00240 
00241 void vmonitor::setTimeMagnify(double magnify) {
00242   if (!obj) {
00243     getObject();
00244   }
00245   if (obj->mode != Monitor::RealDevice) {
00246     VXV::set_DelayTimeMagnify(magnify);
00247     obj->task->setTimeMagnify(magnify);
00248   }
00249 }
00250 
00251 
00252 void vmonitor::setViewMagnify(double magnify) {
00253   if (!obj) {
00254     getObject();
00255   }
00256   if (obj->mode != Monitor::RealDevice) {
00257     obj->scr->updateViewMagnify(magnify);
00258   }
00259 }
00260 
00261 
00262 const char* vmonitor::getURGType(void) {
00263   return property->urg_type.c_str();
00264 }
00265 
00266 
00267 void vmonitor::clear(unsigned long layer) {
00268   if (obj) {
00269     obj->scr->clear(layer);
00270   }
00271 }
00272 
00273 
00274 unsigned long vmonitor::drawPoints(const std::vector<VXV::Grid3D>& points,
00275                                    unsigned long color, int r) {
00276   if (obj) {
00277     return obj->scr->setPoints(points, color, r);
00278   }
00279   return InvalidLayerId;
00280 }
00281 
00282 
00283 unsigned long vmonitor::drawLine(const VXV::Grid3D& p0, const VXV::Grid3D& p1,
00284                                  unsigned long color) {
00285   if (obj) {
00286     return obj->scr->setLine(p0, p1, color);
00287   }
00288   return InvalidLayerId;
00289 }
00290 
00291 
00292 unsigned long vmonitor::drawContLine(const std::deque<VXV::Grid3D>& points,
00293                                      unsigned long color) {
00294   if (obj) {
00295     return obj->scr->setContLine(points, color);
00296   }
00297   return InvalidLayerId;
00298 }
00299 
00300 
00301 unsigned long vmonitor::drawCircle(const VXV::Grid& center, int r,
00302                                    unsigned long color) {
00303   if (!obj || (r <= 0)) {
00304     return InvalidLayerId;
00305   }
00306   return obj->scr->setCircle(center, r, color);
00307 }
00308 
00309 
00310 
00311 unsigned long vmonitor::drawText(const char* text, const VXV::Grid& pos,
00312                                  int pxSize, bool pin, unsigned long color,
00313                                  unsigned long background) {
00314   if (obj) {
00315     return obj->scr->setText(text, pos, pxSize, pin, color, background);
00316   }
00317   return InvalidLayerId;
00318 }
00319 
00320 
00321 int vmonitor::printf(const char* fmt, ...) {
00322   if (obj) {
00323     va_list ap;
00324     va_start(ap, fmt);
00325     int n = obj->scr->consolePrintf(fmt, ap);
00326 
00327     return n;
00328   }
00329   return 0;
00330 }
00331 
00332 
00333 void vmonitor::scrollWithRobot(bool on) {
00334   if (!obj) {
00335     getObject();
00336   }
00337   obj->scr->scrollWithRobot(on);
00338 }
00339 

Generated on Mon Apr 13 22:52:03 2009 by  doxygen 1.5.7.1