<commit_before>-----------------------------------------------------------------------------
-- |
-- Module      :  Main.hs
-- Copyright   :  (c) <NAME> 2007
-- License     :  BSD3-style (see LICENSE)
-- 
-- Maintainer  :  <EMAIL>
-- Stability   :  unstable
-- Portability :  not portable, uses mtl, X11, posix
--
-----------------------------------------------------------------------------
--
-- thunk, a minimal window manager for X11
--

import Data.List
import Data.Bits hiding (rotate)
import qualified Data.Map as M

import System.IO
import System.Exit

import Graphics.X11.Xlib
import Graphics.X11.Xlib.Extras

import Control.Monad.State

import WMonad
import qualified StackSet as W

--
-- The number of workspaces:
--
workspaces :: Int
workspaces = 5

--
-- The keys list
--
keys :: M.Map (KeyMask, KeySym) (W ())
keys = M.fromList $
    [ ((mod1Mask .|. shiftMask, xK_Return), spawn "xterm")
    , ((mod1Mask,               xK_p     ), spawn "exe=`dmenu_path | dmenu` && exec $exe")
    , ((controlMask,            xK_space ), spawn "gmrun")
    , ((mod1Mask,               xK_Tab   ), focus GT)
    , ((mod1Mask,               xK_j     ), focus GT)
    , ((mod1Mask,               xK_k     ), focus LT)
    , ((mod1Mask .|. shiftMask, xK_c     ), kill)
    , ((mod1Mask .|. shiftMask, xK_q     ), io $ exitWith ExitSuccess)
    ] ++
    -- generate keybindings to each workspace:
    [((m .|. mod1Mask, xK_0 + fromIntegral i), f i)
        | i <- [1 .. workspaces]
        , (f, m) <- [(view, 0), (tag, shiftMask)]]

--
-- The main entry point
-- 
main :: IO ()
main = do
    dpy <- openDisplay ""
    let dflt      = defaultScreen dpy
        initState = WState
            { display      = dpy
            , screenWidth  = displayWidth  dpy dflt
            , screenHeight = displayHeight dpy dflt
            , workspace    = W.empty workspaces
            }

    runW initState $ do
        r <- io $ rootWindow dpy dflt
        io $ do selectInput dpy r (substructureRedirectMask .|. substructureNotifyMask)
                sync dpy False
        grabKeys dpy r
        (_, _, ws) <- io $ queryTree dpy r

        -- scan for initial windows
        forM_ ws $ \w -> do
            wa <- io $ getWindowAttributes dpy w
            when (not (waOverrideRedirect wa) && waMapState wa == waIsViewable)
                    (manage w)

        forever $ handle =<< io (allocaXEvent $ \ev ->
                                    nextEvent dpy ev >> getEvent ev)
    return ()
  where
    forever a = a >> forever a

-- | Grab the keys back
grabKeys :: Display -> Window -> W ()
grabKeys dpy r = forM_ (M.keys keys) $ \(m,s) -> io $ do
    kc <- keysymToKeycode dpy s
    grabKey dpy kc m r True grabModeAsync grabModeAsync

-- ---------------------------------------------------------------------
-- Event handler
--
-- | handle. Handle X events
--
-- Events dwm handles that we don't:
--
--    [ButtonPress]    = buttonpress,
--    [EnterNotify]    = enternotify,
--    [LeaveNotify]    = leavenotify,
--    [Expose]         = expose,
--    [PropertyNotify] = propertynotify,
-- 
handle :: Event -> W ()

handle (MapRequestEvent    {window = w}) = withDisplay $ \dpy -> do
    wa <- io $ getWindowAttributes dpy w
    when (not (waOverrideRedirect wa)) $ manage w

handle (DestroyWindowEvent {window = w}) = unmanage w
handle (UnmapEvent         {window = w}) = unmanage w

handle e@(MappingNotifyEvent {window = w}) = do<commit_msg>also select for enter and leave window events (need for XCrossing?)<commit_after>-----------------------------------------------------------------------------
-- |
-- Module      :  Main.hs
-- Copyright   :  (c) S<NAME> 2007
-- License     :  BSD3-style (see LICENSE)
-- 
-- Maintainer  :  <EMAIL>
-- Stability   :  unstable
-- Portability :  not portable, uses mtl, X11, posix
--
-----------------------------------------------------------------------------
--
-- thunk, a minimal window manager for X11
--

