baselist.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /*
  2. * dselect - Debian package maintenance user interface
  3. * baselist.cc - list of somethings
  4. *
  5. * Copyright (C) 1994,1995 Ian Jackson <ian@chiark.greenend.org.uk>
  6. * Copyright (C) 2001 Wichert Akkerman <wakkerma@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. extern "C" {
  23. #include <config.h>
  24. }
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <assert.h>
  28. #include <ctype.h>
  29. #include <unistd.h>
  30. #include <sys/ioctl.h>
  31. #include <sys/termios.h>
  32. extern "C" {
  33. #include <dpkg.h>
  34. #include <dpkg-db.h>
  35. }
  36. #include "dselect.h"
  37. #include "bindings.h"
  38. void mywerase(WINDOW *win) {
  39. int my,mx,y,x;
  40. getmaxyx(win,my,mx);
  41. for (y=0; y<my; y++) {
  42. wmove(win,y,0); for (x=0; x<mx; x++) waddch(win,' ');
  43. }
  44. wmove(win,0,0);
  45. }
  46. baselist *baselist::signallist= 0;
  47. void baselist::sigwinchhandler(int) {
  48. struct winsize size;
  49. if (debug) fprintf(debug,"baselist::sigwinchhandler(), signallist=%p\n",signallist);
  50. baselist *p= signallist;
  51. p->enddisplay();
  52. endwin(); initscr();
  53. if (ioctl(fileno(stdout), TIOCGWINSZ, &size) != 0) ohshite(_("ioctl(TIOCGWINSZ) failed"));
  54. resizeterm(size.ws_row, size.ws_col); wrefresh(curscr);
  55. p->startdisplay();
  56. if (doupdate() == ERR) ohshite(_("doupdate in SIGWINCH handler failed"));
  57. }
  58. static void cu_sigwinch(int, void **argv) {
  59. struct sigaction *osigactp= (struct sigaction*)argv[0];
  60. sigset_t *oblockedp= (sigset_t*)argv[1];
  61. if (sigaction(SIGWINCH,osigactp,0)) ohshite(_("failed to restore old SIGWINCH sigact"));
  62. delete osigactp;
  63. if (sigprocmask(SIG_SETMASK,oblockedp,0)) ohshite(_("failed to restore old signal mask"));
  64. delete oblockedp;
  65. }
  66. void baselist::setupsigwinch() {
  67. sigemptyset(&sigwinchset);
  68. sigaddset(&sigwinchset,SIGWINCH);
  69. osigactp= new(struct sigaction);
  70. oblockedp= new(sigset_t);
  71. if (sigprocmask(0,0,oblockedp)) ohshite(_("failed to get old signal mask"));
  72. if (sigaction(SIGWINCH,0,osigactp)) ohshite(_("failed to get old SIGWINCH sigact"));
  73. push_cleanup(cu_sigwinch,~0, 0,0, 2,(void*)osigactp,(void*)oblockedp);
  74. if (sigprocmask(SIG_BLOCK,&sigwinchset,0)) ohshite(_("failed to block SIGWINCH"));
  75. memset(&nsigact,0,sizeof(nsigact));
  76. nsigact.sa_handler= sigwinchhandler;
  77. sigemptyset(&nsigact.sa_mask);
  78. //nsigact.sa_flags= SA_INTERRUPT;
  79. if (sigaction(SIGWINCH,&nsigact,0)) ohshite(_("failed to set new SIGWINCH sigact"));
  80. }
  81. void baselist::setheights() {
  82. int y= ymax - (title_height + colheads_height + thisstate_height);
  83. assert(y>=1);
  84. if (showinfo==2 && y>=7) {
  85. list_height= 5;
  86. whatinfo_height= 1;
  87. info_height= y-6;
  88. } else if (showinfo==1 && y>=10) {
  89. list_height= y/2;
  90. info_height= (y-1)/2;
  91. whatinfo_height= 1;
  92. } else {
  93. list_height= y;
  94. info_height= 0;
  95. whatinfo_height= 0;
  96. }
  97. colheads_row= title_height;
  98. list_row= colheads_row + colheads_height;
  99. thisstate_row= list_row + list_height;
  100. info_row= thisstate_row + thisstate_height;
  101. whatinfo_row= ymax - 1;
  102. }
  103. void baselist::startdisplay() {
  104. if (debug) fprintf(debug,"baselist[%p]::startdisplay()\n",this);
  105. cbreak(); noecho(); nonl(); keypad(stdscr,TRUE);
  106. clear(); wnoutrefresh(stdscr);
  107. // find attributes
  108. if (has_colors() && start_color()==OK && COLOR_PAIRS >= numscreenparts) {
  109. int i;
  110. printf("allocing\n");
  111. for (i = 1; i < numscreenparts; i++) {
  112. if (init_pair(i, color[i].fore, color[i].back) != OK)
  113. ohshite(_("failed to allocate colour pair"));
  114. }
  115. /* TODO: should use an array of attr's, indexed by attr name. Oh well. */
  116. list_attr= COLOR_PAIR(list) | color[list].attr;
  117. listsel_attr= COLOR_PAIR(listsel) | color[listsel].attr;
  118. title_attr= COLOR_PAIR(title) | color[title].attr;
  119. thisstate_attr= COLOR_PAIR(thisstate) | color[thisstate].attr;
  120. selstate_attr= COLOR_PAIR(selstate) | color[selstate].attr;
  121. selstatesel_attr= COLOR_PAIR(selstatesel) | color[selstatesel].attr;
  122. colheads_attr= COLOR_PAIR(colheads) | color[colheads].attr;
  123. query_attr= COLOR_PAIR(query) | color[query].attr;
  124. info_attr= COLOR_PAIR(info) | color[info].attr;
  125. info_headattr= COLOR_PAIR(info_head) | color[info_head].attr;
  126. whatinfo_attr= COLOR_PAIR(whatinfo) | color[whatinfo].attr;
  127. helpscreen_attr= COLOR_PAIR(helpscreen) | color[helpscreen].attr;
  128. } else {
  129. /* User defined attributes for B&W mode are not currently supported. */
  130. title_attr= A_REVERSE;
  131. thisstate_attr= A_STANDOUT;
  132. list_attr= 0;
  133. listsel_attr= A_STANDOUT;
  134. selstate_attr= A_BOLD;
  135. selstatesel_attr= A_STANDOUT;
  136. colheads_attr= A_BOLD;
  137. query_attr= title_attr;
  138. info_attr= list_attr;
  139. info_headattr= A_BOLD;
  140. whatinfo_attr= thisstate_attr;
  141. }
  142. // set up windows and pads, based on screen size
  143. getmaxyx(stdscr,ymax,xmax);
  144. title_height= ymax>=6;
  145. colheads_height= ymax>=5;
  146. thisstate_height= ymax>=3;
  147. setheights();
  148. setwidths();
  149. titlewin= newwin(1,xmax, 0,0);
  150. if (!titlewin) ohshite(_("failed to create title window"));
  151. wattrset(titlewin,title_attr);
  152. whatinfowin= newwin(1,xmax, whatinfo_row,0);
  153. if (!whatinfowin) ohshite(_("failed to create whatinfo window"));
  154. wattrset(whatinfowin,whatinfo_attr);
  155. listpad= newpad(nitems+1, total_width);
  156. if (!listpad) ohshite(_("failed to create baselist pad"));
  157. colheadspad= newpad(1, total_width);
  158. if (!colheadspad) ohshite(_("failed to create heading pad"));
  159. wattrset(colheadspad,colheads_attr);
  160. thisstatepad= newpad(1, total_width);
  161. if (!thisstatepad) ohshite(_("failed to create thisstate pad"));
  162. wattrset(thisstatepad,thisstate_attr);
  163. infopad= newpad(MAX_DISPLAY_INFO, total_width);
  164. if (!infopad) ohshite(_("failed to create info pad"));
  165. wattrset(infopad,info_attr);
  166. wbkgdset(infopad, ' ' | info_attr);
  167. querywin= newwin(1,xmax,ymax-1,0);
  168. if (!querywin) ohshite(_("failed to create query window"));
  169. wbkgdset(querywin, ' ' | query_attr);
  170. if (cursorline >= topofscreen + list_height) topofscreen= cursorline;
  171. if (topofscreen > nitems - list_height) topofscreen= nitems - list_height;
  172. if (topofscreen < 0) topofscreen= 0;
  173. infotopofscreen= 0; leftofscreen= 0;
  174. redrawall();
  175. if (debug)
  176. fprintf(debug,
  177. _("baselist::startdisplay() done ...\n\n"
  178. " xmax=%d, ymax=%d;\n\n"
  179. " title_height=%d, colheads_height=%d, list_height=%d;\n"
  180. " thisstate_height=%d, info_height=%d, whatinfo_height=%d;\n\n"
  181. " colheads_row=%d, thisstate_row=%d, info_row=%d;\n"
  182. " whatinfo_row=%d, list_row=%d;\n\n"),
  183. xmax, ymax,
  184. title_height, colheads_height, list_height,
  185. thisstate_height, info_height, whatinfo_height,
  186. colheads_row, thisstate_row, info_row,
  187. whatinfo_row, list_row);
  188. }
  189. void baselist::enddisplay() {
  190. delwin(titlewin);
  191. delwin(whatinfowin);
  192. delwin(listpad);
  193. delwin(colheadspad);
  194. delwin(thisstatepad);
  195. delwin(infopad);
  196. wmove(stdscr,ymax,0); wclrtoeol(stdscr);
  197. listpad= 0;
  198. }
  199. void baselist::redrawall() {
  200. redrawtitle();
  201. redrawcolheads();
  202. wattrset(listpad,list_attr); mywerase(listpad);
  203. ldrawnstart= ldrawnend= -1; // start is first drawn; end is first undrawn; -1=none
  204. refreshlist();
  205. redrawthisstate();
  206. redrawinfo();
  207. }
  208. void baselist::redraw1item(int index) {
  209. redraw1itemsel(index, index == cursorline);
  210. }
  211. baselist::baselist(keybindings *kb) {
  212. if (debug)
  213. fprintf(debug,"baselist[%p]::baselist()\n",this);
  214. bindings= kb;
  215. nitems= 0;
  216. xmax= -1;
  217. list_height=0; info_height=0;
  218. topofscreen= 0; leftofscreen= 0;
  219. listpad= 0; cursorline= -1;
  220. showinfo= 1;
  221. searchstring[0]= 0;
  222. }
  223. void baselist::itd_keys() {
  224. whatinfovb(_("Keybindings"));
  225. const int givek= xmax/3;
  226. bindings->describestart();
  227. const char **ta;
  228. while ((ta= bindings->describenext()) != 0) {
  229. const char **tap= ta+1;
  230. for (;;) {
  231. waddstr(infopad, gettext(*tap));
  232. tap++; if (!*tap) break;
  233. waddstr(infopad, ", ");
  234. }
  235. int y,x;
  236. getyx(infopad,y,x);
  237. if (x >= givek) y++;
  238. mvwaddstr(infopad, y,givek, ta[0]);
  239. waddch(infopad,'\n');
  240. }
  241. }
  242. void baselist::dosearch() {
  243. int offset, index;
  244. if (debug) fprintf(debug,"baselist[%p]::dosearch(); searchstring=`%s'\n",
  245. this,searchstring);
  246. for (offset=1, index=greaterint(topofscreen,cursorline+1);
  247. offset<nitems;
  248. offset++, index++) {
  249. if (index >= nitems) index -= nitems;
  250. if (matchsearch(index)) {
  251. topofscreen= index-1;
  252. if (topofscreen > nitems - list_height) topofscreen= nitems-list_height;
  253. if (topofscreen < 0) topofscreen= 0;
  254. setcursor(index);
  255. return;
  256. }
  257. }
  258. beep();
  259. }
  260. void baselist::refreshinfo() {
  261. pnoutrefresh(infopad, infotopofscreen,leftofscreen, info_row,0,
  262. lesserint(info_row + info_height - 1, info_row + MAX_DISPLAY_INFO - 1),
  263. lesserint(total_width - leftofscreen - 1, xmax - 1));
  264. if (whatinfo_height) {
  265. mywerase(whatinfowin);
  266. mvwaddstr(whatinfowin,0,0, whatinfovb.string());
  267. if (infolines > info_height) {
  268. wprintw(whatinfowin,_(" -- %d%%, press "),
  269. (int)((infotopofscreen + info_height) * 100.0 / infolines));
  270. if (infotopofscreen + info_height < infolines) {
  271. wprintw(whatinfowin,_("%s for more"), bindings->find("iscrollon"));
  272. if (infotopofscreen) waddstr(whatinfowin, ", ");
  273. }
  274. if (infotopofscreen)
  275. wprintw(whatinfowin, _("%s to go back"),bindings->find("iscrollback"));
  276. waddch(whatinfowin,'.');
  277. }
  278. wnoutrefresh(whatinfowin);
  279. }
  280. }
  281. void baselist::wordwrapinfo(int offset, const char *m) {
  282. int usemax= xmax-5;
  283. if (debug) fprintf(debug,"baselist[%p]::wordwrapinfo(%d, `%s')\n",this,offset,m);
  284. int wrapping=0;
  285. for (;;) {
  286. int offleft=offset; while (*m == ' ' && offleft>0) { m++; offleft--; }
  287. const char *p= strchr(m,'\n');
  288. int l= p ? (int)(p-m) : strlen(m);
  289. while (l && isspace(m[l-1])) l--;
  290. if (!l || *m == '.' && l == 1) {
  291. if (wrapping) waddch(infopad,'\n');
  292. waddch(infopad,'\n');
  293. wrapping= 0;
  294. } else if (*m == ' ' || usemax < 10) {
  295. if (wrapping) waddch(infopad,'\n');
  296. waddnstr(infopad, m, l);
  297. waddch(infopad,'\n'); wrapping= 0;
  298. } else {
  299. int x,y;
  300. if (wrapping) {
  301. getyx(infopad, y,x);
  302. if (x+1 >= usemax) {
  303. waddch(infopad,'\n');
  304. } else {
  305. waddch(infopad,' ');
  306. }
  307. }
  308. for (;;) {
  309. getyx(infopad, y,x);
  310. int dosend= usemax-x;
  311. if (l <= dosend) {
  312. dosend=l;
  313. } else {
  314. int i=dosend;
  315. while (i > 0 && m[i] != ' ') i--;
  316. if (i > 0 || x > 0) dosend=i;
  317. }
  318. if (dosend) waddnstr(infopad, m, dosend);
  319. while (dosend < l && m[dosend] == ' ') dosend++;
  320. l-= dosend; m+= dosend;
  321. if (l <= 0) break;
  322. waddch(infopad,'\n');
  323. }
  324. wrapping= 1;
  325. }
  326. if (!p) break;
  327. m= ++p;
  328. }
  329. if (debug) fprintf(debug,"baselist[%p]::wordwrapinfo() done\n",this);
  330. }
  331. baselist::~baselist() { }
  332. /* vi: sw=2 ts=8
  333. */