Beispiel #1
0
void key_pressed_cb(XPointer arg, XRecordInterceptData *d) {
  if (stop_thread) {
    if (!XRecordDisableContext(dpy, rc)) {
      fprintf(stderr, "XRecordDisableContext error\n");
      exit(1);
    }
    if (!XRecordFreeContext(dpy, rc)) {
      fprintf(stderr, "XRecordFreeContext error\n");
      exit(1);
    }
    pthread_exit(0);
  }

  if (d->category != XRecordFromServer) return;
  unsigned char type = ((unsigned char*)d->data)[0] & 0x7F;
  unsigned char detail = ((unsigned char*)d->data)[1];
  if (type == KeyPress) {
    switch (detail) {
      case 36:
        uri = 1;
        break;
      case 65:
        uri = 2;
        break;
      case 22:
        uri = 3;
        break;
      default:
        uri = 0;
    }
    g_signal_emit_by_name(arg, "activate");
  }
}
Beispiel #2
0
 void stop(){
   printf("DEBUG: Stop Recording ...\n");
   if(!XRecordDisableContext(disp, xrctx)){ // FAIL
     printf("DEBUG: Stop Recording FAIL\n");
   }
   XRecordFreeContext(disp, xrctx);
   XCloseDisplay(disp);
   // XCloseDisplay(udisp);  // will cause fatal
 }
Beispiel #3
0
static gpointer xevent_thread_fuc(gpointer ctx)
{
    // 会一直阻塞在这里
    XRecordEnableContext(s_display, s_context, xevent_event_callback, 0);

    XRecordFreeContext(s_display, s_context);
    XCloseDisplay(s_display);

    return NULL;
}
void finalize_xrecord ()
{
    if ( !XRecordFreeContext (self_xcape->ctrl_conn, 
                self_xcape->record_ctx) ) {
        g_debug ("Failed to free xrecord connect!\n");
    }

    XCloseDisplay (self_xcape->ctrl_conn);
    XCloseDisplay (self_xcape->data_conn);
    if (key_table) {
        g_hash_table_destroy (key_table);
    }
    g_debug ("init xcape exiting!\n");

    return ;
}
Beispiel #5
0
int
main()
{
  XRecordContext xrd;
  XRecordRange *range;
  XRecordClientSpec client;

  osd  = configure_osd(NKEYS);
  keystack = create_keystack(NKEYS);
  
  d0 = XOpenDisplay(NULL);
  d1 = XOpenDisplay(NULL);

  XSynchronize(d0, True);
  if (d0 == NULL || d1 == NULL) {
    fprintf(stderr, "Cannot connect to X server");
    exit (-1);
  }

  client=XRecordAllClients;

  range=XRecordAllocRange();
  memset(range, 0, sizeof(XRecordRange));
  range->device_events.first=KeyPress;
  range->device_events.last=KeyRelease;

  xrd = XRecordCreateContext(d0, 0, &client, 1, &range, 1);

  if (! xrd) {
    fprintf(stderr, "Error in creating context");
    exit (-1);
  }

  XRecordEnableContext(d1, xrd, update_key_ring, (XPointer)osd);

  XRecordProcessReplies (d1);


  XRecordDisableContext (d0, xrd);
  XRecordFreeContext (d0, xrd);


  XCloseDisplay(d0);
  XCloseDisplay(d1);
  exit(0);
}
Beispiel #6
0
void deinitKeyHook(void)
{
    if (ctrldpy)
    {
        if (xrc)
        {
            XRecordDisableContext(ctrldpy, xrc);
            XRecordFreeContext(ctrldpy, xrc);
        } // if
        XCloseDisplay(ctrldpy);
    } // if

    if (datadpy)
        XCloseDisplay(datadpy);

    ctrldpy = NULL;
    datadpy = NULL;
    xrc = 0;
    sawKeyCombo = 0;
    keyPressFlags = 0;
} // deinitKeyHook
Beispiel #7
0
void
grab_xrecord_finalize ()
{
    if (key_table) {
        g_hash_table_remove_all (key_table);
        key_table = NULL;
    }

    if (!grab_info) {
        return;
    }

    if (grab_info->context) {
        XRecordDisableContext(grab_info->data_disp, grab_info->context);
        XRecordFreeContext(grab_info->data_disp, grab_info->context);
    }

    if (grab_info->range) {
        XFree(grab_info->range);
        grab_info->range = NULL;
    }

    if (grab_info->ctrl_disp) {
        XCloseDisplay (grab_info->ctrl_disp);
        grab_info->ctrl_disp = NULL;
    }

    if (grab_info->data_disp) {
        XCloseDisplay (grab_info->data_disp);
        grab_info->data_disp = NULL;
    }

    if (grab_info) {
        g_free (grab_info);
        grab_info = NULL;
    }
}
Beispiel #8
0
int main (int argc, char **argv)
{
  ctrl_disp = XOpenDisplay (NULL);
  data_disp = XOpenDisplay (NULL);

  if (!ctrl_disp || !data_disp) {
    fprintf (stderr, "Error to open local display!\n");
    exit (1);
  }

  /* 
   * we must set the ctrl_disp to sync mode, or, when we the enalbe 
   * context in data_disp, there will be a fatal X error !!!
   */
  XSynchronize(ctrl_disp,True);

  int major, minor;
  if (!XRecordQueryVersion (ctrl_disp, &major, &minor)) {
    fprintf (stderr, "RECORD extension not supported on this X server!\n");
    exit (2);
  }
 
  printf ("RECORD extension for local server is version is %d.%d\n", major, minor);

  XRecordRange  *rr;
  XRecordClientSpec  rcs;
  XRecordContext   rc;

  rr = XRecordAllocRange ();
  if (!rr) {
    fprintf (stderr, "Could not alloc record range object!\n");
    exit (3);
  }

  rr->device_events.first = KeyPress;
  rr->device_events.last = MotionNotify;
  rcs = XRecordAllClients;

  rc = XRecordCreateContext (ctrl_disp, 0, &rcs, 1, &rr, 1);
  if (!rc) {
    fprintf (stderr, "Could not create a record context!\n");
    exit (4);
  }
 
  if (!XRecordEnableContext (data_disp, rc, event_callback, NULL)) {
    fprintf (stderr, "Cound not enable the record context!\n");
    exit (5);
  }

  while (stop != 1) {
    XRecordProcessReplies (data_disp);
  }

  XRecordDisableContext (ctrl_disp, rc);
  XRecordFreeContext (ctrl_disp, rc);
  XFree (rr);
 
  XCloseDisplay (data_disp);
  XCloseDisplay (ctrl_disp);
  return 0;
}
Beispiel #9
0
/************************************************************************
 * Main function
 ***********************************************************************/
