diff options
| author | epoch <epoch@hacking.allowed.org> | 2019-02-05 22:09:13 -0600 | 
|---|---|---|
| committer | epoch <epoch@hacking.allowed.org> | 2019-02-05 22:09:13 -0600 | 
| commit | 888b870c576313c4c4c13958acb7d0e833b375c1 (patch) | |
| tree | 8034e567b917cc5d20670f76b131c6f611f14528 /src | |
| parent | 7bfb38770dbbce06cef6208705fc7d28af561413 (diff) | |
| download | hackvr-888b870c576313c4c4c13958acb7d0e833b375c1.tar.gz hackvr-888b870c576313c4c4c13958acb7d0e833b375c1.zip | |
added the separated out mouse and keyboard files
Diffstat (limited to 'src')
| -rw-r--r-- | src/input.h | 7 | ||||
| -rw-r--r-- | src/keyboard.c | 82 | ||||
| -rw-r--r-- | src/keyboard.h | 21 | ||||
| -rw-r--r-- | src/keyboard_die.c | 52 | ||||
| -rw-r--r-- | src/keyboard_die.h | 27 | ||||
| -rw-r--r-- | src/keyboard_x11.c | 89 | ||||
| -rw-r--r-- | src/keyboard_x11.h | 40 | ||||
| -rw-r--r-- | src/mouse_die.c | 38 | ||||
| -rw-r--r-- | src/mouse_x11.c | 43 | ||||
| -rw-r--r-- | src/mouse_x11.h | 11 | 
10 files changed, 410 insertions, 0 deletions
| diff --git a/src/input.h b/src/input.h new file mode 100644 index 0000000..89a6e22 --- /dev/null +++ b/src/input.h @@ -0,0 +1,7 @@ +#ifndef _HV_MOUSE_H_ +#define _HV_MOUSE_H_ + +int mouse_event_handler(); +int keyboard_event_handler(); + +#endif diff --git a/src/keyboard.c b/src/keyboard.c new file mode 100644 index 0000000..d2daf5f --- /dev/null +++ b/src/keyboard.c @@ -0,0 +1,82 @@ +#include <stdio.h> +#include "keyboard.h" //HVK values +#include "common.h" + +extern struct global global; + +#include "graphics_c3.h" +extern struct gra_global gra_global; + +char keyboard_map[1024]={0}; + +int keyboard_event_handler() { +  int k; +  char line[2560]; +  c3_group_rot_t *gr; +  int e;//it is HVK_ values. positive or negative for press and release. 0 for nothing. +  if((e=get_keyboard_event())) { +    k=(e<0?-e:e);//absolute value +    printf("key: %d\n",k); +    if(e < 0) { //key release +      keyboard_map[k]=-1; +      //update keyboard state map? +      //let's do stuff for each key released? +    } +    else { //key is now held down +      keyboard_map[k]=1; +      //update keyboard state map? +      //fire what happens when the key is pressed down +    } +  } +  if(keyboard_map[HVK_ENTER]==1) { +    keyboard_map[HVK_ENTER]=0;//don't repeat this. +    snprintf(line,sizeof(line)-1,"%s action %s\n",global.user,global.selected_object); +    selfcommand(line); +  } +  if(keyboard_map[HVK_JUMP]==1) {//if we hold down jump we don't jump higher. or more. or again. just once. +    keyboard_map[HVK_JUMP]=0; +    fprintf(stderr,"# JUMP!\n"); +    //jump uses velocity. +    gr=get_group_relative(global.camera.id);//should we make the currently selected object jump? does the camera id follow the controlled object? +    if(gr) gr->v.y=-5; +    else fprintf(stderr,"# camera doesn't have a group relative so it can't jump!\n"); +  } + +//// keys that keep doing stuff if they're held down. +  if(keyboard_map[HVK_FORWARD]==1) {//do velocity? maybe... +    snprintf(line,sizeof(line)-1,"%s move forward\n",global.user); +    selfcommand(line);//moving forward at a speed based on the framerate... :/ +  } +  if(keyboard_map[HVK_BACKWARD]==1) { +    snprintf(line,sizeof(line)-1,"%s move backward\n",global.user); +    selfcommand(line); +  } +  if(keyboard_map[HVK_LEFT]==1) { +    snprintf(line,sizeof(line)-1,"%s move left\n",global.user); +    selfcommand(line); +  } +  if(keyboard_map[HVK_RIGHT]==1) { +    snprintf(line,sizeof(line)-1,"%s move right\n",global.user); +    selfcommand(line); +  } +  if(keyboard_map[HVK_UP]==1) { +    snprintf(line,sizeof(line)-1,"%s move up\n",global.user); +    selfcommand(line); +  } +  if(keyboard_map[HVK_DOWN]==1) { +    snprintf(line,sizeof(line)-1,"%s move down\n",global.user); +    selfcommand(line); +  } +  //don't forget to add the rotation keys. + +  // ^ here +  if(keyboard_map[HVK_DEBUG]==1) { +    keyboard_map[HVK_DEBUG]=0;//make sure we don't hold-down toggle it. just on press. +    global.debug ^= 1; +  } +  if(keyboard_map[HVK_ESCAPE]==1) { +    keyboard_map[HVK_ESCAPE]=0; +    return -1; +  } + return 0;//I dunno. +} diff --git a/src/keyboard.h b/src/keyboard.h new file mode 100644 index 0000000..1e0c483 --- /dev/null +++ b/src/keyboard.h @@ -0,0 +1,21 @@ +#ifndef _HACKVR_KEYBOARD_H_ +#define _HACKVR_KEYBOARD_H_ + +typedef enum { + HVK_NONE, + HVK_ESCAPE, + HVK_FORWARD, + HVK_BACKWARD, + HVK_UP, + HVK_DOWN, + HVK_LEFT, + HVK_RIGHT, + HVK_JUMP, + HVK_DEBUG, + HVK_ENTER, + HVK_MAGIC +} hvk_t; + +hvk_t get_keyboard_event(); + +#endif diff --git a/src/keyboard_die.c b/src/keyboard_die.c new file mode 100644 index 0000000..8b54188 --- /dev/null +++ b/src/keyboard_die.c @@ -0,0 +1,52 @@ +#include <stdio.h> +#include <string.h> +#include <linux/input.h> +#include <fcntl.h> +#include <unistd.h> + +#include "keyboard.h" + +#define KBDEV "/dev/input/event0" + +int kbfd = -1; + +hvk_t die_keypress_handler(unsigned short code) { +  switch(code) { +    case KEY_W: return HVK_FORWARD; +    case KEY_S: return HVK_BACKWARD; +    case KEY_A: return HVK_LEFT; +    case KEY_D: return HVK_RIGHT; +    case KEY_SPACE: return HVK_JUMP; +    case KEY_ESC: return HVK_ESCAPE; +  } +  return 0;//I dunno. +} + +hvk_t get_keyboard_event() { +  struct input_event ie; +  int l; +  memset(&ie,0,sizeof(ie)); +  if(kbfd == -1) { +    kbfd=open(KBDEV,O_RDWR);  +    fcntl(kbfd,F_SETFL,O_NONBLOCK); +  } +  if(kbfd == -1) { +    fprintf(stderr,"# keyboard shit fucked up.\n"); +    return 1; +  } +  if((l=read(kbfd,&ie,sizeof(ie))) > 0) { +    if(ie.type == 1) { +     fprintf(stderr,"# value: %d code: %d type: %d\n",ie.value,ie.code,ie.type); +     if(ie.value == 0) { //release +      return -die_keypress_handler(ie.code); +     } +     if(ie.value == 1) { //press +      return die_keypress_handler(ie.code); +     } +     if(ie.value == 2) { //repeated from being held down +      //we should return die_keypress_handler(ie.code) if we want to allow outside repeats to be treated like a rapid-fire button. +     } +    } +  } +  return 0; +} diff --git a/src/keyboard_die.h b/src/keyboard_die.h new file mode 100644 index 0000000..27ab456 --- /dev/null +++ b/src/keyboard_die.h @@ -0,0 +1,27 @@ + +//exit hackvr quickly by pressing this key +#define HV_KEY_EXIT  + +//increase (INC) or decrease (DEC) X, Y, or Z of currently selected object +#define HV_KEY_X_INC +#define HV_KEY_X_DEC +#define HV_KEY_Y_INC +#define HV_KEY_Y_DEC +#define HV_KEY_Z_INC +#define HV_KEY_Z_DEC + +//pass the move commands for forward, backward, left, right, up or down +#define HV_KEY_FORWARD +#define HV_KEY_BACKWARD +#define HV_KEY_LEFT +#define HV_KEY_RIGHT +#define HV_KEY_UP +#define HV_KEY_DOWN + +//inc or dec x, y, or z rotation of currently selected object. +#define HV_KEY_XR_INC +#define HV_KEY_XR_DEC +#define HV_KEY_YR_INC +#define HV_KEY_YR_DEC +#define HV_KEY_ZR_INC +#define HV_KEY_ZR_DEC diff --git a/src/keyboard_x11.c b/src/keyboard_x11.c new file mode 100644 index 0000000..0a083b9 --- /dev/null +++ b/src/keyboard_x11.c @@ -0,0 +1,89 @@ +//so... this gets called... where? input.c???? +#include <stdio.h> +#include "common.h" +#include <X11/keysym.h> +#include <X11/Xutil.h>//XLookupString() +#include "input.h" //we need to call the function that does stuff with the keys? +#include "keyboard.h" +#include "keyboard_x11.h" //for event mask and key definitions + +extern struct global global; + +#include "graphics_c3.h" +extern struct gra_global gra_global; + +#include "graphics_x11.h" +extern struct x11_global x11_global; + +//I need some xkey -> HVK conversion +hvk_t x11_keypress_handler(XKeyEvent *); +hvk_t x11_passthru(XKeyEvent *); + +hvk_t get_keyboard_event() {//this returns a HVK_ key + for keydown and - for keyup? +  XEvent e; +  if(XCheckMaskEvent(x11_global.dpy,HV_X11_KB_EVENT_MASK,&e)) { +    switch(e.type) { +      case KeyPress: +        if(gra_global.input_mode == 0) return x11_keypress_handler(&e.xkey); +        else return x11_passthru(&e.xkey); +      case KeyRelease: +        if(gra_global.input_mode == 0) return -x11_keypress_handler(&e.xkey); +      default: +        return 0;//I have not idea how this happened. +    } +  } +  return 0; +} + +hvk_t x11_passthru(XKeyEvent *xkey) { +  int i,len,sym=XLookupKeysym(xkey,0); +  char line[8]; +  char line2[16]; +  switch(sym) { +    case XK_Return: +      strcpy(line,"\n"); +      len=1; +      break; +    case XK_Left://hack. probably just replace this with printf()s +      strcpy(line,"\x1b[D"); +      len=3; +      break; +    case XK_Right: +      strcpy(line,"\x1b[C"); +      len=3; +      break; +    case XK_Down: +      strcpy(line,"\x1b[B"); +      len=3; +      break; +    case XK_Up: +      strcpy(line,"\x1b[A"); +      len=3; +      break; +    default: +      len=XLookupString(xkey,line,1023,NULL,NULL); +      break; +  } +  for(i=0;i/2 < len;i++) { +    line2[i]="0123456789abcdef"[(line[i/2]>>(4*(1-(i%2)))) % 16]; +  } +  line2[i]=0; +  printf("%s data %s\n",global.user,line2); +  return 0; +} + +hvk_t x11_keypress_handler(XKeyEvent *xkey) {//this only needs to return HVK_ keys based on the XKeyEvent's value... this could be a sparse array. +  int sym=XLookupKeysym(xkey,0); +  switch(sym) { +    case XK_r: return HVK_UP; +    case XK_w: return HVK_FORWARD; +    case XK_space: return HVK_JUMP; +    case XK_s: return HVK_BACKWARD; +    case XK_a: return HVK_LEFT; +    case XK_d: return HVK_RIGHT; +    case XK_Escape: return HVK_ESCAPE; +    case XK_f: return HVK_MAGIC; +    default: return 0;//HVK_NONE;//0 +  } +  return 0; +} diff --git a/src/keyboard_x11.h b/src/keyboard_x11.h new file mode 100644 index 0000000..c3b72c4 --- /dev/null +++ b/src/keyboard_x11.h @@ -0,0 +1,40 @@ +#ifndef _HV_KEYBOARD_X11_H_ +#define _HV_KEYBOARD_X11_H_ + +#include <X11/Xlib.h> +#include <X11/keysym.h> + +#define HV_X11_KB_EVENT_MASK KeyPressMask|KeyReleaseMask + +#if 0 //why do I still have this shit enabled!?!? +//exit hackvr quickly by pressing this key +#define HVK_EXIT  + +//increase (INC) or decrease (DEC) X, Y, or Z of currently selected object +#define HVK_X_INC	XK_Left		//not normally used. +#define HVK_X_DEC	XK_Right	//you'd think these would be strafe +#define HVK_Y_INC	XK_Up		//maybe ignore all of these +#define HVK_Y_DEC	XK_Down		//I used to have this instead of UP and DOWN. +#define HVK_Z_INC			//but they're not. +#define HVK_Z_DEC			//these X and Z don't care about the direction the object is facing + +//pass the move commands for forward, backward, left, right, up or down +#define HV_KEY_FORWARD	XK_w		//+1 in the direction the object is facing +#define HV_KEY_BACKWARD	XK_s +#define HV_KEY_LEFT	XK_a +#define HV_KEY_RIGHT	XK_d +#define HV_KEY_UP	XK_r		//up and down may either work only on Y axis +#define HV_KEY_DOWN	XK_f		//or may be relative to you're X rotation (and Z if you need that) +#define HVK_JUMP	XK_space +#define HVK_ACTION	XK_Return	//not really used actually... + +//inc or dec x, y, or z rotation of currently selected object. +#define HV_KEY_XR_INC // +#define HV_KEY_XR_DEC // +#define HV_KEY_YR_INC // +#define HV_KEY_YR_DEC // +#define HV_KEY_ZR_INC //do a barrel roll? +#define HV_KEY_ZR_DEC //maybe not. +#endif + +#endif diff --git a/src/mouse_die.c b/src/mouse_die.c new file mode 100644 index 0000000..6729a94 --- /dev/null +++ b/src/mouse_die.c @@ -0,0 +1,38 @@ +#include <stdio.h> +#include <string.h> +#include <linux/input.h> +#include <fcntl.h> +#include <unistd.h> + + +//#define MOUSEDEV "/dev/input/mouse0" //just one of the many possibly connected mice. (just in case you want to use one mouse for one thing and another mouse for something else) +#define MOUSEDEV "/dev/input/mice" //all the mice connected act as one. + +int mfd = -1; + +//#define "mouse.h" //I guess + +struct wtf { +  unsigned char type; +  char dx; +  char dy; +}; + +int mouse_event_handler() { +  struct wtf ie; +  int l; +  memset(&ie,0,sizeof(ie)); +  if(mfd == -1) { +    mfd=open(MOUSEDEV,O_RDWR); +    fcntl(mfd,F_SETFL,O_NONBLOCK); +  } +  if(mfd == -1) { +    fprintf(stderr,"# mouse shit fucked up.\n"); +    return 1; +  } +  if((l=read(mfd,&ie,sizeof(ie))) > 0) { +    //type == 8 and a or of some bits to say which direction. +    fprintf(stderr,"# mouse debug: type:\t%d\tdx:%d\tdy:%d\n",ie.type,ie.dx,ie.dy); +  } +  return 0; +} diff --git a/src/mouse_x11.c b/src/mouse_x11.c new file mode 100644 index 0000000..6cb1490 --- /dev/null +++ b/src/mouse_x11.c @@ -0,0 +1,43 @@ +#include "mouse_x11.h" +#include "common.h" +#include "graphics_c3.h" +#include "graphics_c2.h" +#include "graphics_x11.h" + +extern struct global global; +extern struct gra_global gra_global; +extern struct x11_global x11_global; + +int mouse_event_handler() {//this returns HVM_ key + for buttondown and - for buttonup... set the mousex and mousey in here? +  XEvent e; +  Window root,child;//just dimmies +  unsigned int mask;//just dummies +  char motion_notify=0; +  cs_t mouse; +  cs_t rmouse; +  while(XCheckMaskEvent(x11_global.dpy,HV_MOUSE_X11_EVENT_MASK,&e)) {//we want to collapse mouse stuff to one for each loop. +    switch(e.type) { +      case ButtonPress: //e.xbutton.button == 1 for first button. we don't need to start at 1. let's start at 0 with the -1 +        gra_global.mousemap[e.xbutton.button-1]=1; +        break; +      case ButtonRelease: +        gra_global.mousemap[e.xbutton.button-1]=-1;//we can trigger on -1 or on 1 then set back to 0 to prevent double-trigger +        break; +      case MotionNotify: +        motion_notify=1; +        break; +    } +  } +  if(motion_notify) { +    //mouse.x and mouse.y are shorts. this function expects ints. why are these shorts? +    XQueryPointer(x11_global.dpy,x11_global.w,&root,&child,&rmouse.x,&rmouse.y,&mouse.x,&mouse.y,&mask); +    gra_global.mouse=cs_to_c2(mouse); +    //return 1; +    //we're not going to set camera based on mousex and y here. +    //but just so I can get it out of the graphics code... +    //global.camera.r.x.d=((gra_global.height/2) - gra_global.mouse.y); +    //global.camera.r.y.d=(gra_global.mouse.x - (gra_global.width/2)); +    return 1; +  } +  return 0; +} diff --git a/src/mouse_x11.h b/src/mouse_x11.h new file mode 100644 index 0000000..9e27707 --- /dev/null +++ b/src/mouse_x11.h @@ -0,0 +1,11 @@ +#ifndef _HV_MOUSE_X11_H_ +#define _HV_MOUSE_X11_H_ + +#include <X11/Xlib.h> +//#include <X11/keysym.h>//doesn't include mask shit +//#include <X11/Xutil.h> +//#include <X11/keysym.h> + +#define HV_MOUSE_X11_EVENT_MASK ButtonPressMask|ButtonReleaseMask|PointerMotionMask + +#endif | 
