#include "MainWindow.h"
#include "config.h"
#include "Common.h"
#include <locale.h>
#include <libintl.h>
#include "DmGreeterHelper.h"
#include <gtkmm/main.h>
#include <gdk/gdkx.h>
#include <X11/Xatom.h>
#include <X11/extensions/XInput2.h>
#include <X11/Xcursor/Xcursor.h>
#include <pthread.h>

static void initCursor();
static void initTouchPad();
static void touchPadEnableTap(GdkDisplay* display, GdkDevice* dev);

int main(int argc, char* argv[]) {

    bindtextdomain (GLOBAL_GET_TEXT, GLOBAL_LOCALE_DIR);
    bind_textdomain_codeset (GLOBAL_GET_TEXT, "UTF-8");
    textdomain (GLOBAL_GET_TEXT);
    setlocale(LC_ALL, "");
    Gtk::Main appmain(argc, argv);

    if (!DmGreeterHelper::inst()->connectSyncDm()) {
        return EXIT_FAILURE;
    }

    MainWindow mw;
    mw.initialize();
    mw.show_all();

    initCursor();
    initTouchPad();
    Common::setNumlock(true);

    Gtk::Main::run();
    mw.finalize();
    return 0;
}

static void initTouchPad() {
    GSettings* settings = g_settings_new("org.gnome.desktop.peripherals.touchpad");
    gboolean enabled = g_settings_get_boolean (settings, "tap-to-click");
    if (TRUE == enabled) {
        GdkDisplay* display = gdk_display_get_default();
        GList* seatList = gdk_display_list_seats(display);
        GList* seatIt = g_list_first(seatList);
        for (seatIt = g_list_first(seatList); seatIt != NULL; seatIt = g_list_next(seatIt)) {
            GList* devlist = gdk_seat_get_slaves(GDK_SEAT(seatIt->data), GDK_SEAT_CAPABILITY_ALL);
            GList* it = g_list_first(devlist);
            for (it = g_list_first(devlist); it != NULL; it = g_list_next(it)) {
                touchPadEnableTap(display, GDK_DEVICE(it->data));
            }
            g_list_free(devlist);
        }
    }
    g_object_unref(settings);
}

static void touchPadEnableTap(GdkDisplay* display, GdkDevice* dev) {
    if (! (GDK_SOURCE_TOUCHPAD == gdk_device_get_source(GDK_DEVICE(dev))
            || GDK_SOURCE_MOUSE == gdk_device_get_source(GDK_DEVICE(dev)))) {
        return;
    }
    Glib::ustring devName = Glib::ustring(gdk_device_get_name(dev)).lowercase();
    if (-1 == devName.find("touchpad")) {
        return;
    }

    int devid = gdk_x11_device_get_id(GDK_DEVICE(dev));
    Atom enable_atom = XInternAtom (GDK_DISPLAY_XDISPLAY(display), "Device Enabled", True);
    Atom property_atom = XInternAtom (GDK_DISPLAY_XDISPLAY(display), "libinput Tapping Enabled", True);
    guchar value = 1;
    XIChangeProperty (GDK_DISPLAY_XDISPLAY(display), devid, enable_atom, XA_INTEGER,
        8, XIPropModeReplace, &value, 1);
    XIChangeProperty (GDK_DISPLAY_XDISPLAY(display), devid, property_atom, XA_INTEGER,
        8, XIPropModeReplace, &value, 1);
}

static void initCursor() {
    GdkDisplay* display = gdk_display_get_default();
    GdkCursor* cursor = gdk_cursor_new_for_display(display, GDK_LEFT_PTR);
    XDefineCursor(GDK_DISPLAY_XDISPLAY(display), RootWindow (GDK_DISPLAY_XDISPLAY(display), 0), GDK_CURSOR_XCURSOR(cursor));
}
