1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
6 // Clients do not have raw access to the event stream
7 // (only filtered through the lens of package draw)
8 // but perhaps they will.
21 // An eventType identifies the type of a Native Client Event.
25 eventActive = 1 + iota
36 // A key represents a key on a keyboard.
229 // Arrow & insert/delete pad
284 // Add any other keys here
288 // A keymod is a set of bit flags
293 keymodLshift = 0x0001
294 keymodRshift = 0x0002
304 keymodReserved = 0x8000
309 mouseButtonMiddle = 2
316 mouseStateLeftButtonPressed = 1
317 mouseStateMiddleButtonPressed = 2
318 mouseStateRightButtonPressed = 4
322 activeMouse = 1 // mouse leaving/entering
323 activeInputFocus = 2 // input focus lost/restored
324 activeApplication = 4 // application minimized/restored
327 const maxEventBytes = 64
329 type activeEvent struct {
335 type exposeEvent struct {
339 type keyboardEvent struct {
351 type mouseMotionEvent struct {
362 type mouseButtonEvent struct {
371 type quitEvent struct {
375 type syncEvent struct{}
377 type event interface{}
381 func (r *reader) Read(p []byte) (n int, err os.Error) {
383 if len(b) == 0 && len(p) > 0 {
391 func (w *Window) readEvents() {
392 buf := make([]byte, maxEventBytes)
398 mme *mouseMotionEvent
399 mbe *mouseButtonEvent
402 var m draw.MouseEvent
404 if err := videoPollEvent(buf); err != nil {
406 clean = w.eventc <- draw.ConfigEvent{image.Config{ColorModel, w.Image.Bounds().Dx(), w.Image.Bounds().Dy()}}
408 time.Sleep(10e6) // 10ms
415 log.Print("unsupported event type", buf[0])
418 ea = new(activeEvent)
421 ee = new(exposeEvent)
423 case eventKeyDown, eventKeyUp:
424 ke = new(keyboardEvent)
426 case eventMouseMotion:
427 mme = new(mouseMotionEvent)
429 case eventMouseButtonDown, eventMouseButtonUp:
430 mbe = new(mouseButtonEvent)
437 if err := binary.Read(&r, binary.LittleEndian, e); err != nil {
438 log.Print("unpacking %T event: %s", e, err)
441 // log.Printf("%#v\n", e);
444 w.eventc <- draw.ConfigEvent{image.Config{ColorModel, w.Image.Bounds().Dx(), w.Image.Bounds().Dy()}}
446 w.eventc <- draw.KeyEvent{int(ke.Key)}
448 w.eventc <- draw.KeyEvent{-int(ke.Key)}
449 case eventMouseMotion:
452 m.Buttons = int(mme.Buttons)
453 m.Nsec = time.Nanoseconds()
455 case eventMouseButtonDown:
458 // TODO(rsc): Remove uint cast once 8g bug is fixed.
459 m.Buttons |= 1 << uint(mbe.Button-1)
460 m.Nsec = time.Nanoseconds()
462 case eventMouseButtonUp:
465 // TODO(rsc): Remove uint cast once 8g bug is fixed.
466 m.Buttons &^= 1 << uint(mbe.Button-1)
467 m.Nsec = time.Nanoseconds()