home *** CD-ROM | disk | FTP | other *** search
- /*
- * riscoswimp.c: RISC OS windowed front end for uEmacs (and others)
- */
-
- #include "kernel.h"
-
- #include <stdio.h>
- #include "estruct.h"
- #include "edef.h"
-
- #if RISCOSWIMP
-
- #include "wimp.h"
- #include "wimpt.h"
- #include "win.h"
- #include "event.h"
- #include "res.h"
- #include "menu.h"
- #include "template.h"
- #include "dbox.h"
- #include "werr.h"
- #include "baricon.h"
- #include "xferrecv.h"
- #include "msgs.h"
- #include "saveas.h"
-
- #define NROW 64 /* Screen size. */
- #define NCOL 192 /* Edit if you want to. */
- #define NPAUSE 750 /* # times thru update to pause */
- #define MARGIN 8 /* size of minimim margin and */
- #define SCRSIZ 64 /* scroll size for extended lines */
- #define BEL 0x07 /* BEL character. */
- #define ESC 0x1B /* ESC character. */
-
- #define INBUFSIZE 16 /* Cyclic buffer for input */
- /* Must be a power of two */
-
- extern int risc_osgetc(void); /* forward references */
- extern int risc_osputc(int);
- extern int risc_osflush(void);
- extern int risc_osmove(int, int);
- extern int risc_oseeol(void);
- extern int risc_oseeop(void);
- extern int risc_osbeep(void);
- extern int risc_osopen(void);
- extern int risc_osrev(int);
- extern int risc_oscres();
- extern int risc_osclose(void);
- extern int risc_oskopen(void);
- extern int risc_oskclose(void);
-
- #if COLOR
- extern int risc_osfcol();
- extern int risc_osbcol();
-
- int cfcolor = -1; /* current forground color */
- int cbcolor = -1; /* current background color */
- #endif
-
- TERM term = {
- NROW-1,
- NROW-1,
- NCOL,
- NCOL,
- MARGIN,
- SCRSIZ,
- NPAUSE,
- risc_osopen,
- risc_osclose,
- risc_oskopen,
- risc_oskclose,
- risc_osgetc,
- risc_osputc,
- risc_osflush,
- risc_osmove,
- risc_oseeol,
- risc_oseeop,
- risc_osbeep,
- risc_osrev,
- risc_oscres
- #if COLOR
- , risc_osfcol,
- risc_osbcol
- #endif
- };
-
- #define vdu(x) _kernel_oswrch(x)
-
- static BOOL win_displayed;
- static int ue_xpos, ue_ypos;
- static char sb[NROW][NCOL];
- static char attribs[NROW][NCOL];
- static char attrib_byte;
- static int use_colour;
- static wimp_w uemacs_window;
- static menu uemacs_menu, uemacs_icon_menu;
- static char inbuffer[INBUFSIZE];
- static int in_next, in_last;
-
- BOOL need_to_redraw;
- static wimp_box rb, rb_blank = {NCOL, NROW, -1, -1};
-
- static void in_init(void)
- {
- in_next = in_last = INBUFSIZE;
- }
-
- static int in_any(void)
- {
- return (in_next != in_last);
- }
-
- static void in_push(char c)
- {
- in_last &= (INBUFSIZE -1);
- inbuffer[in_last++]=c;
- }
-
- static void in_push_spec(int c)
- {
- in_last &= (INBUFSIZE -1);
- inbuffer[in_last++]=0;
- in_last &= (INBUFSIZE -1);
- inbuffer[in_last++]=(c >> 8);
- in_last &= (INBUFSIZE -1);
- inbuffer[in_last++]=(c & 0xff);
- }
-
- static void in_push_nop(void)
- {
- in_push_spec(ALTD | SHFT | SPEC | CTLX | META | CTRL | 'N');
- }
-
- static void in_push_mouse(int m, int x, int y, char t)
- {
- in_last &= (INBUFSIZE -1);
- inbuffer[in_last++]=0;
- in_last &= (INBUFSIZE -1);
- inbuffer[in_last++]=(m | MOUS) >> 8;
- in_last &= (INBUFSIZE -1);
- inbuffer[in_last++]=x;
- in_last &= (INBUFSIZE -1);
- inbuffer[in_last++]=y;
- in_last &= (INBUFSIZE -1);
- inbuffer[in_last++]=t;
- }
-
- static char in_pull(void)
- {
- in_next &= (INBUFSIZE -1);
- return (inbuffer[in_next++]);
- }
-
- static void uemacs_set_pointer(char *name, int x, int y)
- {
- os_regset r;
-
- r.r[0] = 36 + 256;
- r.r[2] = (int) name;
- r.r[3] = 2;
- r.r[4] = x;
- r.r[5] = y;
- r.r[6] = 0;
- r.r[7] = 0;
- wimpt_noerr(wimp_spriteop_full(&r));
- }
-
- static void place_caret(BOOL force)
- {
- wimp_caretstr car;
-
- wimp_get_caret_pos(&car);
- if (force || car.w == uemacs_window)
- {
- car.w = uemacs_window;
- car.i = -1;
- car.x = ue_xpos << 4;
- car.y = -(ue_ypos+1)<< 5;
- car.height = 1<<24 | 32;
- car.index = -1;
-
- wimp_set_caret_pos(&car);
- }
- }
-
- static void uemacs_force_redraw(void)
- {
- wimp_redrawstr r;
-
- r.w = uemacs_window;
- r.box.x0 = rb.x0 << 4;
- r.box.x1 = (rb.x1 + 1) << 4;
- r.box.y1 = -(rb.y0 << 5);
- r.box.y0 = -((rb.y1 +1) << 5);
-
- wimp_force_redraw(&r);
-
- rb = rb_blank;
- need_to_redraw = FALSE;
- }
-
-
- static BOOL uemacs_create_window(char *name, wimp_w *handle)
- {
- wimp_wind *window;
-
- window = template_syshandle(name);
- if (window == 0)
- return FALSE;
-
- use_colour = (3 << 4) | window->colours[wimp_WCWKAREAFORE] ^
- window->colours[wimp_WCWKAREABACK] ;
-
- return (wimpt_complain(wimp_create_wind(window, handle)) == 0);
- }
-
- static void uemacs_open_window(wimp_openstr *o)
- {
- wimp_redrawstr r;
- int ox,oy;
-
- o->x = 0;
- o->y = 0;
-
- wimpt_noerr(wimp_open_wind(o));
-
- oy = term.t_mrow;
- ox = term.t_mcol;
-
- if (oy != (term.t_mrow = ((o->box.y1 - o->box.y0) >> 5)-1))
- newsize(TRUE, term.t_mrow +1);
- if (ox != (term.t_mcol = ((o->box.x1 - o->box.x0) >> 4)))
- newwidth(TRUE, term.t_mcol);
-
- if (term.t_mrow != oy || term.t_mcol != ox)
- {
- update(TRUE);
-
- r.w = -1;
- r.box = o->box;
- wimp_force_redraw(&r);
- }
- }
-
- static void raise_window(void)
- {
- wimp_wstate state;
- wimp_caretstr r;
-
- if (wimpt_complain(wimp_get_wind_state(uemacs_window, &state)) == 0)
- {
- state.o.behind = -1; /* Make sure window is opened in front */
- wimpt_complain(wimp_open_wind(&state.o));
- }
-
- r.w = uemacs_window;
- r.i = -1;
- r.x = ue_xpos << 4;
- r.y = -(ue_ypos+1)<< 5;
- r.height = 1<<24 | 32;
- r.index = -1;
-
- wimp_set_caret_pos(&r);
- }
-
- static void uemacs_redraw_window(wimp_w handle)
- {
- int more;
- wimp_redrawstr r;
- int ox,oy;
-
- r.w = handle;
- wimpt_noerr(wimp_redraw_wind(&r, &more));
-
- ox = r.box.x0 - r.scx;
- oy = r.box.y1 - r.scy;
-
- while (more)
- {
- int top, left, right, bottom;
- int i,j;
-
- top = r.g.y1 + 1 - oy;
- left = r.g.x0 - ox;
- right = r.g.x1 - ox;
- bottom = r.g.y0 + 1 - oy;
-
- top = (-top) >> 5;
- left = left >> 4;
- right = right >> 4;
- bottom = (-bottom) >> 5;
-
- wimp_setcolour(use_colour);
-
- for(j = top; j<= bottom; j++)
- {
- int revs = -1, last = 0;
-
- bbc_move(ox + (left << 4), oy-1-(j<<5) );
- for(i = left; i <= right; i++)
- {
- vdu(sb[j][i]);
- if (attribs[j][i] != last)
- {
- if (revs == -1)
- {
- revs = i;
- }
- else
- {
- bbc_rectanglefill(ox+(revs<<4), oy-((j+1) <<5),
- ((i - revs)<<4)-1, 31 );
- revs = -1;
- }
- bbc_move(ox +((i+1) << 4) , oy-((j+1) << 5) + 31 );
- last = attribs[j][i];
- }
- }
- if (last)
- {
- bbc_rectanglefill(ox+(revs<<4), oy-((j+1) <<5),
- ((i - revs)<<4)-1, 31 );
- }
- }
-
- wimp_get_rectangle(&r, &more);
- }
-
- place_caret(FALSE);
-
- }
-
- static wimp_bbits last_button;
-
- static void uemacs_drag_event(wimp_box *b)
- {
- wimp_wstate s;
- int at_x, at_y;
-
- wimpt_noerr(wimp_get_wind_state(uemacs_window, &s));
-
- at_x = (b->x0 - s.o.box.x0) >> 4;
- at_y = (s.o.box.y1 -1 - b->y0) >> 5;
-
- in_push_mouse(0, at_x, at_y, (last_button & wimp_BDRAGLEFT) ? 'b' : 'f');
- uemacs_set_pointer("ptr_write",4,4);
- }
-
- static void uemacs_mouse_event(wimp_eventdata *d)
- {
- wimp_wstate s;
- int at_x, at_y;
-
- wimpt_noerr(wimp_get_wind_state(uemacs_window, &s));
-
- at_x = (d->but.m.x - s.o.box.x0) >> 4;
- at_y = (s.o.box.y1 -1 - d->but.m.y) >> 5;
-
- place_caret(TRUE);
-
- if (d->but.m.bbits & (wimp_BDRAGRIGHT | wimp_BDRAGLEFT))
- {
- wimp_dragstr ds;
-
- ds.window = uemacs_window;
- ds.type = wimp_USER_HIDDEN;
- ds.box.x0 = ds.box.x1 = d->but.m.x;
- ds.box.y0 = ds.box.y1 = d->but.m.y;
- ds.parent = s.o.box;
-
- wimp_drag_box(&ds);
-
- if (d->but.m.bbits & wimp_BDRAGLEFT)
- uemacs_set_pointer("ptr_hand",12,8);
-
- last_button = d->but.m.bbits;
-
- in_push_mouse(0, at_x, at_y, (d->but.m.bbits & wimp_BDRAGLEFT) ? 'a' : 'e');
- }
- else
- {
- if (d->but.m.bbits == last_button >> 4)
- {
- last_button = 0;
- }
- else
- {
- in_push_mouse(0, at_x, at_y, (d->but.m.bbits & wimp_BLEFT) ? 'a' : 'e');
- in_push_mouse(0, at_x, at_y, (d->but.m.bbits & wimp_BLEFT) ? 'b' : 'f');
- }
- }
- }
-
- static void uemacs_key_pressed(int c)
- {
- static had_escape = 0;
-
- /* Keys between 100 and 180 and f12 keys get filtered out */
- if ((c >= 0x100 && c < 0x180) || (c >= 0x1c0 && ((c & 0xf) == 0xc)))
- wimp_processkey(c);
- else
- {
- if (0) /* c = ESC ????? */
- {
- if (had_escape)
- {
- in_push_spec(ALTD | ESC);
- had_escape = 0;
- }
- else
- {
- had_escape = 1;
- }
- }
- else
- {
- if (c < 0x100)
- {
- if (had_escape)
- {
- if (c >= 0x20)
- {
- in_push_spec(ALTD | (c & 0xff));
- }
- else
- {
- in_push_spec(ALTD | CTRL | '@' | c);
- }
- }
- else
- {
- if (c == 0)
- {
- in_push(0);
- in_push(0);
- in_push(0);
- }
- else
- {
- in_push(c & 0xff);
- }
- }
- }
- else
- {
- int base_key = c & 0xCF;
- int shft = (c & 0x10 ? SHFT : 0);
- int ctrl = (c & 0x20 ? CTRL : 0);
- int alt = (had_escape ? ALTD : 0);
-
- if (base_key >= 0x81 && base_key <= 0x89)
- in_push_spec(SPEC | shft | ctrl | base_key - 0x81 + '1');
- else
- {
- #define asc (shft | ctrl | alt)
- switch (base_key)
- {
- case 0x80: /* F0 (Print) */
- in_push_spec(SPEC | asc | '`');
- break;
-
- case 0x8A: /* Tab */
- if (asc == 0)
- in_push ('\t');
- else if (!ctrl)
- in_push_spec (alt | shft | CTRL | 'I');
- else
- in_push_spec (SPEC | asc | 'T');
- break;
-
- case 0x8B: /* Copy */
- in_push_spec (SPEC | asc | '>');
- break;
-
- case 0x8C: /* Cursor Left */
- in_push_spec (SPEC | asc | 'B');
- break;
-
- case 0x8D: /* Cursor Right */
- in_push_spec (SPEC | asc | 'F');
- break;
-
- case 0x8E: /* Cursor Down */
- in_push_spec (SPEC | asc | 'N');
- break;
-
- case 0x8F: /* Cursor Up */
- in_push_spec (SPEC | asc | 'P');
- break;
-
- case 0xCA: /* F10 */
- in_push_spec(SPEC | asc | '0');
- break;
-
- case 0xCB: /* F11 */
- in_push_spec(SPEC | asc | '-');
- break;
-
- case 0xCC: /* F12 */
- in_push_spec(SPEC | asc | '=');
- break;
-
- case 0xCD: /* Insert */
- in_push_spec (SPEC | asc | 'C');
- break;
- }
- }
- }
- }
- }
- }
-
- static void uemacs_scroll_event(int y)
- {
- switch (y)
- {
- case -2:
- in_push_spec(SPEC | 'W');
- break;
- case -1:
- in_push_spec(SPEC | 'X');
- break;
- case 1:
- in_push_spec(SPEC | 'Y');
- break;
- case 2:
- in_push_spec(SPEC | 'Z');
- break;
- }
- }
-
- static void uemacs_clickproc(wimp_i i)
- {
- i=i;
-
- raise_window();
- }
-
- static void uemacs_iconbar_loader(void)
- {
- char *filename;
-
- xferrecv_checkinsert(&filename);
-
- getfile(filename, TRUE);
-
- raise_window();
-
- update(TRUE);
-
- xferrecv_insertfileok();
- }
-
- static void uemacs_window_loader(void)
- {
- char *filename;
-
- xferrecv_checkinsert(&filename);
-
- ifile(filename);
-
- raise_window();
-
- update(TRUE);
-
- xferrecv_insertfileok();
- }
-
- static BOOL uemacs_saveproc(char *fname, void *handle)
- {
- int s;
-
- handle = handle;
-
- if ((s=writeout(fname, "w")) == TRUE)
- {
- strcpy(curbp->b_fname, fname);
- curbp->b_flag &= ~BFCHG;
- /* Update mode lines. */
- upmode();
- update(TRUE);
- }
-
- return s;
- }
-
- static void uemacs_event_handler(wimp_eventstr *e, void *handle)
- {
- handle = handle; /* We don't need handle: this stops compiler warning */
-
- /* Deal with event */
- switch (e->e)
- {
- case wimp_EREDRAW:
- uemacs_redraw_window(e->data.o.w);
- break;
-
- case wimp_EOPEN:
- uemacs_open_window(&e->data.o);
- break;
-
- case wimp_ECLOSE:
- wimp_close_wind(e->data.o.w);
- win_displayed = FALSE;
- break;
-
- case wimp_EPTRLEAVE:
- os_swi2(6,106,1); /* Select pointer 1 */
- break;
-
- case wimp_EPTRENTER:
- uemacs_set_pointer("ptr_write",4,4);
- break;
-
- case wimp_EBUT:
- if (e->data.but.m.w < 0)
- {
- uemacs_clickproc(e->data.but.m.i);
- }
- else
- {
- uemacs_mouse_event(&e->data);
- }
- break;
-
- case wimp_EUSERDRAG:
- uemacs_drag_event(&e->data.dragbox);
- break;
-
- case wimp_EKEY:
- uemacs_key_pressed(e->data.key.chcode);
- break;
-
- case wimp_ESCROLL:
- uemacs_scroll_event(e->data.scroll.y);
- break;
-
- case wimp_ESEND:
- case wimp_ESENDWANTACK:
- {
- switch (e->data.msg.hdr.action)
- {
- case wimp_MDATAOPEN:
- if (e->data.msg.data.dataopen.type == 0xfff)
- uemacs_iconbar_loader();
- break;
- case wimp_MDATALOAD:
- if (e->data.msg.data.dataload.w == uemacs_window)
- {
- uemacs_window_loader();
- }
- else
- {
- uemacs_iconbar_loader();
- }
- break;
- case wimp_MPREQUIT:
- break;
- default:
- break;
- }
- }
-
- default: /* Ignore any other event */
- break;
- }
- }
-
- int risc_osflush()
- {
- return 0;
- }
-
- int risc_osmove(int row, int col)
- {
- ue_xpos=col;
- ue_ypos=row;
-
- place_caret(FALSE);
-
- if (need_to_redraw)
- {
- /* If the move is to a line not next to the rb, redraw now */
- if ((row < rb.y0-1) || (row > rb.y1+1))
- uemacs_force_redraw();
- }
-
- return 0;
- }
-
- int risc_oseeol(void)
- {
- int i;
- for(i=ue_xpos; i<NCOL; i++)
- {
- sb[ue_ypos][i]=32;
- attribs[ue_ypos][i]=attrib_byte;
- }
-
- if (rb.x0 > ue_xpos)
- rb.x0 = ue_xpos;
- if (rb.y0 > ue_ypos)
- rb.y0 = ue_ypos;
- if (rb.y1 < ue_ypos)
- rb.y1 = ue_ypos;
- rb.x1 = NCOL-1;
-
- need_to_redraw=TRUE;
-
- return 0;
- }
-
- int risc_oseeop(void)
- {
- int i,j;
- risc_oseeol();
- for(j=ue_ypos+1; j<NROW; j++)
- for(i=0; i<NCOL; i++)
- {
- sb[j][i]=32;
- attribs[j][i]=attrib_byte;
- }
-
- rb.x0 = 0;
- rb.y1 = NROW-1;
-
- return 0;
- }
-
- int risc_osrev(int state)
- {
- if (state)
- attrib_byte = 1;
- else
- attrib_byte = 0;
-
- return 0;
- }
-
- int risc_oscres(char *newres)
- {
- return 0;
- }
-
- int spal(int a)
- {
- return 0;
- }
-
- int risc_osbeep(void)
- {
- vdu(BEL);
- return 0;
- }
-
- static void uemacs_program_info(void)
- {
- dbox d;
-
- if (d = dbox_new("ProgInfo"), d != NULL)
- {
- dbox_setfield(d, 4, "0.80 ("__DATE__")" );
- dbox_setfield(d, 1, PROGNAME" "VERSION );
-
- dbox_show(d);
-
- dbox_fillin(d);
-
- dbox_dispose(&d);
- }
- }
-
- static void save_buffer(void)
- {
- #if EXTINFO
- int type = (curbp->b_os.load >> 8) & 0xfff;
- #else
- #if FTYPE
- int type = curbp->b_type;
- #else
- int type = 0xfff;
- #endif /* FTYPE */
- #endif /* EXTINFO */
- saveas(type, curbp->b_fname, 1, uemacs_saveproc, 0, 0, 0);
- }
-
- static void uemacs_barmenuproc(void *handle, char *hit)
- {
- handle = handle;
-
- switch(hit[0])
- {
- case 1:
- uemacs_program_info();
- break;
- case 2:
- raise_window();
- break;
- case 3:
- quit(FALSE, 0);
- break;
- }
- in_push_nop();
- }
-
- static void uemacs_menuproc(void *handle, char *hit)
- {
- handle = handle;
-
- switch(hit[0])
- {
- case 1:
- save_buffer();
- break;
- case 2:
- raise_window();
- break;
- case 3:
- quit(FALSE, 0);
- break;
- }
- in_push_nop();
- }
-
- int risc_osopen(void)
- {
- wimp_wstate state;
-
- wimpt_init(PROGNAME);
- res_init("uEmacs");
- template_init();
- msgs_init();
-
- baricon("!uemacs", 1, uemacs_clickproc);
- win_register_event_handler(win_ICONBAR, uemacs_event_handler, 0);
- win_claim_unknown_events(win_ICONBAR);
-
- uemacs_icon_menu = menu_new("uEmacs", msgs_lookup("barmenu"));
- event_attachmenu(win_ICONBAR, uemacs_icon_menu, uemacs_barmenuproc, 0);
-
- uemacs_create_window("MainWindow", &uemacs_window);
- win_register_event_handler(uemacs_window, uemacs_event_handler, 0);
- win_claim_idle_events(uemacs_window);
- event_setmask(event_getmask() & (~wimp_EMNULL));
-
- uemacs_menu = menu_new("uEmacs", msgs_lookup("mainmenu"));
- event_attachmenu(uemacs_window, uemacs_menu, uemacs_menuproc, 0);
-
- if (wimpt_complain(wimp_get_wind_state(uemacs_window, &state)) == 0)
- {
-
- term.t_nrow = ((state.o.box.y1 - state.o.box.y0) >> 5)-1;
- term.t_ncol = ((state.o.box.x1 - state.o.box.x0) >> 4);
-
- state.o.behind = -1; /* Make sure window is opened in front */
- wimpt_complain(wimp_open_wind(&state.o));
- win_activeinc();
- win_displayed = TRUE;
- }
- attrib_byte = 0;
- risc_osmove(0,0);
- risc_oseeop();
- place_caret(TRUE);
-
- rb = rb_blank;
-
- return 0;
- }
-
- int risc_osclose(void)
- {
- os_swi2(6,106,1); /* reset pointer to number 1 */
- return 0;
- }
-
- int risc_oskopen(void)
- {
- return 0;
- }
-
- int risc_oskclose(void)
- {
- return 0;
- }
-
- int risc_osgetc()
- {
- while (!in_any())
- {
- if (need_to_redraw)
- uemacs_force_redraw();
- event_process();
- }
-
- return in_pull();
- }
-
- #if TYPEAH
- int typahead(void)
- {
- #if 0
- if (need_to_redraw || !in_any())
- {
- if (need_to_redraw)
- uemacs_force_redraw();
- event_process();
- }
- #else
- if (!in_any())
- event_process();
- #endif
- return in_any();
- }
- #endif /* TYPEAH */
-
- int risc_osputc(int c)
- {
- if (c < 0x20)
- {
- switch (c) {
- case 8:
- if (ue_xpos != 0)
- ue_xpos--;
- break;
- case 9:
- if (ue_xpos != NCOL-1)
- ue_xpos++;
- break;
- case 10:
- if (ue_ypos != NROW-1)
- ue_ypos++;
- break;
- case 11:
- if (ue_ypos != 0)
- ue_ypos--;
- break;
- case 12:
- ue_xpos = ue_ypos = 0;
- risc_oseeop();
- break;
- case 30:
- ue_xpos = ue_ypos =0;
- break;
- default:
- break;
- }
- }
- else
- {
- if (c == 0x7f)
- {
- sb[ue_ypos][--ue_xpos] = 32;
- }
-
- if (rb.x0 > ue_xpos)
- rb.x0 = ue_xpos;
- if (rb.x1 < ue_xpos)
- rb.x1 = ue_xpos;
- if (rb.y0 > ue_ypos)
- rb.y0 = ue_ypos;
- if (rb.y1 < ue_ypos)
- rb.y1 = ue_ypos;
-
-
- attribs[ue_ypos][ue_xpos]=attrib_byte;
-
-
- if (c != 127)
- {
- sb[ue_ypos][ue_xpos++] = c;
- }
-
- need_to_redraw = TRUE;
- }
-
- return 0;
- }
-
- #if FLABEL
- fnclabel(f, n) /* label a function key */
- int f,n; /* default flag, numeric argument [unused] */
- {
- /* on machines with no function keys...don't bother */
- return(TRUE);
- }
-
-
-
- #endif
-
- #else
- void risc_oshello(void)
- {
- }
-
- #endif
-