Skip to content

Instantly share code, notes, and snippets.

@cefstat
Created November 10, 2011 19:31
Show Gist options
  • Save cefstat/1355895 to your computer and use it in GitHub Desktop.
Save cefstat/1355895 to your computer and use it in GitHub Desktop.
Patch for Emacs fullscreen support under Mac OS X 10.7 (Lion)
=== modified file 'lisp/term/ns-win.el'
--- old/lisp/term/ns-win.el 2011-10-01 20:32:01 +0000
+++ new/lisp/term/ns-win.el 2011-11-10 16:22:51 +0000
@@ -928,6 +928,10 @@
(add-to-list 'frame-creation-function-alist '(ns . x-create-frame-with-faces))
(add-to-list 'window-system-initialization-alist '(ns . ns-initialize-window-system))
+(declare-function ns-toggle-fullscreen-internal "nsfns.m" ())
+(defun ns-toggle-fullscreen ()
+ (interactive)
+ (ns-toggle-fullscreen-internal))
(provide 'ns-win)
=== modified file 'src/nsfns.m'
--- old/src/nsfns.m 2011-11-05 12:25:01 +0000
+++ new/src/nsfns.m 2011-11-10 18:54:31 +0000
@@ -2567,6 +2567,22 @@
}
+DEFUN ("ns-toggle-fullscreen-internal", Fns_toggle_fullscreen_internal, Sns_toggle_fullscreen_internal,
+ 0, 0, 0,
+ doc: /* Toggle fulscreen mode */)
+()
+{
+ struct frame *f = SELECTED_FRAME();
+ EmacsWindow *window = ns_get_window(f);
+
+#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
+ [window toggleFullScreen:nil];
+#endif
+
+ return Qnil;
+}
+
+
/* ==========================================================================
Class implementations
@@ -2724,6 +2740,8 @@
defsubr (&Sx_show_tip);
defsubr (&Sx_hide_tip);
+ defsubr (&Sns_toggle_fullscreen_internal);
+
/* used only in fontset.c */
check_window_system_func = check_ns;
=== modified file 'src/nsmenu.m'
--- old/src/nsmenu.m 2011-07-29 05:31:12 +0000
+++ new/src/nsmenu.m 2011-11-10 16:22:51 +0000
@@ -986,7 +986,7 @@
-------------------------------------------------------------------------- */
{
BLOCK_INPUT;
- [[FRAME_NS_VIEW (f) toolbar] setVisible: NO];
+ [[FRAME_NS_VIEW (f) window] setToolbar: nil];
FRAME_TOOLBAR_HEIGHT (f) = 0;
UNBLOCK_INPUT;
}
@@ -1003,6 +1003,8 @@
EmacsToolbar *toolbar = [view toolbar];
BLOCK_INPUT;
+ [window setToolbar: toolbar];
+
[toolbar clearActive];
/* update EmacsToolbar as in GtkUtils, build items list */
=== modified file 'src/nsterm.h'
--- old/src/nsterm.h 2011-09-09 01:06:52 +0000
+++ new/src/nsterm.h 2011-11-10 18:39:22 +0000
@@ -38,6 +38,9 @@
#ifndef MAC_OS_X_VERSION_10_6
#define MAC_OS_X_VERSION_10_6 1060
#endif
+#ifndef MAC_OS_X_VERSION_10_7
+#define MAC_OS_X_VERSION_10_7 1070
+#endif
#endif /* NS_IMPL_COCOA */
#ifdef __OBJC__
=== modified file 'src/nsterm.m'
--- old/src/nsterm.m 2011-11-05 12:25:01 +0000
+++ new/src/nsterm.m 2011-11-10 18:54:02 +0000
@@ -5299,39 +5299,7 @@
#endif
if (rows < MINHEIGHT)
rows = MINHEIGHT;
-#ifdef NS_IMPL_COCOA
- {
- /* this sets window title to have size in it; the wm does this under GS */
- NSRect r = [[self window] frame];
- if (r.size.height == frameSize.height && r.size.width == frameSize.width)
- {
- if (old_title != 0)
- {
- xfree (old_title);
- old_title = 0;
- }
- }
- else
- {
- char *size_title;
- NSWindow *window = [self window];
- if (old_title == 0)
- {
- const char *t = [[[self window] title] UTF8String];
- char *pos = strstr (t, " — ");
- if (pos)
- *pos = '\0';
- old_title = (char *) xmalloc (strlen (t) + 1);
- strcpy (old_title, t);
- }
- size_title = xmalloc (strlen (old_title) + 40);
- esprintf (size_title, "%s — (%d x %d)", old_title, cols, rows);
- [window setTitle: [NSString stringWithUTF8String: size_title]];
- [window display];
- xfree (size_title);
- }
- }
-#endif /* NS_IMPL_COCOA */
+
/*fprintf (stderr," ...size became %.0f x %.0f (%d x %d)\n",frameSize.width,frameSize.height,cols,rows); */
return frameSize;
@@ -5353,14 +5321,6 @@
NSTRACE (windowDidResize);
/*fprintf (stderr,"windowDidResize: %.0f\n",[theWindow frame].size.height); */
-#ifdef NS_IMPL_COCOA
- if (old_title != 0)
- {
- xfree (old_title);
- old_title = 0;
- }
-#endif /* NS_IMPL_COCOA */
-
/* Avoid loop under GNUstep due to call at beginning of this function.
(x_set_window_size causes a resize which causes
a "windowDidResize" which calls x_set_window_size). */
@@ -5388,6 +5348,60 @@
ns_send_appdefined (-1);
}
+#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
+- (void)windowDidEnterFullScreen:(NSNotification *)notification
+{
+ NSTRACE (windowDidEnterFullScreen);
+ /* NSLog(@"Calling windowDidEnterFullScreen"); */
+
+ NSWindow *window = [self window];
+ NSRect wr = [window frame];
+ int w = (int)wr.size.width - emacsframe->border_width;
+ int h = (int)wr.size.height;
+ cols = FRAME_PIXEL_WIDTH_TO_TEXT_COLS(emacsframe, w);
+ rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES(emacsframe, h);
+ /* NSLog(@"window_size=%dx%d (%dx%d)", w, h, cols, rows); */
+ FRAME_PIXEL_WIDTH (emacsframe) = w;
+ FRAME_PIXEL_HEIGHT (emacsframe) = h;
+ change_frame_size (emacsframe, rows, cols, 0, 1, 0);
+ SET_FRAME_GARBAGED (emacsframe);
+ cancel_mouse_face (emacsframe);
+ ns_send_appdefined (-1);
+}
+#endif
+
+#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
+- (void)windowDidExitFullScreen:(NSNotification *)notification
+{
+ NSTRACE (windowDidExitFullScreen);
+ /* NSLog(@"Calling windowDidExitFullScreen"); */
+
+ NSWindow *window = [self window];
+ NSRect wr = [window frame];
+ int w = (int)wr.size.width - emacsframe->border_width;
+ int h = (int)wr.size.height
+ - FRAME_NS_TITLEBAR_HEIGHT (emacsframe)
+ - FRAME_TOOLBAR_HEIGHT (emacsframe);
+ cols = FRAME_PIXEL_WIDTH_TO_TEXT_COLS(emacsframe, w);
+ rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES(emacsframe, h);
+ /* NSLog(@"window_size=%dx%d (%dx%d)", w, h, cols, rows); */
+ FRAME_PIXEL_WIDTH (emacsframe) = w;
+ FRAME_PIXEL_HEIGHT (emacsframe) = h;
+ change_frame_size (emacsframe, rows, cols, 0, 1, 0);
+ SET_FRAME_GARBAGED (emacsframe);
+ cancel_mouse_face (emacsframe);
+ ns_send_appdefined (-1);
+}
+#endif
+
+#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
+- (NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions
+{
+ /* NSLog(@"Calling window:willUseFullScreenPresentationOptions: %d", proposedOptions); */
+ return proposedOptions | NSApplicationPresentationAutoHideToolbar;
+}
+#endif
+
- (void)windowDidBecomeKey: (NSNotification *)notification
/* cf. x_detect_focus_change(), x_focus_changed(), x_new_focus_frame() */
@@ -5559,6 +5573,14 @@
[NSApp registerServicesMenuSendTypes: ns_send_types
returnTypes: nil];
+#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
+ if (NSApp != nil)
+ {
+ [NSApp setPresentationOptions: NSApplicationPresentationFullScreen | [NSApp presentationOptions] ];
+ [win setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary | [win collectionBehavior] ];
+ }
+#endif
+
ns_window_num++;
return self;
}
@Jhering
Copy link

