cmCursesMainForm.cxx 35 KB

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