cmCursesMainForm.cxx 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. "Press [c] to configure Press [g] to generate and exit");
  293. } else {
  294. sprintf(firstLine,
  295. "Press [c] to configure ");
  296. }
  297. {
  298. const char* toggleKeyInstruction =
  299. "Press [t] to toggle advanced mode (Currently %s)";
  300. sprintf(thirdLine, toggleKeyInstruction,
  301. this->AdvancedMode ? "On" : "Off");
  302. }
  303. sprintf(secondLine,
  304. "Press [h] for help "
  305. "Press [q] to quit without generating");
  306. }
  307. curses_move(y - 4, 0);
  308. char fmt[512] =
  309. "Press [enter] to edit option Press [d] to 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. std::string status = (percentCompleted < 100 ? " " : "");
  432. status += (percentCompleted < 10 ? " " : "");
  433. status += std::to_string(percentCompleted) + "% [";
  434. status.append(progressBarCompleted, '#');
  435. status.append(progressBarWidth - progressBarCompleted, ' ');
  436. status += "] " + msg + "...";
  437. this->UpdateStatusBar(status.c_str());
  438. } else {
  439. this->Outputs.emplace_back(msg);
  440. }
  441. this->PrintKeys(1);
  442. curses_move(1, 1);
  443. touchwin(stdscr);
  444. refresh();
  445. }
  446. int cmCursesMainForm::Configure(int noconfigure)
  447. {
  448. int xi;
  449. int yi;
  450. getmaxyx(stdscr, yi, xi);
  451. this->UpdateProgress("Configuring", 0);
  452. this->CMakeInstance->SetProgressCallback(
  453. [this](const std::string& msg, float prog) {
  454. this->UpdateProgress(msg, prog);
  455. });
  456. // always save the current gui values to disk
  457. this->FillCacheManagerFromUI();
  458. this->CMakeInstance->SaveCache(
  459. this->CMakeInstance->GetHomeOutputDirectory());
  460. this->LoadCache(nullptr);
  461. this->ResetOutputs();
  462. // run the generate process
  463. this->OkToGenerate = true;
  464. int retVal;
  465. if (noconfigure) {
  466. retVal = this->CMakeInstance->DoPreConfigureChecks();
  467. this->OkToGenerate = false;
  468. if (retVal > 0) {
  469. retVal = 0;
  470. }
  471. } else {
  472. retVal = this->CMakeInstance->Configure();
  473. }
  474. this->CMakeInstance->SetProgressCallback(nullptr);
  475. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  476. if (retVal != 0 || this->HasNonStatusOutputs) {
  477. // see if there was an error
  478. if (cmSystemTools::GetErrorOccuredFlag()) {
  479. this->OkToGenerate = false;
  480. }
  481. int xx;
  482. int yy;
  483. getmaxyx(stdscr, yy, xx);
  484. const char* title = "Configure produced the following output.";
  485. if (cmSystemTools::GetErrorOccuredFlag()) {
  486. title = "Configure failed with the following output.";
  487. }
  488. cmCursesLongMessageForm* msgs =
  489. new cmCursesLongMessageForm(this->Outputs, title);
  490. // reset error condition
  491. cmSystemTools::ResetErrorOccuredFlag();
  492. CurrentForm = msgs;
  493. msgs->Render(1, 1, xx, yy);
  494. msgs->HandleInput();
  495. // If they typed the wrong source directory, we report
  496. // an error and exit
  497. if (retVal == -2) {
  498. return retVal;
  499. }
  500. CurrentForm = this;
  501. this->Render(1, 1, xx, yy);
  502. }
  503. this->InitializeUI();
  504. this->Render(1, 1, xi, yi);
  505. return 0;
  506. }
  507. int cmCursesMainForm::Generate()
  508. {
  509. int xi;
  510. int yi;
  511. getmaxyx(stdscr, yi, xi);
  512. this->UpdateProgress("Generating", 0);
  513. this->CMakeInstance->SetProgressCallback(
  514. [this](const std::string& msg, float prog) {
  515. this->UpdateProgress(msg, prog);
  516. });
  517. this->ResetOutputs();
  518. // run the generate process
  519. int retVal = this->CMakeInstance->Generate();
  520. this->CMakeInstance->SetProgressCallback(nullptr);
  521. keypad(stdscr, true); /* Use key symbols as KEY_DOWN */
  522. if (retVal != 0 || this->HasNonStatusOutputs) {
  523. // see if there was an error
  524. if (cmSystemTools::GetErrorOccuredFlag()) {
  525. this->OkToGenerate = false;
  526. }
  527. // reset error condition
  528. cmSystemTools::ResetErrorOccuredFlag();
  529. int xx;
  530. int yy;
  531. getmaxyx(stdscr, yy, xx);
  532. const char* title = "Generate produced the following output.";
  533. if (cmSystemTools::GetErrorOccuredFlag()) {
  534. title = "Generate failed with the following output.";
  535. }
  536. cmCursesLongMessageForm* msgs =
  537. new cmCursesLongMessageForm(this->Outputs, title);
  538. CurrentForm = msgs;
  539. msgs->Render(1, 1, xx, yy);
  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. }
  559. void cmCursesMainForm::RemoveEntry(const char* value)
  560. {
  561. if (!value) {
  562. return;
  563. }
  564. auto removeIt =
  565. std::find_if(this->Entries.begin(), this->Entries.end(),
  566. [value](cmCursesCacheEntryComposite& entry) -> bool {
  567. const char* val = entry.GetValue();
  568. return val != nullptr && !strcmp(value, val);
  569. });
  570. if (removeIt != this->Entries.end()) {
  571. this->CMakeInstance->UnwatchUnusedCli(value);
  572. this->Entries.erase(removeIt);
  573. }
  574. }
  575. // copy from the list box to the cache manager
  576. void cmCursesMainForm::FillCacheManagerFromUI()
  577. {
  578. for (cmCursesCacheEntryComposite& entry : this->Entries) {
  579. const std::string& cacheKey = entry.Key;
  580. const char* existingValue =
  581. this->CMakeInstance->GetState()->GetCacheEntryValue(cacheKey);
  582. if (existingValue) {
  583. std::string oldValue = existingValue;
  584. std::string newValue = entry.Entry->GetValue();
  585. std::string fixedOldValue;
  586. std::string fixedNewValue;
  587. cmStateEnums::CacheEntryType t =
  588. this->CMakeInstance->GetState()->GetCacheEntryType(cacheKey);
  589. this->FixValue(t, oldValue, fixedOldValue);
  590. this->FixValue(t, newValue, fixedNewValue);
  591. if (!(fixedOldValue == fixedNewValue)) {
  592. // The user has changed the value. Mark it as modified.
  593. this->CMakeInstance->GetState()->SetCacheEntryBoolProperty(
  594. cacheKey, "MODIFIED", true);
  595. this->CMakeInstance->GetState()->SetCacheEntryValue(cacheKey,
  596. fixedNewValue);
  597. }
  598. }
  599. }
  600. }
  601. void cmCursesMainForm::FixValue(cmStateEnums::CacheEntryType type,
  602. const std::string& in, std::string& out) const
  603. {
  604. out = in.substr(0, in.find_last_not_of(' ') + 1);
  605. if (type == cmStateEnums::PATH || type == cmStateEnums::FILEPATH) {
  606. cmSystemTools::ConvertToUnixSlashes(out);
  607. }
  608. if (type == cmStateEnums::BOOL) {
  609. if (cmIsOff(out)) {
  610. out = "OFF";
  611. } else {
  612. out = "ON";
  613. }
  614. }
  615. }
  616. void cmCursesMainForm::HandleInput()
  617. {
  618. int x = 0;
  619. int y = 0;
  620. if (!this->Form) {
  621. return;
  622. }
  623. FIELD* currentField;
  624. cmCursesWidget* currentWidget;
  625. char debugMessage[128];
  626. for (;;) {
  627. this->UpdateStatusBar();
  628. this->PrintKeys();
  629. if (this->SearchMode) {
  630. std::string searchstr = "Search: " + this->SearchString;
  631. this->UpdateStatusBar(searchstr.c_str());
  632. this->PrintKeys(1);
  633. curses_move(y - 5, static_cast<unsigned int>(searchstr.size()));
  634. // curses_move(1,1);
  635. touchwin(stdscr);
  636. refresh();
  637. }
  638. int key = getch();
  639. getmaxyx(stdscr, y, x);
  640. // If window too small, handle 'q' only
  641. if (x < cmCursesMainForm::MIN_WIDTH || y < cmCursesMainForm::MIN_HEIGHT) {
  642. // quit
  643. if (key == 'q') {
  644. break;
  645. }
  646. continue;
  647. }
  648. currentField = current_field(this->Form);
  649. currentWidget =
  650. reinterpret_cast<cmCursesWidget*>(field_userptr(currentField));
  651. bool widgetHandled = false;
  652. if (this->SearchMode) {
  653. if (key == 10 || key == KEY_ENTER) {
  654. this->SearchMode = false;
  655. if (!this->SearchString.empty()) {
  656. this->JumpToCacheEntry(this->SearchString.c_str());
  657. this->OldSearchString = this->SearchString;
  658. }
  659. this->SearchString.clear();
  660. }
  661. /*
  662. else if ( key == KEY_ESCAPE )
  663. {
  664. this->SearchMode = false;
  665. }
  666. */
  667. else if ((key >= 'a' && key <= 'z') || (key >= 'A' && key <= 'Z') ||
  668. (key >= '0' && key <= '9') || (key == '_')) {
  669. if (this->SearchString.size() <
  670. static_cast<std::string::size_type>(x - 10)) {
  671. this->SearchString += static_cast<char>(key);
  672. }
  673. } else if (key == ctrl('h') || key == KEY_BACKSPACE || key == KEY_DC) {
  674. if (!this->SearchString.empty()) {
  675. this->SearchString.pop_back();
  676. }
  677. }
  678. } else if (currentWidget && !this->SearchMode) {
  679. // Ask the current widget if it wants to handle input
  680. widgetHandled = currentWidget->HandleInput(key, this, stdscr);
  681. if (widgetHandled) {
  682. this->OkToGenerate = false;
  683. this->UpdateStatusBar();
  684. this->PrintKeys();
  685. }
  686. }
  687. if ((!currentWidget || !widgetHandled) && !this->SearchMode) {
  688. // If the current widget does not want to handle input,
  689. // we handle it.
  690. sprintf(debugMessage, "Main form handling input, key: %d", key);
  691. cmCursesForm::LogMessage(debugMessage);
  692. // quit
  693. if (key == 'q') {
  694. break;
  695. }
  696. // if not end of page, next field otherwise next page
  697. // each entry consists of fields: label, isnew, value
  698. // therefore, the label field for the prev. entry is index-5
  699. // and the label field for the next entry is index+1
  700. // (index always corresponds to the value field)
  701. // scroll down with arrow down, ctrl+n (emacs binding), or j (vim
  702. // binding)
  703. if (key == KEY_DOWN || key == ctrl('n') || key == 'j') {
  704. FIELD* cur = current_field(this->Form);
  705. size_t findex = field_index(cur);
  706. if (findex == 3 * this->NumberOfVisibleEntries - 1) {
  707. continue;
  708. }
  709. if (new_page(this->Fields[findex + 1])) {
  710. form_driver(this->Form, REQ_NEXT_PAGE);
  711. } else {
  712. form_driver(this->Form, REQ_NEXT_FIELD);
  713. }
  714. }
  715. // if not beginning of page, previous field, otherwise previous page
  716. // each entry consists of fields: label, isnew, value
  717. // therefore, the label field for the prev. entry is index-5
  718. // and the label field for the next entry is index+1
  719. // (index always corresponds to the value field)
  720. // scroll down with arrow up, ctrl+p (emacs binding), or k (vim binding)
  721. else if (key == KEY_UP || key == ctrl('p') || key == 'k') {
  722. FIELD* cur = current_field(this->Form);
  723. int findex = field_index(cur);
  724. if (findex == 2) {
  725. continue;
  726. }
  727. if (new_page(this->Fields[findex - 2])) {
  728. form_driver(this->Form, REQ_PREV_PAGE);
  729. set_current_field(this->Form, this->Fields[findex - 3]);
  730. } else {
  731. form_driver(this->Form, REQ_PREV_FIELD);
  732. }
  733. }
  734. // pg down
  735. else if (key == KEY_NPAGE || key == ctrl('d')) {
  736. form_driver(this->Form, REQ_NEXT_PAGE);
  737. }
  738. // pg up
  739. else if (key == KEY_PPAGE || key == ctrl('u')) {
  740. form_driver(this->Form, REQ_PREV_PAGE);
  741. }
  742. // configure
  743. else if (key == 'c') {
  744. this->Configure();
  745. }
  746. // display help
  747. else if (key == 'h') {
  748. getmaxyx(stdscr, y, x);
  749. FIELD* cur = current_field(this->Form);
  750. int findex = field_index(cur);
  751. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(
  752. field_userptr(this->Fields[findex - 2]));
  753. const char* curField = lbl->GetValue();
  754. const char* helpString = nullptr;
  755. const char* existingValue =
  756. this->CMakeInstance->GetState()->GetCacheEntryValue(curField);
  757. if (existingValue) {
  758. helpString = this->CMakeInstance->GetState()->GetCacheEntryProperty(
  759. curField, "HELPSTRING");
  760. }
  761. if (helpString) {
  762. this->HelpMessage[1] =
  763. cmStrCat("Current option is: ", curField, '\n',
  764. "Help string for this option is: ", helpString, '\n');
  765. } else {
  766. this->HelpMessage[1] = "";
  767. }
  768. cmCursesLongMessageForm* msgs =
  769. new cmCursesLongMessageForm(this->HelpMessage, "Help.");
  770. CurrentForm = msgs;
  771. msgs->Render(1, 1, x, y);
  772. msgs->HandleInput();
  773. CurrentForm = this;
  774. this->Render(1, 1, x, y);
  775. set_current_field(this->Form, cur);
  776. }
  777. // display last errors
  778. else if (key == 'l') {
  779. getmaxyx(stdscr, y, x);
  780. cmCursesLongMessageForm* msgs = new cmCursesLongMessageForm(
  781. this->Outputs, "CMake produced the following output.");
  782. CurrentForm = msgs;
  783. msgs->Render(1, 1, x, y);
  784. msgs->HandleInput();
  785. CurrentForm = this;
  786. this->Render(1, 1, x, y);
  787. } else if (key == '/') {
  788. this->SearchMode = true;
  789. this->UpdateStatusBar("Search");
  790. this->PrintKeys(1);
  791. touchwin(stdscr);
  792. refresh();
  793. } else if (key == 'n') {
  794. if (!this->OldSearchString.empty()) {
  795. this->JumpToCacheEntry(this->OldSearchString.c_str());
  796. }
  797. }
  798. // switch advanced on/off
  799. else if (key == 't') {
  800. if (this->AdvancedMode) {
  801. this->AdvancedMode = false;
  802. } else {
  803. this->AdvancedMode = true;
  804. }
  805. getmaxyx(stdscr, y, x);
  806. this->RePost();
  807. this->Render(1, 1, x, y);
  808. }
  809. // generate and exit
  810. else if (key == 'g') {
  811. if (this->OkToGenerate) {
  812. this->Generate();
  813. break;
  814. }
  815. }
  816. // delete cache entry
  817. else if (key == 'd' && this->NumberOfVisibleEntries) {
  818. this->OkToGenerate = false;
  819. FIELD* cur = current_field(this->Form);
  820. size_t findex = field_index(cur);
  821. // make the next or prev. current field after deletion
  822. // each entry consists of fields: label, isnew, value
  823. // therefore, the label field for the prev. entry is findex-5
  824. // and the label field for the next entry is findex+1
  825. // (findex always corresponds to the value field)
  826. FIELD* nextCur;
  827. if (findex == 2) {
  828. nextCur = nullptr;
  829. } else if (findex == 3 * this->NumberOfVisibleEntries - 1) {
  830. nextCur = this->Fields[findex - 5];
  831. } else {
  832. nextCur = this->Fields[findex + 1];
  833. }
  834. // Get the label widget
  835. // each entry consists of fields: label, isnew, value
  836. // therefore, the label field for the is findex-2
  837. // (findex always corresponds to the value field)
  838. cmCursesWidget* lbl = reinterpret_cast<cmCursesWidget*>(
  839. field_userptr(this->Fields[findex - 2]));
  840. if (lbl) {
  841. this->CMakeInstance->GetState()->RemoveCacheEntry(lbl->GetValue());
  842. std::string nextVal;
  843. if (nextCur) {
  844. nextVal =
  845. (reinterpret_cast<cmCursesWidget*>(field_userptr(nextCur))
  846. ->GetValue());
  847. }
  848. getmaxyx(stdscr, y, x);
  849. this->RemoveEntry(lbl->GetValue());
  850. this->RePost();
  851. this->Render(1, 1, x, y);
  852. if (nextCur) {
  853. // make the next or prev. current field after deletion
  854. auto nextEntryIt = std::find_if(
  855. this->Entries.begin(), this->Entries.end(),
  856. [&nextVal](cmCursesCacheEntryComposite const& entry) {
  857. return nextVal == entry.Key;
  858. });
  859. if (nextEntryIt != this->Entries.end()) {
  860. set_current_field(this->Form, nextEntryIt->Entry->Field);
  861. }
  862. }
  863. }
  864. }
  865. }
  866. touchwin(stdscr);
  867. wrefresh(stdscr);
  868. }
  869. }
  870. int cmCursesMainForm::LoadCache(const char* /*unused*/)
  871. {
  872. int r = this->CMakeInstance->LoadCache();
  873. if (r < 0) {
  874. return r;
  875. }
  876. this->CMakeInstance->SetCacheArgs(this->Args);
  877. this->CMakeInstance->PreLoadCMakeFiles();
  878. return r;
  879. }
  880. void cmCursesMainForm::JumpToCacheEntry(const char* astr)
  881. {
  882. std::string str;
  883. if (astr) {
  884. str = cmSystemTools::LowerCase(astr);
  885. }
  886. if (str.empty()) {
  887. return;
  888. }
  889. FIELD* cur = current_field(this->Form);
  890. int start_index = field_index(cur);
  891. int findex = start_index;
  892. for (;;) {
  893. if (!str.empty()) {
  894. cmCursesWidget* lbl = nullptr;
  895. if (findex >= 0) {
  896. lbl = reinterpret_cast<cmCursesWidget*>(
  897. field_userptr(this->Fields[findex - 2]));
  898. }
  899. if (lbl) {
  900. const char* curField = lbl->GetValue();
  901. if (curField) {
  902. std::string cfld = cmSystemTools::LowerCase(curField);
  903. if (cfld.find(str) != std::string::npos && findex != start_index) {
  904. break;
  905. }
  906. }
  907. }
  908. }
  909. if (size_t(findex) >= 3 * this->NumberOfVisibleEntries - 1) {
  910. set_current_field(this->Form, this->Fields[2]);
  911. } else if (new_page(this->Fields[findex + 1])) {
  912. form_driver(this->Form, REQ_NEXT_PAGE);
  913. } else {
  914. form_driver(this->Form, REQ_NEXT_FIELD);
  915. }
  916. /*
  917. char buffer[1024];
  918. sprintf(buffer, "Line: %d != %d / %d\n", findex, idx,
  919. this->NumberOfVisibleEntries);
  920. touchwin(stdscr);
  921. refresh();
  922. this->UpdateStatusBar( buffer );
  923. usleep(100000);
  924. */
  925. cur = current_field(this->Form);
  926. findex = field_index(cur);
  927. if (findex == start_index) {
  928. break;
  929. }
  930. }
  931. }
  932. void cmCursesMainForm::ResetOutputs()
  933. {
  934. this->Outputs.clear();
  935. this->HasNonStatusOutputs = false;
  936. }
  937. const char* cmCursesMainForm::s_ConstHelpMessage =
  938. "CMake is used to configure and generate build files for software projects. "
  939. "The basic steps for configuring a project with ccmake are as follows:\n\n"
  940. "1. Run ccmake in the directory where you want the object and executable "
  941. "files to be placed (build directory). If the source directory is not the "
  942. "same as this build directory, you have to specify it as an argument on the "
  943. "command line.\n\n"
  944. "2. When ccmake is run, it will read the configuration files and display "
  945. "the current build options. "
  946. "If you have run CMake before and have updated the configuration files "
  947. "since then, any new entries will be displayed on top and will be marked "
  948. "with a *. "
  949. "On the other hand, the first time you run ccmake, all build options will "
  950. "be new and will be marked as such. "
  951. "At this point, you can modify any options (see keys below) you want to "
  952. "change. "
  953. "When you are satisfied with your changes, press 'c' to have CMake process "
  954. "the configuration files. "
  955. "Please note that changing some options may cause new ones to appear. These "
  956. "will be shown on top and will be marked with *. "
  957. "Repeat this procedure until you are satisfied with all the options and "
  958. "there are no new entries. "
  959. "At this point, a new command will appear: G)enerate and Exit. You can now "
  960. "hit 'g' to have CMake generate all the build files (i.e. makefiles or "
  961. "project files) and exit. "
  962. "At any point during the process, you can exit ccmake with 'q'. However, "
  963. "this will not generate/change any build files.\n\n"
  964. "ccmake KEYS:\n\n"
  965. "Navigation: "
  966. "You can use the arrow keys and page up, down to navigate the options. "
  967. "Alternatively, you can use the following keys: \n"
  968. " C-n or j : next option\n"
  969. " C-p or k : previous options\n"
  970. " C-d : down one page\n"
  971. " C-u : up one page\n\n"
  972. "Editing options: "
  973. "To change an option press enter or return. If the current options is a "
  974. "boolean, this will toggle its value. "
  975. "Otherwise, ccmake will enter edit mode. Alternatively, you can toggle "
  976. "a bool variable by pressing space, and enter edit mode with i."
  977. "In this mode you can edit an option using arrow keys and backspace. "
  978. "Alternatively, you can use the following keys:\n"
  979. " C-b : back one character\n"
  980. " C-f : forward one character\n"
  981. " C-a : go to the beginning of the field\n"
  982. " C-e : go to the end of the field\n"
  983. " C-d : delete previous character\n"
  984. " C-k : kill the rest of the field\n"
  985. " Esc : Restore field (discard last changes)\n"
  986. " Enter : Leave edit mode\n"
  987. "Commands:\n"
  988. " q : quit ccmake without generating build files\n"
  989. " h : help, shows this screen\n"
  990. " c : process the configuration files with the current options\n"
  991. " g : generate build files and exit, only available when there are no "
  992. "new options and no errors have been detected during last configuration.\n"
  993. " l : shows cmake output\n"
  994. " d : delete an option\n"
  995. " t : toggles advanced mode. In normal mode, only the most important "
  996. "options are shown. In advanced mode, all options are shown. We recommend "
  997. "using normal mode unless you are an expert.\n"
  998. " / : search for a variable name.\n";