File: x11mouse.c

package info (click to toggle)
netmaze 0.81%2Bjpg0.82-14.1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 1,208 kB
  • ctags: 915
  • sloc: ansic: 8,277; tcl: 1,223; makefile: 66; sh: 26
file content (148 lines) | stat: -rw-r--r-- 3,878 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <stdlib.h>
#include <stdio.h>

#define JOY_LEFT 1
#define JOY_RIGHT 2
#define JOY_UP 4
#define JOY_DOWN 8
#define JOY_BUTTON 128

#define LOW_THRESHOLD 93   /* perhaps it's better to test x relativ to y */
#define HIGH_THRESHOLD 107

void main(void)
{
  Display *xd;
  Window r,own;
  int end=0,catch=0;
  int s,x,y,joy=0;
  GC gc;
  int redraw=0;

  xd = XOpenDisplay("");
  r = DefaultRootWindow(xd);
  s = DefaultScreen(xd);

  printf("This is a program to test a playable mousecontrol.\n");
  printf("  To start the test, press Button1 in the Window\n");
  printf("  To end the program, press Button3 in the Window\n");
  printf("While the test is running, Button1 = Fire, Button = StopWalking.\n");

  own = XCreateSimpleWindow(xd,r,100,100,200,200,5,0,BlackPixel(xd,s));
  gc = XCreateGC (xd,r,0,0);

  XSelectInput (xd,own,PointerMotionMask|ButtonPressMask|ButtonReleaseMask);

  XMapRaised (xd,own);
  XFlush(xd);

  for(;end==0;)
  {
    XEvent event1;
    XNextEvent(xd,&event1);
    switch(event1.type)
    {
      case ButtonPress:
        if(event1.xbutton.button == Button1)
        {
          if(!catch)
          {
            catch = 1;
            XGrabPointer(xd,own,False,
                 PointerMotionMask|ButtonPressMask|ButtonReleaseMask,
                 GrabModeAsync,GrabModeAsync,own,None,event1.xbutton.time);
          }
          else
          {
            redraw = 1;
            joy |= JOY_BUTTON;
          }
        }
        else if(event1.xbutton.button == Button3)
        {
          XUngrabPointer(xd,event1.xbutton.time);
          end = 1;
        }
        else if(event1.xbutton.button == Button2)
          joy &= 0xf0;
        break;
      case ButtonRelease:
        if(event1.xbutton.button == Button1)
        {
          joy &= ~JOY_BUTTON;
          redraw = 1;
        }
        break;
      case MotionNotify:
        if(catch == 1)
        {
          if((event1.xmotion.x != 100) || (event1.xmotion.y != 100))
          {
            if(event1.xmotion.x < LOW_THRESHOLD)
            {
              joy &= 0xf0;
              joy |= JOY_LEFT;
              if(event1.xmotion.y < LOW_THRESHOLD+2)
                joy |= JOY_UP;
              else if(event1.xmotion.y > HIGH_THRESHOLD-2)
                joy |= JOY_DOWN;
            }
            else if(event1.xmotion.x > HIGH_THRESHOLD)
            {
              joy &= 0xf0;
              joy |= JOY_RIGHT;
              if(event1.xmotion.y < LOW_THRESHOLD+2)
                joy |= JOY_UP;
              else if(event1.xmotion.y > HIGH_THRESHOLD-2)
                joy |= JOY_DOWN;
            }
            else if(event1.xmotion.y < LOW_THRESHOLD)
            {
              joy &= 0xf0;
              joy |= JOY_UP;
            }
            else if(event1.xmotion.y > HIGH_THRESHOLD)
            {
              joy &= 0xf0;
              joy |= JOY_DOWN;
            }

            XClearWindow(xd,own);
            if(joy & JOY_LEFT) x = 0;
            else if(joy & JOY_RIGHT) x = 200;
            else x = 100;
            if(joy & JOY_UP) y = 0;
            else if(joy & JOY_DOWN) y = 200;
            else y = 100;
            XDrawLine(xd,own,gc,100,100,x,y);
            if(joy & JOY_BUTTON)
              XDrawArc(xd,own,gc,93,93,14,14,0,64*360);
            redraw = 0;

            XWarpPointer(xd,None,own,0,0,0,0,100,100);
          }
        }
        break;
    }
    if(redraw)
    {
      XClearWindow(xd,own);
      if(joy & JOY_LEFT) x = 0;
      else if(joy & JOY_RIGHT) x = 200;
      else x = 100;
      if(joy & JOY_UP) y = 0;
      else if(joy & JOY_DOWN) y = 200;
      else y = 100;
      XDrawLine(xd,own,gc,100,100,x,y);
      if(joy & JOY_BUTTON)
        XDrawArc(xd,own,gc,93,93,14,14,0,64*360);
      redraw = 0;
    }
  }

  XDestroyWindow(xd,own);
  XCloseDisplay(xd);
}