1
0

cmCursesMainForm.cxx 36 KB

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