diff options
author | ennewalker <ennewalker@c06c8d41-db1a-0410-9941-cceddc491573> | 2008-01-05 01:33:53 +0000 |
---|---|---|
committer | ennewalker <ennewalker@c06c8d41-db1a-0410-9941-cceddc491573> | 2008-01-05 01:33:53 +0000 |
commit | 62f7040f14b39e67042be98f951575fbc819e84e (patch) | |
tree | d4fa0598a1bee1d34fff81e2c150de08c2256753 /crawl-ref/source/libx11.cc | |
parent | 19155f1f85058ef9d65d11e60c63cc69c36d4e8a (diff) | |
download | crawl-ref-62f7040f14b39e67042be98f951575fbc819e84e.tar.gz crawl-ref-62f7040f14b39e67042be98f951575fbc819e84e.zip |
Tiles!
git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@3194 c06c8d41-db1a-0410-9941-cceddc491573
Diffstat (limited to 'crawl-ref/source/libx11.cc')
-rw-r--r-- | crawl-ref/source/libx11.cc | 478 |
1 files changed, 478 insertions, 0 deletions
diff --git a/crawl-ref/source/libx11.cc b/crawl-ref/source/libx11.cc new file mode 100644 index 0000000000..cf291db862 --- /dev/null +++ b/crawl-ref/source/libx11.cc @@ -0,0 +1,478 @@ +#include "AppHdr.h" +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#ifdef USE_X11 //Sys dep +#include <X11/Xlib.h> +#include <X11/X.h> +#include <X11/Xutil.h> +#include <X11/Xlocale.h> +#include <X11/keysym.h> +#include <X11/keysymdef.h> +#include <X11/Xmd.h> +#endif + +#include "cio.h" +#include "enum.h" +#include "externs.h" +#include "guic.h" +#include "libutil.h" + +/* + * Tile related stuff + */ +#ifdef USE_TILE + +#include "tiles.h" + +static Display *display=NULL; +static int screen; + +static int x11_keypress(XKeyEvent *xev); +static void x11_check_exposure(XEvent *xev); + +extern WinClass *win_main; + +void GetNextEvent(int *etype, int *key, bool *shift, bool *ctrl, + int *x1, int *y1, int *x2, int *y2) +{ + XEvent xev; + + while(1) + { + XNextEvent(display, &xev); + + if(xev.type==KeyPress) + { + *etype = EV_KEYIN; + *key = x11_keypress(&(xev.xkey)); + break; + } + else if(xev.type==Expose) + { + x11_check_exposure(&xev); + } + else if(xev.type == ConfigureNotify) + { + win_main->ox = xev.xconfigure.x; + win_main->oy = xev.xconfigure.y; + break; + } + else if(xev.type==ButtonPress) + { + *etype = EV_BUTTON; + int button = xev.xbutton.button; + *shift = (xev.xkey.state & ShiftMask) ? true : false; + *ctrl = (xev.xkey.state & ControlMask) ? true : false; + *x1 = xev.xbutton.x; + *y1 = xev.xbutton.y; + + if (button == 3) button = 2; + else if (button==2) button=3; + *key = button; + break; + } + else if(xev.type==MotionNotify || xev.type==EnterNotify) + { + *etype = EV_MOVE; + *x1 = xev.xbutton.x; + *y1 = xev.xbutton.y; + break; + } + else if (xev.type==LeaveNotify) + { + *etype = EV_MOVE; + *x1 = -100; + *y1 = -100; + break; + } + else if(xev.type==ButtonRelease) + { + *etype = EV_UNBUTTON; + int button = xev.xbutton.button; + if (button == 3) button = 2; + else if (button==2) button=3; + + *x1 = xev.xbutton.x; + *y1 = xev.xbutton.y; + *key = button; + break; + } + }/*while*/ +} + +char *my_getenv(const char *envname, const char *def) +{ + const char *result = getenv(envname); + if (!result) result = def; + return (char *)result; +} + +int my_getenv_int(const char *envname, int def) +{ + const char *rstr = getenv(envname); + if (!rstr) return def; + return atoi(rstr); +} + +#if 0 +void libgui_load_prefs(struct pref_data *pref, int nprefs) +{ + int i; + strcpy(font_name, my_getenv("CRAWL_X11_FONT", + "-alias-fixed-bold-r-normal--16-*")); + strcpy(font_name, my_getenv("CRAWL_X11_FONT", "9x15")); + *crt_x = my_getenv_int("CRAWL_X11_CRTX",80); + *crt_y = my_getenv_int("CRAWL_X11_CRTY",25); + *msg_x = my_getenv_int("CRAWL_X11_MSGX",80); + *msg_y = my_getenv_int("CRAWL_X11_MSGY",8); + *map_x = my_getenv_int("CRAWL_X11_MAPX",3); + *map_y = my_getenv_int("CRAWL_X11_MAPY",3); + *dngn_x = my_getenv_int("CRAWL_X11_DNGNX",17); + *dngn_y = my_getenv_int("CRAWL_X11_DNGNY",17); +} + +void libgui_save_prefs() +{ +} +#endif + +extern WinClass *win_main; +extern TextRegionClass *region_tip; +void update_tip_text(const char *tip) +{ + static char newtip[512]; + static char oldtip[512]; + char tbuf[35]; + + strncpy(newtip, tip, 500); + char* pc = strchr(newtip, '\n'); + if (pc) + *pc = 0; + + if (strncmp(oldtip, newtip, 500)==0) + return; + strncpy(oldtip, newtip, 500); + + strncpy(tbuf,tip,34); + tbuf[34]=0; + + if (win_main->active_layer == 0) + { + // avoid overruns... + if ((int)strlen(newtip) > region_tip->mx) + { + // try to remove inscriptions + char *ins = strchr(newtip, '{'); + if (ins && ins[-1] == ' ') ins--; + if (ins && (ins - newtip <= region_tip->mx)) + { + *ins = 0; + } + else + { + // try to remove state, e.g. "(worn)" + char *state = strchr(newtip, '('); + if (state && state[-1] == ' ') state--; + if (state && (state - newtip <= region_tip->mx)) + { + *state = 0; + } + else + { + // if nothing else... + newtip[region_tip->mx] = 0; + newtip[region_tip->mx-1] = '.'; + newtip[region_tip->mx-2] = '.'; + } + } + } + + region_tip->clear(); + region_tip->gotoxy(1,1); + region_tip->addstr(newtip); + region_tip->make_active(); + } + else + { + textattr( WHITE ); + gotoxy(1, get_number_of_lines() + 1); + cprintf("%s", oldtip); + clear_to_end_of_line(); + } +} + +void TileDrawDungeonAux() +{ +} + +#endif /* USE_TILE */ + +/* X11 */ +void x11_check_exposure(XEvent *xev){ + int sx, sy, ex, ey; + + sx = xev->xexpose.x; + ex = xev->xexpose.x + (xev->xexpose.width)-1; + sy = xev->xexpose.y; + ey = xev->xexpose.y + (xev->xexpose.height)-1; + + if (xev->xany.window != win_main->win) return; + + win_main->redraw(sx,sy,ex,ey); +} + +/* X11 */ +// This routine is taken from Angband main-x11.c +int x11_keypress(XKeyEvent *xev) +{ + +#define IsSpecialKey(keysym) \ + ((unsigned)(keysym) >= 0xFF00) + + const unsigned int ck_table[9]= + { + CK_END, CK_DOWN, CK_PGDN, + CK_LEFT, CK_INSERT, CK_RIGHT, + CK_HOME, CK_UP, CK_PGUP + }; + + int dir, base; + + int n; + bool mc, ms, ma; + unsigned int ks1; + + XKeyEvent *ev = (XKeyEvent*)(xev); + KeySym ks; + char buf[256]; + + n = XLookupString(ev, buf, 125, &ks, NULL); + buf[n] = '\0'; + + if (IsModifierKey(ks)) return 0; + + /* Extract "modifier flags" */ + mc = (ev->state & ControlMask) ? true : false; + ms = (ev->state & ShiftMask) ? true : false; + ma = (ev->state & Mod1Mask) ? true : false; + + /* Normal keys */ + if (n && !IsSpecialKey(ks)) + { + buf[n] = 0; + + //Hack Ctrl+[0-9] etc. + if (mc && ((ks>='0' && ks<='9') || buf[0]>=' ')) + { + return 1024|ks; + } + + if (!ma) + { + return buf[0]; + } + + /* Alt + ? */ + return 2048|buf[0]; + } + + /* Hack -- convert into an unsigned int */ + ks1 = (uint)(ks); + + /* Handle a few standard keys (bypass modifiers) XXX XXX XXX */ + base = dir = 0; + switch (ks1) + { + case XK_Escape: + base = 0x1b; + break; + case XK_Return: + base = '\r'; + break; + case XK_Tab: + base = '\t'; + break; + case XK_Delete: + case XK_BackSpace: + base = '\010'; + break; + + // for menus + case XK_Down: + return CK_DOWN; + case XK_Up: + return CK_UP; + case XK_Left: + return CK_LEFT; + case XK_Right: + return CK_RIGHT; + + /* + * Keypad + */ + + case XK_KP_1: + case XK_KP_End: + dir = 1; + break; + + case XK_KP_2: + case XK_KP_Down: + dir = 2; + break; + + case XK_KP_3: + case XK_KP_Page_Down: + dir = 3; + break; + + case XK_KP_6: + case XK_KP_Right: + dir = 6; + break; + + case XK_KP_9: + case XK_KP_Page_Up: + dir = 9; + break; + + case XK_KP_8: + case XK_KP_Up: + dir = 8; + break; + + case XK_KP_7: + case XK_KP_Home: + dir = 7; + break; + + case XK_KP_4: + case XK_KP_Left: + dir = 4; + break; + + case XK_KP_5: + dir = 5; + break; + }/* switch */ + + //Handle keypad first + if (dir != 0) + { + int result = ck_table[dir-1]; + + if (ms) result += CK_SHIFT_UP - CK_UP; + if (mc) result += CK_CTRL_UP - CK_UP; + return result; + } + + if (base != 0) + { + if (ms) base |= 1024; + if (mc) base |= 2048; + if (ma) base |= 4096; + return base; + } + //Hack Special key + if (ks1 >=0xff00) + { + base = 512 + ks1 - 0xff00; + if (ms) base |= 1024; + if (mc) base |= 2048; + if (ma) base |= 4096; + return base; + } + + return 0; +} + +void libgui_init_sys() +{ + GuicInit(&display, &screen); +} + +void libgui_shutdown_sys() +{ + GuicDeinit(); +} + +void update_screen() +{ + XFlush(display); +} + +int kbhit() +{ + XEvent xev; + + if (XCheckMaskEvent(display, + KeyPressMask | ButtonPressMask, &xev)) + { + XPutBackEvent(display, &xev); + return 1; + } + return 0; +} + +void delay( unsigned long time ) +{ + usleep( time * 1000 ); +} + +/* Convert value to string */ +int itoa(int value, char *strptr, int radix) +{ + unsigned int bitmask = 32768; + int ctr = 0; + int startflag = 0; + + if (radix == 10) + { + sprintf(strptr, "%i", value); + } + if (radix == 2) /* int to "binary string" */ + { + while (bitmask) + { + if (value & bitmask) + { + startflag = 1; + sprintf(strptr + ctr, "1"); + } + else + { + if (startflag) + sprintf(strptr + ctr, "0"); + } + + bitmask = bitmask >> 1; + if (startflag) + ctr++; + } + + if (!startflag) /* Special case if value == 0 */ + sprintf((strptr + ctr++), "0"); + + strptr[ctr] = (char) NULL; + } + return (0); /* Me? Fail? Nah. */ +} + + +// Convert string to lowercase. +char *strlwr(char *str) +{ + unsigned int i; + + for (i = 0; i < strlen(str); i++) + str[i] = tolower(str[i]); + + return (str); +} + +int stricmp( const char *str1, const char *str2 ) +{ + return (strcmp(str1, str2)); +} |