int
tp_init_buttons(struct tp_dispatch *tp,
                struct evdev_device *device)
{
    struct libinput *libinput = tp_libinput_context(tp);
    struct tp_touch *t;
    const struct input_absinfo *absinfo_x, *absinfo_y;

    tp->buttons.is_clickpad = libevdev_has_property(device->evdev,
                              INPUT_PROP_BUTTONPAD);
    tp->buttons.has_topbuttons = libevdev_has_property(device->evdev,
                                 INPUT_PROP_TOPBUTTONPAD);

    if (libevdev_has_event_code(device->evdev, EV_KEY, BTN_MIDDLE) ||
            libevdev_has_event_code(device->evdev, EV_KEY, BTN_RIGHT)) {
        if (tp->buttons.is_clickpad)
            log_bug_kernel(libinput,
                           "%s: clickpad advertising right button\n",
                           device->devname);
    } else if (libevdev_has_event_code(device->evdev, EV_KEY, BTN_LEFT) &&
               !tp->buttons.is_clickpad &&
               libevdev_get_id_vendor(device->evdev) != VENDOR_ID_APPLE) {
        log_bug_kernel(libinput,
                       "%s: non clickpad without right button?\n",
                       device->devname);
    }

    absinfo_x = device->abs.absinfo_x;
    absinfo_y = device->abs.absinfo_y;

    /* pinned-finger motion threshold, see tp_unpin_finger. */
    tp->buttons.motion_dist.x_scale_coeff = 1.0/absinfo_x->resolution;
    tp->buttons.motion_dist.y_scale_coeff = 1.0/absinfo_y->resolution;

    tp->buttons.config_method.get_methods = tp_button_config_click_get_methods;
    tp->buttons.config_method.set_method = tp_button_config_click_set_method;
    tp->buttons.config_method.get_method = tp_button_config_click_get_method;
    tp->buttons.config_method.get_default_method = tp_button_config_click_get_default_method;
    tp->device->base.config.click_method = &tp->buttons.config_method;

    tp->buttons.click_method = tp_click_get_default_method(tp);
    tp_switch_click_method(tp);

    tp_init_top_softbuttons(tp, device, 1.0);

    tp_init_middlebutton_emulation(tp, device);

    tp_for_each_touch(tp, t) {
        t->button.state = BUTTON_STATE_NONE;
        libinput_timer_init(&t->button.timer,
                            tp_libinput_context(tp),
                            tp_button_handle_timeout, t);
    }
int
tp_process_button(struct tp_dispatch *tp,
		  const struct input_event *e,
		  uint64_t time)
{
	struct libinput *libinput = tp_libinput_context(tp);
	uint32_t mask = 1 << (e->code - BTN_LEFT);

	/* Ignore other buttons on clickpads */
	if (tp->buttons.is_clickpad && e->code != BTN_LEFT) {
		log_bug_kernel(libinput,
			       "received %s button event on a clickpad\n",
			       libevdev_event_code_get_name(EV_KEY, e->code));
		return 0;
	}

	if (e->value) {
		tp->buttons.state |= mask;
		tp->queued |= TOUCHPAD_EVENT_BUTTON_PRESS;
	} else {
		tp->buttons.state &= ~mask;
		tp->queued |= TOUCHPAD_EVENT_BUTTON_RELEASE;
	}

	return 0;
}