Beispiel #1
0
/** XRandR event handler for RRNotify subtype XRROutputChangeNotifyEvent
 */
static void
event_handle_randr_output_change_notify(xcb_randr_notify_event_t *ev)
{
    if(ev->subCode == XCB_RANDR_NOTIFY_OUTPUT_CHANGE) {
        xcb_randr_output_t output = ev->u.oc.output;
        uint8_t connection = ev->u.oc.connection;
        char *output_name = NULL;
        const char *connection_str = NULL;
        xcb_randr_get_output_info_reply_t *info = NULL;
        lua_State *L = globalconf_get_lua_State();

        info = xcb_randr_get_output_info_reply(globalconf.connection,
            xcb_randr_get_output_info_unchecked(globalconf.connection,
                output,
                XCB_CURRENT_TIME),
            NULL);
        if(!info)
            return;

        output_name = p_dup((char *)xcb_randr_get_output_info_name(info),
                            xcb_randr_get_output_info_name_length(info));

        switch(connection) {
            case XCB_RANDR_CONNECTION_CONNECTED:
                connection_str = "Connected";
                break;
            case XCB_RANDR_CONNECTION_DISCONNECTED:
                connection_str = "Disconnected";
                break;
            default:
                connection_str = "Unknown";
                break;
        }

        lua_pushstring(L, output_name);
        lua_pushstring(L, connection_str);
        signal_object_emit(L, &global_signals, "screen::change", 2);

        p_delete(&output_name);
        p_delete(&info);

        /* The docs for RRSetOutputPrimary say we get this signal */
        screen_update_primary();
    }
}
static void add_screen(xcb_randr_get_screen_info_reply_t *reply)
{
    const gchar *title = "Display";
    GtkWidget *item = gtk_menu_item_new_with_label(title);
    GtkMenuShell *menu = GTK_MENU_SHELL(app_menu);
    struct screen_info *info = g_malloc(sizeof *info);
    xcb_randr_rotation_t rotation = normalize_rotation(reply->rotation);
    xcb_randr_rotation_t rotations = reply->rotations;

    info->label_menu_item = item;
    info->rotation_menu_group = NULL;
    info->rotation = rotation;
    info->config_timestamp = reply->config_timestamp;
    info->root = reply->root;
    info->sizeID = reply->sizeID;
    info->rate = reply->rate;
    screens_info = g_slist_append(screens_info, info);

    gtk_widget_set_sensitive(item, FALSE);
    gtk_menu_shell_append(menu, item);

#define R(rot, title) \
    if (rotations & XCB_RANDR_ROTATION_##rot) \
        add_screen_rotation(info, XCB_RANDR_ROTATION_##rot, title, \
                XCB_RANDR_ROTATION_##rot & rotation)
    R(ROTATE_0, "Landscape");
    R(ROTATE_90, "Portrait");
    R(ROTATE_180, "Landscape Flipped");
    R(ROTATE_270, "Portrait Flipped");

    if (rotations & xcb_rotations_mask && rotations & xcb_reflections_mask)
        gtk_menu_shell_append(menu, gtk_separator_menu_item_new());

    R(REFLECT_X, "Reflected X");
    R(REFLECT_Y, "Reflected Y");
#undef R

    gtk_menu_shell_append(menu, gtk_separator_menu_item_new());
    gtk_widget_show_all(app_menu);

    /* Get screen resources */
    xcb_randr_get_screen_resources_current_cookie_t resources_cookie;
    xcb_randr_get_screen_resources_current_reply_t *resources_reply;
    xcb_generic_error_t *err = NULL;

    resources_cookie = xcb_randr_get_screen_resources_current(conn,
            info->root);
    resources_reply = xcb_randr_get_screen_resources_current_reply(conn,
            resources_cookie, &err);
    if (err) {
        g_warning("Get Screen Resources returned error %u\n", err->error_code);
        return;
    }

    /* Get screen outputs */
    xcb_randr_output_t *outputs;
    guint i;
    gchar *output_name;

    outputs = xcb_randr_get_screen_resources_current_outputs(resources_reply);
    for (i = 0; i < resources_reply->num_outputs; i++) {
        xcb_randr_get_output_info_reply_t *output_info_reply;
        xcb_randr_get_output_info_cookie_t output_info_cookie =
            xcb_randr_get_output_info_unchecked(conn, outputs[i],
                    resources_reply->config_timestamp);
        output_info_reply =
            xcb_randr_get_output_info_reply(conn, output_info_cookie, NULL);
        /* Show only if connected */
        switch (output_info_reply->connection) {
            case XCB_RANDR_CONNECTION_DISCONNECTED:
            case XCB_RANDR_CONNECTION_UNKNOWN:
                continue;
            case XCB_RANDR_CONNECTION_CONNECTED:
                break;
        }
        output_name = get_output_name(output_info_reply);
        /* Put output names on the menu */
        gtk_menu_item_set_label(GTK_MENU_ITEM(item), output_name);
        g_free(output_name);
        /* TODO: concatenate multiple names or pick them intelligently */
    }
}