Jhering commented Dec 31, 2011

Thank you first.
And i found one patch which very famlilar with your's, here they are:
http://debbugs.gnu.org/cgi/bugreport.cgi?bug=10014

Two methods of patch both have some codes. What relationship between these two patch? Which one should i select to patch my Emacs 24.0.92?
Can your patch here patch Emacs 24.0.92?

@cefstat
Copy link
Author

cefstat commented Jan 2, 2012

The two patches you found are the same. I don't know if the patches apply cleanly to 24.0.92. Can you try and let me know the result?

@Jhering
Copy link

Jhering commented Jan 2, 2012

i have diffed them, there are some differences between them obviously. You can try it and fonud it.
And i patched my 24.0.92 using http://debbugs.gnu.org/cgi/bugreport.cgi?bug=10014
It works well. I have a real Lion-featured Fullscreen.
There is only one thing need to improve:
There is still a blank area of some pixels(5 to 10 width and modeline's height in emacs' background color) in the right side of mode line. That is the mode-line can not extend to the right edge of the screen when in fullscreen.

@cefstat
Copy link
Author

cefstat commented Jan 2, 2012

It is good to know that the patch works with 24.0.92! I also see the blank area you mention but I don't know how to change this. And to be honest I am not terribly motivated to do so since I just use Emacs with a white background and it is almost impossible to see this blank area (BTW, isn't this blank area also visible when the window is not maximized?)

