Example #1
0
int main(int argc, char* argv[])
{
	char keyname[128];
	int pointer_button, pointer_x, pointer_y;
	char windowname[64];
	struct lirc_config* config;
	char* config_file = NULL;
	int c;
	unsigned int WindowID;

	while ((c = getopt_long(argc, argv, "dhV", long_options, NULL)) != EOF) {
		switch (c) {
		case 'd':
			bDaemon = 1;
			continue;
		case 'h':
			printf("Usage: %s [option]... [config file]\n"
			       "       -d --daemon     fork and run in background\n"
			       "       -h --help       display usage summary\n"
			       "       -V --version    display version\n", prog);
			return EXIT_SUCCESS;
		case 'V':
			printf("%s %s\n", prog, VERSION);
			return EXIT_SUCCESS;
		case '?':
			fprintf(stderr, "unrecognized option: -%c\n", optopt);
			fprintf(stderr, "Try `%s --help' for more information.\n", prog);
			return EXIT_FAILURE;
		}
	}

	if (argc == optind + 1) {
		config_file = argv[optind];
	} else if (argc > optind + 1) {
		fprintf(stderr, "%s: incorrect number of arguments.\n", prog);
		fprintf(stderr, "Try `%s --help' for more information.\n", prog);
		return EXIT_FAILURE;
	}

	dpy = XOpenDisplay(NULL);
	if (dpy == NULL) {
		fprintf(stderr, "Can't open DISPLAY.\n");
		exit(1);
	}
	root = RootWindow(dpy, DefaultScreen(dpy));

	// windows may get closed at wrong time. Override default error handler...
	XSetErrorHandler(errorHandler);

	if (lirc_init("irxevent", 1) == -1)
		exit(EXIT_FAILURE);

	if (lirc_readconfig(config_file, &config, check) == 0) {
		char* ir;
		char* c;
		int ret;

		if (bDaemon) {
			if (daemon(1, 0) < 0) {
				perror("Failed to run as daemon");
				exit(EXIT_FAILURE);
			}
		}

		while (lirc_nextcode(&ir) == 0) {
			if (ir == NULL)
				continue;
			while ((ret = lirc_code2char(config, ir, &c)) == 0 && c != NULL) {
				log_debug("Received code: %s Sending:\n", ir);
				bInError = 0;   // reset error state, want to see error msg

				*windowname = 0;
				if (2 == sscanf(c, "Key %s Focus WindowID %i", keyname, &WindowID) ||
				    4 == sscanf(c, "Button %d %d %d Focus WindowID %i", &pointer_button, &pointer_x,
						&pointer_y, &WindowID)
				    || 4 == sscanf(c, "xy_Key %d %d %s Focus WindowID %i", &pointer_x, &pointer_y,
						   keyname, &WindowID)
				    || 2 == sscanf(c, "Key %s Focus %s", keyname, windowname)
				    || 4 == sscanf(c, "Button %d %d %d Focus %s", &pointer_button, &pointer_x,
						   &pointer_y, windowname)
				    || 4 == sscanf(c, "xy_Key %d %d %s Focus %s", &pointer_x, &pointer_y, keyname,
						   windowname)) {
					log_debug("Focus\n");
					/* focussed ? */
					if (*windowname) {
						WindowID = find_window_focused(root, windowname);
						if (!WindowID) {
							log_debug("target window '%s' doesn't have focus\n",
								  windowname);
							continue;
						}
						log_debug("focused:  %s\n", windowname);
					} else {
						Window cur;
						int tmp;

						XGetInputFocus(dpy, &cur, &tmp);
						if (WindowID != cur) {
							log_debug("target window '0x%x' doesn't have focus\n",
								  WindowID);
							continue;
						}
						log_debug("focused:  0x%x\n", WindowID);
					}
				} else if (2 == sscanf(c, "Key %s WindowID %i", keyname, &WindowID) ||
					   4 == sscanf(c, "Button %d %d %d WindowID %i", &pointer_button, &pointer_x,
						       &pointer_y, &WindowID)
					   || 4 == sscanf(c, "xy_Key %d %d %s WindowID %i", &pointer_x, &pointer_y,
							  keyname, &WindowID)) {
					log_debug("WindowID:  0x%x\n", WindowID);
					/* WindowID passed */
				} else if (2 == sscanf(c, "Key %s %s", keyname, windowname) ||
					   4 == sscanf(c, "Button %d %d %d %s", &pointer_button, &pointer_x, &pointer_y,
						       windowname)
					   || 4 == sscanf(c, "xy_Key %d %d %s %s\n", &pointer_x, &pointer_y, keyname,
							  windowname)) {
					log_debug("name: %s\n", windowname);
					WindowID = find_window(root, windowname);
					if (WindowID == 0) {
						log_debug("target window '%s' not found\n", windowname);
						continue;
					}
				}

				switch (c[0]) {
				case 'K':       // Key
					log_debug("keyname: %s \t WindowID: 0x%x\n", keyname, WindowID);
					log_debug("%s\n", c);
					sendkey(keyname, 1, 1, (Window)WindowID, 0);
					break;

				case 'B':       // Button
				case 'x':       // xy_Key
					subw = find_sub_window(root, windowname, &pointer_x, &pointer_y);
					if (subw) {
						if (WindowID == subw)
							subw = 0;
						log_debug("%s\n", c);
						switch (c[0]) {
						case 'B':
							/* FIXME: pointer_button potentially uninititalzed. */
							sendbutton(pointer_button, pointer_x, pointer_y, WindowID,
								   subw);
							break;
						case 'x':
							sendkey(keyname, pointer_x, pointer_y, WindowID, subw);
							break;
						}
					}
					break;
				}
			}
			free(ir);
			if (ret == -1)
				break;
		}
		lirc_freeconfig(config);
	}

	lirc_deinit();

	exit(0);
}
Example #2
0
int main(int argc, char *argv[])
{
  char keyname[128];
  int pointer_button,pointer_x,pointer_y;
  char windowname[64];
  struct lirc_config *config;

  progname=argv[0];
  if(argc>2)  {
    fprintf(stderr,"Usage: %s <config file>\n",argv[0]);
    exit(1);
  };

  dpy=XOpenDisplay(NULL);
  if(dpy==NULL) {
    fprintf(stderr,"Can't open DISPLAY.\n");
    exit(1);
  }
  root=RootWindow(dpy,DefaultScreen(dpy));

  if(lirc_init("irxevent",1)==-1) exit(EXIT_FAILURE);

  if(lirc_readconfig(argc==2 ? argv[1]:NULL,&config,check)==0)
    {
      char *ir;
      char *c;
      int ret;
      
      while(lirc_nextcode(&ir)==0)
	{
	  if(ir==NULL) continue;
	  while((ret=lirc_code2char(config,ir,&c))==0 && c!=NULL)
	    {
	      debugprintf("Recieved code: %sSending event: ",ir);
	      if(2==sscanf(c,"Key %s %s\n",keyname,windowname))
		{
		  if((w=find_window(root,windowname)))
		    {
		      debugprintf("keyname: %s \t windowname: %s\n",keyname,windowname);
		      sendkey(keyname,1,1,w,0);
		    }
		  else
		    {
		      debugprintf("target window '%s' not found \n",windowname);
		    }
		}
	      else if(4==sscanf(c,"Button %d %d %d %s\n",
				&pointer_button,&pointer_x,
				&pointer_y,windowname))
		{
		  
		  if((w=find_window(root,windowname)) &&
		     (subw=find_sub_window(root,windowname,&pointer_x,&pointer_y)))
		    {
		      if (w==subw) subw=0;
		      debugprintf(" %s\n",c);
		      sendbutton(pointer_button,pointer_x,pointer_y,w,subw);
		    }
		  else
		    {
		      debugprintf("target window '%s' not found \n",windowname);
		    }
		}
	      else if(4==sscanf(c,"xy_Key %d %d %s %s\n",
				&pointer_x,&pointer_y,
				keyname,windowname))
		{
		  
		  if((w=find_window(root,windowname))&& (subw=find_sub_window(root,windowname,&pointer_x,&pointer_y)))
		    {
		      debugprintf(" %s\n",c);
		      if (w==subw) subw=0;
		      sendkey(keyname,pointer_x,pointer_y,w,subw);
		    }
		  else
		    {
		      debugprintf("target window '%s' not found \n",windowname);
		    }
		}
	    }
	  free(ir);
	  if(ret==-1) break;
	}
      lirc_freeconfig(config);
    }
  
  lirc_deinit();
  
  exit(0);
}