-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathvid_sdl2.c
1513 lines (1241 loc) · 42.4 KB
/
vid_sdl2.c
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.
This file is part of Quake III Arena source code.
Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.
Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include "quakedef.h"
#include <SDL.h>
#include <SDL_syswm.h>
#ifdef __linux__
#include <X11/extensions/xf86vmode.h>
#endif
#ifdef _WIN32
#include <windows.h>
void Sys_ActiveAppChanged (void);
#endif
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include "in_osx.h"
#else
#include <GL/gl.h>
#endif
#include "ezquake-icon.c"
#include "keys.h"
#include "tr_types.h"
#include "input.h"
#include "rulesets.h"
#include "utils.h"
#include "gl_model.h"
#include "gl_local.h"
#include "textencoding.h"
#define WINDOW_CLASS_NAME "ezQuake"
/* FIXME: This should be in a header file and it probably shouldn't be called TP_
* since there are a lot of triggers that has nothing to do with teamplay.
* Should probably split them
*/
extern void TP_ExecTrigger(const char *);
static void in_raw_callback(cvar_t *var, char *value, qbool *cancel);
static void in_grab_windowed_mouse_callback(cvar_t *var, char *value, qbool *cancel);
static void conres_changed_callback (cvar_t *var, char *string, qbool *cancel);
static void GrabMouse(qbool grab, qbool raw);
static void GfxInfo_f(void);
static void HandleEvents(void);
static void VID_UpdateConRes(void);
void IN_Restart_f(void);
static SDL_Window *sdl_window;
static SDL_GLContext sdl_context;
glconfig_t glConfig;
qbool vid_hwgamma_enabled = false;
static qbool mouse_active = false;
qbool mouseinitialized = false; // unfortunately non static, lame...
int mx, my;
static int old_x = 0, old_y = 0;
extern double cursor_x, cursor_y;
qbool ActiveApp = true;
qbool Minimized = false;
double vid_vsync_lag;
double vid_last_swap_time;
static SDL_DisplayMode *modelist;
static int modelist_count;
#ifdef __linux__
static unsigned short sysramps[768];
#endif
qbool vid_initialized = false;
static int last_working_width;
static int last_working_height;
static int last_working_hz;
static int last_working_display;
static qbool last_working_values = false;
//
// cvars
//
extern cvar_t sys_inactivesleep;
// latched variables that can only change over a restart
cvar_t r_colorbits = {"vid_colorbits", "0", CVAR_LATCH };
cvar_t r_24bit_depth = {"vid_24bit_depth", "1", CVAR_LATCH };
cvar_t r_stereo = {"vid_stereo", "0", CVAR_LATCH };
cvar_t r_fullscreen = {"vid_fullscreen", "1", CVAR_LATCH };
cvar_t r_displayRefresh = {"vid_displayfrequency", "0", CVAR_LATCH | CVAR_AUTO };
cvar_t vid_displayNumber = {"vid_displaynumber", "0", CVAR_LATCH | CVAR_AUTO };
cvar_t vid_usedesktopres = {"vid_usedesktopres", "1", CVAR_LATCH | CVAR_AUTO };
cvar_t vid_win_borderless = {"vid_win_borderless", "0", CVAR_LATCH };
cvar_t vid_width = {"vid_width", "0", CVAR_LATCH | CVAR_AUTO };
cvar_t vid_height = {"vid_height", "0", CVAR_LATCH | CVAR_AUTO };
cvar_t vid_win_width = {"vid_win_width", "640", CVAR_LATCH };
cvar_t vid_win_height = {"vid_win_height", "480", CVAR_LATCH };
cvar_t vid_hwgammacontrol = {"vid_hwgammacontrol", "2", CVAR_LATCH };
// TODO: Move the in_* cvars
cvar_t in_raw = {"in_raw", "1", CVAR_ARCHIVE | CVAR_SILENT, in_raw_callback};
cvar_t in_grab_windowed_mouse = {"in_grab_windowed_mouse","1", CVAR_ARCHIVE | CVAR_SILENT, in_grab_windowed_mouse_callback};
cvar_t in_release_mouse_modes = {"in_release_mouse_modes","2", CVAR_SILENT };
cvar_t vid_vsync_lag_fix = {"vid_vsync_lag_fix", "0" };
cvar_t vid_vsync_lag_tweak = {"vid_vsync_lag_tweak", "1.0" };
cvar_t r_swapInterval = {"vid_vsync", "0", CVAR_SILENT };
cvar_t r_win_save_pos = {"vid_win_save_pos", "1", CVAR_SILENT };
cvar_t r_win_save_size = {"vid_win_save_size", "1", CVAR_SILENT };
cvar_t vid_xpos = {"vid_xpos", "3", CVAR_SILENT };
cvar_t vid_ypos = {"vid_ypos", "39", CVAR_SILENT };
cvar_t vid_win_displayNumber = {"vid_win_displaynumber", "0", CVAR_SILENT };
cvar_t r_conwidth = {"vid_conwidth", "0", CVAR_NO_RESET | CVAR_SILENT | CVAR_AUTO, conres_changed_callback };
cvar_t r_conheight = {"vid_conheight", "0", CVAR_NO_RESET | CVAR_SILENT | CVAR_AUTO, conres_changed_callback };
cvar_t r_conscale = {"vid_conscale", "2.0", CVAR_NO_RESET | CVAR_SILENT, conres_changed_callback };
cvar_t vid_flashonactivity = {"vid_flashonactivity", "1", CVAR_SILENT };
cvar_t r_verbose = {"vid_verbose", "0", CVAR_SILENT };
cvar_t r_showextensions = {"vid_showextensions", "0", CVAR_SILENT };
cvar_t gl_multisamples = {"gl_multisamples", "0", CVAR_LATCH }; // It's here because it needs to be registered before window creation
//
// function declaration
//
// True if we need to release the mouse and let the OS show cursor again
static qbool IN_OSMouseCursorRequired(void)
{
// Explicit check here for key_game... really setting all modes is equivalent to "in_grab_windowed_mouse 0"
qbool in_os_cursor_mode = (key_dest != key_game || cls.demoplayback) && (in_release_mouse_modes.integer & (1 << key_dest));
// Windowed & (not-grabbing mouse | in OS cursor mode)
return (!r_fullscreen.value && (!in_grab_windowed_mouse.value || in_os_cursor_mode));
}
// True if we're in a mode where we need to keep track of mouse movement
qbool IN_MouseTrackingRequired(void)
{
return (key_dest == key_menu || key_dest == key_hudeditor || key_dest == key_demo_controls);
}
// True if we need to display the internal Quake cursor to track the mouse
qbool IN_QuakeMouseCursorRequired(void)
{
return mouse_active && IN_MouseTrackingRequired() && !IN_OSMouseCursorRequired();
}
static void in_raw_callback(cvar_t *var, char *value, qbool *cancel)
{
if (var == &in_raw)
Cvar_SetValue(&in_raw, atoi(value));
IN_Restart_f();
}
static void in_grab_windowed_mouse_callback(cvar_t *val, char *value, qbool *cancel)
{
GrabMouse((atoi(value) > 0 ? true : false), in_raw.integer);
}
static void GrabMouse(qbool grab, qbool raw)
{
if ((grab && mouse_active && raw == in_raw.integer) || (!grab && !mouse_active) || !mouseinitialized || !sdl_window)
return;
if (!r_fullscreen.integer && in_grab_windowed_mouse.integer == 0)
{
if (!mouse_active)
return;
grab = 0;
}
// set initial position
if (!raw && grab) {
SDL_WarpMouseInWindow(sdl_window, glConfig.vidWidth / 2, glConfig.vidHeight / 2);
old_x = glConfig.vidWidth / 2;
old_y = glConfig.vidHeight / 2;
}
SDL_SetWindowGrab(sdl_window, grab ? SDL_TRUE : SDL_FALSE);
SDL_SetRelativeMouseMode((raw && grab) ? SDL_TRUE : SDL_FALSE);
SDL_GetRelativeMouseState(NULL, NULL);
// never show real cursor in fullscreen
if (r_fullscreen.integer) {
SDL_ShowCursor(SDL_DISABLE);
} else {
SDL_ShowCursor(grab ? SDL_DISABLE : SDL_ENABLE);
}
SDL_SetCursor(NULL); /* Force rewrite of it */
mouse_active = grab;
}
void IN_Commands(void)
{
}
void IN_StartupMouse(void)
{
Cvar_Register(&in_raw);
Cvar_Register(&in_grab_windowed_mouse);
Cvar_Register(&in_release_mouse_modes);
mouseinitialized = true;
Com_Printf("%s mouse input initialized\n", in_raw.integer > 0 ? "RAW" : "SDL");
}
void IN_ActivateMouse(void)
{
GrabMouse(true, in_raw.integer);
}
void IN_DeactivateMouse(void)
{
GrabMouse(false, in_raw.integer);
}
void IN_Frame(void)
{
if (!sdl_window)
return;
HandleEvents();
if (!ActiveApp || Minimized || IN_OSMouseCursorRequired()) {
IN_DeactivateMouse();
return;
} else {
IN_ActivateMouse();
}
if (mouse_active && SDL_GetRelativeMouseMode()) {
#ifdef __APPLE__
OSX_Mouse_GetMouseMovement(&mx, &my);
#else
SDL_GetRelativeMouseState(&mx, &my);
#endif
}
}
void Sys_SendKeyEvents(void)
{
IN_Frame();
if (sys_inactivesleep.integer > 0) {
// Yield the CPU a little
if ((ISPAUSED && (!ActiveApp)) || Minimized || block_drawing) {
if (!cls.download) {
SDL_Delay(50);
}
scr_skipupdate = 1; // no point to draw anything
} else if (!ActiveApp) { // Delay a bit less if just not active window
if (!cls.download) {
SDL_Delay(20);
}
}
}
}
void IN_Restart_f(void)
{
qbool old_mouse_active = mouse_active;
IN_Shutdown();
IN_Init();
// if mouse was active before restart, try to re-activate it
if (old_mouse_active) {
IN_ActivateMouse();
}
}
// Converts co-ordinates for the whole desktop to co-ordinates for a specific display
static void VID_RelativePositionFromAbsolute(int* x, int* y, int* display)
{
int displays = SDL_GetNumVideoDisplays();
int i = 0;
for (i = 0; i < displays; ++i)
{
SDL_Rect bounds;
if (SDL_GetDisplayBounds(i, &bounds) == 0)
{
if (*x >= bounds.x && *x < bounds.x + bounds.w && *y >= bounds.y && *y < bounds.y + bounds.h)
{
*x = *x - bounds.x;
*y = *y - bounds.y;
*display = i;
return;
}
}
}
*display = 0;
}
// Converts co-ordinates for a specific display to those for whole desktop
static void VID_AbsolutePositionFromRelative(int* x, int* y, int* display)
{
SDL_Rect bounds;
// Try and get bounds for the specified display - default back to main display if there's an issue
if (SDL_GetDisplayBounds(*display, &bounds))
{
*display = 0;
if (SDL_GetDisplayBounds(*display, &bounds))
{
// Still an issue - reset back to top-left of screen
Com_Printf("Error detecting resolution...\n");
*x = *y = 0;
return;
}
}
// Adjust co-ordinates, making sure some of the window will always be visible
*x = bounds.x + min(*x, bounds.w - 30);
*y = bounds.y + min(*y, bounds.h - 30);
}
static void VID_SetDeviceGammaRampReal(unsigned short *ramps)
{
#ifdef __linux__
SDL_SysWMinfo info;
Display *display;
int screen;
static short once = 1;
static short gamma_works = 0;
SDL_VERSION(&info.version);
screen = SDL_GetWindowDisplayIndex(sdl_window);
if (screen < 0) {
Com_Printf("error: couldn't get screen number to set gamma\n");
return;
}
if (SDL_GetWindowWMInfo(sdl_window, &info) != SDL_TRUE) {
Com_Printf("error: can not get display pointer, gamma won't work: %s\n", SDL_GetError());
return;
}
if (info.subsystem != SDL_SYSWM_X11) {
Com_Printf("error: not x11, gamma won't work\n");
return;
}
display = info.info.x11.display;
if (once) {
int size;
XF86VidModeGetGammaRampSize(display, screen, &size);
if (size != 256) {
Com_Printf("error: gamma size (%d) not supported, gamma wont work!\n", size);
once = 0;
return;
}
if (!XF86VidModeGetGammaRamp(display, screen, 256, sysramps, sysramps+256, sysramps+512)) {
Com_DPrintf("error: cannot get system gamma ramps, gamma won't work\n");
once = 0;
return;
}
once = 0;
gamma_works = 1;
}
if (gamma_works) {
/* It returns true unconditionally ... */
XF86VidModeSetGammaRamp(display, screen, 256, ramps, ramps+256, ramps+512);
vid_hwgamma_enabled = true;
}
return;
#else
SDL_SetWindowGammaRamp(sdl_window, ramps, ramps+256,ramps+512);
#endif
vid_hwgamma_enabled = true;
}
#ifdef __linux__
static void VID_RestoreSystemGamma(void)
{
VID_SetDeviceGammaRampReal(sysramps);
}
#endif
static void window_event(SDL_WindowEvent *event)
{
extern qbool scr_skipupdate;
int flags = SDL_GetWindowFlags(sdl_window);
switch (event->event) {
case SDL_WINDOWEVENT_MINIMIZED:
Minimized = true;
case SDL_WINDOWEVENT_FOCUS_LOST:
ActiveApp = false;
#ifdef __linux__
if (Minimized || vid_hwgammacontrol.integer != 3) {
VID_RestoreSystemGamma();
}
#endif
#ifdef _WIN32
Sys_ActiveAppChanged ();
#endif
break;
case SDL_WINDOWEVENT_FOCUS_GAINED:
TP_ExecTrigger("f_focusgained");
/* Fall through */
case SDL_WINDOWEVENT_RESTORED:
Minimized = false;
ActiveApp = true;
scr_skipupdate = 0;
#ifdef __linux__
v_gamma.modified = true;
#endif
#ifdef _WIN32
Sys_ActiveAppChanged ();
#endif
break;
case SDL_WINDOWEVENT_MOVED:
if (!(flags & SDL_WINDOW_FULLSCREEN) && r_win_save_pos.integer) {
int displayNumber = 0;
int x = event->data1;
int y = event->data2;
VID_RelativePositionFromAbsolute(&x, &y, &displayNumber);
Cvar_SetValue(&vid_win_displayNumber, displayNumber);
Cvar_SetValue(&vid_xpos, x);
Cvar_SetValue(&vid_ypos, y);
}
break;
case SDL_WINDOWEVENT_RESIZED:
if (!(flags & SDL_WINDOW_FULLSCREEN)) {
glConfig.vidWidth = event->data1;
glConfig.vidHeight = event->data2;
if (r_win_save_size.integer) {
Cvar_LatchedSetValue(&vid_win_width, event->data1);
Cvar_LatchedSetValue(&vid_win_height, event->data2);
}
if (!r_conwidth.integer || !r_conheight.integer)
VID_UpdateConRes();
}
break;
}
}
// FIXME: APPLE K_F13-15 etc...
static const byte scantokey[128] = {
// 0 1 2 3 4 5 6 7
// 8 9 A B C D E F
0, 0, 0, 0, 'a', 'b', 'c', 'd', // 0
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', // 1
'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
'3', '4', '5', '6', '7', '8', '9', '0', // 2
K_ENTER, K_ESCAPE, K_BACKSPACE, K_TAB, K_SPACE, '-', '=', '[',
']', '\\', 0, ';', '\'', '`', ',', '.', // 3
'/' , K_CAPSLOCK, K_F1, K_F2, K_F3, K_F4, K_F5, K_F6,
K_F7, K_F8, K_F9, K_F10, K_F11, K_F12, K_PRINTSCR, K_SCRLCK, // 4
K_PAUSE, K_INS, K_HOME, K_PGUP, K_DEL, K_END, K_PGDN, K_RIGHTARROW,
K_LEFTARROW, K_DOWNARROW, K_UPARROW, KP_NUMLOCK, KP_SLASH, KP_STAR, KP_MINUS, KP_PLUS, // 5
KP_ENTER, KP_END, KP_DOWNARROW, KP_PGDN, KP_LEFTARROW, KP_5, KP_RIGHTARROW, KP_HOME,
KP_UPARROW, KP_PGUP, KP_INS, KP_DEL, K_ISO, K_MENU, 0, 0, // 6
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, K_MENU, 0, // 7
#ifdef __APPLE__
K_LCTRL, K_LSHIFT, K_LALT, K_CMD, K_RCTRL, K_RSHIFT, K_RALT, K_CMD, // E
#else
K_LCTRL, K_LSHIFT, K_LALT, K_LWIN, K_RCTRL, K_RSHIFT, K_RALT, K_RWIN, // E
#endif
};
byte Key_ScancodeToQuakeCode(int scancode)
{
byte quakeCode = 0;
if (scancode < 120)
quakeCode = scantokey[scancode];
else if (scancode >= 224 && scancode < 224 + 8)
quakeCode = scantokey[scancode - 104];
if (!cl_keypad.integer) {
// compatibility mode without knowledge about keypad-keys:
switch (quakeCode)
{
case KP_NUMLOCK: quakeCode = K_PAUSE; break;
case KP_SLASH: quakeCode = '/'; break;
case KP_STAR: quakeCode = '*'; break;
case KP_MINUS: quakeCode = '-'; break;
case KP_HOME: quakeCode = K_HOME; break;
case KP_UPARROW: quakeCode = K_UPARROW; break;
case KP_PGUP: quakeCode = K_PGUP; break;
case KP_LEFTARROW: quakeCode = K_LEFTARROW; break;
case KP_5: quakeCode = '5'; break;
case KP_RIGHTARROW: quakeCode = K_RIGHTARROW; break;
case KP_PLUS: quakeCode = '+'; break;
case KP_END: quakeCode = K_END; break;
case KP_DOWNARROW: quakeCode = K_DOWNARROW; break;
case KP_PGDN: quakeCode = K_PGDN; break;
case KP_INS: quakeCode = K_INS; break;
case KP_DEL: quakeCode = K_DEL; break;
case KP_ENTER: quakeCode = K_ENTER; break;
default: break;
}
}
return quakeCode;
}
byte Key_CharacterToQuakeCode(char ch)
{
// Uses fact that SDLK_a == 'a'... is this okay?
// Convert from key-code to scan-code to see what physical button they pressed
int scancode = SDL_GetScancodeFromKey(ch);
return Key_ScancodeToQuakeCode(scancode);
}
wchar Key_Event_TextInput(wchar unichar);
static void keyb_textinputevent(char* text)
{
int i = 0;
int len = 0;
wchar unichar = 0;
// Only process text input messages here
if (key_dest != key_console && key_dest != key_message)
return;
if (!*text)
return;
len = strlen(text);
for (i = 0; i < len; ++i)
{
unichar = TextEncodingDecodeUTF8(text, &i);
if (unichar)
Key_Event_TextInput(unichar);
}
}
static void keyb_event(SDL_KeyboardEvent *event)
{
byte result = Key_ScancodeToQuakeCode(event->keysym.scancode);
if (result == 0) {
Com_DPrintf("%s: unknown scancode %d\n", __func__, event->keysym.scancode);
return;
}
Key_Event(result, event->state);
}
static void mouse_button_event(SDL_MouseButtonEvent *event)
{
unsigned key;
switch (event->button) {
case SDL_BUTTON_LEFT:
key = K_MOUSE1;
break;
case SDL_BUTTON_RIGHT:
key = K_MOUSE2;
break;
case SDL_BUTTON_MIDDLE:
key = K_MOUSE3;
break;
case 8:
case SDL_BUTTON_X1:
key = K_MOUSE4;
break;
case 9:
case SDL_BUTTON_X2:
key = K_MOUSE5;
break;
default:
Com_DPrintf("%s: unknown button %d\n", __func__, event->button);
return;
}
Key_Event(key, event->state);
}
static void mouse_wheel_event(SDL_MouseWheelEvent *event)
{
if (event->y > 0) {
Key_Event(K_MWHEELUP, true);
Key_Event(K_MWHEELUP, false);
} else if (event->y < 0) {
Key_Event(K_MWHEELDOWN, true);
Key_Event(K_MWHEELDOWN, false);
}
}
static void HandleEvents(void)
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
Sys_Quit();
break;
case SDL_WINDOWEVENT:
window_event(&event.window);
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
keyb_event(&event.key);
break;
case SDL_TEXTINPUT:
keyb_textinputevent(event.text.text);
break;
case SDL_MOUSEMOTION:
if (mouse_active && !SDL_GetRelativeMouseMode()) {
mx = old_x - event.motion.x;
my = old_y - event.motion.y;
old_x = event.motion.x;
old_y = event.motion.y;
cursor_x = min(max(0, cursor_x + event.motion.x - glConfig.vidWidth / 2), glConfig.vidWidth);
cursor_y = min(max(0, cursor_y + event.motion.y - glConfig.vidHeight / 2), glConfig.vidHeight);
SDL_WarpMouseInWindow(sdl_window, glConfig.vidWidth / 2, glConfig.vidHeight / 2);
}
else {
cursor_x = event.motion.x;
cursor_y = event.motion.y;
}
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
mouse_button_event(&event.button);
break;
case SDL_MOUSEWHEEL:
mouse_wheel_event(&event.wheel);
break;
case SDL_DROPFILE:
/* TODO: Add handling for different file types */
Cbuf_AddText("playdemo ");
Cbuf_AddText(event.drop.file);
Cbuf_AddText("\n");
SDL_free(event.drop.file);
break;
}
}
}
/*****************************************************************************/
void VID_Shutdown(void)
{
IN_DeactivateMouse();
SDL_StopTextInput();
#ifdef __linux__
VID_RestoreSystemGamma();
#endif
if (sdl_context) {
SDL_GL_DeleteContext(sdl_context);
sdl_context = NULL;
}
if (sdl_window) {
SDL_DestroyWindow(sdl_window);
sdl_window = NULL;
}
if (SDL_WasInit(SDL_INIT_VIDEO) != 0)
SDL_QuitSubSystem(SDL_INIT_VIDEO);
memset(&glConfig, 0, sizeof(glConfig));
Q_free(modelist);
modelist_count = 0;
vid_hwgamma_enabled = false;
vid_initialized = false;
}
static int VID_SDL_InitSubSystem(void)
{
if (SDL_WasInit(SDL_INIT_VIDEO) == 0) {
if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
Sys_Error("Couldn't initialize SDL video: %s\n", SDL_GetError());
return -1;
}
}
SDL_StartTextInput();
return 0;
}
void VID_RegisterLatchCvars(void)
{
Cvar_SetCurrentGroup(CVAR_GROUP_VIDEO);
Cvar_Register(&vid_width);
Cvar_Register(&vid_height);
Cvar_Register(&vid_win_width);
Cvar_Register(&vid_win_height);
Cvar_Register(&vid_hwgammacontrol);
Cvar_Register(&r_colorbits);
Cvar_Register(&r_24bit_depth);
Cvar_Register(&r_fullscreen);
Cvar_Register(&r_displayRefresh);
Cvar_Register(&vid_usedesktopres);
Cvar_Register(&vid_win_borderless);
Cvar_Register(&gl_multisamples);
Cvar_Register(&vid_displayNumber);
Cvar_ResetCurrentGroup();
}
void VID_RegisterCvars(void)
{
Cvar_SetCurrentGroup(CVAR_GROUP_VIDEO);
Cvar_Register(&vid_vsync_lag_fix);
Cvar_Register(&vid_vsync_lag_tweak);
Cvar_Register(&r_win_save_pos);
Cvar_Register(&r_win_save_size);
Cvar_Register(&r_swapInterval);
Cvar_Register(&r_verbose);
Cvar_Register(&vid_xpos);
Cvar_Register(&vid_ypos);
Cvar_Register(&r_conwidth);
Cvar_Register(&r_conheight);
Cvar_Register(&r_conscale);
Cvar_Register(&vid_flashonactivity);
Cvar_Register(&r_showextensions);
Cvar_Register(&vid_win_displayNumber);
Cvar_ResetCurrentGroup();
}
// Returns valid display number
static int VID_DisplayNumber(qbool fullscreen)
{
int displayNumber = (fullscreen ? vid_displayNumber.value : vid_win_displayNumber.value);
int displays = SDL_GetNumVideoDisplays();
return max(0, min(displays - 1, displayNumber));
}
static void VID_SetupModeList(void)
{
int i;
modelist_count = SDL_GetNumDisplayModes(VID_DisplayNumber(r_fullscreen.integer == 1));
if (modelist_count <= 0) {
Com_Printf("error getting display modes: %s\n", SDL_GetError());
modelist_count = 0;
}
modelist = Q_calloc(modelist_count, sizeof(*modelist));
for (i = 0; i < modelist_count; i++) {
SDL_GetDisplayMode(0, i, &modelist[i]);
}
}
static void VID_SetupResolution(void)
{
SDL_DisplayMode display_mode;
int display_nbr;
if (r_fullscreen.integer == 1) {
display_nbr = VID_DisplayNumber(true);
if (vid_usedesktopres.integer == 1) {
if (SDL_GetDesktopDisplayMode(display_nbr, &display_mode) == 0) {
glConfig.vidWidth = last_working_width = display_mode.w;
glConfig.vidHeight = last_working_height = display_mode.h;
glConfig.displayFrequency = last_working_hz = display_mode.refresh_rate;
last_working_display = display_nbr;
last_working_values = true;
Cvar_AutoSetInt(&vid_width, display_mode.w);
Cvar_AutoSetInt(&vid_height, display_mode.h);
Cvar_AutoSetInt(&r_displayRefresh, display_mode.refresh_rate);
return;
} else {
Com_Printf("warning: failed to get desktop resolution\n");
}
}
/* Note the fall through in case the above fails */
if (vid_width.integer == 0 || vid_height.integer == 0) {
/* Try some default if nothing is set and we failed to get desktop res */
glConfig.vidWidth = 1024;
glConfig.vidHeight = 768;
glConfig.displayFrequency = 0;
Cvar_LatchedSetValue(&vid_width, 1024);
Cvar_LatchedSetValue(&vid_height, 768);
Cvar_LatchedSetValue(&r_displayRefresh, 0);
return;
}
/* USER specified resolution */
glConfig.vidWidth = bound(320, vid_width.integer, vid_width.integer);
glConfig.vidHeight = bound(200, vid_height.integer, vid_height.integer);
glConfig.displayFrequency = r_displayRefresh.integer;
} else { /* Windowed mode */
if (vid_win_width.integer == 0 || vid_win_height.integer == 0) {
Cvar_LatchedSetValue(&vid_win_width, 640);
Cvar_LatchedSetValue(&vid_win_height, 480);
Cvar_LatchedSetValue(&r_displayRefresh, 0);
}
glConfig.vidWidth = bound(320, vid_win_width.integer, vid_win_width.integer);
glConfig.vidHeight = bound(240, vid_win_height.integer, vid_win_height.integer);
glConfig.displayFrequency = 0;
}
}
int VID_GetCurrentModeIndex(void)
{
int i;
int best_freq = 0;
int best_idx = -1;
for (i = 0; i < modelist_count; i++) {
if (modelist[i].w == vid_width.integer && modelist[i].h == vid_height.integer) {
if (modelist[i].refresh_rate == r_displayRefresh.integer) {
Com_DPrintf("MATCHED: %dx%d hz:%d\n", modelist[i].w, modelist[i].h, modelist[i].refresh_rate);
return i;
}
if (modelist[i].refresh_rate > best_freq) {
best_freq = modelist[i].refresh_rate;
best_idx = i;
}
}
}
/* width/height matched but not hz, using the best available */
if (best_idx >= 0) {
Cvar_AutoSetInt(&r_displayRefresh, modelist[best_idx].refresh_rate);
}
return best_idx;
}
int VID_GetModeIndexCount(void) {
return modelist_count;
}
const SDL_DisplayMode *VID_GetDisplayMode(int index)
{
if (index < 0 || index >= modelist_count) {
return NULL;
}
return &modelist[index];
}
static void VID_SDL_GL_SetupAttributes(void)
{
if (gl_multisamples.integer > 0) {
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, bound(0, gl_multisamples.integer, 16));
}
if (r_24bit_depth.integer == 1) {
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
} /* else, SDL2 defaults to 16 */
}
static int VID_SetWindowIcon(SDL_Window *sdl_window)
{
#ifdef __APPLE__
// on OS X the icon is handled by the app bundle
// it actually is higher resolution than the one here.
return 0;
#else
SDL_Surface *icon_surface;
icon_surface = SDL_CreateRGBSurfaceFrom((void *)ezquake_icon.pixel_data, ezquake_icon.width, ezquake_icon.height, ezquake_icon.bytes_per_pixel * 8,
ezquake_icon.width * ezquake_icon.bytes_per_pixel,
0x000000FF,0x0000FF00,0x00FF0000,0xFF000000);
if (icon_surface) {
SDL_SetWindowIcon(sdl_window, icon_surface);
SDL_FreeSurface(icon_surface);
return 0;
}
return -1;
#endif
}
static void VID_SDL_Init(void)
{
int flags;
int r, g, b, a;
if (glConfig.initialized == true) {
return;
}
flags = SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_SHOWN;
#ifdef SDL_WINDOW_ALLOW_HIGHDPI
flags |= SDL_WINDOW_ALLOW_HIGHDPI;
#endif
if (r_fullscreen.integer > 0) {
if (vid_usedesktopres.integer == 1) {
flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
}
} else {
if (vid_win_borderless.integer > 0) {
flags |= SDL_WINDOW_BORDERLESS;
}
}
#ifdef __APPLE__
SDL_SetHint(SDL_HINT_VIDEO_MAC_FULLSCREEN_SPACES, "0");
SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
#endif
#if defined(__linux__) || defined(__FreeBSD__)
SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
#endif
SDL_SetHintWithPriority(SDL_HINT_MOUSE_RELATIVE_MODE_WARP, "0", SDL_HINT_OVERRIDE);
VID_SDL_InitSubSystem();
VID_SDL_GL_SetupAttributes();
VID_SetupModeList();
VID_SetupResolution();
if (r_fullscreen.integer == 0) {
int displayNumber = VID_DisplayNumber(false);
int xpos = vid_xpos.integer;
int ypos = vid_ypos.integer;
VID_AbsolutePositionFromRelative(&xpos, &ypos, &displayNumber);
sdl_window = SDL_CreateWindow(WINDOW_CLASS_NAME, xpos, ypos, glConfig.vidWidth, glConfig.vidHeight, flags);
} else {
int windowWidth = glConfig.vidWidth;
int windowHeight = glConfig.vidHeight;
int windowX = SDL_WINDOWPOS_CENTERED;
int windowY = SDL_WINDOWPOS_CENTERED;
int displayNumber = VID_DisplayNumber(true);
SDL_Rect bounds;
if (SDL_GetDisplayBounds(displayNumber, &bounds) == 0)
{
windowX = bounds.x;
windowY = bounds.y;
windowWidth = bounds.w;
windowHeight = bounds.h;
}
else
{
Com_Printf("Couldn't determine bounds of display #%d, defaulting to main display\n", displayNumber);
}
sdl_window = SDL_CreateWindow(WINDOW_CLASS_NAME, windowX, windowY, windowWidth, windowHeight, flags);
}