summaryrefslogtreecommitdiffstats
path: root/src/base/base_platform_linux.cpp
blob: 891907bee508a9013a2781b929b453621177294c (plain) (blame)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#if PLATFORM_LINUX
internal LinuxWindowHandle
x11_create_window(MemArena *arena,
                  u32 window_height,
                  u32 window_width,
                  u32 display_x,
                  u32 display_y)
{
    
    
    Display *main_display = XOpenDisplay(0);
    if(!main_display)
    {
        Log("Error! Failed to open x11 display. Are you on X11?");
    }
    else
    {
        Log("Successfully opened x11 display?\n");
        Log("Display: %p\n", main_display);
    }
    
    Window root = XDefaultRootWindow(main_display);
    int screen = DefaultScreen(main_display);
    
    Visual *v = DefaultVisual(main_display, screen);
    
    XSetWindowAttributes wa =
	{
	    .background_pixmap = None,
	    .background_pixel = BlackPixel(main_display, DefaultScreen(main_display)),
	    .border_pixmap = CopyFromParent,
	    .border_pixel= 0,
	    .bit_gravity = ForgetGravity,
	    .win_gravity = NorthWestGravity,
	    .backing_store = NotUseful,
	    .backing_planes = 1,
	    .backing_pixel = 0,
	    .save_under = False,
	    .event_mask = 0,
	    .do_not_propagate_mask = 0,
	    .override_redirect = False,
	    .colormap = CopyFromParent,
	    .cursor = None
	};
    
    Window window = XCreateWindow(
                                  main_display,            // display
                                  root,                    // parent
                                  display_x,               // x
                                  display_y,               // y
                                  window_width,            // width
                                  window_height,           // height
                                  0,                       // border_width
                                  CopyFromParent,          // depth
                                  CopyFromParent,          // class
                                  v,                       // depth
                                  CWBackPixel,             // visual
                                  &wa);
    
    XSetWindowBorder(main_display, window, 60);
    XSelectInput(main_display, window, ExposureMask | StructureNotifyMask | KeyReleaseMask | KeyPressMask);
    XMapWindow(main_display, window);
    
    LinuxWindowHandle out = { main_display, window };
    return out;
    
}

internal void
x11_draw(LinuxWindowHandle *s, s32 start_x, s32 end_x, s32 start_y, s32 end_y)
{
    
    u32 color = 0x55ffaaff;
    GC gc = XCreateGC(s->display, s->window, 0, Nil);
    XSetForeground(s->display, gc, color);
    
    XFreeGC(s->display, gc);
}


internal s32
x11_cleanup(LinuxWindowHandle *s)
{
    XCloseDisplay(s->display);
    return 0;
    
}


internal s32
x11_read_input()
{
    
    return 0;
}

#endif // PLATFORM_LINUX

//-

internal Library
unix_library_load(MemArena *arena, String8 path)
{
    
    const char *cstring_path = null_terminate(arena, &path);
    void *lib = dlopen((const char *)path.data, RTLD_LAZY|RTLD_LOCAL);
    return { (u64)lib } ;
    
}

internal void *
unix_library_get_proc(Library *library, String8 proc_name)
{
    
    return NULL;
}


internal int
unix_library_close(Library *library)
{
    int result = dlclose(library);
    
    if(result)
    {
        
    }
    
    return result;
}

//- implementations
internal b32
unix_file_exists(String8 path)
{
    return access((const char *)path.data, F_OK) == 0;
}

//- file stuff
internal String8
unix_file_load(MemArena *arena, String8 path)
{
    String8     result = {0};
    struct stat sbuf   = {0};
    
    s32 file = open((const char *)path.data, O_RDONLY);
    if(file == -1)
    {
        Log("An error occurred. File was not loaded. error code: %s", file);
        return (String8)
        {
            0
        };
    }
    
    if(fstat(file, &sbuf) == -1)
    {
        close(file);
        return str8_zero();
    }
    
    result      = PushString8(arena, (u64)sbuf.st_size);
    result.size = (u64)sbuf.st_size;
    if(result.size != 0)
        result.data = (u8 *)mmap(0, result.size, PROT_READ, MAP_PRIVATE, file, 0);
    
    close(file);
    return result;
}

internal String8
unix_file_write(String8 data, String8 path)
{
    String8 result = {0};
    s32     file   = open((const char *)path.data, O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if(file == -1)
    {
        return (String8)
        {
            0
        };
    }
    
    u64 written = 0;
    while(written < data.size)
    {
        s64 err = write(file, data.data + written, data.size - written);
        if(err == -1)
        {
            close(file);
            return str8_zero();
        }
        written += err;
    }
    
    close(file);
    result = data;
    return result;
}

internal void
unix_log_with_args(const char *format, ...)
{
    va_list arguments;
    va_start(arguments, format);
    vprintf(format, arguments);
    va_end(arguments);
}

internal b32
unix_os_exit()
{
    _exit(0);
    return true;
}