Concerning the difference between the two patches, I have also written http://debbugs.gnu.org/cgi/bugreport.cgi?bug=10014. The differences between the patches that you see are just differences in the patch format. The one here is created so that it can be used by patch -p1 while the other one is the format preferred by emacs maintainers. The actual content of the two patches is identical.

@leoncamel
Copy link

First, thank you. This patch works great on my Lion box. But, I still have a small suggestion about this patch. Just like "(set-frame-paramter nil 'fullscreen 'fullscreen)" on Linux. Why not hooking ns_toggle_fullscreen to x_set_fullscreen, which is the same interface as in X-windows like system.

I think, there is no reason to add one more platform-specific elisp function. Right?

@cefstat
Copy link
Author

cefstat commented Jan 4, 2012

I completely agree with not using a platform specific function. Actually, as far as I can understand this is a necessary (but probably not sufficient) requirement for the patch to be accepted upstream. I have already looked into this but this is a rather busy period for me and it make take some time before I have something to show.

@Jhering
Copy link

Jhering commented Jan 9, 2012

New bug report:
1.switch to Fullscreen, and it's works fine, perfect.
2.if you M-x evel-buffer .emacs
3.bug: There are a blank of 4 or 5 pixels on the right edge from up to down,and a blank of 1 or 2 pixels on the bottom of screen from left to right.Now it's still in Fullscreen.
---------> http://www.flickr.com/photos/73886905@N05/6663944237/
---------> http://www.flickr.com/photos/73886905@N05/6663908337/
4.quit Fullscreen.
5.bug: Minibuffer disappears!!!
---------> http://www.flickr.com/photos/73886905@N05/6663908531/
6.go to Fullscreen again.
7.bug:Minibuffer still doesn't come out!!! Then whatever how many times you quit and enter Fullscreen, minibuffer has gone, dead,she left me.
---------> http://www.flickr.com/photos/73886905@N05/6663908759/

