2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * gator_curseswindows.c
14 * Implementation of the gator curses window facility.
16 *------------------------------------------------------------------------*/
18 #include <afsconfig.h>
19 #include <afs/param.h>
24 #if defined(AFS_HPUX110_ENV) && !defined(__HP_CURSES)
29 #include <curses.h> /*Curses library*/
31 #include <sys/types.h>
33 #if !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV)
49 #include "gtxcurseswin.h" /*Interface definition*/
50 #include "gtxobjects.h"
55 int curses_debug; /*Is debugging turned on?*/
56 static char mn[] = "gator_curseswindows"; /*Module name*/
59 * Version of standard operations for a curses window.
61 struct gwinops curses_gwinops = {
63 gator_cursesgwin_clear,
64 gator_cursesgwin_destroy,
65 gator_cursesgwin_display,
66 gator_cursesgwin_drawline,
67 gator_cursesgwin_drawrectangle,
68 gator_cursesgwin_drawchar,
69 gator_cursesgwin_drawstring,
70 gator_cursesgwin_invert,
71 gator_cursesgwin_getchar,
72 gator_cursesgwin_getdimensions,
73 gator_cursesgwin_wait,
76 struct gwinbaseops gator_curses_gwinbops = {
77 gator_cursesgwin_create,
78 gator_cursesgwin_cleanup,
83 * Macros to map pixel positions to row & column positions.
84 * (Note: for now, they are the identity function!!)
86 #define GATOR_MAP_X_TO_COL(w, x) (x)
87 #define GATOR_MAP_Y_TO_LINE(w, y) (y)
89 /*------------------------------------------------------------------------
90 * gator_cursesgwin_init
93 * Initialize the curses window package.
96 * int adebug: Is debugging turned on?
100 * Error value otherwise.
103 * Nothing interesting.
107 *------------------------------------------------------------------------*/
109 int gator_cursesgwin_init(adebug)
112 { /*gator_cursesgwin_init*/
114 static char rn[] = "gator_cursesgwin_init"; /*Routine name*/
115 struct gator_cursesgwin *c_data; /*Ptr to curses-specific data*/
118 * Remember if we'll be doing debugging, then init the curses package.
120 curses_debug = adebug;
123 fprintf(stderr, "[%s:%s] Calling initscr()\n", mn, rn);
127 * Fill out the base window structure for curses.
130 fprintf(stderr, "[%s:%s] Allocating %d bytes for curses window private space in base window\n", mn, rn, sizeof(struct gator_cursesgwin));
131 c_data = (struct gator_cursesgwin *) malloc(sizeof(struct gator_cursesgwin));
132 if (c_data == (struct gator_cursesgwin *)0){
133 fprintf(stderr, "[%s:%s] Can't allocate %d bytes for curses window private space in base window\n", mn, rn, sizeof(struct gator_cursesgwin));
138 * Fill in the curses-specific base window info. We assume that chars are 8x13.
141 c_data->charwidth = 8;
142 c_data->charheight = 13;
143 c_data->box_vertchar = '|';
144 c_data->box_horizchar = '-';
147 * Fill in the generic base window info.
149 gator_basegwin.w_type = GATOR_WIN_CURSES;
150 gator_basegwin.w_x = 0;
151 gator_basegwin.w_y = 0;
152 gator_basegwin.w_width = c_data->charwidth * COLS;
153 gator_basegwin.w_height = c_data->charheight * LINES;
154 gator_basegwin.w_changed = 0;
155 gator_basegwin.w_op = &curses_gwinops;
156 gator_basegwin.w_parent = NULL;
159 * Plug the private data into the generic part of the base window.
161 gator_basegwin.w_data = (int *)c_data;
164 * Now, set the terminal into the right mode for handling input
166 raw(); /* curses raw mode */
169 gator_basegwin.w_frame = gtxframe_Create();
172 * Clear out the screen and return the good news.
174 wclear(((struct gator_cursesgwin *)(gator_basegwin.w_data))->wp);
177 } /*gator_cursesgwin_init*/
179 /*------------------------------------------------------------------------
180 * gator_cursesgwin_create
183 * Create a curses window (incorrectly).
186 * struct gator_cursesgwin_params *params : Ptr to creation parameters.
189 * Ptr to the created curses window if successful,
190 * Null ptr otherwise.
193 * Nothing interesting.
197 *------------------------------------------------------------------------*/
199 struct gwin *gator_cursesgwin_create(params)
200 struct gator_cursesgwin_params *params;
202 { /*gator_cursesgwin_create*/
204 static char rn[] = "gator_cursesgwin_create"; /*Routine name*/
205 struct gwin *newgwin; /*Ptr to new curses window*/
206 struct gator_cursesgwin *c_data; /*Ptr to curses-specific data*/
207 WINDOW *newcursgwin; /*Ptr to new curses window*/
210 fprintf(stderr, "[%s:%s] Allocating %d bytes for new gwin structure\n", mn, rn, sizeof(struct gwin));
211 newgwin = (struct gwin *) malloc(sizeof(struct gwin));
212 if (newgwin == NULL) {
213 fprintf(stderr, "[%s:%s] Can't malloc() %d bytes for new gwin structure: Errno is %d\n", mn, rn, sizeof(struct gwin), errno);
217 newgwin->w_type = GATOR_WIN_CURSES;
218 newgwin->w_x = params->gwin_params.cr_x;
219 newgwin->w_y = params->gwin_params.cr_y;
220 newgwin->w_width = params->gwin_params.cr_width;
221 newgwin->w_height = params->gwin_params.cr_height;
222 newgwin->w_changed = 1;
223 newgwin->w_op = &curses_gwinops;
224 newgwin->w_parent = params->gwin_params.cr_parentwin;
227 fprintf(stderr, "[%s:%s] Allocating %d bytes for curses window private space\n", mn, rn, sizeof(struct gator_cursesgwin));
228 c_data = (struct gator_cursesgwin *) malloc(sizeof(struct gator_cursesgwin));
229 if (c_data == (struct gator_cursesgwin *)0){
230 fprintf(stderr, "[%s:%s] Can't allocate %d bytes for curses window private space\n", mn, rn, sizeof(struct gator_cursesgwin));
235 newcursgwin = newwin(newgwin->w_height, /*Number of lines*/
236 newgwin->w_width, /*Number of columns*/
237 newgwin->w_y, /*Beginning y value*/
238 newgwin->w_x); /*Beginning x value*/
239 if (newcursgwin == (WINDOW *)0) {
240 fprintf(stderr, "[%s:%s] Failed to create curses window via newwin()\n", mn, rn);
247 * Now, fill in the curses-specific window info.
249 c_data->wp = newcursgwin;
250 c_data->charwidth = params->charwidth;
251 c_data->charheight = params->charheight;
252 c_data->box_vertchar = params->box_vertchar;
253 c_data->box_horizchar = params->box_horizchar;
256 * Plug in a frame at the top-level.
258 newgwin->w_frame = gtxframe_Create();
261 * Plug the curses private data into the generic window object, then
262 * return the new window's info.
264 newgwin->w_data = (int *) c_data;
267 } /*gator_cursesgwin_create*/
269 /*------------------------------------------------------------------------
270 * gator_cursesgwin_cleanup
273 * Clean up, probably right before the caller exits.
276 * struct gwin *gwp : Ptr to base window.
280 * Error value otherwise.
283 * Nothing interesting.
287 *------------------------------------------------------------------------*/
289 int gator_cursesgwin_cleanup(gwp)
292 { /*gator_cursesgwin_cleanup*/
294 static char rn[] = "gator_cursesgwin_cleanup"; /*Routine name*/
295 struct gator_cursesgwin *cwp; /*Curses private area ptr*/
297 cwp = (struct gator_cursesgwin *)(gwp->w_data);
300 * Cleaning up in curses is extremely easy - one simple call. We also
301 * want to clear the screen before we go.
304 fprintf(stderr, "[%s:%s] Calling wclear() on window at 0x%x\n", mn, rn, cwp->wp);
309 * Now, set the terminal back into normal mode.
314 fprintf(stderr, "[%s:%s] Calling endwin()\n", mn, rn);
319 } /*gator_cursesgwin_cleanup*/
321 /*------------------------------------------------------------------------
322 * gator_cursesgwin_box
325 * Draw a box around the given curses window.
328 * struct gwin *gwp : Ptr to the curses window to draw
333 * Error value otherwise.
336 * Nothing interesting.
340 *------------------------------------------------------------------------*/
342 int gator_cursesgwin_box(gwp)
345 { /*gator_cursesgwin_box*/
347 static char rn[] = "gator_cursesgwin_box"; /*Routine name*/
348 struct gator_cursesgwin *cwp; /*Ptr to curses private area*/
350 cwp = (struct gator_cursesgwin *)(gwp->w_data);
352 fprintf(stderr, "[%s:%s] Calling box() on window at 0x%x\n", mn, rn, cwp->wp);
353 box(cwp->wp, cwp->box_vertchar, cwp->box_horizchar);
357 } /*gator_cursesgwin_box*/
359 /*------------------------------------------------------------------------
360 * gator_cursesgwin_clear
363 * Clear out the given curses window.
366 * struct gwin *gwp : Ptr to the curses window to clear out.
370 * Error value otherwise.
373 * Nothing interesting.
377 *------------------------------------------------------------------------*/
379 int gator_cursesgwin_clear(gwp)
382 { /*gator_cursesgwin_clear*/
384 static char rn[] = "gator_cursesgwin_clear"; /*Routine name*/
385 struct gator_cursesgwin *cwp; /*Ptr to curses private area*/
388 * Clearing windows is very easy in curses; just one call will do it.
390 cwp = (struct gator_cursesgwin *)(gwp->w_data);
392 fprintf(stderr, "[%s:%s] Calling wclear() on window at 0x%x\n", mn, rn, cwp->wp);
397 } /*gator_cursesgwin_clear*/
399 /*------------------------------------------------------------------------
400 * gator_cursesgwin_destroy
403 * Destroy the given curses window.
406 * struct gwin *gwp : Ptr to the curses window to destroy.
410 * Error value otherwise.
413 * Nothing interesting.
417 *------------------------------------------------------------------------*/
419 int gator_cursesgwin_destroy(gwp)
422 { /*gator_cursesgwin_destroy*/
424 static char rn[] = "gator_cursesgwin_destroy"; /*Routine name*/
425 struct gator_cursesgwin *cwp; /*Ptr to curses private area*/
427 cwp = (struct gator_cursesgwin *)(gwp->w_data);
429 fprintf(stderr, "[%s:%s] Calling delwin() on window at 0x%x\n", mn, rn, cwp->wp);
434 } /*gator_cursesgwin_destroy*/
436 /*------------------------------------------------------------------------
437 * gator_cursesgwin_display
440 * Display/redraw the given curses window.
443 * struct gwin *gwp : Ptr to the curses window to draw.
447 * Error value otherwise.
450 * Nothing interesting.
454 *------------------------------------------------------------------------*/
456 int gator_cursesgwin_display(gwp)
459 { /*gator_cursesgwin_display*/
461 struct gator_cursesgwin *cwp; /*Curses private area ptr*/
463 cwp = (struct gator_cursesgwin *)(gwp->w_data);
465 wclear(cwp->wp); /* clear screen */
466 gtxframe_Display(gwp->w_frame, gwp); /* display the frame */
467 wrefresh(cwp->wp); /* redraw the guy */
470 } /*gator_cursesgwin_display*/
472 /*------------------------------------------------------------------------
473 * gator_cursesgwin_drawline
476 * Draw a line between two points in the given curses
480 * struct gwin *gwp : Ptr to the curses window in which
481 * the line is to be drawn.
482 * struct gwin_lineparams *params : Ptr to other params.
486 * Error value otherwise.
489 * Nothing interesting.
493 *------------------------------------------------------------------------*/
495 int gator_cursesgwin_drawline(gwp, params)
497 struct gwin_lineparams *params;
499 { /*gator_cursesgwin_drawline*/
501 static char rn[] = "gator_cursesgwin_drawline"; /*Routine name*/
504 fprintf(stderr, "[%s:%s] This routine is currently a no-op\n", mn, rn);
508 } /*gator_cursesgwin_drawline*/
510 /*------------------------------------------------------------------------
511 * gator_cursesgwin_drawrectangle
514 * Draw a rectangle in the given curses window.
517 * struct gwin *gwp : Ptr to the curses window in which
518 * the rectangle is to be drawn.
519 * struct gwin_rectparams *params : Ptr to other params.
523 * Error value otherwise.
526 * Nothing interesting.
530 *------------------------------------------------------------------------*/
532 int gator_cursesgwin_drawrectangle(gwp, params)
534 struct gwin_rectparams *params;
536 { /*gator_cursesgwin_drawrectangle*/
538 static char rn[] = "gator_cursesgwin_drawrectangle"; /*Routine name*/
541 fprintf(stderr, "[%s:%s] This routine is currently a no-op\n", mn, rn);
545 } /*gator_cursesgwin_drawrectangle*/
547 /*------------------------------------------------------------------------
548 * gator_cursesgwin_drawchar
551 * Draw a character in the given curses window.
554 * struct gwin *gwp : Ptr to the curses window in which
555 * the character is to be drawn.
556 * struct gwin_charparams *params : Ptr to other params.
560 * Error value otherwise.
563 * Nothing interesting.
567 *------------------------------------------------------------------------*/
569 int gator_cursesgwin_drawchar(gwp, params)
571 struct gwin_charparams *params;
573 { /*gator_cursesgwin_drawchar*/
575 static char rn[] = "gator_cursesgwin_drawchar"; /*Routine name*/
576 struct gator_cursesgwin *cwp; /*Ptr to curses private area*/
577 int curses_x, curses_y; /*Mapped x,y positions*/
579 cwp = (struct gator_cursesgwin *)(gwp->w_data);
580 curses_x = GATOR_MAP_X_TO_COL(cwp, params->x);
581 curses_y = GATOR_MAP_Y_TO_LINE(cwp, params->y);
583 fprintf(stderr, "[%s:%s] Drawing char '%c' on window at 0x%x at (%d, %d) [line %d, column %d]%s\n", mn, rn, params->c, cwp->wp, params->x, params->y, curses_y, curses_x, (params->highlight ? ", using standout mode" : ""));
584 wmove(cwp->wp, curses_y, curses_x);
585 if (params->highlight)
587 waddch(cwp->wp, params->c);
588 if (params->highlight)
593 } /*gator_cursesgwin_drawchar*/
595 /*------------------------------------------------------------------------
596 * gator_cursesgwin_drawstring
599 * Draw a string in the given curses window.
602 * struct gwin *gwp : Ptr to the curses window in which
603 * the string is to be drawn.
604 * struct gwin_strparams *params : Ptr to other params.
608 * Error value otherwise.
611 * Nothing interesting.
615 *------------------------------------------------------------------------*/
617 int gator_cursesgwin_drawstring(gwp, params)
619 struct gwin_strparams *params;
621 { /*gator_cursesgwin_drawstring*/
623 static char rn[] = "gator_cursesgwin_drawstring"; /*Routine name*/
624 struct gator_cursesgwin *cwp; /*Ptr to curses private area*/
625 int curses_x, curses_y; /*Mapped x,y positions*/
627 cwp = (struct gator_cursesgwin *)(gwp->w_data);
628 curses_x = GATOR_MAP_X_TO_COL(cwp, params->x);
629 curses_y = GATOR_MAP_Y_TO_LINE(cwp, params->y);
631 fprintf(stderr, "[%s:%s] Drawing string '%s' on window at 0x%x at (%d, %d) [line %d, column %d]%s\n", mn, rn, params->s, cwp->wp, params->x, params->y, curses_y, curses_x, (params->highlight ? ", using standout mode" : ""));
632 wmove(cwp->wp, curses_y, curses_x);
633 if (params->highlight)
635 waddstr(cwp->wp, params->s);
636 if (params->highlight)
641 } /*gator_cursesgwin_drawstring*/
643 /*------------------------------------------------------------------------
644 * gator_cursesgwin_invert
647 * Invert a region in the given curses window.
650 * struct gwin *gwp : Ptr to the curses window in which
651 * the inverted region lies.
652 * struct gwin_invparams *params : Ptr to other params.
656 * Error value otherwise.
659 * Nothing interesting.
663 *------------------------------------------------------------------------*/
665 int gator_cursesgwin_invert(gwp, params)
667 struct gwin_invparams *params;
669 { /*gator_cursesgwin_invert*/
671 static char rn[] = "gator_cursesgwin_invert"; /*Routine name*/
674 fprintf(stderr, "[%s:%s] This routine is currently a no-op\n", mn, rn);
678 } /*gator_cursesgwin_invert*/
680 /*------------------------------------------------------------------------
681 * gator_cursesgwin_getchar
684 * Pick up a character from the given window.
687 * struct gwin *gwp : Ptr to the curses window to listen to.
690 * Value of the character read,
694 * Nothing interesting.
698 *------------------------------------------------------------------------*/
700 int gator_cursesgwin_getchar(gwp)
703 { /*gator_cursesgwin_getchar*/
707 } /*gator_cursesgwin_getchar*/
709 /*------------------------------------------------------------------------
710 * gator_cursesgwin_wait
713 * Wait until input is available.
716 * struct gwin *gwp : Ptr to the curses window to wait on.
720 * Error value otherwise.
723 * Nothing interesting.
727 *------------------------------------------------------------------------*/
729 int gator_cursesgwin_wait(gwp)
732 { /*gator_cursesgwin_wait*/
734 while (!LWP_WaitForKeystroke(-1));
738 } /*gator_cursesgwin_wait*/
740 /*------------------------------------------------------------------------
741 * gator_cursesgwin_getdimensions
744 * Get the window's X,Y dimensions.
747 * struct gwin *gwp : Ptr to the curses window to examine.
748 * struct gwin_sizeparams *params : Ptr to the size params to set.
752 * Error value otherwise.
755 * Nothing interesting.
759 *------------------------------------------------------------------------*/
761 int gator_cursesgwin_getdimensions(gwp, aparms)
762 struct gwin_sizeparams *aparms;
765 { /*gator_cursesgwin_getdimensions*/
767 struct gator_cursesgwin *cwp; /*Curses-specific data*/
769 cwp = (struct gator_cursesgwin *)(gwp->w_data);
770 #if defined(AFS_DARWIN_ENV) && !defined(AFS_DARWIN60_ENV)
771 aparms->maxx = cwp->wp->maxx;
772 aparms->maxy = cwp->wp->maxy;
773 #elif defined(AFS_NBSD_ENV)
774 aparms->maxx = getmaxx(cwp->wp);
775 aparms->maxy = getmaxy(cwp->wp);
777 aparms->maxx = cwp->wp->_maxx;
778 aparms->maxy = cwp->wp->_maxy;
783 } /*gator_cursesgwin_getdimensions*/