4coder/platform_linux/linux_4ed.cpp

2170 lines
66 KiB
C++
Raw Normal View History

2016-09-04 20:41:48 +00:00
/*
* Mr. 4th Dimention - Allen Webster
* (Mostly by insofaras)
*
* 14.11.2015
*
* Linux layer for 4coder
2016-09-04 20:41:48 +00:00
*
*/
// TOP
2017-06-30 21:28:09 +00:00
#define IS_PLAT_LAYER
2017-11-21 21:30:40 +00:00
#include "4coder_os_comp_cracking.h"
2017-06-30 21:28:09 +00:00
#include <string.h>
#include "4ed_defines.h"
#include "4coder_API/version.h"
2016-09-04 20:41:48 +00:00
2017-02-18 01:04:41 +00:00
#include "4coder_lib/4coder_utf8.h"
#if defined(FRED_SUPER)
# include "4coder_generated/keycodes.h"
# include "4coder_generated/style.h"
2016-09-04 20:41:48 +00:00
# define FSTRING_IMPLEMENTATION
# include "4coder_lib/4coder_string.h"
# include "4coder_lib/4coder_mem.h"
2016-09-04 20:41:48 +00:00
# include "4coder_API/types.h"
2016-09-04 20:41:48 +00:00
#else
# include "4coder_default_bindings.cpp"
#endif
#include "4ed_math.h"
#include "4ed_font.h"
2016-09-04 20:41:48 +00:00
#include "4ed_system.h"
2017-06-30 19:08:11 +00:00
#include "4ed_log.h"
2017-11-10 18:27:39 +00:00
#include "4ed_render_target.h"
#include "4ed_render_format.h"
2016-09-04 20:41:48 +00:00
#include "4ed.h"
#include "4ed_linked_node_macros.h"
2016-09-04 20:41:48 +00:00
2017-06-30 22:09:18 +00:00
#include "4ed_file_track.h"
#include "4ed_system_shared.h"
2017-07-18 21:23:45 +00:00
#include "unix_4ed_headers.h"
2016-09-04 20:41:48 +00:00
#include <math.h>
#include <stdlib.h>
2016-09-04 20:41:48 +00:00
#include <locale.h>
#include <xmmintrin.h>
2016-09-04 20:41:48 +00:00
#include <ucontext.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/wait.h>
2016-09-04 20:41:48 +00:00
#include <sys/timerfd.h>
#include <sys/eventfd.h>
#include <sys/epoll.h>
2016-09-04 20:41:48 +00:00
#include <sys/inotify.h>
#include <X11/Xlib.h>
#include <X11/cursorfont.h>
#include <X11/Xatom.h>
#include <X11/extensions/Xfixes.h>
#include <GL/glx.h>
#include <GL/glext.h>
#include <linux/fs.h>
#include <linux/input.h>
2017-07-18 20:17:57 +00:00
#include "4ed_shared_thread_constants.h"
2017-09-10 18:24:26 +00:00
#include "unix_threading_wrapper.h"
2017-11-08 01:15:09 +00:00
#include "linux_semaphore_wrapper.h"
2017-07-18 20:17:57 +00:00
2016-09-04 20:41:48 +00:00
//
// Linux macros
//
#define FPS 60L
#define frame_useconds (1000000UL / FPS)
#define LINUX_FN_DEBUG(fmt, ...) do { \
2017-07-03 15:43:51 +00:00
LOGF("%s: " fmt "\n", __func__, ##__VA_ARGS__); \
2017-07-18 22:36:50 +00:00
} while (0)
2016-09-04 20:41:48 +00:00
// TODO(allen): Make an intrinsics header that uses the cracked OS to define a single set of intrinsic names.
#define InterlockedCompareExchange(dest, ex, comp) \
__sync_val_compare_and_swap((dest), (comp), (ex))
2016-09-04 20:41:48 +00:00
//
// Linux structs / enums
//
enum {
LINUX_4ED_EVENT_X11 = (UINT64_C(1) << 32),
LINUX_4ED_EVENT_X11_INTERNAL = (UINT64_C(2) << 32),
LINUX_4ED_EVENT_STEP = (UINT64_C(3) << 32),
LINUX_4ED_EVENT_STEP_TIMER = (UINT64_C(4) << 32),
LINUX_4ED_EVENT_CLI = (UINT64_C(5) << 32),
2016-09-04 20:41:48 +00:00
};
2017-07-18 17:44:54 +00:00
//
// Linux forward declarations
//
2017-07-18 17:44:54 +00:00
internal void LinuxStringDup(String*, void*, size_t);
////////////////////////////////
#define SLASH '/'
#define DLL "so"
2017-07-19 00:23:38 +00:00
global System_Functions sysfunc;
#include "4ed_shared_library_constants.h"
2017-09-10 18:24:26 +00:00
#include "unix_library_wrapper.h"
#include "4ed_standard_libraries.cpp"
#include "4ed_coroutine.cpp"
#include "4ed_font.cpp"
////////////////////////////////
2016-09-04 20:41:48 +00:00
struct Linux_Vars{
Display *XDisplay;
Window XWindow;
2016-09-04 20:41:48 +00:00
XIM input_method;
XIMStyle input_style;
XIC input_context;
2016-09-04 20:41:48 +00:00
Application_Step_Input input;
2016-09-04 20:41:48 +00:00
String clipboard_contents;
String clipboard_outgoing;
b32 new_clipboard;
2016-09-04 20:41:48 +00:00
Atom atom_TARGETS;
Atom atom_CLIPBOARD;
Atom atom_UTF8_STRING;
Atom atom__NET_WM_STATE;
Atom atom__NET_WM_STATE_MAXIMIZED_HORZ;
Atom atom__NET_WM_STATE_MAXIMIZED_VERT;
Atom atom__NET_WM_STATE_FULLSCREEN;
Atom atom__NET_WM_PING;
Atom atom__NET_WM_WINDOW_TYPE;
Atom atom__NET_WM_WINDOW_TYPE_NORMAL;
Atom atom__NET_WM_PID;
Atom atom_WM_DELETE_WINDOW;
2016-09-04 20:41:48 +00:00
b32 has_xfixes;
int xfixes_selection_event;
int epoll;
2016-09-04 20:41:48 +00:00
int step_timer_fd;
int step_event_fd;
int x11_fd;
int inotify_fd;
2016-09-04 20:41:48 +00:00
u64 last_step;
b32 full_screen;
b32 do_toggle;
2016-09-04 20:41:48 +00:00
b32 keep_running;
2016-09-04 20:41:48 +00:00
Application_Mouse_Cursor cursor;
b32 hide_cursor;
Cursor hidden_cursor;
sem_t thread_semaphore;
2016-09-04 20:41:48 +00:00
i32 dpi_x, dpi_y;
2016-09-04 20:41:48 +00:00
b32 vsync;
};
////////////////////////////////
2016-09-04 20:41:48 +00:00
global Linux_Vars linuxvars;
2017-07-18 23:17:40 +00:00
global Render_Target target;
global Application_Memory memory_vars;
2017-07-18 21:19:28 +00:00
global Plat_Settings plat_settings;
global Libraries libraries;
global App_Functions app;
global Custom_API custom_api;
2016-09-04 20:41:48 +00:00
global Coroutine_System_Auto_Alloc coroutines;
2017-07-18 17:44:54 +00:00
////////////////////////////////
2016-09-04 20:41:48 +00:00
#include "linux_icon.h"
internal void
2017-07-18 23:41:28 +00:00
linux_set_icon(Display* d, Window w){
Atom WM_ICON = XInternAtom(d, "_NET_WM_ICON", False);
XChangeProperty(d, w, WM_ICON, XA_CARDINAL, 32, PropModeReplace, (unsigned char*)linux_icon, sizeof(linux_icon) / sizeof(long));
}
2017-07-18 23:41:28 +00:00
#include "linux_error_box.cpp"
////////////////////////////////
2017-07-18 17:44:54 +00:00
internal sem_t*
handle_sem(Plat_Handle h){
return(*(sem_t**)&h);
}
2016-09-04 20:41:48 +00:00
2017-07-18 17:44:54 +00:00
internal Plat_Handle
handle_sem(sem_t *sem){
return(*(Plat_Handle*)&sem);
2017-07-18 17:44:54 +00:00
}
2016-09-04 20:41:48 +00:00
2017-07-18 17:44:54 +00:00
////////////////////////////////
2016-09-04 20:41:48 +00:00
2017-11-04 05:07:14 +00:00
internal
Sys_Get_4ed_Path_Sig(system_get_4ed_path){
ssize_t size = readlink("/proc/self/exe", out, capacity - 1);
if (size != -1 && size < capacity - 1){
String str = make_string(out, size);
remove_last_folder(&str);
terminate_with_null(&str);
size = str.size;
}
else{
size = 0;
}
return(size);
}
2017-07-18 21:23:45 +00:00
#include "unix_4ed_functions.cpp"
internal
Sys_Now_Time_Sig(system_now_time){
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
u64 result = (spec.tv_sec*UINT64_C(1000000)) + (spec.tv_nsec/UINT64_C(1000));
return(result);
}
2017-07-18 21:23:45 +00:00
#include "4ed_shared_file_handling.cpp"
////////////////////////////////
internal void
system_schedule_step(){
u64 now = system_now_time();
u64 diff = (now - linuxvars.last_step);
if (diff > (u64)frame_useconds){
u64 ev = 1;
ssize_t size = write(linuxvars.step_event_fd, &ev, sizeof(ev));
AllowLocal(size);
}
else{
struct itimerspec its = {};
timerfd_gettime(linuxvars.step_timer_fd, &its);
if (its.it_value.tv_sec == 0 && its.it_value.tv_nsec == 0){
its.it_value.tv_nsec = (frame_useconds - diff) * 1000UL;
timerfd_settime(linuxvars.step_timer_fd, 0, &its, NULL);
}
}
}
////////////////////////////////
#include "4ed_work_queues.cpp"
////////////////////////////////
2017-07-18 20:21:04 +00:00
internal void
linux_set_wm_state(Display* d, Window w, Atom one, Atom two, int mode){
2017-07-18 20:21:04 +00:00
//NOTE(inso): this will only work after it is mapped
enum { STATE_REMOVE, STATE_ADD, STATE_TOGGLE };
XEvent e = {};
e.xany.type = ClientMessage;
e.xclient.message_type = linuxvars.atom__NET_WM_STATE;
e.xclient.format = 32;
e.xclient.window = w;
e.xclient.data.l[0] = mode;
e.xclient.data.l[1] = one;
e.xclient.data.l[2] = two;
e.xclient.data.l[3] = 1L;
XSendEvent(d, RootWindow(d, 0), 0, SubstructureNotifyMask | SubstructureRedirectMask, &e);
}
internal void
linux_maximize_window(b32 maximize){
linux_set_wm_state(linuxvars.XDisplay, linuxvars.XWindow, linuxvars.atom__NET_WM_STATE_MAXIMIZED_HORZ, linuxvars.atom__NET_WM_STATE_MAXIMIZED_VERT, maximize?1:0);
}
internal void
linux_toggle_fullscreen(){
linuxvars.full_screen = !linuxvars.full_screen;
linux_set_wm_state(linuxvars.XDisplay, linuxvars.XWindow, linuxvars.atom__NET_WM_STATE_FULLSCREEN, 0, linuxvars.full_screen?1:0);
}
2017-07-18 20:21:04 +00:00
2016-09-04 20:41:48 +00:00
internal
Sys_Show_Mouse_Cursor_Sig(system_show_mouse_cursor){
2016-09-04 20:41:48 +00:00
linuxvars.hide_cursor = !show;
XDefineCursor(linuxvars.XDisplay, linuxvars.XWindow, show?None:linuxvars.hidden_cursor);
2016-09-04 20:41:48 +00:00
}
internal
Sys_Set_Fullscreen_Sig(system_set_fullscreen){
b32 success = true;
linuxvars.do_toggle = (linuxvars.full_screen != full_screen);
return(success);
2016-09-04 20:41:48 +00:00
}
internal
Sys_Is_Fullscreen_Sig(system_is_fullscreen){
b32 result = (linuxvars.full_screen != linuxvars.do_toggle);
return(result);
2016-09-04 20:41:48 +00:00
}
// HACK(allen): Why does this work differently from the win32 version!?
2016-09-10 02:57:45 +00:00
internal
Sys_Send_Exit_Signal_Sig(system_send_exit_signal){
linuxvars.keep_running = false;
2016-09-10 02:57:45 +00:00
}
2017-09-10 19:14:41 +00:00
#include "4ed_coroutine_functions.cpp"
#include "4ed_system_shared.cpp"
2016-09-04 20:41:48 +00:00
//
// Clipboard
//
internal
Sys_Post_Clipboard_Sig(system_post_clipboard){
LinuxStringDup(&linuxvars.clipboard_outgoing, str.str, str.size);
XSetSelectionOwner(linuxvars.XDisplay, linuxvars.atom_CLIPBOARD, linuxvars.XWindow, CurrentTime);
}
//
// CLI
//
internal
Sys_CLI_Call_Sig(system_cli_call, path, script_name, cli_out){
LINUX_FN_DEBUG("%s %s", path, script_name);
int pipe_fds[2];
2017-07-01 02:33:15 +00:00
if (pipe(pipe_fds) == -1){
perror("system_cli_call: pipe");
return 0;
}
pid_t child_pid = fork();
2017-07-01 02:33:15 +00:00
if (child_pid == -1){
perror("system_cli_call: fork");
return 0;
}
enum { PIPE_FD_READ, PIPE_FD_WRITE };
// child
2017-07-01 02:33:15 +00:00
if (child_pid == 0){
close(pipe_fds[PIPE_FD_READ]);
dup2(pipe_fds[PIPE_FD_WRITE], STDOUT_FILENO);
dup2(pipe_fds[PIPE_FD_WRITE], STDERR_FILENO);
2017-07-03 15:59:39 +00:00
if (chdir(path) == -1){
perror("system_cli_call: chdir");
exit(1);
2017-07-03 15:59:39 +00:00
}
char* argv[] = { "sh", "-c", script_name, NULL };
2017-07-03 15:59:39 +00:00
if (execv("/bin/sh", argv) == -1){
perror("system_cli_call: execv");
}
exit(1);
2017-07-01 02:33:15 +00:00
}
else{
close(pipe_fds[PIPE_FD_WRITE]);
*(pid_t*)&cli_out->proc = child_pid;
*(int*)&cli_out->out_read = pipe_fds[PIPE_FD_READ];
*(int*)&cli_out->out_write = pipe_fds[PIPE_FD_WRITE];
struct epoll_event e = {};
e.events = EPOLLIN | EPOLLET;
e.data.u64 = LINUX_4ED_EVENT_CLI;
epoll_ctl(linuxvars.epoll, EPOLL_CTL_ADD, pipe_fds[PIPE_FD_READ], &e);
}
2017-07-01 02:33:15 +00:00
return(true);
}
internal
Sys_CLI_Begin_Update_Sig(system_cli_begin_update){
// NOTE(inso): I don't think anything needs to be done here.
}
internal
Sys_CLI_Update_Step_Sig(system_cli_update_step){
int pipe_read_fd = *(int*)&cli->out_read;
fd_set fds;
FD_ZERO(&fds);
FD_SET(pipe_read_fd, &fds);
struct timeval tv = {};
size_t space_left = max;
char* ptr = dest;
2017-07-18 22:36:50 +00:00
while (space_left > 0 && select(pipe_read_fd + 1, &fds, NULL, NULL, &tv) == 1){
ssize_t num = read(pipe_read_fd, ptr, space_left);
2017-07-03 15:59:39 +00:00
if (num == -1){
perror("system_cli_update_step: read");
2017-07-03 15:59:39 +00:00
} else if (num == 0){
// NOTE(inso): EOF
break;
} else {
ptr += num;
space_left -= num;
}
}
*amount = (ptr - dest);
2017-07-01 02:33:15 +00:00
return((ptr - dest) > 0);
}
internal
Sys_CLI_End_Update_Sig(system_cli_end_update){
pid_t pid = *(pid_t*)&cli->proc;
2017-07-01 02:33:15 +00:00
b32 close_me = false;
int status;
2017-07-03 15:59:39 +00:00
if (pid && waitpid(pid, &status, WNOHANG) > 0){
cli->exit = WEXITSTATUS(status);
2017-11-10 18:27:39 +00:00
close_me = true;
close(*(int*)&cli->out_read);
close(*(int*)&cli->out_write);
2017-11-10 18:27:39 +00:00
struct epoll_event e = {};
epoll_ctl(linuxvars.epoll, EPOLL_CTL_DEL, *(int*)&cli->out_read, &e);
}
2017-07-01 02:33:15 +00:00
return(close_me);
}
#include "4ed_font_provider_freetype.h"
global u32 system_font_method = SystemFontMethod_FilePath;
#include "4ed_font_provider_freetype.cpp"
2017-11-17 21:20:25 +00:00
#undef internal
#include <fontconfig/fontconfig.h>
#define internal static
global FcConfig *fc_config = 0;
internal
Sys_Font_Path(name, parameters){
if (fc_config == 0){
fc_config = FcInitLoadConfigAndFonts();
}
Font_Path path = {0};
FcPattern *pattern_regular = FcPatternBuild(
0,
FC_POSTSCRIPT_NAME, FcTypeString, name,
FC_SIZE, FcTypeDouble, (double)parameters->pt_size,
FC_FONTFORMAT, FcTypeString, "TrueType",
FC_STYLE, FcTypeString, (FcChar8*)"Regular",
(void*)0);
FcPattern *pattern_styled = 0;
if (parameters->italics || parameters->bold){
if (parameters->italics && !parameters->bold){
pattern_styled = FcPatternBuild(
0,
FC_POSTSCRIPT_NAME, FcTypeString, name,
FC_SIZE, FcTypeDouble, (double)parameters->pt_size,
FC_FONTFORMAT, FcTypeString, "TrueType",
FC_STYLE, FcTypeString, (FcChar8*)"Italic",
(void*)0);
}
else if (!parameters->italics && parameters->bold){
pattern_styled = FcPatternBuild(
0,
FC_POSTSCRIPT_NAME, FcTypeString, name,
FC_SIZE, FcTypeDouble, (double)parameters->pt_size,
FC_FONTFORMAT, FcTypeString, "TrueType",
FC_STYLE, FcTypeString, (FcChar8*)"Bold",
(void*)0);
}
else{
pattern_styled = FcPatternBuild(
0,
FC_POSTSCRIPT_NAME, FcTypeString, name,
FC_SIZE, FcTypeDouble, (double)parameters->pt_size,
FC_FONTFORMAT, FcTypeString, "TrueType",
FC_STYLE, FcTypeString, (FcChar8*)"Bold Italic",
(void*)0);
}
}
2017-11-21 02:28:46 +00:00
Partition *part = &shared_vars.font_scratch;
path.temp = begin_temp_memory(part);
b32 got_font = false;
if (pattern_styled != 0){
if (FcConfigSubstitute(fc_config, pattern_styled, FcMatchPattern)){
FcDefaultSubstitute(pattern_styled);
FcResult res;
FcPattern *font = FcFontMatch(fc_config, pattern_styled, &res);
FcChar8 *filename = 0;
if (font != 0){
FcPatternGetString(font, FC_FILE, 0, &filename);
if (filename != 0){
i32 len = str_size((char*)filename);
char *buffer = push_array(part, char, len + 1);
if (buffer == 0){
sysshared_partition_grow(part, l_round_up_i32(len + 1, KB(4)));
buffer = push_array(part, char, len + 1);
}
if (buffer != 0){
push_align(part, 8);
memcpy(buffer, filename, len + 1);
path.len = len;
path.name = buffer;
}
got_font = true;
}
FcPatternDestroy(font);
}
}
FcPatternDestroy(pattern_styled);
pattern_styled = 0;
}
if (!got_font){
if (FcConfigSubstitute(fc_config, pattern_regular, FcMatchPattern)){
FcDefaultSubstitute(pattern_regular);
FcResult res;
FcPattern *font = FcFontMatch(fc_config, pattern_regular, &res);
FcChar8 *filename = 0;
if (font != 0){
FcPatternGetString(font, FC_FILE, 0, &filename);
if (filename != 0){
i32 len = str_size((char*)filename);
char *buffer = push_array(part, char, len + 1);
if (buffer == 0){
sysshared_partition_grow(part, l_round_up_i32(len + 1, KB(4)));
buffer = push_array(part, char, len + 1);
}
if (buffer != 0){
push_align(part, 8);
memcpy(buffer, filename, len + 1);
path.len = len;
path.name = buffer;
}
got_font = true;
}
FcPatternDestroy(font);
}
}
FcPatternDestroy(pattern_styled);
}
FcPatternDestroy(pattern_regular);
return(path);
}
Sys_Font_Data_Not_Used;
2017-11-17 21:20:25 +00:00
internal char*
linux_get_loadable_fonts(Partition *part, Font_Setup_List *list){
if (fc_config == 0){
fc_config = FcInitLoadConfigAndFonts();
}
2017-11-17 21:20:25 +00:00
FcPattern* pat = FcPatternBuild(
0,
FC_STYLE, FcTypeString, (FcChar8*)"Regular",
(void*)0);
FcObjectSet* os = FcObjectSetBuild(FC_FAMILY, FC_FILE, (char*)0);
FcFontSet* fs = FcFontList(fc_config, pat, os);
2017-11-17 21:20:25 +00:00
if (fs != 0){
LOGF("Total matching fonts: %d\n", fs->nfont);
for (int i=0; fs && i < fs->nfont; ++i) {
FcPattern* font = fs->fonts[i];
FcChar8 *file = 0;
FcChar8 *style = 0;
FcChar8 *family = 0;
if (FcPatternGetString(font, FC_FILE, 0, &file) == FcResultMatch &&
FcPatternGetString(font, FC_FAMILY, 0, &family) == FcResultMatch)
{
Temp_Memory reset = begin_temp_memory(part);
Font_Setup *setup = push_array(part, Font_Setup, 1);
if (setup != 0){
memset(setup, 0, sizeof(*setup));
i32 len = str_size((char*)file);
if (len < sizeof(setup->stub.name)){
i32 name_len = str_size((char*)family);
if (name_len < sizeof(setup->name)){
setup->stub.load_from_path = true;
memcpy(setup->stub.name, file, len + 1);
setup->stub.len = len;
setup->has_display_name = true;
setup->len = name_len;
memcpy(setup->name, family, name_len + 1);
sll_push(list->first, list->last, setup);
}
else{
end_temp_memory(reset);
}
}
else{
end_temp_memory(reset);
}
}
}
}
FcFontSetDestroy(fs);
}
}
#include <GL/gl.h>
#include "opengl/4ed_opengl_render.cpp"
2016-09-04 20:41:48 +00:00
//
// End of system funcs
//
//
// Renderer
//
internal void
LinuxResizeTarget(i32 width, i32 height){
if (width > 0 && height > 0){
2017-07-18 23:17:40 +00:00
target.width = width;
target.height = height;
2016-09-04 20:41:48 +00:00
}
}
//
// OpenGL init
//
// NOTE(allen): Thanks to Casey for providing the linux OpenGL launcher.
static bool ctxErrorOccurred = false;
internal int
2017-11-10 21:13:02 +00:00
ctxErrorHandler( Display *dpy, XErrorEvent *ev ){
2016-09-04 20:41:48 +00:00
ctxErrorOccurred = true;
return 0;
}
2017-11-10 21:55:43 +00:00
typedef GLXContext (glXCreateContextAttribsARB_Function)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
typedef void (glXSwapIntervalEXT_Function) (Display *dpy, GLXDrawable drawable, int interval);
typedef int (glXSwapIntervalMESA_Function) (unsigned int interval);
typedef int (glXGetSwapIntervalMESA_Function) (void);
typedef int (glXSwapIntervalSGI_Function) (int interval);
2016-09-04 20:41:48 +00:00
internal GLXContext
2017-11-10 21:55:43 +00:00
InitializeOpenGLContext(Display *XDisplay, Window XWindow, GLXFBConfig *best_config){
2016-09-04 20:41:48 +00:00
const char *glxExts = glXQueryExtensionsString(XDisplay, DefaultScreen(XDisplay));
2017-11-10 21:58:02 +00:00
glXCreateContextAttribsARB_Function *glXCreateContextAttribsARB = 0;
glXSwapIntervalEXT_Function *glXSwapIntervalEXT = 0;
glXSwapIntervalMESA_Function *glXSwapIntervalMESA = 0;
glXGetSwapIntervalMESA_Function *glXGetSwapIntervalMESA = 0;
glXSwapIntervalSGI_Function *glXSwapIntervalSGI = 0;
2017-11-10 21:55:43 +00:00
#define GLXLOAD(f) f = (f##_Function*) glXGetProcAddressARB((const GLubyte*) #f);
2016-09-04 20:41:48 +00:00
GLXLOAD(glXCreateContextAttribsARB);
2016-09-04 20:41:48 +00:00
GLXContext ctx = 0;
ctxErrorOccurred = false;
int (*oldHandler)(Display*, XErrorEvent*) = XSetErrorHandler(&ctxErrorHandler);
2017-11-10 21:55:43 +00:00
if (glXCreateContextAttribsARB == 0){
2017-07-03 15:59:39 +00:00
LOG("glXCreateContextAttribsARB() not found, using old-style GLX context\n" );
2017-11-10 21:55:43 +00:00
ctx = glXCreateNewContext( XDisplay, *best_config, GLX_RGBA_TYPE, 0, True );
2016-09-04 20:41:48 +00:00
}
2017-11-10 21:55:43 +00:00
else{
int context_attribs[] = {
GLX_CONTEXT_MAJOR_VERSION_ARB, 2,
GLX_CONTEXT_MINOR_VERSION_ARB, 1,
2016-09-04 20:41:48 +00:00
GLX_CONTEXT_PROFILE_MASK_ARB , GLX_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
#if defined(FRED_INTERNAL)
2016-09-04 20:41:48 +00:00
GLX_CONTEXT_FLAGS_ARB , GLX_CONTEXT_DEBUG_BIT_ARB,
#endif
None
};
2017-11-10 21:55:43 +00:00
LOG("Creating GL 2.1 context... ");
ctx = glXCreateContextAttribsARB(XDisplay, *best_config, 0, True, context_attribs);
2016-09-04 20:41:48 +00:00
XSync( XDisplay, False );
2017-11-10 21:55:43 +00:00
if (!ctxErrorOccurred && ctx){
LOG("Created GL 2.1 context.\n");
2016-09-04 20:41:48 +00:00
}
2017-11-10 21:55:43 +00:00
else{
LOG("Could not create a context.\n");
exit(1);
2016-09-04 20:41:48 +00:00
}
}
2016-09-04 20:41:48 +00:00
XSync(XDisplay, False);
XSetErrorHandler(oldHandler);
2017-07-03 15:59:39 +00:00
if (ctxErrorOccurred || !ctx){
LOG("Failed to create an OpenGL context\n");
2016-09-04 20:41:48 +00:00
exit(1);
}
2016-09-04 20:41:48 +00:00
b32 Direct;
2017-11-10 21:13:02 +00:00
if (!glXIsDirect(XDisplay, ctx)){
2017-07-03 15:59:39 +00:00
LOG("Indirect GLX rendering context obtained\n");
Direct = false;
2016-09-04 20:41:48 +00:00
}
2017-07-03 15:59:39 +00:00
else{
LOG("Direct GLX rendering context obtained\n");
Direct = true;
2016-09-04 20:41:48 +00:00
}
2017-07-03 15:59:39 +00:00
LOG("Making context current\n");
2016-09-04 20:41:48 +00:00
glXMakeCurrent( XDisplay, XWindow, ctx );
//TODO(inso): glGetStringi is required in core profile if the GL version is >= 3.0
2017-11-10 21:13:02 +00:00
//char *Extensions = (char *)glGetString(GL_EXTENSIONS);
2016-09-04 20:41:48 +00:00
//NOTE(inso): enable vsync if available. this should probably be optional
2017-07-03 15:59:39 +00:00
if (Direct && strstr(glxExts, "GLX_EXT_swap_control ")){
2016-09-04 20:41:48 +00:00
GLXLOAD(glXSwapIntervalEXT);
2017-11-10 21:13:02 +00:00
if (glXSwapIntervalEXT != 0){
2016-09-04 20:41:48 +00:00
glXSwapIntervalEXT(XDisplay, XWindow, 1);
2016-09-04 20:41:48 +00:00
unsigned int swap_val = 0;
glXQueryDrawable(XDisplay, XWindow, GLX_SWAP_INTERVAL_EXT, &swap_val);
2017-11-10 21:13:02 +00:00
linuxvars.vsync = (swap_val == true);
2017-07-03 15:59:39 +00:00
LOGF("VSync enabled? %s.\n", linuxvars.vsync ? "Yes" : "No");
2016-09-04 20:41:48 +00:00
}
2017-07-03 15:59:39 +00:00
}
else if (Direct && strstr(glxExts, "GLX_MESA_swap_control ")){
2016-09-04 20:41:48 +00:00
GLXLOAD(glXSwapIntervalMESA);
GLXLOAD(glXGetSwapIntervalMESA);
2017-11-10 21:13:02 +00:00
if (glXSwapIntervalMESA != 0){
2016-09-04 20:41:48 +00:00
glXSwapIntervalMESA(1);
2017-11-10 21:13:02 +00:00
if (glXGetSwapIntervalMESA != 0){
2016-09-04 20:41:48 +00:00
linuxvars.vsync = glXGetSwapIntervalMESA();
2017-07-03 15:59:39 +00:00
LOGF("VSync enabled? %s (MESA)\n", linuxvars.vsync ? "Yes" : "No");
2017-11-10 21:13:02 +00:00
}
else{
2016-09-04 20:41:48 +00:00
// NOTE(inso): assume it worked?
2017-11-10 21:13:02 +00:00
linuxvars.vsync = true;
2017-07-03 15:59:39 +00:00
LOG("VSync enabled? possibly (MESA)\n");
2016-09-04 20:41:48 +00:00
}
}
2017-07-03 15:59:39 +00:00
}
else if (Direct && strstr(glxExts, "GLX_SGI_swap_control ")){
2016-09-04 20:41:48 +00:00
GLXLOAD(glXSwapIntervalSGI);
2017-07-03 15:59:39 +00:00
if (glXSwapIntervalSGI){
2016-09-04 20:41:48 +00:00
glXSwapIntervalSGI(1);
2017-07-18 21:19:28 +00:00
// NOTE(inso): The SGI one doesn't seem to have a way to confirm we got it...
2017-11-10 21:13:02 +00:00
linuxvars.vsync = true;
2017-07-03 15:59:39 +00:00
LOG("VSync enabled? hopefully (SGI)\n");
2016-09-04 20:41:48 +00:00
}
2017-07-03 15:59:39 +00:00
}
else{
LOG("VSync enabled? nope, no suitable extension\n");
2016-09-04 20:41:48 +00:00
}
2016-09-04 20:41:48 +00:00
return(ctx);
}
internal b32
GLXCanUseFBConfig(Display *XDisplay)
{
b32 Result = false;
2016-09-04 20:41:48 +00:00
int GLXMajor, GLXMinor;
2016-09-04 20:41:48 +00:00
char *XVendor = ServerVendor(XDisplay);
2017-07-03 15:59:39 +00:00
LOGF("XWindows vendor: %s\n", XVendor);
if (glXQueryVersion(XDisplay, &GLXMajor, &GLXMinor))
2016-09-04 20:41:48 +00:00
{
2017-07-03 15:59:39 +00:00
LOGF("GLX version %d.%d\n", GLXMajor, GLXMinor);
if (((GLXMajor == 1 ) && (GLXMinor >= 3)) || (GLXMajor > 1))
2016-09-04 20:41:48 +00:00
{
Result = true;
}
}
2016-09-04 20:41:48 +00:00
return(Result);
}
struct glx_config_result{
2016-09-04 20:41:48 +00:00
b32 Found;
GLXFBConfig BestConfig;
XVisualInfo BestInfo;
};
2016-09-04 20:41:48 +00:00
internal glx_config_result
ChooseGLXConfig(Display *XDisplay, int XScreenIndex)
{
glx_config_result Result = {0};
2017-11-10 21:55:43 +00:00
int DesiredAttributes[] = {
GLX_X_RENDERABLE , True,
GLX_DRAWABLE_TYPE , GLX_WINDOW_BIT,
GLX_RENDER_TYPE , GLX_RGBA_BIT,
GLX_X_VISUAL_TYPE , GLX_TRUE_COLOR,
GLX_RED_SIZE , 8,
GLX_GREEN_SIZE , 8,
GLX_BLUE_SIZE , 8,
GLX_ALPHA_SIZE , 8,
GLX_DEPTH_SIZE , 24,
GLX_STENCIL_SIZE , 8,
GLX_DOUBLEBUFFER , True,
None
};
int ConfigCount = 0;
2017-11-10 21:55:43 +00:00
GLXFBConfig *Configs = glXChooseFBConfig(XDisplay, XScreenIndex, DesiredAttributes, &ConfigCount);
if (Configs != 0 && ConfigCount > 0){
2016-09-04 20:41:48 +00:00
XVisualInfo* VI = glXGetVisualFromFBConfig(XDisplay, Configs[0]);
2017-11-10 21:55:43 +00:00
if (VI != 0){
2016-09-04 20:41:48 +00:00
Result.Found = true;
Result.BestConfig = Configs[0];
Result.BestInfo = *VI;
2016-09-04 20:41:48 +00:00
int id = 0;
glXGetFBConfigAttrib(XDisplay, Result.BestConfig, GLX_FBCONFIG_ID, &id);
2017-07-03 15:59:39 +00:00
LOGF("Using FBConfig: %d (0x%x)\n", id, id);
2016-09-04 20:41:48 +00:00
XFree(VI);
}
2016-09-04 20:41:48 +00:00
XFree(Configs);
}
2016-09-04 20:41:48 +00:00
return(Result);
}
//
// X11 input / events init
//
struct Init_Input_Result{
XIM input_method;
XIMStyle best_style;
XIC xic;
};
static Init_Input_Result null_init_input_result = {0};
2016-09-04 20:41:48 +00:00
internal Init_Input_Result
LinuxInputInit(Display *dpy, Window XWindow){
2016-09-04 20:41:48 +00:00
Init_Input_Result result = {};
XIMStyles *styles = 0;
XIMStyle style;
unsigned long xim_event_mask = 0;
2017-05-29 17:00:18 +00:00
setlocale(LC_ALL, "");
XSetLocaleModifiers("");
2017-05-29 15:51:26 +00:00
b32 locale_supported = XSupportsLocale();
2017-07-03 15:59:39 +00:00
LOGF("Supported locale?: %s.\n", locale_supported ? "Yes" : "No");
2017-05-29 15:51:26 +00:00
if (!locale_supported){
2017-07-03 15:59:39 +00:00
LOG("Reverting to 'C' ... ");
2017-05-29 17:00:18 +00:00
setlocale(LC_ALL, "C");
2017-05-29 15:51:26 +00:00
locale_supported = XSupportsLocale();
2017-07-03 15:59:39 +00:00
LOGF("C is supported? %s.\n", locale_supported ? "Yes" : "No");
2017-05-29 15:51:26 +00:00
}
2016-09-04 20:41:48 +00:00
result.input_method = XOpenIM(dpy, 0, 0, 0);
if (!result.input_method){
// NOTE(inso): Try falling back to the internal XIM implementation that
// should in theory always exist.
2016-09-04 20:41:48 +00:00
XSetLocaleModifiers("@im=none");
result.input_method = XOpenIM(dpy, 0, 0, 0);
}
2016-09-04 20:41:48 +00:00
if (result.input_method){
if (!XGetIMValues(result.input_method, XNQueryInputStyle, &styles, NULL) && styles){
for (i32 i = 0; i < styles->count_styles; ++i){
2016-09-04 20:41:48 +00:00
style = styles->supported_styles[i];
if (style == (XIMPreeditNothing | XIMStatusNothing)){
result.best_style = style;
break;
}
}
}
2016-09-04 20:41:48 +00:00
if (result.best_style){
XFree(styles);
result.xic = XCreateIC(result.input_method, XNInputStyle, result.best_style, XNClientWindow, XWindow, XNFocusWindow, XWindow, NULL);
2016-09-04 20:41:48 +00:00
if (XGetICValues(result.xic, XNFilterEvents, &xim_event_mask, NULL)){
xim_event_mask = 0;
}
}
else{
result = null_init_input_result;
2017-07-03 15:59:39 +00:00
LOG("Could not get minimum required input style.\n");
2017-05-29 15:51:26 +00:00
exit(1);
2016-09-04 20:41:48 +00:00
}
}
else{
result = null_init_input_result;
2017-07-03 15:59:39 +00:00
LOG("Could not open X Input Method.\n");
2017-05-29 15:51:26 +00:00
exit(1);
}
u32 flags = ExposureMask | KeyPressMask | KeyReleaseMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask | PointerMotionMask | FocusChangeMask | StructureNotifyMask | MappingNotify | ExposureMask | VisibilityChangeMask | xim_event_mask;
XSelectInput(linuxvars.XDisplay, linuxvars.XWindow, flags);
2016-09-04 20:41:48 +00:00
return(result);
}
//
// Keyboard handling funcs
//
2018-06-09 08:11:47 +00:00
#if 0
2016-09-04 20:41:48 +00:00
internal void
LinuxKeycodeInit(Display* dpy){
2016-09-04 20:41:48 +00:00
// NOTE(inso): This looks a bit dumb, but it's the best way I can think of to do it, since:
// KeySyms are the type representing "virtual" keys, like XK_BackSpace, but they are 32-bit ints.
// KeyCodes are guaranteed to fit in 1 byte (and therefore the keycode_lookup_table) but
// have dynamic numbers assigned by the XServer.
// There is XKeysymToKeycode, but it only returns 1 KeyCode for a KeySym. I have my capslock
// rebound to esc, so there are two KeyCodes for the XK_Escape KeyCode but XKeysymToKeycode only
// gets one of them, hence the need for this crazy lookup which works correctly with rebound keys.
2016-09-04 20:41:48 +00:00
memset(keycode_lookup_table, 0, sizeof(keycode_lookup_table));
2016-09-04 20:41:48 +00:00
struct SymMapping {
KeySym sym;
u16 code;
};
SymMapping sym_table[] = {
2016-09-04 20:41:48 +00:00
{ XK_BackSpace, key_back },
{ XK_Delete, key_del },
{ XK_Up, key_up },
{ XK_Down, key_down },
{ XK_Left, key_left },
{ XK_Right, key_right },
{ XK_Insert, key_insert },
{ XK_Home, key_home },
{ XK_End, key_end },
{ XK_Page_Up, key_page_up },
{ XK_Page_Down, key_page_down },
{ XK_Escape, key_esc },
{ XK_F1, key_f1 },
{ XK_F2, key_f2 },
{ XK_F3, key_f3 },
{ XK_F4, key_f4 },
{ XK_F5, key_f5 },
{ XK_F6, key_f6 },
{ XK_F7, key_f7 },
{ XK_F8, key_f8 },
{ XK_F9, key_f9 },
{ XK_F10, key_f10 },
{ XK_F11, key_f11 },
{ XK_F12, key_f12 },
{ XK_F13, key_f13 },
{ XK_F14, key_f14 },
{ XK_F15, key_f15 },
{ XK_F16, key_f16 },
};
2016-09-04 20:41:48 +00:00
const int table_size = sizeof(sym_table) / sizeof(struct SymMapping);
2016-09-04 20:41:48 +00:00
int key_min, key_max, syms_per_code;
XDisplayKeycodes(dpy, &key_min, &key_max);
2016-09-04 20:41:48 +00:00
int key_count = (key_max - key_min) + 1;
2017-11-07 21:35:26 +00:00
KeySym* syms = XGetKeyboardMapping(dpy, key_min, key_count, &syms_per_code);
2018-06-09 08:11:47 +00:00
if (syms == 0){
return;
}
2016-09-04 20:41:48 +00:00
int key = key_min;
for(int i = 0; i < key_count * syms_per_code; ++i){
for(int j = 0; j < table_size; ++j){
2017-07-03 15:59:39 +00:00
if (sym_table[j].sym == syms[i]){
2016-09-04 20:41:48 +00:00
keycode_lookup_table[key + (i/syms_per_code)] = sym_table[j].code;
break;
}
}
}
2016-09-04 20:41:48 +00:00
XFree(syms);
}
2018-06-09 08:11:47 +00:00
#endif
2016-09-04 20:41:48 +00:00
internal void
LinuxPushKey(Key_Code code, Key_Code chr, Key_Code chr_nocaps, b8 *mods)
2016-09-04 20:41:48 +00:00
{
2017-01-30 16:04:47 +00:00
i32 *count = &linuxvars.input.keys.count;
Key_Event_Data *data = linuxvars.input.keys.keys;
2017-07-03 15:59:39 +00:00
if (*count < KEY_INPUT_BUFFER_SIZE){
data[*count].keycode = code;
data[*count].character = chr;
data[*count].character_no_caps_lock = chr_nocaps;
memcpy(data[*count].modifiers, mods, sizeof(*mods)*MDFR_INDEX_COUNT);
++(*count);
}
2016-09-04 20:41:48 +00:00
}
//
// Misc utility funcs
//
internal void
LinuxStringDup(String* str, void* data, size_t size){
2017-06-30 19:08:11 +00:00
if (str->memory_size < size){
if (str->str){
system_memory_free(str->str, str->memory_size);
2016-09-04 20:41:48 +00:00
}
str->memory_size = size;
2017-06-30 19:08:11 +00:00
str->str = (char*)system_memory_allocate(size);
2016-09-04 20:41:48 +00:00
//TODO(inso): handle alloc failure case
}
2016-09-04 20:41:48 +00:00
str->size = size;
memcpy(str->str, data, size);
}
//
// X11 utility funcs
//
internal void
LinuxX11ConnectionWatch(Display* dpy, XPointer cdata, int fd, Bool opening, XPointer* wdata){
2016-09-04 20:41:48 +00:00
struct epoll_event e = {};
e.events = EPOLLIN | EPOLLET;
e.data.u64 = LINUX_4ED_EVENT_X11_INTERNAL | fd;
2016-09-04 20:41:48 +00:00
int op = opening ? EPOLL_CTL_ADD : EPOLL_CTL_DEL;
epoll_ctl(linuxvars.epoll, op, fd, &e);
2016-09-04 20:41:48 +00:00
}
internal int
LinuxGetXSettingsDPI(Display* dpy, int screen)
{
struct XSettingHeader {
u8 type;
u8 pad0;
u16 name_len;
char name[0];
};
2016-09-04 20:41:48 +00:00
struct XSettings {
u8 byte_order;
u8 pad[3];
u32 serial;
u32 num_settings;
};
2016-09-04 20:41:48 +00:00
enum { XSettingsTypeInt, XSettingsTypeString, XSettingsTypeColor };
2016-09-04 20:41:48 +00:00
int dpi = -1;
unsigned char* prop = NULL;
char sel_buffer[64];
struct XSettings* xs;
const char* p;
2016-09-04 20:41:48 +00:00
snprintf(sel_buffer, sizeof(sel_buffer), "_XSETTINGS_S%d", screen);
2016-09-04 20:41:48 +00:00
Atom XSET_SEL = XInternAtom(dpy, sel_buffer, True);
Atom XSET_SET = XInternAtom(dpy, "_XSETTINGS_SETTINGS", True);
2017-07-03 15:59:39 +00:00
if (XSET_SEL == None || XSET_SET == None){
LOG("XSETTINGS unavailable.\n");
return(dpi);
2016-09-04 20:41:48 +00:00
}
2016-09-04 20:41:48 +00:00
Window xset_win = XGetSelectionOwner(dpy, XSET_SEL);
2017-07-03 15:59:39 +00:00
if (xset_win == None){
2016-09-04 20:41:48 +00:00
// TODO(inso): listen for the ClientMessage about it becoming available?
// there's not much point atm if DPI scaling is only done at startup
goto out;
}
2016-09-04 20:41:48 +00:00
{
Atom type;
int fmt;
unsigned long pad, num;
2017-07-03 15:59:39 +00:00
if (XGetWindowProperty(dpy, xset_win, XSET_SET, 0, 1024, False, XSET_SET, &type, &fmt, &num, &pad, &prop) != Success){
LOG("XSETTINGS: GetWindowProperty failed.\n");
2016-09-04 20:41:48 +00:00
goto out;
}
2017-07-03 15:59:39 +00:00
if (fmt != 8){
LOG("XSETTINGS: Wrong format.\n");
2016-09-04 20:41:48 +00:00
goto out;
}
}
2016-09-04 20:41:48 +00:00
xs = (struct XSettings*)prop;
p = (char*)(xs + 1);
2017-07-03 15:59:39 +00:00
if (xs->byte_order != 0){
LOG("FIXME: XSETTINGS not host byte order?\n");
2016-09-04 20:41:48 +00:00
goto out;
}
2017-07-03 15:59:39 +00:00
for (int i = 0; i < xs->num_settings; ++i){
2016-09-04 20:41:48 +00:00
struct XSettingHeader* h = (struct XSettingHeader*)p;
2016-09-04 20:41:48 +00:00
p += sizeof(struct XSettingHeader);
p += h->name_len;
p += ((4 - (h->name_len & 3)) & 3);
p += 4; // serial
2017-07-03 15:59:39 +00:00
switch (h->type){
2016-09-04 20:41:48 +00:00
case XSettingsTypeInt: {
2017-07-03 15:59:39 +00:00
if (strncmp(h->name, "Xft/DPI", h->name_len) == 0){
2016-09-04 20:41:48 +00:00
dpi = *(i32*)p;
2017-07-03 15:59:39 +00:00
if (dpi != -1) dpi /= 1024;
2016-09-04 20:41:48 +00:00
}
p += 4;
} break;
2016-09-04 20:41:48 +00:00
case XSettingsTypeString: {
u32 len = *(u32*)p;
p += 4;
p += len;
p += ((4 - (len & 3)) & 3);
} break;
2016-09-04 20:41:48 +00:00
case XSettingsTypeColor: {
p += 8;
} break;
2016-09-04 20:41:48 +00:00
default: {
2017-07-03 15:59:39 +00:00
LOG("XSETTINGS: Got invalid type...\n");
2016-09-04 20:41:48 +00:00
goto out;
} break;
}
}
out:
2017-07-03 15:59:39 +00:00
if (prop){
2016-09-04 20:41:48 +00:00
XFree(prop);
}
2016-09-04 20:41:48 +00:00
return dpi;
}
//
// X11 window init
//
2017-05-23 15:37:58 +00:00
internal f32
2017-03-19 18:25:12 +00:00
size_change(i32 x, i32 y){
f32 xs = x/96.f;
f32 ys = y/96.f;
f32 s = Min(xs, ys);
2017-05-23 15:37:58 +00:00
return(s);
2017-03-19 18:25:12 +00:00
}
2016-09-04 20:41:48 +00:00
#define BASE_W 800
#define BASE_H 600
2017-05-18 21:28:33 +00:00
internal b32
LinuxX11WindowInit(int argc, char** argv, int* window_width, int* window_height){
2017-07-18 21:19:28 +00:00
if (plat_settings.set_window_size){
*window_width = plat_settings.window_w;
*window_height = plat_settings.window_h;
2016-09-04 20:41:48 +00:00
} else {
2017-05-23 15:37:58 +00:00
f32 schange = size_change(linuxvars.dpi_x, linuxvars.dpi_y);
*window_width = ceil32(BASE_W * schange);
*window_height = ceil32(BASE_H * schange);
2016-09-04 20:41:48 +00:00
}
2017-05-23 15:37:58 +00:00
*window_width = Max(*window_width, 1);
*window_height = Max(*window_height, 1);
2016-09-04 20:41:48 +00:00
if (!GLXCanUseFBConfig(linuxvars.XDisplay)){
2017-07-18 23:41:28 +00:00
system_error_box("Your XServer's GLX version is too old. GLX 1.3+ is required.");
2016-09-04 20:41:48 +00:00
}
2016-09-04 20:41:48 +00:00
glx_config_result Config = ChooseGLXConfig(linuxvars.XDisplay, DefaultScreen(linuxvars.XDisplay));
if (!Config.Found){
2017-07-18 23:41:28 +00:00
system_error_box("Could not get a matching GLX FBConfig. Check your OpenGL drivers are installed correctly.");
2016-09-04 20:41:48 +00:00
}
2016-09-04 20:41:48 +00:00
XSetWindowAttributes swa = {};
swa.backing_store = WhenMapped;
swa.event_mask = StructureNotifyMask;
swa.bit_gravity = NorthWestGravity;
swa.colormap = XCreateColormap(linuxvars.XDisplay, RootWindow(linuxvars.XDisplay, Config.BestInfo.screen), Config.BestInfo.visual, AllocNone);
2017-05-18 21:28:33 +00:00
u32 CWflags = CWBackingStore|CWBitGravity|CWBackPixel|CWBorderPixel|CWColormap|CWEventMask;
linuxvars.XWindow = XCreateWindow(linuxvars.XDisplay, RootWindow(linuxvars.XDisplay, Config.BestInfo.screen), 0, 0, *window_width, *window_height, 0, Config.BestInfo.depth, InputOutput, Config.BestInfo.visual, CWflags, &swa);
2016-09-04 20:41:48 +00:00
if (!linuxvars.XWindow){
2017-07-18 23:41:28 +00:00
system_error_box("XCreateWindow failed. Make sure your display is set up correctly.");
2016-09-04 20:41:48 +00:00
}
2016-09-04 20:41:48 +00:00
//NOTE(inso): Set the window's type to normal
2017-03-29 22:35:13 +00:00
XChangeProperty(linuxvars.XDisplay, linuxvars.XWindow, linuxvars.atom__NET_WM_WINDOW_TYPE, XA_ATOM, 32, PropModeReplace, (unsigned char*)&linuxvars.atom__NET_WM_WINDOW_TYPE_NORMAL, 1);
2016-09-04 20:41:48 +00:00
//NOTE(inso): window managers want the PID as a window property for some reason.
pid_t pid = getpid();
2017-03-29 22:35:13 +00:00
XChangeProperty(linuxvars.XDisplay, linuxvars.XWindow, linuxvars.atom__NET_WM_PID, XA_CARDINAL, 32, PropModeReplace, (unsigned char*)&pid, 1);
2016-09-04 20:41:48 +00:00
//NOTE(inso): set wm properties
XStoreName(linuxvars.XDisplay, linuxvars.XWindow, WINDOW_NAME);
2016-09-04 20:41:48 +00:00
XSizeHints *sz_hints = XAllocSizeHints();
XWMHints *wm_hints = XAllocWMHints();
XClassHint *cl_hints = XAllocClassHint();
2016-09-04 20:41:48 +00:00
sz_hints->flags = PMinSize | PMaxSize | PWinGravity;
2016-09-04 20:41:48 +00:00
sz_hints->min_width = 50;
sz_hints->min_height = 50;
2016-09-04 20:41:48 +00:00
sz_hints->max_width = sz_hints->max_height = (1UL << 16UL);
/* NOTE(inso): fluxbox thinks this is minimum, so don't set it
sz_hints->base_width = BASE_W;
sz_hints->base_height = BASE_H;
*/
2016-09-04 20:41:48 +00:00
sz_hints->win_gravity = NorthWestGravity;
2017-07-18 21:19:28 +00:00
if (plat_settings.set_window_pos){
2016-09-04 20:41:48 +00:00
sz_hints->flags |= USPosition;
2017-07-18 21:19:28 +00:00
sz_hints->x = plat_settings.window_x;
sz_hints->y = plat_settings.window_y;
2016-09-04 20:41:48 +00:00
}
2016-09-04 20:41:48 +00:00
wm_hints->flags |= InputHint | StateHint;
wm_hints->input = True;
wm_hints->initial_state = NormalState;
2016-09-04 20:41:48 +00:00
cl_hints->res_name = "4coder";
cl_hints->res_class = "4coder";
2016-09-04 20:41:48 +00:00
char* win_name_list[] = { WINDOW_NAME };
XTextProperty win_name;
XStringListToTextProperty(win_name_list, 1, &win_name);
2017-03-29 22:35:13 +00:00
XSetWMProperties(linuxvars.XDisplay, linuxvars.XWindow, &win_name, NULL, argv, argc, sz_hints, wm_hints, cl_hints);
2016-09-04 20:41:48 +00:00
XFree(win_name.value);
2016-09-04 20:41:48 +00:00
XFree(sz_hints);
XFree(wm_hints);
XFree(cl_hints);
2017-07-18 23:41:28 +00:00
linux_set_icon(linuxvars.XDisplay, linuxvars.XWindow);
// NOTE(inso): make the window visible
2016-09-04 20:41:48 +00:00
XMapWindow(linuxvars.XDisplay, linuxvars.XWindow);
2016-09-04 20:41:48 +00:00
GLXContext GLContext =
2017-11-10 21:55:43 +00:00
InitializeOpenGLContext(linuxvars.XDisplay, linuxvars.XWindow, &Config.BestConfig);
2016-09-04 20:41:48 +00:00
XRaiseWindow(linuxvars.XDisplay, linuxvars.XWindow);
2017-07-18 21:19:28 +00:00
if (plat_settings.set_window_pos){
XMoveWindow(linuxvars.XDisplay, linuxvars.XWindow, plat_settings.window_x, plat_settings.window_y);
2016-09-04 20:41:48 +00:00
}
2017-07-18 21:19:28 +00:00
if (plat_settings.maximize_window){
linux_maximize_window(true);
2016-09-04 20:41:48 +00:00
}
2017-07-18 21:19:28 +00:00
else if (plat_settings.fullscreen_window){
2017-07-18 22:36:50 +00:00
linux_toggle_fullscreen();
2016-09-04 20:41:48 +00:00
}
2016-09-04 20:41:48 +00:00
XSync(linuxvars.XDisplay, False);
2016-09-04 20:41:48 +00:00
XWindowAttributes WinAttribs;
2017-07-18 22:36:50 +00:00
if (XGetWindowAttributes(linuxvars.XDisplay, linuxvars.XWindow, &WinAttribs)){
2017-05-18 21:28:33 +00:00
*window_width = WinAttribs.width;
*window_height = WinAttribs.height;
2016-09-04 20:41:48 +00:00
}
2016-09-04 20:41:48 +00:00
Atom wm_protos[] = {
linuxvars.atom_WM_DELETE_WINDOW,
linuxvars.atom__NET_WM_PING
};
2016-09-04 20:41:48 +00:00
XSetWMProtocols(linuxvars.XDisplay, linuxvars.XWindow, wm_protos, 2);
return(true);
2016-09-04 20:41:48 +00:00
}
internal void
LinuxHandleX11Events(void)
{
2018-06-09 08:11:47 +00:00
static XEvent prev_event = {};
2017-11-11 01:21:50 +00:00
b32 should_step = false;
2017-07-18 22:36:50 +00:00
while (XPending(linuxvars.XDisplay))
2016-09-04 20:41:48 +00:00
{
2018-06-09 08:11:47 +00:00
XEvent event;
XNextEvent(linuxvars.XDisplay, &event);
2018-06-09 08:11:47 +00:00
if (XFilterEvent(&event, None) == True){
2016-09-04 20:41:48 +00:00
continue;
}
2018-06-09 08:11:47 +00:00
switch (event.type){
2016-09-04 20:41:48 +00:00
case KeyPress: {
2017-11-11 01:21:50 +00:00
should_step = true;
2018-06-09 08:11:47 +00:00
b32 is_hold = (prev_event.type == KeyRelease &&
prev_event.xkey.time == event.xkey.time &&
prev_event.xkey.keycode == event.xkey.keycode);
2016-09-04 20:41:48 +00:00
b8 mods[MDFR_INDEX_COUNT] = {};
mods[MDFR_HOLD_INDEX] = is_hold;
2018-06-09 08:11:47 +00:00
if (event.xkey.state & ShiftMask) mods[MDFR_SHIFT_INDEX] = 1;
if (event.xkey.state & ControlMask) mods[MDFR_CONTROL_INDEX] = 1;
if (event.xkey.state & LockMask) mods[MDFR_CAPS_INDEX] = 1;
if (event.xkey.state & Mod1Mask) mods[MDFR_ALT_INDEX] = 1;
2018-06-09 08:11:47 +00:00
event.xkey.state &= ~(ControlMask);
2016-09-04 20:41:48 +00:00
Status status;
KeySym keysym = NoSymbol;
u8 buff[32] = {};
2018-06-09 08:11:47 +00:00
Xutf8LookupString(linuxvars.input_context, &event.xkey, (char*)buff, sizeof(buff) - 1, &keysym, &status);
2017-07-03 15:59:39 +00:00
if (status == XBufferOverflow){
2016-09-04 20:41:48 +00:00
//TODO(inso): handle properly
Xutf8ResetIC(linuxvars.input_context);
XSetICFocus(linuxvars.input_context);
2017-07-03 15:59:39 +00:00
LOG("FIXME: XBufferOverflow from LookupString.\n");
2016-09-04 20:41:48 +00:00
}
2018-06-09 08:11:47 +00:00
// don't push modifiers
if (keysym >= XK_Shift_L && keysym <= XK_Hyper_R){
break;
}
u32 key = utf8_to_u32_unchecked(buff);
u32 key_no_caps = key;
2018-06-09 08:11:47 +00:00
if (mods[MDFR_CAPS_INDEX] && status == XLookupBoth && event.xkey.keycode){
u8 buff_no_caps[32] = {0};
2018-06-09 08:11:47 +00:00
event.xkey.state &= ~(LockMask);
2018-06-09 08:11:47 +00:00
Xutf8LookupString(linuxvars.input_context, &event.xkey, (char*)buff_no_caps, sizeof(buff_no_caps) - 1, NULL, &status);
if (status == XBufferOverflow){
//TODO(inso): handle properly
Xutf8ResetIC(linuxvars.input_context);
XSetICFocus(linuxvars.input_context);
LOG("FIXME: XBufferOverflow from LookupString.\n");
}
2017-07-03 15:59:39 +00:00
if (*buff_no_caps){
key_no_caps = utf8_to_u32_unchecked(buff_no_caps);
2016-09-04 20:41:48 +00:00
}
}
2017-07-03 15:59:39 +00:00
if (key == '\r') key = '\n';
if (key_no_caps == '\r') key_no_caps = '\n';
2017-07-03 15:59:39 +00:00
if (keysym == XK_ISO_Left_Tab){
2016-09-04 20:41:48 +00:00
key = key_no_caps = '\t';
mods[MDFR_SHIFT_INDEX] = 1;
}
2018-06-09 08:11:47 +00:00
//Key_Code special_key = keycode_lookup_table[keysym];
Key_Code special_key = 0;
switch (keysym){
case XK_BackSpace:
{
special_key = key_back;
}break;
case XK_Delete:
{
special_key = key_del;
}break;
case XK_Up:
{
special_key = key_up;
}break;
case XK_Down:
{
special_key = key_down;
}break;
case XK_Left:
{
special_key = key_left;
}break;
case XK_Right:
{
special_key = key_right;
}break;
case XK_Insert:
{
special_key = key_insert;
}break;
case XK_Home:
{
special_key = key_home;
}break;
case XK_End:
{
special_key = key_end;
}break;
case XK_Page_Up:
{
special_key = key_page_up;
}break;
case XK_Page_Down:
{
special_key = key_page_down;
}break;
case XK_Escape:
{
special_key = key_esc;
}break;
case XK_F1:
{
special_key = key_f1;
}break;
case XK_F2:
{
special_key = key_f2;
}break;
case XK_F3:
{
special_key = key_f3;
}break;
case XK_F4:
{
special_key = key_f4;
}break;
case XK_F5:
{
special_key = key_f5;
}break;
case XK_F6:
{
special_key = key_f6;
}break;
case XK_F7:
{
special_key = key_f7;
}break;
case XK_F8:
{
special_key = key_f8;
}break;
case XK_F9:
{
special_key = key_f9;
}break;
case XK_F10:
{
special_key = key_f10;
}break;
case XK_F11:
{
special_key = key_f11;
}break;
case XK_F12:
{
special_key = key_f12;
}break;
case XK_F13:
{
special_key = key_f13;
}break;
case XK_F14:
{
special_key = key_f14;
}break;
case XK_F15:
{
special_key = key_f15;
}break;
case XK_F16:
{
special_key = key_f16;
}break;
}
2017-07-03 15:59:39 +00:00
if (special_key){
LinuxPushKey(special_key, 0, 0, mods);
} else if (key != 0){
LinuxPushKey(key, key, key_no_caps, mods);
2016-09-04 20:41:48 +00:00
} else {
LinuxPushKey(0, 0, 0, mods);
2016-09-04 20:41:48 +00:00
}
}break;
2016-09-04 20:41:48 +00:00
case KeyRelease: {
2017-11-11 01:21:50 +00:00
should_step = true;
2016-09-04 20:41:48 +00:00
}break;
2016-09-04 20:41:48 +00:00
case MotionNotify: {
2017-11-11 01:21:50 +00:00
should_step = true;
2018-06-09 08:11:47 +00:00
linuxvars.input.mouse.x = event.xmotion.x;
linuxvars.input.mouse.y = event.xmotion.y;
2016-09-04 20:41:48 +00:00
}break;
2016-09-04 20:41:48 +00:00
case ButtonPress: {
2017-11-11 01:21:50 +00:00
should_step = true;
2018-06-09 08:11:47 +00:00
switch(event.xbutton.button){
2016-09-04 20:41:48 +00:00
case Button1: {
linuxvars.input.mouse.press_l = 1;
linuxvars.input.mouse.l = 1;
} break;
case Button3: {
linuxvars.input.mouse.press_r = 1;
linuxvars.input.mouse.r = 1;
} break;
2016-09-04 20:41:48 +00:00
//NOTE(inso): scroll up
case Button4: {
2017-11-11 01:21:50 +00:00
linuxvars.input.mouse.wheel = -100;
2016-09-04 20:41:48 +00:00
}break;
2016-09-04 20:41:48 +00:00
//NOTE(inso): scroll down
case Button5: {
2017-11-11 01:21:50 +00:00
linuxvars.input.mouse.wheel = 100;
2016-09-04 20:41:48 +00:00
}break;
}
}break;
2016-09-04 20:41:48 +00:00
case ButtonRelease: {
2017-11-11 01:21:50 +00:00
should_step = true;
2018-06-09 08:11:47 +00:00
switch(event.xbutton.button){
2016-09-04 20:41:48 +00:00
case Button1: {
linuxvars.input.mouse.release_l = 1;
linuxvars.input.mouse.l = 0;
} break;
case Button3: {
linuxvars.input.mouse.release_r = 1;
linuxvars.input.mouse.r = 0;
} break;
}
}break;
2016-09-04 20:41:48 +00:00
case EnterNotify: {
2017-11-11 01:21:50 +00:00
should_step = true;
2016-09-04 20:41:48 +00:00
linuxvars.input.mouse.out_of_window = 0;
}break;
2016-09-04 20:41:48 +00:00
case LeaveNotify: {
2017-11-11 01:21:50 +00:00
should_step = true;
2016-09-04 20:41:48 +00:00
linuxvars.input.mouse.out_of_window = 1;
}break;
2016-09-04 20:41:48 +00:00
case FocusIn:
case FocusOut: {
2017-11-11 01:21:50 +00:00
should_step = true;
2016-09-04 20:41:48 +00:00
linuxvars.input.mouse.l = 0;
linuxvars.input.mouse.r = 0;
}break;
2016-09-04 20:41:48 +00:00
case ConfigureNotify: {
2017-11-11 01:21:50 +00:00
should_step = true;
2018-06-09 08:11:47 +00:00
i32 w = event.xconfigure.width;
i32 h = event.xconfigure.height;
2017-07-18 23:17:40 +00:00
if (w != target.width || h != target.height){
LinuxResizeTarget(w, h);
2016-09-04 20:41:48 +00:00
}
}break;
2016-09-04 20:41:48 +00:00
case MappingNotify: {
2018-06-09 08:11:47 +00:00
if (event.xmapping.request == MappingModifier || event.xmapping.request == MappingKeyboard){
XRefreshKeyboardMapping(&event.xmapping);
//LinuxKeycodeInit(linuxvars.XDisplay);
2016-09-04 20:41:48 +00:00
}
}break;
2016-09-04 20:41:48 +00:00
case ClientMessage: {
2018-06-09 08:11:47 +00:00
if ((Atom)event.xclient.data.l[0] == linuxvars.atom_WM_DELETE_WINDOW) {
2017-11-11 01:21:50 +00:00
should_step = true;
2016-09-04 20:41:48 +00:00
linuxvars.keep_running = 0;
}
2018-06-09 08:11:47 +00:00
else if ((Atom)event.xclient.data.l[0] == linuxvars.atom__NET_WM_PING) {
event.xclient.window = DefaultRootWindow(linuxvars.XDisplay);
2016-09-04 20:41:48 +00:00
XSendEvent(
linuxvars.XDisplay,
2018-06-09 08:11:47 +00:00
event.xclient.window,
2016-09-04 20:41:48 +00:00
False,
SubstructureRedirectMask | SubstructureNotifyMask,
2018-06-09 08:11:47 +00:00
&event);
2016-09-04 20:41:48 +00:00
}
}break;
// NOTE(inso): Someone wants us to give them the clipboard data.
2016-09-04 20:41:48 +00:00
case SelectionRequest: {
2018-06-09 08:11:47 +00:00
XSelectionRequestEvent request = event.xselectionrequest;
2016-09-04 20:41:48 +00:00
XSelectionEvent response = {};
response.type = SelectionNotify;
response.requestor = request.requestor;
response.selection = request.selection;
response.target = request.target;
response.time = request.time;
response.property = None;
2017-09-10 18:40:11 +00:00
if (linuxvars.clipboard_outgoing.size &&
2016-09-04 20:41:48 +00:00
request.selection == linuxvars.atom_CLIPBOARD &&
request.property != None &&
request.display &&
2017-09-10 18:40:11 +00:00
request.requestor){
2016-09-04 20:41:48 +00:00
Atom atoms[] = {
XA_STRING,
linuxvars.atom_UTF8_STRING
};
2017-07-03 15:59:39 +00:00
if (request.target == linuxvars.atom_TARGETS){
2016-09-04 20:41:48 +00:00
XChangeProperty(
request.display,
request.requestor,
request.property,
XA_ATOM,
32,
PropModeReplace,
(u8*)atoms,
2017-09-10 18:40:11 +00:00
ArrayCount(atoms));
2016-09-04 20:41:48 +00:00
response.property = request.property;
2016-09-04 20:41:48 +00:00
} else {
b32 found = false;
for(int i = 0; i < ArrayCount(atoms); ++i){
2017-07-03 15:59:39 +00:00
if (request.target == atoms[i]){
2016-09-04 20:41:48 +00:00
found = true;
break;
}
}
2017-07-03 15:59:39 +00:00
if (found){
2016-09-04 20:41:48 +00:00
XChangeProperty(
request.display,
request.requestor,
request.property,
request.target,
8,
PropModeReplace,
(u8*)linuxvars.clipboard_outgoing.str,
linuxvars.clipboard_outgoing.size
);
2016-09-04 20:41:48 +00:00
response.property = request.property;
}
}
}
2016-09-04 20:41:48 +00:00
XSendEvent(request.display, request.requestor, True, 0, (XEvent*)&response);
2016-09-04 20:41:48 +00:00
}break;
// NOTE(inso): Another program is now the clipboard owner.
2016-09-04 20:41:48 +00:00
case SelectionClear: {
2018-06-09 08:11:47 +00:00
if (event.xselectionclear.selection == linuxvars.atom_CLIPBOARD){
2016-09-04 20:41:48 +00:00
linuxvars.clipboard_outgoing.size = 0;
}
}break;
// NOTE(inso): A program is giving us the clipboard data we asked for.
2016-09-04 20:41:48 +00:00
case SelectionNotify: {
2018-06-09 08:11:47 +00:00
XSelectionEvent* e = (XSelectionEvent*)&event;
2017-07-03 15:59:39 +00:00
if (e->selection == linuxvars.atom_CLIPBOARD && e->target == linuxvars.atom_UTF8_STRING && e->property != None){
2016-09-04 20:41:48 +00:00
Atom type;
int fmt;
2017-11-11 01:21:50 +00:00
unsigned long nitems;
unsigned long bytes_left;
2016-09-04 20:41:48 +00:00
u8 *data;
2017-11-27 17:39:26 +00:00
#define PASTE_MAX_WORDS 0x20000000L
int result = XGetWindowProperty(linuxvars.XDisplay, linuxvars.XWindow, linuxvars.atom_CLIPBOARD, 0L, PASTE_MAX_WORDS, False, linuxvars.atom_UTF8_STRING, &type, &fmt, &nitems, &bytes_left, &data);
2017-07-03 15:59:39 +00:00
if (result == Success && fmt == 8){
2016-09-04 20:41:48 +00:00
LinuxStringDup(&linuxvars.clipboard_contents, data, nitems);
2017-11-11 01:21:50 +00:00
should_step = true;
linuxvars.new_clipboard = true;
2016-09-04 20:41:48 +00:00
XFree(data);
XDeleteProperty(linuxvars.XDisplay, linuxvars.XWindow, linuxvars.atom_CLIPBOARD);
}
}
}break;
2016-09-04 20:41:48 +00:00
case Expose:
case VisibilityNotify: {
2017-11-11 01:21:50 +00:00
should_step = true;
2016-09-04 20:41:48 +00:00
}break;
2016-09-04 20:41:48 +00:00
default: {
2018-06-09 08:11:47 +00:00
if (event.type == linuxvars.xfixes_selection_event){
XFixesSelectionNotifyEvent* sne = (XFixesSelectionNotifyEvent*)&event;
2017-07-03 15:59:39 +00:00
if (sne->subtype == XFixesSelectionNotify && sne->owner != linuxvars.XWindow){
XConvertSelection(linuxvars.XDisplay, linuxvars.atom_CLIPBOARD, linuxvars.atom_UTF8_STRING, linuxvars.atom_CLIPBOARD, linuxvars.XWindow, CurrentTime);
2016-09-04 20:41:48 +00:00
}
}
}break;
}
2018-06-09 08:11:47 +00:00
prev_event = event;
2016-09-04 20:41:48 +00:00
}
2017-07-03 15:59:39 +00:00
if (should_step){
system_schedule_step();
2016-09-04 20:41:48 +00:00
}
}
#include "4ed_link_system_functions.cpp"
2017-07-18 23:17:40 +00:00
#include "4ed_shared_init_logic.cpp"
2016-09-04 20:41:48 +00:00
int
main(int argc, char **argv){
//
// System Linkage
//
link_system_code();
//
// Memory init
//
memset(&linuxvars, 0, sizeof(linuxvars));
memset(&target, 0, sizeof(target));
memset(&memory_vars, 0, sizeof(memory_vars));
memset(&plat_settings, 0, sizeof(plat_settings));
memset(&libraries, 0, sizeof(libraries));
memset(&app, 0, sizeof(app));
memset(&custom_api, 0, sizeof(custom_api));
memory_init();
//
// HACK(allen):
// Previously zipped stuff is here, it should be zipped in the new pattern now.
//
init_shared_vars();
2016-09-04 20:41:48 +00:00
//
// Load Core Code
2016-09-04 20:41:48 +00:00
//
load_app_code();
2016-09-04 20:41:48 +00:00
//
// Read command line
//
read_command_line(argc, argv);
//
// Load Custom Code
//
#if defined(FRED_SUPER)
load_custom_code();
#else
custom_api.get_bindings = get_bindings;
#endif
//
// Threads
//
work_system_init();
2016-09-04 20:41:48 +00:00
//
// Coroutines
2016-09-04 20:41:48 +00:00
//
2017-07-19 20:10:05 +00:00
coroutines_init();
2016-09-04 20:41:48 +00:00
//
// X11 init
//
2016-09-04 20:41:48 +00:00
linuxvars.XDisplay = XOpenDisplay(0);
2017-07-03 15:59:39 +00:00
if (!linuxvars.XDisplay){
2016-09-04 20:41:48 +00:00
// NOTE(inso): probably not worth trying the popup in this case...
2017-07-03 15:59:39 +00:00
LOG("Can't open display!\n");
exit(1);
2016-09-04 20:41:48 +00:00
}
2016-09-04 20:41:48 +00:00
#define LOAD_ATOM(x) linuxvars.atom_##x = XInternAtom(linuxvars.XDisplay, #x, False);
2016-09-04 20:41:48 +00:00
LOAD_ATOM(TARGETS);
LOAD_ATOM(CLIPBOARD);
LOAD_ATOM(UTF8_STRING);
LOAD_ATOM(_NET_WM_STATE);
LOAD_ATOM(_NET_WM_STATE_MAXIMIZED_HORZ);
LOAD_ATOM(_NET_WM_STATE_MAXIMIZED_VERT);
LOAD_ATOM(_NET_WM_STATE_FULLSCREEN);
LOAD_ATOM(_NET_WM_PING);
LOAD_ATOM(_NET_WM_WINDOW_TYPE);
LOAD_ATOM(_NET_WM_WINDOW_TYPE_NORMAL);
LOAD_ATOM(_NET_WM_PID);
LOAD_ATOM(WM_DELETE_WINDOW);
2016-09-04 20:41:48 +00:00
#undef LOAD_ATOM
2016-09-04 20:41:48 +00:00
linuxvars.dpi_x = linuxvars.dpi_y = LinuxGetXSettingsDPI(linuxvars.XDisplay, DefaultScreen(linuxvars.XDisplay));
2016-09-04 20:41:48 +00:00
// fallback
2017-07-03 15:59:39 +00:00
if (linuxvars.dpi_x == -1){
2016-09-04 20:41:48 +00:00
int scr = DefaultScreen(linuxvars.XDisplay);
2016-09-04 20:41:48 +00:00
int dw = DisplayWidth(linuxvars.XDisplay, scr);
int dh = DisplayHeight(linuxvars.XDisplay, scr);
2016-09-04 20:41:48 +00:00
int dw_mm = DisplayWidthMM(linuxvars.XDisplay, scr);
int dh_mm = DisplayHeightMM(linuxvars.XDisplay, scr);
2016-09-04 20:41:48 +00:00
linuxvars.dpi_x = dw_mm ? dw / (dw_mm / 25.4) : 96;
linuxvars.dpi_y = dh_mm ? dh / (dh_mm / 25.4) : 96;
2017-07-03 15:59:39 +00:00
LOGF("%dx%d - %dmmx%dmm DPI: %dx%d\n", dw, dh, dw_mm, dh_mm, linuxvars.dpi_x, linuxvars.dpi_y);
}
else{
2017-07-03 15:59:39 +00:00
LOGF("DPI from XSETTINGS: %d\n", linuxvars.dpi_x);
2016-09-04 20:41:48 +00:00
}
2017-05-18 21:28:33 +00:00
int window_width, window_height;
if (!LinuxX11WindowInit(argc, argv, &window_width, &window_height)){
2016-09-04 20:41:48 +00:00
return 1;
}
2016-09-04 20:41:48 +00:00
int xfixes_version_unused, xfixes_err_unused;
b32 xquery_extension_r = XQueryExtension(linuxvars.XDisplay, "XFIXES", &xfixes_version_unused, &linuxvars.xfixes_selection_event, &xfixes_err_unused);
linuxvars.has_xfixes = (xquery_extension_r == True);
if (linuxvars.has_xfixes){
XFixesSelectSelectionInput(linuxvars.XDisplay, linuxvars.XWindow, linuxvars.atom_CLIPBOARD, XFixesSetSelectionOwnerNotifyMask);
}
else{
2017-07-03 15:59:39 +00:00
LOG("Your X server doesn't support XFIXES, mention this fact if you report any clipboard-related issues.\n");
2016-09-04 20:41:48 +00:00
}
Init_Input_Result input_result = LinuxInputInit(linuxvars.XDisplay, linuxvars.XWindow);
2016-09-04 20:41:48 +00:00
linuxvars.input_method = input_result.input_method;
linuxvars.input_style = input_result.best_style;
linuxvars.input_context = input_result.xic;
2018-06-09 08:11:47 +00:00
//LinuxKeycodeInit(linuxvars.XDisplay);
2016-09-04 20:41:48 +00:00
Cursor xcursors[APP_MOUSE_CURSOR_COUNT] = {
None,
2016-09-10 00:10:43 +00:00
None,
2016-09-04 20:41:48 +00:00
XCreateFontCursor(linuxvars.XDisplay, XC_xterm),
XCreateFontCursor(linuxvars.XDisplay, XC_sb_h_double_arrow),
XCreateFontCursor(linuxvars.XDisplay, XC_sb_v_double_arrow)
};
2016-09-04 20:41:48 +00:00
{
char data = 0;
XColor c = {};
Pixmap p = XCreateBitmapFromData(linuxvars.XDisplay, linuxvars.XWindow, &data, 1, 1);
2016-09-04 20:41:48 +00:00
linuxvars.hidden_cursor = XCreatePixmapCursor(linuxvars.XDisplay, p, p, &c, &c, 0, 0);
2016-09-04 20:41:48 +00:00
XFreePixmap(linuxvars.XDisplay, p);
}
2017-03-19 18:25:12 +00:00
//
// Font System Init
//
Partition *scratch = &shared_vars.scratch;
Temp_Memory temp = begin_temp_memory(scratch);
Font_Setup_List font_setup = system_font_get_local_stubs(scratch);
2017-11-17 21:20:25 +00:00
linux_get_loadable_fonts(scratch, &font_setup);
system_font_init(&sysfunc.font, plat_settings.font_size, plat_settings.use_hinting, font_setup);
end_temp_memory(temp);
2017-03-19 18:25:12 +00:00
2016-09-04 20:41:48 +00:00
//
// Epoll init
//
2016-09-04 20:41:48 +00:00
linuxvars.x11_fd = ConnectionNumber(linuxvars.XDisplay);
linuxvars.inotify_fd = inotify_init1(IN_NONBLOCK);
linuxvars.step_event_fd = eventfd(0, EFD_NONBLOCK);
linuxvars.step_timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK);
linuxvars.epoll = epoll_create(16);
2016-09-04 20:41:48 +00:00
{
struct epoll_event e = {};
e.events = EPOLLIN | EPOLLET;
2016-09-04 20:41:48 +00:00
e.data.u64 = LINUX_4ED_EVENT_X11;
epoll_ctl(linuxvars.epoll, EPOLL_CTL_ADD, linuxvars.x11_fd, &e);
2016-09-04 20:41:48 +00:00
e.data.u64 = LINUX_4ED_EVENT_STEP;
epoll_ctl(linuxvars.epoll, EPOLL_CTL_ADD, linuxvars.step_event_fd, &e);
2016-09-04 20:41:48 +00:00
e.data.u64 = LINUX_4ED_EVENT_STEP_TIMER;
epoll_ctl(linuxvars.epoll, EPOLL_CTL_ADD, linuxvars.step_timer_fd, &e);
2016-09-04 20:41:48 +00:00
}
2016-09-04 20:41:48 +00:00
//
// App init
//
2016-09-04 20:41:48 +00:00
XAddConnectionWatch(linuxvars.XDisplay, &LinuxX11ConnectionWatch, NULL);
char cwd[4096];
u32 size = sysfunc.get_current_path(cwd, sizeof(cwd));
if (size == 0 || size >= sizeof(cwd)){
system_error_box("Could not get current directory at launch.");
}
String curdir = make_string(cwd, size);
terminate_with_null(&curdir);
replace_char(&curdir, '\\', '/');
LOG("Initializing application variables\n");
2017-07-19 20:24:04 +00:00
app.init(&sysfunc, &target, &memory_vars, linuxvars.clipboard_contents, curdir, custom_api);
2017-05-18 21:28:33 +00:00
LinuxResizeTarget(window_width, window_height);
2016-09-04 20:41:48 +00:00
//
// Main loop
//
2016-09-04 20:41:48 +00:00
system_acquire_lock(FRAME_LOCK);
system_schedule_step();
linuxvars.keep_running = true;
linuxvars.input.first_step = true;
2016-09-04 20:41:48 +00:00
linuxvars.input.dt = (frame_useconds / 1000000.f);
for (;;){
if (XEventsQueued(linuxvars.XDisplay, QueuedAlready)){
2016-09-04 20:41:48 +00:00
LinuxHandleX11Events();
}
2016-09-04 20:41:48 +00:00
system_release_lock(FRAME_LOCK);
2016-09-04 20:41:48 +00:00
struct epoll_event events[16];
int num_events = epoll_wait(linuxvars.epoll, events, ArrayCount(events), -1);
2016-09-04 20:41:48 +00:00
system_acquire_lock(FRAME_LOCK);
if (num_events == -1){
if (errno != EINTR){
2017-07-03 15:59:39 +00:00
LOG("epoll_wait\n");
2016-09-04 20:41:48 +00:00
}
continue;
}
b32 do_step = false;
for (int i = 0; i < num_events; ++i){
2016-09-04 20:41:48 +00:00
int fd = events[i].data.u64 & UINT32_MAX;
u64 type = events[i].data.u64 & ~fd;
switch (type){
2016-09-04 20:41:48 +00:00
case LINUX_4ED_EVENT_X11: {
LinuxHandleX11Events();
} break;
2016-09-04 20:41:48 +00:00
case LINUX_4ED_EVENT_X11_INTERNAL: {
XProcessInternalConnection(linuxvars.XDisplay, fd);
} break;
2016-09-04 20:41:48 +00:00
case LINUX_4ED_EVENT_STEP: {
u64 ev;
int ret;
do {
ret = read(linuxvars.step_event_fd, &ev, 8);
2017-07-18 22:36:50 +00:00
} while (ret != -1 || errno != EAGAIN);
do_step = true;
2016-09-04 20:41:48 +00:00
} break;
2016-09-04 20:41:48 +00:00
case LINUX_4ED_EVENT_STEP_TIMER: {
u64 count;
int ret;
do {
ret = read(linuxvars.step_timer_fd, &count, 8);
2017-07-18 22:36:50 +00:00
} while (ret != -1 || errno != EAGAIN);
do_step = true;
2016-09-04 20:41:48 +00:00
} break;
case LINUX_4ED_EVENT_CLI: {
system_schedule_step();
2016-09-04 20:41:48 +00:00
} break;
}
}
2017-07-03 15:59:39 +00:00
if (do_step){
2016-09-04 20:41:48 +00:00
linuxvars.last_step = system_now_time();
// NOTE(allen): Frame Clipboard Input
2017-07-03 15:59:39 +00:00
if (linuxvars.input.first_step || !linuxvars.has_xfixes){
2017-07-18 17:44:54 +00:00
XConvertSelection(linuxvars.XDisplay, linuxvars.atom_CLIPBOARD, linuxvars.atom_UTF8_STRING, linuxvars.atom_CLIPBOARD, linuxvars.XWindow, CurrentTime);
2016-09-04 20:41:48 +00:00
}
2017-07-03 15:59:39 +00:00
if (linuxvars.new_clipboard){
2016-09-04 20:41:48 +00:00
linuxvars.input.clipboard = linuxvars.clipboard_contents;
linuxvars.new_clipboard = 0;
} else {
linuxvars.input.clipboard = null_string;
}
2018-03-10 00:34:35 +00:00
Application_Step_Input frame_input = linuxvars.input;
2018-03-03 07:46:44 +00:00
frame_input.trying_to_kill = !linuxvars.keep_running;
2017-11-07 21:35:26 +00:00
// HACK(allen): THIS SHIT IS FUCKED (happens on mac too)
2016-09-10 02:57:45 +00:00
b32 keep_running = linuxvars.keep_running;
2017-11-08 01:15:09 +00:00
// NOTE(allen): Application Core Update
target.buffer.pos = 0;
2018-03-03 07:46:44 +00:00
Application_Step_Result result = {0};
2017-11-12 19:15:11 +00:00
if (app.step != 0){
2018-03-03 07:46:44 +00:00
result = app.step(&sysfunc, &target, &memory_vars, &frame_input);
2017-11-12 19:13:04 +00:00
}
2017-11-12 19:15:11 +00:00
else{
LOG("app.step == 0 -- skipping\n");
}
// NOTE(allen): Finish the Loop
2017-07-03 15:59:39 +00:00
if (result.perform_kill){
2016-09-04 20:41:48 +00:00
break;
}
else if (!keep_running && !linuxvars.keep_running){
linuxvars.keep_running = true;
}
2017-11-30 19:15:40 +00:00
// NOTE(NAME): Switch to New Title
if (result.has_new_title){
XStoreName(linuxvars.XDisplay, linuxvars.XWindow, result.title_string);
}
// NOTE(allen): Switch to New Cursor
if (result.mouse_cursor_type != linuxvars.cursor && !linuxvars.input.mouse.l){
Cursor c = xcursors[result.mouse_cursor_type];
if (!linuxvars.hide_cursor){
XDefineCursor(linuxvars.XDisplay, linuxvars.XWindow, c);
}
linuxvars.cursor = result.mouse_cursor_type;
}
// NOTE(allen): Render
interpret_render_buffer(&target, &shared_vars.pixel_scratch);
glXSwapBuffers(linuxvars.XDisplay, linuxvars.XWindow);
// NOTE(allen): Toggle Full Screen
if (linuxvars.do_toggle){
linux_toggle_fullscreen();
linuxvars.do_toggle = false;
2016-09-04 20:41:48 +00:00
}
// NOTE(allen): Schedule Another Step if Needed
2017-07-03 15:59:39 +00:00
if (result.animating){
system_schedule_step();
2016-09-04 20:41:48 +00:00
}
2016-09-04 20:41:48 +00:00
flush_thread_group(BACKGROUND_THREADS);
// TODO(allen): CROSS REFERENCE WITH WIN32 "TIC898989"
2018-03-10 02:06:55 +00:00
memset(&linuxvars.input.keys, 0, sizeof(linuxvars.input.keys));
2016-09-04 20:41:48 +00:00
linuxvars.input.mouse.press_l = 0;
linuxvars.input.mouse.release_l = 0;
linuxvars.input.mouse.press_r = 0;
linuxvars.input.mouse.release_r = 0;
linuxvars.input.mouse.wheel = 0;
linuxvars.input.first_step = 0;
2016-09-04 20:41:48 +00:00
}
}
2017-07-03 15:59:39 +00:00
if (linuxvars.XDisplay){
if (linuxvars.XWindow){
XDestroyWindow(linuxvars.XDisplay, linuxvars.XWindow);
}
XCloseDisplay(linuxvars.XDisplay);
}
2017-07-01 02:39:24 +00:00
return(0);
2016-09-04 20:41:48 +00:00
}
2017-07-01 02:39:24 +00:00
#include "linux_4ed_file_track.cpp"
2017-03-19 18:25:12 +00:00
2016-09-04 20:41:48 +00:00
// BOTTOM
// vim: expandtab:ts=4:sts=4:sw=4