Converting tab characters to spaces in the source code ...
git-svn-id: https://svn.code.sf.net/p/freeglut/code/trunk@839 7f0cb862-5218-0410-a997-914c9d46530a
This commit is contained in:
parent
e9ad401d84
commit
e84e92b56d
@ -21,153 +21,153 @@ void draw_cube(void);
|
|||||||
void disp(void);
|
void disp(void);
|
||||||
void reshape(int x, int y);
|
void reshape(int x, int y);
|
||||||
void keyb(unsigned char key, int x, int y);
|
void keyb(unsigned char key, int x, int y);
|
||||||
void sbmot(int x, int y, int z); /* spaceball translation */
|
void sbmot(int x, int y, int z); /* spaceball translation */
|
||||||
void sbrot(int x, int y, int z); /* spaceball rotation */
|
void sbrot(int x, int y, int z); /* spaceball rotation */
|
||||||
void sbbut(int bn, int state); /* spaceball button */
|
void sbbut(int bn, int state); /* spaceball button */
|
||||||
|
|
||||||
vec3_t pos = {0, 0, -6};
|
vec3_t pos = {0, 0, -6};
|
||||||
quat_t rot = {0, 0, 0, 1};
|
quat_t rot = {0, 0, 0, 1};
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
glutInit(&argc, argv);
|
glutInit(&argc, argv);
|
||||||
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
|
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
|
||||||
glutCreateWindow("spaceball demo");
|
glutCreateWindow("spaceball demo");
|
||||||
|
|
||||||
glutDisplayFunc(disp);
|
glutDisplayFunc(disp);
|
||||||
glutReshapeFunc(reshape);
|
glutReshapeFunc(reshape);
|
||||||
glutKeyboardFunc(keyb);
|
glutKeyboardFunc(keyb);
|
||||||
glutSpaceballMotionFunc(sbmot);
|
glutSpaceballMotionFunc(sbmot);
|
||||||
glutSpaceballRotateFunc(sbrot);
|
glutSpaceballRotateFunc(sbrot);
|
||||||
glutSpaceballButtonFunc(sbbut);
|
glutSpaceballButtonFunc(sbbut);
|
||||||
|
|
||||||
glEnable(GL_CULL_FACE);
|
glEnable(GL_CULL_FACE);
|
||||||
|
|
||||||
glutMainLoop();
|
glutMainLoop();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void disp(void)
|
void disp(void)
|
||||||
{
|
{
|
||||||
mat4_t xform;
|
mat4_t xform;
|
||||||
|
|
||||||
quat_to_mat(xform, rot);
|
quat_to_mat(xform, rot);
|
||||||
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
|
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glTranslatef(pos.x, pos.y, pos.z);
|
glTranslatef(pos.x, pos.y, pos.z);
|
||||||
glMultTransposeMatrixf((float*)xform);
|
glMultTransposeMatrixf((float*)xform);
|
||||||
|
|
||||||
draw_cube();
|
draw_cube();
|
||||||
|
|
||||||
glutSwapBuffers();
|
glutSwapBuffers();
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_cube(void)
|
void draw_cube(void)
|
||||||
{
|
{
|
||||||
glBegin(GL_QUADS);
|
glBegin(GL_QUADS);
|
||||||
/* face +Z */
|
/* face +Z */
|
||||||
glNormal3f(0, 0, 1);
|
glNormal3f(0, 0, 1);
|
||||||
glColor3f(1, 0, 0);
|
glColor3f(1, 0, 0);
|
||||||
glVertex3f(-1, -1, 1);
|
glVertex3f(-1, -1, 1);
|
||||||
glVertex3f(1, -1, 1);
|
glVertex3f(1, -1, 1);
|
||||||
glVertex3f(1, 1, 1);
|
glVertex3f(1, 1, 1);
|
||||||
glVertex3f(-1, 1, 1);
|
glVertex3f(-1, 1, 1);
|
||||||
/* face +X */
|
/* face +X */
|
||||||
glNormal3f(1, 0, 0);
|
glNormal3f(1, 0, 0);
|
||||||
glColor3f(0, 1, 0);
|
glColor3f(0, 1, 0);
|
||||||
glVertex3f(1, -1, 1);
|
glVertex3f(1, -1, 1);
|
||||||
glVertex3f(1, -1, -1);
|
glVertex3f(1, -1, -1);
|
||||||
glVertex3f(1, 1, -1);
|
glVertex3f(1, 1, -1);
|
||||||
glVertex3f(1, 1, 1);
|
glVertex3f(1, 1, 1);
|
||||||
/* face -Z */
|
/* face -Z */
|
||||||
glNormal3f(0, 0, -1);
|
glNormal3f(0, 0, -1);
|
||||||
glColor3f(0, 0, 1);
|
glColor3f(0, 0, 1);
|
||||||
glVertex3f(1, -1, -1);
|
glVertex3f(1, -1, -1);
|
||||||
glVertex3f(-1, -1, -1);
|
glVertex3f(-1, -1, -1);
|
||||||
glVertex3f(-1, 1, -1);
|
glVertex3f(-1, 1, -1);
|
||||||
glVertex3f(1, 1, -1);
|
glVertex3f(1, 1, -1);
|
||||||
/* face -X */
|
/* face -X */
|
||||||
glNormal3f(-1, 0, 0);
|
glNormal3f(-1, 0, 0);
|
||||||
glColor3f(1, 1, 0);
|
glColor3f(1, 1, 0);
|
||||||
glVertex3f(-1, -1, -1);
|
glVertex3f(-1, -1, -1);
|
||||||
glVertex3f(-1, -1, 1);
|
glVertex3f(-1, -1, 1);
|
||||||
glVertex3f(-1, 1, 1);
|
glVertex3f(-1, 1, 1);
|
||||||
glVertex3f(-1, 1, -1);
|
glVertex3f(-1, 1, -1);
|
||||||
/* face +Y */
|
/* face +Y */
|
||||||
glNormal3f(0, 1, 0);
|
glNormal3f(0, 1, 0);
|
||||||
glColor3f(0, 1, 1);
|
glColor3f(0, 1, 1);
|
||||||
glVertex3f(-1, 1, 1);
|
glVertex3f(-1, 1, 1);
|
||||||
glVertex3f(1, 1, 1);
|
glVertex3f(1, 1, 1);
|
||||||
glVertex3f(1, 1, -1);
|
glVertex3f(1, 1, -1);
|
||||||
glVertex3f(-1, 1, -1);
|
glVertex3f(-1, 1, -1);
|
||||||
/* face -Y */
|
/* face -Y */
|
||||||
glNormal3f(0, -1, 0);
|
glNormal3f(0, -1, 0);
|
||||||
glColor3f(1, 0, 1);
|
glColor3f(1, 0, 1);
|
||||||
glVertex3f(-1, -1, -1);
|
glVertex3f(-1, -1, -1);
|
||||||
glVertex3f(1, -1, -1);
|
glVertex3f(1, -1, -1);
|
||||||
glVertex3f(1, -1, 1);
|
glVertex3f(1, -1, 1);
|
||||||
glVertex3f(-1, -1, 1);
|
glVertex3f(-1, -1, 1);
|
||||||
glEnd();
|
glEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 45deg fov */
|
/* 45deg fov */
|
||||||
#define FOV (M_PI / 4.0)
|
#define FOV (M_PI / 4.0)
|
||||||
|
|
||||||
void reshape(int x, int y)
|
void reshape(int x, int y)
|
||||||
{
|
{
|
||||||
float aspect = (float)x / (float)y;
|
float aspect = (float)x / (float)y;
|
||||||
float halfy = tan(FOV / 2.0);
|
float halfy = tan(FOV / 2.0);
|
||||||
float halfx = halfy * aspect;
|
float halfx = halfy * aspect;
|
||||||
|
|
||||||
glViewport(0, 0, x, y);
|
glViewport(0, 0, x, y);
|
||||||
|
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glFrustum(-halfx, halfx, -halfy, halfy, 1.0, 1000.0);
|
glFrustum(-halfx, halfx, -halfy, halfy, 1.0, 1000.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void keyb(unsigned char key, int x, int y)
|
void keyb(unsigned char key, int x, int y)
|
||||||
{
|
{
|
||||||
switch(key) {
|
switch(key) {
|
||||||
case 'q':
|
case 'q':
|
||||||
case 'Q':
|
case 'Q':
|
||||||
case 27:
|
case 27:
|
||||||
exit(0);
|
exit(0);
|
||||||
|
|
||||||
case ' ':
|
case ' ':
|
||||||
/* reset initial view */
|
/* reset initial view */
|
||||||
pos = v3_cons(0, 0, -6);
|
pos = v3_cons(0, 0, -6);
|
||||||
rot = quat_cons(1, 0, 0, 0);
|
rot = quat_cons(1, 0, 0, 0);
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void sbmot(int x, int y, int z)
|
void sbmot(int x, int y, int z)
|
||||||
{
|
{
|
||||||
pos.x += x * 0.001;
|
pos.x += x * 0.001;
|
||||||
pos.y += y * 0.001;
|
pos.y += y * 0.001;
|
||||||
pos.z -= z * 0.001;
|
pos.z -= z * 0.001;
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
}
|
}
|
||||||
|
|
||||||
void sbrot(int x, int y, int z)
|
void sbrot(int x, int y, int z)
|
||||||
{
|
{
|
||||||
float axis_len = sqrt(x * x + y * y + z * z);
|
float axis_len = sqrt(x * x + y * y + z * z);
|
||||||
rot = quat_rotate(rot, axis_len * 0.001, -x / axis_len, -y / axis_len, z / axis_len);
|
rot = quat_rotate(rot, axis_len * 0.001, -x / axis_len, -y / axis_len, z / axis_len);
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
}
|
}
|
||||||
|
|
||||||
void sbbut(int bn, int state)
|
void sbbut(int bn, int state)
|
||||||
{
|
{
|
||||||
if(state == GLUT_DOWN) {
|
if(state == GLUT_DOWN) {
|
||||||
pos = v3_cons(0, 0, -6);
|
pos = v3_cons(0, 0, -6);
|
||||||
rot = quat_cons(1, 0, 0, 0);
|
rot = quat_cons(1, 0, 0, 0);
|
||||||
glutPostRedisplay();
|
glutPostRedisplay();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,14 +3,14 @@
|
|||||||
|
|
||||||
quat_t quat_rotate(quat_t q, float angle, float x, float y, float z)
|
quat_t quat_rotate(quat_t q, float angle, float x, float y, float z)
|
||||||
{
|
{
|
||||||
quat_t rq;
|
quat_t rq;
|
||||||
float half_angle = angle * 0.5;
|
float half_angle = angle * 0.5;
|
||||||
float sin_half = sin(half_angle);
|
float sin_half = sin(half_angle);
|
||||||
|
|
||||||
rq.w = cos(half_angle);
|
rq.w = cos(half_angle);
|
||||||
rq.x = x * sin_half;
|
rq.x = x * sin_half;
|
||||||
rq.y = y * sin_half;
|
rq.y = y * sin_half;
|
||||||
rq.z = z * sin_half;
|
rq.z = z * sin_half;
|
||||||
|
|
||||||
return quat_mul(q, rq);
|
return quat_mul(q, rq);
|
||||||
}
|
}
|
||||||
|
@ -21,11 +21,11 @@ quat_t quat_rotate(quat_t q, float angle, float x, float y, float z);
|
|||||||
|
|
||||||
/* matrix functions */
|
/* matrix functions */
|
||||||
static inline void m4_cons(mat4_t m,
|
static inline void m4_cons(mat4_t m,
|
||||||
float m11, float m12, float m13, float m14,
|
float m11, float m12, float m13, float m14,
|
||||||
float m21, float m22, float m23, float m24,
|
float m21, float m22, float m23, float m24,
|
||||||
float m31, float m32, float m33, float m34,
|
float m31, float m32, float m33, float m34,
|
||||||
float m41, float m42, float m43, float m44);
|
float m41, float m42, float m43, float m44);
|
||||||
|
|
||||||
#include "vmath.inl"
|
#include "vmath.inl"
|
||||||
|
|
||||||
#endif /* VMATH_H_ */
|
#endif /* VMATH_H_ */
|
||||||
|
@ -1,68 +1,68 @@
|
|||||||
/* vector functions */
|
/* vector functions */
|
||||||
static inline vec3_t v3_cons(float x, float y, float z)
|
static inline vec3_t v3_cons(float x, float y, float z)
|
||||||
{
|
{
|
||||||
vec3_t res;
|
vec3_t res;
|
||||||
res.x = x;
|
res.x = x;
|
||||||
res.y = y;
|
res.y = y;
|
||||||
res.z = z;
|
res.z = z;
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline vec3_t quat_vec(quat_t q)
|
static inline vec3_t quat_vec(quat_t q)
|
||||||
{
|
{
|
||||||
vec3_t v;
|
vec3_t v;
|
||||||
v.x = q.x;
|
v.x = q.x;
|
||||||
v.y = q.y;
|
v.y = q.y;
|
||||||
v.z = q.z;
|
v.z = q.z;
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline float v3_dot(vec3_t v1, vec3_t v2)
|
static inline float v3_dot(vec3_t v1, vec3_t v2)
|
||||||
{
|
{
|
||||||
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
return v1.x * v2.x + v1.y * v2.y + v1.z * v2.z;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* quaternion functions */
|
/* quaternion functions */
|
||||||
static inline quat_t quat_cons(float s, float x, float y, float z)
|
static inline quat_t quat_cons(float s, float x, float y, float z)
|
||||||
{
|
{
|
||||||
quat_t q;
|
quat_t q;
|
||||||
q.x = x;
|
q.x = x;
|
||||||
q.y = y;
|
q.y = y;
|
||||||
q.z = z;
|
q.z = z;
|
||||||
q.w = s;
|
q.w = s;
|
||||||
return q;
|
return q;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline quat_t quat_mul(quat_t q1, quat_t q2)
|
static inline quat_t quat_mul(quat_t q1, quat_t q2)
|
||||||
{
|
{
|
||||||
quat_t res;
|
quat_t res;
|
||||||
vec3_t v1 = quat_vec(q1);
|
vec3_t v1 = quat_vec(q1);
|
||||||
vec3_t v2 = quat_vec(q2);
|
vec3_t v2 = quat_vec(q2);
|
||||||
|
|
||||||
res.w = q1.w * q2.w - v3_dot(v1, v2);
|
res.w = q1.w * q2.w - v3_dot(v1, v2);
|
||||||
res.x = v2.x * q1.w + v1.x * q2.w + (v1.y * v2.z - v1.z * v2.y);
|
res.x = v2.x * q1.w + v1.x * q2.w + (v1.y * v2.z - v1.z * v2.y);
|
||||||
res.y = v2.y * q1.w + v1.y * q2.w + (v1.z * v2.x - v1.x * v2.z);
|
res.y = v2.y * q1.w + v1.y * q2.w + (v1.z * v2.x - v1.x * v2.z);
|
||||||
res.z = v2.z * q1.w + v1.z * q2.w + (v1.x * v2.y - v1.y * v2.x);
|
res.z = v2.z * q1.w + v1.z * q2.w + (v1.x * v2.y - v1.y * v2.x);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline void quat_to_mat(mat4_t res, quat_t q)
|
static inline void quat_to_mat(mat4_t res, quat_t q)
|
||||||
{
|
{
|
||||||
m4_cons(res, 1.0 - 2.0 * q.y*q.y - 2.0 * q.z*q.z, 2.0 * q.x * q.y + 2.0 * q.w * q.z, 2.0 * q.z * q.x - 2.0 * q.w * q.y, 0,
|
m4_cons(res, 1.0 - 2.0 * q.y*q.y - 2.0 * q.z*q.z, 2.0 * q.x * q.y + 2.0 * q.w * q.z, 2.0 * q.z * q.x - 2.0 * q.w * q.y, 0,
|
||||||
2.0 * q.x * q.y - 2.0 * q.w * q.z, 1.0 - 2.0 * q.x*q.x - 2.0 * q.z*q.z, 2.0 * q.y * q.z + 2.0 * q.w * q.x, 0,
|
2.0 * q.x * q.y - 2.0 * q.w * q.z, 1.0 - 2.0 * q.x*q.x - 2.0 * q.z*q.z, 2.0 * q.y * q.z + 2.0 * q.w * q.x, 0,
|
||||||
2.0 * q.z * q.x + 2.0 * q.w * q.y, 2.0 * q.y * q.z - 2.0 * q.w * q.x, 1.0 - 2.0 * q.x*q.x - 2.0 * q.y*q.y, 0,
|
2.0 * q.z * q.x + 2.0 * q.w * q.y, 2.0 * q.y * q.z - 2.0 * q.w * q.x, 1.0 - 2.0 * q.x*q.x - 2.0 * q.y*q.y, 0,
|
||||||
0, 0, 0, 1);
|
0, 0, 0, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* matrix functions */
|
/* matrix functions */
|
||||||
static inline void m4_cons(mat4_t m,
|
static inline void m4_cons(mat4_t m,
|
||||||
float m11, float m12, float m13, float m14,
|
float m11, float m12, float m13, float m14,
|
||||||
float m21, float m22, float m23, float m24,
|
float m21, float m22, float m23, float m24,
|
||||||
float m31, float m32, float m33, float m34,
|
float m31, float m32, float m33, float m34,
|
||||||
float m41, float m42, float m43, float m44)
|
float m41, float m42, float m43, float m44)
|
||||||
{
|
{
|
||||||
m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14;
|
m[0][0] = m11; m[0][1] = m12; m[0][2] = m13; m[0][3] = m14;
|
||||||
m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24;
|
m[1][0] = m21; m[1][1] = m22; m[1][2] = m23; m[1][3] = m24;
|
||||||
m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34;
|
m[2][0] = m31; m[2][1] = m32; m[2][2] = m33; m[2][3] = m34;
|
||||||
m[3][0] = m41; m[3][1] = m42; m[3][2] = m43; m[3][3] = m44;
|
m[3][0] = m41; m[3][1] = m42; m[3][2] = m43; m[3][3] = m44;
|
||||||
}
|
}
|
||||||
|
@ -14,9 +14,9 @@
|
|||||||
#include <X11/Xlib.h>
|
#include <X11/Xlib.h>
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
SPNAV_EVENT_ANY, /* used by spnav_remove_events() */
|
SPNAV_EVENT_ANY, /* used by spnav_remove_events() */
|
||||||
SPNAV_EVENT_MOTION,
|
SPNAV_EVENT_MOTION,
|
||||||
SPNAV_EVENT_BUTTON /* includes both press and release */
|
SPNAV_EVENT_BUTTON /* includes both press and release */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct spnav_event_motion {
|
struct spnav_event_motion {
|
||||||
@ -114,7 +114,7 @@ int fgSpaceballNumButtons(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if TARGET_HOST_POSIX_X11
|
#if TARGET_HOST_POSIX_X11
|
||||||
return 2; /* TODO implement this properly */
|
return 2; /* TODO implement this properly */
|
||||||
#else
|
#else
|
||||||
return 0;
|
return 0;
|
||||||
#endif
|
#endif
|
||||||
@ -171,7 +171,7 @@ void fgSpaceballHandleXEvent(const XEvent *xev)
|
|||||||
INVOKE_WCB(*spnav_win, SpaceMotion, (sev.motion.x, sev.motion.y, sev.motion.z));
|
INVOKE_WCB(*spnav_win, SpaceMotion, (sev.motion.x, sev.motion.y, sev.motion.z));
|
||||||
}
|
}
|
||||||
if(sev.motion.rx | sev.motion.ry | sev.motion.rz) {
|
if(sev.motion.rx | sev.motion.ry | sev.motion.rz) {
|
||||||
INVOKE_WCB(*spnav_win, SpaceRotation, (sev.motion.rx, sev.motion.ry, sev.motion.rz));
|
INVOKE_WCB(*spnav_win, SpaceRotation, (sev.motion.rx, sev.motion.ry, sev.motion.rz));
|
||||||
}
|
}
|
||||||
spnav_remove_events(SPNAV_EVENT_MOTION);
|
spnav_remove_events(SPNAV_EVENT_MOTION);
|
||||||
break;
|
break;
|
||||||
@ -228,227 +228,227 @@ static Window app_win;
|
|||||||
static Atom motion_event, button_press_event, button_release_event, command_event;
|
static Atom motion_event, button_press_event, button_release_event, command_event;
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
CMD_APP_WINDOW = 27695,
|
CMD_APP_WINDOW = 27695,
|
||||||
CMD_APP_SENS
|
CMD_APP_SENS
|
||||||
};
|
};
|
||||||
|
|
||||||
#define IS_OPEN dpy
|
#define IS_OPEN dpy
|
||||||
|
|
||||||
struct event_node {
|
struct event_node {
|
||||||
spnav_event event;
|
spnav_event event;
|
||||||
struct event_node *next;
|
struct event_node *next;
|
||||||
};
|
};
|
||||||
|
|
||||||
static int spnav_x11_open(Display *display, Window win)
|
static int spnav_x11_open(Display *display, Window win)
|
||||||
{
|
{
|
||||||
if(IS_OPEN) {
|
if(IS_OPEN) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
dpy = display;
|
dpy = display;
|
||||||
|
|
||||||
motion_event = XInternAtom(dpy, "MotionEvent", True);
|
motion_event = XInternAtom(dpy, "MotionEvent", True);
|
||||||
button_press_event = XInternAtom(dpy, "ButtonPressEvent", True);
|
button_press_event = XInternAtom(dpy, "ButtonPressEvent", True);
|
||||||
button_release_event = XInternAtom(dpy, "ButtonReleaseEvent", True);
|
button_release_event = XInternAtom(dpy, "ButtonReleaseEvent", True);
|
||||||
command_event = XInternAtom(dpy, "CommandEvent", True);
|
command_event = XInternAtom(dpy, "CommandEvent", True);
|
||||||
|
|
||||||
if(!motion_event || !button_press_event || !button_release_event || !command_event) {
|
if(!motion_event || !button_press_event || !button_release_event || !command_event) {
|
||||||
dpy = 0;
|
dpy = 0;
|
||||||
return -1; /* daemon not started */
|
return -1; /* daemon not started */
|
||||||
}
|
}
|
||||||
|
|
||||||
if(spnav_x11_window(win) == -1) {
|
if(spnav_x11_window(win) == -1) {
|
||||||
dpy = 0;
|
dpy = 0;
|
||||||
return -1; /* daemon not started */
|
return -1; /* daemon not started */
|
||||||
}
|
}
|
||||||
|
|
||||||
app_win = win;
|
app_win = win;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int spnav_close(void)
|
static int spnav_close(void)
|
||||||
{
|
{
|
||||||
if(dpy) {
|
if(dpy) {
|
||||||
spnav_x11_window(DefaultRootWindow(dpy));
|
spnav_x11_window(DefaultRootWindow(dpy));
|
||||||
app_win = 0;
|
app_win = 0;
|
||||||
dpy = 0;
|
dpy = 0;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int spnav_x11_window(Window win)
|
static int spnav_x11_window(Window win)
|
||||||
{
|
{
|
||||||
int (*prev_xerr_handler)(Display*, XErrorEvent*);
|
int (*prev_xerr_handler)(Display*, XErrorEvent*);
|
||||||
XEvent xev;
|
XEvent xev;
|
||||||
Window daemon_win;
|
Window daemon_win;
|
||||||
|
|
||||||
if(!IS_OPEN) {
|
if(!IS_OPEN) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!(daemon_win = get_daemon_window(dpy))) {
|
if(!(daemon_win = get_daemon_window(dpy))) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
prev_xerr_handler = XSetErrorHandler(catch_badwin);
|
prev_xerr_handler = XSetErrorHandler(catch_badwin);
|
||||||
|
|
||||||
xev.type = ClientMessage;
|
xev.type = ClientMessage;
|
||||||
xev.xclient.send_event = False;
|
xev.xclient.send_event = False;
|
||||||
xev.xclient.display = dpy;
|
xev.xclient.display = dpy;
|
||||||
xev.xclient.window = win;
|
xev.xclient.window = win;
|
||||||
xev.xclient.message_type = command_event;
|
xev.xclient.message_type = command_event;
|
||||||
xev.xclient.format = 16;
|
xev.xclient.format = 16;
|
||||||
xev.xclient.data.s[0] = ((unsigned int)win & 0xffff0000) >> 16;
|
xev.xclient.data.s[0] = ((unsigned int)win & 0xffff0000) >> 16;
|
||||||
xev.xclient.data.s[1] = (unsigned int)win & 0xffff;
|
xev.xclient.data.s[1] = (unsigned int)win & 0xffff;
|
||||||
xev.xclient.data.s[2] = CMD_APP_WINDOW;
|
xev.xclient.data.s[2] = CMD_APP_WINDOW;
|
||||||
|
|
||||||
XSendEvent(dpy, daemon_win, False, 0, &xev);
|
XSendEvent(dpy, daemon_win, False, 0, &xev);
|
||||||
XSync(dpy, False);
|
XSync(dpy, False);
|
||||||
|
|
||||||
XSetErrorHandler(prev_xerr_handler);
|
XSetErrorHandler(prev_xerr_handler);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int spnav_fd(void)
|
static int spnav_fd(void)
|
||||||
{
|
{
|
||||||
if(dpy) {
|
if(dpy) {
|
||||||
return ConnectionNumber(dpy);
|
return ConnectionNumber(dpy);
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*static int spnav_wait_event(spnav_event *event)
|
/*static int spnav_wait_event(spnav_event *event)
|
||||||
{
|
{
|
||||||
if(dpy) {
|
if(dpy) {
|
||||||
for(;;) {
|
for(;;) {
|
||||||
XEvent xev;
|
XEvent xev;
|
||||||
XNextEvent(dpy, &xev);
|
XNextEvent(dpy, &xev);
|
||||||
|
|
||||||
if(spnav_x11_event(&xev, event) > 0) {
|
if(spnav_x11_event(&xev, event) > 0) {
|
||||||
return event->type;
|
return event->type;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int spnav_poll_event(spnav_event *event)
|
static int spnav_poll_event(spnav_event *event)
|
||||||
{
|
{
|
||||||
if(dpy) {
|
if(dpy) {
|
||||||
if(XPending(dpy)) {
|
if(XPending(dpy)) {
|
||||||
XEvent xev;
|
XEvent xev;
|
||||||
XNextEvent(dpy, &xev);
|
XNextEvent(dpy, &xev);
|
||||||
|
|
||||||
return spnav_x11_event(&xev, event);
|
return spnav_x11_event(&xev, event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
static Bool match_events(Display *dpy, XEvent *xev, char *arg)
|
static Bool match_events(Display *dpy, XEvent *xev, char *arg)
|
||||||
{
|
{
|
||||||
int evtype = *(int*)arg;
|
int evtype = *(int*)arg;
|
||||||
|
|
||||||
if(xev->type != ClientMessage) {
|
if(xev->type != ClientMessage) {
|
||||||
return False;
|
return False;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(xev->xclient.message_type == motion_event) {
|
if(xev->xclient.message_type == motion_event) {
|
||||||
return !evtype || evtype == SPNAV_EVENT_MOTION ? True : False;
|
return !evtype || evtype == SPNAV_EVENT_MOTION ? True : False;
|
||||||
}
|
}
|
||||||
if(xev->xclient.message_type == button_press_event ||
|
if(xev->xclient.message_type == button_press_event ||
|
||||||
xev->xclient.message_type == button_release_event) {
|
xev->xclient.message_type == button_release_event) {
|
||||||
return !evtype || evtype == SPNAV_EVENT_BUTTON ? True : False;
|
return !evtype || evtype == SPNAV_EVENT_BUTTON ? True : False;
|
||||||
}
|
}
|
||||||
return False;
|
return False;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int spnav_remove_events(int type)
|
static int spnav_remove_events(int type)
|
||||||
{
|
{
|
||||||
int rm_count = 0;
|
int rm_count = 0;
|
||||||
|
|
||||||
if(dpy) {
|
if(dpy) {
|
||||||
XEvent xev;
|
XEvent xev;
|
||||||
|
|
||||||
while(XCheckIfEvent(dpy, &xev, match_events, (char*)&type)) {
|
while(XCheckIfEvent(dpy, &xev, match_events, (char*)&type)) {
|
||||||
rm_count++;
|
rm_count++;
|
||||||
}
|
}
|
||||||
return rm_count;
|
return rm_count;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int spnav_x11_event(const XEvent *xev, spnav_event *event)
|
static int spnav_x11_event(const XEvent *xev, spnav_event *event)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
int xmsg_type;
|
int xmsg_type;
|
||||||
|
|
||||||
if(xev->type != ClientMessage) {
|
if(xev->type != ClientMessage) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
xmsg_type = xev->xclient.message_type;
|
xmsg_type = xev->xclient.message_type;
|
||||||
|
|
||||||
if(xmsg_type != motion_event && xmsg_type != button_press_event &&
|
if(xmsg_type != motion_event && xmsg_type != button_press_event &&
|
||||||
xmsg_type != button_release_event) {
|
xmsg_type != button_release_event) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(xmsg_type == motion_event) {
|
if(xmsg_type == motion_event) {
|
||||||
event->type = SPNAV_EVENT_MOTION;
|
event->type = SPNAV_EVENT_MOTION;
|
||||||
event->motion.data = &event->motion.x;
|
event->motion.data = &event->motion.x;
|
||||||
|
|
||||||
for(i=0; i<6; i++) {
|
for(i=0; i<6; i++) {
|
||||||
event->motion.data[i] = xev->xclient.data.s[i + 2];
|
event->motion.data[i] = xev->xclient.data.s[i + 2];
|
||||||
}
|
}
|
||||||
event->motion.period = xev->xclient.data.s[8];
|
event->motion.period = xev->xclient.data.s[8];
|
||||||
} else {
|
} else {
|
||||||
event->type = SPNAV_EVENT_BUTTON;
|
event->type = SPNAV_EVENT_BUTTON;
|
||||||
event->button.press = xmsg_type == button_press_event ? 1 : 0;
|
event->button.press = xmsg_type == button_press_event ? 1 : 0;
|
||||||
event->button.bnum = xev->xclient.data.s[2];
|
event->button.bnum = xev->xclient.data.s[2];
|
||||||
}
|
}
|
||||||
return event->type;
|
return event->type;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static Window get_daemon_window(Display *dpy)
|
static Window get_daemon_window(Display *dpy)
|
||||||
{
|
{
|
||||||
Window win, root_win;
|
Window win, root_win;
|
||||||
XTextProperty wname;
|
XTextProperty wname;
|
||||||
Atom type;
|
Atom type;
|
||||||
int fmt;
|
int fmt;
|
||||||
unsigned long nitems, bytes_after;
|
unsigned long nitems, bytes_after;
|
||||||
unsigned char *prop;
|
unsigned char *prop;
|
||||||
|
|
||||||
root_win = DefaultRootWindow(dpy);
|
root_win = DefaultRootWindow(dpy);
|
||||||
|
|
||||||
XGetWindowProperty(dpy, root_win, command_event, 0, 1, False, AnyPropertyType, &type, &fmt, &nitems, &bytes_after, &prop);
|
XGetWindowProperty(dpy, root_win, command_event, 0, 1, False, AnyPropertyType, &type, &fmt, &nitems, &bytes_after, &prop);
|
||||||
if(!prop) {
|
if(!prop) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
win = *(Window*)prop;
|
win = *(Window*)prop;
|
||||||
XFree(prop);
|
XFree(prop);
|
||||||
|
|
||||||
if(!XGetWMName(dpy, win, &wname) || strcmp("Magellan Window", (char*)wname.value) != 0) {
|
if(!XGetWMName(dpy, win, &wname) || strcmp("Magellan Window", (char*)wname.value) != 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return win;
|
return win;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int catch_badwin(Display *dpy, XErrorEvent *err)
|
static int catch_badwin(Display *dpy, XErrorEvent *err)
|
||||||
{
|
{
|
||||||
char buf[256];
|
char buf[256];
|
||||||
|
|
||||||
if(err->error_code == BadWindow) {
|
if(err->error_code == BadWindow) {
|
||||||
/* do nothing? */
|
/* do nothing? */
|
||||||
} else {
|
} else {
|
||||||
XGetErrorText(dpy, err->error_code, buf, sizeof buf);
|
XGetErrorText(dpy, err->error_code, buf, sizeof buf);
|
||||||
fprintf(stderr, "Caught unexpected X error: %s\n", buf);
|
fprintf(stderr, "Caught unexpected X error: %s\n", buf);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif /* TARGET_HOST_POSIX_X11 */
|
#endif /* TARGET_HOST_POSIX_X11 */
|
||||||
|
Reference in New Issue
Block a user