aboutsummaryrefslogtreecommitdiffhomepage
path: root/unix_test_app/main.cpp
blob: d21723602f57d1fc31b4cc733f48f89872edcb59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "X11/Xlib.h"
#include "X11/keysym.h"

#include "SkApplication.h"
#include "SkKey.h"
#include "SkView.h"
#include "SkWindow.h"
#include "XkeysToSkKeys.h"
extern "C" {
    #include "keysym2ucs.h"
}
#include "SkTypes.h"
//#include <signal.h>
//#include <sys/time.h>

// Globals for access to the window
Display* dsp = 0;
Window win;

const int WIDTH = 1000;
const int HEIGHT = 1000;

// Put an event in the X queue to fire an SkEvent.
static void post_linuxevent()
{
    if (!dsp) return;
    long event_mask = NoEventMask;
    XClientMessageEvent event;
    event.type = ClientMessage;
    Atom myAtom;
    event.message_type = myAtom;
    event.format = 32;
    event.data.l[0] = 0;
    XSendEvent(dsp, win, false, 0, (XEvent*) &event);
}

#if 0
static void catch_alarm(int sig)
{
    SkDebugf("caught alarm; calling ServiceQueueTimer\n");
    SkEvent::ServiceQueueTimer();
}
#endif

int main(){
    dsp = XOpenDisplay(NULL);
    if(!dsp) {
        return 1;
    }

//    signal(SIGALRM, catch_alarm);

    win = XCreateSimpleWindow(dsp, DefaultRootWindow(dsp), 0, 0, WIDTH, HEIGHT, 0, 0, 0);
    XMapWindow(dsp, win);

    long eventMask = StructureNotifyMask;
    XSelectInput(dsp, win, eventMask);

    // Wait until screen is ready.
    XEvent evt;
    do {
        XNextEvent(dsp, &evt);
    } while(evt.type != MapNotify);

    GC gc = XCreateGC(dsp, win, 0, NULL);
    // Start normal Skia sequence
    application_init();

    SkOSWindow* window = create_sk_window(NULL);
    window->setUnixWindow(dsp, win, DefaultScreen(dsp), gc);
    window->resize(WIDTH, HEIGHT);


    // Determine which events to listen for.
    eventMask = StructureNotifyMask|ButtonPressMask|ButtonReleaseMask
            |ExposureMask|Button1MotionMask|KeyPressMask|KeyReleaseMask;
    XSelectInput(dsp, win, eventMask);
 
    bool loop = true;
    while (loop) {
        XNextEvent(dsp, &evt);
        switch (evt.type) {
            case Expose:
                if (evt.xexpose.count == 0)
                    window->inval(NULL);
                break;
            case ConfigureNotify:
                window->resize(evt.xconfigure.width, evt.xconfigure.height);
                break;
            case ButtonPress:
                if (evt.xbutton.button == Button1)
                    window->handleClick(evt.xbutton.x, evt.xbutton.y, SkView::Click::kDown_State);
                break;
            case ButtonRelease:
                if (evt.xbutton.button == Button1)
                    window->handleClick(evt.xbutton.x, evt.xbutton.y, SkView::Click::kUp_State);
                break;
            case MotionNotify:
                // 'If' statement is unnecessary, since we are only masking for button 1
                if (evt.xbutton.button == Button1)
                    window->handleClick(evt.xmotion.x, evt.xmotion.y, SkView::Click::kMoved_State);
                break;
            case KeyPress:
            {
                KeySym keysym = XKeycodeToKeysym(dsp, evt.xkey.keycode, 0);
                //SkDebugf("pressed key %i!\n\tKeySym:%i\n", evt.xkey.keycode, XKeycodeToKeysym(dsp, evt.xkey.keycode, 0));
                if (keysym == XK_Escape) {
                    loop = false;
                    break;
                }
                window->handleKey(XKeyToSkKey(keysym));
                long uni = keysym2ucs(keysym);
                if (uni != -1) {
                    window->handleChar((SkUnichar) uni);
                }
                break;
            }
            case KeyRelease:
                //SkDebugf("released key %i\n", evt.xkey.keycode);
                window->handleKeyUp(XKeyToSkKey(XKeycodeToKeysym(dsp, evt.xkey.keycode, 0)));
                break;
            case ClientMessage:
                if (SkEvent::ProcessEvent()) {
                    post_linuxevent();
                }
                break;
            default:
                // Do nothing for other events
                break;
        }
    }

    XFreeGC(dsp, gc);
    XDestroyWindow(dsp, win);
    XCloseDisplay(dsp);

    application_term();
    return 0;
}

// SkEvent handlers

void SkEvent::SignalNonEmptyQueue()
{
    post_linuxevent();
}

void SkEvent::SignalQueueTimer(SkMSec delay)
{
#if 0
    itimerval newTimer;
    newTimer.it_interval.tv_sec = 0;
    newTimer.it_interval.tv_usec = 0;
    newTimer.it_value.tv_sec = 0;
    newTimer.it_value.tv_usec = delay * 1000;
    int success = setitimer(ITIMER_REAL, NULL, &newTimer);
    SkDebugf("SignalQueueTimer(%i)\nreturnval = %i\n", delay, success);
#endif
}