import Data.List
import Data.Bits hiding (rotate)
import qualified Data.Map as M

import System.IO
import System.Exit

import Graphics.X11.Xlib
import Graphics.X11.Xlib.Extras

import Control.Monad.State

import WMonad
import qualified StackSet as W

--
-- The number of workspaces:
--
workspaces :: Int
workspaces = 5

--
-- The keys list
--
keys :: M.Map (KeyMask, KeySym) (W ())
keys = M.fromList $
    [ ((mod1Mask .|. shiftMask, xK_Return), spawn "xterm")
    , ((mod1Mask,               xK_p     ), spawn "exe=`dmenu_path | dmenu` && exec $exe")
    , ((controlMask,            xK_space ), spawn "gmrun")
    , ((mod1Mask,               xK_Tab   ), focus GT)
    , ((mod1Mask,               xK_j     ), focus GT)
    , ((mod1Mask,               xK_k     ), focus LT)
    , ((mod1Mask .|. shiftMask, xK_c     ), kill)
    , ((mod1Mask .|. shiftMask, xK_q     ), io $ exitWith ExitSuccess)
    ] ++
    -- generate keybindings to each workspace:
    [((m .|. mod1Mask, xK_0 + fromIntegral i), f i)
        | i <- [1 .. workspaces]
        , (f, m) <- [(view, 0), (tag, shiftMask)]]

--
-- The main entry point
-- 
main :: IO ()
main = do
    dpy <- openDisplay ""
    let dflt      = defaultScreen dpy
        initState = WState
            { display      = dpy
            , screenWidth  = displayWidth  dpy dflt
            , screenHeight = displayHeight dpy dflt
            , workspace    = W.empty workspaces
            }

    runW initState $ do
        r <- io $ rootWindow dpy dflt
        io $ do selectInput dpy r $  substructureRedirectMask
                                     .|. substructureNotifyMask
                                     .|. enterWindowMask
                                     .|. leaveWindowMask

                sync dpy False
        grabKeys dpy r
        (_, _, ws) <- io $ queryTree dpy r

        -- scan for initial windows
        forM_ ws $ \w -> do
            wa <- io $ getWindowAttributes dpy w
            when (not (waOverrideRedirect wa) && waMapState wa == waIsViewable)
                    (manage w)

        forever $ handle =<< io (allocaXEvent $ \ev ->
                                    nextEvent dpy ev >> getEvent ev)
    return ()
  where
    forever a = a >> forever a

-- | Grab the keys back
grabKeys :: Display -> Window -> W ()
grabKeys dpy r = forM_ (M.keys keys) $ \(m,s) -> io $ do
    kc <- keysymToKeycode dpy s
    grabKey dpy kc m r True grabModeAsync grabModeAsync

-- ---------------------------------------------------------------------
-- Event handler
--
-- | handle. Handle X events
--
-- Events dwm handles that we don't:
--
--    [ButtonPress]    = buttonpress,
--    [EnterNotify]    = enternotify,
--    [LeaveNotify]    = leavenotify,
--    [Expose]         = expose,
--    [PropertyNotify] = propertynotify,
-- 
handle :: Event -> W ()

handle (MapRequestEvent    {window = w}) = withDisplay $ \dpy -> do
    wa <- io $ getWindowAttributes dpy w
    when (not (waOverrideRedirect wa)) $ manage w

-- XCreateWindowEvent(3X11)
-- Window manager clients normally should ignore this window if the
-- override_redirect member is True.

handle (DestroyWindowEvent {window = w}) = unmanage w
handle (UnmapEvent         {window = w}) = unmanage w

handle e@(MappingNotifyEvent {window = w}) = do
{'fraction_non_alphanumeric': 0.1769017980636238, 'fraction_numerical': 0.007330567081604426, 'mean_word_length': 1.5834226509467666, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4562158', 'n_tokens_mistral': 2249, 'n_tokens_neox': 2150, 'n_words': 660}