int main(int argc, char **argv) {
	XCape_t *self = malloc(sizeof(XCape_t));
	int dummy, ch;
	static char default_mapping[] = "Num_Lock;Caps_Lock";
	char *mapping = default_mapping;
	self->debug = False;

	while ((ch = getopt(argc, argv, "d")) != -1) {
		switch (ch) {
		case 'd':
			self->debug = True;
			break;
		default:
			fprintf(stdout, "Usage: %s [-d]\n", argv[0]);
			fprintf(stdout, "Runs as a daemon unless -d flag is set\n");
			return EXIT_SUCCESS;
		}
	}

	self->data_conn = XOpenDisplay(NULL);
	self->ctrl_conn = XOpenDisplay(NULL);

	if (!self->data_conn || !self->ctrl_conn) {
		fprintf(stderr, "Unable to connect to X11 display. Is $DISPLAY set?\n");
		exit(EXIT_FAILURE);
	}
	if (!XQueryExtension(self->ctrl_conn, "XTEST", &dummy, &dummy, &dummy)) {
		fprintf(stderr, "Xtst extension missing\n");
		exit(EXIT_FAILURE);
	}
	if (!XRecordQueryVersion(self->ctrl_conn, &dummy, &dummy)) {
		fprintf(stderr, "Failed to obtain xrecord version\n");
		exit(EXIT_FAILURE);
	}
	if (!XkbQueryExtension(self->ctrl_conn, &dummy, &dummy, &dummy, &dummy,
			&dummy)) {
		fprintf(stderr, "Failed to obtain xkb version\n");
		exit(EXIT_FAILURE);
	}

	self->map = parse_mapping(self->ctrl_conn, mapping);

	if (self->map == NULL)
		exit(EXIT_FAILURE);

	if (self->debug != True)
		daemon(0, 0);

	sigemptyset(&self->sigset);
	sigaddset(&self->sigset, SIGINT);
	sigaddset(&self->sigset, SIGTERM);
	pthread_sigmask(SIG_BLOCK, &self->sigset, NULL);

	pthread_create(&self->sigwait_thread,
	NULL, sig_handler, self);

	XRecordRange *rec_range = XRecordAllocRange();
	rec_range->device_events.first = KeyPress;
	rec_range->device_events.last = ButtonRelease;
	XRecordClientSpec client_spec = XRecordAllClients;

	self->record_ctx = XRecordCreateContext(self->ctrl_conn, 0, &client_spec, 1,
			&rec_range, 1);

	if (self->record_ctx == 0) {
		fprintf(stderr, "Failed to create xrecord context\n");
		exit(EXIT_FAILURE);
	}

	XSync(self->ctrl_conn, False);

	if (!XRecordEnableContext(self->data_conn, self->record_ctx, intercept,
			(XPointer) self)) {
		fprintf(stderr, "Failed to enable xrecord context\n");
		exit(EXIT_FAILURE);
	}

	if (!XRecordFreeContext(self->ctrl_conn, self->record_ctx)) {
		fprintf(stderr, "Failed to free xrecord context\n");
	}

	XCloseDisplay(self->ctrl_conn);
	XCloseDisplay(self->data_conn);

	if (self->debug)
		fprintf(stdout, "main exiting\n");

	return EXIT_SUCCESS;
}