📄 miniswt.c
字号:
/* * minimal XInput version of swt.c - stephen's window toolkit * * Copyright (C) 1999 Stephen F. White * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program (see the file "COPYING" for details); if * not, write to the Free Software Foundation, Inc., 675 Mass Ave, * Cambridge, MA 02139, USA. */#include <X11/Xlib.h>#include <X11/IntrinsicP.h>#include <stdio.h>#include "config.h"#include "swtxinput.h"XtAppContext TheAppContext = NULL;static Display *TheDisplay = NULL;voidswInitialize(int *argc, char **argv){ XtToolkitInitialize (); TheAppContext = XtCreateApplicationContext(); TheDisplay = XtOpenDisplay ( TheAppContext, NULL, NULL, "ProgramClass", NULL, 0, argc, argv); if (!TheDisplay) { int errorflag=1;#ifdef HAVE_X11STARTCOMMAND if (getenv("DISPLAY") == NULL) { fprintf(stderr,"$DISPLAY unset, using \"localhost:0\"\n"); setenv("DISPLAY","localhost:0",0); TheDisplay = XtOpenDisplay ( TheAppContext, NULL, NULL, "ProgramClass", NULL, 0, argc, argv); if (TheDisplay) errorflag = 0; else { int pid; fprintf(stderr,"Warning: start on console:\n%s\n", HAVE_X11STARTCOMMAND); pid=fork(); if (pid==-1) perror("fork: "); else if (pid==0) { setsid(); if (execlp("/bin/sh","/bin/sh", "-c",HAVE_X11STARTCOMMAND,NULL) == -1) perror(HAVE_X11STARTCOMMAND); } else { int i; /* wait for X11 startup */ int seconds = 30; for (i = 0; i < seconds; i++) { TheDisplay = XtOpenDisplay ( TheAppContext, NULL, NULL, "ProgramClass", NULL, 0, argc, argv); if (TheDisplay) { errorflag = 0; break; } sleep(1); } if (errorflag == 1) fprintf(stderr, "failed? %s\n%s after %d %s %d %s\n", HAVE_X11STARTCOMMAND, "can't open display", seconds,"trials +",seconds,"seconds"); } } }#endif if (errorflag == 1) { if (getenv("DISPLAY")==NULL) fprintf(stderr, "can't open display\n"); else fprintf(stderr, "can't open display: %s\n",getenv("DISPLAY")); exit(1); } }}#ifdef HAVE_XINPUTvoid swInitXinputDevice(struct swXinput* xinput,const char *device) { XExtensionVersion* version; XDeviceInfo *info; int number_of_devices; int i; int MotionEventType, ButtonPressEventType, ButtonReleaseEventType; int MotionEventClass, ButtonPressEventClass, ButtonReleaseEventClass; xinput->xinput=NULL; version=XGetExtensionVersion(TheDisplay,"XInputExtension"); if ((version==NULL) || (version==(void*)NoSuchExtension)) { fprintf(stderr,"No XInputExtension (%s)\n",device); return; };# ifdef DEBUG printf("X Input Extension Version %d.%d\n", version->major_version,version->minor_version);# endif XFree(version); info=XListInputDevices(TheDisplay,&number_of_devices); for (i=0;i<number_of_devices;++i) { int c; XAnyClassPtr xclass; xclass=info[i].inputclassinfo; if (strcmp(info[i].name,device)==0) { xinput->xinput=XOpenDevice(TheDisplay,info[i].id); for (c=0;c<info[i].num_classes;++c) { if (xclass->class==ButtonClass) { XButtonInfo *button_class=(XButtonInfo *)xclass; xinput->number_buttons=button_class->num_buttons; } if (xclass->class==ValuatorClass) { int i; XValuatorInfo *axes_class=(XValuatorInfo *)xclass; XAxisInfoPtr axes=axes_class->axes; xinput->number_axes=axes_class->num_axes; xinput->axes=malloc(axes_class->num_axes*sizeof(int)); xinput->minvalue=malloc(axes_class->num_axes*sizeof(int)); xinput->maxvalue=malloc(axes_class->num_axes*sizeof(int)); for (i=0;i<xinput->number_axes;i++) { xinput->maxvalue[i]=axes->max_value; xinput->minvalue[i]=axes->min_value; axes++; } } xclass=(XAnyClassPtr)((char*)xclass+xclass->length); } } } XFreeDeviceList(info); if (xinput == NULL) { fprintf(stderr,"xinputdevice %s ",device); fprintf(stderr,"initialisation failed\n"); return; } }void swQueryXinputDevice(struct swXinput* xinput) { int c; XDeviceState* state; XInputClass* xclass; state=XQueryDeviceState(TheDisplay,xinput->xinput); if (state==XI_BadDevice) xinput->xinput=NULL; xclass=state->data; for (c=0;c<state->num_classes;++c) { if (xclass->class==ButtonClass) { XButtonState *button_class=(XButtonState *)xclass; xinput->number_buttons=button_class->num_buttons; } if (xclass->class==ValuatorClass) { XValuatorState *axes_class=(XValuatorState *)xclass; int i; for (i=0;i<axes_class->num_valuators;i++) xinput->axes[i]=axes_class->valuators[i]; } xclass=(XInputClass*)((char*)xclass+xclass->length); } }void swCloseXinputDevice(struct swXinput* xinput) { XCloseDevice(TheDisplay,xinput->xinput); }#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -