~ubuntu-branches/ubuntu/precise/easystroke/precise

« back to all changes in this revision

Viewing changes to access/track-focus.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Jaeger
  • Date: 2009-01-16 11:31:54 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090116113154-kqvaghw9phb6280b
Tags: 0.3.98.1-0ubuntu1
* New upstream beta (LP: #318051)
* Use lzma compression in the deb.
  - pass -Z lzma to dh_builddeb
  - Pre-Depend on dpkg (>= 1.14.12ubuntu3)
* Build-Depend on intltool for the i18n of the desktop file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (c) 2008, Thomas Jaeger <ThJaeger@gmail.com>
3
 
 *
4
 
 * Permission to use, copy, modify, and/or distribute this software for any
5
 
 * purpose with or without fee is hereby granted, provided that the above
6
 
 * copyright notice and this permission notice appear in all copies.
7
 
 *
8
 
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
 
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
 
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
11
 
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
 
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
13
 
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
14
 
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
 
 */
16
 
#include <stdio.h>
17
 
#include <stdlib.h>
18
 
#include <signal.h>
19
 
#include "cspi/spi.h"
20
 
#include <map>
21
 
#include <string>
22
 
#include <iostream>
23
 
 
24
 
static AccessibleEventListener *focus_listener;
25
 
static AccessibleEventListener *activate_listener;
26
 
static AccessibleEventListener *deactivate_listener;
27
 
 
28
 
int stack[4];
29
 
int length;
30
 
 
31
 
void push(int x) {
32
 
        for (int i=0; i<length; i++)
33
 
                if (stack[i] == x)
34
 
                        return;
35
 
        for (int i=3; i>0; i--)
36
 
                stack[i] = stack[i-1];
37
 
        stack[0] = x;
38
 
        if (length < 4)
39
 
                length++;
40
 
}
41
 
 
42
 
void pop(int x) {
43
 
        for (int i=0; i<length; i++)
44
 
                if (stack[i] == x) {
45
 
                        for (int j=i; j<3; j++)
46
 
                                stack[j] = stack[j+1];
47
 
                        length--;
48
 
                }
49
 
}
50
 
 
51
 
std::map<int, std::string> focus_map;
52
 
 
53
 
void print() {
54
 
        static std::string last = "";
55
 
        std::string now = length ? focus_map[stack[0]] : "no app or app doesn't support at-spi";
56
 
        if (now == "")
57
 
                now = "no focus information about this app yet";
58
 
        if (now == last)
59
 
                return;
60
 
        last = now;
61
 
        std::cout << now << std::endl;
62
 
}
63
 
 
64
 
void on_focus(const AccessibleEvent *event, void *user_data) {
65
 
        if (!event->source)
66
 
                return;
67
 
        AccessibleApplication *app = Accessible_getHostApplication(event->source);
68
 
        char *name = Accessible_getName(event->source);
69
 
        char *role = Accessible_getRoleName(event->source);
70
 
        if (app && name && role) {
71
 
                int app_id = AccessibleApplication_getID(app);
72
 
                focus_map[app_id] = std::string(name) + "(" + std::string(role) + ")";
73
 
                push(app_id);
74
 
                print();
75
 
        } else printf("something went wrong\n");
76
 
        if (app) {
77
 
                AccessibleApplication_unref(app);
78
 
                AccessibleApplication_unref(app);
79
 
        }
80
 
        if (name)
81
 
                SPI_freeString(name);
82
 
        if (role)
83
 
                SPI_freeString(role);
84
 
}
85
 
 
86
 
void on_activate(const AccessibleEvent *event, void *user_data) {
87
 
        if (!event->source)
88
 
                return;
89
 
        AccessibleApplication *app = Accessible_getHostApplication(event->source);
90
 
        if (!app)
91
 
                return;
92
 
        push(AccessibleApplication_getID(app));
93
 
        print();
94
 
        AccessibleApplication_unref(app);
95
 
        AccessibleApplication_unref(app);
96
 
}
97
 
 
98
 
void on_deactivate(const AccessibleEvent *event, void *user_data) {
99
 
        if (!event->source)
100
 
                return;
101
 
        AccessibleApplication *app = Accessible_getHostApplication(event->source);
102
 
        if (!app)
103
 
                return;
104
 
        pop(AccessibleApplication_getID(app));
105
 
        print();
106
 
        AccessibleApplication_unref(app);
107
 
        AccessibleApplication_unref(app);
108
 
}
109
 
 
110
 
void quit(int sig) {
111
 
        SPI_deregisterGlobalEventListenerAll(focus_listener);
112
 
        SPI_deregisterGlobalEventListenerAll(activate_listener);
113
 
        SPI_deregisterGlobalEventListenerAll(deactivate_listener);
114
 
        AccessibleEventListener_unref(focus_listener);
115
 
        AccessibleEventListener_unref(activate_listener);
116
 
        AccessibleEventListener_unref(deactivate_listener);
117
 
        SPI_event_quit();
118
 
}
119
 
 
120
 
int main (int argc, char **argv) {
121
 
        if (SPI_init()) {
122
 
                printf("Error: AT-SPI not available\n");
123
 
                exit(EXIT_FAILURE);
124
 
        };
125
 
        signal(SIGINT, &quit);
126
 
        focus_listener = SPI_createAccessibleEventListener(on_focus, NULL);
127
 
        activate_listener = SPI_createAccessibleEventListener(on_activate, NULL);
128
 
        deactivate_listener = SPI_createAccessibleEventListener(on_deactivate, NULL);
129
 
        SPI_registerGlobalEventListener(focus_listener, "focus:");
130
 
        SPI_registerGlobalEventListener(activate_listener, "window:activate");
131
 
        SPI_registerGlobalEventListener(deactivate_listener, "window:deactivate");
132
 
        SPI_event_main();
133
 
        return SPI_exit();
134
 
}