added a Makefile to use it as a C library. no python dependencies. added 2 example...
[rofl0r-libxauto.git] / src / xaut_display.c
blobd40f5d077b36fa51544fe9bd6a05b132983a0b4a
1 /***************************************************************************
2 * Copyright (C) 2009 by Chris Parker *
3 * chrsprkr3@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the Python License version 2.5 or later. *
7 * *
8 * This program is distributed in the hope that it will be useful, *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
11 ***************************************************************************/
14 $URL$
15 $Author$
16 $Date$
17 $Rev$
20 #include "xaut_display.h"
21 #include "xaut_window.h"
23 int display_h() {
24 if (!_check_init()) {
25 return -1;
27 return defaults->dsp_height;
30 int display_w() {
31 if (!_check_init()) {
32 return -1;
34 return defaults->dsp_width;
37 long desktop_count() {
38 if (!_check_init()) {
39 return -1;
41 if (!_is_supported_feature("_NET_NUMBER_OF_DESKTOPS")) {
42 fprintf(stderr,
43 "Your window manager does not support _NET_NUMBER_OF_DESKTOPS "
44 "so I'm returning 1 since you obviously have one desktop");
45 return 1;
48 Atom type;
49 int size = 0;
50 long item_count = 0;
51 long ret = 1;
53 Window root = XDefaultRootWindow(defaults->display);
54 Atom request =
55 XInternAtom(defaults->display, "_NET_NUMBER_OF_DESKTOPS", FALSE);
57 unsigned char *data =
58 _window_property(root, request, &item_count, &type, &size);
60 if (item_count > 0) {
61 ret = *((long*) data);
63 if(data) {
64 XFree(data);
66 return ret;
69 long desktop(long desk) {
70 if (!_check_init()) {
71 return -1;
73 if(desk > -1) {
74 set_current_desktop(desk);
76 return get_current_desktop();
79 long get_current_desktop() {
80 Atom type;
81 int size = 0;
82 long item_count = 0;
83 long desktop = 1;
85 if (!_is_supported_feature("_NET_CURRENT_DESKTOP")) {
86 fprintf(stderr,
87 "Apparently your window manager doesn't support "
88 "_NET_CURRENT_DESKTOP so I'm returning 1.\n");
89 return 1;
92 Atom request =
93 XInternAtom(defaults->display, "_NET_CURRENT_DESKTOP", FALSE);
94 Window root = XDefaultRootWindow(defaults->display);
96 unsigned char *desktops =
97 _window_property(root, request, &item_count, &type, &size);
99 if (item_count > 0) {
100 desktop = *((long*) desktops);
101 } else {
102 desktop = -2; //Signals a problem as opposed to "I don't do that"
104 if(desktop) {
105 XFree(desktops);
107 desktop++; //Force desktop to be one based rather than zero based
108 return desktop;
111 BOOL set_current_desktop(long desktop) {
112 if (!_check_init()) {
113 return FALSE;
116 if (!_is_supported_feature("_NET_WM_DESKTOP")) {
117 fprintf(stderr,
118 "Apparently your window manager doesn't support "
119 "_NET_WM_DESKTOP so I'm leaving you where you are.\n");
120 return FALSE;
123 logit(LOG_LEVEL_VERBOSE, "Switching to desktop %ld\n", desktop);
124 desktop--; //Force desktop to be one based rather than zero based
125 if(desktop < 0) {
126 char *msg = "This software starts desktop indexes at 1, so your"
127 " request for desktop %ld failed\n";
128 fprintf(stderr, msg, (desktop +1));
129 return FALSE;
131 if((desktop +1) > desktop_count()) {
132 char *msg = "You have specified desktop %ld, but you have only %ld"
133 " desktops available\n";
134 fprintf(stderr, msg, (desktop +1), desktop_count());
135 return FALSE;
138 Window root = RootWindow(defaults->display, 0);
139 XEvent event;
140 memset(&event, 0, sizeof(event));
141 event.xclient.format = 32;
142 event.xclient.data.l[0] = desktop;
143 event.xclient.data.l[1] = CurrentTime;
144 event.type = ClientMessage;
145 event.xclient.display = defaults->display;
146 event.xclient.window = root;
147 event.xclient.message_type =
148 XInternAtom(defaults->display, "_NET_CURRENT_DESKTOP", FALSE);
149 long mask = SubstructureNotifyMask | SubstructureRedirectMask;
150 Status status =
151 XSendEvent(defaults->display, root, FALSE, mask, &event );
152 XFlush(defaults->display);
153 return (status != 0);