comparison src/video/SDL_video.c @ 1734:f7c667ded87d SDL-1.3

The general code handles restoring video mode/gamma/etc. when the window loses focus. Support for changing fullscreen/windowed mode in progress.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 09 Jul 2006 18:09:16 +0000
parents 0b1070f2f94d
children 8dd28c4ef746
comparison
equal deleted inserted replaced
1733:0b1070f2f94d 1734:f7c667ded87d
710 SDL_WINDOW_BORDERLESS | 710 SDL_WINDOW_BORDERLESS |
711 SDL_WINDOW_RESIZABLE | 711 SDL_WINDOW_RESIZABLE |
712 SDL_WINDOW_MAXIMIZED | 712 SDL_WINDOW_MAXIMIZED |
713 SDL_WINDOW_MINIMIZED | 713 SDL_WINDOW_MINIMIZED |
714 SDL_WINDOW_INPUT_GRABBED); 714 SDL_WINDOW_INPUT_GRABBED);
715 SDL_VideoDisplay *display;
715 SDL_Window window; 716 SDL_Window window;
716 int num_windows; 717 int num_windows;
717 SDL_Window *windows; 718 SDL_Window *windows;
718 719
719 if (!_this) { 720 if (!_this) {
722 } 723 }
723 724
724 SDL_zero(window); 725 SDL_zero(window);
725 window.id = _this->next_object_id++; 726 window.id = _this->next_object_id++;
726 window.title = title ? SDL_strdup(title) : NULL; 727 window.title = title ? SDL_strdup(title) : NULL;
727 if (flags & SDL_WINDOW_FULLSCREEN) { 728 window.x = x;
728 const SDL_DisplayMode *mode = &SDL_CurrentDisplay.current_mode; 729 window.y = y;
729 window.x = (mode->w - w) / 2;
730 window.y = (mode->h - h) / 2;
731 } else {
732 window.x = x;
733 window.y = y;
734 }
735 window.w = w; 730 window.w = w;
736 window.h = h; 731 window.h = h;
737 window.flags = (flags & allowed_flags); 732 window.flags = (flags & allowed_flags);
738 window.display = _this->current_display; 733 window.display = _this->current_display;
739 734
742 SDL_free(window.title); 737 SDL_free(window.title);
743 } 738 }
744 return 0; 739 return 0;
745 } 740 }
746 741
747 num_windows = SDL_CurrentDisplay.num_windows; 742 display = &SDL_CurrentDisplay;
743 num_windows = display->num_windows;
748 windows = 744 windows =
749 SDL_realloc(SDL_CurrentDisplay.windows, 745 SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
750 (num_windows + 1) * sizeof(*windows));
751 if (!windows) { 746 if (!windows) {
752 if (_this->DestroyWindow) { 747 if (_this->DestroyWindow) {
753 _this->DestroyWindow(_this, &window); 748 _this->DestroyWindow(_this, &window);
754 } 749 }
755 if (window.title) { 750 if (window.title) {
756 SDL_free(window.title); 751 SDL_free(window.title);
757 } 752 }
758 return 0; 753 return 0;
759 } 754 }
760 windows[num_windows] = window; 755 windows[num_windows] = window;
761 SDL_CurrentDisplay.windows = windows; 756 display->windows = windows;
762 SDL_CurrentDisplay.num_windows++; 757 display->num_windows++;
758
759 if (FULLSCREEN_VISIBLE(&window)) {
760 /* Hide any other fullscreen windows */
761 int i;
762 for (i = 0; i < display->num_windows; ++i) {
763 SDL_Window *other = &display->windows[i];
764 if (other->id != window.id && FULLSCREEN_VISIBLE(other)) {
765 SDL_MinimizeWindow(other->id);
766 }
767 }
768 SDL_SetDisplayMode(display->fullscreen_mode);
769 }
763 770
764 return window.id; 771 return window.id;
765 } 772 }
766 773
767 SDL_WindowID 774 SDL_WindowID
768 SDL_CreateWindowFrom(const void *data) 775 SDL_CreateWindowFrom(const void *data)
769 { 776 {
777 SDL_VideoDisplay *display;
770 SDL_Window window; 778 SDL_Window window;
771 int num_windows; 779 int num_windows;
772 SDL_Window *windows; 780 SDL_Window *windows;
773 781
774 if (!_this) { 782 if (!_this) {
783 if (!_this->CreateWindowFrom || 791 if (!_this->CreateWindowFrom ||
784 _this->CreateWindowFrom(_this, &window, data) < 0) { 792 _this->CreateWindowFrom(_this, &window, data) < 0) {
785 return 0; 793 return 0;
786 } 794 }
787 795
788 num_windows = SDL_CurrentDisplay.num_windows; 796 display = &SDL_CurrentDisplay;
797 num_windows = display->num_windows;
789 windows = 798 windows =
790 SDL_realloc(SDL_CurrentDisplay.windows, 799 SDL_realloc(display->windows, (num_windows + 1) * sizeof(*windows));
791 (num_windows + 1) * sizeof(*windows));
792 if (!windows) { 800 if (!windows) {
793 if (_this->DestroyWindow) { 801 if (_this->DestroyWindow) {
794 _this->DestroyWindow(_this, &window); 802 _this->DestroyWindow(_this, &window);
795 } 803 }
796 if (window.title) { 804 if (window.title) {
797 SDL_free(window.title); 805 SDL_free(window.title);
798 } 806 }
799 return 0; 807 return 0;
800 } 808 }
801 windows[num_windows] = window; 809 windows[num_windows] = window;
802 SDL_CurrentDisplay.windows = windows; 810 display->windows = windows;
803 SDL_CurrentDisplay.num_windows++; 811 display->num_windows++;
804 812
805 return window.id; 813 return window.id;
806 } 814 }
807 815
808 SDL_Window * 816 SDL_Window *
1060 if (_this->RestoreWindow) { 1068 if (_this->RestoreWindow) {
1061 _this->RestoreWindow(_this, window); 1069 _this->RestoreWindow(_this, window);
1062 } 1070 }
1063 } 1071 }
1064 1072
1073 int
1074 SDL_SetWindowFullscreen(SDL_WindowID windowID, int fullscreen)
1075 {
1076 SDL_Window *window = SDL_GetWindowFromID(windowID);
1077
1078 if (!window) {
1079 return -1;
1080 }
1081
1082 if (fullscreen) {
1083 fullscreen = SDL_WINDOW_FULLSCREEN;
1084 }
1085 if ((window->flags & SDL_WINDOW_FULLSCREEN) == fullscreen) {
1086 return 0;
1087 }
1088
1089 if (fullscreen) {
1090 window->flags |= SDL_WINDOW_FULLSCREEN;
1091
1092 if (FULLSCREEN_VISIBLE(window)) {
1093 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
1094
1095 /* Hide any other fullscreen windows */
1096 int i;
1097 for (i = 0; i < display->num_windows; ++i) {
1098 SDL_Window *other = &display->windows[i];
1099 if (other->id != windowID && FULLSCREEN_VISIBLE(other)) {
1100 SDL_MinimizeWindow(other->id);
1101 }
1102 }
1103
1104 SDL_SetDisplayMode(display->fullscreen_mode);
1105 }
1106 } else {
1107 window->flags &= ~SDL_WINDOW_FULLSCREEN;
1108
1109 if (FULLSCREEN_VISIBLE(window)) {
1110 SDL_SetDisplayMode(NULL);
1111 }
1112 }
1113 }
1114
1065 void 1115 void
1066 SDL_SetWindowGrab(SDL_WindowID windowID, int mode) 1116 SDL_SetWindowGrab(SDL_WindowID windowID, int mode)
1067 { 1117 {
1068 SDL_Window *window = SDL_GetWindowFromID(windowID); 1118 SDL_Window *window = SDL_GetWindowFromID(windowID);
1069 1119
1075 window->flags |= SDL_WINDOW_INPUT_GRABBED; 1125 window->flags |= SDL_WINDOW_INPUT_GRABBED;
1076 } else { 1126 } else {
1077 window->flags &= ~SDL_WINDOW_INPUT_GRABBED; 1127 window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
1078 } 1128 }
1079 1129
1080 if (_this->SetWindowGrab) { 1130 if ((window->flags & SDL_WINDOW_INPUT_FOCUS) && _this->SetWindowGrab) {
1081 _this->SetWindowGrab(_this, window); 1131 _this->SetWindowGrab(_this, window);
1082 } 1132 }
1083 } 1133 }
1084 1134
1085 int 1135 int
1113 SDL_SetDisplayMode(display->fullscreen_mode); 1163 SDL_SetDisplayMode(display->fullscreen_mode);
1114 } 1164 }
1115 if (display->gamma && _this->SetDisplayGammaRamp) { 1165 if (display->gamma && _this->SetDisplayGammaRamp) {
1116 _this->SetDisplayGammaRamp(_this, display->gamma); 1166 _this->SetDisplayGammaRamp(_this, display->gamma);
1117 } 1167 }
1168 if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
1169 _this->SetWindowGrab(_this, window);
1170 }
1118 } 1171 }
1119 1172
1120 void 1173 void
1121 SDL_OnWindowFocusLost(SDL_Window * window) 1174 SDL_OnWindowFocusLost(SDL_Window * window)
1122 { 1175 {
1123 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); 1176 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
1124 1177
1125 if (window->flags & SDL_WINDOW_FULLSCREEN) { 1178 if (window->flags & SDL_WINDOW_FULLSCREEN) {
1179 SDL_MinimizeWindow(window->id);
1126 SDL_SetDisplayMode(NULL); 1180 SDL_SetDisplayMode(NULL);
1127 SDL_MinimizeWindow(window->id);
1128 } 1181 }
1129 if (display->gamma && _this->SetDisplayGammaRamp) { 1182 if (display->gamma && _this->SetDisplayGammaRamp) {
1130 _this->SetDisplayGammaRamp(_this, display->saved_gamma); 1183 _this->SetDisplayGammaRamp(_this, display->saved_gamma);
1131 } 1184 }
1185 if ((window->flags & SDL_WINDOW_INPUT_GRABBED) && _this->SetWindowGrab) {
1186 _this->SetWindowGrab(_this, window);
1187 }
1188 }
1189
1190 SDL_WindowID
1191 SDL_GetFocusWindow(void)
1192 {
1193 SDL_VideoDisplay *display;
1194 int i;
1195
1196 if (!_this) {
1197 return 0;
1198 }
1199
1200 display = &SDL_CurrentDisplay;
1201 for (i = 0; i < display->num_windows; ++i) {
1202 SDL_Window *window = &display->windows[i];
1203
1204 if (window->flags & SDL_WINDOW_INPUT_FOCUS) {
1205 return window->id;
1206 }
1207 }
1208 return 0;
1132 } 1209 }
1133 1210
1134 void 1211 void
1135 SDL_DestroyWindow(SDL_WindowID windowID) 1212 SDL_DestroyWindow(SDL_WindowID windowID)
1136 { 1213 {
1137 int i, j; 1214 int i, j;
1138 1215
1139 if (!_this) { 1216 if (!_this) {
1140 return; 1217 return;
1141 } 1218 }
1219
1220 /* Restore video mode, etc. */
1221 SDL_SendWindowEvent(windowID, SDL_WINDOWEVENT_FOCUS_LOST, 0, 0);
1142 1222
1143 for (i = 0; i < _this->num_displays; ++i) { 1223 for (i = 0; i < _this->num_displays; ++i) {
1144 SDL_VideoDisplay *display = &_this->displays[i]; 1224 SDL_VideoDisplay *display = &_this->displays[i];
1145 for (j = 0; j < display->num_windows; ++j) { 1225 for (j = 0; j < display->num_windows; ++j) {
1146 SDL_Window *window = &display->windows[j]; 1226 SDL_Window *window = &display->windows[j];
1147 if (window->id != windowID) { 1227 if (window->id != windowID) {
1148 continue; 1228 continue;
1149 }
1150 if (window->flags & SDL_WINDOW_FULLSCREEN) {
1151 SDL_SetDisplayMode(NULL);
1152 }
1153 if (display->gamma && _this->SetDisplayGammaRamp) {
1154 _this->SetDisplayGammaRamp(_this, display->saved_gamma);
1155 }
1156 if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
1157 window->flags &= ~SDL_WINDOW_INPUT_GRABBED;
1158 _this->SetWindowGrab(_this, window);
1159 } 1229 }
1160 if (window->renderer) { 1230 if (window->renderer) {
1161 SDL_DestroyRenderer(window->id); 1231 SDL_DestroyRenderer(window->id);
1162 } 1232 }
1163 if (_this->DestroyWindow) { 1233 if (_this->DestroyWindow) {