@cefstat
Copy link
Author

cefstat commented Jan 9, 2012

Thanks for the bug report. I can confirm that things behave the way you describe. I will have to take a more careful look into the frame size computations. On my system I have tracked the bugs you describe to the line (tool-bar-mode 0) in my .emacs. If you remove this then things will improve. In order to not have a toolbar you can set the frame parameter tool-bar-lines to 0 instead of calling (tool-bar-mode 0).

@Jhering
Copy link

Jhering commented Jan 10, 2012

remove (tool-bar-mode 0)
set tool-bar-lines to 0
Minibuffer will not disappear, but Tool-bar is still on the top of the frame. It can hide automaticly when in Fullscreen.
Tool-bar is so ugly, i don't like it.

@darren
Copy link

darren commented May 12, 2012

To make emacs 24.1.50 and Lion(10.7.4) full screen work, in the following hunk

+#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7

  • if (NSApp != nil && [NSApp isActive])
  • {
  • [NSApp setPresentationOptions: NSApplicationPresentationFullScreen | [NSApp presentationOptions] ];
  • [win setCollectionBehavior: NSWindowCollectionBehaviorFullScreenPrimary | [win collectionBehavior] ];
  • }
    +#endif

[NSApp isActive] should be removed from the condition. I do not know anything about cocoa, So I just don't
know why. The only thing I know is that your patch does not with latest emacs trunk and latest lion.

@cefstat
Copy link
Author

cefstat commented Jun 7, 2012

Darren, thank you for reporting this. I applied the patch to the Emacs 24.1 release candidate and things seemed to work - with one exception. If I launch Emacs and immediately switch to another application so that Emacs is not active when it runs the code above then fullscreen support is not activated. Let me know if you have any more problems.

@cefstat
Copy link
Author

cefstat commented Jun 7, 2012

I forgot to mention: I modified the gist so that now there is no check whether Emacs is active.

@renard
Copy link

renard commented Sep 20, 2012

In order to make this patch work correctly you should change the windowDidEnterFullScreen function
with that hunk:

+#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
+- (void)windowDidEnterFullScreen:(NSNotification *)notification
+{
+    NSTRACE (windowDidEnterFullScreen);
+    /* NSLog(@"Calling windowDidEnterFullScreen"); */
+    NSWindow *window = [self window];
+    NSRect wr = [window frame];
+    int w = (int)wr.size.width - emacsframe->border_width;
+    int h = (int)wr.size.height;
+    NSView *view = FRAME_NS_VIEW (emacsframe);
+    cols = FRAME_PIXEL_WIDTH_TO_TEXT_COLS(emacsframe, w);
+    rows = FRAME_PIXEL_HEIGHT_TO_TEXT_LINES(emacsframe, h);
+    /* NSLog(@"window_size=%dx%d (%dx%d)", w, h, cols, rows); */
+    FRAME_PIXEL_WIDTH (emacsframe) = w;
+    FRAME_PIXEL_HEIGHT (emacsframe) = h;
+    change_frame_size (emacsframe, rows, cols, 0, 1, 0);
+    SET_FRAME_GARBAGED (emacsframe);
+    cancel_mouse_face (emacsframe);
+    [view setFrame: NSMakeRect (0, 0, w, h)];
+    ns_send_appdefined (-1);
+}
+#endif

You need that because now the updateFrameSize function redraws the window frame and the top of the fullscreen window is not used (this size is the toolbar size) and the minibuffer is then on half drawn.

@renard
Copy link

renard commented Sep 28, 2012

For information a working patch and build setup s available at https://github.com/renard/emacs-build-macosx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment