blob: ef8f214368af97d7612f2905f7720785728111c4 [file] [log] [blame]
scroggob7e9aee2011-03-15 15:15:15 +00001#include "X11/Xlib.h"
2#include "X11/keysym.h"
3
4#include "SkApplication.h"
5#include "SkKey.h"
6#include "SkView.h"
7#include "SkWindow.h"
8#include "XkeysToSkKeys.h"
bsalomon@google.comc8ad63e2011-03-18 14:29:44 +00009extern "C" {
10 #include "keysym2ucs.h"
11}
scroggob7e9aee2011-03-15 15:15:15 +000012#include "SkTypes.h"
13//#include <signal.h>
14//#include <sys/time.h>
15
16// Globals for access to the window
17Display* dsp = 0;
18Window win;
19
20const int WIDTH = 1000;
21const int HEIGHT = 1000;
22
23// Put an event in the X queue to fire an SkEvent.
24static void post_linuxevent()
25{
26 if (!dsp) return;
27 long event_mask = NoEventMask;
28 XClientMessageEvent event;
29 event.type = ClientMessage;
30 Atom myAtom;
31 event.message_type = myAtom;
32 event.format = 32;
33 event.data.l[0] = 0;
34 XSendEvent(dsp, win, false, 0, (XEvent*) &event);
35}
36
37#if 0
38static void catch_alarm(int sig)
39{
40 SkDebugf("caught alarm; calling ServiceQueueTimer\n");
41 SkEvent::ServiceQueueTimer();
42}
43#endif
44
45int main(){
46 dsp = XOpenDisplay(NULL);
47 if(!dsp) {
48 return 1;
49 }
50
51// signal(SIGALRM, catch_alarm);
52
53 win = XCreateSimpleWindow(dsp, DefaultRootWindow(dsp), 0, 0, WIDTH, HEIGHT, 0, 0, 0);
54 XMapWindow(dsp, win);
55
56 long eventMask = StructureNotifyMask;
57 XSelectInput(dsp, win, eventMask);
58
59 // Wait until screen is ready.
60 XEvent evt;
61 do {
62 XNextEvent(dsp, &evt);
63 } while(evt.type != MapNotify);
64
65 GC gc = XCreateGC(dsp, win, 0, NULL);
66 // Start normal Skia sequence
67 application_init();
68
69 SkOSWindow* window = create_sk_window(NULL);
70 window->setUnixWindow(dsp, win, DefaultScreen(dsp), gc);
71 window->resize(WIDTH, HEIGHT);
72
73
74 // Determine which events to listen for.
75 eventMask = StructureNotifyMask|ButtonPressMask|ButtonReleaseMask
scroggo08526c02011-03-22 14:03:21 +000076 |ExposureMask|PointerMotionMask|KeyPressMask|KeyReleaseMask;
scroggob7e9aee2011-03-15 15:15:15 +000077 XSelectInput(dsp, win, eventMask);
78
79 bool loop = true;
80 while (loop) {
81 XNextEvent(dsp, &evt);
82 switch (evt.type) {
83 case Expose:
84 if (evt.xexpose.count == 0)
85 window->inval(NULL);
86 break;
87 case ConfigureNotify:
88 window->resize(evt.xconfigure.width, evt.xconfigure.height);
89 break;
90 case ButtonPress:
91 if (evt.xbutton.button == Button1)
92 window->handleClick(evt.xbutton.x, evt.xbutton.y, SkView::Click::kDown_State);
93 break;
94 case ButtonRelease:
95 if (evt.xbutton.button == Button1)
96 window->handleClick(evt.xbutton.x, evt.xbutton.y, SkView::Click::kUp_State);
97 break;
98 case MotionNotify:
Scroggo0f185c22011-03-24 18:35:50 +000099 window->handleClick(evt.xmotion.x, evt.xmotion.y, SkView::Click::kMoved_State);
scroggob7e9aee2011-03-15 15:15:15 +0000100 break;
101 case KeyPress:
102 {
103 KeySym keysym = XKeycodeToKeysym(dsp, evt.xkey.keycode, 0);
104 //SkDebugf("pressed key %i!\n\tKeySym:%i\n", evt.xkey.keycode, XKeycodeToKeysym(dsp, evt.xkey.keycode, 0));
105 if (keysym == XK_Escape) {
106 loop = false;
107 break;
108 }
109 window->handleKey(XKeyToSkKey(keysym));
110 long uni = keysym2ucs(keysym);
111 if (uni != -1) {
112 window->handleChar((SkUnichar) uni);
113 }
114 break;
115 }
116 case KeyRelease:
117 //SkDebugf("released key %i\n", evt.xkey.keycode);
118 window->handleKeyUp(XKeyToSkKey(XKeycodeToKeysym(dsp, evt.xkey.keycode, 0)));
119 break;
120 case ClientMessage:
121 if (SkEvent::ProcessEvent()) {
122 post_linuxevent();
123 }
124 break;
125 default:
126 // Do nothing for other events
127 break;
128 }
129 }
130
131 XFreeGC(dsp, gc);
132 XDestroyWindow(dsp, win);
133 XCloseDisplay(dsp);
134
135 application_term();
136 return 0;
137}
138
139// SkEvent handlers
140
141void SkEvent::SignalNonEmptyQueue()
142{
143 post_linuxevent();
144}
145
146void SkEvent::SignalQueueTimer(SkMSec delay)
147{
148#if 0
149 itimerval newTimer;
150 newTimer.it_interval.tv_sec = 0;
151 newTimer.it_interval.tv_usec = 0;
152 newTimer.it_value.tv_sec = 0;
153 newTimer.it_value.tv_usec = delay * 1000;
154 int success = setitimer(ITIMER_REAL, NULL, &newTimer);
155 SkDebugf("SignalQueueTimer(%i)\nreturnval = %i\n", delay, success);
156#endif
157}