/* * switch-display: A tool for switching display for Lenovo T61 laptops * (and friends - with Nvidia Quadro 140 NVS graphics) * * Copyright (C) 2008 Andreas Jellinghaus * * This program is free software; you can redistribute it and/or * modify it under the terms of Version 2 of the GNU General Public * License as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See Version 2 * of the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the: * * Free Software Foundation, Inc. * 59 Temple Place - Suite 330 * Boston, MA 02111-1307, USA * * This program is based on * nvidia-settings: A tool for configuring the NVIDIA X driver on Unix * and Linux systems. * * Copyright (C) 2004 NVIDIA Corporation. * */ #include #include #include #include "NVCtrl.h" #include "NVCtrlLib.h" int main(int argc, char *argv[]) { Display *dpy; Bool ret; int screen = -1; int display_devices, connected_displays, enabled_displays, new_displays; /* * Open a display connection, and make sure the NV-CONTROL X * extension is present on the screen we want to use. */ dpy = XOpenDisplay(NULL); if (!dpy) { fprintf(stderr, "Cannot open display '%s'.\n", XDisplayName(NULL)); return 1; } screen = DefaultScreen(dpy); if (!XNVCTRLIsNvScreen(dpy, screen)) { fprintf(stderr, "The NV-CONTROL X not available on screen " "%d of '%s'.\n", screen, XDisplayName(NULL)); return 1; } /* Probe the GPU for new/old display devices */ ret = XNVCTRLQueryTargetAttribute(dpy, NV_CTRL_TARGET_TYPE_GPU, 0, 0, NV_CTRL_PROBE_DISPLAYS, &display_devices); if (!ret) { fprintf(stderr, "Failed to probe the enabled Display " "Devices on GPU-%d.\n\n", 0); return 1; } printf(" display devices on GPU-%d):\n", 0); /* * Get the bitmask of connected display */ ret = XNVCTRLQueryAttribute(dpy, screen, 0, NV_CTRL_CONNECTED_DISPLAYS, &connected_displays); if (!ret) { fprintf(stderr, "Unable to determine connected displays for " "screen %d of '%s'\n", screen, XDisplayName(NULL)); return 1; } /* * Get the bitmask of enabled display */ ret = XNVCTRLQueryAttribute(dpy, screen, 0, NV_CTRL_ENABLED_DISPLAYS, &enabled_displays); if (!ret) { fprintf(stderr, "Unable to determine enabled displays for " "screen %d of '%s'\n", screen, XDisplayName(NULL)); return 1; } /* now enable what is not enabled (switch) */ new_displays = connected_displays ^ enabled_displays; printf ("connected displays: 0x%08x, enabled displays: 0x%08x new displays: 0x%08x\n", connected_displays, enabled_displays, new_displays); if (!new_displays) { fprintf(stderr, "no new display, aborting!\n"); return 1; } ret = XNVCTRLSetAttributeAndGetStatus (dpy, screen, 0, NV_CTRL_SWITCH_TO_DISPLAYS, new_displays); if (!ret) { fprintf(stderr, "Unable to switch to new displays for " "screen %d of '%s'\n", screen, XDisplayName(NULL)); return 1; } return 0; }