1/*
2* Copyright 2016 Google Inc.
3*
4* Use of this source code is governed by a BSD-style license that can be
5* found in the LICENSE file.
6*/
7
8#ifndef Window_unix_DEFINED
9#define Window_unix_DEFINED
10
11#include "include/private/SkChecksum.h"
12#include "src/core/SkTDynamicHash.h"
13#include "tools/sk_app/Window.h"
14
15#include <GL/glx.h>
16#include <X11/Xlib.h>
17
18#include <string>
19
20typedef Window XWindow;
21
22namespace sk_app {
23
24class Window_unix : public Window {
25public:
26    Window_unix()
27            : Window()
28            , fDisplay(nullptr)
29            , fWindow(0)
30            , fGC(nullptr)
31            , fFBConfig(nullptr)
32            , fVisualInfo(nullptr)
33            , fMSAASampleCount(1) {}
34    ~Window_unix() override { this->closeWindow(); }
35
36    bool initWindow(Display* display);
37
38    void setTitle(const char*) override;
39    void show() override;
40
41    const char* getClipboardText() override;
42    void        setClipboardText(const char*) override;
43
44    bool attach(BackendType) override;
45
46    void onInval() override;
47
48    bool handleEvent(const XEvent& event);
49
50    static const XWindow& GetKey(const Window_unix& w) {
51        return w.fWindow;
52    }
53
54    static uint32_t Hash(const XWindow& w) {
55        return SkChecksum::Mix(w);
56    }
57
58    static SkTDynamicHash<Window_unix, XWindow> gWindowMap;
59
60    void markPendingPaint() { fPendingPaint = true; }
61    void finishPaint() {
62        if (fPendingPaint) {
63            this->onPaint();
64            fPendingPaint = false;
65        }
66    }
67
68    void markPendingResize(int width, int height) {
69        if (width != this->width() || height != this->height()){
70            fPendingResize = true;
71            fPendingWidth = width;
72            fPendingHeight = height;
73        }
74    }
75    void finishResize() {
76        if (fPendingResize) {
77            this->onResize(fPendingWidth, fPendingHeight);
78            fPendingResize = false;
79        }
80    }
81
82    void setRequestedDisplayParams(const DisplayParams&, bool allowReattach) override;
83
84private:
85    void closeWindow();
86
87    Display*     fDisplay;
88    XWindow      fWindow;
89    GC           fGC;
90    GLXFBConfig* fFBConfig;
91    XVisualInfo* fVisualInfo;
92    int          fMSAASampleCount;
93
94    Atom     fWmDeleteMessage;
95
96    bool     fPendingPaint;
97    int      fPendingWidth;
98    int      fPendingHeight;
99    bool     fPendingResize;
100
101    BackendType fBackend = BackendType::kRaster_BackendType;
102
103    std::string fClipboardText;
104
105    using INHERITED = Window;
106};
107
108}   // namespace sk_app
109
110#endif
111