cmCursesMainForm.cxx 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCursesMainForm.h"
  4. #include <algorithm>
  5. #include <cstdio>
  6. #include <cstring>
  7. #include <utility>
  8. #include <cm/memory>
  9. #include "cmCursesCacheEntryComposite.h"
  10. #include "cmCursesDummyWidget.h"
  11. #include "cmCursesForm.h"
  12. #include "cmCursesLabelWidget.h"
  13. #include "cmCursesLongMessageForm.h"
  14. #include "cmCursesStandardIncludes.h"
  15. #include "cmCursesStringWidget.h"
  16. #include "cmCursesWidget.h"
  17. #include "cmState.h"
  18. #include "cmStateTypes.h"
  19. #include "cmStringAlgorithms.h"
  20. #include "cmSystemTools.h"
  21. #include "cmVersion.h"
  22. #include "cmake.h"
  23. inline int ctrl(int z)
  24. {
  25. return (z & 037);
  26. }
  27. cmCursesMainForm::cmCursesMainForm(std::vector<std::string> args,
  28. int initWidth)
  29. : Args(std::move(args))
  30. , InitialWidth(initWidth)
  31. {
  32. this->HasNonStatusOutputs = false;
  33. this->NumberOfPages = 0;
  34. this->AdvancedMode = false;
  35. this->NumberOfVisibleEntries = 0;
  36. this->OkToGenerate = false;
  37. this->HelpMessage.emplace_back(
  38. "Welcome to ccmake, curses based user interface for CMake.");
  39. this->HelpMessage.emplace_back();
  40. this->HelpMessage.emplace_back(s_ConstHelpMessage);
  41. this->CMakeInstance =
  42. cm::make_unique<cmake>(cmake::RoleProject, cmState::Project);
  43. this->CMakeInstance->SetCMakeEditCommand(
  44. cmSystemTools::GetCMakeCursesCommand());
  45. // create the arguments for the cmake object
  46. std::string whereCMake =
  47. cmStrCat(cmSystemTools::GetProgramPath(this->Args[0]), "/cmake");
  48. this->Args[0] = whereCMake;
  49. this->CMakeInstance->SetArgs(this->Args);
  50. this->SearchMode = false;
  51. }
  52. cmCursesMainForm::~cmCursesMainForm()
  53. {
  54. if (this->Form) {
  55. unpost_form(this->Form);
  56. free_form(this->Form);
  57. this->Form = nullptr;
  58. }
  59. }
  60. // See if a cache entry is in the list of entries in the ui.
  61. bool cmCursesMainForm::LookForCacheEntry(const std::string& key)
  62. {
  63. return std::any_of(this->Entries.begin(), this->Entries.end(),
  64. [&key](cmCursesCacheEntryComposite const& entry) {
  65. return key == entry.Key;
  66. });
  67. }
  68. // Create new cmCursesCacheEntryComposite entries from the cache
  69. void cmCursesMainForm::InitializeUI()
  70. {
  71. // Create a vector of cmCursesCacheEntryComposite's
  72. // which contain labels, entries and new entry markers
  73. std::vector<cmCursesCacheEntryComposite> newEntries;
  74. std::vector<std::string> cacheKeys =
  75. this->CMakeInstance->GetState()->GetCacheEntryKeys();
  76. newEntries.reserve(cacheKeys.size());
  77. // Count non-internal and non-static entries
  78. int count = 0;
  79. for (std::string const& key : cacheKeys) {
  80. cmStateEnums::CacheEntryType t =
  81. this->CMakeInstance->GetState()->GetCacheEntryType(key);
  82. if (t != cmStateEnums::INTERNAL && t != cmStateEnums::STATIC &&
  83. t != cmStateEnums::UNINITIALIZED) {
  84. ++count;
  85. }
  86. }
  87. int entrywidth = this->InitialWidth - 35;
  88. if (count == 0) {
  89. // If cache is empty, display a label saying so and a
  90. // dummy entry widget (does not respond to input)
  91. cmCursesCacheEntryComposite comp("EMPTY CACHE", 30, 30);
  92. comp.Entry = cm::make_unique<cmCursesDummyWidget>(1, 1, 1, 1);
  93. newEntries.emplace_back(std::move(comp));
  94. } else {
  95. // Create the composites.
  96. // First add entries which are new
  97. for (std::string const& key : cacheKeys) {
  98. cmStateEnums::CacheEntryType t =
  99. this->CMakeInstance->GetState()->GetCacheEntryType(key);
  100. if (t == cmStateEnums::INTERNAL || t == cmStateEnums::STATIC ||
  101. t == cmStateEnums::UNINITIALIZED) {
  102. continue;
  103. }
  104. if (!this->LookForCacheEntry(key)) {
  105. newEntries.emplace_back(key, this->CMakeInstance->GetState(), true, 30,
  106. entrywidth);
  107. this->OkToGenerate = false;
  108. }
  109. }
  110. // then add entries which are old
  111. for (std::string const& key : cacheKeys) {
  112. cmStateEnums::CacheEntryType t =
  113. this->CMakeInstance->GetState()->GetCacheEntryType(key);
  114. if (t == cmStateEnums::INTERNAL || t == cmStateEnums::STATIC ||
  115. t == cmStateEnums::UNINITIALIZED) {
  116. continue;
  117. }
  118. if (this->LookForCacheEntry(key)) {
  119. newEntries.emplace_back(key, this->CMakeInstance->GetState(), false,
  120. 30, entrywidth);
  121. }
  122. }
  123. }
  124. // Replace old entries
  125. this->Entries = std::move(newEntries);
  126. // Compute fields from composites
  127. this->RePost();
  128. }
  129. void cmCursesMainForm::RePost()
  130. {
  131. // Create the fields to be passed to the form.
  132. if (this->Form) {
  133. unpost_form(this->Form);
  134. free_form(this->Form);
  135. this->Form = nullptr;
  136. }
  137. this->Fields.clear();
  138. if (this->AdvancedMode) {
  139. this->NumberOfVisibleEntries = this->Entries.size();
  140. } else {
  141. // If normal mode, count only non-advanced entries
  142. this->NumberOfVisibleEntries = 0;
  143. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  144. const char* existingValue =
  145. this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue());
  146. bool advanced =
  147. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  148. entry.GetValue(), "ADVANCED");
  149. if (!existingValue || (!this->AdvancedMode && advanced)) {
  150. continue;
  151. }
  152. this->NumberOfVisibleEntries++;
  153. }
  154. }
  155. // there is always one even if it is the dummy one
  156. if (this->NumberOfVisibleEntries == 0) {
  157. this->NumberOfVisibleEntries = 1;
  158. }
  159. // Assign the fields: 3 for each entry: label, new entry marker
  160. // ('*' or ' ') and entry widget
  161. this->Fields.reserve(3 * this->NumberOfVisibleEntries + 1);
  162. // Assign fields
  163. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  164. const char* existingValue =
  165. this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue());
  166. bool advanced =
  167. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  168. entry.GetValue(), "ADVANCED");
  169. if (!existingValue || (!this->AdvancedMode && advanced)) {
  170. continue;
  171. }
  172. this->Fields.push_back(entry.Label->Field);
  173. this->Fields.push_back(entry.IsNewLabel->Field);
  174. this->Fields.push_back(entry.Entry->Field);
  175. }
  176. // if no cache entries there should still be one dummy field
  177. if (this->Fields.empty()) {
  178. const auto& front = this->Entries.front();
  179. this->Fields.push_back(front.Label->Field);
  180. this->Fields.push_back(front.IsNewLabel->Field);
  181. this->Fields.push_back(front.Entry->Field);
  182. this->NumberOfVisibleEntries = 1;
  183. }
  184. // Has to be null terminated.
  185. this->Fields.push_back(nullptr);
  186. }
  187. void cmCursesMainForm::Render(int left, int top, int width, int height)
  188. {
  189. if (this->Form) {
  190. FIELD* currentField = current_field(this->Form);
  191. cmCursesWidget* cw =
  192. reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  193. // If in edit mode, get out of it
  194. if (cw->GetType() == cmStateEnums::STRING ||
  195. cw->GetType() == cmStateEnums::PATH ||
  196. cw->GetType() == cmStateEnums::FILEPATH) {
  197. cmCursesStringWidget* sw = static_cast<cmCursesStringWidget*>(cw);
  198. sw->SetInEdit(false);
  199. }
  200. // Delete the previous form
  201. unpost_form(this->Form);
  202. free_form(this->Form);
  203. this->Form = nullptr;
  204. }
  205. // Wrong window size
  206. if (width < cmCursesMainForm::MIN_WIDTH || width < this->InitialWidth ||
  207. height < cmCursesMainForm::MIN_HEIGHT) {
  208. return;
  209. }
  210. // Leave room for toolbar
  211. height -= 7;
  212. if (this->AdvancedMode) {
  213. this->NumberOfVisibleEntries = this->Entries.size();
  214. } else {
  215. // If normal, display only non-advanced entries
  216. this->NumberOfVisibleEntries = 0;
  217. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  218. const char* existingValue =
  219. this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue());
  220. bool advanced =
  221. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  222. entry.GetValue(), "ADVANCED");
  223. if (!existingValue || (!this->AdvancedMode && advanced)) {
  224. continue;
  225. }
  226. this->NumberOfVisibleEntries++;
  227. }
  228. }
  229. // Re-adjust the fields according to their place
  230. this->NumberOfPages = 1;
  231. if (height > 0) {
  232. bool isNewPage;
  233. int i = 0;
  234. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  235. const char* existingValue =
  236. this->CMakeInstance->GetState()->GetCacheEntryValue(entry.GetValue());
  237. bool advanced =
  238. this->CMakeInstance->GetState()->GetCacheEntryPropertyAsBool(
  239. entry.GetValue(), "ADVANCED");
  240. if (!existingValue || (!this->AdvancedMode && advanced)) {
  241. continue;
  242. }
  243. int row = (i % height) + 1;
  244. int page = (i / height) + 1;
  245. isNewPage = (page > 1) && (row == 1);
  246. if (isNewPage) {
  247. this->NumberOfPages++;
  248. }
  249. entry.Label->Move(left, top + row - 1, isNewPage);
  250. entry.IsNewLabel->Move(left + 32, top + row - 1, false);
  251. entry.Entry->Move(left + 33, top + row - 1, false);
  252. entry.Entry->SetPage(this->NumberOfPages);
  253. i++;
  254. }
  255. }
  256. // Post the form
  257. this->Form = new_form(this->Fields.data());
  258. post_form(this->Form);
  259. // Update toolbar
  260. this->UpdateStatusBar();
  261. this->PrintKeys();
  262. touchwin(stdscr);
  263. refresh();
  264. }
  265. void cmCursesMainForm::PrintKeys(int process /* = 0 */)
  266. {
  267. int x;
  268. int y;
  269. getmaxyx(stdscr, y, x);
  270. if (x < cmCursesMainForm::MIN_WIDTH || x < this->InitialWidth ||
  271. y < cmCursesMainForm::MIN_HEIGHT) {
  272. return;
  273. }
  274. // Give the current widget (if it exists), a chance to print keys
  275. cmCursesWidget* cw = nullptr;
  276. if (this->Form) {
  277. FIELD* currentField = current_field(this->Form);
  278. cw = reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  279. }
  280. char fmt_s[] = "%s";
  281. if (cw == nullptr || !cw->PrintKeys()) {
  282. char firstLine[512] = "";
  283. char secondLine[512] = "";
  284. char thirdLine[512] = "";
  285. if (process) {
  286. memset(firstLine, ' ', 68);
  287. memset(secondLine, ' ', 68);
  288. memset(thirdLine, ' ', 68);
  289. } else {
  290. if (this->OkToGenerate) {
  291. sprintf(firstLine,
  292. " [l] Show log output [c] Configure"
  293. " [g] Generate ");
  294. } else {
  295. sprintf(firstLine,
  296. " [l] Show log output [c] Configure"
  297. " ");
  298. }
  299. {
  300. const char* toggleKeyInstruction =
  301. " [t] Toggle advanced mode (currently %s)";
  302. sprintf(thirdLine, toggleKeyInstruction,
  303. this->AdvancedMode ? "on" : "off");
  304. }
  305. sprintf(secondLine,
  306. " [h] Help [q] Quit without generating");
  307. }
  308. curses_move(y - 4, 0);
  309. char fmt[512] = "Keys: [enter] Edit an entry [d] Delete an entry";
  310. if (process) {
  311. memset(fmt, ' ', 57);
  312. }
  313. printw(fmt_s, fmt);
  314. curses_move(y - 3, 0);
  315. printw(fmt_s, firstLine);
  316. curses_move(y - 2, 0);
  317. printw(fmt_s, secondLine);
  318. curses_move(y - 1, 0);
  319. printw(fmt_s, thirdLine);
  320. }
  321. if (cw) {
  322. char pageLine[512] = "";
  323. sprintf(pageLine, "Page %d of %d", cw->GetPage(), this->NumberOfPages);
  324. curses_move(0, 65 - static_cast<unsigned int>(strlen(pageLine)) - 1);
  325. printw(fmt_s, pageLine);
  326. }
  327. pos_form_cursor(this->Form);
  328. }
  329. // Print the key of the current entry and the CMake version
  330. // on the status bar. Designed for a width of 80 chars.
  331. void cmCursesMainForm::UpdateStatusBar(const char* message)
  332. {
  333. int x;
  334. int y;
  335. getmaxyx(stdscr, y, x);
  336. // If window size is too small, display error and return
  337. if (x < cmCursesMainForm::MIN_WIDTH || x < this->InitialWidth ||
  338. y < cmCursesMainForm::MIN_HEIGHT) {
  339. curses_clear();
  340. curses_move(0, 0);
  341. char fmt[] = "Window is too small. A size of at least %dx%d is required.";
  342. printw(fmt,
  343. (cmCursesMainForm::MIN_WIDTH < this->InitialWidth
  344. ? this->InitialWidth
  345. : cmCursesMainForm::MIN_WIDTH),
  346. cmCursesMainForm::MIN_HEIGHT);
  347. touchwin(stdscr);
  348. wrefresh(stdscr);
  349. return;
  350. }
  351. // Get the key of the current entry
  352. FIELD* cur = current_field(this->Form);
  353. int findex = field_index(cur);
  354. cmCursesWidget* lbl = nullptr;
  355. if (findex >= 0) {
  356. lbl = reinterpret_cast<cmCursesWidget*>(
  357. field_userptr(this->Fields[findex - 2]));
  358. }
  359. char help[128] = "";
  360. const char* curField = "";
  361. if (lbl) {
  362. curField = lbl->GetValue();
  363. // Get the help string of the current entry
  364. // and add it to the help string
  365. const char* existingValue =
  366. this->CMakeInstance->GetState()->GetCacheEntryValue(curField);
  367. if (existingValue) {
  368. const char* hs = this->CMakeInstance->GetState()->GetCacheEntryProperty(
  369. curField, "HELPSTRING");
  370. if (hs) {
  371. strncpy(help, hs, 127);
  372. help[127] = '\0';
  373. } else {
  374. help[0] = 0;
  375. }
  376. } else {
  377. sprintf(help, " ");
  378. }
  379. }
  380. // Join the key, help string and pad with spaces
  381. // (or truncate) as necessary
  382. char bar[cmCursesMainForm::MAX_WIDTH];
  383. size_t curFieldLen = strlen(curField);
  384. size_t helpLen = strlen(help);
  385. size_t width = std::min<size_t>(x, cmCursesMainForm::MAX_WIDTH);
  386. if (message) {
  387. curField = message;
  388. curFieldLen = strlen(message);
  389. strncpy(bar, curField, width);
  390. if (curFieldLen < width) {
  391. memset(bar + curFieldLen, ' ', width - curFieldLen);
  392. }
  393. } else {
  394. strncpy(bar, curField, width);
  395. if (curFieldLen < width) {
  396. bar[curFieldLen] = ':';
  397. bar[curFieldLen + 1] = ' ';
  398. strncpy(bar + curFieldLen + 2, help, width - curFieldLen - 2);
  399. if (curFieldLen + helpLen + 2 < width) {
  400. memset(bar + curFieldLen + helpLen + 2, ' ',
  401. width - (curFieldLen + helpLen + 2));
  402. }
  403. }
  404. }
  405. bar[width] = '\0';
  406. // Display CMake version info on the next line
  407. // We want to display this on the right
  408. char version[cmCursesMainForm::MAX_WIDTH];
  409. char vertmp[128];
  410. sprintf(vertmp, "CMake Version %s", cmVersion::GetCMakeVersion());
  411. size_t sideSpace = (width - strlen(vertmp));
  412. memset(version, ' ', sideSpace);
  413. sprintf(version + sideSpace, "%s", vertmp);
  414. version[width] = '\0';
  415. // Now print both lines
  416. char fmt_s[] = "%s";
  417. curses_move(y - 5, 0);
  418. attron(A_STANDOUT);
  419. printw(fmt_s, bar);
  420. attroff(A_STANDOUT);
  421. curses_move(y - 4, 0);
  422. printw(fmt_s, version);
  423. pos_form_cursor(this->Form);
  424. }
  425. void cmCursesMainForm::UpdateProgress(const std::string& msg, float prog)
  426. {
  427. if (prog >= 0) {
  428. constexpr int progressBarWidth = 40;
  429. int progressBarCompleted = static_cast<int>(progressBarWidth * prog);
  430. int percentCompleted = static_cast<int>(100 * prog);
  431. this->LastProgress = (percentCompleted < 100 ? " " : "");
  432. this->LastProgress += (percentCompleted < 10 ? " " : "");
  433. this->LastProgress += std::to_string(percentCompleted) + "% [";
  434. this->LastProgress.append(progressBarCompleted, '#');
  435. this->LastProgress.append(progressBarWidth - progressBarCompleted, ' ');
  436. this->LastProgress += "] " + msg + "...";
  437. } else {
  438. this->Outputs.emplace_back(msg);
  439. }
  440. this->DisplayOutputs();
  441. }
  442. int cmCursesMainForm::Configure(int noconfigure)
  443. {
  444. int xi;
  445. int yi;
  446. getmaxyx(stdscr, yi, xi);
  447. this->ResetOutputs();
  448. if (noconfigure == 0) {
  449. this->UpdateProgress("Configuring", 0);
  450. this->CMakeInstance->SetProgressCallback(
  451. [this](const std::string& msg, float prog) {
  452. this->UpdateProgress(msg, prog);
  453. });
  454. }
  455. // always save the current gui values to disk
  456. this->FillCacheManagerFromUI();
  457. this->CMakeInstance->SaveCache(
  458. this->CMakeInstance->GetHomeOutputDirectory());
  459. this->LoadCache(nullptr);
  460. // run the generate process
  461. this->OkToGenerate = true;
  462. int retVal;
  463. if (noconfigure) {
  464. retVal = this->CMakeInstance->DoPreConfigureChecks();
  465. this->OkToGenerate = false;
  466. if (retVal > 0) {
  467. retVal = 0;
  468. }
  469. } else {
  470. retVal = this->CMakeInstance->Configure();
  471. }
  472. this->CMakeInstance->SetProgressCallback(nullptr);
  473. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  474. if (retVal != 0 || this->HasNonStatusOutputs) {
  475. // see if there was an error
  476. if (cmSystemTools::GetErrorOccuredFlag()) {
  477. this->OkToGenerate = false;
  478. }
  479. int xx;
  480. int yy;
  481. getmaxyx(stdscr, yy, xx);
  482. const char* title = "Configure produced the following output";
  483. if (cmSystemTools::GetErrorOccuredFlag()) {
  484. title = "Configure failed with the following output";
  485. }
  486. cmCursesLongMessageForm* msgs =
  487. new cmCursesLongMessageForm(this->Outputs, title);
  488. // reset error condition
  489. cmSystemTools::ResetErrorOccuredFlag();
  490. CurrentForm = msgs;
  491. msgs->Render(1, 1, xx, yy);
  492. msgs->ScrollDown();
  493. msgs->HandleInput();
  494. // If they typed the wrong source directory, we report
  495. // an error and exit
  496. if (retVal == -2) {
  497. return retVal;
  498. }
  499. CurrentForm = this;
  500. this->Render(1, 1, xx, yy);
  501. }
  502. this->InitializeUI();
  503. this->Render(1, 1, xi, yi);
  504. return 0;
  505. }
  506. int cmCursesMainForm::Generate()
  507. {
  508. int xi;
  509. int yi;
  510. getmaxyx(stdscr, yi, xi);
  511. this->ResetOutputs();
  512. this->UpdateProgress("Generating", 0);
  513. this->CMakeInstance->SetProgressCallback(
  514. [this](const std::string& msg, float prog) {
  515. this->UpdateProgress(msg, prog);
  516. });
  517. // run the generate process
  518. int retVal = this->CMakeInstance->Generate();
  519. this->CMakeInstance->SetProgressCallback(nullptr);
  520. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  521. if (retVal != 0 || this->HasNonStatusOutputs) {
  522. // see if there was an error
  523. if (cmSystemTools::GetErrorOccuredFlag()) {
  524. this->OkToGenerate = false;
  525. }
  526. // reset error condition
  527. cmSystemTools::ResetErrorOccuredFlag();
  528. int xx;
  529. int yy;
  530. getmaxyx(stdscr, yy, xx);
  531. const char* title = "Generate produced the following output";
  532. if (cmSystemTools::GetErrorOccuredFlag()) {
  533. title = "Generate failed with the following output";
  534. }
  535. cmCursesLongMessageForm* msgs =
  536. new cmCursesLongMessageForm(this->Outputs, title);
  537. CurrentForm = msgs;
  538. msgs->Render(1, 1, xx, yy);
  539. msgs->ScrollDown();
  540. msgs->HandleInput();
  541. // If they typed the wrong source directory, we report
  542. // an error and exit
  543. if (retVal == -2) {
  544. return retVal;
  545. }
  546. CurrentForm = this;
  547. this->Render(1, 1, xx, yy);
  548. }
  549. this->InitializeUI();
  550. this->Render(1, 1, xi, yi);
  551. return 0;
  552. }
  553. void cmCursesMainForm::AddError(const std::string& message,
  554. const char* /*unused*/)
  555. {
  556. this->Outputs.emplace_back(message);
  557. this->HasNonStatusOutputs = true;
  558. this->DisplayOutputs();
  559. }
  560. void cmCursesMainForm::RemoveEntry(const char* value)
  561. {
  562. if (!value) {
  563. return;
  564. }
  565. auto removeIt =
  566. std::find_if(this->Entries.begin(), this->Entries.end(),
  567. [value](cmCursesCacheEntryComposite& entry) -> bool {
  568. const char* val = entry.GetValue();
  569. return val != nullptr && !strcmp(value, val);
  570. });
  571. if (removeIt != this->Entries.end()) {
  572. this->CMakeInstance->UnwatchUnusedCli(value);
  573. this->Entries.erase(removeIt);
  574. }
  575. }
  576. // copy from the list box to the cache manager
  577. void cmCursesMainForm::FillCacheManagerFromUI()
  578. {
  579. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  580. const std::string& cacheKey = entry.Key;
  581. const char* existingValue =
  582. this->CMakeInstance->GetState()->GetCacheEntryValue(cacheKey);
  583. if (existingValue) {
  584. std::string oldValue = existingValue;
  585. std::string newValue = entry.Entry->GetValue();
  586. std::string fixedOldValue;
  587. std::string fixedNewValue;
  588. cmStateEnums::CacheEntryType t =
  589. this->CMakeInstance->GetState()->GetCacheEntryType(cacheKey);
  590. this->FixValue(t, oldValue, fixedOldValue);
  591. this->FixValue(t, newValue, fixedNewValue);
  592. if (!(fixedOldValue == fixedNewValue)) {
  593. // The user has changed the value. Mark it as modified.
  594. this->CMakeInstance->GetState()->SetCacheEntryBoolProperty(
  595. cacheKey, "MODIFIED", true);
  596. this->CMakeInstance->GetState()->SetCacheEntryValue(cacheKey,
  597. fixedNewValue);
  598. }
  599. }
  600. }
  601. }
  602. void cmCursesMainForm::FixValue(cmStateEnums::CacheEntryType type,
  603. const std::string& in, std::string& out) const
  604. {
  605. out = in.substr(0, in.find_last_not_of(' ') + 1);
  606. if (type == cmStateEnums::PATH || type == cmStateEnums::FILEPATH) {
  607. cmSystemTools::ConvertToUnixSlashes(out);
  608. }
  609. if (type == cmStateEnums::BOOL) {
  610. if (cmIsOff(out)) {
  611. out = "OFF";
  612. } else {
  613. out = "ON";
  614. }
  615. }
  616. }
  617. void cmCursesMainForm::HandleInput()
  618. {
  619. int x = 0;
  620. int y = 0;
  621. if (!this->Form) {
  622. return;
  623. }
  624. FIELD* currentField;
  625. cmCursesWidget* currentWidget;
  626. char debugMessage[128];
  627. for (;;) {
  628. this->UpdateStatusBar();
  629. this->PrintKeys();
  630. if (this->SearchMode) {
  631. std::string searchstr = "Search: " + this->SearchString;
  632. this->UpdateStatusBar(searchstr.c_str());
  633. this->PrintKeys(1);
  634. curses_move(y - 5, static_cast<unsigned int>(searchstr.size()));
  635. // curses_move(1,1);
  636. touchwin(stdscr);
  637. refresh();
  638. }
  639. int key = getch();
  640. getmaxyx(stdscr, y, x);
  641. // If window too small, handle 'q' only
  642. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  643. // quit
  644. if (key == 'q') {
  645. break;
  646. }
  647. continue;
  648. }
  649. currentField = current_field(this->Form);
  650. currentWidget =
  651. reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  652. bool widgetHandled = false;
  653. if (this->SearchMode) {
  654. if (key == 10 || key == KEY_ENTER) {
  655. this->SearchMode = false;
  656. if (!this->SearchString.empty()) {
  657. this->JumpToCacheEntry(this->SearchString.c_str());
  658. this->OldSearchString = this->SearchString;
  659. }
  660. this->SearchString.clear();
  661. }
  662. /*
  663. else if ( key == KEY_ESCAPE )
  664. {
  665. this->SearchMode = false;
  666. }
  667. */
  668. else if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') ||
  669. (key >= '0' && key <= '9') || (key == '_')) {
  670. if (this->SearchString.size() <
  671. static_cast<std::string::size_type>(x - 10)) {
  672. this->SearchString += static_cast<char>(key);
  673. }
  674. } else if (key == ctrl('h') || key == KEY_BACKSPACE || key == KEY_DC) {
  675. if (!this->SearchString.empty()) {
  676. this->SearchString.pop_back();
  677. }
  678. }
  679. } else if (currentWidget && !this->SearchMode) {
  680. // Ask the current widget if it wants to handle input
  681. widgetHandled = currentWidget->HandleInput(key, this, stdscr);
  682. if (widgetHandled) {
  683. this->OkToGenerate = false;
  684. this->UpdateStatusBar();
  685. this->PrintKeys();
  686. }
  687. }
  688. if ((!currentWidget || !widgetHandled) && !this->SearchMode) {
  689. // If the current widget does not want to handle input,
  690. // we handle it.
  691. sprintf(debugMessage, "Main form handling input, key: %d", key);
  692. cmCursesForm::LogMessage(debugMessage);
  693. // quit
  694. if (key == 'q') {
  695. break;
  696. }
  697. // if not end of page, next field otherwise next page
  698. // each entry consists of fields: label, isnew, value
  699. // therefore, the label field for the prev. entry is index-5
  700. // and the label field for the next entry is index+1
  701. // (index always corresponds to the value field)
  702. // scroll down with arrow down, ctrl+n (emacs binding), or j (vim
  703. // binding)
  704. if (key == KEY_DOWN || key == ctrl('n') || key == 'j') {
  705. FIELD* cur = current_field(this->Form);
  706. size_t findex = field_index(cur);
  707. if (findex == 3 * this->NumberOfVisibleEntries - 1) {
  708. continue;
  709. }
  710. if (new_page(this->Fields[findex + 1])) {
  711. form_driver(this->Form, REQ_NEXT_PAGE);
  712. } else {
  713. form_driver(this->Form, REQ_NEXT_FIELD);
  714. }
  715. }
  716. // if not beginning of page, previous field, otherwise previous page
  717. // each entry consists of fields: label, isnew, value
  718. // therefore, the label field for the prev. entry is index-5
  719. // and the label field for the next entry is index+1
  720. // (index always corresponds to the value field)
  721. // scroll down with arrow up, ctrl+p (emacs binding), or k (vim binding)
  722. else if (key == KEY_UP || key == ctrl('p') || key == 'k') {
  723. FIELD* cur = current_field(this->Form);
  724. int findex = field_index(cur);
  725. if (findex == 2) {
  726. continue;
  727. }
  728. if (new_page(this->Fields[findex - 2])) {
  729. form_driver(this->Form, REQ_PREV_PAGE);
  730. set_current_field(this->Form, this->Fields[findex - 3]);
  731. } else {
  732. form_driver(this->Form, REQ_PREV_FIELD);
  733. }
  734. }
  735. // pg down
  736. else if (key == KEY_NPAGE || key == ctrl('d')) {
  737. form_driver(this->Form, REQ_NEXT_PAGE);
  738. }
  739. // pg up
  740. else if (key == KEY_PPAGE || key == ctrl('u')) {
  741. form_driver(this->Form, REQ_PREV_PAGE);
  742. }
  743. // configure
  744. else if (key == 'c') {
  745. this->Configure();
  746. }
  747. // display help
  748. else if (key == 'h') {
  749. getmaxyx(stdscr, y, x);
  750. FIELD* cur = current_field(this->Form);
  751. int findex = field_index(cur);
  752. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(
  753. field_userptr(this->Fields[findex - 2]));
  754. const char* curField = lbl->GetValue();
  755. const char* helpString = nullptr;
  756. const char* existingValue =
  757. this->CMakeInstance->GetState()->GetCacheEntryValue(curField);
  758. if (existingValue) {
  759. helpString = this->CMakeInstance->GetState()->GetCacheEntryProperty(
  760. curField, "HELPSTRING");
  761. }
  762. if (helpString) {
  763. this->HelpMessage[1] =
  764. cmStrCat("Current option is: ", curField, '\n',
  765. "Help string for this option is: ", helpString, '\n');
  766. } else {
  767. this->HelpMessage[1] = "";
  768. }
  769. cmCursesLongMessageForm* msgs =
  770. new cmCursesLongMessageForm(this->HelpMessage, "Help");
  771. CurrentForm = msgs;
  772. msgs->Render(1, 1, x, y);
  773. msgs->HandleInput();
  774. CurrentForm = this;
  775. this->Render(1, 1, x, y);
  776. set_current_field(this->Form, cur);
  777. }
  778. // display last errors
  779. else if (key == 'l') {
  780. getmaxyx(stdscr, y, x);
  781. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(
  782. this->Outputs, "CMake produced the following output");
  783. CurrentForm = msgs;
  784. msgs->Render(1, 1, x, y);
  785. msgs->HandleInput();
  786. CurrentForm = this;
  787. this->Render(1, 1, x, y);
  788. } else if (key == '/') {
  789. this->SearchMode = true;
  790. this->UpdateStatusBar("Search");
  791. this->PrintKeys(1);
  792. touchwin(stdscr);
  793. refresh();
  794. } else if (key == 'n') {
  795. if (!this->OldSearchString.empty()) {
  796. this->JumpToCacheEntry(this->OldSearchString.c_str());
  797. }
  798. }
  799. // switch advanced on/off
  800. else if (key == 't') {
  801. if (this->AdvancedMode) {
  802. this->AdvancedMode = false;
  803. } else {
  804. this->AdvancedMode = true;
  805. }
  806. getmaxyx(stdscr, y, x);
  807. this->RePost();
  808. this->Render(1, 1, x, y);
  809. }
  810. // generate and exit
  811. else if (key == 'g') {
  812. if (this->OkToGenerate) {
  813. this->Generate();
  814. break;
  815. }
  816. }
  817. // delete cache entry
  818. else if (key == 'd' && this->NumberOfVisibleEntries) {
  819. this->OkToGenerate = false;
  820. FIELD* cur = current_field(this->Form);
  821. size_t findex = field_index(cur);
  822. // make the next or prev. current field after deletion
  823. // each entry consists of fields: label, isnew, value
  824. // therefore, the label field for the prev. entry is findex-5
  825. // and the label field for the next entry is findex+1
  826. // (findex always corresponds to the value field)
  827. FIELD* nextCur;
  828. if (findex == 2) {
  829. nextCur = nullptr;
  830. } else if (findex == 3 * this->NumberOfVisibleEntries - 1) {
  831. nextCur = this->Fields[findex - 5];
  832. } else {
  833. nextCur = this->Fields[findex + 1];
  834. }
  835. // Get the label widget
  836. // each entry consists of fields: label, isnew, value
  837. // therefore, the label field for the is findex-2
  838. // (findex always corresponds to the value field)
  839. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(
  840. field_userptr(this->Fields[findex - 2]));
  841. if (lbl) {
  842. this->CMakeInstance->GetState()->RemoveCacheEntry(lbl->GetValue());
  843. std::string nextVal;
  844. if (nextCur) {
  845. nextVal =
  846. (reinterpret_cast<cmCursesWidget*>(field_userptr(nextCur))
  847. ->GetValue());
  848. }
  849. getmaxyx(stdscr, y, x);
  850. this->RemoveEntry(lbl->GetValue());
  851. this->RePost();
  852. this->Render(1, 1, x, y);
  853. if (nextCur) {
  854. // make the next or prev. current field after deletion
  855. auto nextEntryIt = std::find_if(
  856. this->Entries.begin(), this->Entries.end(),
  857. [&nextVal](cmCursesCacheEntryComposite const& entry) {
  858. return nextVal == entry.Key;
  859. });
  860. if (nextEntryIt != this->Entries.end()) {
  861. set_current_field(this->Form, nextEntryIt->Entry->Field);
  862. }
  863. }
  864. }
  865. }
  866. }
  867. touchwin(stdscr);
  868. wrefresh(stdscr);
  869. }
  870. }
  871. int cmCursesMainForm::LoadCache(const char* /*unused*/)
  872. {
  873. int r = this->CMakeInstance->LoadCache();
  874. if (r < 0) {
  875. return r;
  876. }
  877. this->CMakeInstance->SetCacheArgs(this->Args);
  878. this->CMakeInstance->PreLoadCMakeFiles();
  879. return r;
  880. }
  881. void cmCursesMainForm::JumpToCacheEntry(const char* astr)
  882. {
  883. std::string str;
  884. if (astr) {
  885. str = cmSystemTools::LowerCase(astr);
  886. }
  887. if (str.empty()) {
  888. return;
  889. }
  890. FIELD* cur = current_field(this->Form);
  891. int start_index = field_index(cur);
  892. int findex = start_index;
  893. for (;;) {
  894. if (!str.empty()) {
  895. cmCursesWidget* lbl = nullptr;
  896. if (findex >= 0) {
  897. lbl = reinterpret_cast<cmCursesWidget*>(
  898. field_userptr(this->Fields[findex - 2]));
  899. }
  900. if (lbl) {
  901. const char* curField = lbl->GetValue();
  902. if (curField) {
  903. std::string cfld = cmSystemTools::LowerCase(curField);
  904. if (cfld.find(str) != std::string::npos && findex != start_index) {
  905. break;
  906. }
  907. }
  908. }
  909. }
  910. if (size_t(findex) >= 3 * this->NumberOfVisibleEntries - 1) {
  911. set_current_field(this->Form, this->Fields[2]);
  912. } else if (new_page(this->Fields[findex + 1])) {
  913. form_driver(this->Form, REQ_NEXT_PAGE);
  914. } else {
  915. form_driver(this->Form, REQ_NEXT_FIELD);
  916. }
  917. /*
  918. char buffer[1024];
  919. sprintf(buffer, "Line: %d != %d / %d\n", findex, idx,
  920. this->NumberOfVisibleEntries);
  921. touchwin(stdscr);
  922. refresh();
  923. this->UpdateStatusBar( buffer );
  924. usleep(100000);
  925. */
  926. cur = current_field(this->Form);
  927. findex = field_index(cur);
  928. if (findex == start_index) {
  929. break;
  930. }
  931. }
  932. }
  933. void cmCursesMainForm::ResetOutputs()
  934. {
  935. this->LogForm.reset();
  936. this->Outputs.clear();
  937. this->HasNonStatusOutputs = false;
  938. this->LastProgress.clear();
  939. }
  940. void cmCursesMainForm::DisplayOutputs()
  941. {
  942. int xi;
  943. int yi;
  944. getmaxyx(stdscr, yi, xi);
  945. auto newLogForm =
  946. new cmCursesLongMessageForm(this->Outputs, this->LastProgress.c_str());
  947. CurrentForm = newLogForm;
  948. this->LogForm.reset(newLogForm);
  949. this->LogForm->Render(1, 1, xi, yi);
  950. this->LogForm->ScrollDown();
  951. }
  952. const char* cmCursesMainForm::s_ConstHelpMessage =
  953. "CMake is used to configure and generate build files for software projects. "
  954. "The basic steps for configuring a project with ccmake are as follows:\n\n"
  955. "1. Run ccmake in the directory where you want the object and executable "
  956. "files to be placed (build directory). If the source directory is not the "
  957. "same as this build directory, you have to specify it as an argument on the "
  958. "command line.\n\n"
  959. "2. When ccmake is run, it will read the configuration files and display "
  960. "the current build options. "
  961. "If you have run CMake before and have updated the configuration files "
  962. "since then, any new entries will be displayed on top and will be marked "
  963. "with a *. "
  964. "On the other hand, the first time you run ccmake, all build options will "
  965. "be new and will be marked as such. "
  966. "At this point, you can modify any options (see keys below) you want to "
  967. "change. "
  968. "When you are satisfied with your changes, press 'c' to have CMake process "
  969. "the configuration files. "
  970. "Please note that changing some options may cause new ones to appear. These "
  971. "will be shown on top and will be marked with *. "
  972. "Repeat this procedure until you are satisfied with all the options and "
  973. "there are no new entries. "
  974. "At this point, a new command will appear: G)enerate and Exit. You can now "
  975. "hit 'g' to have CMake generate all the build files (i.e. makefiles or "
  976. "project files) and exit. "
  977. "At any point during the process, you can exit ccmake with 'q'. However, "
  978. "this will not generate/change any build files.\n\n"
  979. "ccmake KEYS:\n\n"
  980. "Navigation: "
  981. "You can use the arrow keys and page up, down to navigate the options. "
  982. "Alternatively, you can use the following keys: \n"
  983. " C-n or j : next option\n"
  984. " C-p or k : previous options\n"
  985. " C-d : down one page\n"
  986. " C-u : up one page\n\n"
  987. "Editing options: "
  988. "To change an option press enter or return. If the current options is a "
  989. "boolean, this will toggle its value. "
  990. "Otherwise, ccmake will enter edit mode. Alternatively, you can toggle "
  991. "a bool variable by pressing space, and enter edit mode with i."
  992. "In this mode you can edit an option using arrow keys and backspace. "
  993. "Alternatively, you can use the following keys:\n"
  994. " C-b : back one character\n"
  995. " C-f : forward one character\n"
  996. " C-a : go to the beginning of the field\n"
  997. " C-e : go to the end of the field\n"
  998. " C-d : delete previous character\n"
  999. " C-k : kill the rest of the field\n"
  1000. " Esc : Restore field (discard last changes)\n"
  1001. " Enter : Leave edit mode\n"
  1002. "Commands:\n"
  1003. " q : quit ccmake without generating build files\n"
  1004. " h : help, shows this screen\n"
  1005. " c : process the configuration files with the current options\n"
  1006. " g : generate build files and exit, only available when there are no "
  1007. "new options and no errors have been detected during last configuration.\n"
  1008. " l : shows cmake output\n"
  1009. " d : delete an option\n"
  1010. " t : toggles advanced mode. In normal mode, only the most important "
  1011. "options are shown. In advanced mode, all options are shown. We recommend "
  1012. "using normal mode unless you are an expert.\n"
  1013. " / : search for a variable name.\n";